Skip to content

Commit

Permalink
tstops -> tsaves
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Feb 26, 2024
1 parent 93fd5a3 commit 2f027fa
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 15 deletions.
5 changes: 3 additions & 2 deletions core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function create_callbacks(
end

"""
Integrate flows over timesteps
Integrate flows over the last timestep
"""
function integrate_flows!(u, t, integrator)::Nothing
(; p, dt) = integrator
Expand Down Expand Up @@ -422,7 +422,8 @@ function set_control_params!(p::Parameters, node_id::NodeID, control_state::Stri
end
end

"Copy the current flow to the SavedValues"
"Compute the average flows over the last saveat interval and write
them to SavedValues"
function save_flow(u, t, integrator)
(; dt, p) = integrator
(; graph) = p
Expand Down
2 changes: 1 addition & 1 deletion core/src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function Model(config::Config)::Model
end

"Get all saved times in seconds since start"
tstops(model::Model)::Vector{Float64} = model.integrator.sol.t
tsaves(model::Model)::Vector{Float64} = model.integrator.sol.t

"Get all saved times as a Vector{DateTime}"
function datetimes(model::Model)::Vector{DateTime}
Expand Down
2 changes: 1 addition & 1 deletion core/src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function get_storages_and_levels(
(; sol, p) = integrator

node_id = p.basin.node_id.values::Vector{NodeID}
tsteps = datetime_since.(tstops(model), config.starttime)
tsteps = datetime_since.(tsaves(model), config.starttime)

storage = hcat([collect(u_.storage) for u_ in sol.u]...)
level = zero(storage)
Expand Down
2 changes: 1 addition & 1 deletion core/test/allocation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ end
model = Ribasim.run(toml_path)

storage = Ribasim.get_storages_and_levels(model).storage[1, :]
t = Ribasim.tstops(model)
t = Ribasim.tsaves(model)

p = model.integrator.p
(; user_demand, graph, allocation, basin, level_demand) = p
Expand Down
12 changes: 6 additions & 6 deletions core/test/control_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
["off", "active", "on", "off", "inactive"]

level = Ribasim.get_storages_and_levels(model).level
t = Ribasim.tstops(model)
t = Ribasim.tsaves(model)

# Control times
t_1 = discrete_control.record.time[3]
Expand All @@ -56,7 +56,7 @@ end

Δt = discrete_control.look_ahead[1]

t = Ribasim.tstops(model)
t = Ribasim.tsaves(model)
t_control = discrete_control.record.time[2]
t_control_index = searchsortedfirst(t, t_control)

Expand All @@ -80,7 +80,7 @@ end

Δt = discrete_control.look_ahead[1]

t = Ribasim.tstops(model)
t = Ribasim.tsaves(model)
t_control = discrete_control.record.time[2]
t_control_index = searchsortedfirst(t, t_control)

Expand All @@ -100,7 +100,7 @@ end
(; basin, pid_control, flow_boundary) = p

level = Ribasim.get_storages_and_levels(model).level[1, :]
t = Ribasim.tstops(model)
t = Ribasim.tsaves(model)

target_itp = pid_control.target[1]
t_target_change = target_itp.t[2]
Expand Down Expand Up @@ -161,7 +161,7 @@ end
(; discrete_control) = p
(; record, greater_than) = discrete_control
level = Ribasim.get_storages_and_levels(model).level[1, :]
t = Ribasim.tstops(model)
t = Ribasim.tsaves(model)

t_none_1 = discrete_control.record.time[2]
t_in = discrete_control.record.time[3]
Expand Down Expand Up @@ -192,7 +192,7 @@ end
p = model.integrator.p
(; discrete_control, pid_control) = p

t = Ribasim.tstops(model)
t = Ribasim.tsaves(model)
level = Ribasim.get_storages_and_levels(model).level[1, :]

target_high =
Expand Down
8 changes: 4 additions & 4 deletions core/test/run_models_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Tables.DataAPI: nrow
using Dates: DateTime
import Arrow
using Ribasim: get_tstops, tstops
using Ribasim: get_tstops, tsaves

toml_path = normpath(@__DIR__, "../../generated_testmodels/trivial/ribasim.toml")
@test ispath(toml_path)
Expand Down Expand Up @@ -84,7 +84,7 @@
end

@testset "Results size" begin
nsaved = length(tstops(model))
nsaved = length(tsaves(model))
@test nsaved > 10
# t0 has no flow, 2 flow edges and 2 boundary condition flows
@test nrow(flow) == (nsaved - 1) * 4
Expand Down Expand Up @@ -189,7 +189,7 @@ end
@test all(isconcretetype, fieldtypes(typeof(p)))

@test successful_retcode(model)
@test allunique(Ribasim.tstops(model))
@test allunique(Ribasim.tsaves(model))
@test model.integrator.sol.u[end] Float32[519.8817, 519.8798, 339.3959, 1418.4331] skip =
Sys.isapple() atol = 1.5

Expand Down Expand Up @@ -366,7 +366,7 @@ end
# No outlet flow when upstream level is below minimum crest level
@test all(@. outlet_flow.flow_rate[t <= t_min_crest_level] == 0)

t = Ribasim.tstops(model)
t = Ribasim.tsaves(model)
t_maximum_level = level.t[2]
level_basin = Ribasim.get_storages_and_levels(model).level[:]

Expand Down

0 comments on commit 2f027fa

Please sign in to comment.