Skip to content

Commit

Permalink
Use 32 bit signed integer for all IDs and priorities (#1252)
Browse files Browse the repository at this point in the history
We used to have a test that `typemax(Int64)` was working as a node ID,
but this broke in the add API. Rather than fixing that I think Int32 IDs
will always suffice (goes to over 2 billion), and save some memory and
(uncompressed) output file size.

Also when creating a new table in QGIS it creates Int32:

![image](https://github.com/Deltares/Ribasim/assets/4471859/c12f2baf-bc84-4086-8696-27c7e6d0d9fb)

Diff is fairly large since we are restrictive in our function type
signatures. `Int` is still used for indices.
  • Loading branch information
visr authored Mar 14, 2024
1 parent e1f8b81 commit 08bcb1c
Show file tree
Hide file tree
Showing 19 changed files with 221 additions and 213 deletions.
32 changes: 16 additions & 16 deletions core/src/allocation_init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ end
Find all nodes in the subnetwork which will be used in the allocation network.
Some nodes are skipped to optimize allocation optimization.
"""
function allocation_graph_used_nodes!(p::Parameters, allocation_network_id::Int)::Nothing
function allocation_graph_used_nodes!(p::Parameters, allocation_network_id::Int32)::Nothing
(; graph, basin, fractional_flow, allocation) = p
(; main_network_connections) = allocation

Expand Down Expand Up @@ -105,7 +105,7 @@ This loop finds allocation network edges in several ways:
"""
function find_allocation_graph_edges!(
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Tuple{Vector{Vector{NodeID}}, SparseMatrixCSC{Float64, Int}}
(; graph) = p

Expand Down Expand Up @@ -189,7 +189,7 @@ function process_allocation_graph_edges!(
capacity::SparseMatrixCSC{Float64, Int},
edges_composite::Vector{Vector{NodeID}},
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::SparseMatrixCSC{Float64, Int}
(; graph) = p
node_ids = graph[].node_ids[allocation_network_id]
Expand Down Expand Up @@ -285,7 +285,7 @@ const allocation_source_nodetypes =
"""
Remove allocation UserDemand return flow edges that are upstream of the UserDemand itself.
"""
function avoid_using_own_returnflow!(p::Parameters, allocation_network_id::Int)::Nothing
function avoid_using_own_returnflow!(p::Parameters, allocation_network_id::Int32)::Nothing
(; graph) = p
node_ids = graph[].node_ids[allocation_network_id]
edge_ids = graph[].edge_ids[allocation_network_id]
Expand All @@ -310,7 +310,7 @@ end
Add the edges connecting the main network work to a subnetwork to both the main network
and subnetwork allocation network.
"""
function add_subnetwork_connections!(p::Parameters, allocation_network_id::Int)::Nothing
function add_subnetwork_connections!(p::Parameters, allocation_network_id::Int32)::Nothing
(; graph, allocation) = p
(; main_network_connections) = allocation
edge_ids = graph[].edge_ids[allocation_network_id]
Expand All @@ -330,7 +330,7 @@ Build the graph used for the allocation problem.
"""
function allocation_graph(
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::SparseMatrixCSC{Float64, Int}
# 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 @@ -360,7 +360,7 @@ Non-negativivity constraints are also immediately added to the flow variables.
function add_variables_flow!(
problem::JuMP.Model,
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Nothing
(; graph) = p
edge_ids = graph[].edge_ids[allocation_network_id]
Expand All @@ -375,7 +375,7 @@ The variable indices are the node_ids of the basins in the subnetwork.
function add_variables_basin!(
problem::JuMP.Model,
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Nothing
(; graph) = p
node_ids_basin = [
Expand All @@ -398,7 +398,7 @@ posing the appropriate constraints.
function add_variables_absolute_value!(
problem::JuMP.Model,
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Nothing
(; graph, allocation) = p
(; main_network_connections) = allocation
Expand Down Expand Up @@ -444,7 +444,7 @@ function add_constraints_capacity!(
problem::JuMP.Model,
capacity::SparseMatrixCSC{Float64, Int},
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Nothing
(; graph) = p
main_network_source_edges = get_main_network_connections(p, allocation_network_id)
Expand Down Expand Up @@ -476,7 +476,7 @@ flow over source edge <= source flow in subnetwork
function add_constraints_source!(
problem::JuMP.Model,
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Nothing
(; graph) = p
edge_ids = graph[].edge_ids[allocation_network_id]
Expand Down Expand Up @@ -556,7 +556,7 @@ sum(flows out of node node) == flows into node + flow from storage and vertical
function add_constraints_flow_conservation!(
problem::JuMP.Model,
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Nothing
(; graph) = p
F = problem[:F]
Expand Down Expand Up @@ -594,7 +594,7 @@ outflow from user_demand <= return factor * inflow to user_demand
function add_constraints_user_demand_returnflow!(
problem::JuMP.Model,
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Nothing
(; graph, user_demand) = p
F = problem[:F]
Expand Down Expand Up @@ -710,7 +710,7 @@ flow after fractional_flow node <= fraction * inflow
function add_constraints_fractional_flow!(
problem::JuMP.Model,
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Nothing
(; graph, fractional_flow) = p
F = problem[:F]
Expand Down Expand Up @@ -775,7 +775,7 @@ Construct the allocation problem for the current subnetwork as a JuMP.jl model.
function allocation_problem(
p::Parameters,
capacity::SparseMatrixCSC{Float64, Int},
allocation_network_id::Int,
allocation_network_id::Int32,
)::JuMP.Model
optimizer = JuMP.optimizer_with_attributes(HiGHS.Optimizer, "log_to_console" => false)
problem = JuMP.direct_model(optimizer)
Expand Down Expand Up @@ -811,7 +811,7 @@ Outputs
An AllocationModel object.
"""
function AllocationModel(
allocation_network_id::Int,
allocation_network_id::Int32,
p::Parameters,
Δt_allocation::Float64,
)::AllocationModel
Expand Down
8 changes: 4 additions & 4 deletions core/src/allocation_optim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ function save_demands_and_allocations!(
push!(record_demand.time, t)
push!(record_demand.subnetwork_id, allocation_network_id)
push!(record_demand.node_type, string(node_id.type))
push!(record_demand.node_id, Int(node_id))
push!(record_demand.node_id, Int32(node_id))
push!(record_demand.priority, priorities[priority_idx])
push!(record_demand.demand, demand)
push!(record_demand.allocated, allocated)
Expand All @@ -458,7 +458,7 @@ function save_allocation_flows!(
p::Parameters,
t::Float64,
allocation_model::AllocationModel,
priority::Int,
priority::Int32,
collect_demands::Bool,
)::Nothing
(; problem, allocation_network_id) = allocation_model
Expand All @@ -478,9 +478,9 @@ function save_allocation_flows!(
push!(record_flow.time, t)
push!(record_flow.edge_id, edge_metadata.id)
push!(record_flow.from_node_type, string(node_ids[i].type))
push!(record_flow.from_node_id, Int(node_ids[i]))
push!(record_flow.from_node_id, Int32(node_ids[i]))
push!(record_flow.to_node_type, string(node_ids[i + 1].type))
push!(record_flow.to_node_id, Int(node_ids[i + 1]))
push!(record_flow.to_node_id, Int32(node_ids[i + 1]))
push!(record_flow.subnetwork_id, allocation_network_id)
push!(record_flow.priority, priority)
push!(record_flow.flow_rate, flow_rate)
Expand Down
6 changes: 3 additions & 3 deletions core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ function discrete_control_affect!(
record = discrete_control.record

push!(record.time, integrator.t)
push!(record.control_node_id, Int(discrete_control_node_id))
push!(record.control_node_id, Int32(discrete_control_node_id))
push!(record.truth_state, truth_state_used)
push!(record.control_state, control_state_new)

Expand All @@ -347,7 +347,7 @@ function discrete_control_affect!(
return control_state_change
end

function get_allocation_model(p::Parameters, allocation_network_id::Int)::AllocationModel
function get_allocation_model(p::Parameters, allocation_network_id::Int32)::AllocationModel
(; allocation) = p
(; allocation_network_ids, allocation_models) = allocation
idx = findsorted(allocation_network_ids, allocation_network_id)
Expand All @@ -360,7 +360,7 @@ end

function get_main_network_connections(
p::Parameters,
allocation_network_id::Int,
allocation_network_id::Int32,
)::Vector{Tuple{NodeID, NodeID}}
(; allocation) = p
(; allocation_network_ids, main_network_connections) = allocation
Expand Down
6 changes: 3 additions & 3 deletions core/src/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ function create_graph(db::DB, config::Config, chunk_sizes::Vector{Int})::MetaGra
"SELECT fid, from_node_type, from_node_id, to_node_type, to_node_id, edge_type, subnetwork_id FROM Edge ORDER BY fid",
)
# Node IDs per subnetwork
node_ids = Dict{Int, Set{NodeID}}()
node_ids = Dict{Int32, Set{NodeID}}()
# Allocation edges per subnetwork
edge_ids = Dict{Int, Set{Tuple{NodeID, NodeID}}}()
edge_ids = Dict{Int32, Set{Tuple{NodeID, NodeID}}}()
# Source edges per subnetwork
edges_source = Dict{Int, Set{EdgeMetadata}}()
edges_source = Dict{Int32, Set{EdgeMetadata}}()
# The number of flow edges
flow_counter = 0
# Dictionary from flow edge to index in flow vector
Expand Down
56 changes: 28 additions & 28 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ NodeType.T(str::AbstractString) = NodeType.T(Symbol(str))

struct NodeID
type::NodeType.T
value::Int
value::Int32
end

NodeID(type::Symbol, value::Int) = NodeID(NodeType.T(type), value)
NodeID(type::AbstractString, value::Int) = NodeID(NodeType.T(type), value)
NodeID(type::Symbol, value::Integer) = NodeID(NodeType.T(type), value)
NodeID(type::AbstractString, value::Integer) = NodeID(NodeType.T(type), value)

Base.Int(id::NodeID) = id.value
Base.convert(::Type{Int}, id::NodeID) = id.value
Base.Int32(id::NodeID) = id.value
Base.convert(::Type{Int32}, id::NodeID) = id.value
Base.broadcastable(id::NodeID) = Ref(id)
Base.show(io::IO, id::NodeID) = print(io, id.type, " #", Int(id))
Base.show(io::IO, id::NodeID) = print(io, id.type, " #", id.value)

function Base.isless(id_1::NodeID, id_2::NodeID)::Bool
if id_1.type != id_2.type
error("Cannot compare NodeIDs of different types")
end
return Int(id_1) < Int(id_2)
return id_1.value < id_2.value
end

Base.to_index(id::NodeID) = Int(id)
Base.to_index(id::NodeID) = Int(id.value)

const ScalarInterpolation =
LinearInterpolation{Vector{Float64}, Vector{Float64}, true, Float64}
Expand All @@ -49,7 +49,7 @@ problem: The JuMP.jl model for solving the allocation problem
Δt_allocation: The time interval between consecutive allocation solves
"""
struct AllocationModel
allocation_network_id::Int
allocation_network_id::Int32
capacity::SparseMatrixCSC{Float64, Int}
problem::JuMP.Model
Δt_allocation::Float64
Expand All @@ -68,31 +68,31 @@ record_flow: A record of all flows computed by allocation optimization, eventual
output file
"""
struct Allocation
allocation_network_ids::Vector{Int}
allocation_network_ids::Vector{Int32}
allocation_models::Vector{AllocationModel}
main_network_connections::Vector{Vector{Tuple{NodeID, NodeID}}}
priorities::Vector{Int}
priorities::Vector{Int32}
subnetwork_demands::Dict{Tuple{NodeID, NodeID}, Vector{Float64}}
subnetwork_allocateds::Dict{Tuple{NodeID, NodeID}, Vector{Float64}}
record_demand::@NamedTuple{
time::Vector{Float64},
subnetwork_id::Vector{Int},
subnetwork_id::Vector{Int32},
node_type::Vector{String},
node_id::Vector{Int},
priority::Vector{Int},
node_id::Vector{Int32},
priority::Vector{Int32},
demand::Vector{Float64},
allocated::Vector{Float64},
realized::Vector{Float64},
}
record_flow::@NamedTuple{
time::Vector{Float64},
edge_id::Vector{Int},
edge_id::Vector{Int32},
from_node_type::Vector{String},
from_node_id::Vector{Int},
from_node_id::Vector{Int32},
to_node_type::Vector{String},
to_node_id::Vector{Int},
subnetwork_id::Vector{Int},
priority::Vector{Int},
to_node_id::Vector{Int32},
subnetwork_id::Vector{Int32},
priority::Vector{Int32},
flow_rate::Vector{Float64},
collect_demands::BitVector,
}
Expand All @@ -107,7 +107,7 @@ allocation_network_id: Allocation network ID (0 if not in subnetwork)
"""
struct NodeMetadata
type::Symbol
allocation_network_id::Int
allocation_network_id::Int32
end

"""
Expand All @@ -123,9 +123,9 @@ node_ids: if this edge has allocation flow, these are all the
nodes from the physical layer this edge consists of
"""
struct EdgeMetadata
id::Int
id::Int32
type::EdgeType.T
allocation_network_id_source::Int
allocation_network_id_source::Int32
from_id::NodeID
to_id::NodeID
allocation_flow::Bool
Expand Down Expand Up @@ -447,7 +447,7 @@ struct DiscreteControl <: AbstractParameterNode
logic_mapping::Dict{Tuple{NodeID, String}, String}
record::@NamedTuple{
time::Vector{Float64},
control_node_id::Vector{Int},
control_node_id::Vector{Int32},
truth_state::Vector{String},
control_state::Vector{String},
}
Expand Down Expand Up @@ -535,12 +535,12 @@ struct LevelDemand
node_id::Vector{NodeID}
min_level::Vector{LinearInterpolation}
max_level::Vector{LinearInterpolation}
priority::Vector{Int}
priority::Vector{Int32}
end

"Subgrid linearly interpolates basin levels."
struct Subgrid
basin_index::Vector{Int}
basin_index::Vector{Int32}
interpolations::Vector{ScalarInterpolation}
level::Vector{Float64}
end
Expand All @@ -555,9 +555,9 @@ struct Parameters{T, C1, C2}
NodeMetadata,
EdgeMetadata,
@NamedTuple{
node_ids::Dict{Int, Set{NodeID}},
edge_ids::Dict{Int, Set{Tuple{NodeID, NodeID}}},
edges_source::Dict{Int, Set{EdgeMetadata}},
node_ids::Dict{Int32, Set{NodeID}},
edge_ids::Dict{Int32, Set{Tuple{NodeID, NodeID}}},
edges_source::Dict{Int32, Set{EdgeMetadata}},
flow_dict::Dict{Tuple{NodeID, NodeID}, Int},
flow::T,
flow_prev::Vector{Float64},
Expand Down
Loading

0 comments on commit 08bcb1c

Please sign in to comment.