Skip to content

Commit

Permalink
Collect demands with empty sources (apart from the main network inlet)
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Apr 25, 2024
1 parent 3ea6ac6 commit 1b8f175
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 26 additions & 2 deletions core/src/allocation_optim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ end

"""
Set the capacities of the sources in the subnetwork
as the latest instantaneous flow out of the source in the physical layer
as the average flow over the last Δt_allocation of the source in the physical layer
"""
function set_initial_capacities_source!(
allocation_model::AllocationModel,
Expand Down Expand Up @@ -996,6 +996,26 @@ function set_initial_values!(
return nothing
end

"""
Set the capacities of all edges that denote a source to 0.0.
"""
function empty_sources!(allocation_model::AllocationModel, allocation::Allocation)::Nothing
(; problem) = allocation_model
(; subnetwork_demands) = allocation

for constraint_set_name in [:source, :source_user, :basin_outflow, :flow_buffer_outflow]
constraint_set = problem[constraint_set_name]
for key in only(constraint_set.axes)
# Do not set the capacity to 0.0 if the edge
# is a main to subnetwork connection edge
if key keys(subnetwork_demands)
JuMP.set_normalized_rhs(constraint_set[key], 0.0)
end
end
end
return nothing
end

"""
Update the allocation optimization problem for the given subnetwork with the problem state
and flows, solve the allocation problem and assign the results to the UserDemand.
Expand Down Expand Up @@ -1027,7 +1047,11 @@ function allocate!(

set_initial_capacities_inlet!(allocation_model, p, optimization_type)

if optimization_type != OptimizationType.collect_demands
if optimization_type == OptimizationType.collect_demands
# When collecting demands, only flow should be available
# from the main to subnetwork connections
empty_sources!(allocation_model, allocation)
else
set_initial_values!(allocation_model, p, u, t)
end

Expand Down
4 changes: 2 additions & 2 deletions core/test/allocation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ end
@test user_demand.allocated[7, :] [0.001, 0.0, 0.0]
end

@testitem "subnetworks with sources" begin
@testitem "Subnetworks with sources" begin
using SQLite
using Ribasim: NodeID, OptimizationType
using ComponentArrays: ComponentVector
Expand Down Expand Up @@ -307,7 +307,7 @@ end
# See the difference between these values here and in
# "allocation with main network optimization problem", internal sources
# lower the subnetwork demands
@test subnetwork_demands[(NodeID(:Basin, 2), NodeID(:Pump, 11))] [3.1, 4.0, 0.0]
@test subnetwork_demands[(NodeID(:Basin, 2), NodeID(:Pump, 11))] [4.0, 4.0, 0.0]
@test subnetwork_demands[(NodeID(:Basin, 6), NodeID(:Pump, 24))] [0.004, 0.0, 0.0]
@test subnetwork_demands[(NodeID(:Basin, 10), NodeID(:Pump, 38))][1:2] [0.001, 0.001]
end
Expand Down

0 comments on commit 1b8f175

Please sign in to comment.