Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mean output flows #1159

Merged
merged 28 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b8692cd
Mean output flows
SouthEndMusic Feb 21, 2024
e50efdc
Merge branch 'main' into mean_flow_output
SouthEndMusic Feb 21, 2024
f9d0eba
Fix test and add test
SouthEndMusic Feb 21, 2024
8ca608e
Merge branch 'main' into mean_flow_output
SouthEndMusic Feb 21, 2024
83ec278
Update flow save Δt
SouthEndMusic Feb 22, 2024
9257ffb
Add saveat to tstops
SouthEndMusic Feb 22, 2024
edfe8cd
small stuff
SouthEndMusic Feb 22, 2024
64bbac0
Add tests
SouthEndMusic Feb 22, 2024
ec01f49
Pass tests
SouthEndMusic Feb 22, 2024
51175e6
Pass old tests as well
SouthEndMusic Feb 22, 2024
5962757
timesteps -> tstops
SouthEndMusic Feb 22, 2024
88e449b
Merge branch 'main' into mean_flow_output
SouthEndMusic Feb 22, 2024
84bb244
Merge branch 'main' into mean_flow_output
SouthEndMusic Feb 26, 2024
fa1f548
Merge branch 'main' into mean_flow_output
SouthEndMusic Feb 26, 2024
f757c37
Use dt from integrator
SouthEndMusic Feb 26, 2024
93fd5a3
Merge branch 'main' into mean_flow_output
SouthEndMusic Feb 26, 2024
2f027fa
tstops -> tsaves
SouthEndMusic Feb 26, 2024
5601282
Fix tests
SouthEndMusic Feb 26, 2024
72e2b58
update docs
SouthEndMusic Feb 26, 2024
aeec184
Merge branch 'main' into mean_flow_output
SouthEndMusic Feb 26, 2024
24a7609
Merge branch 'main' into mean_flow_output
SouthEndMusic Feb 26, 2024
b20c814
Set timestamps at the beginning of the period
visr Feb 26, 2024
a8b335c
fix empty results
visr Feb 27, 2024
2763867
Adress most code comments
SouthEndMusic Feb 27, 2024
550920e
Final comments adressed
SouthEndMusic Feb 28, 2024
5f5e8de
Fix tests
SouthEndMusic Feb 28, 2024
031cb71
Merge branch 'main' into mean_flow_output
SouthEndMusic Feb 28, 2024
f86410c
Last comment adressed
SouthEndMusic Feb 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function set_initial_discrete_controlled_parameters!(
storage0::Vector{Float64},
)::Nothing
(; p) = integrator
(; basin, discrete_control) = p
(; discrete_control) = p

