power-grid-model 1.10.105__py3-none-win_amd64.whl → 1.12.72__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of power-grid-model might be problematic. Click here for more details.

Files changed (67) hide show
  1. power_grid_model/__init__.py +54 -29
  2. power_grid_model/_core/__init__.py +3 -3
  3. power_grid_model/_core/buffer_handling.py +493 -478
  4. power_grid_model/_core/data_handling.py +195 -141
  5. power_grid_model/_core/data_types.py +143 -0
  6. power_grid_model/_core/dataset_definitions.py +109 -112
  7. power_grid_model/_core/enum.py +226 -0
  8. power_grid_model/_core/error_handling.py +206 -205
  9. power_grid_model/_core/errors.py +130 -0
  10. power_grid_model/_core/index_integer.py +17 -17
  11. power_grid_model/_core/options.py +71 -69
  12. power_grid_model/_core/power_grid_core.py +563 -597
  13. power_grid_model/_core/power_grid_dataset.py +535 -528
  14. power_grid_model/_core/power_grid_meta.py +257 -244
  15. power_grid_model/_core/power_grid_model.py +969 -692
  16. power_grid_model/_core/power_grid_model_c/__init__.py +3 -0
  17. power_grid_model/_core/power_grid_model_c/bin/power_grid_model_c.dll +0 -0
  18. power_grid_model/_core/power_grid_model_c/get_pgm_dll_path.py +63 -0
  19. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/basics.h +255 -0
  20. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/buffer.h +108 -0
  21. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/dataset.h +316 -0
  22. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/dataset_definitions.h +1052 -0
  23. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/handle.h +99 -0
  24. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/meta_data.h +189 -0
  25. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/model.h +125 -0
  26. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/options.h +142 -0
  27. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c/serialization.h +118 -0
  28. power_grid_model/_core/power_grid_model_c/include/power_grid_model_c.h +36 -0
  29. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/basics.hpp +65 -0
  30. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/buffer.hpp +61 -0
  31. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/dataset.hpp +220 -0
  32. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/handle.hpp +108 -0
  33. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/meta_data.hpp +84 -0
  34. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/model.hpp +63 -0
  35. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/options.hpp +52 -0
  36. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/serialization.hpp +124 -0
  37. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp/utils.hpp +81 -0
  38. power_grid_model/_core/power_grid_model_c/include/power_grid_model_cpp.hpp +19 -0
  39. power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelConfig.cmake +37 -0
  40. power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelConfigVersion.cmake +65 -0
  41. power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelTargets-release.cmake +19 -0
  42. power_grid_model/_core/power_grid_model_c/lib/cmake/power_grid_model/power_grid_modelTargets.cmake +144 -0
  43. power_grid_model/_core/power_grid_model_c/lib/power_grid_model_c.lib +0 -0
  44. power_grid_model/_core/power_grid_model_c/share/LICENSE +292 -0
  45. power_grid_model/_core/power_grid_model_c/share/README.md +15 -0
  46. power_grid_model/_core/serialization.py +317 -317
  47. power_grid_model/_core/typing.py +20 -0
  48. power_grid_model/{_utils.py → _core/utils.py} +798 -783
  49. power_grid_model/data_types.py +321 -319
  50. power_grid_model/enum.py +27 -227
  51. power_grid_model/errors.py +37 -127
  52. power_grid_model/typing.py +43 -48
  53. power_grid_model/utils.py +473 -400
  54. power_grid_model/validation/__init__.py +25 -14
  55. power_grid_model/validation/_rules.py +1171 -999
  56. power_grid_model/validation/_validation.py +1172 -1020
  57. power_grid_model/validation/assertions.py +93 -92
  58. power_grid_model/validation/errors.py +602 -520
  59. power_grid_model/validation/utils.py +313 -318
  60. {power_grid_model-1.10.105.dist-info → power_grid_model-1.12.72.dist-info}/METADATA +180 -167
  61. power_grid_model-1.12.72.dist-info/RECORD +65 -0
  62. {power_grid_model-1.10.105.dist-info → power_grid_model-1.12.72.dist-info}/WHEEL +1 -1
  63. power_grid_model-1.12.72.dist-info/entry_points.txt +3 -0
  64. power_grid_model/_core/_power_grid_core.dll +0 -0
  65. power_grid_model-1.10.105.dist-info/RECORD +0 -32
  66. power_grid_model-1.10.105.dist-info/top_level.txt +0 -1
  67. {power_grid_model-1.10.105.dist-info → power_grid_model-1.12.72.dist-info}/licenses/LICENSE +0 -0
