From 1b6e887805b11b6a7c7907387a3fcc1deb1adf16 Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Thu, 27 Jul 2023 17:51:58 +0200 Subject: [PATCH] Rename `Bus.short_circuit` to `add_short_circuit` (#103) This is done to differentiate between the method that adds a short circuit and the property `short_circuits` that returns a list of short circuits Also made `Bus.short_circuits` return a copy so that users do not modify it directly. --- doc/models/Bus.md | 2 +- doc/notebooks/Short_Circuit.ipynb | 569 +----------------- roseau/load_flow/io/dict.py | 2 +- roseau/load_flow/models/buses.py | 10 +- roseau/load_flow/models/tests/test_buses.py | 14 +- roseau/load_flow/models/tests/test_loads.py | 2 +- .../tests/test_electrical_network.py | 2 +- 7 files changed, 45 insertions(+), 556 deletions(-) diff --git a/doc/models/Bus.md b/doc/models/Bus.md index 2982ae82..9a41f73f 100644 --- a/doc/models/Bus.md +++ b/doc/models/Bus.md @@ -84,7 +84,7 @@ vs = VoltageSource(id="source", bus=bus1, voltages=voltages) pref = PotentialRef(id="pref", element=bus1, phase="n") # Create a short circuit on bus2 between phases "a" and "b" -bus2.short_circuit("a", "b") +bus2.add_short_circuit("a", "b") # Create a network and solve a load flow en = ElectricalNetwork.from_element(bus1) diff --git a/doc/notebooks/Short_Circuit.ipynb b/doc/notebooks/Short_Circuit.ipynb index f0ff132a..4a1959d1 100644 --- a/doc/notebooks/Short_Circuit.ipynb +++ b/doc/notebooks/Short_Circuit.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2023-06-09T12:44:12.206802774Z", @@ -49,7 +49,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2023-06-09T13:15:01.378991576Z", @@ -97,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2023-06-09T09:38:08.084249736Z", @@ -107,7 +107,7 @@ }, "outputs": [], "source": [ - "bus2.short_circuit(\"a\", \"b\")" + "bus2.add_short_circuit(\"a\", \"b\")" ] }, { @@ -121,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2023-06-09T09:38:19.164884465Z", @@ -137,7 +137,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2023-06-09T09:38:25.046565516Z", @@ -145,166 +145,7 @@ }, "collapsed": false }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
current1current2power1power2potential1potential2
branch_idphase
line1a376.726141+75.273614j-3.765079e+02-7.516544e+01j87001.175581-17383.696547j-6.962717e+04+2.413923e+04j230.9401008-0.0000000j190.149827-26.152266j
b-376.137057-74.958399j3.761182e+02+7.496233e+01j58424.246079+66571.961003j-4.114025e+04-5.981005e+04j-115.470054-200.000000j-74.719733-173.911328j
c-0.4933660-0.4185120j4.929085e-01+2.055072e-01j-26.733437-00146.99870000j-1.490257e+01+1.267102e+02j-115.470054+200.000000j-117.063139+208.259546j
n-0.0957190+0.1032970j-1.031467e-01-2.394060e-03j0.0000000000+0.0000000000j-1.488217e-01+8.492953e-01j0.00000000+0.00000000j1.63304500-8.19595200j
line2a376.507942+75.165439j-3.764496e+02-7.493254e+01j69627.173712-24139.231256j-1.421789e+04+4.199279e+04j190.149827-26.152266j57.686778-100.066962j
b-376.118181-74.962326j3.764496e+02+7.493254e+01j41140.247738+59810.047467j1.421789e+04-4.199279e+04j-74.719733-173.911328j57.686778-100.066962j
c-0.4929090-0.2055070j-1.421085e-14+1.136868e-13j14.902575-00126.71022600j2.725799e-11+1.047715e-11j-117.063139+208.259546j-120.249589+224.732658j
n0.1031470+0.0023940j4.263256e-14+0.000000e+00j0.1488220000-0.8492950000j2.078778e-13-1.048707e-12j1.63304500-8.19595200j4.876033-024.5987340j
\n", - "
" - ], - "text/plain": [ - " current1 current2 \\\n", - "branch_id phase \n", - "line1 a 376.726141+75.273614j -3.765079e+02-7.516544e+01j \n", - " b -376.137057-74.958399j 3.761182e+02+7.496233e+01j \n", - " c -0.4933660-0.4185120j 4.929085e-01+2.055072e-01j \n", - " n -0.0957190+0.1032970j -1.031467e-01-2.394060e-03j \n", - "line2 a 376.507942+75.165439j -3.764496e+02-7.493254e+01j \n", - " b -376.118181-74.962326j 3.764496e+02+7.493254e+01j \n", - " c -0.4929090-0.2055070j -1.421085e-14+1.136868e-13j \n", - " n 0.1031470+0.0023940j 4.263256e-14+0.000000e+00j \n", - "\n", - " power1 power2 \\\n", - "branch_id phase \n", - "line1 a 87001.175581-17383.696547j -6.962717e+04+2.413923e+04j \n", - " b 58424.246079+66571.961003j -4.114025e+04-5.981005e+04j \n", - " c -26.733437-00146.99870000j -1.490257e+01+1.267102e+02j \n", - " n 0.0000000000+0.0000000000j -1.488217e-01+8.492953e-01j \n", - "line2 a 69627.173712-24139.231256j -1.421789e+04+4.199279e+04j \n", - " b 41140.247738+59810.047467j 1.421789e+04-4.199279e+04j \n", - " c 14.902575-00126.71022600j 2.725799e-11+1.047715e-11j \n", - " n 0.1488220000-0.8492950000j 2.078778e-13-1.048707e-12j \n", - "\n", - " potential1 potential2 \n", - "branch_id phase \n", - "line1 a 230.9401008-0.0000000j 190.149827-26.152266j \n", - " b -115.470054-200.000000j -74.719733-173.911328j \n", - " c -115.470054+200.000000j -117.063139+208.259546j \n", - " n 0.00000000+0.00000000j 1.63304500-8.19595200j \n", - "line2 a 190.149827-26.152266j 57.686778-100.066962j \n", - " b -74.719733-173.911328j 57.686778-100.066962j \n", - " c -117.063139+208.259546j -120.249589+224.732658j \n", - " n 1.63304500-8.19595200j 4.876033-024.5987340j " - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "en.solve_load_flow(auth=auth)\n", "en.res_branches" @@ -338,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2023-06-09T09:38:47.669356250Z", @@ -346,169 +187,10 @@ }, "collapsed": false }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
current1current2power1power2potential1potential2
branch_idphase
line1a371.737757-146.296806j-3.715461e+02+1.463893e+02j85849.157555+33785.800020j-6.352586e+04-2.464708e+04j230.9401008+0.0000000j170.626282-0.890279j
b-325.125461-309.415778j3.251080e+02+3.094177e+02j99425.410149+29296.835634j-7.575517e+04-2.003848e+04j-115.470054-200.000000j-91.485558-148.706685j
c-46.494657+455.595154j4.650593e+01-4.557992e+02j96487.771322+43308.665615j-7.540992e+04-3.185850e+04j-115.470054+200.000000j-85.882505+156.682747j
n-0.11763900+0.11743000j-6.777239e-02-7.800686e-03j0.0000000000+0.0000000000j-4.016327e-01+5.328110e-01j0.00000000+0.00000000j6.7417810-7.0857840j
line2a371.546147-146.389264j-3.715851e+02+1.465620e+02j63525.864916+24647.076142j3.541552e+03-1.646579e+03j170.6262802-0.8902790j-6.7353330+7.0878040j
b-325.107990-309.417735j3.252783e+02+3.093020e+02j75755.171610+20038.477314j1.413844e+00+4.388761e+03j-91.485558-148.706685j-6.7353330+7.0878040j
c-46.505929+455.799198j4.630677e+01-4.558639e+02j75409.916329+31858.500204j-3.542966e+03-2.742182e+03j-85.882505+156.682747j-6.7353330+7.0878040j
n0.06777200+0.00780100j7.105427e-15+4.263256e-14j0.4016330000-0.5328110000j-7.629415e-13-1.012519e-12j6.74178100-7.08578400j20.205999-21.263413j
\n", - "
" - ], - "text/plain": [ - " current1 current2 \\\n", - "branch_id phase \n", - "line1 a 371.737757-146.296806j -3.715461e+02+1.463893e+02j \n", - " b -325.125461-309.415778j 3.251080e+02+3.094177e+02j \n", - " c -46.494657+455.595154j 4.650593e+01-4.557992e+02j \n", - " n -0.11763900+0.11743000j -6.777239e-02-7.800686e-03j \n", - "line2 a 371.546147-146.389264j -3.715851e+02+1.465620e+02j \n", - " b -325.107990-309.417735j 3.252783e+02+3.093020e+02j \n", - " c -46.505929+455.799198j 4.630677e+01-4.558639e+02j \n", - " n 0.06777200+0.00780100j 7.105427e-15+4.263256e-14j \n", - "\n", - " power1 power2 \\\n", - "branch_id phase \n", - "line1 a 85849.157555+33785.800020j -6.352586e+04-2.464708e+04j \n", - " b 99425.410149+29296.835634j -7.575517e+04-2.003848e+04j \n", - " c 96487.771322+43308.665615j -7.540992e+04-3.185850e+04j \n", - " n 0.0000000000+0.0000000000j -4.016327e-01+5.328110e-01j \n", - "line2 a 63525.864916+24647.076142j 3.541552e+03-1.646579e+03j \n", - " b 75755.171610+20038.477314j 1.413844e+00+4.388761e+03j \n", - " c 75409.916329+31858.500204j -3.542966e+03-2.742182e+03j \n", - " n 0.4016330000-0.5328110000j -7.629415e-13-1.012519e-12j \n", - "\n", - " potential1 potential2 \n", - "branch_id phase \n", - "line1 a 230.9401008+0.0000000j 170.626282-0.890279j \n", - " b -115.470054-200.000000j -91.485558-148.706685j \n", - " c -115.470054+200.000000j -85.882505+156.682747j \n", - " n 0.00000000+0.00000000j 6.7417810-7.0857840j \n", - "line2 a 170.6262802-0.8902790j -6.7353330+7.0878040j \n", - " b -91.485558-148.706685j -6.7353330+7.0878040j \n", - " c -85.882505+156.682747j -6.7353330+7.0878040j \n", - " n 6.74178100-7.08578400j 20.205999-21.263413j " - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "bus2.clear_short_circuits()\n", - "bus2.short_circuit(\"a\", \"b\", \"c\")\n", + "bus2.add_short_circuit(\"a\", \"b\", \"c\")\n", "en.solve_load_flow(auth=auth)\n", "en.res_branches" ] @@ -535,7 +217,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2023-06-09T13:15:05.411101257Z", @@ -543,169 +225,10 @@ }, "collapsed": false }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
current1current2power1power2potential1potential2
branch_idphase
line1a96.012654-188.549830j-9.579721e+01+1.886470e+02j22173.172540+43543.718061j-1.685871e+04-2.947666e+04j230.9401008-0.0000000j160.2978106-7.9652950j
b0.5278520-0.4176090j-5.447323e-01+4.163191e-01j22.570709-00153.79182900j-3.386010e+00+1.921577e+02j-115.470054-200.000000j-166.266602-225.684569j
c-0.4126860-0.5103670j4.341498e-01+2.786264e-01j-54.420413-00141.46933500j-2.119180e+01+1.217538e+02j-115.470054+200.000000j-162.048885+176.443020j
n-0.0396740-0.0650550j-1.739503e-01+1.803321e-01j0.0000000000+0.0000000000j4.190759e+00+1.361504e+01j0.00000000+0.00000000j-50.7214930-25.6873780j
line2a95.797205-188.647037j-9.590970e+01+1.889045e+02j16858.712030+29476.655117j0.000000e+00+0.000000e+00j160.2978106-7.9652950j0.00000000+0.00000000j
b0.5447320-0.4163190j1.136868e-13-5.684342e-14j3.386010-000192.157653000j-1.469214e-11-4.671278e-11j-166.266602-225.684569j-267.742704-277.018629j
c-0.4341500-0.2786260j-5.684342e-14-1.136868e-13j21.191795-00121.75379700j-1.998991e-13-3.635331e-11j-162.048885+176.443020j-255.110334+129.313498j
n0.1739500-0.1803320j2.842171e-14+4.263256e-14j-4.190759-00013.615042000j-7.607391e-12+4.295299e-12j-50.7214930-25.6873780j-152.108420-77.035237j
\n", - "
" - ], - "text/plain": [ - " current1 current2 \\\n", - "branch_id phase \n", - "line1 a 96.012654-188.549830j -9.579721e+01+1.886470e+02j \n", - " b 0.5278520-0.4176090j -5.447323e-01+4.163191e-01j \n", - " c -0.4126860-0.5103670j 4.341498e-01+2.786264e-01j \n", - " n -0.0396740-0.0650550j -1.739503e-01+1.803321e-01j \n", - "line2 a 95.797205-188.647037j -9.590970e+01+1.889045e+02j \n", - " b 0.5447320-0.4163190j 1.136868e-13-5.684342e-14j \n", - " c -0.4341500-0.2786260j -5.684342e-14-1.136868e-13j \n", - " n 0.1739500-0.1803320j 2.842171e-14+4.263256e-14j \n", - "\n", - " power1 power2 \\\n", - "branch_id phase \n", - "line1 a 22173.172540+43543.718061j -1.685871e+04-2.947666e+04j \n", - " b 22.570709-00153.79182900j -3.386010e+00+1.921577e+02j \n", - " c -54.420413-00141.46933500j -2.119180e+01+1.217538e+02j \n", - " n 0.0000000000+0.0000000000j 4.190759e+00+1.361504e+01j \n", - "line2 a 16858.712030+29476.655117j 0.000000e+00+0.000000e+00j \n", - " b 3.386010-000192.157653000j -1.469214e-11-4.671278e-11j \n", - " c 21.191795-00121.75379700j -1.998991e-13-3.635331e-11j \n", - " n -4.190759-00013.615042000j -7.607391e-12+4.295299e-12j \n", - "\n", - " potential1 potential2 \n", - "branch_id phase \n", - "line1 a 230.9401008-0.0000000j 160.2978106-7.9652950j \n", - " b -115.470054-200.000000j -166.266602-225.684569j \n", - " c -115.470054+200.000000j -162.048885+176.443020j \n", - " n 0.00000000+0.00000000j -50.7214930-25.6873780j \n", - "line2 a 160.2978106-7.9652950j 0.00000000+0.00000000j \n", - " b -166.266602-225.684569j -267.742704-277.018629j \n", - " c -162.048885+176.443020j -255.110334+129.313498j \n", - " n -50.7214930-25.6873780j -152.108420-77.035237j " - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "bus2.clear_short_circuits()\n", - "bus2.short_circuit(\"a\", ground=ground) # ground MUST be passed as a keyword argument\n", + "bus2.add_short_circuit(\"a\", ground=ground) # ground MUST be passed as a keyword argument\n", "en.solve_load_flow(auth=auth)\n", "en.res_branches" ] @@ -741,7 +264,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2023-06-09T13:15:14.199462325Z", @@ -749,34 +272,17 @@ }, "collapsed": false }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "The power load 'load' is connected on bus 'b2' that already has a short-circuit. It makes the short-circuit calculation impossible.\n" - ] - }, - { - "ename": "RoseauLoadFlowException", - "evalue": "The power load 'load' is connected on bus 'b2' that already has a short-circuit. It makes the short-circuit calculation impossible. [bad_short_circuit]", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mRoseauLoadFlowException\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[16], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m load \u001b[38;5;241m=\u001b[39m \u001b[43mPowerLoad\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mload\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbus\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbus2\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpowers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.pyenv/versions/3.11.3/envs/roseau_load_flow_engine/lib/python3.11/site-packages/roseau/load_flow/models/loads/loads.py:281\u001b[0m, in \u001b[0;36mPowerLoad.__init__\u001b[0;34m(self, id, bus, powers, phases, flexible_params, **kwargs)\u001b[0m\n\u001b[1;32m 276\u001b[0m msg \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 277\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe power load \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mid\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m is connected on bus \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbus\u001b[38;5;241m.\u001b[39mid\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m that already has a short-circuit. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 278\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIt makes the short-circuit calculation impossible.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 279\u001b[0m )\n\u001b[1;32m 280\u001b[0m logger\u001b[38;5;241m.\u001b[39merror(msg)\n\u001b[0;32m--> 281\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m RoseauLoadFlowException(msg\u001b[38;5;241m=\u001b[39mmsg, code\u001b[38;5;241m=\u001b[39mRoseauLoadFlowExceptionCode\u001b[38;5;241m.\u001b[39mBAD_SHORT_CIRCUIT)\n\u001b[1;32m 282\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m flexible_params:\n\u001b[1;32m 283\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(flexible_params) \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_size:\n", - "\u001b[0;31mRoseauLoadFlowException\u001b[0m: The power load 'load' is connected on bus 'b2' that already has a short-circuit. It makes the short-circuit calculation impossible. [bad_short_circuit]" - ] - } - ], + "outputs": [], "source": [ - "load = PowerLoad(\"load\", bus=bus2, powers=[10, 10, 10])" + "try:\n", + " load = PowerLoad(\"load\", bus=bus2, powers=[10, 10, 10])\n", + "except RoseauLoadFlowException as e:\n", + " print(e)" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2023-06-09T13:15:16.667926670Z", @@ -784,49 +290,32 @@ }, "collapsed": false }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "For the short-circuit on bus 'b2', at least two phases (or a phase and a ground) should be given (only ('a',) is given).\n" - ] - }, - { - "ename": "RoseauLoadFlowException", - "evalue": "For the short-circuit on bus 'b2', at least two phases (or a phase and a ground) should be given (only ('a',) is given). [bad_phase]", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mRoseauLoadFlowException\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[17], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m bus2\u001b[38;5;241m.\u001b[39mremove_short_circuit()\n\u001b[0;32m----> 2\u001b[0m \u001b[43mbus2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mshort_circuit\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43ma\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/.pyenv/versions/3.11.3/envs/roseau_load_flow_engine/lib/python3.11/site-packages/roseau/load_flow/models/buses.py:178\u001b[0m, in \u001b[0;36mBus.short_circuit\u001b[0;34m(self, ground, *phases)\u001b[0m\n\u001b[1;32m 173\u001b[0m msg \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 174\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFor the short-circuit on bus \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mid\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m, at least two phases (or a phase and a ground) should be \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 175\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgiven (only \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mphases\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m is given).\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 176\u001b[0m )\n\u001b[1;32m 177\u001b[0m logger\u001b[38;5;241m.\u001b[39merror(msg)\n\u001b[0;32m--> 178\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m RoseauLoadFlowException(msg\u001b[38;5;241m=\u001b[39mmsg, code\u001b[38;5;241m=\u001b[39mRoseauLoadFlowExceptionCode\u001b[38;5;241m.\u001b[39mBAD_PHASE)\n\u001b[1;32m 179\u001b[0m duplicates \u001b[38;5;241m=\u001b[39m [item \u001b[38;5;28;01mfor\u001b[39;00m item \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mset\u001b[39m(phases) \u001b[38;5;28;01mif\u001b[39;00m phases\u001b[38;5;241m.\u001b[39mcount(item) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m]\n\u001b[1;32m 180\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m duplicates:\n", - "\u001b[0;31mRoseauLoadFlowException\u001b[0m: For the short-circuit on bus 'b2', at least two phases (or a phase and a ground) should be given (only ('a',) is given). [bad_phase]" - ] - } - ], + "outputs": [], "source": [ - "bus2.short_circuit(\"a\")" + "try:\n", + " bus2.add_short_circuit(\"a\")\n", + "except RoseauLoadFlowException as e:\n", + " print(e)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.6" + "pygments_lexer": "ipython3", + "version": "3.11.4" } }, "nbformat": 4, diff --git a/roseau/load_flow/io/dict.py b/roseau/load_flow/io/dict.py index bb10c1ca..7f52544e 100644 --- a/roseau/load_flow/io/dict.py +++ b/roseau/load_flow/io/dict.py @@ -127,7 +127,7 @@ def network_from_dict( for sc in short_circuits: ground_id = sc["short_circuit"]["ground"] ground = grounds[ground_id] if ground_id is not None else None - buses[sc["bus_id"]].short_circuit(*sc["short_circuit"]["phases"], ground=ground) + buses[sc["bus_id"]].add_short_circuit(*sc["short_circuit"]["phases"], ground=ground) return buses, branches_dict, loads, sources, grounds, potential_refs diff --git a/roseau/load_flow/models/buses.py b/roseau/load_flow/models/buses.py index c274f49c..f022799d 100644 --- a/roseau/load_flow/models/buses.py +++ b/roseau/load_flow/models/buses.py @@ -156,8 +156,8 @@ def _results_to_dict(self, warning: bool) -> JsonDict: "potentials": [[v.real, v.imag] for v in self._res_potentials_getter(warning)], } - def short_circuit(self, *phases: str, ground: Optional["Ground"] = None) -> None: - """Make a short-circuit by connecting multiple phases together. + def add_short_circuit(self, *phases: str, ground: Optional["Ground"] = None) -> None: + """Add a short-circuit by connecting multiple phases together optionally with a ground. Args: phases: @@ -200,9 +200,9 @@ def short_circuit(self, *phases: str, ground: Optional["Ground"] = None) -> None self.network._valid = False @property - def short_circuits(self) -> list[dict]: - """Return the list of short circuits of this bus""" - return self._short_circuits + def short_circuits(self) -> list[dict[str, Any]]: + """Return the list of short circuits of this bus.""" + return self._short_circuits[:] # return a copy as users should not modify the list directly def clear_short_circuits(self): """Remove the short circuits.""" diff --git a/roseau/load_flow/models/tests/test_buses.py b/roseau/load_flow/models/tests/test_buses.py index 4690b345..298d7564 100644 --- a/roseau/load_flow/models/tests/test_buses.py +++ b/roseau/load_flow/models/tests/test_buses.py @@ -31,24 +31,24 @@ def test_short_circuit(): # Bad parameters with pytest.raises(RoseauLoadFlowException) as e: - bus.short_circuit("a", "n") + bus.add_short_circuit("a", "n") assert "Phase 'n' is not in the phases" in e.value.msg assert e.value.args[1] == RoseauLoadFlowExceptionCode.BAD_PHASE with pytest.raises(RoseauLoadFlowException) as e: - bus.short_circuit("n", "a") + bus.add_short_circuit("n", "a") assert "Phase 'n' is not in the phases" in e.value.msg assert e.value.args[1] == RoseauLoadFlowExceptionCode.BAD_PHASE with pytest.raises(RoseauLoadFlowException) as e: - bus.short_circuit("a", "a") + bus.add_short_circuit("a", "a") assert "some phases are duplicated" in e.value.msg assert e.value.args[1] == RoseauLoadFlowExceptionCode.BAD_PHASE with pytest.raises(RoseauLoadFlowException) as e: - bus.short_circuit("a") + bus.add_short_circuit("a") assert "at least two phases (or a phase and a ground) should be given" in e.value.msg assert e.value.args[1] == RoseauLoadFlowExceptionCode.BAD_PHASE assert not bus._short_circuits - bus.short_circuit("c", "a", "b") + bus.add_short_circuit("c", "a", "b") assert bus._short_circuits[0]["phases"] == ["c", "a", "b"] assert bus._short_circuits[0]["ground"] is None @@ -63,7 +63,7 @@ def test_short_circuit(): assert en2.buses["bus"]._short_circuits[0]["ground"] is None ground = Ground("ground") - bus.short_circuit("a", ground=ground) # ok + bus.add_short_circuit("a", ground=ground) # ok assert len(bus.short_circuits) == 2 # With power load @@ -71,6 +71,6 @@ def test_short_circuit(): assert not bus.short_circuits PowerLoad(id="load", bus=bus, powers=[10, 10, 10]) with pytest.raises(RoseauLoadFlowException) as e: - bus.short_circuit("a", "b") + bus.add_short_circuit("a", "b") assert "is already connected on bus" in e.value.msg assert e.value.args[1] == RoseauLoadFlowExceptionCode.BAD_SHORT_CIRCUIT diff --git a/roseau/load_flow/models/tests/test_loads.py b/roseau/load_flow/models/tests/test_loads.py index 62e6dd74..d4244542 100644 --- a/roseau/load_flow/models/tests/test_loads.py +++ b/roseau/load_flow/models/tests/test_loads.py @@ -146,7 +146,7 @@ def test_loads(): # Short-circuit bus = Bus(id="bus", phases="abcn") - bus.short_circuit("a", "b") + bus.add_short_circuit("a", "b") with pytest.raises(RoseauLoadFlowException) as e: PowerLoad(id="load", bus=bus, powers=[10, 10, 10]) assert "that already has a short-circuit. It makes the short-circuit calculation impossible." in e.value.msg diff --git a/roseau/load_flow/tests/test_electrical_network.py b/roseau/load_flow/tests/test_electrical_network.py index c5d01e7e..90791efb 100644 --- a/roseau/load_flow/tests/test_electrical_network.py +++ b/roseau/load_flow/tests/test_electrical_network.py @@ -1354,7 +1354,7 @@ def test_short_circuits(): vn = 400 / np.sqrt(3) voltages = [vn, vn * np.exp(-2 / 3 * np.pi * 1j), vn * np.exp(2 / 3 * np.pi * 1j)] bus = Bus("bus", phases="abcn") - bus.short_circuit("a", "n") + bus.add_short_circuit("a", "n") _ = VoltageSource(id="vs", bus=bus, voltages=voltages) _ = PotentialRef(id="pref", element=bus) en = ElectricalNetwork.from_element(initial_bus=bus)