Skip to content

Commit

Permalink
Create all variables with connection instead of dataframes variable. (#…
Browse files Browse the repository at this point in the history
…926)

Separate creation of union and lowest resolution tables from
storage_level_intra_rp creation.

Closes #923
  • Loading branch information
abelsiqueira authored Nov 25, 2024
1 parent 38c20d4 commit 127db68
Show file tree
Hide file tree
Showing 16 changed files with 511 additions and 282 deletions.
9 changes: 5 additions & 4 deletions src/constraints/ramping-and-unit-commitment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Adds the ramping constraints for producer and conversion assets where ramping =
"""
function add_ramping_constraints!(
model,
variables,
graph,
df_units_on_and_outflows,
df_units_on,
df_highest_out,
outgoing_flow_highest_out_resolution,
accumulated_units,
Expand Down Expand Up @@ -55,9 +55,10 @@ function add_ramping_constraints!(
model[:limit_units_on] = [
@constraint(
model,
row.units_on accumulated_units[accumulated_units_lookup[(row.asset, row.year)]],
base_name = "limit_units_on[$(row.asset),$(row.year),$(row.rep_period),$(row.timesteps_block)]"
) for row in eachrow(df_units_on)
units_on accumulated_units[accumulated_units_lookup[(row.asset, row.year)]],
base_name = "limit_units_on[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for (units_on, row) in
zip(variables[:units_on].container, eachrow(variables[:units_on].indices))
]

# - Minimum output flow above the minimum operating point
Expand Down
70 changes: 37 additions & 33 deletions src/constraints/storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,41 @@ Adds the storage asset constraints to the model.

function add_storage_constraints!(
model,
variables,
graph,
dataframes,
accumulated_energy_capacity,
incoming_flow_lowest_storage_resolution_intra_rp,
outgoing_flow_lowest_storage_resolution_intra_rp,
df_storage_intra_rp_balance_grouped,
df_storage_inter_rp_balance_grouped,
storage_level_intra_rp,
storage_level_inter_rp,
incoming_flow_storage_inter_rp_balance,
outgoing_flow_storage_inter_rp_balance,
)

## INTRA-TEMPORAL CONSTRAINTS (within a representative period)
storage_level_intra_rp = variables[:storage_level_intra_rp]
df_storage_intra_rp_balance_grouped =
DataFrames.groupby(storage_level_intra_rp.indices, [:asset, :year, :rep_period])

storage_level_inter_rp = variables[:storage_level_inter_rp]
df_storage_inter_rp_balance_grouped =
DataFrames.groupby(storage_level_inter_rp.indices, [:asset, :year])

# - Balance constraint (using the lowest temporal resolution)
for ((a, rp, y), sub_df) in pairs(df_storage_intra_rp_balance_grouped)
for ((a, y, rp), sub_df) in pairs(df_storage_intra_rp_balance_grouped)
# This assumes an ordering of the time blocks, that is guaranteed inside
# construct_dataframes
# The storage_inflows have been moved here
model[Symbol("storage_intra_rp_balance_$(a)_$(y)_$(rp)")] = [
@constraint(
model,
storage_level_intra_rp[row.index] ==
storage_level_intra_rp.container[row.index] ==
(
if k > 1
storage_level_intra_rp[row.index-1] # This assumes contiguous index
storage_level_intra_rp.container[row.index-1] # This assumes contiguous index
else
(
if ismissing(graph[a].initial_storage_level[row.year])
storage_level_intra_rp[last(sub_df.index)]
storage_level_intra_rp.container[last(sub_df.index)]
else
graph[a].initial_storage_level[row.year]
end
Expand All @@ -51,12 +55,12 @@ function add_storage_constraints!(
row.year,
row.year,
("inflows", rp),
row.timesteps_block,
row.time_block_start:row.time_block_end,
0.0,
) * graph[a].storage_inflows[row.year] +
incoming_flow_lowest_storage_resolution_intra_rp[row.index] -
outgoing_flow_lowest_storage_resolution_intra_rp[row.index],
base_name = "storage_intra_rp_balance[$a,$y,$rp,$(row.timesteps_block)]"
base_name = "storage_intra_rp_balance[$a,$y,$rp,$(row.time_block_start:row.time_block_end)]"
) for (k, row) in enumerate(eachrow(sub_df))
]
end
Expand All @@ -65,44 +69,44 @@ function add_storage_constraints!(
model[:max_storage_level_intra_rp_limit] = [
@constraint(
model,
storage_level_intra_rp[row.index]
storage_level_intra_rp.container[row.index]
profile_aggregation(
Statistics.mean,
graph[row.asset].rep_periods_profiles,
row.year,
row.year,
("max-storage-level", row.rep_period),
row.timesteps_block,
row.time_block_start:row.time_block_end,
1.0,
) * accumulated_energy_capacity[row.year, row.asset],
base_name = "max_storage_level_intra_rp_limit[$(row.asset),$(row.year),$(row.rep_period),$(row.timesteps_block)]"
) for row in eachrow(dataframes[:storage_level_intra_rp])
base_name = "max_storage_level_intra_rp_limit[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(storage_level_intra_rp.indices)
]

# - Minimum storage level
model[:min_storage_level_intra_rp_limit] = [
@constraint(
model,
storage_level_intra_rp[row.index]
storage_level_intra_rp.container[row.index]
profile_aggregation(
Statistics.mean,
graph[row.asset].rep_periods_profiles,
row.year,
row.year,
("min_storage_level", row.rep_period),
row.timesteps_block,
row.time_block_start:row.time_block_end,
0.0,
) * accumulated_energy_capacity[row.year, row.asset],
base_name = "min_storage_level_intra_rp_limit[$(row.asset),$(row.year),$(row.rep_period),$(row.timesteps_block)]"
) for row in eachrow(dataframes[:storage_level_intra_rp])
base_name = "min_storage_level_intra_rp_limit[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for row in eachrow(storage_level_intra_rp.indices)
]

# - Cycling condition
for ((a, _, y), sub_df) in pairs(df_storage_intra_rp_balance_grouped)
for ((a, y, _), sub_df) in pairs(df_storage_intra_rp_balance_grouped)
# Ordering is assumed
if !ismissing(graph[a].initial_storage_level[y])
JuMP.set_lower_bound(
storage_level_intra_rp[last(sub_df.index)],
storage_level_intra_rp.container[last(sub_df.index)],
graph[a].initial_storage_level[y],
)
end
Expand All @@ -118,14 +122,14 @@ function add_storage_constraints!(
model[Symbol("storage_inter_rp_balance_$(a)_$(y)")] = [
@constraint(
model,
storage_level_inter_rp[row.index] ==
storage_level_inter_rp.container[row.index] ==
(
if k > 1
storage_level_inter_rp[row.index-1] # This assumes contiguous index
storage_level_inter_rp.container[row.index-1] # This assumes contiguous index
else
(
if ismissing(graph[a].initial_storage_level[row.year])
storage_level_inter_rp[last(sub_df.index)]
storage_level_inter_rp.container[last(sub_df.index)]
else
graph[a].initial_storage_level[row.year]
end
Expand All @@ -135,7 +139,7 @@ function add_storage_constraints!(
row.inflows_profile_aggregation +
incoming_flow_storage_inter_rp_balance[row.index] -
outgoing_flow_storage_inter_rp_balance[row.index],
base_name = "storage_inter_rp_balance[$a,$(row.year),$(row.periods_block)]"
base_name = "storage_inter_rp_balance[$a,$(row.year),$(row.period_block_start):$(row.period_block_end)]"
) for (k, row) in enumerate(eachrow(sub_df))
]
end
Expand All @@ -144,44 +148,44 @@ function add_storage_constraints!(
model[:max_storage_level_inter_rp_limit] = [
@constraint(
model,
storage_level_inter_rp[row.index]
storage_level_inter_rp.container[row.index]
profile_aggregation(
Statistics.mean,
graph[row.asset].timeframe_profiles,
row.year,
row.year,
"max_storage_level",
row.periods_block,
row.period_block_start:row.period_block_end,
1.0,
) * accumulated_energy_capacity[row.year, row.asset],
base_name = "max_storage_level_inter_rp_limit[$(row.asset),$(row.year),$(row.periods_block)]"
) for row in eachrow(dataframes[:storage_level_inter_rp])
base_name = "max_storage_level_inter_rp_limit[$(row.asset),$(row.year),$(row.period_block_start):$(row.period_block_end)]"
) for row in eachrow(storage_level_inter_rp.indices)
]

# - Minimum storage level
model[:min_storage_level_inter_rp_limit] = [
@constraint(
model,
storage_level_inter_rp[row.index]
storage_level_inter_rp.container[row.index]
profile_aggregation(
Statistics.mean,
graph[row.asset].timeframe_profiles,
row.year,
row.year,
"min_storage_level",
row.periods_block,
row.period_block_start:row.period_block_end,
0.0,
) * accumulated_energy_capacity[row.year, row.asset],
base_name = "min_storage_level_inter_rp_limit[$(row.asset),$(row.year),$(row.periods_block)]"
) for row in eachrow(dataframes[:storage_level_inter_rp])
base_name = "min_storage_level_inter_rp_limit[$(row.asset),$(row.year),$(row.period_block_start):$(row.period_block_end)]"
) for row in eachrow(storage_level_inter_rp.indices)
]

# - Cycling condition
for ((a, y), sub_df) in pairs(df_storage_inter_rp_balance_grouped)
# Ordering is assumed
if !ismissing(graph[a].initial_storage_level[y])
JuMP.set_lower_bound(
storage_level_inter_rp[last(sub_df.index)],
storage_level_inter_rp.container[last(sub_df.index)],
graph[a].initial_storage_level[y],
)
end
Expand Down
8 changes: 4 additions & 4 deletions src/constraints/transport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function add_transport_constraints!(
row.year,
row.year,
("availability", row.rep_period),
row.timesteps_block,
row.time_block_start:row.time_block_end,
1.0,
) *
graph[row.from, row.to].capacity *
Expand All @@ -54,7 +54,7 @@ function add_transport_constraints!(
row.year,
row.year,
("availability", row.rep_period),
row.timesteps_block,
row.time_block_start:row.time_block_end,
1.0,
) *
graph[row.from, row.to].capacity *
Expand All @@ -69,7 +69,7 @@ function add_transport_constraints!(
@constraint(
model,
flow[row.index] upper_bound_transport_flow[idx],
base_name = "max_transport_flow_limit[($(row.from),$(row.to)),$(row.year),$(row.rep_period),$(row.timesteps_block)]"
base_name = "max_transport_flow_limit[($(row.from),$(row.to)),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for (idx, row) in enumerate(eachrow(transport_flows_indices))
]

Expand All @@ -78,7 +78,7 @@ function add_transport_constraints!(
@constraint(
model,
flow[row.index] -lower_bound_transport_flow[idx],
base_name = "min_transport_flow_limit[($(row.from),$(row.to)),$(row.year),$(row.rep_period),$(row.timesteps_block)]"
base_name = "min_transport_flow_limit[($(row.from),$(row.to)),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for (idx, row) in enumerate(eachrow(transport_flows_indices))
]
end
21 changes: 2 additions & 19 deletions src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ function create_model(

# Unpacking dataframes
@timeit to "unpacking dataframes" begin
df_flows = dataframes[:flows]
df_units_on = dataframes[:units_on]
df_units_on_and_outflows = dataframes[:units_on_and_outflows]
df_storage_intra_rp_balance_grouped =
DataFrames.groupby(dataframes[:storage_level_intra_rp], [:asset, :rep_period, :year])
df_storage_inter_rp_balance_grouped =
DataFrames.groupby(dataframes[:storage_level_inter_rp], [:asset, :year])
end

## Model
Expand All @@ -84,14 +78,6 @@ function create_model(
)
@timeit to "add_storage_variables!" add_storage_variables!(model, graph, sets, variables)

# TODO: This should disapear after the changes on add_expressions_to_dataframe! and storing the solution
model[:flow] = df_flows.flow = variables[:flow].container
model[:units_on] = df_units_on.units_on = variables[:units_on].container
storage_level_intra_rp =
model[:storage_level_intra_rp] = variables[:storage_level_intra_rp].container
storage_level_inter_rp =
model[:storage_level_inter_rp] = variables[:storage_level_inter_rp].container

## Add expressions to dataframes
# TODO: What will improve this? Variables (#884)?, Constraints?
(
Expand Down Expand Up @@ -171,15 +157,12 @@ function create_model(

@timeit to "add_storage_constraints!" add_storage_constraints!(
model,
variables,
graph,
dataframes,
accumulated_energy_capacity,
incoming_flow_lowest_storage_resolution_intra_rp,
outgoing_flow_lowest_storage_resolution_intra_rp,
df_storage_intra_rp_balance_grouped,
df_storage_inter_rp_balance_grouped,
storage_level_intra_rp,
storage_level_inter_rp,
incoming_flow_storage_inter_rp_balance,
outgoing_flow_storage_inter_rp_balance,
)
Expand Down Expand Up @@ -224,9 +207,9 @@ function create_model(
if !isempty(dataframes[:units_on_and_outflows])
@timeit to "add_ramping_constraints!" add_ramping_constraints!(
model,
variables,
graph,
df_units_on_and_outflows,
df_units_on,
dataframes[:highest_out],
outgoing_flow_highest_out_resolution,
accumulated_units,
Expand Down
Loading

0 comments on commit 127db68

Please sign in to comment.