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

Store state node IDs in parameters #2086

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion core/src/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function log_bottlenecks(model; converged::Bool)
max_errors = 5
# Iterate over the errors in descending order
for i in sortperm(flow_error; rev = true)
node_id = Symbol(id_from_state_index(p, u, i))
node_id = Symbol(p.node_id[i])
error = flow_error[i]
isnan(error) && continue # NaN are sorted as largest
# Stop reporting errors if they are too small or too many
Expand Down
2 changes: 2 additions & 0 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,8 @@ const ModelGraph = MetaGraph{
const water_balance_reltol::Float64
# State at previous saveat
const u_prev_saveat::C11 = ComponentVector()
# Node ID associated with each state
const node_id::Vector{NodeID} = NodeID[]
end

# To opt-out of type checking for ForwardDiff
Expand Down
15 changes: 15 additions & 0 deletions core/src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,20 @@ function Parameters(db::DB, config::Config)::Parameters

subgrid = Subgrid(db, config, basin)

# aligned with the state
node_id = vcat(
tabulated_rating_curve.node_id,
pump.node_id,
outlet.node_id,
user_demand.node_id,
user_demand.node_id,
linear_resistance.node_id,
manning_resistance.node_id,
basin.node_id,
basin.node_id,
pid_control.node_id,
)

p = Parameters(;
config.starttime,
graph,
Expand All @@ -1722,6 +1736,7 @@ function Parameters(db::DB, config::Config)::Parameters
subgrid,
config.solver.water_balance_abstol,
config.solver.water_balance_reltol,
node_id,
)

collect_control_mappings!(p)
Expand Down
30 changes: 3 additions & 27 deletions core/src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ end
function build_state_vector(p::Parameters)
# It is assumed that the horizontal flow states come first in
# p.state_inflow_link and p.state_outflow_link
return ComponentVector{Float64}(;
u = ComponentVector{Float64}(;
tabulated_rating_curve = zeros(length(p.tabulated_rating_curve.node_id)),
pump = zeros(length(p.pump.node_id)),
outlet = zeros(length(p.outlet.node_id)),
Expand All @@ -879,6 +879,8 @@ function build_state_vector(p::Parameters)
infiltration = zeros(length(p.basin.node_id)),
integral = zeros(length(p.pid_control.node_id)),
)
@assert length(u) == length(p.node_id)
return u
end

function build_flow_to_storage(p::Parameters, u::ComponentVector)::Parameters
Expand Down Expand Up @@ -999,32 +1001,6 @@ function set_state_flow_links(p::Parameters, u0::ComponentVector)::Parameters
return p
end

function id_from_state_index(
p::Parameters,
::ComponentVector{Float64, Vector{Float64}, <:Tuple{<:Axis{NT}}},
global_idx::Int,
)::NodeID where {NT}
local_idx = 0
component = Symbol()
for (comp, range) in pairs(NT)
if global_idx in range
component = comp
local_idx = global_idx - first(range) + 1
break
end
end
component_string = String(component)
if endswith(component_string, "_inflow") || endswith(component_string, "_outflow")
component = :user_demand
elseif component == :integral
component = :pid_control
elseif component in [:infiltration, :evaporation]
component = :basin
end

getfield(p, component).node_id[local_idx]
end

function get_state_index(
id::NodeID,
::ComponentVector{A, B, <:Tuple{<:Axis{NT}}};
Expand Down
6 changes: 3 additions & 3 deletions core/test/allocation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
4.5
allocation_model = p.allocation.allocation_models[1]
(; flow) = allocation_model
u = ComponentVector(; storage = zeros(length(p.basin.node_id)))
u = ComponentVector()
t = 0.0
Ribasim.allocate_demands!(p, allocation_model, t, u)

Expand Down Expand Up @@ -133,7 +133,7 @@ end
t = 0.0

# Collecting demands
u = ComponentVector(; storage = zeros(length(basin.node_id)))
u = ComponentVector()
for allocation_model in allocation_models[2:end]
Ribasim.collect_demands!(p, allocation_model, t, u)
end
Expand Down Expand Up @@ -227,7 +227,7 @@ end
mean_input_flows[4][(NodeID(:FlowBoundary, 59, p), NodeID(:Basin, 44, p))] = 1e-3

# Collecting demands
u = ComponentVector(; storage = zeros(length(basin.node_id)))
u = ComponentVector()
for allocation_model in allocation_models[2:end]
Ribasim.collect_demands!(p, allocation_model, t, u)
end
Expand Down
1 change: 1 addition & 0 deletions core/test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ end
db = SQLite.DB(db_path)

p = Ribasim.Parameters(db, cfg)
@test p.node_id == [4, 5, 8, 7, 10, 12, 2, 1, 3, 6, 9, 1, 3, 6, 9]
close(db)
t0 = 0.0
u0 = Ribasim.build_state_vector(p)
Expand Down
1 change: 0 additions & 1 deletion docs/concept/allocation.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ The following is an example of an optimization problem for the example shown [he
using Ribasim
using Ribasim: NodeID
using SQLite
using ComponentArrays: ComponentVector

toml_path = normpath(@__DIR__, "../../generated_testmodels/allocation_example/ribasim.toml")
model = Ribasim.Model(toml_path)
Expand Down