Skip to content

Commit

Permalink
fix: add is_symmetrical to RatedPower
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
sasanjac committed Jul 4, 2023
1 parent 2dd0480 commit cf85636
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
17 changes: 14 additions & 3 deletions psdm/topology/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ def validate_total(values: dict[str, float]) -> dict[str, float]:


def validate_symmetry(values: dict[str, float]) -> dict[str, float]:
if values["is_symmetrical"] and not (values["value_a"] == values["value_b"] == values["value_c"]):
msg = "Power mismatch: Three-phase power of load is not symmetrical."
raise ValueError(msg)
if values["value"] != 0:
if values["is_symmetrical"]:
if not (values["value_a"] == values["value_b"] == values["value_c"]):
msg = "Power mismatch: Three-phase power of load is not symmetrical."
raise ValueError(msg)

elif values["value_a"] == values["value_b"] == values["value_c"]:
msg = "Power mismatch: Three-phase power of load is symmetrical."
raise ValueError(msg)

return values

Expand All @@ -106,6 +112,11 @@ class RatedPower(Base):
cosphi_b: float = pydantic.Field(1, ge=0, le=1) # rated cos(phi) (phase b)
cosphi_c: float = pydantic.Field(1, ge=0, le=1) # rated cos(phi) (phase c)
power_type: PowerType
is_symmetrical: bool

@pydantic.root_validator(skip_on_failure=True)
def _validate_symmetry(cls, values: dict[str, float]) -> dict[str, float]:
return validate_symmetry(values)

@pydantic.root_validator(skip_on_failure=True)
def _validate_total(cls, values: dict[str, float]) -> dict[str, float]:
Expand Down
18 changes: 12 additions & 6 deletions tests/test_topology_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ class TestRatedPower:
"cosphi_b",
"cosphi_c",
"power_type",
"is_symmetrical",
"expectation",
),
[
(0, 0, 0, 0, 0, 0, 0, 0, PowerType.AC_APPARENT, does_not_raise()),
(3, 1, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, does_not_raise()),
(4, 2, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, does_not_raise()),
(2, 2, 1, 1, 1, 1, 2, 1, PowerType.AC_APPARENT, pytest.raises(pydantic.ValidationError)),
(2, -2, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, pytest.raises(pydantic.ValidationError)),
(0, -2, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, pytest.raises(pydantic.ValidationError)),
(0, 0, 0, 0, 0, 0, 0, 0, PowerType.AC_APPARENT, True, does_not_raise()),
(3, 1, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, True, does_not_raise()),
(4, 2, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, False, does_not_raise()),
(2, 2, 1, 1, 1, 1, 2, 1, PowerType.AC_APPARENT, False, pytest.raises(pydantic.ValidationError)),
(2, -2, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, True, pytest.raises(pydantic.ValidationError)),
(0, -2, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, True, pytest.raises(pydantic.ValidationError)),
(4, 2, 1, 1, 1, 1, 2, 1, PowerType.AC_APPARENT, True, pytest.raises(pydantic.ValidationError)),
(0, -2, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, True, pytest.raises(pydantic.ValidationError)),
(3, 1, 1, 1, 1, 1, 1, 1, PowerType.AC_APPARENT, False, pytest.raises(pydantic.ValidationError)),
],
)
def test_init( # noqa: PLR0913
Expand All @@ -45,6 +49,7 @@ def test_init( # noqa: PLR0913
cosphi_b,
cosphi_c,
power_type,
is_symmetrical,
expectation,
) -> None:
with expectation:
Expand All @@ -57,5 +62,6 @@ def test_init( # noqa: PLR0913
cosphi_a=cosphi_a,
cosphi_b=cosphi_b,
cosphi_c=cosphi_c,
is_symmetrical=is_symmetrical,
power_type=power_type,
)

0 comments on commit cf85636

Please sign in to comment.