diff --git a/doc/Changelog.md b/doc/Changelog.md index 5e921f97..0dade601 100644 --- a/doc/Changelog.md +++ b/doc/Changelog.md @@ -4,6 +4,8 @@ **In development** +* [PR113](https://github.com/RoseauTechnologies/Roseau_Load_Flow/pull/113) Raise an error when accessing the results of + disconnected elements. * [PR112](https://github.com/RoseauTechnologies/Roseau_Load_Flow/pull/112) Make the geometry serialization optional. * [PR106](https://github.com/RoseauTechnologies/Roseau_Load_Flow/pull/106) Improvements for non-euclidean projections. * [PR104](https://github.com/RoseauTechnologies/Roseau_Load_Flow/pull/104) Remove `roseau.load_flow.utils.BranchType` @@ -14,14 +16,14 @@ * [GH100](https://github.com/RoseauTechnologies/Roseau_Load_Flow/issues/100) Fix the `Yz` transformers * [PR97](https://github.com/RoseauTechnologies/Roseau_Load_Flow/pull/97) Add the model section to the documentation * [PR96](https://github.com/RoseauTechnologies/Roseau_Load_Flow/pull/96) - * Add single-phase transformer - * Add center-tapped transformer - * Remove the `TransformerType` enumeration + * Add single-phase transformer + * Add center-tapped transformer + * Remove the `TransformerType` enumeration * [PR93](https://github.com/RoseauTechnologies/Roseau_Load_Flow/pull/93) Add short circuit computation * [PR92](https://github.com/RoseauTechnologies/Roseau_Load_Flow/pull/92) - * Add the changelog in the documentation - * Use NodeJs 20 in the Dockerfile - * Correction of a dead link in the README + * Add the changelog in the documentation + * Use NodeJs 20 in the Dockerfile + * Correction of a dead link in the README ## Version 0.4.0 diff --git a/roseau/load_flow/models/loads/loads.py b/roseau/load_flow/models/loads/loads.py index 4b958496..e878f537 100644 --- a/roseau/load_flow/models/loads/loads.py +++ b/roseau/load_flow/models/loads/loads.py @@ -122,6 +122,7 @@ def _validate_value(self, value: Sequence[complex]) -> np.ndarray: return np.asarray(value, dtype=complex) def _res_potentials_getter(self, warning: bool) -> np.ndarray: + self._raise_disconnected_error() return self.bus._get_potentials_of(self.phases, warning) @property @@ -159,6 +160,13 @@ def disconnect(self) -> None: self._disconnect() self.bus = None + def _raise_disconnected_error(self) -> None: + """Raise an error if the load is disconnected.""" + if self.bus is None: + msg = f"The load {self.id!r} is disconnected and cannot be used anymore." + logger.error(msg) + raise RoseauLoadFlowException(msg=msg, code=RoseauLoadFlowExceptionCode.DISCONNECTED_ELEMENT) + # # Json Mixin interface # @@ -309,10 +317,7 @@ def res_flexible_powers(self) -> Q_[np.ndarray]: # Json Mixin interface # def to_dict(self, include_geometry: bool = True) -> JsonDict: - if self.bus is None: - msg = f"The load {self.id!r} is disconnected and cannot be used anymore." - logger.error(msg) - raise RoseauLoadFlowException(msg=msg, code=RoseauLoadFlowExceptionCode.DISCONNECTED_ELEMENT) + self._raise_disconnected_error() res = { "id": self.id, "bus": self.bus.id, @@ -384,10 +389,7 @@ def currents(self, value: Sequence[complex]) -> None: self._invalidate_network_results() def to_dict(self, include_geometry: bool = True) -> JsonDict: - if self.bus is None: - msg = f"The load {self.id!r} is disconnected and cannot be used anymore." - logger.error(msg) - raise RoseauLoadFlowException(msg=msg, code=RoseauLoadFlowExceptionCode.DISCONNECTED_ELEMENT) + self._raise_disconnected_error() return { "id": self.id, "bus": self.bus.id, @@ -442,10 +444,7 @@ def impedances(self, impedances: Sequence[complex]) -> None: self._invalidate_network_results() def to_dict(self, include_geometry: bool = True) -> JsonDict: - if self.bus is None: - msg = f"The load {self.id!r} is disconnected and cannot be used anymore." - logger.error(msg) - raise RoseauLoadFlowException(msg=msg, code=RoseauLoadFlowExceptionCode.DISCONNECTED_ELEMENT) + self._raise_disconnected_error() return { "id": self.id, "bus": self.bus.id, diff --git a/roseau/load_flow/models/sources.py b/roseau/load_flow/models/sources.py index 4ee826d2..5da3a7bd 100644 --- a/roseau/load_flow/models/sources.py +++ b/roseau/load_flow/models/sources.py @@ -118,6 +118,7 @@ def res_currents(self) -> Q_[np.ndarray]: return self._res_currents_getter(warning=True) def _res_potentials_getter(self, warning: bool) -> np.ndarray: + self._raise_disconnected_error() return self.bus._get_potentials_of(self.phases, warning) @property @@ -145,6 +146,13 @@ def disconnect(self) -> None: self._disconnect() self.bus = None + def _raise_disconnected_error(self) -> None: + """Raise an error if the voltage source is disconnected.""" + if self.bus is None: + msg = f"The voltage source {self.id!r} is disconnected and cannot be used anymore." + logger.error(msg) + raise RoseauLoadFlowException(msg=msg, code=RoseauLoadFlowExceptionCode.DISCONNECTED_ELEMENT) + # # Json Mixin interface # @@ -154,10 +162,7 @@ def from_dict(cls, data: JsonDict) -> Self: return cls(data["id"], data["bus"], voltages=voltages, phases=data["phases"]) def to_dict(self, include_geometry: bool = True) -> JsonDict: - if self.bus is None: - msg = f"The voltage source {self.id!r} is disconnected and cannot be used anymore." - logger.error(msg) - raise RoseauLoadFlowException(msg=msg, code=RoseauLoadFlowExceptionCode.DISCONNECTED_ELEMENT) + self._raise_disconnected_error() return { "id": self.id, "bus": self.bus.id,