Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsiqueira committed Jan 7, 2025
1 parent 61087ae commit 073dad7
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions src/model-preparation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ This strategy is based on the replies in this discourse thread:
function add_expression_terms_rep_period_constraints!(
connection,
cons::TulipaConstraint,
flow::TulipaVariable,
workspace;
flow::TulipaVariable;
use_highest_resolution = true,
multiply_by_duration = true,
add_min_outgoing_flow_duration = false,
Expand All @@ -43,6 +42,11 @@ function add_expression_terms_rep_period_constraints!(
),
]
num_rows = size(cons.indices, 1)
Tmax = only(
row[1] for
row in DuckDB.query(connection, "SELECT MAX(num_timesteps) FROM rep_periods_data")
)::Int32
workspace = [Dict{Int,Float64}() for _ in 1:Tmax]

for case in cases
attach_expression!(cons, case.expr_key, Vector{JuMP.AffExpr}(undef, num_rows))
Expand All @@ -61,7 +65,8 @@ function add_expression_terms_rep_period_constraints!(
attach_coefficient!(cons, :min_outgoing_flow_duration, ones(num_rows))
end

resolution_query = multiply_by_duration ? "ANY_VALUE(rep_periods_data.resolution)" : "1.0"
resolution_query =
multiply_by_duration ? "ANY_VALUE(rep_periods_data.resolution)" : "1.0::FLOAT8"
# Walk through (asset, year, rep_period)
for group_row in DuckDB.query(
connection,
Expand All @@ -83,12 +88,11 @@ function add_expression_terms_rep_period_constraints!(
GROUP BY cons.asset, cons.year, cons.rep_period
",
)
asset, year, rep_period, resolution =
group_row.asset, group_row.year, group_row.rep_period, group_row.resolution
# for i in eachindex(workspace)
# workspace[i] = JuMP.AffExpr(0.0)
# end
pre_workspace = [Dict{Int,Float64}() for _ in 1:length(workspace)]
asset = group_row.asset::String
year = group_row.year::Int32
rep_period = group_row.rep_period::Int32
resolution = group_row.resolution::Float64
empty!.(workspace)
outgoing_flow_durations = typemax(Int64) #LARGE_NUMBER to start finding the minimum outgoing flow duration
# Store the corresponding flow in the workspace
for var_row in DuckDB.query(
Expand All @@ -101,51 +105,53 @@ function add_expression_terms_rep_period_constraints!(
AND var.rep_period = $rep_period
",
)
for t in var_row.time_block_start:var_row.time_block_end
time_block = (var_row.time_block_start:var_row.time_block_end)::UnitRange{Int32}
var_idx = var_row.index::Int64
for t in time_block
# Set the efficiency to 1 for inflows and outflows of hub and consumer assets, and outflows for producer assets
# And when you want the highest resolution (which is asset type-agnostic)
efficiency_coefficient =
if group_row.type in case.selected_assets || use_highest_resolution
if group_row.type::String in case.selected_assets || use_highest_resolution
1.0
else
efficiency = var_row.efficiency::Float64
if case.expr_key == :incoming
var_row.efficiency
efficiency::Float64
else
# Divide by efficiency for outgoing flows
1.0 / var_row.efficiency
1.0 / efficiency::Float64
end
end
pre_workspace[t][var_row.index] = resolution * efficiency_coefficient
workspace[t][var_idx] = resolution * efficiency_coefficient
if conditions_to_add_min_outgoing_flow_duration
outgoing_flow_durations = min(
outgoing_flow_durations,
var_row.time_block_end - var_row.time_block_start + 1,
(var_row.time_block_end - var_row.time_block_start + 1)::Int64,
)
end
end
end

# Sum the corresponding flows from the workspace
for (index, time_block_start, time_block_end) in zip(
group_row.agg_index,
group_row.agg_time_block_start,
group_row.agg_time_block_end,
group_row.agg_index::Vector{Union{Missing,Int64}},
group_row.agg_time_block_start::Vector{Union{Missing,Int32}},
group_row.agg_time_block_end::Vector{Union{Missing,Int32}},
)
time_block = time_block_start:time_block_end
pre_workspace_agg = Dict{Int,Float64}()
workspace_agg = Dict{Int,Float64}()
for t in time_block
for (var_idx, var_coefficient) in pre_workspace[t]
if !haskey(pre_workspace_agg, var_idx)
pre_workspace_agg[var_idx] = var_coefficient
for (var_idx, var_coefficient) in workspace[t]
if !haskey(workspace_agg, var_idx)
workspace_agg[var_idx] = var_coefficient
elseif multiply_by_duration
pre_workspace_agg[var_idx] += var_coefficient
workspace_agg[var_idx] += var_coefficient
end
end
end
if length(pre_workspace_agg) > 0
if length(workspace_agg) > 0
cons.expressions[case.expr_key][index] = sum(
duration * flow.container[var_idx] for
(var_idx, duration) in pre_workspace_agg
duration * flow.container[var_idx] for (var_idx, duration) in workspace_agg
)
end
if conditions_to_add_min_outgoing_flow_duration
Expand Down Expand Up @@ -378,32 +384,28 @@ function add_expressions_to_constraints!(
@timeit to "add_expression_terms_rep_period_constraints!" add_expression_terms_rep_period_constraints!(
connection,
constraints[:balance_conversion],
variables[:flow],
expression_workspace;
variables[:flow];
use_highest_resolution = false,
multiply_by_duration = true,
)
@timeit to "add_expression_terms_rep_period_constraints!" add_expression_terms_rep_period_constraints!(
connection,
constraints[:balance_storage_rep_period],
variables[:flow],
expression_workspace;
variables[:flow];
use_highest_resolution = false,
multiply_by_duration = true,
)
@timeit to "add_expression_terms_rep_period_constraints!" add_expression_terms_rep_period_constraints!(
connection,
constraints[:balance_consumer],
variables[:flow],
expression_workspace;
variables[:flow];
use_highest_resolution = true,
multiply_by_duration = false,
)
@timeit to "add_expression_terms_rep_period_constraints!" add_expression_terms_rep_period_constraints!(
connection,
constraints[:balance_hub],
variables[:flow],
expression_workspace;
variables[:flow];
use_highest_resolution = true,
multiply_by_duration = false,
)
Expand All @@ -419,8 +421,7 @@ function add_expressions_to_constraints!(
@timeit to "add_expression_terms_rep_period_constraints! for $table_name" add_expression_terms_rep_period_constraints!(
connection,
constraints[table_name],
variables[:flow],
expression_workspace;
variables[:flow];
use_highest_resolution = true,
multiply_by_duration = false,
)
Expand All @@ -442,8 +443,7 @@ function add_expressions_to_constraints!(
@timeit to "add_expression_terms_rep_period_constraints!" add_expression_terms_rep_period_constraints!(
connection,
constraints[table_name],
variables[:flow],
expression_workspace;
variables[:flow];
use_highest_resolution = true,
multiply_by_duration = false,
add_min_outgoing_flow_duration = true,
Expand Down

0 comments on commit 073dad7

Please sign in to comment.