Skip to content

Commit

Permalink
Get rid of Base.RefValue fields
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Nov 26, 2024
1 parent 24ba3e9 commit b4c8eb9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 52 deletions.
24 changes: 12 additions & 12 deletions core/src/allocation_optim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function set_initial_capacities_inlet!(
end
source = sources[edge_id]
@assert source.type == AllocationSourceType.main_to_sub
source.capacity[] = source_capacity
source.capacity = source_capacity
end
return nothing
end
Expand All @@ -216,7 +216,7 @@ function set_initial_capacities_source!(
# Reset the source to the averaged flow over the last allocation period
source = sources[edge]
@assert source.type == AllocationSourceType.edge
source.capacity[] = mean_input_flows[edge][]
source.capacity = mean_input_flows[edge][]
end
end
end
Expand Down Expand Up @@ -244,7 +244,7 @@ function reduce_source_capacity!(problem::JuMP.Model, source::AllocationSource):
error("Unknown source type")
end

source.capacity_reduced[] = max(source.capacity_reduced[] - used_capacity, 0.0)
source.capacity_reduced = max(source.capacity_reduced - used_capacity, 0.0)
return nothing
end

Expand Down Expand Up @@ -275,7 +275,7 @@ function increase_source_capacities!(
continue
end

source.capacity_reduced[] += additional_capacity
source.capacity_reduced += additional_capacity
end
return nothing
end
Expand Down Expand Up @@ -432,7 +432,7 @@ function set_initial_capacities_basin!(
for node_id in only(constraints_outflow.axes)
source = sources[(node_id, node_id)]
@assert source.type == AllocationSourceType.basin
source.capacity[] = get_basin_capacity(allocation_model, u, p, t, node_id)
source.capacity = get_basin_capacity(allocation_model, u, p, t, node_id)
end
return nothing
end
Expand Down Expand Up @@ -502,7 +502,7 @@ function set_initial_capacities_returnflow!(
for node_id in only(constraints_outflow.axes)
source = sources[user_demand.outflow_edge[node_id.idx].edge]
@assert source.type == AllocationSourceType.user_return
source.capacity[] = 0.0
source.capacity = 0.0
end
return nothing
end
Expand Down Expand Up @@ -626,7 +626,7 @@ function set_initial_capacities_buffer!(allocation_model::AllocationModel)::Noth
for node_id in only(constraints_flow_buffer.axes)
source = sources[(node_id, node_id)]
@assert source.type == AllocationSourceType.buffer
source.capacity[] = 0.0
source.capacity = 0.0
end
return nothing
end
Expand Down Expand Up @@ -816,14 +816,14 @@ function allocate_to_users_from_connected_basin!(
# The capacity of the upstream basin
source = sources[(upstream_basin_id, upstream_basin_id)]
@assert source.type == AllocationSourceType.basin
capacity = source.capacity[]
capacity = source.capacity

# The allocated amount
allocated = min(demand, capacity)

# Subtract the allocated amount from the user demand and basin capacity
user_demand.demand_reduced[node_id.idx, priority_idx] -= allocated
source.capacity[] -= allocated
source.capacity -= allocated

# Add the allocated flow
flow[(upstream_basin_id, node_id)] += allocated
Expand Down Expand Up @@ -856,7 +856,7 @@ function set_source_capacity!(
source.type == AllocationSourceType.main_to_sub
Inf
else
source_current.capacity_reduced[]
source_current.capacity_reduced
end
else
0.0
Expand Down Expand Up @@ -900,7 +900,7 @@ function optimize_per_source!(
for source in values(sources)
# Skip source when it has no capacity
if optimization_type !== OptimizationType.collect_demands &&
source.capacity_reduced[] == 0.0
source.capacity_reduced == 0.0
continue
end

Expand Down Expand Up @@ -1030,7 +1030,7 @@ function set_initial_values!(
set_initial_capacities_returnflow!(allocation_model, p)

for source in values(allocation_model.sources)
source.capacity_reduced[] = source.capacity[]
source.capacity_reduced = source.capacity
end

set_initial_demands_user!(allocation_model, p, t)
Expand Down
2 changes: 1 addition & 1 deletion core/src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function update_cumulative_flows!(u, t, integrator)::Nothing
(; vertical_flux) = basin

# Update tprev
p.tprev[] = t
p.tprev = t

# Update cumulative forcings which are integrated exactly
@. basin.cumulative_drainage += vertical_flux.drainage * dt
Expand Down
66 changes: 33 additions & 33 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ type: The type of source (edge, basin, main_to_sub, user_return, buffer)
capacity: The initial capacity of the source as determined by the physical layer
capacity_reduced: The capacity adjusted by passed optimizations
"""
@kwdef struct AllocationSource
edge::Tuple{NodeID, NodeID}
type::AllocationSourceType.T
capacity::Base.RefValue{Float64} = Ref(0.0)
capacity_reduced::Base.RefValue{Float64} = Ref(0.0)
@kwdef mutable struct AllocationSource
const edge::Tuple{NodeID, NodeID}
const type::AllocationSourceType.T
capacity::Float64 = 0.0
capacity_reduced::Float64 = 0.0
end

function Base.show(io::IO, source::AllocationSource)
Expand Down Expand Up @@ -907,38 +907,38 @@ const ModelGraph = MetaGraph{
Float64,
}

@kwdef struct Parameters{C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11}
starttime::DateTime
graph::ModelGraph
allocation::Allocation
basin::Basin{C1, C2, C3, C4}
linear_resistance::LinearResistance
manning_resistance::ManningResistance
tabulated_rating_curve::TabulatedRatingCurve{C5}
level_boundary::LevelBoundary{C6}
flow_boundary::FlowBoundary{C7}
pump::Pump
outlet::Outlet
terminal::Terminal
discrete_control::DiscreteControl
continuous_control::ContinuousControl
pid_control::PidControl
user_demand::UserDemand{C8}
level_demand::LevelDemand
flow_demand::FlowDemand
subgrid::Subgrid
@kwdef mutable struct Parameters{C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11}
const starttime::DateTime
const graph::ModelGraph
const allocation::Allocation
const basin::Basin{C1, C2, C3, C4}
const linear_resistance::LinearResistance
const manning_resistance::ManningResistance
const tabulated_rating_curve::TabulatedRatingCurve{C5}
const level_boundary::LevelBoundary{C6}
const flow_boundary::FlowBoundary{C7}
const pump::Pump
const outlet::Outlet
const terminal::Terminal
const discrete_control::DiscreteControl
const continuous_control::ContinuousControl
const pid_control::PidControl
const user_demand::UserDemand{C8}
const level_demand::LevelDemand
const flow_demand::FlowDemand
const subgrid::Subgrid
# Per state the in- and outflow edges associated with that state (if they exist)
state_inflow_edge::C9 = ComponentVector()
state_outflow_edge::C10 = ComponentVector()
all_nodes_active::Base.RefValue{Bool} = Ref(false)
tprev::Base.RefValue{Float64} = Ref(0.0)
const state_inflow_edge::C9 = ComponentVector()
const state_outflow_edge::C10 = ComponentVector()
all_nodes_active::Bool = false
tprev::Float64 = 0.0
# Sparse matrix for combining flows into storages
flow_to_storage::SparseMatrixCSC{Float64, Int64} = spzeros(1, 1)
const flow_to_storage::SparseMatrixCSC{Float64, Int64} = spzeros(1, 1)
# Water balance tolerances
water_balance_abstol::Float64
water_balance_reltol::Float64
const water_balance_abstol::Float64
const water_balance_reltol::Float64
# State at previous saveat
u_prev_saveat::C11 = ComponentVector()
const u_prev_saveat::C11 = ComponentVector()
end

# To opt-out of type checking for ForwardDiff
Expand Down
4 changes: 2 additions & 2 deletions core/src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function set_current_basin_properties!(
current_cumulative_drainage = current_cumulative_drainage[parent(du)]

# The exact cumulative precipitation and drainage up to the t of this water_balance call
dt = t - p.tprev[]
dt = t - p.tprev
for node_id in basin.node_id
fixed_area = basin_areas(basin, node_id.idx)[end]
current_cumulative_precipitation[node_id.idx] =
Expand Down Expand Up @@ -141,7 +141,7 @@ function formulate_storages!(
end
mul!(current_storage, flow_to_storage, u, 1, 1)
formulate_storage!(current_storage, basin, du)
formulate_storage!(current_storage, tprev[], t, flow_boundary)
formulate_storage!(current_storage, tprev, t, flow_boundary)
return nothing
end

Expand Down
4 changes: 2 additions & 2 deletions core/src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -842,14 +842,14 @@ function relaxed_root(x, threshold)
end

function get_jac_prototype(du0, u0, p, t0)
p.all_nodes_active[] = true
p.all_nodes_active = true
jac_prototype = jacobian_sparsity(
(du, u) -> water_balance!(du, u, p, t0),
du0,
u0,
TracerSparsityDetector(),
)
p.all_nodes_active[] = false
p.all_nodes_active = false
jac_prototype
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 @@ -158,7 +158,7 @@ end
(; problem) = allocation_model
main_source =
allocation_model.sources[(NodeID(:FlowBoundary, 1, p), NodeID(:Basin, 2, p))]
main_source.capacity_reduced[] = 4.5
main_source.capacity_reduced = 4.5
Ribasim.optimize_priority!(allocation_model, u, p, t, 1, OptimizationType.allocate)

# Main network objective function
Expand Down Expand Up @@ -421,7 +421,7 @@ end
(; u) = model.integrator
optimization_type = OptimizationType.internal_sources
Ribasim.set_initial_values!(allocation_model, u, p, t)
sources[(NodeID(:LevelBoundary, 1, p), node_id_with_flow_demand)].capacity_reduced[] =
sources[(NodeID(:LevelBoundary, 1, p), node_id_with_flow_demand)].capacity_reduced =
2e-3

# Priority 1
Expand Down

0 comments on commit b4c8eb9

Please sign in to comment.