@@ -1,692 +1,969 @@
1
- # SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2
- #
3
- # SPDX-License-Identifier: MPL-2.0
4
-
5
- """
6
- Main power grid model class
7
- """
8
-
9
- from enum import IntEnum
10
- from typing import Type
11
-
12
- import numpy as np
13
-
14
- from power_grid_model._core.data_handling import (
15
- create_output_data,
16
- get_output_type,
17
- prepare_input_view,
18
- prepare_output_view,
19
- prepare_update_view,
20
- )
21
- from power_grid_model._core.dataset_definitions import (
22
- ComponentType,
23
- ComponentTypeLike,
24
- _map_to_component_types,
25
- _str_to_component_type,
26
- )
27
- from power_grid_model._core.error_handling import PowerGridBatchError, assert_no_error, handle_errors
28
- from power_grid_model._core.index_integer import IdNp, IdxNp
29
- from power_grid_model._core.options import Options
30
- from power_grid_model._core.power_grid_core import ConstDatasetPtr, IDPtr, IdxPtr, ModelPtr, power_grid_core as pgc
31
- from power_grid_model.data_types import Dataset, SingleDataset
32
- from power_grid_model.enum import (
33
- CalculationMethod,
34
- CalculationType,
35
- ShortCircuitVoltageScaling,
36
- TapChangingStrategy,
37
- _ExperimentalFeatures,
38
- )
39
- from power_grid_model.typing import ComponentAttributeMapping
40
-
41
-
42
- class PowerGridModel:
43
- """
44
- Main class for Power Grid Model
45
- """
46
-
47
- _model_ptr: ModelPtr
48
- _all_component_count: dict[ComponentType, int] | None
49
- _batch_error: PowerGridBatchError | None
50
-
51
- @property
52
- def batch_error(self) -> PowerGridBatchError | None:
53
- """
54
- Get the batch error object, if present, after a batch calculation with errors.
55
-
56
- Also works when continue_on_batch_error was set to True during the calculation.
57
-
58
- Returns:
59
- Batch error object, or None
60
- """
61
- return self._batch_error
62
-
63
- @property
64
- def _model(self):
65
- if not self._model_ptr:
66
- raise TypeError("You have an empty instance of PowerGridModel!")
67
- return self._model_ptr
68
-
69
- @property
70
- def all_component_count(self) -> dict[ComponentType, int]:
71
- """
72
- Get amount of elements per component type.
73
- If the count for a component type is zero, it will not be in the returned dictionary.
74
-
75
- Returns:
76
- A dictionary with
77
-
78
- - key: Component type name
79
- - value: Integer count of elements of this type
80
- """
81
- if self._all_component_count is None:
82
- raise TypeError("You have an empty instance of PowerGridModel!")
83
- return self._all_component_count
84
-
85
- def copy(self) -> "PowerGridModel":
86
- """
87
- Copy the current model
88
-
89
- Returns:
90
- A copy of PowerGridModel
91
- """
92
- new_model = PowerGridModel.__new__(PowerGridModel)
93
- new_model._model_ptr = pgc.copy_model(self._model) # pylint: disable=W0212
94
- assert_no_error()
95
- new_model._all_component_count = self._all_component_count # pylint: disable=W0212
96
- return new_model
97
-
98
- def __copy__(self):
99
- return self.copy()
100
-
101
- def __new__(cls, *_args, **_kwargs):
102
- instance = super().__new__(cls)
103
- instance._model_ptr = ModelPtr()
104
- instance._all_component_count = None
105
- return instance
106
-
107
- def __init__(self, input_data: SingleDataset, system_frequency: float = 50.0):
108
- """
109
- Initialize the model from an input data set.
110
-
111
- Args:
112
- input_data: Input data dictionary
113
-
114
- - key: Component type
115
- - value: Component data with the correct type :class:`SingleComponentData`
116
-
117
- system_frequency: Frequency of the power system, default 50 Hz
118
- """
119
- # destroy old instance
120
- pgc.destroy_model(self._model_ptr)
121
- self._all_component_count = None
122
- # create new
123
- prepared_input = prepare_input_view(_map_to_component_types(input_data))
124
- self._model_ptr = pgc.create_model(system_frequency, input_data=prepared_input.get_dataset_ptr())
125
- assert_no_error()
126
- self._all_component_count = {k: v for k, v in prepared_input.get_info().total_elements().items() if v > 0}
127
-
128
- def update(self, *, update_data: Dataset):
129
- """
130
- Update the model with changes.
131
-
132
- The model will be in an invalid state if the update fails and should be discarded.
133
-
134
- Args:
135
- update_data: Update data dictionary
136
-
137
- - key: Component type
138
- - value: Component data with the correct type :class:`ComponentData` (single scenario or batch)
139
-
140
- Raises:
141
- PowerGridError if the update fails. The model is left in an invalid state and should be discarded.
142
-
143
- Returns:
144
- None
145
- """
146
- prepared_update = prepare_update_view(_map_to_component_types(update_data))
147
- pgc.update_model(self._model, prepared_update.get_dataset_ptr())
148
- assert_no_error()
149
-
150
- def get_indexer(self, component_type: ComponentTypeLike, ids: np.ndarray):
151
- """
152
- Get array of indexers given array of ids for component type.
153
-
154
- This enables syntax like input_data[ComponentType.node][get_indexer(ids)]
155
-
156
- Args:
157
- component_type: Type of component
158
- ids: Array of ids
159
-
160
- Returns:
161
- Array of indexers, same shape as input array ids
162
- """
163
- component_type = _str_to_component_type(component_type)
164
- ids_c = np.ascontiguousarray(ids, dtype=IdNp).ctypes.data_as(IDPtr)
165
- indexer = np.empty_like(ids, dtype=IdxNp, order="C")
166
- indexer_c = indexer.ctypes.data_as(IdxPtr)
167
- size = ids.size
168
- # call c function
169
- pgc.get_indexer(self._model, component_type, size, ids_c, indexer_c)
170
- assert_no_error()
171
- return indexer
172
-
173
- def _get_output_component_count(self, calculation_type: CalculationType):
174
- exclude_types = {
175
- CalculationType.power_flow: [
176
- ComponentType.sym_voltage_sensor,
177
- ComponentType.asym_voltage_sensor,
178
- ComponentType.sym_power_sensor,
179
- ComponentType.asym_power_sensor,
180
- ComponentType.fault,
181
- ],
182
- CalculationType.state_estimation: [ComponentType.fault],
183
- CalculationType.short_circuit: [
184
- ComponentType.sym_voltage_sensor,
185
- ComponentType.asym_voltage_sensor,
186
- ComponentType.sym_power_sensor,
187
- ComponentType.asym_power_sensor,
188
- ],
189
- }.get(calculation_type, [])
190
-
191
- def include_type(component_type: ComponentType):
192
- for exclude_type in exclude_types:
193
- if exclude_type.value in component_type.value:
194
- return False
195
- return True
196
-
197
- return {ComponentType[k]: v for k, v in self.all_component_count.items() if include_type(k)}
198
-
199
- # pylint: disable=too-many-arguments
200
- def _construct_output( # pylint: disable=too-many-positional-arguments
201
- self,
202
- output_component_types: ComponentAttributeMapping,
203
- calculation_type: CalculationType,
204
- symmetric: bool,
205
- is_batch: bool,
206
- batch_size: int,
207
- ) -> dict[ComponentType, np.ndarray]:
208
- all_component_count = self._get_output_component_count(calculation_type=calculation_type)
209
- return create_output_data(
210
- output_component_types=output_component_types,
211
- output_type=get_output_type(calculation_type=calculation_type, symmetric=symmetric),
212
- all_component_count=all_component_count,
213
- is_batch=is_batch,
214
- batch_size=batch_size,
215
- )
216
-
217
- @staticmethod
218
- def _options(**kwargs) -> Options:
219
- def as_enum_value(key_enum: str, type_: Type[IntEnum]):
220
- if key_enum in kwargs:
221
- value_enum = kwargs[key_enum]
222
- if isinstance(value_enum, str):
223
- kwargs[key_enum] = type_[value_enum]
224
-
225
- as_enum_value("calculation_method", CalculationMethod)
226
- as_enum_value("tap_changing_strategy", TapChangingStrategy)
227
- as_enum_value("short_circuit_voltage_scaling", ShortCircuitVoltageScaling)
228
- as_enum_value("experimental_features", _ExperimentalFeatures)
229
-
230
- opt = Options()
231
- for key, value in kwargs.items():
232
- setattr(opt, key, value.value if isinstance(value, IntEnum) else value)
233
- return opt
234
-
235
- def _handle_errors(self, continue_on_batch_error: bool, batch_size: int, decode_error: bool):
236
- self._batch_error = handle_errors(
237
- continue_on_batch_error=continue_on_batch_error,
238
- batch_size=batch_size,
239
- decode_error=decode_error,
240
- )
241
-
242
- # pylint: disable=too-many-arguments
243
- def _calculate_impl( # pylint: disable=too-many-positional-arguments
244
- self,
245
- calculation_type: CalculationType,
246
- symmetric: bool,
247
- update_data: Dataset | None,
248
- output_component_types: ComponentAttributeMapping,
249
- options: Options,
250
- continue_on_batch_error: bool,
251
- decode_error: bool,
252
- experimental_features: _ExperimentalFeatures | str, # pylint: disable=too-many-arguments,unused-argument
253
- ):
254
- """
255
- Core calculation routine
256
-
257
- Args:
258
- calculation_type:
259
- symmetric:
260
- update_data:
261
- output_component_types:
262
- options:
263
- continue_on_batch_error:
264
- decode_error:
265
-
266
- Returns:
267
- """
268
- self._batch_error = None
269
- is_batch = update_data is not None
270
-
271
- if update_data is not None:
272
- prepared_update = prepare_update_view(update_data)
273
- update_ptr = prepared_update.get_dataset_ptr()
274
- batch_size = prepared_update.get_info().batch_size()
275
- else:
276
- update_ptr = ConstDatasetPtr()
277
- batch_size = 1
278
-
279
- output_data = self._construct_output(
280
- output_component_types=output_component_types,
281
- calculation_type=calculation_type,
282
- symmetric=symmetric,
283
- is_batch=is_batch,
284
- batch_size=batch_size,
285
- )
286
- prepared_result = prepare_output_view(
287
- output_data=output_data,
288
- output_type=get_output_type(calculation_type=calculation_type, symmetric=symmetric),
289
- )
290
-
291
- # run calculation
292
- pgc.calculate(
293
- # model and options
294
- self._model,
295
- options.opt,
296
- output_data=prepared_result.get_dataset_ptr(),
297
- update_data=update_ptr,
298
- )
299
-
300
- self._handle_errors(
301
- continue_on_batch_error=continue_on_batch_error,
302
- batch_size=batch_size,
303
- decode_error=decode_error,
304
- )
305
-
306
- return output_data
307
-
308
- def _calculate_power_flow(
309
- self,
310
- *,
311
- symmetric: bool = True,
312
- error_tolerance: float = 1e-8,
313
- max_iterations: int = 20,
314
- calculation_method: CalculationMethod | str = CalculationMethod.newton_raphson,
315
- update_data: Dataset | None = None,
316
- threading: int = -1,
317
- output_component_types: ComponentAttributeMapping = None,
318
- continue_on_batch_error: bool = False,
319
- decode_error: bool = True,
320
- tap_changing_strategy: TapChangingStrategy | str = TapChangingStrategy.disabled,
321
- experimental_features: _ExperimentalFeatures | str = _ExperimentalFeatures.disabled,
322
- ):
323
- calculation_type = CalculationType.power_flow
324
- options = self._options(
325
- calculation_type=calculation_type,
326
- symmetric=symmetric,
327
- error_tolerance=error_tolerance,
328
- max_iterations=max_iterations,
329
- calculation_method=calculation_method,
330
- tap_changing_strategy=tap_changing_strategy,
331
- threading=threading,
332
- experimental_features=experimental_features,
333
- )
334
- return self._calculate_impl(
335
- calculation_type=calculation_type,
336
- symmetric=symmetric,
337
- update_data=update_data,
338
- output_component_types=output_component_types,
339
- options=options,
340
- continue_on_batch_error=continue_on_batch_error,
341
- decode_error=decode_error,
342
- experimental_features=experimental_features,
343
- )
344
-
345
- def _calculate_state_estimation(
346
- self,
347
- *,
348
- symmetric: bool = True,
349
- error_tolerance: float = 1e-8,
350
- max_iterations: int = 20,
351
- calculation_method: CalculationMethod | str = CalculationMethod.iterative_linear,
352
- update_data: Dataset | None = None,
353
- threading: int = -1,
354
- output_component_types: ComponentAttributeMapping = None,
355
- continue_on_batch_error: bool = False,
356
- decode_error: bool = True,
357
- experimental_features: _ExperimentalFeatures | str = _ExperimentalFeatures.disabled,
358
- ) -> dict[ComponentType, np.ndarray]:
359
- calculation_type = CalculationType.state_estimation
360
- options = self._options(
361
- calculation_type=calculation_type,
362
- symmetric=symmetric,
363
- error_tolerance=error_tolerance,
364
- max_iterations=max_iterations,
365
- calculation_method=calculation_method,
366
- threading=threading,
367
- experimental_features=experimental_features,
368
- )
369
- return self._calculate_impl(
370
- calculation_type=calculation_type,
371
- symmetric=symmetric,
372
- update_data=update_data,
373
- output_component_types=output_component_types,
374
- options=options,
375
- continue_on_batch_error=continue_on_batch_error,
376
- decode_error=decode_error,
377
- experimental_features=experimental_features,
378
- )
379
-
380
- def _calculate_short_circuit(
381
- self,
382
- *,
383
- calculation_method: CalculationMethod | str = CalculationMethod.iec60909,
384
- update_data: Dataset | None = None,
385
- threading: int = -1,
386
- output_component_types: ComponentAttributeMapping = None,
387
- continue_on_batch_error: bool = False,
388
- decode_error: bool = True,
389
- short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str = ShortCircuitVoltageScaling.maximum,
390
- experimental_features: _ExperimentalFeatures | str = _ExperimentalFeatures.disabled,
391
- ) -> dict[ComponentType, np.ndarray]:
392
- calculation_type = CalculationType.short_circuit
393
- symmetric = False
394
-
395
- options = self._options(
396
- calculation_type=calculation_type,
397
- symmetric=symmetric,
398
- calculation_method=calculation_method,
399
- threading=threading,
400
- short_circuit_voltage_scaling=short_circuit_voltage_scaling,
401
- experimental_features=experimental_features,
402
- )
403
- return self._calculate_impl(
404
- calculation_type=calculation_type,
405
- symmetric=symmetric,
406
- update_data=update_data,
407
- output_component_types=output_component_types,
408
- options=options,
409
- continue_on_batch_error=continue_on_batch_error,
410
- decode_error=decode_error,
411
- experimental_features=experimental_features,
412
- )
413
-
414
- def calculate_power_flow(
415
- self,
416
- *,
417
- symmetric: bool = True,
418
- error_tolerance: float = 1e-8,
419
- max_iterations: int = 20,
420
- calculation_method: CalculationMethod | str = CalculationMethod.newton_raphson,
421
- update_data: dict[str, np.ndarray | dict[str, np.ndarray]] | Dataset | None = None,
422
- threading: int = -1,
423
- output_component_types: ComponentAttributeMapping = None,
424
- continue_on_batch_error: bool = False,
425
- decode_error: bool = True,
426
- tap_changing_strategy: TapChangingStrategy | str = TapChangingStrategy.disabled,
427
- ) -> dict[ComponentType, np.ndarray]:
428
- """
429
- Calculate power flow once with the current model attributes.
430
- Or calculate in batch with the given update dataset in batch.
431
-
432
- Args:
433
- symmetric (bool, optional): Whether to perform a three-phase symmetric calculation.
434
-
435
- - True: Three-phase symmetric calculation, even for asymmetric loads/generations (Default).
436
- - False: Three-phase asymmetric calculation.
437
- error_tolerance (float, optional): Error tolerance for voltage in p.u., applicable only when the
438
- calculation method is iterative.
439
- max_iterations (int, optional): Maximum number of iterations, applicable only when the calculation method
440
- is iterative.
441
- calculation_method (an enumeration or string): The calculation method to use.
442
-
443
- - newton_raphson: Use Newton-Raphson iterative method (default).
444
- - linear: Use linear method.
445
- update_data (dict, optional):
446
- None: Calculate power flow once with the current model attributes.
447
- Or a dictionary for batch calculation with batch update.
448
-
449
- - key: Component type name to be updated in batch.
450
- - value:
451
-
452
- - For homogeneous update batch (a 2D numpy structured array):
453
-
454
- - Dimension 0: Each batch.
455
- - Dimension 1: Each updated element per batch for this component type.
456
- - For inhomogeneous update batch (a dictionary containing two keys):
457
-
458
- - indptr: A 1D numpy int64 array with length n_batch + 1. Given batch number k, the
459
- update array for this batch is data[indptr[k]:indptr[k + 1]]. This is the concept of
460
- compressed sparse structure.
461
- https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
462
- - data: 1D numpy structured array in flat.
463
- threading (int, optional): Applicable only for batch calculation.
464
-
465
- - < 0: Sequential
466
- - = 0: Parallel, use number of hardware threads
467
- - > 0: Specify number of parallel threads
468
- output_component_types (ComponentAttributeMapping):
469
-
470
- - None: Row based data for all component types.
471
- - set[ComponentTypeVar] or list[ComponentTypeVar]: Row based data for the specified component types.
472
- - ComponentAttributeFilterOptions: Columnar data for all component types.
473
- - dict[ComponentType, set[str] | list[str] | None | ComponentAttributeFilterOptions]:
474
- key: ComponentType
475
- value:
476
- - None: Row based data for the specified component types.
477
- - ComponentAttributeFilterOptions: Columnar data for the specified component types.
478
- - set[str] | list[str]: Columnar data for the specified component types and attributes.
479
- continue_on_batch_error (bool, optional):
480
- Continue the program (instead of throwing error) if some scenarios fail.
481
- You can still retrieve the errors and succeeded/failed scenarios via the batch_error.
482
- decode_error (bool, optional):
483
- Decode error messages to their derived types if possible.
484
-
485
- Returns:
486
- Dictionary of results of all components.
487
-
488
- - key: Component type name to be updated in batch.
489
- - value:
490
-
491
- - For single calculation: 1D numpy structured array for the results of this component type.
492
- - For batch calculation: 2D numpy structured array for the results of this component type.
493
-
494
- - Dimension 0: Each batch.
495
- - Dimension 1: The result of each element for this component type.
496
-
497
- Raises:
498
- Exception: In case an error in the core occurs, an exception will be thrown.
499
- """
500
- return self._calculate_power_flow(
501
- symmetric=symmetric,
502
- error_tolerance=error_tolerance,
503
- max_iterations=max_iterations,
504
- calculation_method=calculation_method,
505
- update_data=(_map_to_component_types(update_data) if update_data is not None else None),
506
- threading=threading,
507
- output_component_types=output_component_types,
508
- continue_on_batch_error=continue_on_batch_error,
509
- decode_error=decode_error,
510
- tap_changing_strategy=tap_changing_strategy,
511
- )
512
-
513
- def calculate_state_estimation(
514
- self,
515
- *,
516
- symmetric: bool = True,
517
- error_tolerance: float = 1e-8,
518
- max_iterations: int = 20,
519
- calculation_method: CalculationMethod | str = CalculationMethod.iterative_linear,
520
- update_data: dict[str, np.ndarray | dict[str, np.ndarray]] | Dataset | None = None,
521
- threading: int = -1,
522
- output_component_types: ComponentAttributeMapping = None,
523
- continue_on_batch_error: bool = False,
524
- decode_error: bool = True,
525
- ) -> dict[ComponentType, np.ndarray]:
526
- """
527
- Calculate state estimation once with the current model attributes.
528
- Or calculate in batch with the given update dataset in batch.
529
-
530
- Args:
531
- symmetric (bool, optional): Whether to perform a three-phase symmetric calculation.
532
-
533
- - True: Three-phase symmetric calculation, even for asymmetric loads/generations (Default).
534
- - False: Three-phase asymmetric calculation.
535
- error_tolerance (float, optional): error tolerance for voltage in p.u., only applicable when the
536
- calculation method is iterative.
537
- max_iterations (int, optional): Maximum number of iterations, applicable only when the calculation method
538
- is iterative.
539
- calculation_method (an enumeration): Use iterative linear method.
540
- update_data (dict, optional):
541
- None: Calculate state estimation once with the current model attributes.
542
- Or a dictionary for batch calculation with batch update.
543
-
544
- - key: Component type name to be updated in batch.
545
- - value:
546
-
547
- - For homogeneous update batch (a 2D numpy structured array):
548
-
549
- - Dimension 0: Each batch.
550
- - Dimension 1: Each updated element per batch for this component type.
551
- - For inhomogeneous update batch (a dictionary containing two keys):
552
-
553
- - indptr: A 1D numpy int64 array with length n_batch + 1. Given batch number k, the
554
- update array for this batch is data[indptr[k]:indptr[k + 1]]. This is the concept of
555
- compressed sparse structure.
556
- https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
557
- - data: 1D numpy structured array in flat.
558
- threading (int, optional): Applicable only for batch calculation.
559
-
560
- - < 0: Sequential
561
- - = 0: Parallel, use number of hardware threads
562
- - > 0: Specify number of parallel threads
563
- output_component_types (ComponentAttributeMapping):
564
-
565
- - None: Row based data for all component types.
566
- - set[ComponentTypeVar] or list[ComponentTypeVar]: Row based data for the specified component types.
567
- - ComponentAttributeFilterOptions: Columnar data for all component types.
568
- - dict[ComponentType, set[str] | list[str] | None | ComponentAttributeFilterOptions]:
569
- key: ComponentType
570
- value:
571
- - None: Row based data for the specified component types.
572
- - ComponentAttributeFilterOptions: Columnar data for the specified component types.
573
- - set[str] | list[str]: Columnar data for the specified component types and attributes.
574
- continue_on_batch_error (bool, optional):
575
- Continue the program (instead of throwing error) if some scenarios fail.
576
- You can still retrieve the errors and succeeded/failed scenarios via the batch_error.
577
- decode_error (bool, optional):
578
- Decode error messages to their derived types if possible.
579
-
580
- Returns:
581
- Dictionary of results of all components.
582
-
583
- - key: Component type name to be updated in batch.
584
- - value:
585
-
586
- - For single calculation: 1D numpy structured array for the results of this component type.
587
- - For batch calculation: 2D numpy structured array for the results of this component type.
588
-
589
- - Dimension 0: Each batch.
590
- - Dimension 1: The result of each element for this component type.
591
-
592
- Raises:
593
- Exception: In case an error in the core occurs, an exception will be thrown.
594
- """
595
- return self._calculate_state_estimation(
596
- symmetric=symmetric,
597
- error_tolerance=error_tolerance,
598
- max_iterations=max_iterations,
599
- calculation_method=calculation_method,
600
- update_data=(_map_to_component_types(update_data) if update_data is not None else None),
601
- threading=threading,
602
- output_component_types=output_component_types,
603
- continue_on_batch_error=continue_on_batch_error,
604
- decode_error=decode_error,
605
- )
606
-
607
- def calculate_short_circuit(
608
- self,
609
- *,
610
- calculation_method: CalculationMethod | str = CalculationMethod.iec60909,
611
- update_data: dict[str, np.ndarray | dict[str, np.ndarray]] | Dataset | None = None,
612
- threading: int = -1,
613
- output_component_types: ComponentAttributeMapping = None,
614
- continue_on_batch_error: bool = False,
615
- decode_error: bool = True,
616
- short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str = ShortCircuitVoltageScaling.maximum,
617
- ) -> dict[ComponentType, np.ndarray]:
618
- """
619
- Calculate a short circuit once with the current model attributes.
620
- Or calculate in batch with the given update dataset in batch
621
-
622
- Args:
623
- calculation_method (an enumeration): Use the iec60909 standard.
624
- update_data:
625
- None: calculate a short circuit once with the current model attributes.
626
- Or a dictionary for batch calculation with batch update
627
-
628
- - key: Component type name to be updated in batch
629
- - value:
630
-
631
- - For homogeneous update batch (a 2D numpy structured array):
632
-
633
- - Dimension 0: each batch
634
- - Dimension 1: each updated element per batch for this component type
635
- - For inhomogeneous update batch (a dictionary containing two keys):
636
-
637
- - indptr: A 1D numpy int64 array with length n_batch + 1. Given batch number k, the
638
- update array for this batch is data[indptr[k]:indptr[k + 1]]. This is the concept of
639
- compressed sparse structure.
640
- https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
641
- - data: 1D numpy structured array in flat.
642
- threading (int, optional): Applicable only for batch calculation.
643
-
644
- - < 0: Sequential
645
- - = 0: Parallel, use number of hardware threads
646
- - > 0: Specify number of parallel threads
647
- output_component_types (ComponentAttributeMapping):
648
-
649
- - None: Row based data for all component types.
650
- - set[ComponentTypeVar] or list[ComponentTypeVar]: Row based data for the specified component types.
651
- - ComponentAttributeFilterOptions: Columnar data for all component types.
652
- - dict[ComponentType, set[str] | list[str] | None | ComponentAttributeFilterOptions]:
653
- key: ComponentType
654
- value:
655
- - None: Row based data for the specified component types.
656
- - ComponentAttributeFilterOptions: Columnar data for the specified component types.
657
- - set[str] | list[str]: Columnar data for the specified component types and attributes.
658
- continue_on_batch_error (bool, optional):
659
- Continue the program (instead of throwing error) if some scenarios fail.
660
- You can still retrieve the errors and succeeded/failed scenarios via the batch_error.
661
- decode_error (bool, optional):
662
- Decode error messages to their derived types if possible.
663
- short_circuit_voltage_scaling ({ShortCircuitVoltageSaling, str}, optional):
664
- Whether to use the maximum or minimum voltage scaling.
665
- By default, the maximum voltage scaling is used to calculate the short circuit.
666
-
667
- Returns:
668
- Dictionary of results of all components.
669
-
670
- - key: Component type name to be updated in batch.
671
- - value:
672
-
673
- - For single calculation: 1D numpy structured array for the results of this component type.
674
- - For batch calculation: 2D numpy structured array for the results of this component type.
675
-
676
- - Dimension 0: Each batch.
677
- - Dimension 1: The result of each element for this component type.
678
- Raises:
679
- Exception: In case an error in the core occurs, an exception will be thrown.
680
- """
681
- return self._calculate_short_circuit(
682
- calculation_method=calculation_method,
683
- update_data=(_map_to_component_types(update_data) if update_data is not None else None),
684
- threading=threading,
685
- output_component_types=output_component_types,
686
- continue_on_batch_error=continue_on_batch_error,
687
- decode_error=decode_error,
688
- short_circuit_voltage_scaling=short_circuit_voltage_scaling,
689
- )
690
-
691
- def __del__(self):
692
- pgc.destroy_model(self._model_ptr)
1
+ # SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2
+ #
3
+ # SPDX-License-Identifier: MPL-2.0
4
+
5
+ """
6
+ Main power grid model class
7
+ """
8
+
9
+ from enum import IntEnum
10
+ from typing import Any, overload
11
+
12
+ import numpy as np
13
+
14
+ from power_grid_model._core.data_handling import (
15
+ create_output_data,
16
+ get_output_type,
17
+ prepare_input_view,
18
+ prepare_output_view,
19
+ prepare_update_view,
20
+ )
21
+ from power_grid_model._core.data_types import (
22
+ BatchDataset,
23
+ Dataset,
24
+ DenseBatchColumnarOutputDataset,
25
+ DenseBatchOutputDataset,
26
+ DenseBatchRowBasedOutputDataset,
27
+ SingleColumnarOutputDataset,
28
+ SingleDataset,
29
+ SingleOutputDataset,
30
+ SingleRowBasedDataset,
31
+ SingleRowBasedOutputDataset,
32
+ )
33
+ from power_grid_model._core.dataset_definitions import (
34
+ ComponentType,
35
+ ComponentTypeLike,
36
+ ComponentTypeVar,
37
+ _map_to_component_types,
38
+ _str_to_component_type,
39
+ )
40
+ from power_grid_model._core.enum import (
41
+ CalculationMethod,
42
+ CalculationType,
43
+ ComponentAttributeFilterOptions,
44
+ ShortCircuitVoltageScaling,
45
+ TapChangingStrategy,
46
+ _ExperimentalFeatures,
47
+ )
48
+ from power_grid_model._core.error_handling import PowerGridBatchError, assert_no_error, handle_errors
49
+ from power_grid_model._core.index_integer import IdNp, IdxNp
50
+ from power_grid_model._core.options import Options
51
+ from power_grid_model._core.power_grid_core import ConstDatasetPtr, IDPtr, IdxPtr, ModelPtr, power_grid_core as pgc
52
+ from power_grid_model._core.typing import ComponentAttributeMapping, ComponentAttributeMappingDict
53
+
54
+
55
+ class PowerGridModel:
56
+ """
57
+ Main class for Power Grid Model
58
+ """
59
+
60
+ _model_ptr: ModelPtr
61
+ _all_component_count: dict[ComponentType, int] | None
62
+ _batch_error: PowerGridBatchError | None
63
+
64
+ @property
65
+ def batch_error(self) -> PowerGridBatchError | None:
66
+ """
67
+ Get the batch error object, if present, after a batch calculation with errors.
68
+
69
+ Also works when continue_on_batch_error was set to True during the calculation.
70
+
71
+ Returns:
72
+ Batch error object, or None
73
+ """
74
+ return self._batch_error
75
+
76
+ @property
77
+ def _model(self):
78
+ if not self._model_ptr:
79
+ raise TypeError("You have an empty instance of PowerGridModel!")
80
+ return self._model_ptr
81
+
82
+ @property
83
+ def all_component_count(self) -> dict[ComponentType, int]:
84
+ """
85
+ Get amount of elements per component type.
86
+ If the count for a component type is zero, it will not be in the returned dictionary.
87
+
88
+ Returns:
89
+ A dictionary with
90
+
91
+ - key: Component type name
92
+ - value: Integer count of elements of this type
93
+ """
94
+ if self._all_component_count is None:
95
+ raise TypeError("You have an empty instance of PowerGridModel!")
96
+ return self._all_component_count
97
+
98
+ def copy(self) -> "PowerGridModel":
99
+ """
100
+ Copy the current model
101
+
102
+ Returns:
103
+ A copy of PowerGridModel
104
+ """
105
+ new_model = PowerGridModel.__new__(PowerGridModel)
106
+ new_model._model_ptr = pgc.copy_model(self._model)
107
+ assert_no_error()
108
+ new_model._all_component_count = self._all_component_count
109
+ return new_model
110
+
111
+ def __copy__(self) -> "PowerGridModel":
112
+ return self.copy()
113
+
114
+ def __deepcopy__(self, memo: dict[int, Any]) -> "PowerGridModel":
115
+ # PowerGridModel.copy makes already a deepcopy
116
+ new_model = self.copy()
117
+
118
+ # memorize that this object (self) has been deepcopied
119
+ memo[id(self)] = new_model
120
+
121
+ return new_model
122
+
123
+ def __new__(cls, *_args, **_kwargs):
124
+ instance = super().__new__(cls)
125
+ instance._model_ptr = ModelPtr()
126
+ instance._all_component_count = None
127
+ return instance
128
+
129
+ def __init__(self, input_data: SingleDataset, system_frequency: float = 50.0):
130
+ """
131
+ Initialize the model from an input data set.
132
+
133
+ Args:
134
+ input_data: Input data dictionary
135
+
136
+ - key: Component type
137
+ - value: Component data with the correct type :class:`SingleComponentData`
138
+
139
+ system_frequency: Frequency of the power system, default 50 Hz
140
+ """
141
+ # destroy old instance
142
+ pgc.destroy_model(self._model_ptr)
143
+ self._all_component_count = None
144
+ # create new
145
+ prepared_input = prepare_input_view(_map_to_component_types(input_data))
146
+ self._model_ptr = pgc.create_model(system_frequency, input_data=prepared_input.get_dataset_ptr())
147
+ assert_no_error()
148
+ self._all_component_count = {k: v for k, v in prepared_input.get_info().total_elements().items() if v > 0}
149
+
150
+ def update(self, *, update_data: Dataset):
151
+ """
152
+ Update the model with changes.
153
+
154
+ The model will be in an invalid state if the update fails and should be discarded.
155
+
156
+ Args:
157
+ update_data: Update data dictionary
158
+
159
+ - key: Component type
160
+ - value: Component data with the correct type :class:`ComponentData` (single scenario or batch)
161
+
162
+ Raises:
163
+ PowerGridError if the update fails. The model is left in an invalid state and should be discarded.
164
+
165
+ Returns:
166
+ None
167
+ """
168
+ prepared_update = prepare_update_view(_map_to_component_types(update_data))
169
+ pgc.update_model(self._model, prepared_update.get_dataset_ptr())
170
+ assert_no_error()
171
+
172
+ def get_indexer(self, component_type: ComponentTypeLike, ids: np.ndarray):
173
+ """
174
+ Get array of indexers given array of ids for component type.
175
+
176
+ This enables syntax like input_data[ComponentType.node][get_indexer(ids)]
177
+
178
+ Args:
179
+ component_type: Type of component
180
+ ids: Array of ids
181
+
182
+ Returns:
183
+ Array of indexers, same shape as input array ids
184
+ """
185
+ component_type = _str_to_component_type(component_type)
186
+ ids_c = np.ascontiguousarray(ids, dtype=IdNp).ctypes.data_as(IDPtr)
187
+ indexer = np.empty_like(ids, dtype=IdxNp, order="C")
188
+ indexer_c = indexer.ctypes.data_as(IdxPtr)
189
+ size = ids.size
190
+ # call c function
191
+ pgc.get_indexer(self._model, component_type, size, ids_c, indexer_c)
192
+ assert_no_error()
193
+ return indexer
194
+
195
+ def _get_output_component_count(self, calculation_type: CalculationType):
196
+ exclude_types = {
197
+ CalculationType.power_flow: [
198
+ ComponentType.sym_voltage_sensor,
199
+ ComponentType.asym_voltage_sensor,
200
+ ComponentType.sym_power_sensor,
201
+ ComponentType.asym_power_sensor,
202
+ ComponentType.fault,
203
+ ],
204
+ CalculationType.state_estimation: [ComponentType.fault],
205
+ CalculationType.short_circuit: [
206
+ ComponentType.sym_voltage_sensor,
207
+ ComponentType.asym_voltage_sensor,
208
+ ComponentType.sym_power_sensor,
209
+ ComponentType.asym_power_sensor,
210
+ ],
211
+ }.get(calculation_type, [])
212
+
213
+ def include_type(component_type: ComponentType):
214
+ return all(exclude_type.value not in component_type.value for exclude_type in exclude_types)
215
+
216
+ return {ComponentType[k]: v for k, v in self.all_component_count.items() if include_type(k)}
217
+
218
+ def _construct_output(
219
+ self,
220
+ output_component_types: ComponentAttributeMapping,
221
+ calculation_type: CalculationType,
222
+ symmetric: bool,
223
+ is_batch: bool,
224
+ batch_size: int,
225
+ ):
226
+ all_component_count = self._get_output_component_count(calculation_type=calculation_type)
227
+ return create_output_data(
228
+ output_component_types=output_component_types,
229
+ output_type=get_output_type(calculation_type=calculation_type, symmetric=symmetric),
230
+ all_component_count=all_component_count,
231
+ is_batch=is_batch,
232
+ batch_size=batch_size,
233
+ )
234
+
235
+ @staticmethod
236
+ def _options(**kwargs) -> Options:
237
+ def as_enum_value(key_enum: str, type_: type[IntEnum]):
238
+ if key_enum in kwargs:
239
+ value_enum = kwargs[key_enum]
240
+ if isinstance(value_enum, str):
241
+ kwargs[key_enum] = type_[value_enum] # NOSONAR(S5864) IntEnum has __getitem__
242
+
243
+ as_enum_value("calculation_method", CalculationMethod)
244
+ as_enum_value("tap_changing_strategy", TapChangingStrategy)
245
+ as_enum_value("short_circuit_voltage_scaling", ShortCircuitVoltageScaling)
246
+ as_enum_value("experimental_features", _ExperimentalFeatures)
247
+
248
+ opt = Options()
249
+ for key, value in kwargs.items():
250
+ setattr(opt, key, value.value if isinstance(value, IntEnum) else value)
251
+ return opt
252
+
253
+ def _handle_errors(self, continue_on_batch_error: bool, batch_size: int, decode_error: bool):
254
+ self._batch_error = handle_errors(
255
+ continue_on_batch_error=continue_on_batch_error,
256
+ batch_size=batch_size,
257
+ decode_error=decode_error,
258
+ )
259
+
260
+ def _calculate_impl( # noqa: PLR0913
261
+ self,
262
+ calculation_type: CalculationType,
263
+ symmetric: bool,
264
+ update_data: Dataset | None,
265
+ output_component_types: ComponentAttributeMapping,
266
+ options: Options,
267
+ continue_on_batch_error: bool,
268
+ decode_error: bool,
269
+ experimental_features: _ExperimentalFeatures | str, # NOSONAR # noqa: ARG002
270
+ ) -> Dataset:
271
+ """
272
+ Core calculation routine
273
+
274
+ Args:
275
+ calculation_type:
276
+ symmetric:
277
+ update_data:
278
+ output_component_types:
279
+ options:
280
+ continue_on_batch_error:
281
+ decode_error:
282
+
283
+ Returns:
284
+ """
285
+ self._batch_error = None
286
+ is_batch = update_data is not None
287
+
288
+ if update_data is not None:
289
+ prepared_update = prepare_update_view(update_data)
290
+ update_ptr = prepared_update.get_dataset_ptr()
291
+ batch_size = prepared_update.get_info().batch_size()
292
+ else:
293
+ update_ptr = ConstDatasetPtr()
294
+ batch_size = 1
295
+
296
+ output_data = self._construct_output(
297
+ output_component_types=output_component_types,
298
+ calculation_type=calculation_type,
299
+ symmetric=symmetric,
300
+ is_batch=is_batch,
301
+ batch_size=batch_size,
302
+ )
303
+ prepared_result = prepare_output_view(
304
+ output_data=output_data,
305
+ output_type=get_output_type(calculation_type=calculation_type, symmetric=symmetric),
306
+ )
307
+
308
+ # run calculation
309
+ pgc.calculate(
310
+ # model and options
311
+ self._model,
312
+ options.opt,
313
+ output_data=prepared_result.get_dataset_ptr(),
314
+ update_data=update_ptr,
315
+ )
316
+
317
+ self._handle_errors(
318
+ continue_on_batch_error=continue_on_batch_error,
319
+ batch_size=batch_size,
320
+ decode_error=decode_error,
321
+ )
322
+
323
+ return output_data
324
+
325
+ def _calculate_power_flow( # noqa: PLR0913
326
+ self,
327
+ *,
328
+ symmetric: bool = True,
329
+ error_tolerance: float = 1e-8,
330
+ max_iterations: int = 20,
331
+ calculation_method: CalculationMethod | str = CalculationMethod.newton_raphson,
332
+ update_data: Dataset | None = None,
333
+ threading: int = -1,
334
+ output_component_types: ComponentAttributeMapping = None,
335
+ continue_on_batch_error: bool = False,
336
+ decode_error: bool = True,
337
+ tap_changing_strategy: TapChangingStrategy | str = TapChangingStrategy.disabled,
338
+ experimental_features: _ExperimentalFeatures | str = _ExperimentalFeatures.disabled,
339
+ ) -> Dataset:
340
+ calculation_type = CalculationType.power_flow
341
+ options = self._options(
342
+ calculation_type=calculation_type,
343
+ symmetric=symmetric,
344
+ error_tolerance=error_tolerance,
345
+ max_iterations=max_iterations,
346
+ calculation_method=calculation_method,
347
+ tap_changing_strategy=tap_changing_strategy,
348
+ threading=threading,
349
+ experimental_features=experimental_features,
350
+ )
351
+ return self._calculate_impl(
352
+ calculation_type=calculation_type,
353
+ symmetric=symmetric,
354
+ update_data=update_data,
355
+ output_component_types=output_component_types,
356
+ options=options,
357
+ continue_on_batch_error=continue_on_batch_error,
358
+ decode_error=decode_error,
359
+ experimental_features=experimental_features,
360
+ )
361
+
362
+ def _calculate_state_estimation( # noqa: PLR0913
363
+ self,
364
+ *,
365
+ symmetric: bool = True,
366
+ error_tolerance: float = 1e-8,
367
+ max_iterations: int = 20,
368
+ calculation_method: CalculationMethod | str = CalculationMethod.iterative_linear,
369
+ update_data: Dataset | None = None,
370
+ threading: int = -1,
371
+ output_component_types: ComponentAttributeMapping = None,
372
+ continue_on_batch_error: bool = False,
373
+ decode_error: bool = True,
374
+ experimental_features: _ExperimentalFeatures | str = _ExperimentalFeatures.disabled,
375
+ ) -> Dataset:
376
+ calculation_type = CalculationType.state_estimation
377
+ options = self._options(
378
+ calculation_type=calculation_type,
379
+ symmetric=symmetric,
380
+ error_tolerance=error_tolerance,
381
+ max_iterations=max_iterations,
382
+ calculation_method=calculation_method,
383
+ threading=threading,
384
+ experimental_features=experimental_features,
385
+ )
386
+ return self._calculate_impl(
387
+ calculation_type=calculation_type,
388
+ symmetric=symmetric,
389
+ update_data=update_data,
390
+ output_component_types=output_component_types,
391
+ options=options,
392
+ continue_on_batch_error=continue_on_batch_error,
393
+ decode_error=decode_error,
394
+ experimental_features=experimental_features,
395
+ )
396
+
397
+ def _calculate_short_circuit( # noqa: PLR0913
398
+ self,
399
+ *,
400
+ calculation_method: CalculationMethod | str = CalculationMethod.iec60909,
401
+ update_data: Dataset | None = None,
402
+ threading: int = -1,
403
+ output_component_types: ComponentAttributeMapping = None,
404
+ continue_on_batch_error: bool = False,
405
+ decode_error: bool = True,
406
+ short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str = ShortCircuitVoltageScaling.maximum,
407
+ experimental_features: _ExperimentalFeatures | str = _ExperimentalFeatures.disabled,
408
+ ) -> Dataset:
409
+ calculation_type = CalculationType.short_circuit
410
+ symmetric = False
411
+
412
+ options = self._options(
413
+ calculation_type=calculation_type,
414
+ symmetric=symmetric,
415
+ calculation_method=calculation_method,
416
+ threading=threading,
417
+ short_circuit_voltage_scaling=short_circuit_voltage_scaling,
418
+ experimental_features=experimental_features,
419
+ )
420
+ return self._calculate_impl(
421
+ calculation_type=calculation_type,
422
+ symmetric=symmetric,
423
+ update_data=update_data,
424
+ output_component_types=output_component_types,
425
+ options=options,
426
+ continue_on_batch_error=continue_on_batch_error,
427
+ decode_error=decode_error,
428
+ experimental_features=experimental_features,
429
+ )
430
+
431
+ @overload
432
+ def calculate_power_flow(
433
+ self,
434
+ *,
435
+ symmetric: bool = ...,
436
+ error_tolerance: float = ...,
437
+ max_iterations: int = ...,
438
+ calculation_method: CalculationMethod | str = ...,
439
+ threading: int = ...,
440
+ output_component_types: None | set[ComponentTypeVar] | list[ComponentTypeVar] = ...,
441
+ continue_on_batch_error: bool = ...,
442
+ decode_error: bool = ...,
443
+ tap_changing_strategy: TapChangingStrategy | str = ...,
444
+ ) -> SingleRowBasedDataset: ...
445
+ @overload
446
+ def calculate_power_flow(
447
+ self,
448
+ *,
449
+ symmetric: bool = ...,
450
+ error_tolerance: float = ...,
451
+ max_iterations: int = ...,
452
+ calculation_method: CalculationMethod | str = ...,
453
+ update_data: None = ...,
454
+ threading: int = ...,
455
+ output_component_types: None | set[ComponentTypeVar] | list[ComponentTypeVar] = ...,
456
+ continue_on_batch_error: bool = ...,
457
+ decode_error: bool = ...,
458
+ tap_changing_strategy: TapChangingStrategy | str = ...,
459
+ ) -> SingleRowBasedDataset: ...
460
+ @overload
461
+ def calculate_power_flow(
462
+ self,
463
+ *,
464
+ symmetric: bool = ...,
465
+ error_tolerance: float = ...,
466
+ max_iterations: int = ...,
467
+ calculation_method: CalculationMethod | str = ...,
468
+ update_data: None = ...,
469
+ threading: int = ...,
470
+ output_component_types: ComponentAttributeFilterOptions = ...,
471
+ continue_on_batch_error: bool = ...,
472
+ decode_error: bool = ...,
473
+ tap_changing_strategy: TapChangingStrategy | str = ...,
474
+ ) -> SingleColumnarOutputDataset: ...
475
+ @overload
476
+ def calculate_power_flow(
477
+ self,
478
+ *,
479
+ symmetric: bool = ...,
480
+ error_tolerance: float = ...,
481
+ max_iterations: int = ...,
482
+ calculation_method: CalculationMethod | str = ...,
483
+ update_data: None = ...,
484
+ threading: int = ...,
485
+ output_component_types: ComponentAttributeMappingDict = ...,
486
+ continue_on_batch_error: bool = ...,
487
+ decode_error: bool = ...,
488
+ tap_changing_strategy: TapChangingStrategy | str = ...,
489
+ ) -> SingleOutputDataset: ...
490
+ @overload
491
+ def calculate_power_flow(
492
+ self,
493
+ *,
494
+ symmetric: bool = ...,
495
+ error_tolerance: float = ...,
496
+ max_iterations: int = ...,
497
+ calculation_method: CalculationMethod | str = ...,
498
+ update_data: BatchDataset = ...,
499
+ threading: int = ...,
500
+ output_component_types: None | set[ComponentTypeVar] | list[ComponentTypeVar] = ...,
501
+ continue_on_batch_error: bool = ...,
502
+ decode_error: bool = ...,
503
+ tap_changing_strategy: TapChangingStrategy | str = ...,
504
+ ) -> DenseBatchRowBasedOutputDataset: ...
505
+ @overload
506
+ def calculate_power_flow(
507
+ self,
508
+ *,
509
+ symmetric: bool = ...,
510
+ error_tolerance: float = ...,
511
+ max_iterations: int = ...,
512
+ calculation_method: CalculationMethod | str = ...,
513
+ update_data: BatchDataset = ...,
514
+ threading: int = ...,
515
+ output_component_types: ComponentAttributeFilterOptions = ...,
516
+ continue_on_batch_error: bool = ...,
517
+ decode_error: bool = ...,
518
+ tap_changing_strategy: TapChangingStrategy | str = ...,
519
+ ) -> DenseBatchColumnarOutputDataset: ...
520
+ @overload
521
+ def calculate_power_flow(
522
+ self,
523
+ *,
524
+ symmetric: bool = ...,
525
+ error_tolerance: float = ...,
526
+ max_iterations: int = ...,
527
+ calculation_method: CalculationMethod | str = ...,
528
+ update_data: BatchDataset = ...,
529
+ threading: int = ...,
530
+ output_component_types: ComponentAttributeMappingDict = ...,
531
+ continue_on_batch_error: bool = ...,
532
+ decode_error: bool = ...,
533
+ tap_changing_strategy: TapChangingStrategy | str = ...,
534
+ ) -> DenseBatchOutputDataset: ...
535
+ def calculate_power_flow( # noqa: PLR0913
536
+ self,
537
+ *,
538
+ symmetric: bool = True,
539
+ error_tolerance: float = 1e-8,
540
+ max_iterations: int = 20,
541
+ calculation_method: CalculationMethod | str = CalculationMethod.newton_raphson,
542
+ update_data: BatchDataset | None = None,
543
+ threading: int = -1,
544
+ output_component_types: ComponentAttributeMapping = None,
545
+ continue_on_batch_error: bool = False,
546
+ decode_error: bool = True,
547
+ tap_changing_strategy: TapChangingStrategy | str = TapChangingStrategy.disabled,
548
+ ) -> Dataset:
549
+ """
550
+ Calculate power flow once with the current model attributes.
551
+ Or calculate in batch with the given update dataset in batch.
552
+
553
+ Args:
554
+ symmetric (bool, optional): Whether to perform a three-phase symmetric calculation.
555
+
556
+ - True: Three-phase symmetric calculation, even for asymmetric loads/generations (Default).
557
+ - False: Three-phase asymmetric calculation.
558
+ error_tolerance (float, optional): Error tolerance for voltage in p.u., applicable only when the
559
+ calculation method is iterative.
560
+ max_iterations (int, optional): Maximum number of iterations, applicable only when the calculation method
561
+ is iterative.
562
+ calculation_method (an enumeration or string): The calculation method to use.
563
+
564
+ - newton_raphson: Use Newton-Raphson iterative method (default).
565
+ - linear: Use linear method.
566
+ update_data (dict, optional):
567
+ None: Calculate power flow once with the current model attributes.
568
+ Or a dictionary for batch calculation with batch update.
569
+
570
+ - key: Component type name to be updated in batch.
571
+ - value:
572
+
573
+ - For homogeneous update batch (a 2D numpy structured array):
574
+
575
+ - Dimension 0: Each batch.
576
+ - Dimension 1: Each updated element per batch for this component type.
577
+ - For inhomogeneous update batch (a dictionary containing two keys):
578
+
579
+ - indptr: A 1D numpy int64 array with length n_batch + 1. Given batch number k, the
580
+ update array for this batch is data[indptr[k]:indptr[k + 1]]. This is the concept of
581
+ compressed sparse structure.
582
+ https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
583
+ - data: 1D numpy structured array in flat.
584
+ threading (int, optional): Applicable only for batch calculation.
585
+
586
+ - < 0: Sequential
587
+ - = 0: Parallel, use number of hardware threads
588
+ - > 0: Specify number of parallel threads
589
+ output_component_types (ComponentAttributeMapping):
590
+
591
+ - None: Row based data for all component types.
592
+ - set[ComponentTypeVar] or list[ComponentTypeVar]: Row based data for the specified component types.
593
+ - ComponentAttributeFilterOptions: Columnar data for all component types.
594
+ - ComponentAttributeMappingDict:
595
+ key: ComponentType
596
+ value:
597
+ - None: Row based data for the specified component types.
598
+ - ComponentAttributeFilterOptions: Columnar data for the specified component types.
599
+ - set[str] | list[str]: Columnar data for the specified component types and attributes.
600
+ continue_on_batch_error (bool, optional):
601
+ Continue the program (instead of throwing error) if some scenarios fail.
602
+ You can still retrieve the errors and succeeded/failed scenarios via the batch_error.
603
+ decode_error (bool, optional):
604
+ Decode error messages to their derived types if possible.
605
+
606
+ Returns:
607
+ Dictionary of results of all components.
608
+
609
+ - key: Component type name to be updated in batch.
610
+ - value:
611
+
612
+ - For single calculation: 1D numpy structured array for the results of this component type.
613
+ - For batch calculation: 2D numpy structured array for the results of this component type.
614
+
615
+ - Dimension 0: Each batch.
616
+ - Dimension 1: The result of each element for this component type.
617
+
618
+ Raises:
619
+ Exception: In case an error in the core occurs, an exception will be thrown.
620
+ """
621
+ return self._calculate_power_flow(
622
+ symmetric=symmetric,
623
+ error_tolerance=error_tolerance,
624
+ max_iterations=max_iterations,
625
+ calculation_method=calculation_method,
626
+ update_data=(_map_to_component_types(update_data) if update_data is not None else None),
627
+ threading=threading,
628
+ output_component_types=output_component_types,
629
+ continue_on_batch_error=continue_on_batch_error,
630
+ decode_error=decode_error,
631
+ tap_changing_strategy=tap_changing_strategy,
632
+ )
633
+
634
+ @overload
635
+ def calculate_state_estimation(
636
+ self,
637
+ *,
638
+ symmetric: bool = ...,
639
+ error_tolerance: float = ...,
640
+ max_iterations: int = ...,
641
+ calculation_method: CalculationMethod | str = ...,
642
+ update_data: None = ...,
643
+ threading: int = ...,
644
+ output_component_types: None | set[ComponentTypeVar] | list[ComponentTypeVar] = ...,
645
+ continue_on_batch_error: bool = ...,
646
+ decode_error: bool = ...,
647
+ ) -> SingleRowBasedOutputDataset: ...
648
+ @overload
649
+ def calculate_state_estimation(
650
+ self,
651
+ *,
652
+ symmetric: bool = ...,
653
+ error_tolerance: float = ...,
654
+ max_iterations: int = ...,
655
+ calculation_method: CalculationMethod | str = ...,
656
+ update_data: None = ...,
657
+ threading: int = ...,
658
+ output_component_types: ComponentAttributeFilterOptions = ...,
659
+ continue_on_batch_error: bool = ...,
660
+ decode_error: bool = ...,
661
+ ) -> SingleColumnarOutputDataset: ...
662
+ @overload
663
+ def calculate_state_estimation(
664
+ self,
665
+ *,
666
+ symmetric: bool = ...,
667
+ error_tolerance: float = ...,
668
+ max_iterations: int = ...,
669
+ calculation_method: CalculationMethod | str = ...,
670
+ update_data: None = ...,
671
+ threading: int = ...,
672
+ output_component_types: ComponentAttributeMappingDict = ...,
673
+ continue_on_batch_error: bool = ...,
674
+ decode_error: bool = ...,
675
+ ) -> SingleOutputDataset: ...
676
+ @overload
677
+ def calculate_state_estimation(
678
+ self,
679
+ *,
680
+ symmetric: bool = ...,
681
+ error_tolerance: float = ...,
682
+ max_iterations: int = ...,
683
+ calculation_method: CalculationMethod | str = ...,
684
+ update_data: BatchDataset = ...,
685
+ threading: int = ...,
686
+ output_component_types: None | set[ComponentTypeVar] | list[ComponentTypeVar] = ...,
687
+ continue_on_batch_error: bool = ...,
688
+ decode_error: bool = ...,
689
+ ) -> DenseBatchRowBasedOutputDataset: ...
690
+ @overload
691
+ def calculate_state_estimation(
692
+ self,
693
+ *,
694
+ symmetric: bool = ...,
695
+ error_tolerance: float = ...,
696
+ max_iterations: int = ...,
697
+ calculation_method: CalculationMethod | str = ...,
698
+ update_data: BatchDataset = ...,
699
+ threading: int = ...,
700
+ output_component_types: ComponentAttributeFilterOptions = ...,
701
+ continue_on_batch_error: bool = ...,
702
+ decode_error: bool = ...,
703
+ ) -> DenseBatchColumnarOutputDataset: ...
704
+ @overload
705
+ def calculate_state_estimation(
706
+ self,
707
+ *,
708
+ symmetric: bool = ...,
709
+ error_tolerance: float = ...,
710
+ max_iterations: int = ...,
711
+ calculation_method: CalculationMethod | str = ...,
712
+ update_data: BatchDataset = ...,
713
+ threading: int = ...,
714
+ output_component_types: ComponentAttributeMappingDict = ...,
715
+ continue_on_batch_error: bool = ...,
716
+ decode_error: bool = ...,
717
+ ) -> DenseBatchOutputDataset: ...
718
+ def calculate_state_estimation( # noqa: PLR0913
719
+ self,
720
+ *,
721
+ symmetric: bool = True,
722
+ error_tolerance: float = 1e-8,
723
+ max_iterations: int = 20,
724
+ calculation_method: CalculationMethod | str = CalculationMethod.iterative_linear,
725
+ update_data: BatchDataset | None = None,
726
+ threading: int = -1,
727
+ output_component_types: ComponentAttributeMapping = None,
728
+ continue_on_batch_error: bool = False,
729
+ decode_error: bool = True,
730
+ ) -> Dataset:
731
+ """
732
+ Calculate state estimation once with the current model attributes.
733
+ Or calculate in batch with the given update dataset in batch.
734
+
735
+ Args:
736
+ symmetric (bool, optional): Whether to perform a three-phase symmetric calculation.
737
+
738
+ - True: Three-phase symmetric calculation, even for asymmetric loads/generations (Default).
739
+ - False: Three-phase asymmetric calculation.
740
+ error_tolerance (float, optional): error tolerance for voltage in p.u., only applicable when the
741
+ calculation method is iterative.
742
+ max_iterations (int, optional): Maximum number of iterations, applicable only when the calculation method
743
+ is iterative.
744
+ calculation_method (an enumeration): Use iterative linear method.
745
+ update_data (dict, optional):
746
+ None: Calculate state estimation once with the current model attributes.
747
+ Or a dictionary for batch calculation with batch update.
748
+
749
+ - key: Component type name to be updated in batch.
750
+ - value:
751
+
752
+ - For homogeneous update batch (a 2D numpy structured array):
753
+
754
+ - Dimension 0: Each batch.
755
+ - Dimension 1: Each updated element per batch for this component type.
756
+ - For inhomogeneous update batch (a dictionary containing two keys):
757
+
758
+ - indptr: A 1D numpy int64 array with length n_batch + 1. Given batch number k, the
759
+ update array for this batch is data[indptr[k]:indptr[k + 1]]. This is the concept of
760
+ compressed sparse structure.
761
+ https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
762
+ - data: 1D numpy structured array in flat.
763
+ threading (int, optional): Applicable only for batch calculation.
764
+
765
+ - < 0: Sequential
766
+ - = 0: Parallel, use number of hardware threads
767
+ - > 0: Specify number of parallel threads
768
+ output_component_types (ComponentAttributeMapping):
769
+
770
+ - None: Row based data for all component types.
771
+ - set[ComponentTypeVar] or list[ComponentTypeVar]: Row based data for the specified component types.
772
+ - ComponentAttributeFilterOptions: Columnar data for all component types.
773
+ - ComponentAttributeMappingDict:
774
+ key: ComponentType
775
+ value:
776
+ - None: Row based data for the specified component types.
777
+ - ComponentAttributeFilterOptions: Columnar data for the specified component types.
778
+ - set[str] | list[str]: Columnar data for the specified component types and attributes.
779
+ continue_on_batch_error (bool, optional):
780
+ Continue the program (instead of throwing error) if some scenarios fail.
781
+ You can still retrieve the errors and succeeded/failed scenarios via the batch_error.
782
+ decode_error (bool, optional):
783
+ Decode error messages to their derived types if possible.
784
+
785
+ Returns:
786
+ Dictionary of results of all components.
787
+
788
+ - key: Component type name to be updated in batch.
789
+ - value:
790
+
791
+ - For single calculation: 1D numpy structured array for the results of this component type.
792
+ - For batch calculation: 2D numpy structured array for the results of this component type.
793
+
794
+ - Dimension 0: Each batch.
795
+ - Dimension 1: The result of each element for this component type.
796
+
797
+ Raises:
798
+ Exception: In case an error in the core occurs, an exception will be thrown.
799
+ """
800
+ return self._calculate_state_estimation(
801
+ symmetric=symmetric,
802
+ error_tolerance=error_tolerance,
803
+ max_iterations=max_iterations,
804
+ calculation_method=calculation_method,
805
+ update_data=(_map_to_component_types(update_data) if update_data is not None else None),
806
+ threading=threading,
807
+ output_component_types=output_component_types,
808
+ continue_on_batch_error=continue_on_batch_error,
809
+ decode_error=decode_error,
810
+ )
811
+
812
+ @overload
813
+ def calculate_short_circuit(
814
+ self,
815
+ *,
816
+ calculation_method: CalculationMethod | str = ...,
817
+ update_data: None = ...,
818
+ threading: int = ...,
819
+ output_component_types: None | set[ComponentTypeVar] | list[ComponentTypeVar] = ...,
820
+ continue_on_batch_error: bool = ...,
821
+ decode_error: bool = ...,
822
+ short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str,
823
+ ) -> SingleRowBasedDataset: ...
824
+ @overload
825
+ def calculate_short_circuit(
826
+ self,
827
+ *,
828
+ calculation_method: CalculationMethod | str = ...,
829
+ update_data: None = ...,
830
+ threading: int = ...,
831
+ output_component_types: ComponentAttributeFilterOptions = ...,
832
+ continue_on_batch_error: bool = ...,
833
+ decode_error: bool = ...,
834
+ short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str,
835
+ ) -> SingleColumnarOutputDataset: ...
836
+ @overload
837
+ def calculate_short_circuit(
838
+ self,
839
+ *,
840
+ calculation_method: CalculationMethod | str = ...,
841
+ update_data: None = ...,
842
+ threading: int = ...,
843
+ output_component_types: ComponentAttributeMappingDict = ...,
844
+ continue_on_batch_error: bool = ...,
845
+ decode_error: bool = ...,
846
+ short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str,
847
+ ) -> SingleOutputDataset: ...
848
+ @overload
849
+ def calculate_short_circuit(
850
+ self,
851
+ *,
852
+ calculation_method: CalculationMethod | str = ...,
853
+ update_data: BatchDataset = ...,
854
+ threading: int = ...,
855
+ output_component_types: None | set[ComponentTypeVar] | list[ComponentTypeVar] = ...,
856
+ continue_on_batch_error: bool = ...,
857
+ decode_error: bool = ...,
858
+ short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str,
859
+ ) -> DenseBatchRowBasedOutputDataset: ...
860
+ @overload
861
+ def calculate_short_circuit(
862
+ self,
863
+ *,
864
+ calculation_method: CalculationMethod | str = ...,
865
+ update_data: BatchDataset = ...,
866
+ threading: int = ...,
867
+ output_component_types: ComponentAttributeFilterOptions = ...,
868
+ continue_on_batch_error: bool = ...,
869
+ decode_error: bool = ...,
870
+ short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str,
871
+ ) -> DenseBatchColumnarOutputDataset: ...
872
+ @overload
873
+ def calculate_short_circuit(
874
+ self,
875
+ *,
876
+ calculation_method: CalculationMethod | str = ...,
877
+ update_data: BatchDataset = ...,
878
+ threading: int = ...,
879
+ output_component_types: ComponentAttributeMappingDict = ...,
880
+ continue_on_batch_error: bool = ...,
881
+ decode_error: bool = ...,
882
+ short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str,
883
+ ) -> DenseBatchOutputDataset: ...
884
+ def calculate_short_circuit( # noqa: PLR0913
885
+ self,
886
+ *,
887
+ calculation_method: CalculationMethod | str = CalculationMethod.iec60909,
888
+ update_data: BatchDataset | None = None,
889
+ threading: int = -1,
890
+ output_component_types: ComponentAttributeMapping = None,
891
+ continue_on_batch_error: bool = False,
892
+ decode_error: bool = True,
893
+ short_circuit_voltage_scaling: ShortCircuitVoltageScaling | str = ShortCircuitVoltageScaling.maximum,
894
+ ) -> Dataset:
895
+ """
896
+ Calculate a short circuit once with the current model attributes.
897
+ Or calculate in batch with the given update dataset in batch
898
+
899
+ Args:
900
+ calculation_method (an enumeration): Use the iec60909 standard.
901
+ update_data:
902
+ None: calculate a short circuit once with the current model attributes.
903
+ Or a dictionary for batch calculation with batch update
904
+
905
+ - key: Component type name to be updated in batch
906
+ - value:
907
+
908
+ - For homogeneous update batch (a 2D numpy structured array):
909
+
910
+ - Dimension 0: each batch
911
+ - Dimension 1: each updated element per batch for this component type
912
+ - For inhomogeneous update batch (a dictionary containing two keys):
913
+
914
+ - indptr: A 1D numpy int64 array with length n_batch + 1. Given batch number k, the
915
+ update array for this batch is data[indptr[k]:indptr[k + 1]]. This is the concept of
916
+ compressed sparse structure.
917
+ https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
918
+ - data: 1D numpy structured array in flat.
919
+ threading (int, optional): Applicable only for batch calculation.
920
+
921
+ - < 0: Sequential
922
+ - = 0: Parallel, use number of hardware threads
923
+ - > 0: Specify number of parallel threads
924
+ output_component_types (ComponentAttributeMapping):
925
+
926
+ - None: Row based data for all component types.
927
+ - set[ComponentTypeVar] or list[ComponentTypeVar]: Row based data for the specified component types.
928
+ - ComponentAttributeFilterOptions: Columnar data for all component types.
929
+ - ComponentAttributeMappingDict:
930
+ key: ComponentType
931
+ value:
932
+ - None: Row based data for the specified component types.
933
+ - ComponentAttributeFilterOptions: Columnar data for the specified component types.
934
+ - set[str] | list[str]: Columnar data for the specified component types and attributes.
935
+ continue_on_batch_error (bool, optional):
936
+ Continue the program (instead of throwing error) if some scenarios fail.
937
+ You can still retrieve the errors and succeeded/failed scenarios via the batch_error.
938
+ decode_error (bool, optional):
939
+ Decode error messages to their derived types if possible.
940
+ short_circuit_voltage_scaling ({ShortCircuitVoltageSaling, str}, optional):
941
+ Whether to use the maximum or minimum voltage scaling.
942
+ By default, the maximum voltage scaling is used to calculate the short circuit.
943
+
944
+ Returns:
945
+ Dictionary of results of all components.
946
+
947
+ - key: Component type name to be updated in batch.
948
+ - value:
949
+
950
+ - For single calculation: 1D numpy structured array for the results of this component type.
951
+ - For batch calculation: 2D numpy structured array for the results of this component type.
952
+
953
+ - Dimension 0: Each batch.
954
+ - Dimension 1: The result of each element for this component type.
955
+ Raises:
956
+ Exception: In case an error in the core occurs, an exception will be thrown.
957
+ """
958
+ return self._calculate_short_circuit(
959
+ calculation_method=calculation_method,
960
+ update_data=(_map_to_component_types(update_data) if update_data is not None else None),
961
+ threading=threading,
962
+ output_component_types=output_component_types,
963
+ continue_on_batch_error=continue_on_batch_error,
964
+ decode_error=decode_error,
965
+ short_circuit_voltage_scaling=short_circuit_voltage_scaling,
966
+ )
967
+
968
+ def __del__(self):
969
+ pgc.destroy_model(self._model_ptr)