Skip to content

Commit 94b7c96

Browse files
authored
Add electricity angle variables (#1185)
* Add variable electricity angle * Add dc_opf condition * Apply review suggestions * Improve sql
1 parent 71e0242 commit 94b7c96

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

src/create-model.jl

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ function create_model(
8989
@timeit to "add_flow_variables!" add_flow_variables!(connection, model, variables)
9090
@timeit to "add_investment_variables!" add_investment_variables!(model, variables)
9191
@timeit to "add_unit_commitment_variables!" add_unit_commitment_variables!(model, variables)
92+
@timeit to "add_power_flow_variables!" add_power_flow_variables!(model, variables)
9293
@timeit to "add_storage_variables!" add_storage_variables!(connection, model, variables)
9394

9495
@timeit to "add_expressions_to_constraints!" add_expressions_to_constraints!(

src/sql/create-variables.sql

+39
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,45 @@ drop sequence id
4545
create sequence id start 1
4646
;
4747

48+
create table var_electricity_angle as
49+
select
50+
nextval('id') as id,
51+
atr.asset,
52+
atr.year,
53+
atr.rep_period,
54+
atr.time_block_start,
55+
any_value(atr.time_block_end) as time_block_end,
56+
from
57+
-- The angle resolution is the same as the time resolution of the asset
58+
asset_time_resolution_rep_period as atr
59+
-- We need to check if the asset has any connecting flows that are transport
60+
-- We only get the assets that have outgoing flows OR incoming flows
61+
-- With the condition that the flows are transport
62+
left join flow on flow.from_asset = atr.asset
63+
or flow.to_asset = atr.asset
64+
-- After that we need to also check if the flows have dc_opf method
65+
-- This is by joining the flow_milestone table
66+
-- Here we use AND because we need to match flow_milestone with flow
67+
left join flow_milestone on flow_milestone.from_asset = flow.from_asset
68+
and flow_milestone.to_asset = flow.to_asset
69+
and flow_milestone.milestone_year = atr.year
70+
where
71+
flow.is_transport
72+
and flow_milestone.dc_opf
73+
-- We may end up with duplicates because an asset can have both incoming and outgoing flows
74+
-- Or it can have multiple flows
75+
-- GROUP BY is used to remove duplicates
76+
-- Note SELECT only happens after the GROUP BY, so id is unique for each row.
77+
group by
78+
atr.asset, atr.year, atr.rep_period, atr.time_block_start
79+
;
80+
81+
drop sequence id
82+
;
83+
84+
create sequence id start 1
85+
;
86+
4887
create table var_is_charging as
4988
select
5089
nextval('id') as id,

src/variables/create.jl

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function compute_variables_indices(connection)
1010
key => TulipaVariable(connection, "var_$key") for key in (
1111
:flow,
1212
:units_on,
13+
:electricity_angle,
1314
:is_charging,
1415
:storage_level_rep_period,
1516
:storage_level_over_clustered_year,

src/variables/power-flow.jl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export add_power_flow_variables!
2+
3+
"""
4+
add_power_flow_variables!(model, variables)
5+
6+
Adds power flow variables to the optimization `model` based on the `:electricity_angle` indices.
7+
8+
"""
9+
function add_power_flow_variables!(model, variables)
10+
electricity_angle_indices = variables[:electricity_angle].indices
11+
12+
variables[:electricity_angle].container = [
13+
@variable(
14+
model,
15+
base_name = "electricity_angle[$(row.asset),$(row.year),$(row.rep_period),$(row.time_block_start):$(row.time_block_end)]",
16+
) for row in electricity_angle_indices
17+
]
18+
19+
return
20+
end

0 commit comments

Comments
 (0)