Security-Constrained LOPF with SciGRID

This Jupyter Notebook is also available to download at: http://www.pypsa.org/examples/scigrid-sclopf.ipynb and can be viewed as an HTML page at: http://pypsa.org/examples/scigrid-sclopf.html.

In this example, the dispatch of generators is optimised using the security-constrained linear OPF, to guaranteed that no branches are overloaded by certain branch outages.

The data files for this example are in the examples folder of the github repository: https://github.com/PyPSA/PyPSA.

Data sources and health warnings

See the separate notebook at http://www.pypsa.org/examples/add_load_gen_trafos_to_scigrid.ipynb.

In [1]:
from __future__ import print_function, division, absolute_import

import pypsa, os
In [2]:
csv_folder_name = os.path.dirname(pypsa.__file__) + "/../examples/scigrid-de/scigrid-with-load-gen-trafos/"

network = pypsa.Network(csv_folder_name=csv_folder_name)
INFO:pypsa.io:buses.csv found.
INFO:pypsa.io:No carriers.csv found.
INFO:pypsa.io:generators.csv found.
INFO:pypsa.io:lines.csv found.
INFO:pypsa.io:No line_types.csv found.
INFO:pypsa.io:No links.csv found.
INFO:pypsa.io:loads.csv found.
INFO:pypsa.io:No shunt_impedances.csv found.
INFO:pypsa.io:storage_units.csv found.
INFO:pypsa.io:No stores.csv found.
INFO:pypsa.io:transformers.csv found.
INFO:pypsa.io:No transformer_types.csv found.
In [3]:
#There are some infeasibilities without line extensions                                                                                  
for line_name in ["316","527","602"]:
    network.lines.loc[line_name,"s_nom"] = 1200


now = network.snapshots[0]
In [4]:
branch_outages = network.lines.index[:15]

print("Performing security-constrained linear OPF:")

network.sclopf(now,branch_outages=branch_outages)
print("Objective:",network.objective)
INFO:pypsa.pf:Slack bus for sub-network 0 is 1
Performing security-constrained linear OPF:
/home/tom/.virtualenvs/tom3/lib/python3.5/site-packages/scipy/sparse/linalg/dsolve/linsolve.py:253: SparseEfficiencyWarning: splu requires CSC matrix format
  warn('splu requires CSC matrix format', SparseEfficiencyWarning)
/home/tom/fias/lib/pypsa/pypsa/contingency.py:77: RuntimeWarning: divide by zero encountered in true_divide
  denominator = csr_matrix((1/(1-np.diag(branch_PTDF)),(r_[:num_branches],r_[:num_branches])))
INFO:pypsa.opf:Building pyomo model using `angles` formulation
WARNING:pypsa.contingency:No type given for 1, assuming it is a line
WARNING:pypsa.contingency:No type given for 2, assuming it is a line
WARNING:pypsa.contingency:No type given for 3, assuming it is a line
WARNING:pypsa.contingency:No type given for 4, assuming it is a line
WARNING:pypsa.contingency:No type given for 5, assuming it is a line
WARNING:pypsa.contingency:No type given for 6, assuming it is a line
WARNING:pypsa.contingency:No type given for 7, assuming it is a line
WARNING:pypsa.contingency:No type given for 8, assuming it is a line
WARNING:pypsa.contingency:No type given for 9, assuming it is a line
WARNING:pypsa.contingency:No type given for 10, assuming it is a line
WARNING:pypsa.contingency:No type given for 11, assuming it is a line
WARNING:pypsa.contingency:No type given for 12, assuming it is a line
WARNING:pypsa.contingency:No type given for 13, assuming it is a line
WARNING:pypsa.contingency:No type given for 14, assuming it is a line
WARNING:pypsa.contingency:No type given for 15, assuming it is a line
INFO:pypsa.opf:Solving model using glpk
INFO:pypsa.opf:Optimization successful
Objective: 347887.092552127
In [5]:
#For the PF, set the P to the optimised P
network.generators_t.p_set = network.generators_t.p_set.reindex(columns=network.generators.index)
network.generators_t.p_set.loc[now] = network.generators_t.p.loc[now]
network.storage_units_t.p_set = network.storage_units_t.p_set.reindex(columns=network.storage_units.index)
network.storage_units_t.p_set.loc[now] = network.storage_units_t.p.loc[now]

