Skip to content

Commit

Permalink
Merge branch 'main' into UserDemand_outflow_source
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Mar 14, 2024
2 parents 98a20dd + fcee471 commit 4f29259
Show file tree
Hide file tree
Showing 42 changed files with 420 additions and 401 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/python_codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Python Codegen
on:
push:
branches: [main, update/pixi-lock]
paths-ignore: [".teamcity/**"]
tags: ["*"]
pull_request:
merge_group:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
codegen:
name: Codegen
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: prefix-dev/[email protected]
with:
pixi-version: "latest"
- name: Prepare pixi
run: pixi run install-ci
- name: Test if codegen runs without errors
run: pixi run codegen
- name: Ensure that no code has been generated
run: git diff --exit-code
6 changes: 3 additions & 3 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.0"
manifest_format = "2.0"
project_hash = "01c388f950ce89ba903c883c710bf25774a0b7a3"
project_hash = "a66ce8b152bba81b582cbf6d8a2e07a88d016add"

[[deps.ADTypes]]
git-tree-sha1 = "41c37aa88889c171f1300ceac1313c06e891d245"
Expand Down Expand Up @@ -1075,9 +1075,9 @@ version = "6.74.0"

[[deps.OteraEngine]]
deps = ["Markdown", "Pkg", "TOML"]
git-tree-sha1 = "2a8dc8b023055dbfeff7f752bb7fe3e8c9ab60e9"
git-tree-sha1 = "0d57df750e20a4534a117398dc04eb237d86ea70"
uuid = "b2d7f28f-acd6-4007-8b26-bc27716e5513"
version = "0.5.0"
version = "0.4.1"

[[deps.PackageCompiler]]
deps = ["Artifacts", "Glob", "LazyArtifacts", "Libdl", "Pkg", "Printf", "RelocatableFolders", "TOML", "UUIDs", "p7zip_jll"]
Expand Down
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ TestEnv = "1e6cf692-eddd-4d53-88a5-2d735e33781b"
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[compat]
OteraEngine = "0.4"
28 changes: 14 additions & 14 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 @@ -286,7 +286,7 @@ const allocation_source_nodetypes =
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 @@ -306,7 +306,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 All @@ -333,7 +333,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 @@ -348,7 +348,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 @@ -371,7 +371,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 @@ -417,7 +417,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 @@ -467,7 +467,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 @@ -547,7 +547,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 @@ -665,7 +665,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 @@ -730,7 +730,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 @@ -766,7 +766,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 @@ -464,7 +464,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 @@ -484,7 +484,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 @@ -504,9 +504,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 4f29259

Please sign in to comment.