Skip to content

Commit

Permalink
Pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Nov 27, 2024
1 parent eeba58a commit 4344c12
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 66 deletions.
76 changes: 39 additions & 37 deletions core/src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 2 additions & 12 deletions core/test/validation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/guide/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
27 changes: 13 additions & 14 deletions python/ribasim_testmodels/ribasim_testmodels/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])

Expand All @@ -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

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])

Expand Down Expand Up @@ -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])
Expand Down
1 change: 0 additions & 1 deletion python/ribasim_testmodels/ribasim_testmodels/invalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 4344c12

Please sign in to comment.