Skip to content

Commit

Permalink
ENH: Add some setters
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit9126 committed Nov 29, 2024
1 parent 83d941e commit e710cee
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
18 changes: 18 additions & 0 deletions roseau/load_flow_single/models/lines/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ def ampacity(self) -> Q_[Float] | None:
"""
return None if self._ampacity is None else Q_(self._ampacity, "A")

@ampacity.setter
@ureg_wraps(None, (None, "A"))
def ampacity(self, value: float | Q_[float] | None) -> None:
self._ampacity = self._check_positive_float(value=value, name="ampacity", unit="A")

@material.setter
def material(self, value: Material | None) -> None:
self._material = None if pd.isna(value) else Material(value)

@insulator.setter
def insulator(self, value: Insulator | None) -> None:
self._insulator = None if pd.isna(value) else Insulator(value)

@section.setter
@ureg_wraps(None, (None, "mm**2"))
def section(self, value: float | Q_[float] | None) -> None:
self._section = self._check_positive_float(value=value, name="section", unit="mm²")

@classmethod
@ureg_wraps(None, (None, None, "ohm/km", "ohm/km", "S/km", "S/km", "A"))
def from_sym(
Expand Down
45 changes: 22 additions & 23 deletions roseau/load_flow_single/models/tests/test_line_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,20 +472,20 @@ def test_insulator():
# None-like values
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, insulator=None)
assert lp.insulator is None
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, insulator=np.nan)
lp.insulator = np.nan
assert lp.insulator is None
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, insulator=pd.NA)
lp.insulator = pd.NA
assert lp.insulator is None

# Single values
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, insulator=Insulator.EPR)
lp.insulator = Insulator.EPR
assert lp.insulator == Insulator.EPR

lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, insulator=Insulator.EPR.name)
lp.insulator = Insulator.EPR.name
assert lp.insulator == Insulator.EPR

# Special case for Insulator.NONE
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, insulator=Insulator.NONE.name)
lp.insulator = Insulator.NONE.name
assert lp.insulator == Insulator.NONE


Expand All @@ -496,21 +496,21 @@ def test_material():
# None-like values
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, material=None)
assert lp.material is None
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, material=np.nan)
lp.material = np.nan
assert lp.material is None
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, material=pd.NA)
lp.material = pd.NA
assert lp.material is None

# Single values
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, material=Material.AAAC)
lp.material = Material.AAAC
assert lp.material == Material.AAAC

lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, material=Material.AAAC.name)
lp.material = Material.AAAC.name
assert lp.material == Material.AAAC

# Errors
with pytest.raises(RoseauLoadFlowException) as e:
LineParameters("test", z_line=z_line, y_shunt=y_shunt, material="invalid")
lp.material = "invalid"
assert e.value.msg == "'invalid' cannot be converted into a Material."
assert e.value.code == RoseauLoadFlowExceptionCode.BAD_MATERIAL

Expand All @@ -522,26 +522,26 @@ def test_section():
# None-like values
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, section=None)
assert lp.section is None
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, section=np.nan)
lp.section = np.nan
assert lp.section is None
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, section=pd.NA)
lp.section = pd.NA
assert lp.section is None

# Single values
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, section=4)
lp.section = 4
assert np.isclose(lp.section.magnitude, 4)

lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, section=Q_(4, "cm**2"))
lp.section = Q_(4, "cm**2")
assert np.isclose(lp.section.magnitude, 400)

# Errors
with pytest.raises(RoseauLoadFlowException) as e:
LineParameters("test", z_line=z_line, y_shunt=y_shunt, section=-1.0)
lp.section = -1.0
assert e.value.msg == "Section must be positive: -1.0 mm² was provided."
assert e.value.code == RoseauLoadFlowExceptionCode.BAD_SECTIONS_VALUE

with pytest.raises(RoseauLoadFlowException) as e:
LineParameters("test", z_line=z_line, y_shunt=y_shunt, section=0)
lp.section = 0
assert e.value.msg == "Section must be positive: 0 mm² was provided."
assert e.value.code == RoseauLoadFlowExceptionCode.BAD_SECTIONS_VALUE

Expand All @@ -553,27 +553,26 @@ def test_ampacity():
# None-like values
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, ampacity=None)
assert lp.ampacity is None
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, ampacity=np.nan)
lp.ampacity = np.nan
assert lp.ampacity is None
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, ampacity=pd.NA)
lp.ampacity = pd.NA
assert lp.ampacity is None

# Single values
lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, ampacity=4)
lp.ampacity = 4
assert np.isclose(lp.ampacity.magnitude, 4)

lp = LineParameters("test", z_line=z_line, y_shunt=y_shunt, ampacity=Q_(4, "kA"))
lp.ampacity = Q_(4, "kA")
assert np.isclose(lp.ampacity.magnitude, 4000)

# Errors

with pytest.raises(RoseauLoadFlowException) as e:
LineParameters("test", z_line=z_line, y_shunt=y_shunt, ampacity=-2)
lp.ampacity = -2
assert e.value.msg == "Ampacity must be positive: -2 A was provided."
assert e.value.code == RoseauLoadFlowExceptionCode.BAD_AMPACITIES_VALUE

with pytest.raises(RoseauLoadFlowException) as e:
LineParameters("test", z_line=z_line, y_shunt=y_shunt, ampacity=0)
lp.ampacity = 0
assert e.value.msg == "Ampacity must be positive: 0 A was provided."
assert e.value.code == RoseauLoadFlowExceptionCode.BAD_AMPACITIES_VALUE

Expand Down
2 changes: 1 addition & 1 deletion roseau/load_flow_single/tests/test_electrical_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ def test_load_flow_results_frames(small_network_with_results):
assert_frame_equal(en.res_lines, expected_res_lines, rtol=1e-4, atol=1e-5)

# Lines with violated max current
en.lines["line"].parameters._ampacity = 0.002
en.lines["line"].parameters.ampacity = 0.002
expected_res_lines_violated_records = [
d | {"ampacity": 0.002, "violated": True, "loading": 4.3301594951117295} for d in expected_res_lines_records
]
Expand Down

0 comments on commit e710cee

Please sign in to comment.