Skip to content

Commit

Permalink
Use dt from integrator
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Feb 26, 2024
1 parent fa1f548 commit f757c37
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
20 changes: 12 additions & 8 deletions core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ end
Integrate flows over timesteps
"""
function integrate_flows!(u, t, integrator)::Nothing
(; p, tprev) = integrator
(; p, dt) = integrator
(; graph) = p
(;
flow,
Expand All @@ -108,7 +108,6 @@ function integrate_flows!(u, t, integrator)::Nothing
) = graph[]
flow = get_tmp(flow, 0)
flow_vertical = get_tmp(flow_vertical, 0)
Δt = t - tprev

flow_effective = if !isempty(flow_prev) && isnan(flow_prev[1])
# If flow_prev is not populated yet
Expand All @@ -125,8 +124,8 @@ function integrate_flows!(u, t, integrator)::Nothing
0.5 * (flow_vertical + flow_vertical_prev)
end

@. flow_integrated += flow_effective * Δt
@. flow_vertical_integrated += flow_vertical_effective * Δt
@. flow_integrated += flow_effective * dt
@. flow_vertical_integrated += flow_vertical_effective * dt

copyto!(flow_prev, flow)
copyto!(flow_vertical_prev, flow_vertical)
Expand Down Expand Up @@ -425,18 +424,23 @@ end

"Copy the current flow to the SavedValues"
function save_flow(u, t, integrator)
(; tprev, p) = integrator
(; dt, p) = integrator
(; graph) = p
(; flow_integrated, flow_vertical_integrated, saveat) = graph[]

Δt = if iszero(saveat)
t - tprev
dt
elseif isinf(saveat)
t
else
t_end = integrator.sol.prob.tspan[2]
# The last interval might be shorter than saveat
t_end - t >= saveat ? saveat : t - tprev
if t_end - t > saveat
saveat
else
# The last interval might be shorter than saveat
rem = t % saveat
iszero(rem) ? saveat : rem
end
end

mean_flow_vertical = flow_vertical_integrated / Δt
Expand Down
6 changes: 4 additions & 2 deletions core/src/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ and data of edges (EdgeMetadata):
[`EdgeMetadata`](@ref)
"""
function create_graph(db::DB, config::Config, chunk_sizes::Vector{Int})::MetaGraph
node_rows = execute(db, "SELECT node_id, node_type, subnetwork_id FROM Node ORDER BY fid")
node_rows =
execute(db, "SELECT node_id, node_type, subnetwork_id FROM Node ORDER BY fid")
edge_rows = execute(
db,
"SELECT fid, from_node_type, from_node_id, to_node_type, to_node_id, edge_type, subnetwork_id FROM Edge ORDER BY fid",
Expand Down Expand Up @@ -44,7 +45,8 @@ function create_graph(db::DB, config::Config, chunk_sizes::Vector{Int})::MetaGra
end
push!(node_ids[allocation_network_id], node_id)
end
graph[node_id] = NodeMetadata(Symbol(snake_case(row.node_type)), allocation_network_id)
graph[node_id] =
NodeMetadata(Symbol(snake_case(row.node_type)), allocation_network_id)
if row.node_type in nonconservative_nodetypes
flow_vertical_counter += 1
flow_vertical_dict[node_id] = flow_vertical_counter
Expand Down
2 changes: 0 additions & 2 deletions core/src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,6 @@ function formulate_du!(
basin::Basin,
storage::AbstractVector,
)::Nothing
(; flow_vertical) = graph[]
flow_vertical = get_tmp(flow_vertical, storage)
# loop over basins
# subtract all outgoing flows
# add all ingoing flows
Expand Down
6 changes: 3 additions & 3 deletions core/test/equations_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ end
toml_path = normpath(@__DIR__, "../../generated_testmodels/misc_nodes/ribasim.toml")
@test ispath(toml_path)
config = Ribasim.Config(toml_path)
model = Ribasim.run(config)
model = Ribasim.BMI.initialize(Ribasim.Model, toml_path)
@test config.solver.dt === model.integrator.dt
Ribasim.solve!(model)
@test successful_retcode(model)
p = model.integrator.p
(; flow_boundary, fractional_flow, pump) = p
Expand All @@ -186,8 +188,6 @@ end
storage_both = get_storages_and_levels(model).storage
t = tstops(model)
tspan = model.integrator.sol.prob.tspan

@test config.solver.dt === model.integrator.dt
@test t range(tspan...; step = config.solver.saveat)
@test storage_both[1, :] @. storage_both[1, 1] + t * (frac * q_boundary - q_pump)
@test storage_both[2, :] @. storage_both[2, 1] + t * q_pump
Expand Down

0 comments on commit f757c37

Please sign in to comment.