Skip to content

Commit

Permalink
Fix node ID bug in allocation (#1330)
Browse files Browse the repository at this point in the history
Fixes #1262.
  • Loading branch information
SouthEndMusic authored Apr 4, 2024
1 parent 9b2a3bf commit 89b4525
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
25 changes: 14 additions & 11 deletions core/src/allocation_init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ This loop finds allocation network edges in several ways:
function find_allocation_graph_edges!(
p::Parameters,
allocation_network_id::Int32,
)::Tuple{Vector{Vector{NodeID}}, SparseMatrixCSC{Float64, Int}}
)::Tuple{
Vector{Vector{NodeID}},
JuMP.Containers.SparseAxisArray{Float64, 2, Tuple{NodeID, NodeID}},
}
(; graph) = p

edges_composite = Vector{NodeID}[]
capacity = spzeros(nv(graph), nv(graph))
dict = Dict{Tuple{NodeID, NodeID}, Float64}()
capacity = JuMP.Containers.SparseAxisArray(dict)

node_ids = graph[].node_ids[allocation_network_id]
edge_ids = Set{Tuple{NodeID, NodeID}}()
Expand Down Expand Up @@ -199,11 +203,11 @@ For the composite allocation network edges:
- Find out their allowed flow direction(s)
"""
function process_allocation_graph_edges!(
capacity::SparseMatrixCSC{Float64, Int},
capacity::JuMP.Containers.SparseAxisArray{Float64, 2, Tuple{NodeID, NodeID}},
edges_composite::Vector{Vector{NodeID}},
p::Parameters,
allocation_network_id::Int32,
)::SparseMatrixCSC{Float64, Int}
)::Nothing
(; graph) = p
node_ids = graph[].node_ids[allocation_network_id]
edge_ids = graph[].edge_ids[allocation_network_id]
Expand Down Expand Up @@ -289,7 +293,7 @@ function process_allocation_graph_edges!(
push!(edge_ids, (node_id_2, node_id_1))
end
end
return capacity
return nothing
end

const allocation_source_nodetypes =
Expand Down Expand Up @@ -320,7 +324,7 @@ Build the graph used for the allocation problem.
function allocation_graph(
p::Parameters,
allocation_network_id::Int32,
)::SparseMatrixCSC{Float64, Int}
)::JuMP.Containers.SparseAxisArray{Float64, 2, Tuple{NodeID, NodeID}}
# Find out which nodes in the subnetwork are used in the allocation network
allocation_graph_used_nodes!(p, allocation_network_id)

Expand Down Expand Up @@ -460,17 +464,16 @@ flow over edge <= edge capacity
"""
function add_constraints_capacity!(
problem::JuMP.Model,
capacity::SparseMatrixCSC{Float64, Int},
capacity::JuMP.Containers.SparseAxisArray{Float64, 2, Tuple{NodeID, NodeID}},
p::Parameters,
allocation_network_id::Int32,
)::Nothing
(; graph) = p
main_network_source_edges = get_main_network_connections(p, allocation_network_id)
F = problem[:F]
edge_ids = graph[].edge_ids[allocation_network_id]
edge_ids_finite_capacity = Tuple{NodeID, NodeID}[]
for edge in edge_ids
if !isinf(capacity[edge...]) && edge main_network_source_edges
for (edge, c) in capacity.data
if !isinf(c) && edge main_network_source_edges
push!(edge_ids_finite_capacity, edge)
end
end
Expand Down Expand Up @@ -909,7 +912,7 @@ Construct the allocation problem for the current subnetwork as a JuMP.jl model.
"""
function allocation_problem(
p::Parameters,
capacity::SparseMatrixCSC{Float64, Int},
capacity::JuMP.Containers.SparseAxisArray{Float64, 2, Tuple{NodeID, NodeID}},
allocation_network_id::Int32,
)::JuMP.Model
optimizer = JuMP.optimizer_with_attributes(HiGHS.Optimizer, "log_to_console" => false)
Expand Down
5 changes: 1 addition & 4 deletions core/src/allocation_optim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,11 @@ function set_initial_capacities_edge!(
allocation_model::AllocationModel,
p::Parameters,
)::Nothing
(; graph) = p
(; problem, capacity, allocation_network_id) = allocation_model
edge_ids = graph[].edge_ids[allocation_network_id]
constraints_capacity = problem[:capacity]
main_network_source_edges = get_main_network_connections(p, allocation_network_id)

for edge_id in edge_ids
c = capacity[edge_id...]
for (edge_id, c) in capacity.data

# These edges have no capacity constraints:
# - With infinite capacity
Expand Down
5 changes: 4 additions & 1 deletion core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ function get_value(

if variable == "level"
if node_id.type == NodeType.Basin
_, basin_idx = id_index(basin.node_id, node_id)
has_index, basin_idx = id_index(basin.node_id, node_id)
if !has_index
error("Discrete control listen node $node_id does not exist.")
end
_, level = get_area_and_level(basin, basin_idx, u[basin_idx])
elseif node_id.type == NodeType.LevelBoundary
level_boundary_idx = findsorted(level_boundary.node_id, node_id)
Expand Down
2 changes: 1 addition & 1 deletion core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ problem: The JuMP.jl model for solving the allocation problem
"""
struct AllocationModel
allocation_network_id::Int32
capacity::SparseMatrixCSC{Float64, Int}
capacity::JuMP.Containers.SparseAxisArray{Float64, 2, Tuple{NodeID, NodeID}}
problem::JuMP.Model
Δt_allocation::Float64
end
Expand Down

0 comments on commit 89b4525

Please sign in to comment.