Skip to content

Commit

Permalink
Pre-calculate flow neighbor IDs for Pump, Outlet, UserDemand, Fractio…
Browse files Browse the repository at this point in the history
…nalFlow
  • Loading branch information
visr committed May 2, 2024
1 parent 650053b commit 3a19f04
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 62 deletions.
29 changes: 29 additions & 0 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,15 @@ Requirements:
* fraction must be positive.
node_id: node ID of the TabulatedRatingCurve node
inflow_id: node ID across the incoming flow edge
outflow_id: node ID across the outgoing flow edge
fraction: The fraction in [0,1] of flow the node lets through
control_mapping: dictionary from (node_id, control_state) to fraction
"""
struct FractionalFlow <: AbstractParameterNode
node_id::Vector{NodeID}
inflow_id::Vector{NodeID}
outflow_id::Vector{NodeID}
fraction::Vector{Float64}
control_mapping::Dict{Tuple{NodeID, String}, NamedTuple}
end
Expand Down Expand Up @@ -354,6 +358,8 @@ end

"""
node_id: node ID of the Pump node
inflow_id: node ID across the incoming flow edge
outflow_ids: node IDs across the outgoing flow edges
active: whether this node is active and thus contributes flow
flow_rate: target flow rate
min_flow_rate: The minimal flow rate of the pump
Expand All @@ -363,6 +369,8 @@ is_pid_controlled: whether the flow rate of this pump is governed by PID control
"""
struct Pump{T} <: AbstractParameterNode
node_id::Vector{NodeID}
inflow_id::Vector{NodeID}
outflow_ids::Vector{Vector{NodeID}}
active::BitVector
flow_rate::T
min_flow_rate::Vector{Float64}
Expand All @@ -372,6 +380,8 @@ struct Pump{T} <: AbstractParameterNode

function Pump(
node_id,
inflow_id,
outflow_ids,
active,
flow_rate::T,
min_flow_rate,
Expand All @@ -382,6 +392,8 @@ struct Pump{T} <: AbstractParameterNode
if valid_flow_rates(node_id, get_tmp(flow_rate, 0), control_mapping)
return new{T}(
node_id,
inflow_id,
outflow_ids,
active,
flow_rate,
min_flow_rate,
Expand All @@ -397,6 +409,8 @@ end

"""
node_id: node ID of the Outlet node
inflow_id: node ID across the incoming flow edge
outflow_ids: node IDs across the outgoing flow edges
active: whether this node is active and thus contributes flow
flow_rate: target flow rate
min_flow_rate: The minimal flow rate of the outlet
Expand All @@ -406,6 +420,8 @@ is_pid_controlled: whether the flow rate of this outlet is governed by PID contr
"""
struct Outlet{T} <: AbstractParameterNode
node_id::Vector{NodeID}
inflow_id::Vector{NodeID}
outflow_ids::Vector{Vector{NodeID}}
active::BitVector
flow_rate::T
min_flow_rate::Vector{Float64}
Expand All @@ -416,6 +432,8 @@ struct Outlet{T} <: AbstractParameterNode

function Outlet(
node_id,
inflow_id,
outflow_ids,
active,
flow_rate::T,
min_flow_rate,
Expand All @@ -427,6 +445,8 @@ struct Outlet{T} <: AbstractParameterNode
if valid_flow_rates(node_id, get_tmp(flow_rate, 0), control_mapping)
return new{T}(
node_id,
inflow_id,
outflow_ids,
active,
flow_rate,
min_flow_rate,
Expand Down Expand Up @@ -503,6 +523,9 @@ struct PidControl{T} <: AbstractParameterNode
end

"""
node_id: node ID of the UserDemand node
inflow_id: node ID across the incoming flow edge
outflow_id: node ID across the outgoing flow edge
active: whether this node is active and thus demands water
realized_bmi: Cumulative inflow volume, for read or reset by BMI only
demand: water flux demand of UserDemand per priority over time
Expand All @@ -518,6 +541,8 @@ min_level: The level of the source basin below which the UserDemand does not abs
"""
struct UserDemand <: AbstractParameterNode
node_id::Vector{NodeID}
inflow_id::Vector{NodeID}
outflow_id::Vector{NodeID}
active::BitVector
realized_bmi::Vector{Float64}
demand::Matrix{Float64}
Expand All @@ -530,6 +555,8 @@ struct UserDemand <: AbstractParameterNode

function UserDemand(
node_id,
inflow_id,
outflow_id,
active,
realized_bmi,
demand,
Expand All @@ -544,6 +571,8 @@ struct UserDemand <: AbstractParameterNode
if valid_demand(node_id, demand_itp, priorities)
return new(
node_id,
inflow_id,
outflow_id,
active,
realized_bmi,
demand,
Expand Down
36 changes: 25 additions & 11 deletions core/src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,20 @@ function ManningResistance(db::DB, config::Config, graph::MetaGraph)::ManningRes
)
end

function FractionalFlow(db::DB, config::Config)::FractionalFlow
function FractionalFlow(db::DB, config::Config, graph::MetaGraph)::FractionalFlow
static = load_structvector(db, config, FractionalFlowStaticV1)
parsed_parameters, valid = parse_static_and_time(db, config, "FractionalFlow"; static)

if !valid
error("Errors occurred when parsing FractionalFlow data.")
end

node_id = NodeID.(NodeType.FractionalFlow, parsed_parameters.node_id)

return FractionalFlow(
NodeID.(NodeType.FractionalFlow, parsed_parameters.node_id),
node_id,
inflow_id.(Ref(graph), node_id),
outflow_id.(Ref(graph), node_id),
parsed_parameters.fraction,
parsed_parameters.control_mapping,
)
Expand Down Expand Up @@ -427,7 +431,7 @@ function FlowBoundary(db::DB, config::Config)::FlowBoundary
return FlowBoundary(node_ids, parsed_parameters.active, parsed_parameters.flow_rate)
end

function Pump(db::DB, config::Config, chunk_sizes::Vector{Int})::Pump
function Pump(db::DB, config::Config, graph::MetaGraph, chunk_sizes::Vector{Int})::Pump
static = load_structvector(db, config, PumpStaticV1)
defaults = (; min_flow_rate = 0.0, max_flow_rate = Inf, active = true)
parsed_parameters, valid = parse_static_and_time(db, config, "Pump"; static, defaults)
Expand All @@ -444,8 +448,12 @@ function Pump(db::DB, config::Config, chunk_sizes::Vector{Int})::Pump
parsed_parameters.flow_rate
end

node_id = NodeID.(NodeType.Pump, parsed_parameters.node_id)

return Pump(
NodeID.(NodeType.Pump, parsed_parameters.node_id),
node_id,
inflow_id.(Ref(graph), node_id),
[collect(outflow_ids(graph, id)) for id in node_id],
BitVector(parsed_parameters.active),
flow_rate,
parsed_parameters.min_flow_rate,
Expand All @@ -455,7 +463,7 @@ function Pump(db::DB, config::Config, chunk_sizes::Vector{Int})::Pump
)
end

function Outlet(db::DB, config::Config, chunk_sizes::Vector{Int})::Outlet
function Outlet(db::DB, config::Config, graph::MetaGraph, chunk_sizes::Vector{Int})::Outlet
static = load_structvector(db, config, OutletStaticV1)
defaults =
(; min_flow_rate = 0.0, max_flow_rate = Inf, min_crest_level = -Inf, active = true)
Expand All @@ -473,8 +481,12 @@ function Outlet(db::DB, config::Config, chunk_sizes::Vector{Int})::Outlet
parsed_parameters.flow_rate
end

node_id = NodeID.(NodeType.Outlet, parsed_parameters.node_id)

return Outlet(
NodeID.(NodeType.Outlet, parsed_parameters.node_id),
node_id,
inflow_id.(Ref(graph), node_id),
[collect(outflow_ids(graph, id)) for id in node_id],
BitVector(parsed_parameters.active),
flow_rate,
parsed_parameters.min_flow_rate,
Expand Down Expand Up @@ -806,7 +818,7 @@ function user_demand_time!(
return errors
end

function UserDemand(db::DB, config::Config)::UserDemand
function UserDemand(db::DB, config::Config, graph::MetaGraph)::UserDemand
static = load_structvector(db, config, UserDemandStaticV1)
time = load_structvector(db, config, UserDemandTimeV1)

Expand Down Expand Up @@ -865,6 +877,8 @@ function UserDemand(db::DB, config::Config)::UserDemand

return UserDemand(
node_ids,
inflow_id.(Ref(graph), node_ids),
outflow_id.(Ref(graph), node_ids),
active,
realized_bmi,
demand,
Expand Down Expand Up @@ -1045,15 +1059,15 @@ function Parameters(db::DB, config::Config)::Parameters
linear_resistance = LinearResistance(db, config, graph)
manning_resistance = ManningResistance(db, config, graph)
tabulated_rating_curve = TabulatedRatingCurve(db, config)
fractional_flow = FractionalFlow(db, config)
fractional_flow = FractionalFlow(db, config, graph)
level_boundary = LevelBoundary(db, config)
flow_boundary = FlowBoundary(db, config)
pump = Pump(db, config, chunk_sizes)
outlet = Outlet(db, config, chunk_sizes)
pump = Pump(db, config, graph, chunk_sizes)
outlet = Outlet(db, config, graph, chunk_sizes)
terminal = Terminal(db, config)
discrete_control = DiscreteControl(db, config)
pid_control = PidControl(db, config, chunk_sizes)
user_demand = UserDemand(db, config)
user_demand = UserDemand(db, config, graph)
level_demand = LevelDemand(db, config)
flow_demand = FlowDemand(db, config)

Expand Down
Loading

0 comments on commit 3a19f04

Please sign in to comment.