Skip to content

Commit

Permalink
Fix per uniting of lines with not same nominal voltage at both side (#…
Browse files Browse the repository at this point in the history
…651)

* Fix per uniting of lines with not same nominal voltage at both side

Signed-off-by: Geoffroy Jamgotchian <geoffroy.jamgotchian@rte-france.com>
  • Loading branch information
geofjamg authored Sep 22, 2023
1 parent 3029ed8 commit d464956
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
28 changes: 24 additions & 4 deletions pypowsybl/network/impl/perunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,25 @@ def _per_unit_r(self, df: pd.DataFrame, columns: List[str], nominal_v: pd.Series
for col in columns:
df[col] /= factor

def _per_unit_r_not_same_nom_v(self, df: pd.DataFrame, columns: List[str], nominal_v1: pd.Series, nominal_v2: pd.Series) -> None:
factor = nominal_v1 * nominal_v2 / self.sn
for col in columns:
df[col] /= factor

def _per_unit_g(self, df: pd.DataFrame, columns: List[str], nominal_v: pd.Series) -> None:
factor = nominal_v ** 2 / self.sn
for col in columns:
df[col] *= factor

def _y(self, df: pd.DataFrame, r_col: str, x_col: str) -> pd.Series:
return pd.Series(df.apply(lambda row: np.reciprocal(np.complex128(row[r_col] + row[x_col] * 1j)), axis=1))

def _per_unit_g_not_same_nom_v(self, df: pd.DataFrame, column: str, ytr: pd.Series, nominal_v1: pd.Series, nominal_v2: pd.Series) -> None:
df[column] = (df[column] * nominal_v1 * nominal_v1 + (nominal_v1 - nominal_v2) * nominal_v1 * ytr.apply(lambda row: row.real)) / self.sn

def _per_unit_b_not_same_nom_v(self, df: pd.DataFrame, column: str, ytr: pd.Series, nominal_v1: pd.Series, nominal_v2: pd.Series) -> None:
df[column] = (df[column] * nominal_v1 * nominal_v1 + (nominal_v1 - nominal_v2) * nominal_v1 * ytr.apply(lambda row: row.imag)) / self.sn

def _per_unit_i(self, df: pd.DataFrame, columns: List[str], nominal_v: pd.Series) -> None:
factor = self.sn * 10 ** 3 / (self.sqrt3 * nominal_v)
for col in columns:
Expand Down Expand Up @@ -163,11 +177,17 @@ def get_lines(self) -> pd.DataFrame:
a per-united dataframe of lines.
"""
lines = self._network.get_lines()
nominal_v = self._get_indexed_nominal_v(lines, 'voltage_level2_id')
nominal_v1 = self._get_indexed_nominal_v(lines, 'voltage_level1_id')
nominal_v2 = self._get_indexed_nominal_v(lines, 'voltage_level2_id')
self._per_unit_p(lines, ['p1', 'p2', 'q1', 'q2'])
self._per_unit_i(lines, ['i1', 'i2'], nominal_v)
self._per_unit_r(lines, ['r', 'x'], nominal_v)
self._per_unit_g(lines, ['g1', 'g2', 'b1', 'b2'], nominal_v)
self._per_unit_i(lines, ['i1'], nominal_v1)
self._per_unit_i(lines, ['i2'], nominal_v2)
ytr = self._y(lines, 'r', 'x')
self._per_unit_r_not_same_nom_v(lines, ['r', 'x'], nominal_v1, nominal_v2)
self._per_unit_g_not_same_nom_v(lines, 'g1', ytr, nominal_v1, nominal_v2)
self._per_unit_g_not_same_nom_v(lines, 'g2', ytr, nominal_v2, nominal_v1)
self._per_unit_b_not_same_nom_v(lines, 'b1', ytr, nominal_v1, nominal_v2)
self._per_unit_b_not_same_nom_v(lines, 'b2', ytr, nominal_v2, nominal_v1)
return lines

def get_2_windings_transformers(self) -> pd.DataFrame:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_per_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pandas as pd
from numpy import NaN
import util
import pytest


def test_bus_per_unit():
Expand Down Expand Up @@ -413,3 +414,14 @@ def test_ratio_tap_changers_per_unit():
'alpha'],
data=[[1, 0, 2, 3, True, True, 158.0, 0.0, 'VLLOAD_0', 1.00, NaN]])
pd.testing.assert_frame_equal(expected, n.get_ratio_tap_changers(), check_dtype=False, atol=1e-2)

def test_lines_not_same_nominal_voltage_per_unit():
n = pp.network.create_ieee14()
n = per_unit_view(n, 100)
lines = n.get_lines()
assert lines.loc['L7-8-1']['r'] == 0
assert lines.loc['L7-8-1']['x'] == pytest.approx(0.17615, rel=1e-5)
assert lines.loc['L7-8-1']['g1'] == pytest.approx(0, rel=1e-16)
assert lines.loc['L7-8-1']['g2'] == pytest.approx(0, rel=1e-16)
assert lines.loc['L7-8-1']['b1'] == pytest.approx(0, rel=1e-16)
assert lines.loc['L7-8-1']['b2'] == pytest.approx(0, rel=1e-16)

0 comments on commit d464956

Please sign in to comment.