Skip to content

Commit 578af5e

Browse files
committed
adjust to new dimension naming
1 parent cdf4c8f commit 578af5e

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

scripts/pypsa-de/additional_functionality.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def add_power_limits(n, investment_year, limits_power_max):
103103
" Restricts the maximum inflow/outflow of electricity from/to a country.
104104
"""
105105

106-
def add_pos_neg_aux_variables(n, idx, infix):
106+
def add_pos_neg_aux_variables(n, idx, var_name, infix):
107107
"""
108108
For every snapshot in the network `n` this functions adds auxiliary variables corresponding to the positive and negative parts of the dynamical variables of the network components specified in the index `idx`. The `infix` parameter is used to create unique names for the auxiliary variables and constraints.
109109
@@ -116,26 +116,24 @@ def add_pos_neg_aux_variables(n, idx, infix):
116116
infix : str
117117
A string used to create unique names for the auxiliary variables and constraints.
118118
"""
119-
120-
var_key = f"{idx.name}-{'s' if idx.name == 'Line' else 'p'}"
121-
var = n.model[var_key].sel({idx.name: idx})
119+
var = n.model[var_name].sel({"name": idx})
122120
aux_pos = n.model.add_variables(
123-
name=f"{var_key}-{infix}-aux-pos",
121+
name=f"{var_name}-{infix}-aux-pos",
124122
lower=0,
125123
coords=[n.snapshots, idx],
126124
)
127125
aux_neg = n.model.add_variables(
128-
name=f"{var_key}-{infix}-aux-neg",
126+
name=f"{var_name}-{infix}-aux-neg",
129127
upper=0,
130128
coords=[n.snapshots, idx],
131129
)
132130
n.model.add_constraints(
133131
aux_pos >= var,
134-
name=f"{var_key}-{infix}-aux-pos-constr",
132+
name=f"{var_name}-{infix}-aux-pos-constr",
135133
)
136134
n.model.add_constraints(
137135
aux_neg <= var,
138-
name=f"{var_key}-{infix}-aux-neg-constr",
136+
name=f"{var_name}-{infix}-aux-neg-constr",
139137
)
140138
return aux_pos, aux_neg
141139

@@ -166,37 +164,36 @@ def add_pos_neg_aux_variables(n, idx, infix):
166164
# define auxiliary variables for positive and negative parts of line and link flows
167165

168166
incoming_lines_aux_pos, incoming_lines_aux_neg = add_pos_neg_aux_variables(
169-
n, incoming_lines.index, f"incoming-{ct}"
167+
n, incoming_lines.index, "Line-s", f"incoming-{ct}"
170168
)
171169

172170
outgoing_lines_aux_pos, outgoing_lines_aux_neg = add_pos_neg_aux_variables(
173-
n, outgoing_lines.index, f"outgoing-{ct}"
171+
n, outgoing_lines.index, "Line-s", f"outgoing-{ct}"
174172
)
175173

176174
incoming_links_aux_pos, incoming_links_aux_neg = add_pos_neg_aux_variables(
177-
n, incoming_links.index, f"incoming-{ct}"
175+
n, incoming_links.index, "Link-p", f"incoming-{ct}"
178176
)
179177

180178
outgoing_links_aux_pos, outgoing_links_aux_neg = add_pos_neg_aux_variables(
181-
n, outgoing_links.index, f"outgoing-{ct}"
179+
n, outgoing_links.index, "Link-p", f"outgoing-{ct}"
182180
)
183-
184181
# To constraint the absolute values of imports and exports, we have to sum the
185182
# corresponding positive and negative flows separately, using the auxiliary variables
186183

187184
import_lhs = (
188-
incoming_links_aux_pos.sum(dim="Link")
189-
+ incoming_lines_aux_pos.sum(dim="Line")
190-
- outgoing_links_aux_neg.sum(dim="Link")
191-
- outgoing_lines_aux_neg.sum(dim="Line")
192-
) / 10
185+
incoming_links_aux_pos
186+
+ incoming_lines_aux_pos
187+
- outgoing_links_aux_neg
188+
- outgoing_lines_aux_neg
189+
).sum(dim="name") / 10
193190

194191
export_lhs = (
195-
outgoing_links_aux_pos.sum(dim="Link")
196-
+ outgoing_lines_aux_pos.sum(dim="Line")
197-
- incoming_links_aux_neg.sum(dim="Link")
198-
- incoming_lines_aux_neg.sum(dim="Line")
199-
) / 10
192+
outgoing_links_aux_pos
193+
+ outgoing_lines_aux_pos
194+
- incoming_links_aux_neg
195+
- incoming_lines_aux_neg
196+
).sum(dim="name") / 10
200197

201198
n.model.add_constraints(import_lhs <= lim / 10, name=f"Power-import-limit-{ct}")
202199
n.model.add_constraints(export_lhs <= lim / 10, name=f"Power-export-limit-{ct}")

0 commit comments

Comments
 (0)