Skip to content

Commit 053022b

Browse files
committed
Merge remote-tracking branch 'origin/structure_dict_extension' into structure_dict_extension
2 parents 81befb5 + cbc51ca commit 053022b

File tree

63 files changed

+3891
-1665
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+3891
-1665
lines changed

.github/workflows/github_test_action.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ jobs:
4949
- name: Test with pytest
5050
if: ${{ matrix.python-version != '3.10' }}
5151
run: |
52-
uv run pytest -n=auto --splits 2 --group ${{ matrix.group }} --ignore=pandapower/test/opf
52+
uv run pytest --splits 2 --group ${{ matrix.group }} --ignore=pandapower/test/opf
5353
- name: Test with pytest, Codecov and Coverage
5454
if: ${{ matrix.python-version == '3.10' }}
5555
run: |
5656
uv pip install pytest-cov numpy~=1.26
57-
uv run pytest -n=auto --cov=./ --cov-report=xml --splits 2 --group ${{ matrix.group }} --ignore=pandapower/test/opf
57+
uv run pytest --cov=./ --cov-report=xml --splits 2 --group ${{ matrix.group }} --ignore=pandapower/test/opf
5858
cp ./coverage.xml ./coverage-${{ matrix.group }}.xml
5959
- name: Upload coverage as artifact
6060
if: ${{ matrix.python-version == '3.10' }}
@@ -86,7 +86,7 @@ jobs:
8686
uv pip list
8787
- name: Test with pytest
8888
run: |
89-
uv run pytest -n=auto --splits 2 --group ${{ matrix.group }} --ignore=pandapower/test/opf
89+
uv run pytest --splits 2 --group ${{ matrix.group }} --ignore=pandapower/test/opf
9090
9191
opf:
9292
# needs: build
@@ -133,7 +133,7 @@ jobs:
133133
- name: Test with pytest, Codecov and Coverage
134134
run: |
135135
pytest --cov=./ --cov-report=xml pandapower/test/opf
136-
# uv run python-jl -m pytest -n=auto --cov=./ --cov-report=xml
136+
# uv run python-jl -m pytest --cov=./ --cov-report=xml
137137
cp ./coverage.xml ./coverage-${{ matrix.group }}.xml
138138
139139
upload-coverage:
@@ -342,7 +342,7 @@ jobs:
342342
- name: Test with pytest
343343
# Careful when copying this command. The PYTHONPATH setup is Linux specific syntax.
344344
run: |
345-
pytest --nbmake -n=auto --nbmake-timeout=900 --timeout=900 "./tutorials"
345+
pytest --nbmake --nbmake-timeout=900 --timeout=900 "./tutorials"
346346
347347
tutorial_warnings_tests:
348348
needs: build
@@ -358,7 +358,7 @@ jobs:
358358
python -m pip list
359359
- name: Test with pytest
360360
run: |
361-
pytest -W error --nbmake -n=auto --nbmake-timeout=900 --timeout=900 "./tutorials"
361+
pytest -W error --nbmake --nbmake-timeout=900 --timeout=900 "./tutorials"
362362
363363
docs_check:
364364
needs: build

.github/workflows/test_release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ jobs:
5555
5656
- name: Test with pytest
5757
run: |
58-
uv run pytest -n=auto --splits 2 --group ${{ matrix.group }}
58+
uv run pytest --splits 2 --group ${{ matrix.group }}

.test_durations

Lines changed: 1488 additions & 0 deletions
Large diffs are not rendered by default.

