From 4344c12a8bc4b6cb344f48ed1354318c276ea1ca Mon Sep 17 00:00:00 2001 From: Bart de Koning Date: Wed, 27 Nov 2024 13:30:33 +0100 Subject: [PATCH] Pass tests --- core/src/read.jl | 76 ++++++++++--------- core/test/validation_test.jl | 14 +--- docs/guide/examples.ipynb | 4 +- .../ribasim_testmodels/allocation.py | 27 ++++--- .../ribasim_testmodels/invalid.py | 1 - 5 files changed, 56 insertions(+), 66 deletions(-) diff --git a/core/src/read.jl b/core/src/read.jl index d8e43e3eb..17e020b78 100644 --- a/core/src/read.jl +++ b/core/src/read.jl @@ -1269,50 +1269,52 @@ end function Allocation(db::DB, config::Config, graph::MetaGraph)::Allocation mean_input_flows = Dict{Tuple{NodeID, NodeID}, Float64}[] - + mean_realized_flows = Dict{Tuple{NodeID, NodeID}, Float64}() subnetwork_ids = sort(collect(keys(graph[].node_ids))) - for subnetwork_id in subnetwork_ids - push!(mean_input_flows, Dict{Tuple{NodeID, NodeID}, Float64}()) - end - - # Find edges which serve as sources in allocation - for edge_metadata in values(graph.edge_data) - (; edge) = edge_metadata - id_source, _ = edge - if id_source.type in boundary_source_nodetypes - (; subnetwork_id) = graph[id_source] - subnetwork_idx = searchsortedfirst(subnetwork_ids, subnetwork_id) - mean_input_flows[subnetwork_idx][edge] = 0.0 + if config.allocation.use_allocation + for subnetwork_id in subnetwork_ids + push!(mean_input_flows, Dict{Tuple{NodeID, NodeID}, Float64}()) end - end - # Find basins with a level demand - for node_id in values(graph.vertex_labels) - if has_external_demand(graph, node_id, :level_demand)[1] - subnetwork_id = graph[node_id].subnetwork_id - subnetwork_idx = searchsortedfirst(subnetwork_ids, subnetwork_id) - mean_input_flows[subnetwork_idx][(node_id, node_id)] = 0.0 + # Find edges which serve as sources in allocation + for edge_metadata in values(graph.edge_data) + (; edge) = edge_metadata + id_source, _ = edge + if id_source.type in boundary_source_nodetypes + (; subnetwork_id) = graph[id_source] + subnetwork_idx = searchsortedfirst(subnetwork_ids, subnetwork_id) + mean_input_flows[subnetwork_idx][edge] = 0.0 + end end - end - mean_realized_flows = Dict{Tuple{NodeID, NodeID}, Float64}() + # Find basins with a level demand + for node_id in values(graph.vertex_labels) + if has_external_demand(graph, node_id, :level_demand)[1] + subnetwork_id = graph[node_id].subnetwork_id + subnetwork_idx = searchsortedfirst(subnetwork_ids, subnetwork_id) + mean_input_flows[subnetwork_idx][(node_id, node_id)] = 0.0 + end + end - # Find edges that realize a demand - for edge_metadata in values(graph.edge_data) - (; type, edge) = edge_metadata - - src_id, dst_id = edge - user_demand_inflow = (type == EdgeType.flow) && (dst_id.type == NodeType.UserDemand) - level_demand_inflow = - (type == EdgeType.control) && (src_id.type == NodeType.LevelDemand) - flow_demand_inflow = - (type == EdgeType.flow) && has_external_demand(graph, dst_id, :flow_demand)[1] - - if user_demand_inflow || flow_demand_inflow - mean_realized_flows[edge] = 0.0 - elseif level_demand_inflow - mean_realized_flows[(dst_id, dst_id)] = 0.0 + # Find edges that realize a demand + for edge_metadata in values(graph.edge_data) + (; type, edge) = edge_metadata + + src_id, dst_id = edge + user_demand_inflow = + (type == EdgeType.flow) && (dst_id.type == NodeType.UserDemand) + level_demand_inflow = + (type == EdgeType.control) && (src_id.type == NodeType.LevelDemand) + flow_demand_inflow = + (type == EdgeType.flow) && + has_external_demand(graph, dst_id, :flow_demand)[1] + + if user_demand_inflow || flow_demand_inflow + mean_realized_flows[edge] = 0.0 + elseif level_demand_inflow + mean_realized_flows[(dst_id, dst_id)] = 0.0 + end end end diff --git a/core/test/validation_test.jl b/core/test/validation_test.jl index 547a11ff4..e46c0ef74 100644 --- a/core/test/validation_test.jl +++ b/core/test/validation_test.jl @@ -86,12 +86,7 @@ end graph[NodeID(:Pump, 6, 1)] = NodeMetadata(:pump, 9) function set_edge_metadata!(id_1, id_2, edge_type) - graph[id_1, id_2] = EdgeMetadata(; - id = 0, - type = edge_type, - subnetwork_id_source = 0, - edge = (id_1, id_2), - ) + graph[id_1, id_2] = EdgeMetadata(; id = 0, type = edge_type, edge = (id_1, id_2)) return nothing end @@ -148,12 +143,7 @@ end graph[NodeID(:Basin, 7, 1)] = NodeMetadata(:basin, 0) function set_edge_metadata!(id_1, id_2, edge_type) - graph[id_1, id_2] = EdgeMetadata(; - id = 0, - type = edge_type, - subnetwork_id_source = 0, - edge = (id_1, id_2), - ) + graph[id_1, id_2] = EdgeMetadata(; id = 0, type = edge_type, edge = (id_1, id_2)) return nothing end diff --git a/docs/guide/examples.ipynb b/docs/guide/examples.ipynb index 31e899dd6..3e09e5986 100644 --- a/docs/guide/examples.ipynb +++ b/docs/guide/examples.ipynb @@ -1203,7 +1203,7 @@ "metadata": {}, "outputs": [], "source": [ - "model.edge.add(model.flow_boundary[1], model.basin[2], subnetwork_id=1)\n", + "model.edge.add(model.flow_boundary[1], model.basin[2])\n", "model.edge.add(model.basin[2], model.user_demand[3])\n", "model.edge.add(model.basin[2], model.linear_resistance[4])\n", "model.edge.add(model.linear_resistance[4], model.basin[5])\n", @@ -1499,7 +1499,7 @@ "metadata": {}, "outputs": [], "source": [ - "model.edge.add(model.flow_boundary[1], model.basin[2], subnetwork_id=2)\n", + "model.edge.add(model.flow_boundary[1], model.basin[2])\n", "model.edge.add(model.basin[2], model.user_demand[3])\n", "model.edge.add(model.level_demand[4], model.basin[2])\n", "model.edge.add(model.user_demand[3], model.basin[5])\n", diff --git a/python/ribasim_testmodels/ribasim_testmodels/allocation.py b/python/ribasim_testmodels/ribasim_testmodels/allocation.py index e51fbbe40..da31a3ecc 100644 --- a/python/ribasim_testmodels/ribasim_testmodels/allocation.py +++ b/python/ribasim_testmodels/ribasim_testmodels/allocation.py @@ -154,7 +154,7 @@ def subnetwork_model() -> Model: ) model.outlet.add(Node(13, Point(2, 4), subnetwork_id=2), outlet_data) - model.edge.add(model.flow_boundary[1], model.basin[2], subnetwork_id=2) + model.edge.add(model.flow_boundary[1], model.basin[2]) model.edge.add(model.basin[2], model.outlet[3]) model.edge.add(model.outlet[3], model.terminal[4]) model.edge.add(model.basin[2], model.user_demand[10]) @@ -269,7 +269,7 @@ def looped_subnetwork_model() -> Model: ], ) - model.edge.add(model.flow_boundary[5], model.basin[2], subnetwork_id=2) + model.edge.add(model.flow_boundary[5], model.basin[2]) model.edge.add(model.basin[2], model.outlet[3]) model.edge.add(model.outlet[3], model.terminal[4]) model.edge.add(model.basin[2], model.user_demand[1]) @@ -348,7 +348,7 @@ def minimal_subnetwork_model() -> Model: ], ) - model.edge.add(model.flow_boundary[1], model.basin[2], subnetwork_id=2) + model.edge.add(model.flow_boundary[1], model.basin[2]) model.edge.add(model.basin[2], model.pump[3]) model.edge.add(model.pump[3], model.basin[4]) model.edge.add(model.basin[4], model.user_demand[5]) @@ -409,7 +409,7 @@ def allocation_example_model() -> Model: ) model.terminal.add(Node(8, Point(5, 0), subnetwork_id=2)) - model.edge.add(model.flow_boundary[1], model.basin[2], subnetwork_id=2) + model.edge.add(model.flow_boundary[1], model.basin[2]) model.edge.add(model.basin[2], model.user_demand[3]) model.edge.add(model.basin[2], model.linear_resistance[4]) model.edge.add(model.linear_resistance[4], model.basin[5]) @@ -632,7 +632,7 @@ def main_network_with_subnetworks_model() -> Model: [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.flow_boundary[1], model.basin[2]) model.edge.add(model.basin[2], model.linear_resistance[3]) model.edge.add(model.linear_resistance[3], model.basin[4]) model.edge.add(model.basin[4], model.linear_resistance[5]) @@ -690,9 +690,9 @@ def main_network_with_subnetworks_model() -> Model: model.edge.add(model.user_demand[53], model.basin[50]) model.edge.add(model.basin[44], model.user_demand[57]) model.edge.add(model.user_demand[57], model.basin[44]) - 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[2], model.pump[11]) + model.edge.add(model.basin[6], model.pump[24]) + model.edge.add(model.basin[10], model.pump[38]) model.edge.add(model.basin[8], model.user_demand[60]) model.edge.add(model.user_demand[60], model.basin[8]) @@ -713,8 +713,8 @@ def subnetworks_with_sources_model() -> Model: [flow_boundary.Static(flow_rate=[0.003])], ) - model.edge.add(model.flow_boundary[58], model.basin[16], subnetwork_id=3) - model.edge.add(model.flow_boundary[59], model.basin[44], subnetwork_id=7) + model.edge.add(model.flow_boundary[58], model.basin[16]) + model.edge.add(model.flow_boundary[59], model.basin[44]) return model @@ -769,7 +769,7 @@ def level_demand_model() -> Model: [basin.Profile(area=1000.0, level=[0.0, 1.0]), basin.State(level=[2.0])], ) - model.edge.add(model.flow_boundary[1], model.basin[2], subnetwork_id=2) + model.edge.add(model.flow_boundary[1], model.basin[2]) model.edge.add(model.basin[2], model.user_demand[3]) model.edge.add(model.level_demand[4], model.basin[2]) model.edge.add(model.user_demand[3], model.basin[5]) @@ -842,7 +842,6 @@ def flow_demand_model() -> Model: model.edge.add( model.level_boundary[1], model.tabulated_rating_curve[2], - subnetwork_id=2, ) model.edge.add(model.tabulated_rating_curve[2], model.basin[3]) model.edge.add(model.basin[3], model.user_demand[4]) @@ -885,7 +884,7 @@ def linear_resistance_demand_model(): [flow_demand.Static(priority=[1], demand=2.0)], ) - model.edge.add(model.basin[1], model.linear_resistance[2], subnetwork_id=1) + model.edge.add(model.basin[1], model.linear_resistance[2]) model.edge.add(model.linear_resistance[2], model.basin[3]) model.edge.add(model.flow_demand[4], model.linear_resistance[2]) @@ -978,7 +977,7 @@ def fair_distribution_model(): ], ) - model.edge.add(model.level_boundary[1], model.pump[2], subnetwork_id=1) + model.edge.add(model.level_boundary[1], model.pump[2]) model.edge.add(model.pump[2], model.basin[3]) model.edge.add(model.basin[3], model.linear_resistance[4]) model.edge.add(model.linear_resistance[4], model.basin[5]) diff --git a/python/ribasim_testmodels/ribasim_testmodels/invalid.py b/python/ribasim_testmodels/ribasim_testmodels/invalid.py index 995d30993..46473b461 100644 --- a/python/ribasim_testmodels/ribasim_testmodels/invalid.py +++ b/python/ribasim_testmodels/ribasim_testmodels/invalid.py @@ -216,7 +216,6 @@ def invalid_priorities_model() -> Model: model.edge.add( model.level_boundary[1], model.tabulated_rating_curve[2], - subnetwork_id=2, ) model.edge.add(model.tabulated_rating_curve[2], model.basin[3]) model.edge.add(model.basin[3], model.user_demand[4])