forked from PyPSA/PyPSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontingency.py
464 lines (368 loc) · 14.2 KB
/
contingency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# -*- coding: utf-8 -*-
"""
Functionality for contingency analysis, such as branch outages.
"""
__author__ = (
"PyPSA Developers, see https://pypsa.readthedocs.io/en/latest/developers.html"
)
__copyright__ = (
"Copyright 2015-2023 PyPSA Developers, see https://pypsa.readthedocs.io/en/latest/developers.html, "
"MIT License"
)
import logging
from numpy import r_
from scipy.sparse import csr_matrix
logger = logging.getLogger(__name__)
from collections.abc import Iterable
import numpy as np
import pandas as pd
from pypsa.descriptors import get_extendable_i
from pypsa.linopt import get_var, linexpr, set_conref, write_constraint
from pypsa.opt import l_constraint
from pypsa.pf import _as_snapshots, calculate_PTDF
def calculate_BODF(sub_network, skip_pre=False):
"""
Calculate the Branch Outage Distribution Factor (BODF) for sub_network.
Sets sub_network.BODF as a (dense) numpy array.
The BODF is a num_branch x num_branch 2d array.
For the outage of branch l, the new flow on branch k is
given in terms of the flow before the outage
f_k^after = f_k^before + BODF_{kl} f_l^before
Note that BODF_{ll} = -1.
Parameters
----------
sub_network : pypsa.SubNetwork
skip_pre : bool, default False
Skip the preliminary step of computing the PTDF.
Examples
--------
>>> sub_network.caculate_BODF()
"""
if not skip_pre:
calculate_PTDF(sub_network)
num_branches = sub_network.PTDF.shape[0]
# build LxL version of PTDF
branch_PTDF = sub_network.PTDF * sub_network.K
with np.errstate(divide="ignore"):
denominator = csr_matrix(
(1 / (1 - np.diag(branch_PTDF)), (r_[:num_branches], r_[:num_branches]))
)
sub_network.BODF = branch_PTDF * denominator
# make sure the flow on the branch itself is zero
np.fill_diagonal(sub_network.BODF, -1)
def network_lpf_contingency(network, snapshots=None, branch_outages=None):
"""
Computes linear power flow for a selection of branch outages.
Parameters
----------
snapshots : list-like|single snapshot
A subset or an elements of network.snapshots on which to run
the power flow, defaults to network.snapshots
NB: currently this only works for a single snapshot
branch_outages : list-like
A list of passive branches which are to be tested for outages.
If None, it's take as all network.passive_branches_i()
Returns
-------
p0 : pandas.DataFrame
num_passive_branch x num_branch_outages DataFrame of new power flows
Examples
--------
>>> network.lpf_contingency(snapshot, branch_outages)
"""
if snapshots is None:
snapshots = network.snapshots
if isinstance(snapshots, Iterable):
logger.warning(
"Apologies LPF contingency, this only works for single snapshots at the moment, taking the first snapshot."
)
snapshot = snapshots[0]
else:
snapshot = snapshots
network.lpf(snapshot)
# Store the flows from the base case
passive_branches = network.passive_branches()
if branch_outages is None:
branch_outages = passive_branches.index
p0_base = pd.concat(
{c: network.pnl(c).p0.loc[snapshot] for c in network.passive_branch_components}
)
p0 = p0_base.to_frame("base")
for sn in network.sub_networks.obj:
sn._branches = sn.branches()
sn.calculate_BODF()
for branch in branch_outages:
if not isinstance(branch, tuple):
logger.warning("No type given for {}, assuming it is a line".format(branch))
branch = ("Line", branch)
sn = network.sub_networks.obj[passive_branches.sub_network[branch]]
branch_i = sn._branches.index.get_loc(branch)
p0_new = p0_base + pd.Series(
sn.BODF[:, branch_i] * p0_base[branch], sn._branches.index
)
p0[branch] = p0_new
return p0
def add_contingency_constraints(network, snapshots):
passive_branches = network.passive_branches()
branch_outages = network._branch_outages
# prepare the sub networks by calculating BODF and preparing helper DataFrames
for sn in network.sub_networks.obj:
sn.calculate_BODF()
sn._branches = sn.branches()
sn._branches["_i"] = range(sn._branches.shape[0])
sn._extendable_branches = sn._branches[sn._branches.s_nom_extendable]
sn._fixed_branches = sn._branches[~sn._branches.s_nom_extendable]
# a list of tuples with branch_outage and passive branches in same sub_network
branch_outage_keys = []
flow_upper = {}
flow_lower = {}
for branch in branch_outages:
if type(branch) is not tuple:
logger.warning("No type given for {}, assuming it is a line".format(branch))
branch = ("Line", branch)
sub = network.sub_networks.at[passive_branches.at[branch, "sub_network"], "obj"]
branch_i = sub._branches.at[branch, "_i"]
branch_outage_keys.extend(
[(branch[0], branch[1], b[0], b[1]) for b in sub._branches.index]
)
flow_upper.update(
{
(branch[0], branch[1], b[0], b[1], sn): [
[
(1, network.model.passive_branch_p[b[0], b[1], sn]),
(
sub.BODF[sub._branches.at[b, "_i"], branch_i],
network.model.passive_branch_p[branch[0], branch[1], sn],
),
],
"<=",
sub._fixed_branches.at[b, "s_nom"],
]
for b in sub._fixed_branches.index
for sn in snapshots
}
)
flow_upper.update(
{
(branch[0], branch[1], b[0], b[1], sn): [
[
(1, network.model.passive_branch_p[b[0], b[1], sn]),
(
sub.BODF[sub._branches.at[b, "_i"], branch_i],
network.model.passive_branch_p[branch[0], branch[1], sn],
),
(-1, network.model.passive_branch_s_nom[b[0], b[1]]),
],
"<=",
0,
]
for b in sub._extendable_branches.index
for sn in snapshots
}
)
flow_lower.update(
{
(branch[0], branch[1], b[0], b[1], sn): [
[
(1, network.model.passive_branch_p[b[0], b[1], sn]),
(
sub.BODF[sub._branches.at[b, "_i"], branch_i],
network.model.passive_branch_p[branch[0], branch[1], sn],
),
],
">=",
-sub._fixed_branches.at[b, "s_nom"],
]
for b in sub._fixed_branches.index
for sn in snapshots
}
)
flow_lower.update(
{
(branch[0], branch[1], b[0], b[1], sn): [
[
(1, network.model.passive_branch_p[b[0], b[1], sn]),
(
sub.BODF[sub._branches.at[b, "_i"], branch_i],
network.model.passive_branch_p[branch[0], branch[1], sn],
),
(1, network.model.passive_branch_s_nom[b[0], b[1]]),
],
">=",
0,
]
for b in sub._extendable_branches.index
for sn in snapshots
}
)
l_constraint(
network.model,
"contingency_flow_upper",
flow_upper,
branch_outage_keys,
snapshots,
)
l_constraint(
network.model,
"contingency_flow_lower",
flow_lower,
branch_outage_keys,
snapshots,
)
def add_contingency_constraints_lowmem(network, snapshots):
n = network
if not hasattr(n, "_branch_outages"):
n._branch_outages = n.passive_branches().index
branch_outages = [
b if isinstance(b, tuple) else ("Line", b) for b in n._branch_outages
]
comps = n.passive_branch_components & set(n.variables.index.levels[0])
if len(comps) == 0:
return
dispatch_vars = pd.concat({c: get_var(n, c, "s") for c in comps}, axis=1)
invest_vars = pd.concat(
{
c: get_var(n, c, "s_nom")
if not get_extendable_i(n, c).empty
else pd.Series(dtype=float)
for c in comps
}
)
constraints = {}
for sn in n.sub_networks.obj:
sn.calculate_BODF()
branches_i = sn.branches_i()
branches = sn.branches()
outages = branches_i.intersection(branch_outages)
ext_i = branches.loc[branches.s_nom_extendable].index
fix_i = branches.loc[~branches.s_nom_extendable].index
p = dispatch_vars[branches_i]
BODF = pd.DataFrame(sn.BODF, index=branches_i, columns=branches_i)
lhs = {}
rhs = {}
for outage in outages:
def contingency_flow(branch):
if branch.name == outage:
return linexpr((0, branch))
flow = linexpr((1, branch))
added_flow = linexpr((BODF.at[branch.name, outage], p[outage]))
return flow + added_flow
lhs_flow = p.apply(contingency_flow, axis=0)
if len(fix_i):
lhs_flow_fix = lhs_flow[fix_i]
s_nom_fix = branches.loc[fix_i, "s_nom"]
key = ("upper", "non_ext", outage)
lhs[key] = lhs_flow_fix
rhs[key] = s_nom_fix
key = ("lower", "non_ext", outage)
lhs[key] = lhs_flow_fix
rhs[key] = -s_nom_fix
if len(ext_i):
lhs_flow_ext = lhs_flow[ext_i]
s_nom_ext = invest_vars[ext_i]
key = ("upper", "ext", outage)
lhs[key] = lhs_flow_ext + linexpr((-1, s_nom_ext))
rhs[key] = 0
key = ("lower", "ext", outage)
lhs[key] = lhs_flow_ext + linexpr((1, s_nom_ext))
rhs[key] = 0
for k in lhs.keys():
sense = "<=" if k[0] == "upper" else ">="
axes = (lhs[k].index, lhs[k].columns)
con = write_constraint(n, lhs[k], sense, rhs[k], axes)
if k not in constraints.keys():
constraints[k] = []
constraints[k].append(con)
for (bound, spec, outage), constr in constraints.items():
constr = pd.concat(constr, axis=1)
for c in comps:
if c in constr.columns.levels[0]:
constr_name = "_".join([bound, *outage])
set_conref(n, constr[c], c, f"mu_contingency_{constr_name}", spec=spec)
def network_sclopf(
network,
snapshots=None,
branch_outages=None,
solver_name="glpk",
pyomo=True,
skip_pre=False,
extra_functionality=None,
solver_options={},
keep_files=False,
formulation="kirchhoff",
ptdf_tolerance=0.0,
):
"""
Computes Security-Constrained Linear Optimal Power Flow (SCLOPF).
This ensures that no branch is overloaded even given the branch outages.
Parameters
----------
snapshots : list or index slice
A list of snapshots to optimise, must be a subset of
network.snapshots, defaults to network.snapshots
branch_outages : list-like
A list of passive branches which are to be tested for outages.
If None, it's take as all network.passive_branches_i()
solver_name : string
Must be a solver name that pyomo recognises and that is
installed, e.g. "glpk", "gurobi"
pyomo : bool, default True
Whether to use pyomo for building and solving the model, setting
this to False saves a lot of memory and time.
skip_pre : bool, default False
Skip the preliminary steps of computing topology, calculating
dependent values and finding bus controls.
extra_functionality : callable function
This function must take two arguments
`extra_functionality(network, snapshots)` and is called after
the model building is complete, but before it is sent to the
solver. It allows the user to add/change constraints and
add/change the objective function.
solver_options : dictionary
A dictionary with additional options that get passed to the solver.
(e.g. {'threads':2} tells gurobi to use only 2 cpus)
keep_files : bool, default False
Keep the files that pyomo constructs from OPF problem
construction, e.g. .lp file - useful for debugging
formulation : string, default "kirchhoff"
Formulation of the linear power flow equations to use; must be
one of ["angles", "cycles", "kirchoff", "ptdf"]
ptdf_tolerance : float
Returns
-------
None
Examples
--------
>>> network.sclopf(network, branch_outages)
"""
if not skip_pre:
network.calculate_dependent_values()
network.determine_network_topology()
snapshots = _as_snapshots(network, snapshots)
passive_branches = network.passive_branches()
if branch_outages is None:
branch_outages = passive_branches.index
# save to network for extra_functionality
network._branch_outages = branch_outages
def _extra_functionality(network, snapshots):
if pyomo:
add_contingency_constraints(network, snapshots)
else:
add_contingency_constraints_lowmem(network, snapshots)
if extra_functionality is not None:
extra_functionality(network, snapshots)
pyomo_kwargs = {}
if pyomo:
pyomo_kwargs["ptdf_tolerance"] = ptdf_tolerance
# need to skip preparation otherwise it recalculates the sub-networks
network.lopf(
snapshots=snapshots,
solver_name=solver_name,
pyomo=pyomo,
skip_pre=True,
extra_functionality=_extra_functionality,
solver_options=solver_options,
keep_files=keep_files,
formulation=formulation,
**pyomo_kwargs,
)