|
3 | 3 | # SPDX-License-Identifier: MPL-2.0 |
4 | 4 |
|
5 | 5 |
|
| 6 | +from enum import StrEnum |
| 7 | +from unittest.mock import patch |
| 8 | + |
6 | 9 | import numpy as np |
7 | 10 | import pytest |
8 | | -from power_grid_model import TapChangingStrategy, initialize_array |
| 11 | +from power_grid_model import ComponentType, TapChangingStrategy, initialize_array |
9 | 12 |
|
10 | 13 | from power_grid_model_ds._core.data_source.generator.grid_generators import RadialGridGenerator |
11 | | -from power_grid_model_ds._core.load_flow import PowerGridModelInterface |
12 | 14 | from power_grid_model_ds._core.model.arrays import ( |
13 | 15 | LineArray, |
14 | 16 | NodeArray, |
15 | 17 | SourceArray, |
16 | 18 | SymLoadArray, |
17 | 19 | ) |
18 | 20 | from power_grid_model_ds._core.model.grids.base import Grid |
| 21 | +from power_grid_model_ds._core.power_grid_model_interface import PowerGridModelInterface |
19 | 22 | from tests.fixtures.arrays import ExtendedLineArray, ExtendedNodeArray |
20 | 23 | from tests.fixtures.grid_classes import ExtendedGrid |
21 | 24 | from tests.unit.model.grids.test_custom_grid import CustomGrid |
@@ -112,26 +115,48 @@ def test_grid_with_automatic_tap_regulator(self, grid_with_tap_regulator: Grid): |
112 | 115 | assert output["transformer_tap_regulator"]["tap_pos"][0] > 0 |
113 | 116 |
|
114 | 117 |
|
115 | | -class PowerGridModelInterfaceMethods: |
| 118 | +ExtendedComponentType = StrEnum("ExtendedComponentType", {x.name: x.value for x in ComponentType} | {"onzin": "onzin"}) |
| 119 | + |
| 120 | + |
| 121 | +class TestPowerGridModelInterfaceMethods: |
116 | 122 | def test_update_grid(self): |
117 | 123 | """Tests the power flow on a randomly configured grid and update grid with results""" |
118 | 124 | grid_generator = RadialGridGenerator(grid_class=Grid, nr_nodes=5, nr_sources=1, nr_nops=0) |
119 | 125 | grid = grid_generator.run(seed=0) |
120 | | - |
121 | 126 | grid.node = ExtendedNodeArray(grid.node.data) |
122 | 127 | grid.line = ExtendedLineArray(grid.line.data) |
123 | 128 |
|
124 | | - core_interface = PowerGridModelInterface(grid=grid) |
125 | | - core_interface.create_input_from_grid() |
126 | | - core_interface.calculate_power_flow() |
127 | | - core_interface.update_grid() |
| 129 | + with patch("power_grid_model_ds._core.power_grid_model_interface.ComponentType", ExtendedComponentType): |
| 130 | + core_interface = PowerGridModelInterface(grid=grid) |
| 131 | + core_interface.create_input_from_grid() |
| 132 | + core_interface.calculate_power_flow() |
| 133 | + core_interface.update_grid() |
128 | 134 |
|
| 135 | + grid = core_interface.grid |
129 | 136 | # voltage should be in neighbourhood of 10500 |
130 | 137 | assert grid.node.u[0] == pytest.approx(10_500, 0.1) |
131 | 138 | assert grid.node.u[1] == pytest.approx(10_500, 0.1) |
132 | 139 | # all lines have a current |
133 | 140 | assert all(grid.line.i_from > 0) |
134 | 141 |
|
| 142 | + # def test_update_grid_missing_component(self, core_interface_after_loadflow: PowerGridModelInterface): |
| 143 | + # """Test that update grid works when a component is missing in the grid.""" |
| 144 | + # del core_interface_after_loadflow.grid.source |
| 145 | + |
| 146 | + # core_interface_after_loadflow.update_grid() |
| 147 | + |
| 148 | + # # voltage should be in neighbourhood of 10500 |
| 149 | + # grid = core_interface_after_loadflow.grid |
| 150 | + # assert not all(grid.node.is_empty("u")) |
| 151 | + # assert not hasattr(grid, "source") |
| 152 | + |
| 153 | + def test_input_from_grid_missing_component(self, grid: Grid): |
| 154 | + # Mimic no source in grid |
| 155 | + del grid.source |
| 156 | + |
| 157 | + core_interface = PowerGridModelInterface(grid=grid) |
| 158 | + assert "source" not in core_interface.create_input_from_grid() |
| 159 | + |
135 | 160 | def test_update_model(self): |
136 | 161 | """Test whether a pgm model can be updated and returns different results""" |
137 | 162 | grid_generator = RadialGridGenerator(grid_class=Grid, nr_nodes=5, nr_sources=1, nr_nops=0) |
@@ -183,7 +208,7 @@ def test_setup_model(self): |
183 | 208 |
|
184 | 209 | core_interface = PowerGridModelInterface(grid=grid) |
185 | 210 | assert core_interface.model is None |
186 | | - assert core_interface._input_data is None |
| 211 | + assert core_interface._input_data == {} |
187 | 212 | core_interface.setup_model() |
188 | 213 | assert core_interface.model |
189 | 214 | assert core_interface._input_data |
|
0 commit comments