#Check no lines are overloaded with the linear contingency analysis

p0_test = network.lpf_contingency(now,branch_outages=branch_outages)
INFO:pypsa.pf:Slack bus for sub-network 0 is 1
INFO:pypsa.pf:Performing linear load-flow on AC sub-network SubNetwork 0 for snapshot(s) DatetimeIndex(['2011-01-01'], dtype='datetime64[ns]', freq=None)
INFO:pypsa.pf:Slack bus for sub-network 0 is 1
/home/tom/.virtualenvs/tom3/lib/python3.5/site-packages/scipy/sparse/linalg/dsolve/linsolve.py:253: SparseEfficiencyWarning: splu requires CSC matrix format
  warn('splu requires CSC matrix format', SparseEfficiencyWarning)
/home/tom/fias/lib/pypsa/pypsa/contingency.py:77: RuntimeWarning: divide by zero encountered in true_divide
  denominator = csr_matrix((1/(1-np.diag(branch_PTDF)),(r_[:num_branches],r_[:num_branches])))
WARNING:pypsa.contingency:No type given for 1, assuming it is a line
WARNING:pypsa.contingency:No type given for 2, assuming it is a line
WARNING:pypsa.contingency:No type given for 3, assuming it is a line
WARNING:pypsa.contingency:No type given for 4, assuming it is a line
WARNING:pypsa.contingency:No type given for 5, assuming it is a line
WARNING:pypsa.contingency:No type given for 6, assuming it is a line
WARNING:pypsa.contingency:No type given for 7, assuming it is a line
WARNING:pypsa.contingency:No type given for 8, assuming it is a line
WARNING:pypsa.contingency:No type given for 9, assuming it is a line
WARNING:pypsa.contingency:No type given for 10, assuming it is a line
WARNING:pypsa.contingency:No type given for 11, assuming it is a line
WARNING:pypsa.contingency:No type given for 12, assuming it is a line
WARNING:pypsa.contingency:No type given for 13, assuming it is a line
WARNING:pypsa.contingency:No type given for 14, assuming it is a line
WARNING:pypsa.contingency:No type given for 15, assuming it is a line
In [8]:
p0_test
Out[8]:
base (Line, 1) (Line, 2) (Line, 3) (Line, 4) (Line, 5) (Line, 6) (Line, 7) (Line, 8) (Line, 9) (Line, 10) (Line, 11) (Line, 12) (Line, 13) (Line, 14) (Line, 15)
Line 1 -68.803462 0.000000 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462 -68.803462
2 190.707297 190.707297 0.000000 212.790071 13.505587 -122.706583 190.839658 191.016630 190.975588 190.815168 191.158964 154.292840 190.026641 190.064387 191.158210 190.758718
3 325.824500 325.824500 334.145556 0.000000 383.397388 250.482736 325.812944 325.797492 325.801075 325.853072 325.946010 313.484611 327.596357 327.498098 325.945807 325.839954
4 -750.815240 -750.815240 -724.824608 -773.225254 0.000000 -487.570141 -750.782643 -750.739059 -750.749167 -750.921335 -751.264946 -708.345998 -756.662627 -756.338355 -751.264195 -750.871171
5 1069.888707 1069.888707 1045.960042 1054.623201 932.859992 0.000000 1067.815642 1065.043871 1065.686683 1069.894062 1069.913764 1156.214767 1134.263578 1130.693620 1069.913723 1069.893834
6 40.108358 40.108358 40.238820 40.078128 39.889301 13.345204 0.000000 -53.626475 72.309965 40.117117 40.145593 45.585444 131.045866 126.002857 40.145531 40.113080
7 93.734833 93.734833 93.865295 93.704603 93.515776 66.971679 53.626475 0.000000 125.936440 93.743592 93.772068 99.211920 184.672341 179.629332 93.772006 93.739555
8 78.951394 78.951394 79.091364 78.918962 78.716373 50.237845 95.995785 118.784817 0.000000 78.960792 78.991343 84.827630 176.516078 171.105554 78.991276 78.956461
9 -43.404137 -43.404137 -43.584662 -43.531033 -44.614668 -43.521533 -43.419009 -43.438894 -43.434282 0.000000 -61.541207 -44.619607 -42.700177 -42.739216 -61.510929 -27.674042
10 -104.734302 -104.734302 -105.200075 -105.066842 -107.896112 -105.072756 -104.773259 -104.825347 -104.813267 -115.910399 0.000000 -107.904326 -102.867492 -102.971017 -161.621129 -120.022636
11 699.760288 699.760288 691.081852 691.955627 630.753279 969.229603 701.084607 702.855274 702.444632 699.933380 700.492899 0.000000 666.822391 668.648987 700.491676 699.850480
12 -413.216628 -413.216628 -413.172695 -413.520136 -415.789860 -467.639588 -419.171665 -427.133786 -425.287262 -413.189478 -413.099783 -404.296025 0.000000 -22.915243 -413.099978 -413.200594
13 390.301385 390.301385 390.257452 390.604893 392.874617 444.724345 396.256422 404.218543 402.372019 390.274235 390.184540 381.380782 -22.915243 0.000000 390.184735 390.285351
14 -104.621002 -104.621002 -105.086270 -104.953182 -107.779392 -104.959090 -104.659917 -104.711948 -104.699881 -115.785008 -161.541310 -107.787596 -102.756211 -102.859625 0.000000 -119.892797
15 -42.657611 -42.657611 -42.780040 -42.755260 -43.565532 -42.817511 -42.669018 -42.684271 -42.680734 -20.278451 -77.955568 -43.558654 -42.066132 -42.098933 -77.896643 0.000000
16 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895 15.515895
17 516.365491 516.365491 514.067088 515.049565 504.483799 460.628579 516.462557 516.592339 516.562241 516.354504 516.319128 515.956964 512.949256 513.138706 516.319206 516.359901
18 -657.876222 -657.876222 -656.727059 -657.218281 -651.935572 -630.008682 -657.924754 -657.989643 -657.974594 -657.870729 -657.853042 -657.671965 -656.168161 -656.262883 -657.853081 -657.873428
19 -495.139547 -495.139547 -492.839768 -493.822833 -483.250738 -439.369245 -495.236672 -495.366531 -495.336415 -495.128554 -495.093157 -494.730775 -491.721265 -491.910829 -495.093234 -495.133954
20 -825.341074 -825.341074 -825.270194 -825.300493 -824.974656 -823.622208 -825.344068 -825.348070 -825.347142 -825.340736 -825.339645 -825.328476 -825.235721 -825.241564 -825.339647 -825.340902
21 -5.413558 -5.413558 -5.611592 -5.526940 -6.437301 -10.215927 -5.405195 -5.394013 -5.396606 -5.414505 -5.417553 -5.448757 -5.707906 -5.691582 -5.417546 -5.414040
22 484.893158 484.893158 482.798547 483.693911 474.064973 434.098239 484.981618 485.099892 485.072462 484.883146 484.850906 484.520853 481.779828 481.952480 484.850977 484.888064
23 143.505013 143.505013 144.638345 144.153890 149.363822 170.988632 143.457150 143.393156 143.407997 143.510431 143.527875 143.706456 145.189543 145.096127 143.527836 143.507770
24 -195.778283 -195.778283 -196.878672 -196.408299 -201.466791 -222.463025 -195.731811 -195.669677 -195.684086 -195.783543 -195.800479 -195.973870 -197.413848 -197.323146 -195.800442 -195.780959
25 -3.701396 -3.701396 9.443543 3.824598 64.251934 315.067075 -4.256533 -4.998774 -4.826639 -3.638562 -3.436240 -1.364962 15.836610 14.753115 -3.436683 -3.669426
26 533.761708 533.761708 521.799248 526.130150 465.258070 -1.098952 532.725338 531.339669 531.661025 533.764385 533.774235 576.917985 565.944108 564.159408 533.774214 533.764271
27 -714.221071 -714.221071 -716.584596 -715.574282 -726.439417 -771.537224 -714.121254 -713.987795 -714.018746 -714.232368 -714.268747 -714.641173 -717.734101 -717.539283 -714.268667 -714.226819
28 -764.538194 -764.538194 -767.365408 -768.333182 -797.032450 -774.979985 -764.899370 -765.382277 -765.270284 -764.863091 -765.884965 -796.356136 -748.703080 -749.581229 -765.882717 -764.679763
29 799.188040 799.188040 801.990887 802.950319 831.402233 809.539835 799.546103 800.024848 799.913820 799.510137 800.523204 830.731749 783.489407 784.359987 800.520975 799.328389
30 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648 -2.606648
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Transformer 272 -108.635619 -108.635619 -108.641510 -108.638992 -108.666072 -108.778474 -108.635370 -108.635038 -108.635115 -108.635647 -108.635738 -108.636666 -108.644375 -108.643890 -108.635738 -108.635634
279 359.032227 359.032227 359.300832 359.373375 361.978787 360.609931 359.078638 359.140690 359.126300 359.061727 359.238016 361.777001 355.333684 355.538789 359.237673 359.126738
281 -211.714341 -211.714341 -212.158825 -212.174986 -215.807980 -213.191300 -211.771064 -211.846904 -211.829316 -211.679092 -211.610971 -215.654708 -207.988341 -208.194970 -211.611144 -211.740780
282 411.771394 411.771394 412.130186 412.120769 414.906707 412.724071 411.812952 411.868516 411.855630 411.718022 411.526927 414.820836 409.262829 409.401943 411.527335 411.725425
292 -167.782032 -167.782032 -168.340378 -168.536757 -174.377985 -190.110549 -167.410780 -166.914400 -167.029517 -167.746577 -167.627010 -169.894815 -186.149619 -185.131031 -167.627269 -167.758708
303 104.904805 104.904805 104.481592 129.036534 70.467000 141.883989 104.960617 105.035240 105.017934 104.883965 104.814872 115.105247 101.795707 101.968125 104.815022 104.892254
307 184.694881 184.694881 184.589642 184.632413 184.075396 184.686408 184.687131 184.676768 184.679172 184.760699 184.960593 184.066603 185.057424 185.037319 184.960150 184.716598
308 -229.073973 -229.073973 -229.271201 -229.255488 -230.717708 -229.424537 -229.093626 -229.119903 -229.113809 -229.280034 -229.856218 -230.701311 -228.130389 -228.182716 -229.854912 -229.093431
312 186.710719 186.710719 186.815958 186.773188 187.330204 186.719192 186.718470 186.728832 186.726429 186.644901 186.445007 187.338997 186.348176 186.368281 186.445450 186.689002
315 -670.387574 -670.387574 -669.964786 -670.086901 -667.526396 -670.088704 -670.352350 -670.305254 -670.316176 -671.330244 -674.174975 -667.517860 -672.069882 -671.976588 -674.168652 -670.680812
317 -48.418534 -48.418534 -48.396701 -48.402988 -48.270635 -48.402996 -48.416714 -48.414280 -48.414844 -48.467563 -48.615517 -48.270205 -48.505503 -48.500680 -48.615188 -48.433783
322 135.683316 135.683316 135.668227 135.678880 135.628364 135.705351 135.682560 135.681549 135.681783 135.681695 135.676967 135.624846 135.717527 135.715629 135.676978 135.682971
332 -501.130596 -501.130596 -501.191529 -501.163359 -501.463673 -501.104173 -501.134541 -501.139816 -501.138593 -501.156578 -499.801002 -501.473431 -500.979821 -500.988183 -499.803221 -499.736459
334 205.596186 205.596186 205.547028 205.530746 205.034575 205.315401 205.587921 205.576871 205.579434 205.538028 205.070868 205.067976 206.207330 206.173439 205.071745 205.292901
336 132.776055 132.776055 132.861983 132.863509 133.555335 133.034113 132.786512 132.800495 132.797252 132.776769 132.858474 133.530637 132.125777 132.161839 132.858336 132.854064
338 374.302182 374.302182 374.533111 374.549515 376.488386 375.023204 374.330543 374.368461 374.359667 374.286268 375.483868 376.423551 372.624733 372.717757 375.481895 375.515262
351 -127.326666 -127.326666 -127.346130 -127.342827 -127.475922 -127.375693 -127.328934 -127.331965 -127.331262 -127.004426 -127.600323 -127.470144 -127.162794 -127.171881 -127.599866 -128.760016
353 304.935547 304.935547 305.174098 305.091643 306.448225 305.010394 304.953878 304.978387 304.972703 302.100665 305.904806 306.464981 304.119375 304.164637 305.903188 316.138972
360 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003 -626.527003
362 -30.252758 -30.252758 -30.240344 -30.251953 -30.228850 -30.284981 -30.252349 -30.251802 -30.251928 -30.276653 -30.348580 -30.224650 -30.271486 -30.270447 -30.348420 -30.260010
382 -57.382583 -57.382583 -57.631714 -57.611866 -59.458887 -57.825401 -57.407408 -57.440599 -57.432902 -57.642871 -58.370685 -59.438175 -56.190683 -56.256781 -58.369035 -57.407161
384 276.476573 276.476573 277.276704 277.245306 283.386566 277.955040 276.556657 276.663732 276.638900 277.071646 277.789366 283.327536 272.886023 273.085139 277.787175 275.607512
385 357.224231 357.224231 357.004003 357.013825 355.330965 356.794451 357.201707 357.171592 357.178576 357.014667 356.428463 355.352834 358.303638 358.243779 356.429791 357.204223
391 878.678032 878.678032 878.357186 878.310082 875.459804 877.747036 878.640610 878.590575 878.602179 878.535972 878.136631 875.519969 880.455628 880.357050 878.137534 878.662548
403 44.076446 44.076446 44.083479 44.073636 44.065525 44.041659 44.076427 44.076400 44.076406 44.054665 43.989207 44.069478 44.077785 44.077711 43.989352 44.069938
404 3.995926 3.995926 3.998271 3.994990 3.992286 3.984331 3.995920 3.995911 3.995913 3.988666 3.966848 3.993604 3.996373 3.996348 3.966897 3.993757
413 94.603092 94.603092 94.650025 94.624896 94.833998 94.575138 94.606061 94.610030 94.609110 94.624523 94.688311 94.841071 94.467175 94.474713 94.688169 94.608894
421 52.977690 52.977690 53.170624 53.069459 53.942929 52.873378 52.990054 53.006586 53.002752 53.084125 53.401446 53.971018 52.411840 52.443219 53.400739 53.007013
450 82.518670 82.518670 82.460261 82.496273 82.250384 82.130705 82.517163 82.515147 82.515615 82.523638 82.539026 82.337607 82.595020 82.590786 82.538992 82.520601
458 83.475451 83.475451 83.415986 83.452656 83.202370 83.080708 83.473917 83.471864 83.472340 83.480509 83.496173 83.291123 83.553192 83.548881 83.496138 83.477418

948 rows × 16 columns

In [9]:
#check loading as per unit of s_nom in each contingency

max_loading = abs(p0_test.divide(network.passive_branches().s_nom,axis=0)).describe().loc["max"]

print(max_loading)
base          1.0
(Line, 1)     1.0
(Line, 2)     1.0
(Line, 3)     1.0
(Line, 4)     1.0
(Line, 5)     1.0
(Line, 6)     1.0
(Line, 7)     1.0
(Line, 8)     1.0
(Line, 9)     1.0
(Line, 10)    1.0
(Line, 11)    1.0
(Line, 12)    1.0
(Line, 13)    1.0
(Line, 14)    1.0
(Line, 15)    1.0
Name: max, dtype: float64
In [10]:
import numpy as np
np.testing.assert_array_almost_equal(max_loading,np.ones((len(max_loading))))