Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class InvalidBranch3 : public PowerGridError {
class InvalidTransformerClock : public PowerGridError {
public:
InvalidTransformerClock(ID id, IntS clock) {
append_msg("Invalid clock for transformer " + std::to_string(id) + ", clock " + std::to_string(clock) + '\n');
append_msg("Invalid clock for transformer " + std::to_string(id) + ", clock " + std::to_string(clock) + '\n');
Comment thread
mgovers marked this conversation as resolved.
Resolved
}
};

Expand Down Expand Up @@ -137,13 +137,6 @@ class InvalidCalculationMethod : public CalculationError {
InvalidCalculationMethod() : CalculationError("The calculation method is invalid for this calculation!") {}
};

class UnknownAttributeName : public PowerGridError {
Comment thread
Jerry-Jinfeng-Guo marked this conversation as resolved.
Resolved
public:
explicit UnknownAttributeName(std::string const& attr_name) {
append_msg("Unknown attribute name!" + attr_name + "\n");
}
};

class InvalidShortCircuitType : public PowerGridError {
public:
explicit InvalidShortCircuitType(FaultType short_circuit_type) {
Expand All @@ -168,7 +161,7 @@ class InvalidShortCircuitPhases : public PowerGridError {
class InvalidShortCircuitPhaseOrType : public PowerGridError {
public:
InvalidShortCircuitPhaseOrType() {
append_msg("During one calculation the short circuit types phases should be similar for all faults \n");
append_msg("During one calculation the short circuit types phases should be similar for all faults\n");
}
};

Expand Down
93 changes: 84 additions & 9 deletions src/power_grid_model/core/error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,32 @@
Error handling
"""

import re
from typing import Optional

import numpy as np

from power_grid_model.core.index_integer import IdxNp
from power_grid_model.core.power_grid_core import power_grid_core as pgc
from power_grid_model.errors import PowerGridBatchError, PowerGridError, PowerGridSerializationError
from power_grid_model.errors import (
ConflictID,
ConflictVoltage,
IDNotFound,
IDWrongType,
InvalidBranch,
InvalidBranch3,
InvalidCalculationMethod,
InvalidMeasuredObject,
InvalidShortCircuitPhaseOrType,
InvalidTransformerClock,
IterationDiverge,
MissingCaseForEnumError,
NotObservableError,
PowerGridBatchError,
PowerGridError,
PowerGridSerializationError,
SparseMatrixError,
)

VALIDATOR_MSG = "\nTry validate_input_data() or validate_batch_data() to validate your data.\n"
# error codes
Expand All @@ -21,13 +40,61 @@
PGM_BATCH_ERROR = 2
PGM_SERIALIZATION_ERROR = 3


def find_error(batch_size: int = 1) -> Optional[RuntimeError]:
_MISSING_CASE_FOR_ENUM_RE = re.compile(r"(\w+) is not implemented for (\w+) #(-?\d+)!\n")
_CONFLICT_VOLTAGE_RE = re.compile(
r"Conflicting voltage for line (-?\d+)\n voltage at from node (-?\d+) is (.*)\n"
r" voltage at to node (-?\d+) is (.*)\n"
)
_INVALID_BRANCH_RE = re.compile(r"Branch (-?\d+) has the same from- and to-node (-?\d+),\n This is not allowed!\n")
_INVALID_BRANCH3_RE = re.compile(
r"Branch3 (-?\d+) is connected to the same node at least twice. Node 1\/2\/3: (-?\d+)\/(-?\d+)\/(-?\d+),\n"
r" This is not allowed!\n"
)
_INVALID_TRANSFORMER_CLOCK_RE = re.compile(r"Invalid clock for transformer (-?\d+), clock (-?\d+)\n")
_SPARSE_MATRIX_ERROR_RE = re.compile(r"Sparse matrix error") # multiple different flavors
_NOT_OBSERVABLE_ERROR_RE = re.compile(r"Not enough measurements available for state estimation.\n")
_ITERATION_DIVERGE_RE = re.compile(r"Iteration failed to converge") # potentially multiple different flavors
_CONFLICT_ID_RE = re.compile(r"Conflicting id detected: (-?\d+)\n")
_ID_NOT_FOUND_RE = re.compile(r"The id cannot be found: (-?\d+)\n")
_INVALID_MEASURED_OBJECT_RE = re.compile(r"(\w+) is not supported for (\w+)")
_ID_WRONG_TYPE_RE = re.compile(r"Wrong type for object with id (-?\d+)\n")
_INVALID_CALCULATION_METHOD_RE = re.compile(r"The calculation method is invalid for this calculation!")
_INVALID_SHORT_CIRCUIT_PHASE_OR_TYPE_RE = re.compile(r"short circuit type") # multiple different flavors

_ERROR_MESSAGE_PATTERNS = {
_MISSING_CASE_FOR_ENUM_RE: MissingCaseForEnumError,
_CONFLICT_VOLTAGE_RE: ConflictVoltage,
_INVALID_BRANCH_RE: InvalidBranch,
_INVALID_BRANCH3_RE: InvalidBranch3,
_INVALID_TRANSFORMER_CLOCK_RE: InvalidTransformerClock,
_SPARSE_MATRIX_ERROR_RE: SparseMatrixError,
_NOT_OBSERVABLE_ERROR_RE: NotObservableError,
_ITERATION_DIVERGE_RE: IterationDiverge,
_CONFLICT_ID_RE: ConflictID,
_ID_NOT_FOUND_RE: IDNotFound,
_INVALID_MEASURED_OBJECT_RE: InvalidMeasuredObject,
_ID_WRONG_TYPE_RE: IDWrongType,
_INVALID_CALCULATION_METHOD_RE: InvalidCalculationMethod,
_INVALID_SHORT_CIRCUIT_PHASE_OR_TYPE_RE: InvalidShortCircuitPhaseOrType,
}


def _interpret_error(message: str, decode_error: bool = True) -> PowerGridError:
if decode_error:
for pattern, type_ in _ERROR_MESSAGE_PATTERNS.items():
if pattern.search(message) is not None:
return type_(message)

return PowerGridError(message)


def find_error(batch_size: int = 1, decode_error: bool = True) -> Optional[RuntimeError]:
"""
Check if there is an error and return it

Args:
batch_size: Size of batch
batch_size: (int, optional): Size of batch. Defaults to 1.
decode_error (bool, optional): Decode the error message(s) to derived error classes. Defaults to True

Returns: error object, can be none

Expand All @@ -38,7 +105,7 @@ def find_error(batch_size: int = 1) -> Optional[RuntimeError]:
if error_code == PGM_REGULAR_ERROR:
error_message = pgc.error_message()
error_message += VALIDATOR_MSG
return PowerGridError(error_message)
return _interpret_error(error_message, decode_error=decode_error)
if error_code == PGM_BATCH_ERROR:
error_message = "There are errors in the batch calculation." + VALIDATOR_MSG
error = PowerGridBatchError(error_message)
Expand All @@ -47,6 +114,7 @@ def find_error(batch_size: int = 1) -> Optional[RuntimeError]:
failed_msgptr = pgc.batch_errors()
error.failed_scenarios = np.ctypeslib.as_array(failed_idxptr, shape=(n_fails,)).copy()
error.error_messages = [failed_msgptr[i].decode() for i in range(n_fails)] # type: ignore
error.errors = [_interpret_error(message, decode_error=decode_error) for message in error.error_messages]
all_scenarios = np.arange(batch_size, dtype=IdxNp)
mask = np.ones(batch_size, dtype=np.bool_)
mask[error.failed_scenarios] = False
Expand All @@ -57,26 +125,33 @@ def find_error(batch_size: int = 1) -> Optional[RuntimeError]:
return RuntimeError("Unknown error!")


def assert_no_error(batch_size: int = 1):
def assert_no_error(batch_size: int = 1, decode_error: bool = True):
"""
Assert there is no error in the last operation
If there is an error, raise it

Args:
batch_size (int, optional): Size of batch. Defaults to 1.
decode_error (bool, optional): Decode the error message(s) to derived error classes. Defaults to True

Returns:

"""
error = find_error(batch_size=batch_size)
error = find_error(batch_size=batch_size, decode_error=decode_error)
if error is not None:
raise error


def handle_errors(continue_on_batch_error: bool, batch_size: int = 1) -> Optional[PowerGridBatchError]:
def handle_errors(
continue_on_batch_error: bool, batch_size: int = 1, decode_error: bool = True
) -> Optional[PowerGridBatchError]:
"""
Handle any errors in the way that is specified.

Args:
continue_on_batch_error (bool): Return the error when the error type is a batch error instead of reraising it.
batch_size (int, optional): Size of batch. Defaults to 1.
decode_error (bool, optional): Decode the error message(s) to derived error classes. Defaults to True

Raises:
error: Any errors previously encountered, unless it was a batch error and continue_on_batch_error was True.
Expand All @@ -85,7 +160,7 @@ def handle_errors(continue_on_batch_error: bool, batch_size: int = 1) -> Optiona
Optional[PowerGridBatchError]: None if there were no errors, or the previously encountered
error if it was a batch error and continue_on_batch_error was True.
"""
error: Optional[RuntimeError] = find_error(batch_size=batch_size)
error: Optional[RuntimeError] = find_error(batch_size=batch_size, decode_error=decode_error)
if error is None:
return None

Expand Down
36 changes: 30 additions & 6 deletions src/power_grid_model/core/power_grid_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,10 @@ def as_enum_value(key_enum: str, type_: Type[IntEnum]):
setattr(opt, key, value.value if isinstance(value, IntEnum) else value)
return opt

def _handle_errors(self, continue_on_batch_error: bool, batch_size: int):
self._batch_error = handle_errors(continue_on_batch_error=continue_on_batch_error, batch_size=batch_size)
def _handle_errors(self, continue_on_batch_error: bool, batch_size: int, decode_error: bool):
self._batch_error = handle_errors(
continue_on_batch_error=continue_on_batch_error, batch_size=batch_size, decode_error=decode_error
)

# pylint: disable=too-many-arguments
def _calculate_impl(
Expand All @@ -212,6 +214,7 @@ def _calculate_impl(
output_component_types: Optional[Union[Set[str], List[str]]],
options: Options,
continue_on_batch_error: bool,
decode_error: bool,
):
"""
Core calculation routine
Expand All @@ -223,6 +226,7 @@ def _calculate_impl(
output_component_types:
options:
continue_on_batch_error:
decode_error:

Returns:
"""
Expand Down Expand Up @@ -258,7 +262,9 @@ def _calculate_impl(
update_data=update_ptr,
)

self._handle_errors(continue_on_batch_error=continue_on_batch_error, batch_size=batch_size)
self._handle_errors(
continue_on_batch_error=continue_on_batch_error, batch_size=batch_size, decode_error=decode_error
)

return output_data

Expand All @@ -273,6 +279,7 @@ def _calculate_power_flow(
threading: int = -1,
output_component_types: Optional[Union[Set[str], List[str]]] = None,
continue_on_batch_error: bool = False,
decode_error: bool = True,
experimental_features: Union[_ExperimentalFeatures, str] = _ExperimentalFeatures.disabled,
):
calculation_type = CalculationType.power_flow
Expand All @@ -292,6 +299,7 @@ def _calculate_power_flow(
output_component_types=output_component_types,
options=options,
continue_on_batch_error=continue_on_batch_error,
decode_error=decode_error,
)

def _calculate_state_estimation(
Expand All @@ -305,6 +313,7 @@ def _calculate_state_estimation(
threading: int = -1,
output_component_types: Optional[Union[Set[str], List[str]]] = None,
continue_on_batch_error: bool = False,
decode_error: bool = True,
experimental_features: Union[_ExperimentalFeatures, str] = _ExperimentalFeatures.disabled,
) -> Dict[str, np.ndarray]:
calculation_type = CalculationType.state_estimation
Expand All @@ -324,6 +333,7 @@ def _calculate_state_estimation(
output_component_types=output_component_types,
options=options,
continue_on_batch_error=continue_on_batch_error,
decode_error=decode_error,
)

def _calculate_short_circuit(
Expand All @@ -334,6 +344,7 @@ def _calculate_short_circuit(
threading: int = -1,
output_component_types: Optional[Union[Set[str], List[str]]] = None,
continue_on_batch_error: bool = False,
decode_error: bool = True,
short_circuit_voltage_scaling: Union[ShortCircuitVoltageScaling, str] = ShortCircuitVoltageScaling.maximum,
Comment thread
Jerry-Jinfeng-Guo marked this conversation as resolved.
Resolved
experimental_features: Union[_ExperimentalFeatures, str] = _ExperimentalFeatures.disabled,
) -> Dict[str, np.ndarray]:
Expand All @@ -355,6 +366,7 @@ def _calculate_short_circuit(
output_component_types=output_component_types,
options=options,
continue_on_batch_error=continue_on_batch_error,
decode_error=decode_error,
)

def calculate_power_flow(
Expand All @@ -368,6 +380,7 @@ def calculate_power_flow(
threading: int = -1,
output_component_types: Optional[Union[Set[str], List[str]]] = None,
continue_on_batch_error: bool = False,
decode_error: bool = True,
) -> Dict[str, np.ndarray]:
"""
Calculate power flow once with the current model attributes.
Expand Down Expand Up @@ -411,8 +424,10 @@ def calculate_power_flow(
- > 0: Specify number of parallel threads
output_component_types ({set, list}, optional): List or set of component types you want to be present in
the output dict. By default, all component types will be in the output.
continue_on_batch_error (bool, optional): If the program continues (instead of throwing error) if some
continue_on_batch_error (bool, optional): Continue the program (instead of throwing error) if some
scenarios fail.
decode_error (bool, optional):
Decode error messages to their derived types if possible.

Returns:
Dictionary of results of all components.
Expand All @@ -438,6 +453,7 @@ def calculate_power_flow(
threading=threading,
output_component_types=output_component_types,
continue_on_batch_error=continue_on_batch_error,
decode_error=decode_error,
)

def calculate_state_estimation(
Expand All @@ -451,6 +467,7 @@ def calculate_state_estimation(
threading: int = -1,
output_component_types: Optional[Union[Set[str], List[str]]] = None,
continue_on_batch_error: bool = False,
decode_error: bool = True,
) -> Dict[str, np.ndarray]:
"""
Calculate state estimation once with the current model attributes.
Expand Down Expand Up @@ -491,8 +508,10 @@ def calculate_state_estimation(
- > 0: Specify number of parallel threads
output_component_types ({set, list}, optional): List or set of component types you want to be present in
the output dict. By default, all component types will be in the output.
continue_on_batch_error (bool, optional): If the program continues (instead of throwing error) if some
continue_on_batch_error (bool, optional): Continue the program (instead of throwing error) if some
scenarios fail.
decode_error (bool, optional):
Decode error messages to their derived types if possible.

Returns:
Dictionary of results of all components.
Expand All @@ -518,6 +537,7 @@ def calculate_state_estimation(
threading=threading,
output_component_types=output_component_types,
continue_on_batch_error=continue_on_batch_error,
decode_error=decode_error,
)

def calculate_short_circuit(
Expand All @@ -528,6 +548,7 @@ def calculate_short_circuit(
threading: int = -1,
output_component_types: Optional[Union[Set[str], List[str]]] = None,
continue_on_batch_error: bool = False,
decode_error: bool = True,
short_circuit_voltage_scaling: Union[ShortCircuitVoltageScaling, str] = ShortCircuitVoltageScaling.maximum,
) -> Dict[str, np.ndarray]:
"""
Expand Down Expand Up @@ -563,7 +584,9 @@ def calculate_short_circuit(
List or set of component types you want to be present in the output dict.
By default, all component types will be in the output.
continue_on_batch_error (bool, optional):
If the program continues (instead of throwing error) if some scenarios fail.
Continue the program (instead of throwing error) if some scenarios fail.
decode_error (bool, optional):
Decode error messages to their derived types if possible.
short_circuit_voltage_scaling ({ShortCircuitVoltageSaling, str}, optional):
Whether to use the maximum or minimum voltage scaling.
By default, the maximum voltage scaling is used to calculate the short circuit.
Expand All @@ -588,6 +611,7 @@ def calculate_short_circuit(
threading=threading,
output_component_types=output_component_types,
continue_on_batch_error=continue_on_batch_error,
decode_error=decode_error,
short_circuit_voltage_scaling=short_circuit_voltage_scaling,
)

Expand Down
Loading