Skip to content

Commit d1cdc9b

Browse files
committed
Update the examples
Signed-off-by: GitHub Actions Bot <[email protected]>
1 parent 06e8374 commit d1cdc9b

7 files changed

+105
-63
lines changed

examples/Asymmetric Calculation Example.ipynb

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@
119119
"asym_load[\"node\"] = [6]\n",
120120
"asym_load[\"status\"] = [1]\n",
121121
"asym_load[\"type\"] = [LoadGenType.const_power]\n",
122-
"asym_load[\"p_specified\"] = [[10e6, 20e6 , 0]] # the 3 phases may have different loads\n",
123-
"asym_load[\"q_specified\"] = [[0, 8e6, 2e6]] # the 3 phases may have different loads\n",
122+
"asym_load[\"p_specified\"] = [[10e6, 20e6, 0]] # the 3 phases may have different loads\n",
123+
"asym_load[\"q_specified\"] = [[0, 8e6, 2e6]] # the 3 phases may have different loads\n",
124124
"\n",
125125
"# source\n",
126126
"source = initialize_array(DatasetType.input, ComponentType.source, 1)\n",
@@ -135,7 +135,7 @@
135135
" ComponentType.line: line,\n",
136136
" ComponentType.sym_load: sym_load,\n",
137137
" ComponentType.asym_load: asym_load,\n",
138-
" ComponentType.source: source\n",
138+
" ComponentType.source: source,\n",
139139
"}"
140140
]
141141
},
@@ -172,7 +172,8 @@
172172
" symmetric=False, # This enables asymmetric calculations\n",
173173
" error_tolerance=1e-8,\n",
174174
" max_iterations=20,\n",
175-
" calculation_method=CalculationMethod.newton_raphson)"
175+
" calculation_method=CalculationMethod.newton_raphson,\n",
176+
")"
176177
]
177178
},
178179
{
@@ -269,13 +270,13 @@
269270
"outputs": [],
270271
"source": [
271272
"# note the shape of the array, 10 scenarios, 1 objects (asymmetric load_7)\n",
272-
"load_profile = initialize_array(DatasetType.update, ComponentType.asym_load, (10, 1)) \n",
273+
"load_profile = initialize_array(DatasetType.update, ComponentType.asym_load, (10, 1))\n",
273274
"\n",
274275
"# this is a scale of asym_load from 0% to 100%------------------\n",
275276
"# the array is an (10, 1, 3) shape, which shows (scenario, object, phase).\n",
276277
"# Users can always customize the load_profile in different ways.\n",
277278
"load_profile[\"id\"] = [7]\n",
278-
"load_profile[\"p_specified\"] = [10e6, 20e6 , 0] * np.linspace(0, 1, 10).reshape(-1, 1, 1)\n",
279+
"load_profile[\"p_specified\"] = [10e6, 20e6, 0] * np.linspace(0, 1, 10).reshape(-1, 1, 1)\n",
279280
"\n",
280281
"time_series_mutation = {ComponentType.asym_load: load_profile}"
281282
]
@@ -301,7 +302,12 @@
301302
"# Validation (optional)\n",
302303
"from power_grid_model.validation import assert_valid_batch_data\n",
303304
"\n",
304-
"assert_valid_batch_data(input_data=asym_input_data, update_data=time_series_mutation, symmetric=False, calculation_type=CalculationType.power_flow)\n",
305+
"assert_valid_batch_data(\n",
306+
" input_data=asym_input_data,\n",
307+
" update_data=time_series_mutation,\n",
308+
" symmetric=False,\n",
309+
" calculation_type=CalculationType.power_flow,\n",
310+
")\n",
305311
"\n",
306312
"# Batch Asymmetric Power Flow Calculation\n",
307313
"output_data = asym_model.calculate_power_flow(update_data=time_series_mutation, symmetric=False)"
@@ -519,7 +525,7 @@
519525
}
520526
],
521527
"source": [
522-
"display(pd.DataFrame(output_data[ComponentType.line][\"i_from\"][:,0]))"
528+
"display(pd.DataFrame(output_data[ComponentType.line][\"i_from\"][:, 0]))"
523529
]
524530
},
525531
{
@@ -560,10 +566,13 @@
560566
"sym_power_sensor[\"id\"] = [14, 15, 16, 17, 18, 19, 20]\n",
561567
"sym_power_sensor[\"measured_object\"] = [3, 3, 5, 5, 8, 8, 4]\n",
562568
"sym_power_sensor[\"measured_terminal_type\"] = [\n",
563-
" MeasuredTerminalType.branch_from, MeasuredTerminalType.branch_to,\n",
564-
" MeasuredTerminalType.branch_from, MeasuredTerminalType.branch_to,\n",
565-
" MeasuredTerminalType.branch_from, MeasuredTerminalType.branch_to,\n",
566-
" MeasuredTerminalType.load\n",
569+
" MeasuredTerminalType.branch_from,\n",
570+
" MeasuredTerminalType.branch_to,\n",
571+
" MeasuredTerminalType.branch_from,\n",
572+
" MeasuredTerminalType.branch_to,\n",
573+
" MeasuredTerminalType.branch_from,\n",
574+
" MeasuredTerminalType.branch_to,\n",
575+
" MeasuredTerminalType.load,\n",
567576
"]\n",
568577
"sym_power_sensor[\"power_sigma\"] = [1.0e5, 1.0e4, 1.0e5, 1.0e4, 1.0e4, 1.0e5, 1.0e5]\n",
569578
"sym_power_sensor[\"p_measured\"] = [10e6, -20e6, 4e6, -4e6, 25e6, -15e6, 20e6]\n",
@@ -578,7 +587,7 @@
578587
"asym_power_sensor[\"p_measured\"] = [[10e6, 20e6, 0]]\n",
579588
"asym_power_sensor[\"q_measured\"] = [[0, 8e6, 2e6]]\n",
580589
"\n",
581-
"# all \n",
590+
"# all\n",
582591
"asym_input_data = {\n",
583592
" ComponentType.node: node,\n",
584593
" ComponentType.line: line,\n",
@@ -609,6 +618,7 @@
609618
"source": [
610619
"# Validation(optional)\n",
611620
"from power_grid_model.validation import assert_valid_input_data\n",
621+
"\n",
612622
"assert_valid_input_data(input_data=asym_input_data, calculation_type=CalculationType.state_estimation, symmetric=False)\n",
613623
"\n",
614624
"# Construction\n",

examples/Make Test Dataset.ipynb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,12 @@
208208
"source[\"u_ref\"] = [1.0]\n",
209209
"\n",
210210
"# all\n",
211-
"input_data = {ComponentType.node: node, ComponentType.line: line, ComponentType.sym_load: sym_load, ComponentType.source: source}"
211+
"input_data = {\n",
212+
" ComponentType.node: node,\n",
213+
" ComponentType.line: line,\n",
214+
" ComponentType.sym_load: sym_load,\n",
215+
" ComponentType.source: source,\n",
216+
"}"
212217
]
213218
},
214219
{

examples/Power Flow Example.ipynb

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
" ComponentType.node: node,\n",
119119
" ComponentType.line: line,\n",
120120
" ComponentType.sym_load: sym_load,\n",
121-
" ComponentType.source: source\n",
121+
" ComponentType.source: source,\n",
122122
"}"
123123
]
124124
},
@@ -180,6 +180,7 @@
180180
"outputs": [],
181181
"source": [
182182
"from power_grid_model.validation import assert_valid_input_data\n",
183+
"\n",
183184
"assert_valid_input_data(input_data=input_data, calculation_type=CalculationType.power_flow)"
184185
]
185186
},
@@ -249,10 +250,8 @@
249250
"outputs": [],
250251
"source": [
251252
"output_data = model.calculate_power_flow(\n",
252-
" symmetric=True,\n",
253-
" error_tolerance=1e-8,\n",
254-
" max_iterations=20,\n",
255-
" calculation_method=CalculationMethod.newton_raphson)"
253+
" symmetric=True, error_tolerance=1e-8, max_iterations=20, calculation_method=CalculationMethod.newton_raphson\n",
254+
")"
256255
]
257256
},
258257
{
@@ -340,7 +339,8 @@
340339
" error_tolerance=1e-8,\n",
341340
" max_iterations=20,\n",
342341
" calculation_method=CalculationMethod.newton_raphson,\n",
343-
" output_component_types=[ComponentType.node])\n",
342+
" output_component_types=[ComponentType.node],\n",
343+
")\n",
344344
"\n",
345345
"print(\"List of component types in result dataset\")\n",
346346
"print(list(output_data.keys()))\n",
@@ -450,10 +450,7 @@
450450
"update_line[\"from_status\"] = [0] # switch off at from side\n",
451451
"# leave to-side swiching status the same, no need to specify\n",
452452
"\n",
453-
"update_data = {\n",
454-
" ComponentType.sym_load: update_sym_load,\n",
455-
" ComponentType.line: update_line\n",
456-
"}"
453+
"update_data = {ComponentType.sym_load: update_sym_load, ComponentType.line: update_line}"
457454
]
458455
},
459456
{
@@ -475,6 +472,7 @@
475472
"outputs": [],
476473
"source": [
477474
"from power_grid_model.validation import assert_valid_batch_data\n",
475+
"\n",
478476
"assert_valid_batch_data(input_data=input_data, update_data=update_data, calculation_type=CalculationType.power_flow)"
479477
]
480478
},
@@ -620,7 +618,9 @@
620618
"\n",
621619
"time_series_mutation = {ComponentType.sym_load: load_profile}\n",
622620
"\n",
623-
"assert_valid_batch_data(input_data=input_data, update_data=time_series_mutation, calculation_type=CalculationType.power_flow)"
621+
"assert_valid_batch_data(\n",
622+
" input_data=input_data, update_data=time_series_mutation, calculation_type=CalculationType.power_flow\n",
623+
")"
624624
]
625625
},
626626
{
@@ -730,7 +730,7 @@
730730
}
731731
],
732732
"source": [
733-
"print(output_data[ComponentType.line][\"i_from\"][:,0])"
733+
"print(output_data[ComponentType.line][\"i_from\"][:, 0])"
734734
]
735735
},
736736
{
@@ -766,20 +766,14 @@
766766
"# below the same broadcasting trick\n",
767767
"line_profile[\"id\"] = [[3, 5, 8]]\n",
768768
"# fully specify the status of all lines, even it is the same as the base scenario\n",
769-
"line_profile[\"from_status\"] = [\n",
770-
" [0, 1, 1],\n",
771-
" [1, 0, 1],\n",
772-
" [1, 1, 0]\n",
773-
"]\n",
774-
"line_profile[\"to_status\"] = [\n",
775-
" [0, 1, 1],\n",
776-
" [1, 0, 1],\n",
777-
" [1, 1, 0]\n",
778-
"]\n",
769+
"line_profile[\"from_status\"] = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]\n",
770+
"line_profile[\"to_status\"] = [[0, 1, 1], [1, 0, 1], [1, 1, 0]]\n",
779771
"\n",
780772
"n_min_1_mutation_update_all = {ComponentType.line: line_profile}\n",
781773
"\n",
782-
"assert_valid_batch_data(input_data=input_data, update_data=n_min_1_mutation_update_all, calculation_type=CalculationType.power_flow)"
774+
"assert_valid_batch_data(\n",
775+
" input_data=input_data, update_data=n_min_1_mutation_update_all, calculation_type=CalculationType.power_flow\n",
776+
")"
783777
]
784778
},
785779
{
@@ -830,7 +824,9 @@
830824
"metadata": {},
831825
"outputs": [],
832826
"source": [
833-
"line_profile = initialize_array(DatasetType.update, ComponentType.line, (3, 1)) # 3 scenarios, 1 object mutation per scenario\n",
827+
"line_profile = initialize_array(\n",
828+
" DatasetType.update, ComponentType.line, (3, 1)\n",
829+
") # 3 scenarios, 1 object mutation per scenario\n",
834830
"# for each mutation, only one object is specified\n",
835831
"line_profile[\"id\"] = [[3], [5], [8]]\n",
836832
"# specify only the changed status (switch off) of one line\n",
@@ -839,7 +835,9 @@
839835
"\n",
840836
"n_min_1_mutation_update_specific = {ComponentType.line: line_profile}\n",
841837
"\n",
842-
"assert_valid_batch_data(input_data=input_data, update_data=n_min_1_mutation_update_specific, calculation_type=CalculationType.power_flow)"
838+
"assert_valid_batch_data(\n",
839+
" input_data=input_data, update_data=n_min_1_mutation_update_specific, calculation_type=CalculationType.power_flow\n",
840+
")"
843841
]
844842
},
845843
{
@@ -973,10 +971,7 @@
973971
"line_error[\"tan1\"] = [0.0]\n",
974972
"line_error[\"i_n\"] = [1000]\n",
975973
"\n",
976-
"error_data = {\n",
977-
" ComponentType.node: node_error,\n",
978-
" ComponentType.line: line_error\n",
979-
"}\n",
974+
"error_data = {ComponentType.node: node_error, ComponentType.line: line_error}\n",
980975
"\n",
981976
"try:\n",
982977
" assert_valid_input_data(error_data, symmetric=True)\n",
@@ -1059,10 +1054,8 @@
10591054
"source": [
10601055
"try:\n",
10611056
" model.calculate_power_flow(\n",
1062-
" symmetric=True,\n",
1063-
" error_tolerance=1e-20,\n",
1064-
" max_iterations=20,\n",
1065-
" calculation_method=CalculationMethod.newton_raphson)\n",
1057+
" symmetric=True, error_tolerance=1e-20, max_iterations=20, calculation_method=CalculationMethod.newton_raphson\n",
1058+
" )\n",
10661059
"except PowerGridError as e:\n",
10671060
" print(e)"
10681061
]
@@ -1097,7 +1090,7 @@
10971090
"outputs": [],
10981091
"source": [
10991092
"time_series_mutation[ComponentType.sym_load][\"id\"][3] = 1000 # unknown id\n",
1100-
"time_series_mutation[ComponentType.sym_load][\"p_specified\"][7] = 1e100 # large power\n"
1093+
"time_series_mutation[ComponentType.sym_load][\"p_specified\"][7] = 1e100 # large power"
11011094
]
11021095
},
11031096
{
@@ -1169,14 +1162,11 @@
11691162
}
11701163
],
11711164
"source": [
1172-
"# we run the batch calculation with continue_on_batch_error=True, \n",
1165+
"# we run the batch calculation with continue_on_batch_error=True,\n",
11731166
"# it will return the results with partially valid data\n",
11741167
"\n",
11751168
"\n",
1176-
"output_data = model.calculate_power_flow(\n",
1177-
" update_data=time_series_mutation,\n",
1178-
" continue_on_batch_error=True\n",
1179-
")\n",
1169+
"output_data = model.calculate_power_flow(update_data=time_series_mutation, continue_on_batch_error=True)\n",
11801170
"\n",
11811171
"# print node data for u_pu, note that the data is rubbish for scenario 3 and 7\n",
11821172
"print(\"Node data with invalid results\")\n",

examples/Short Circuit Example.ipynb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@
5555
" import pandas as pd\n",
5656
"\n",
5757
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
58-
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType, FaultType, FaultPhase, ShortCircuitVoltageScaling\n",
58+
"from power_grid_model import (\n",
59+
" PowerGridModel,\n",
60+
" CalculationMethod,\n",
61+
" CalculationType,\n",
62+
" FaultType,\n",
63+
" FaultPhase,\n",
64+
" ShortCircuitVoltageScaling,\n",
65+
")\n",
5966
"from power_grid_model import initialize_array"
6067
]
6168
},
@@ -131,7 +138,7 @@
131138
" ComponentType.line: line,\n",
132139
" ComponentType.sym_load: sym_load,\n",
133140
" ComponentType.source: source,\n",
134-
" ComponentType.fault: fault\n",
141+
" ComponentType.fault: fault,\n",
135142
"}"
136143
]
137144
},
@@ -173,6 +180,7 @@
173180
"outputs": [],
174181
"source": [
175182
"from power_grid_model.validation import assert_valid_input_data\n",
183+
"\n",
176184
"assert_valid_input_data(input_data=input_data, calculation_type=CalculationType.short_circuit)"
177185
]
178186
},
@@ -230,7 +238,9 @@
230238
},
231239
"outputs": [],
232240
"source": [
233-
"output_data = model.calculate_short_circuit(calculation_method=CalculationMethod.iec60909, short_circuit_voltage_scaling=ShortCircuitVoltageScaling.maximum)\n"
241+
"output_data = model.calculate_short_circuit(\n",
242+
" calculation_method=CalculationMethod.iec60909, short_circuit_voltage_scaling=ShortCircuitVoltageScaling.maximum\n",
243+
")"
234244
]
235245
},
236246
{
@@ -296,7 +306,7 @@
296306
"print(\"\\n------line result: id------\")\n",
297307
"print(output_data[ComponentType.line][\"id\"])\n",
298308
"print(\"------line result: u_pu------\")\n",
299-
"print(output_data[ComponentType.line][\"i_from\"])\n"
309+
"print(output_data[ComponentType.line][\"i_from\"])"
300310
]
301311
},
302312
{

0 commit comments

Comments
 (0)