Skip to content

Commit

Permalink
Allow missing as static user demand (#1493)
Browse files Browse the repository at this point in the history
Fixes #1490.

---------

Co-authored-by: Martijn Visser <mgvisser@gmail.com>
  • Loading branch information
SouthEndMusic and visr committed May 21, 2024
1 parent ef3eb92 commit 0ba441b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
5 changes: 3 additions & 2 deletions core/src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,9 @@ function user_demand_static!(

for row in group
priority_idx = findsorted(priorities, row.priority)
demand_itp[user_demand_idx][priority_idx].u .= row.demand
demand[user_demand_idx, priority_idx] = row.demand
demand_row = coalesce(row.demand, 0.0)
demand_itp[user_demand_idx][priority_idx].u .= demand_row
demand[user_demand_idx, priority_idx] = demand_row
end
end
return nothing
Expand Down
2 changes: 1 addition & 1 deletion core/src/schema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ end
@version UserDemandStaticV1 begin
node_id::Int32
active::Union{Missing, Bool}
demand::Float64
demand::Union{Missing, Float64}
return_factor::Float64
min_level::Float64
priority::Int32
Expand Down
7 changes: 4 additions & 3 deletions core/test/allocation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ end
# Subnetworks interpreted as user_demands require variables and constraints to
# support absolute value expressions in the objective function
problem = allocation_model_main_network.problem
@test problem[:F_abs_user_demand].axes[1] == NodeID.(:Pump, [11, 24, 38])
@test problem[:abs_positive_user_demand].axes[1] == NodeID.(:Pump, [11, 24, 38])
@test problem[:abs_negative_user_demand].axes[1] == NodeID.(:Pump, [11, 24, 38])
node_ids = [NodeID(:UserDemand, 60), NodeID.(:Pump, [11, 24, 38])...]
@test problem[:F_abs_user_demand].axes[1] == node_ids
@test problem[:abs_positive_user_demand].axes[1] == node_ids
@test problem[:abs_negative_user_demand].axes[1] == node_ids

# In each subnetwork, the connection from the main network to the subnetwork is
# interpreted as a source
Expand Down
2 changes: 1 addition & 1 deletion python/ribasim/ribasim/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class TerminalStaticSchema(_BaseSchema):
class UserDemandStaticSchema(_BaseSchema):
node_id: Series[Int32] = pa.Field(nullable=False, default=0)
active: Series[pa.BOOL] = pa.Field(nullable=True)
demand: Series[float] = pa.Field(nullable=False)
demand: Series[float] = pa.Field(nullable=True)
return_factor: Series[float] = pa.Field(nullable=False)
min_level: Series[float] = pa.Field(nullable=False)
priority: Series[Int32] = pa.Field(nullable=False, default=0)
Expand Down
8 changes: 8 additions & 0 deletions python/ribasim_testmodels/ribasim_testmodels/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,12 @@ def main_network_with_subnetworks_model() -> Model:
],
)

# Missing demand
model.user_demand.add(
Node(60, Point(21, -1), subnetwork_id=1),
[user_demand.Static(return_factor=[0.9], priority=2, min_level=0.0)],
)

model.edge.add(model.flow_boundary[1], model.basin[2], subnetwork_id=1)
model.edge.add(model.basin[2], model.linear_resistance[3])
model.edge.add(model.linear_resistance[3], model.basin[4])
Expand Down Expand Up @@ -858,6 +864,8 @@ def main_network_with_subnetworks_model() -> Model:
model.edge.add(model.basin[2], model.pump[11], subnetwork_id=3)
model.edge.add(model.basin[6], model.pump[24], subnetwork_id=5)
model.edge.add(model.basin[10], model.pump[38], subnetwork_id=7)
model.edge.add(model.basin[8], model.user_demand[60])
model.edge.add(model.user_demand[60], model.basin[8])

return model

Expand Down

0 comments on commit 0ba441b

Please sign in to comment.