Skip to content

Commit

Permalink
Create transport table and update transport.jl (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsiqueira authored Dec 18, 2024
1 parent 2273603 commit 606766e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 63 deletions.
23 changes: 23 additions & 0 deletions src/constraints/create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function compute_constraints_indices(connection)
:balance_storage_over_clustered_year,
:min_energy_over_clustered_year,
:max_energy_over_clustered_year,
:transport_flow_limit,
)
)

Expand Down Expand Up @@ -340,5 +341,27 @@ function _create_constraints_tables(connection)
",
)

DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE cons_transport_flow_limit AS
SELECT
nextval('id') AS index,
var_flow.from,
var_flow.to,
var_flow.year,
var_flow.rep_period,
var_flow.time_block_start,
var_flow.time_block_end,
var_flow.index AS var_flow_index
FROM var_flow
LEFT JOIN flow
ON flow.from_asset = var_flow.from
AND flow.to_asset = var_flow.to
WHERE
flow.is_transport
",
)

return
end
135 changes: 73 additions & 62 deletions src/constraints/transport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,88 @@ add_transport_constraints!(model, graph, df_flows, flow, Ft, flows_investment)
Adds the transport flow constraints to the model.
"""

function add_transport_constraints!(model, variables, graph, sets)
## unpack from sets
Ft = sets[:Ft]

function add_transport_constraints!(model, variables, constraints, graph)
## unpack from model
accumulated_flows_export_units = model[:accumulated_flows_export_units]
accumulated_flows_import_units = model[:accumulated_flows_import_units]

## unpack from variables
flows_indices = variables[:flow].indices
flow = variables[:flow].container
let table_name = :transport_flow_limit, cons = constraints[table_name]
var_flow = variables[:flow].container

## Expressions used by transport flow constraints
# Filter flows_indices to flows only for transport assets
transport_flows_indices =
filter([:from, :to] => (from, to) -> (from, to) Ft, flows_indices; view = true)
# - Create upper limit of transport flow
attach_expression!(
cons,
:upper_bound_transport_flow,
[
@expression(
model,
profile_aggregation(
Statistics.mean,
graph[row.from, row.to].rep_periods_profiles,
row.year,
row.year,
("availability", row.rep_period),
row.time_block_start:row.time_block_end,
1.0,
) *
graph[row.from, row.to].capacity *
accumulated_flows_export_units[row.year, (row.from, row.to)]
) for row in eachrow(cons.indices)
],
)

# - Create upper limit of transport flow
upper_bound_transport_flow = [
@expression(
model,
profile_aggregation(
Statistics.mean,
graph[row.from, row.to].rep_periods_profiles,
row.year,
row.year,
("availability", row.rep_period),
row.time_block_start:row.time_block_end,
1.0,
) *
graph[row.from, row.to].capacity *
accumulated_flows_export_units[row.year, (row.from, row.to)]
) for row in eachrow(transport_flows_indices)
]

# - Create lower limit of transport flow
lower_bound_transport_flow = [
@expression(
model,
profile_aggregation(
Statistics.mean,
graph[row.from, row.to].rep_periods_profiles,
row.year,
row.year,
("availability", row.rep_period),
row.time_block_start:row.time_block_end,
1.0,
) *
graph[row.from, row.to].capacity *
accumulated_flows_import_units[row.year, (row.from, row.to)]
) for row in eachrow(transport_flows_indices)
]
# - Create lower limit of transport flow
attach_expression!(
cons,
:lower_bound_transport_flow,
[
@expression(
model,
profile_aggregation(
Statistics.mean,
graph[row.from, row.to].rep_periods_profiles,
row.year,
row.year,
("availability", row.rep_period),
row.time_block_start:row.time_block_end,
1.0,
) *
graph[row.from, row.to].capacity *
accumulated_flows_import_units[row.year, (row.from, row.to)]
) for row in eachrow(cons.indices)
],
)

## Constraints that define bounds for a transport flow Ft
## Constraints that define bounds for an investable transport flow

# - Max transport flow limit
model[:max_transport_flow_limit] = [
@constraint(
# - Max transport flow limit
attach_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.time_block_start):$(row.time_block_end)]"
) for (idx, row) in enumerate(eachrow(transport_flows_indices))
]
cons,
:max_transport_flow_limit,
[
@constraint(
model,
var_flow[row.var_flow_index] upper_bound_transport_flow,
base_name = "max_transport_flow_limit[($(row.from),$(row.to)),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for (row, upper_bound_transport_flow) in
zip(eachrow(cons.indices), cons.expressions[:upper_bound_transport_flow])
],
)

# - Min transport flow limit
model[:min_transport_flow_limit] = [
@constraint(
# - Min transport flow limit
attach_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.time_block_start):$(row.time_block_end)]"
) for (idx, row) in enumerate(eachrow(transport_flows_indices))
]
cons,
:min_transport_flow_limit,
[
@constraint(
model,
var_flow[row.var_flow_index] -lower_bound_transport_flow,
base_name = "min_transport_flow_limit[($(row.from),$(row.to)),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]"
) for (row, lower_bound_transport_flow) in
zip(eachrow(cons.indices), cons.expressions[:lower_bound_transport_flow])
],
)
end
end
2 changes: 1 addition & 1 deletion src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ function create_model(
@timeit to "add_transport_constraints!" add_transport_constraints!(
model,
variables,
constraints,
graph,
sets,
)

if !isempty(groups)
Expand Down

0 comments on commit 606766e

Please sign in to comment.