|
| 1 | +import itertools |
| 2 | +import pandas as pd |
| 3 | +import pandera as pa |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pandapower.create import create_empty_network, create_bus, create_bus_dc, create_vsc_stacked |
| 7 | +from pandapower.network_schema.tools.validation.network_validation import validate_network |
| 8 | + |
| 9 | +from pandapower.test.network_schema.elements.helper import ( |
| 10 | + strings, |
| 11 | + bools, |
| 12 | + not_strings_list, |
| 13 | + not_floats_list, |
| 14 | + not_boolean_list, |
| 15 | + positiv_ints_plus_zero, |
| 16 | + not_ints_list, |
| 17 | + negativ_ints, |
| 18 | + all_allowed_floats, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class TestVSCSTACKEDRequiredFields: |
| 23 | + """Tests for required vsc_stacked fields""" |
| 24 | + |
| 25 | + @pytest.mark.parametrize( |
| 26 | + "parameter,valid_value", |
| 27 | + list( |
| 28 | + itertools.chain( |
| 29 | + itertools.product(["bus"], positiv_ints_plus_zero), |
| 30 | + itertools.product(["bus_dc_plus"], positiv_ints_plus_zero), |
| 31 | + itertools.product(["bus_dc_minus"], positiv_ints_plus_zero), |
| 32 | + itertools.product(["r_ohm"], all_allowed_floats), |
| 33 | + itertools.product(["x_ohm"], all_allowed_floats), |
| 34 | + itertools.product(["r_dc_ohm"], all_allowed_floats), |
| 35 | + itertools.product(["pl_dc_mw"], all_allowed_floats), |
| 36 | + itertools.product(["control_mode_ac"], ["vm_pu", "q_mvar", "slack"]), |
| 37 | + itertools.product(["control_value_ac"], all_allowed_floats), |
| 38 | + itertools.product(["control_mode_dc"], ["vm_pu", "p_mw"]), |
| 39 | + itertools.product(["control_value_dc"], all_allowed_floats), |
| 40 | + itertools.product(["controllable"], bools), |
| 41 | + itertools.product(["in_service"], bools), |
| 42 | + ) |
| 43 | + ), |
| 44 | + ) |
| 45 | + def test_valid_required_values(self, parameter, valid_value): |
| 46 | + """Test: valid required values are accepted""" |
| 47 | + net = create_empty_network() |
| 48 | + create_bus(net, vn_kv=110.0) # index 0 |
| 49 | + create_bus(net, vn_kv=110.0) # index 1 |
| 50 | + create_bus(net, vn_kv=110.0, index=42) |
| 51 | + create_bus_dc(net, vn_kv=110.0) # index 0 |
| 52 | + create_bus_dc(net, vn_kv=110.0) # index 1 |
| 53 | + create_bus_dc(net, vn_kv=110.0, index=42) |
| 54 | + |
| 55 | + create_vsc_stacked( |
| 56 | + net, |
| 57 | + bus=0, |
| 58 | + bus_dc_plus=0, |
| 59 | + bus_dc_minus=1, |
| 60 | + r_ohm=0.1, |
| 61 | + x_ohm=0.2, |
| 62 | + r_dc_ohm=0.05, |
| 63 | + pl_dc_mw=0.0, |
| 64 | + control_mode_ac="vm_pu", |
| 65 | + control_value_ac=1.0, |
| 66 | + control_mode_dc="p_mw", |
| 67 | + control_value_dc=0.0, |
| 68 | + controllable=True, |
| 69 | + in_service=True, |
| 70 | + name="test", |
| 71 | + ) |
| 72 | + |
| 73 | + net.vsc_stacked[parameter] = valid_value |
| 74 | + validate_network(net) |
| 75 | + |
| 76 | + @pytest.mark.parametrize( |
| 77 | + "parameter,invalid_value", |
| 78 | + list( |
| 79 | + itertools.chain( |
| 80 | + itertools.product(["bus"], [*negativ_ints, *not_ints_list]), |
| 81 | + itertools.product(["bus_dc_plus"], [*negativ_ints, *not_ints_list]), |
| 82 | + itertools.product(["bus_dc_minus"], [*negativ_ints, *not_ints_list]), |
| 83 | + itertools.product(["r_ohm"], not_floats_list), |
| 84 | + itertools.product(["x_ohm"], not_floats_list), |
| 85 | + itertools.product(["r_dc_ohm"], not_floats_list), |
| 86 | + itertools.product(["pl_dc_mw"], not_floats_list), |
| 87 | + itertools.product(["control_mode_ac"], [*strings, *not_strings_list]), |
| 88 | + itertools.product(["control_value_ac"], not_floats_list), |
| 89 | + itertools.product(["control_mode_dc"], [*strings, *not_strings_list]), |
| 90 | + itertools.product(["control_value_dc"], not_floats_list), |
| 91 | + itertools.product(["controllable"], not_boolean_list), |
| 92 | + itertools.product(["in_service"], not_boolean_list), |
| 93 | + ) |
| 94 | + ), |
| 95 | + ) |
| 96 | + def test_invalid_required_values(self, parameter, invalid_value): |
| 97 | + """Test: invalid required values are rejected""" |
| 98 | + net = create_empty_network() |
| 99 | + create_bus(net, 110.0) # index 0 |
| 100 | + create_bus(net, 110.0) # index 1 |
| 101 | + create_bus_dc(net, vn_kv=110.0) # index 0 |
| 102 | + create_bus_dc(net, vn_kv=110.0) # index 1 |
| 103 | + create_bus_dc(net, vn_kv=110.0, index=42) |
| 104 | + create_vsc_stacked( |
| 105 | + net, |
| 106 | + bus=0, |
| 107 | + bus_dc_plus=0, |
| 108 | + bus_dc_minus=1, |
| 109 | + r_ohm=0.1, |
| 110 | + x_ohm=0.2, |
| 111 | + r_dc_ohm=0.05, |
| 112 | + pl_dc_mw=0.0, |
| 113 | + control_mode_ac="vm_pu", |
| 114 | + control_value_ac=1.0, |
| 115 | + control_mode_dc="p_mw", |
| 116 | + control_value_dc=0.0, |
| 117 | + controllable=True, |
| 118 | + in_service=True, |
| 119 | + ) |
| 120 | + |
| 121 | + net.vsc_stacked[parameter] = invalid_value |
| 122 | + with pytest.raises(pa.errors.SchemaError): |
| 123 | + validate_network(net) |
| 124 | + |
| 125 | + |
| 126 | +class TestVSCSTACKEDOptionalFields: |
| 127 | + """Tests for optional vsc_stacked fields""" |
| 128 | + |
| 129 | + @pytest.mark.parametrize( |
| 130 | + "parameter,valid_value", |
| 131 | + list( |
| 132 | + itertools.chain( |
| 133 | + itertools.product(["name"], [pd.NA, *strings]), |
| 134 | + ) |
| 135 | + ), |
| 136 | + ) |
| 137 | + def test_valid_optional_values(self, parameter, valid_value): |
| 138 | + """Test: valid optional values are accepted""" |
| 139 | + net = create_empty_network() |
| 140 | + b0 = create_bus(net, 110.0) |
| 141 | + create_bus_dc(net, vn_kv=110.0) # index 0 |
| 142 | + create_bus_dc(net, vn_kv=110.0) # index 1 |
| 143 | + create_vsc_stacked( |
| 144 | + net, |
| 145 | + bus=b0, |
| 146 | + bus_dc_plus=0, |
| 147 | + bus_dc_minus=1, |
| 148 | + r_ohm=0.1, |
| 149 | + x_ohm=0.2, |
| 150 | + r_dc_ohm=0.05, |
| 151 | + pl_dc_mw=0.0, |
| 152 | + control_mode_ac="vm_pu", |
| 153 | + control_value_ac=1.0, |
| 154 | + control_mode_dc="p_mw", |
| 155 | + control_value_dc=0.0, |
| 156 | + controllable=True, |
| 157 | + in_service=True, |
| 158 | + name="initial", |
| 159 | + ) |
| 160 | + |
| 161 | + net.vsc_stacked[parameter] = pd.Series([valid_value], dtype=pd.StringDtype()) |
| 162 | + validate_network(net) |
| 163 | + |
| 164 | + @pytest.mark.parametrize( |
| 165 | + "parameter,invalid_value", |
| 166 | + list( |
| 167 | + itertools.chain( |
| 168 | + itertools.product(["name"], not_strings_list), |
| 169 | + ) |
| 170 | + ), |
| 171 | + ) |
| 172 | + def test_invalid_optional_values(self, parameter, invalid_value): |
| 173 | + """Test: invalid optional values are rejected""" |
| 174 | + net = create_empty_network() |
| 175 | + b0 = create_bus(net, 110.0) |
| 176 | + create_bus_dc(net, vn_kv=110.0) # index 0 |
| 177 | + create_bus_dc(net, vn_kv=110.0) # index 1 |
| 178 | + create_vsc_stacked( |
| 179 | + net, |
| 180 | + bus=b0, |
| 181 | + bus_dc_plus=0, |
| 182 | + bus_dc_minus=1, |
| 183 | + r_ohm=0.1, |
| 184 | + x_ohm=0.2, |
| 185 | + r_dc_ohm=0.05, |
| 186 | + pl_dc_mw=0.0, |
| 187 | + control_mode_ac="vm_pu", |
| 188 | + control_value_ac=1.0, |
| 189 | + control_mode_dc="p_mw", |
| 190 | + control_value_dc=0.0, |
| 191 | + controllable=True, |
| 192 | + in_service=True, |
| 193 | + ) |
| 194 | + |
| 195 | + net.vsc_stacked[parameter] = invalid_value |
| 196 | + with pytest.raises(pa.errors.SchemaError): |
| 197 | + validate_network(net) |
| 198 | + |
| 199 | + |
| 200 | +class TestVSCSTACKEDSchemaForeignKey: |
| 201 | + """Tests for foreign key constraints""" |
| 202 | + |
| 203 | + def test_invalid_bus_index(self): |
| 204 | + """Test: bus FK must reference an existing bus index""" |
| 205 | + net = create_empty_network() |
| 206 | + b0 = create_bus(net, 110.0) |
| 207 | + create_bus_dc(net, vn_kv=110.0) # index 0 |
| 208 | + create_bus_dc(net, vn_kv=110.0) # index 1 |
| 209 | + create_vsc_stacked( |
| 210 | + net, |
| 211 | + bus=b0, |
| 212 | + bus_dc_plus=0, |
| 213 | + bus_dc_minus=1, |
| 214 | + r_ohm=0.1, |
| 215 | + x_ohm=0.2, |
| 216 | + r_dc_ohm=0.05, |
| 217 | + pl_dc_mw=0.0, |
| 218 | + control_mode_ac="vm_pu", |
| 219 | + control_value_ac=1.0, |
| 220 | + control_mode_dc="p_mw", |
| 221 | + control_value_dc=0.0, |
| 222 | + controllable=True, |
| 223 | + in_service=True, |
| 224 | + ) |
| 225 | + |
| 226 | + net.vsc_stacked["bus"] = 9999 |
| 227 | + with pytest.raises(pa.errors.SchemaError): |
| 228 | + validate_network(net) |
| 229 | + |
| 230 | + def test_invalid_bus_dc_plus_index(self): |
| 231 | + """Test: bus_dc_plus FK must reference an existing dc bus index""" |
| 232 | + net = create_empty_network() |
| 233 | + b0 = create_bus(net, 110.0) |
| 234 | + create_bus_dc(net, vn_kv=110.0) # index 0 |
| 235 | + create_bus_dc(net, vn_kv=110.0) # index 1 |
| 236 | + create_vsc_stacked( |
| 237 | + net, |
| 238 | + bus=b0, |
| 239 | + bus_dc_plus=0, |
| 240 | + bus_dc_minus=1, |
| 241 | + r_ohm=0.1, |
| 242 | + x_ohm=0.2, |
| 243 | + r_dc_ohm=0.05, |
| 244 | + pl_dc_mw=0.0, |
| 245 | + control_mode_ac="vm_pu", |
| 246 | + control_value_ac=1.0, |
| 247 | + control_mode_dc="p_mw", |
| 248 | + control_value_dc=0.0, |
| 249 | + controllable=True, |
| 250 | + in_service=True, |
| 251 | + ) |
| 252 | + |
| 253 | + net.vsc_stacked["bus_dc_plus"] = 9999 |
| 254 | + with pytest.raises(pa.errors.SchemaError): |
| 255 | + validate_network(net) |
| 256 | + |
| 257 | + def test_invalid_bus_dc_minus_index(self): |
| 258 | + """Test: bus_dc_minus FK must reference an existing dc bus index""" |
| 259 | + net = create_empty_network() |
| 260 | + b0 = create_bus(net, 110.0) |
| 261 | + create_bus_dc(net, vn_kv=110.0) # index 0 |
| 262 | + create_bus_dc(net, vn_kv=110.0) # index 1 |
| 263 | + create_vsc_stacked( |
| 264 | + net, |
| 265 | + bus=b0, |
| 266 | + bus_dc_plus=0, |
| 267 | + bus_dc_minus=1, |
| 268 | + r_ohm=0.1, |
| 269 | + x_ohm=0.2, |
| 270 | + r_dc_ohm=0.05, |
| 271 | + pl_dc_mw=0.0, |
| 272 | + control_mode_ac="vm_pu", |
| 273 | + control_value_ac=1.0, |
| 274 | + control_mode_dc="p_mw", |
| 275 | + control_value_dc=0.0, |
| 276 | + controllable=True, |
| 277 | + in_service=True, |
| 278 | + ) |
| 279 | + |
| 280 | + net.vsc_stacked["bus_dc_minus"] = 9999 |
| 281 | + with pytest.raises(pa.errors.SchemaError): |
| 282 | + validate_network(net) |
| 283 | + |
| 284 | + |
| 285 | +class TestVCSSTACKEDResults: |
| 286 | + """Tests for vsc_stacked results after calculations""" |
| 287 | + |
| 288 | + @pytest.mark.skip(reason="Not yet implemented") |
| 289 | + def test_vsc_stacked_result_totals(self): |
| 290 | + """Test: aggregated p_mw / q_mvar results are consistent""" |
| 291 | + pass |
| 292 | + |
| 293 | + @pytest.mark.skip(reason="Not yet implemented") |
| 294 | + def test_vsc_stacked_internal_results(self): |
| 295 | + """Test: internal vm/va and dc quantities are within expected ranges""" |
| 296 | + pass |
0 commit comments