Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed May 16, 2024
1 parent a2a22aa commit b327a00
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion core/src/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function BMI.get_value_ptr(model::Model, name::AbstractString)::AbstractVector{F
elseif name == "user_demand.demand"
vec(model.integrator.p.user_demand.demand)
elseif name == "user_demand.realized"
model.saved.stored_for_bmi.integrand.user_realized
model.saved.integrated_bmi_data.user_realized
else
error("Unknown variable $name")
end
Expand Down
17 changes: 11 additions & 6 deletions core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ function TrapezoidIntegrationCallback(integrand_func!, integral_value)::Discrete
cache,
integral_value,
)
return DiscreteCallback((u, t, integrator) -> t != 0, affect!)
return DiscreteCallback(
(u, t, integrator) -> t != 0,
affect!;
save_positions = (false, false),
)
end

function (affect!::TrapezoidIntegrationAffect)(integrator)::Nothing
Expand All @@ -27,10 +31,11 @@ function (affect!::TrapezoidIntegrationAffect)(integrator)::Nothing
copyto!(integrand_value_prev, integrand_value)
integrand_func!(integrand_value, integrator.p)

cache += integrand_value_prev
cache += integrand_value
cache *= 0.5 * dt
integral += cache
cache .= integrand_value_prev
cache .+= integrand_value
cache .*= 0.5 * dt

integral .+= cache
return nothing
end

Expand Down Expand Up @@ -164,7 +169,7 @@ function save_flow(u, t, integrator)
flow_mean = copy(integrated_flow)
flow_mean ./= Δt
fill!(integrated_flow, 0.0)
inflow_mean, outflow_mean = compute_mean_inoutflows(flow_mean.flow, graph, basin)
inflow_mean, outflow_mean = compute_mean_inoutflows(flow_mean.flow, basin)
return SavedFlow(; flow = flow_mean, inflow = inflow_mean, outflow = outflow_mean)
end

Expand Down
11 changes: 5 additions & 6 deletions core/src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -763,26 +763,25 @@ end

function compute_mean_inoutflows(
flow_mean::AbstractVector,
graph::MetaGraph,
basin::Basin,
)::Tuple{Vector{Float64}, Vector{Float64}}
(; node_id) = basin
(; node_id, inflow_edges, outflow_edges) = basin

# Divide the flows over edges to Basin inflow and outflow, regardless of edge direction.
inflow_mean = zeros(length(node_id))
outflow_mean = zeros(length(node_id))

for (i, basin_id) in enumerate(node_id)
for inflow_edge in basin.inflow_edges[i]
q = get_flow(graph, inflow_edge, 0)
for inflow_edge in inflow_edges[i]
q = flow_mean[inflow_edge.flow_idx]
if q > 0
inflow_mean[i] += q
else
outflow_mean[i] -= q
end
end
for outflow_edge in basin.outflow_edges[i]
q = get_flow(graph, outflow_edge, 0)
for outflow_edge in outflow_edges[i]
q = flow_mean[outflow_edge.flow_idx]
if q > 0
inflow_mean[i] += q
else
Expand Down
11 changes: 5 additions & 6 deletions core/src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ function basin_table(
balance_error = zeros(nrows)
relative_error = zeros(nrows)

@show length(model.integrator.sol.t)

idx_row = 0
for cvec in saved.flow.saveval
for (precipitation_, evaporation_, drainage_, infiltration_) in zip(
Expand All @@ -132,9 +130,6 @@ function basin_table(
end
end

@show ntsteps
@show length(saved.flow.saveval)

time = repeat(data.time[begin:(end - 1)]; inner = nbasin)
Δtime_seconds = seconds.(diff(data.time))
Δtime = repeat(Δtime_seconds; inner = nbasin)
Expand All @@ -146,6 +141,9 @@ function basin_table(
storage_increase = max(storage_flow, 0.0)
storage_decrease = max(-storage_flow, 0.0)

inflow_rate[i]
precipitation[i]

total_in = inflow_rate[i] + precipitation[i] + drainage[i] - storage_increase
total_out = outflow_rate[i] + evaporation[i] + infiltration[i] - storage_decrease
balance_error[i] = total_in - total_out
Expand Down Expand Up @@ -224,7 +222,8 @@ function flow_table(
from_node_id = repeat(from_node_id; outer = ntsteps)
to_node_type = repeat(to_node_type; outer = ntsteps)
to_node_id = repeat(to_node_id; outer = ntsteps)
flow_rate = average_over_saveats(saved.flow, :flow, saveats)
flow_rate =
FlatVector([collect(saved_flows.flow.flow) for saved_flows in saved.flow.saveval])

return (;
time,
Expand Down
2 changes: 2 additions & 0 deletions core/test/bmi_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ end
model = BMI.initialize(Ribasim.Model, toml_path)
demand = BMI.get_value_ptr(model, "user_demand.demand")
realized = BMI.get_value_ptr(model, "user_demand.realized")
@test realized ===
model.integrator.opts.callback.discrete_callbacks[4].affect!.integral.user_realized
day = 86400.0
BMI.update_until(model, 0.4day)
demand = 0.001 # for both users at the start
Expand Down
9 changes: 3 additions & 6 deletions core/test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ end
using Dictionaries: Indices
using StructArrays: StructVector
using Logging
using Ribasim: NodeID
using Ribasim: EdgeMetadata, NodeID

level = [
0.0,
Expand Down Expand Up @@ -94,11 +94,8 @@ end
demand = zeros(1)
basin = Ribasim.Basin(
Indices(NodeID.(:Basin, [1])),
[NodeID[]],
[NodeID[]],
zeros(1),
zeros(1),
zeros(1),
[EdgeMetadata[]],
[EdgeMetadata[]],
zeros(1),
zeros(1),
zeros(1),
Expand Down

0 comments on commit b327a00

Please sign in to comment.