Skip to content

Commit

Permalink
[WIP] Reenable some version of accumulated_energy_capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsiqueira committed Jan 29, 2025
1 parent 3cd90d7 commit d0e2f3c
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 97 deletions.
204 changes: 109 additions & 95 deletions src/constraints/storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ add_storage_constraints!(model, graph,...)
Adds the storage asset constraints to the model.
"""
function add_storage_constraints!(connection, model, variables, constraints, profiles)
function add_storage_constraints!(connection, model, variables, expressions, constraints, profiles)
var_storage_level_rep_period = variables[:storage_level_rep_period]
var_storage_level_over_clustered_year = variables[:storage_level_over_clustered_year]

Expand Down Expand Up @@ -74,55 +74,60 @@ function add_storage_constraints!(connection, model, variables, constraints, pro
],
)

accumulated_energy_capacity =
expressions[:accumulated_energy_capacity].expressions[:energy_capacity]

# - Maximum storage level
# attach_constraint!(
# model,
# cons,
# :max_storage_level_rep_period_limit,
# [
# begin
# max_storage_level_agg = _profile_aggregate(
# profiles.rep_period,
# (row.max_storage_level_profile_name, row.year, row.rep_period),
# row.time_block_start:row.time_block_end,
# Statistics.mean,
# 1.0,
# )
# @constraint(
# model,
# var_storage_level ≤
# max_storage_level_agg * accumulated_energy_capacity[row.year, row.asset],
# base_name = "max_storage_level_rep_period_limit[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
# )
# end for
# (row, var_storage_level) in zip(indices, var_storage_level_rep_period.container)
# ],
# )
attach_constraint!(
model,
cons,
:max_storage_level_rep_period_limit,
[
begin
max_storage_level_agg = _profile_aggregate(
profiles.rep_period,
(row.max_storage_level_profile_name, row.year, row.rep_period),
row.time_block_start:row.time_block_end,
Statistics.mean,
1.0,
)
@constraint(
model,
var_storage_level
max_storage_level_agg *
accumulated_energy_capacity[row.acc_energy_capacity_index],
base_name = "max_storage_level_rep_period_limit[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
)
end for
(row, var_storage_level) in zip(indices, var_storage_level_rep_period.container)
],
)

# - Minimum storage level
# attach_constraint!(
# model,
# cons,
# :min_storage_level_rep_period_limit,
# [
# begin
# min_storage_level_agg = _profile_aggregate(
# profiles.rep_period,
# (row.min_storage_level_profile_name, row.year, row.rep_period),
# row.time_block_start:row.time_block_end,
# Statistics.mean,
# 0.0,
# )
# @constraint(
# model,
# var_storage_level ≥
# min_storage_level_agg * accumulated_energy_capacity[row.year, row.asset],
# base_name = "min_storage_level_rep_period_limit[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
# )
# end for
# (row, var_storage_level) in zip(indices, var_storage_level_rep_period.container)
# ],
# )
attach_constraint!(
model,
cons,
:min_storage_level_rep_period_limit,
[
begin
min_storage_level_agg = _profile_aggregate(
profiles.rep_period,
(row.min_storage_level_profile_name, row.year, row.rep_period),
row.time_block_start:row.time_block_end,
Statistics.mean,
0.0,
)
@constraint(
model,
var_storage_level
min_storage_level_agg *
accumulated_energy_capacity[row.acc_energy_capacity_index],
base_name = "min_storage_level_rep_period_limit[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
)
end for
(row, var_storage_level) in zip(indices, var_storage_level_rep_period.container)
],
)
end

## INTER-TEMPORAL CONSTRAINTS (between representative periods)
Expand Down Expand Up @@ -184,55 +189,60 @@ function add_storage_constraints!(connection, model, variables, constraints, pro
],
)

accumulated_energy_capacity =
expressions[:accumulated_energy_capacity].expressions[:energy_capacity]

# - Maximum storage level
# attach_constraint!(
# model,
# cons,
# :max_storage_level_over_clustered_year_limit,
# [
# begin
# max_storage_level_agg = _profile_aggregate(
# profiles.over_clustered_year,
# (row.max_storage_level_profile_name, row.year),
# row.period_block_start:row.period_block_end,
# Statistics.mean,
# 1.0,
# )
# @constraint(
# model,
# var_storage_level ≤
# max_storage_level_agg * accumulated_energy_capacity[row.year, row.asset],
# base_name = "max_storage_level_over_clustered_year_limit[$(row.asset),$(row.year),$(row.period_block_start):$(row.period_block_end)]"
# )
# end for (row, var_storage_level) in
# zip(indices, var_storage_level_over_clustered_year.container)
# ],
# )
#
# # - Minimum storage level
# attach_constraint!(
# model,
# cons,
# :min_storage_level_over_clustered_year_limit,
# [
# begin
# min_storage_level_agg = _profile_aggregate(
# profiles.over_clustered_year,
# (row.min_storage_level_profile_name, row.year),
# row.period_block_start:row.period_block_end,
# Statistics.mean,
# 0.0,
# )
# @constraint(
# model,
# var_storage_level ≥
# min_storage_level_agg * accumulated_energy_capacity[row.year, row.asset],
# base_name = "min_storage_level_over_clustered_year_limit[$(row.asset),$(row.year),$(row.period_block_start):$(row.period_block_end)]"
# )
# end for (row, var_storage_level) in
# zip(indices, var_storage_level_over_clustered_year.container)
# ],
# )
attach_constraint!(
model,
cons,
:max_storage_level_over_clustered_year_limit,
[
begin
max_storage_level_agg = _profile_aggregate(
profiles.over_clustered_year,
(row.max_storage_level_profile_name, row.year),
row.period_block_start:row.period_block_end,
Statistics.mean,
1.0,
)
@constraint(
model,
var_storage_level
max_storage_level_agg *
accumulated_energy_capacity[row.acc_energy_capacity_index],
base_name = "max_storage_level_over_clustered_year_limit[$(row.asset),$(row.year),$(row.period_block_start):$(row.period_block_end)]"
)
end for (row, var_storage_level) in
zip(indices, var_storage_level_over_clustered_year.container)
],
)

# - Minimum storage level
attach_constraint!(
model,
cons,
:min_storage_level_over_clustered_year_limit,
[
begin
min_storage_level_agg = _profile_aggregate(
profiles.over_clustered_year,
(row.min_storage_level_profile_name, row.year),
row.period_block_start:row.period_block_end,
Statistics.mean,
0.0,
)
@constraint(
model,
var_storage_level
min_storage_level_agg *
accumulated_energy_capacity[row.acc_energy_capacity_index],
base_name = "min_storage_level_over_clustered_year_limit[$(row.asset),$(row.year),$(row.period_block_start):$(row.period_block_end)]"
)
end for (row, var_storage_level) in
zip(indices, var_storage_level_over_clustered_year.container)
],
)
end
end

Expand All @@ -248,6 +258,7 @@ function _append_storage_data_to_indices(connection, table_name)
inflows_profile.profile_name AS inflows_profile_name,
max_storage_level_profile.profile_name AS max_storage_level_profile_name,
min_storage_level_profile.profile_name AS min_storage_level_profile_name,
expr_acc.index AS acc_energy_capacity_index
FROM cons_$table_name AS cons
LEFT JOIN asset
ON cons.asset = asset.asset
Expand All @@ -257,6 +268,9 @@ function _append_storage_data_to_indices(connection, table_name)
LEFT JOIN asset_milestone
ON cons.asset = asset_milestone.asset
AND cons.year = asset_milestone.milestone_year
LEFT JOIN expr_accumulated_energy_capacity AS expr_acc
ON cons.asset = expr_acc.asset
AND cons.year = expr_acc.milestone_year
LEFT OUTER JOIN assets_profiles AS inflows_profile
ON cons.asset = inflows_profile.asset
AND cons.year = inflows_profile.commission_year
Expand Down
3 changes: 2 additions & 1 deletion src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function create_model(
)

## Expressions for storage assets
@timeit to "add_storage_expressions!" add_storage_expressions!(model, graph, sets, variables)
@timeit to "add_storage_expressions!" add_storage_expressions!(connection, model, expressions)

## Expressions for the objective function
@timeit to "add_objective!" add_objective!(
Expand Down Expand Up @@ -134,6 +134,7 @@ function create_model(
connection,
model,
variables,
expressions,
constraints,
profiles,
)
Expand Down
46 changes: 45 additions & 1 deletion src/expressions/storage.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
function add_storage_expressions!(model, graph, sets, variables)
function add_storage_expressions!(connection, model, expressions)
DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE expr_accumulated_energy_capacity AS
SELECT
nextval('id') AS index,
asset_milestone.asset,
asset_milestone.milestone_year,
ANY_VALUE(asset.capacity_storage_energy) AS capacity_storage_energy,
ARRAY_AGG(expr_acc.index) AS acc_indices,
FROM asset_milestone
LEFT JOIN asset
ON asset_milestone.asset = asset.asset
LEFT JOIN expr_accumulated_units AS expr_acc
ON asset_milestone.asset = expr_acc.asset
AND asset_milestone.milestone_year = expr_acc.milestone_year
GROUP BY
asset_milestone.asset,
asset_milestone.milestone_year
ORDER BY index
",
)

expressions[:accumulated_energy_capacity] =
TulipaExpression(connection, "expr_accumulated_energy_capacity")

expr_acc = expressions[:accumulated_units].expressions[:assets_energy]

# TODO: Reevaluate the accumulated_energy_capacity definition
let table_name = :accumulated_energy_capacity, expr = expressions[table_name]
indices = DuckDB.query(connection, "FROM expr_$table_name")
attach_expression!(
expr,
:energy_capacity,
JuMP.AffExpr[
@expression(
model,
row.capacity_storage_energy *
sum(expr_acc[acc_index] for acc_index in row.acc_indices)
) for row in indices
],
)
end

# assets_investment_energy = variables[:assets_investment_energy].lookup
# assets_decommission_energy_simple_method =
# variables[:assets_decommission_energy_simple_method].lookup
Expand Down

0 comments on commit d0e2f3c

Please sign in to comment.