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 073dad7 commit ca58367
Showing 1 changed file with 50 additions and 27 deletions.
77 changes: 50 additions & 27 deletions src/model-preparation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,56 +65,79 @@ 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::FLOAT8"
resolution_query = multiply_by_duration ? "rep_periods_data.resolution" : "1.0::FLOAT8"
# Walk through (asset, year, rep_period)
for group_row in DuckDB.query(
connection,
"SELECT
cons.asset,
cons.year,
cons.rep_period,
ARRAY_AGG(cons.index) AS agg_index,
ARRAY_AGG(cons.time_block_start) AS agg_time_block_start,
ARRAY_AGG(cons.time_block_end) AS agg_time_block_end,
ANY_VALUE(asset.type) AS type,
cons.index AS cons_idx,
cons.time_block_start AS cons_time_block_start,
cons.time_block_end AS cons_time_block_end,
var.index AS var_idx,
var.time_block_start AS var_time_block_start,
var.time_block_end AS var_time_block_end,
var.efficiency,
asset.type AS type,
$resolution_query AS resolution,
FROM $(cons.table_name) AS cons
FROM (
SELECT
cons.asset,
cons.year,
cons.rep_period,
ARRAY_AGG(cons.index) AS index,
ARRAY_AGG(cons.time_block_start) AS time_block_start,
ARRAY_AGG(cons.time_block_end) AS time_block_end,
FROM $(cons.table_name) AS cons
GROUP BY cons.asset, cons.year, cons.rep_period
) AS cons
LEFT JOIN (
SELECT
var.$(case.asset_match) AS asset,
var.year,
var.rep_period,
ARRAY_AGG(var.index) AS index,
ARRAY_AGG(var.time_block_start) AS time_block_start,
ARRAY_AGG(var.time_block_end) AS time_block_end,
ARRAY_AGG(var.efficiency) AS efficiency,
FROM $(flow.table_name) AS var
GROUP BY asset, var.year, var.rep_period
) AS var
ON cons.asset = var.asset
AND cons.year = var.year
AND cons.rep_period = var.rep_period
LEFT JOIN asset
ON cons.asset = asset.asset
LEFT JOIN rep_periods_data
ON cons.rep_period = rep_periods_data.rep_period
AND cons.year = rep_periods_data.year
GROUP BY cons.asset, cons.year, cons.rep_period
WHERE
len(var.index) > 0
",
)
asset = group_row.asset::String
year = group_row.year::Int32
rep_period = group_row.rep_period::Int32
# 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(
connection,
"SELECT *
FROM $(flow.table_name) AS var
WHERE
var.$(case.asset_match) = '$asset'
AND var.year = $year
AND var.rep_period = $rep_period
",
for (var_idx, time_block_start, time_block_end, efficiency) in zip(
group_row.var_idx::Vector{Union{Missing,Int64}},
group_row.var_time_block_start::Vector{Union{Missing,Int32}},
group_row.var_time_block_end::Vector{Union{Missing,Int32}},
group_row.efficiency::Vector{Union{Missing,Float64}},
)
time_block = (var_row.time_block_start:var_row.time_block_end)::UnitRange{Int32}
var_idx = var_row.index::Int64
time_block = time_block_start:time_block_end
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::String in case.selected_assets || use_highest_resolution
1.0
else
efficiency = var_row.efficiency::Float64
if case.expr_key == :incoming
efficiency::Float64
else
Expand All @@ -126,17 +149,17 @@ function add_expression_terms_rep_period_constraints!(
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)::Int64,
(time_block_end - 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::Vector{Union{Missing,Int64}},
group_row.agg_time_block_start::Vector{Union{Missing,Int32}},
group_row.agg_time_block_end::Vector{Union{Missing,Int32}},
group_row.cons_idx::Vector{Union{Missing,Int64}},
group_row.cons_time_block_start::Vector{Union{Missing,Int32}},
group_row.cons_time_block_end::Vector{Union{Missing,Int32}},
)
time_block = time_block_start:time_block_end
workspace_agg = Dict{Int,Float64}()
Expand Down

0 comments on commit ca58367

Please sign in to comment.