Skip to content

Commit

Permalink
Pass node rows to get_sources_in_order
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Jan 30, 2025
1 parent 85d6d63 commit 2a19b32
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
4 changes: 3 additions & 1 deletion core/src/allocation_init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ TODO: Get preferred source order from input
function get_sources_in_order(
problem::JuMP.Model,
p::Parameters,
node_rows::SQLite.Query,
subnetwork_id::Integer,
)::OrderedDict{Tuple{NodeID, NodeID}, AllocationSource}
# NOTE: return flow has to be done before other sources, to prevent that
Expand Down Expand Up @@ -552,11 +553,12 @@ An AllocationModel object.
function AllocationModel(
subnetwork_id::Int32,
p::Parameters,
node_rows::SQLite.Query,
Δt_allocation::Float64,
)::AllocationModel
capacity = get_capacity(p, subnetwork_id)
problem = allocation_problem(p, capacity, subnetwork_id)
sources = get_sources_in_order(problem, p, subnetwork_id)
sources = get_sources_in_order(problem, p, node_rows, subnetwork_id)
flow = JuMP.Containers.SparseAxisArray(Dict(only(problem[:F].axes) .=> 0.0))

return AllocationModel(; subnetwork_id, capacity, flow, sources, problem, Δt_allocation)
Expand Down
11 changes: 8 additions & 3 deletions core/src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ const conservative_nodetypes = Set{NodeType.T}([
NodeType.ManningResistance,
])

function initialize_allocation!(p::Parameters, config::Config)::Nothing
function initialize_allocation!(p::Parameters, db::DB, config::Config)::Nothing
(; graph, allocation) = p
(; subnetwork_ids, allocation_models, main_network_connections) = allocation
subnetwork_ids_ = sort(collect(keys(graph[].node_ids)))
Expand All @@ -269,10 +269,15 @@ function initialize_allocation!(p::Parameters, config::Config)::Nothing
find_subnetwork_connections!(p)
end

node_rows = execute(
db,
"SELECT node_id, node_type, subnetwork_id, source_priority FROM Node ORDER BY subnetwork_id, source_priority",
)

for subnetwork_id in subnetwork_ids_
push!(
allocation_models,
AllocationModel(subnetwork_id, p, config.allocation.timestep),
AllocationModel(subnetwork_id, p, node_rows, config.allocation.timestep),
)
end
return nothing
Expand Down Expand Up @@ -1569,7 +1574,7 @@ function Parameters(db::DB, config::Config)::Parameters

# Allocation data structures
if config.allocation.use_allocation
initialize_allocation!(p, config)
initialize_allocation!(p, db, config)
end
return p
end
Expand Down
21 changes: 21 additions & 0 deletions python/ribasim/ribasim/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@
from ribasim.utils import _concat, _pascal_to_snake


class DefaultSourcePriority(ChildModel):
"""
Specify per source node type what its default source priority is.
flow_boundary and level_boundary nodes are combined into the single category 'boundary'.
"""

user_demand: int = 1000
boundary: int = 2000
level_demand: int = 3000
flow_demand: int = 4000
subnetwork_inlet: int = 5000


class Allocation(ChildModel):
"""
Defines the allocation optimization algorithm options.
Expand All @@ -70,6 +84,7 @@ class Allocation(ChildModel):

timestep: float = 86400.0
use_allocation: bool = False
default_source_priority: DefaultSourcePriority = DefaultSourcePriority()


class Results(ChildModel):
Expand Down Expand Up @@ -173,12 +188,15 @@ class Node(pydantic.BaseModel):
An optional name of the node.
subnetwork_id : int
Optionally adds this node to a subnetwork, which is input for the allocation algorithm.
source_priority : int
Optionally adds a source priority to this node, which is input for the allocation algorithm.
"""

node_id: NonNegativeInt | None = None
geometry: Point
name: str = ""
subnetwork_id: int | None = None
source_priority: int | None = None

model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow")

Expand All @@ -203,6 +221,9 @@ def into_geodataframe(self, node_type: str, node_id: int) -> GeoDataFrame:
"node_type": pd.Series([node_type], dtype=str),
"name": pd.Series([self.name], dtype=str),
"subnetwork_id": pd.Series([self.subnetwork_id], dtype=pd.Int32Dtype()),
"source_priority": pd.Series(
[self.source_priority], dtype=pd.Int32Dtype()
),
**extra,
},
geometry=[self.geometry],
Expand Down
2 changes: 1 addition & 1 deletion python/ribasim_testmodels/ribasim_testmodels/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ def linear_resistance_demand_model():
)

model.flow_demand.add(
Node(4, Point(1, 1), subnetwork_id=2),
Node(4, Point(1, 1), subnetwork_id=2, source_priority=1),
[flow_demand.Static(demand_priority=[1], demand=2.0)],
)

Expand Down

0 comments on commit 2a19b32

Please sign in to comment.