n_conditions = length(discrete_control.condition_value)
condition_diffs = zeros(Float64, n_conditions)
Expand Down Expand Up @@ -40,6 +40,9 @@ function create_callbacks(
basin_cb = PresetTimeCallback(tstops, update_basin)
push!(callbacks, basin_cb)

integrating_flows_cb = FunctionCallingCallback(integrate_flows!)
push!(callbacks, integrating_flows_cb)

tstops = get_tstops(tabulated_rating_curve.time.time, starttime)
tabulated_rating_curve_cb = PresetTimeCallback(tstops, update_tabulated_rating_curve!)
push!(callbacks, tabulated_rating_curve_cb)
Expand Down Expand Up @@ -87,6 +90,29 @@ function create_callbacks(
return callback, saved
end

function integrate_flows!(u, t, integrator)::Nothing
(; p, tprev) = integrator
(; graph) = p
(;
flow,
flow_vertical,
flow_prev,
flow_vertical_prev,
flow_integrated,
flow_vertical_integrated,
) = graph[]
flow = get_tmp(flow, 0)
flow_vertical = get_tmp(flow_vertical, 0)
Δt = t - tprev

@. flow_integrated += 0.5 * (flow + flow_prev) * Δt
@. flow_vertical_integrated += 0.5 * (flow_vertical + flow_vertical_prev) * Δt

copyto!(flow_prev, flow)
copyto!(flow_vertical_prev, flow_vertical)
return nothing
end

"""
Listens for changes in condition truths.
"""
Expand Down Expand Up @@ -379,10 +405,17 @@ end

"Copy the current flow to the SavedValues"
function save_flow(u, t, integrator)
vcat(
get_tmp(integrator.p.graph[].flow_vertical, 0.0),
get_tmp(integrator.p.graph[].flow, 0.0),
)
(; graph) = integrator.p
(; flow_integrated, flow_vertical_integrated, tprev_flow_save) = graph[]
Δt = t - tprev_flow_save[1]
visr marked this conversation as resolved.
Show resolved Hide resolved
tprev_flow_save[1] = t

mean_flow_vertical = flow_vertical_integrated / Δt
mean_flow = flow_integrated / Δt

fill!(flow_vertical_integrated, 0.0)
fill!(flow_integrated, 0.0)
return vcat(mean_flow_vertical, mean_flow)
SouthEndMusic marked this conversation as resolved.
Show resolved Hide resolved
end

function update_subgrid_level!(integrator)::Nothing
Expand Down
10 changes: 10 additions & 0 deletions core/src/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,29 @@ function create_graph(db::DB, config::Config, chunk_sizes::Vector{Int})::MetaGra
end

flow = zeros(flow_counter)
flow_prev = zeros(flow_counter)
flow_integrated = zeros(flow_counter)
flow_vertical = zeros(flow_vertical_counter)
flow_vertical_prev = zeros(flow_vertical_counter)
flow_vertical_integrated = zeros(flow_vertical_counter)
if config.solver.autodiff
flow = DiffCache(flow, chunk_sizes)
flow_vertical = DiffCache(flow_vertical, chunk_sizes)
end
tprev_flow_save = zeros(1)
graph_data = (;
node_ids,
edge_ids,
edges_source,
flow_dict,
flow,
flow_prev,
flow_integrated,
flow_vertical_dict,
flow_vertical,
flow_vertical_prev,
flow_vertical_integrated,
tprev_flow_save,
)
graph = @set graph.graph_data = graph_data

Expand Down
5 changes: 5 additions & 0 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,13 @@ struct Parameters{T, C1, C2}
edges_source::Dict{Int, Set{EdgeMetadata}},
flow_dict::Dict{Tuple{NodeID, NodeID}, Int},
flow::T,
flow_prev::Vector{Float64},
flow_integrated::Vector{Float64},
visr marked this conversation as resolved.
Show resolved Hide resolved
flow_vertical_dict::Dict{NodeID, Int},
flow_vertical::T,
flow_vertical_prev::Vector{Float64},
flow_vertical_integrated::Vector{Float64},
visr marked this conversation as resolved.
Show resolved Hide resolved
tprev_flow_save::Vector{Float64},
},
MetaGraphsNext.var"#11#13",
Float64,
Expand Down
13 changes: 13 additions & 0 deletions core/test/run_models_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,16 @@ end
0,
) ≈ 5.0 atol = 0.001 skip = Sys.isapple()
end

@testitem "mean_flow" begin
toml_path =
normpath(@__DIR__, "../../generated_testmodels/linear_resistance/ribasim.toml")
@test ispath(toml_path)
t_end = 3.16224e7

config = Ribasim.Config(toml_path; solver_saveat = t_end)
model = Ribasim.run(config)
mean_flow = model.saved.flow.saveval[1]
storage = Ribasim.get_storages_and_levels(model).storage[1, :]
@test storage[2] ≈ storage[1] - t_end * mean_flow[3] atol = 1
end
2 changes: 1 addition & 1 deletion core/test/time_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
t = Ribasim.seconds_since.(flow_1_to_2.time, model.config.starttime)
flow_expected = @. 1 + sin(0.5 * π * (t - t[1]) / (t[end] - t[1]))^2
# some difference is expected since the modeled flow is for the period up to t
@test isapprox(flow_added_1, flow_expected, rtol = 0.005)
@test isapprox(flow_added_1, flow_expected, rtol = 0.01)
visr marked this conversation as resolved.
Show resolved Hide resolved
end
Loading