pandapower/auxiliary.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,7 +1938,17 @@ def SVabc_from_SV012(
19381938

19391939

19401940
def _add_dcline_gens(net: pandapowerNet) -> None:
1941+
"""
1942+
For each HVDC Link create consumption and supply generator
1943+
1944+
Parameters:
1945+
net: network to add the generators to
1946+
1947+
Returns:
1948+
None
1949+
"""
19411950
from pandapower.create import create_gen
1951+
19421952
for dctab in net.dcline.itertuples():
19431953
p_mw = np.abs(dctab.p_mw)
19441954
p_loss = p_mw * (1 - dctab.loss_percent / 100) - dctab.loss_mw # type: ignore[operator]

pandapower/create/switch_create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def create_switches(
139139
buses: Sequence,
140140
elements: Sequence,
141141
et: SwitchElementType | Sequence[str],
142-
closed: bool = True,
142+
closed: bool | Iterable[bool] = True,
143143
type: SwitchType | None = None,
144144
name: Iterable[str] | None = None,
145145
index: Int | Iterable[Int] | None = None,

pandapower/grid_equivalents/auxiliary.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import numpy as np
77
import pandas as pd
88

9-
from pandapower.auxiliary import _init_runpp_options, _add_dcline_gens, LoadflowNotConverged
9+
from pandapower.auxiliary import (
10+
_init_runpp_options,
11+
_add_dcline_gens,
12+
LoadflowNotConverged,
13+
pandapowerNet,
14+
)
1015
from pandapower.create import create_ext_grid, create_bus, create_impedance, create_transformer_from_parameters, \
1116
create_load
1217
from pandapower.diagnostic import diagnostic
@@ -27,7 +32,7 @@
2732
impedance_columns = ["from_bus", "to_bus", "rft_pu", "xft_pu", "rtf_pu", "xtf_pu", "gf_pu", "bf_pu", "gt_pu", "bt_pu"]
2833

2934

30-
def _runpp_except_voltage_angles(net, **kwargs):
35+
def _runpp_except_voltage_angles(net: pandapowerNet, **kwargs) -> pandapowerNet:
3136
if "calculate_voltage_angles" not in kwargs or not kwargs["calculate_voltage_angles"]:
3237
runpp(net, **kwargs)
3338
else:
@@ -404,15 +409,19 @@ def match_cost_functions_and_eq_net(net, boundary_buses, eq_type):
404409

405410
def _check_network(net):
406411
"""
407-
This function will perfoms some checks and modifications on the given grid model.
412+
This function will perform some checks and modifications on the given grid model.
413+
414+
Check inactive elements
415+
Check dc lines and replace by gen if exists
416+
Check controller names
408417
"""
409-
# --- check invative elements
418+
# --- check inactive elements
410419
if net.res_bus.vm_pu.isnull().any():
411420
logger.info("There are some inactive buses. It is suggested to remove "
412421
"them using 'pandapower.drop_inactive_elements()' "
413422
"before starting the grid equivalent calculation.")
414423

415-
# --- check dclines
424+
# --- check and replace dclines by gens
416425
if "dcline" in net and len(net.dcline.query("in_service")) > 0:
417426
_add_dcline_gens(net)
418427
dcline_index = net.dcline.index.values

pandapower/grid_equivalents/get_equivalent.py

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
import pandas as pd
55

66
from pandapower.create import create_group_from_dict
7-
from pandapower.grid_equivalents.auxiliary import drop_assist_elms_by_creating_ext_net, \
8-
drop_internal_branch_elements, add_ext_grids_to_boundaries, \
9-
_ensure_unique_boundary_bus_names, match_cost_functions_and_eq_net, \
10-
_check_network, _runpp_except_voltage_angles
11-
from pandapower.grid_equivalents.rei_generation import _create_net_zpbn, \
12-
_get_internal_and_external_nets, _calculate_equivalent_Ybus, \
13-
_create_bus_lookups, _calclate_equivalent_element_params, \
14-
_replace_ext_area_by_impedances_and_shunts
15-
from pandapower.grid_equivalents.ward_generation import \
16-
_calculate_ward_and_impedance_parameters, \
17-
_calculate_xward_and_impedance_parameters, \
18-
create_passive_external_net_for_ward_admittance, \
19-
_replace_external_area_by_wards, _replace_external_area_by_xwards
7+
from pandapower.grid_equivalents.auxiliary import (
8+
drop_assist_elms_by_creating_ext_net, drop_internal_branch_elements, add_ext_grids_to_boundaries,
9+
_ensure_unique_boundary_bus_names, match_cost_functions_and_eq_net, _check_network, _runpp_except_voltage_angles
10+
)
11+
from pandapower.grid_equivalents.rei_generation import (
12+
_create_net_zpbn, _get_internal_and_external_nets, _calculate_equivalent_Ybus, _create_bus_lookups,
13+
_calclate_equivalent_element_params, _replace_ext_area_by_impedances_and_shunts
14+
)
15+
from pandapower.grid_equivalents.ward_generation import (
16+
_calculate_ward_and_impedance_parameters, _calculate_xward_and_impedance_parameters,
17+
create_passive_external_net_for_ward_admittance, _replace_external_area_by_wards, _replace_external_area_by_xwards
18+
)
2019
from pandapower.groups import isin_group, set_group_reference_column
21-
from pandapower.run import runpp
2220
from pandapower.toolbox.data_modification import reindex_buses
23-
from pandapower.toolbox.grid_modification import replace_ward_by_internal_elements, replace_xward_by_internal_elements, \
24-
drop_buses, drop_elements_at_buses, merge_nets, fuse_buses
21+
from pandapower.toolbox.grid_modification import (
22+
replace_ward_by_internal_elements, replace_xward_by_internal_elements, drop_buses, drop_elements_at_buses,
23+
merge_nets, fuse_buses
24+
)
2525
from pandapower.topology.create_graph import create_nxgraph
2626
from pandapower.topology.graph_searches import connected_component, connected_components
2727

@@ -144,11 +144,6 @@ def get_equivalent(
144144
logger.debug("xward elements of the external network are replaced by internal elements.")
145145
replace_xward_by_internal_elements(net, xwards=ext_buses_with_xward.index)
146146

147-
# --- switch from ward injection to ward addmittance if requested
148-
if eq_type in ["ward", "xward"] and ward_type == "ward_admittance":
149-
create_passive_external_net_for_ward_admittance(
150-
net, all_external_buses, boundary_buses, runpp_fct=runpp_fct, **kwargs)
151-
152147
# --- rei calculations
153148
if eq_type == "rei":
154149
# --- create zero power balance network
@@ -182,6 +177,11 @@ def get_equivalent(
182177

183178
# --- ward and xward calculations
184179
elif eq_type in ["ward", "xward"]:
180+
# --- switch from ward injection to ward addmittance if requested
181+
if ward_type == "ward_admittance":
182+
create_passive_external_net_for_ward_admittance(
183+
net, all_external_buses, boundary_buses, runpp_fct=runpp_fct, **kwargs)
184+
185185
net_internal, net_external = _get_internal_and_external_nets(
186186
net, boundary_buses, all_internal_buses, all_external_buses,
187187
calc_volt_angles=calculate_voltage_angles, runpp_fct=runpp_fct)
@@ -201,34 +201,38 @@ def get_equivalent(
201201

202202
if eq_type == "ward":
203203
# --- calculate equivalent impedance and wards
204-
ward_parameter_no_power, impedance_parameter = \
205-
_calculate_ward_and_impedance_parameters(Ybus_eq, bus_lookups,
206-
show_computing_time)
204+
ward_parameter_no_power, impedance_parameter = _calculate_ward_and_impedance_parameters(
205+
Ybus_eq, bus_lookups, show_computing_time
206+
)
207207

208208
# --- replace external network by equivalent elements
209-
_replace_external_area_by_wards(net_external, bus_lookups,
210-
ward_parameter_no_power,
211-
impedance_parameter,
212-
ext_buses_with_xward,
213-
show_computing_time,
214-
calc_volt_angles=calculate_voltage_angles,
215-
runpp_fct=runpp_fct)
209+
_replace_external_area_by_wards(
210+
net_external,
211+
bus_lookups,
212+
ward_parameter_no_power,
213+
impedance_parameter,
214+
ext_buses_with_xward,
215+
show_computing_time,
216+
calc_volt_angles=calculate_voltage_angles,
217+
runpp_fct=runpp_fct
218+
)
216219
else: # eq_type == "xward"
217220
# --- calculate equivalent impedance and xwards
218-
xward_parameter_no_power, impedance_parameter = \
219-
_calculate_xward_and_impedance_parameters(net_external,
220-
Ybus_eq,
221-
bus_lookups,
222-
show_computing_time)
221+
xward_parameter_no_power, impedance_parameter = _calculate_xward_and_impedance_parameters(
222+
net_external, Ybus_eq, bus_lookups, show_computing_time
223+
)
223224

224225
# --- replace external network by equivalent elements
225-
_replace_external_area_by_xwards(net_external, bus_lookups,
226-
xward_parameter_no_power,
227-
impedance_parameter,
228-
ext_buses_with_xward,
229-
show_computing_time,
230-
calc_volt_angles=calculate_voltage_angles,
231-
runpp_fct=runpp_fct)
226+
_replace_external_area_by_xwards(
227+
net_external,
228+
bus_lookups,
229+
xward_parameter_no_power,
230+
impedance_parameter,
231+
ext_buses_with_xward,
232+
show_computing_time,
233+
calc_volt_angles=calculate_voltage_angles,
234+
runpp_fct=runpp_fct
235+
)
232236
net_eq = net_external
233237
else:
234238
raise NotImplementedError(f"The {eq_type=} is unknown.")

pandapower/grid_equivalents/rei_generation.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -523,33 +523,35 @@ def _get_internal_and_external_nets(net, boundary_buses, all_internal_buses,
523523
all_external_buses, show_computing_time=False,
524524
calc_volt_angles=True,
525525
runpp_fct=_runpp_except_voltage_angles, **kwargs):
526-
"This function identifies the internal area and the external area"
526+
"""
527+
This function identifies the internal area and the external area
528+
"""
527529
t_start = time.perf_counter()
528530
if not all_internal_buses:
529531
net_internal = None
530532
else:
531533
net_internal = deepcopy(net)
532-
drop_measurements_and_controllers(net_internal, all_external_buses)
534+
drop_measurements_and_controllers(net_internal, all_external_buses, True)
533535
drop_and_edit_cost_functions(net_internal, all_external_buses + boundary_buses, True, True)
534536
drop_buses(net_internal, all_external_buses)
535537

536538
net_external = deepcopy(net)
537539
if "group" in net_external:
538-
net_external.group = net_external.group.drop(net_external.group.index)
540+
net_external.group = net_external.group[:0] # clear dataframe
539541
drop_and_edit_cost_functions(net_external, all_internal_buses, True, True)
540542
drop_measurements_and_controllers(net_external, net_external.bus.index.tolist())
541543
drop_buses(net_external, all_internal_buses)
542544
replace_motor_by_load(net_external, all_external_buses)
543545
# add_ext_grids_to_boundaries(net_external, boundary_buses, runpp_fct=runpp_fct, **kwargs)
544546
# runpp_fct(net_external, calculate_voltage_angles=calc_volt_angles, **kwargs)
545-
_integrate_power_elements_connected_with_switch_buses(net, net_external,
546-
all_external_buses) # for sgens, gens, and loads
547+
548+
# for sgens, gens, and loads:
549+
_integrate_power_elements_connected_with_switch_buses(net, net_external, all_external_buses)
550+
547551
runpp_fct(net_external, calculate_voltage_angles=calc_volt_angles, **kwargs)
548552
t_end = time.perf_counter()
549553
if show_computing_time:
550-
logger.info("\"get_int_and_ext_nets\" " +
551-
"finished in %s seconds:" % round((t_end - t_start), 2))
552-
554+
logger.info(f'"get_int_and_ext_nets" finished in {t_end - t_start:.02f} seconds.')
553555
return net_internal, net_external
554556

555557

pandapower/grid_equivalents/ward_generation.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
logger = logging.getLogger(__name__)
1313

1414

15-
def _calculate_ward_and_impedance_parameters(Ybus_eq, bus_lookups, show_computing_time, power_eq=0):
16-
"""calculates the wards and equivalente impedance to represente the
17-
external network"""
15+
def _calculate_ward_and_impedance_parameters(Ybus_eq, bus_lookups, show_computing_time):
16+
"""
17+
calculates the wards and equivalent impedance to represent the external network
18+
"""
1819
t_start = time.perf_counter()
19-
# --- calculate ward paramter
20+
# --- calculate ward parameter
2021
b_buses_ppc = bus_lookups["bus_lookup_ppc"]["b_area_buses"]
2122
b_buses_pd = bus_lookups["bus_lookup_pd"]["b_area_buses"]
2223
nb_b_buses_ppc = len(b_buses_ppc)
@@ -27,36 +28,35 @@ def _calculate_ward_and_impedance_parameters(Ybus_eq, bus_lookups, show_computin
2728
ward_parameter["shunt"] = Ybus_eq.sum(axis=1)[-nb_b_buses_ppc:]
2829
ward_parameter["power_eq"] = 0 + 1j * 0 # power_eq.power_eq.values
2930

30-
# --- calculate impedance paramter
31+
# --- calculate impedance parameter
3132
params = Ybus_eq[-nb_b_buses_ppc:, -nb_b_buses_ppc:]
3233
nl = nb_b_buses_ppc * (nb_b_buses_ppc - 1) // 2
3334
impedance_parameter = pd.DataFrame(
3435
data=np.arange(nl * len(impedance_columns)).reshape((nl, len(impedance_columns))), columns=impedance_columns,
3536
dtype=np.float64)
3637
k = 0
3738
for i in range(nb_b_buses_ppc):
38-
for j in range(nb_b_buses_ppc):
39-
if j > i:
40-
if np.abs(params[i, j]) > 1e-10:
41-
impedance_parameter.loc[k, 'from_bus'] = b_buses_pd[i]
42-
impedance_parameter.loc[k, 'to_bus'] = b_buses_pd[j]
43-
impedance_parameter.loc[k, 'rft_pu'] = (-1 / params[i, j]).real
44-
impedance_parameter.loc[k, 'xft_pu'] = (-1 / params[i, j]).imag
45-
impedance_parameter.loc[k, 'rtf_pu'] = (-1 / params[j, i]).real
46-
impedance_parameter.loc[k, 'xtf_pu'] = (-1 / params[j, i]).imag
47-
k += 1
48-
else:
49-
impedance_parameter = impedance_parameter[:-1]
39+
for j in range(i+1, nb_b_buses_ppc):
40+
if np.abs(params[i, j]) > 1e-10:
41+
impedance_parameter.loc[k, 'from_bus'] = b_buses_pd[i]
42+
impedance_parameter.loc[k, 'to_bus'] = b_buses_pd[j]
43+
impedance_parameter.loc[k, 'rft_pu'] = (-1 / params[i, j]).real
44+
impedance_parameter.loc[k, 'xft_pu'] = (-1 / params[i, j]).imag
45+
impedance_parameter.loc[k, 'rtf_pu'] = (-1 / params[j, i]).real
46+
impedance_parameter.loc[k, 'xtf_pu'] = (-1 / params[j, i]).imag
47+
k += 1
48+
else:
49+
impedance_parameter = impedance_parameter[:-1]
5050
t_end = time.perf_counter()
5151
if show_computing_time:
52-
logger.info("\"calculate_ward_and_impedance_parameters\" finished in %s seconds:" % round((
53-
t_end - t_start), 2))
52+
logger.info(f'"calculate_ward_and_impedance_parameters" finished in {t_end-t_start:02f} seconds.')
5453
return ward_parameter, impedance_parameter
5554

5655

57-
def _calculate_xward_and_impedance_parameters(net_external, Ybus_eq, bus_lookups,
58-
show_computing_time, power_eq=0):
59-
"""calculates the xwards and the equivalent impedance"""
56+
def _calculate_xward_and_impedance_parameters(net_external, Ybus_eq, bus_lookups, show_computing_time):
57+
"""
58+
calculates the xward and the equivalent impedance
59+
"""
6060
t_start = time.perf_counter()
6161
xward_parameter, impedance_parameter = \
6262
_calculate_ward_and_impedance_parameters(Ybus_eq, bus_lookups, False)
@@ -65,13 +65,10 @@ def _calculate_xward_and_impedance_parameters(net_external, Ybus_eq, bus_lookups
6565
-1 / xward_parameter.shunt.values.imag /
6666
net_external.sn_mva * net_external.bus.vn_kv[xward_parameter.bus_pd].values ** 2 #/2
6767
)
68-
# np.square(net_external.bus.vn_kv[xward_parameter.bus_pd.values].values) / \
69-
# net_external.sn_mva/2
7068
xward_parameter["vm_pu"] = net_external.res_bus.vm_pu[xward_parameter.bus_pd.values].values
7169
t_end = time.perf_counter()
7270
if show_computing_time:
73-
logger.info("\"calculate_xward_and_impedance_parameters\" finished in %s seconds:" % round((
74-
t_end - t_start), 2))
71+
logger.info(f'"calculate_xward_and_impedance_parameters" finished in {t_end-t_start:02f} seconds.')
7572
return xward_parameter, impedance_parameter
7673

7774

0 commit comments

Comments
 (0)