|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: MPL-2.0 |
4 | 4 |
|
5 | | -from copy import copy |
| 5 | +from copy import copy, deepcopy |
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import pytest |
@@ -176,6 +176,37 @@ def test_copy_model(model: PowerGridModel, sym_output): |
176 | 176 | compare_result(result, sym_output, rtol=0.0, atol=1e-8) |
177 | 177 |
|
178 | 178 |
|
| 179 | +def test_deepcopy_model(model: PowerGridModel, sym_output, update_batch, sym_output_batch): |
| 180 | + model_other = PowerGridModel({}) |
| 181 | + |
| 182 | + # list containing different models twice |
| 183 | + model_list = [model, model_other, model, model_other] |
| 184 | + |
| 185 | + new_model_list = deepcopy(model_list) |
| 186 | + |
| 187 | + # check if identities are as expected |
| 188 | + assert id(new_model_list[0]) != id(model_list[0]) |
| 189 | + assert id(new_model_list[1]) != id(model_list[1]) |
| 190 | + assert id(new_model_list[0]) != id(new_model_list[1]) |
| 191 | + assert id(new_model_list[0]) == id(new_model_list[2]) |
| 192 | + assert id(new_model_list[1]) == id(new_model_list[3]) |
| 193 | + |
| 194 | + # check if the deepcopied objects are really independent from the original ones |
| 195 | + # by modifying the copies and seeing if the original one is impacted by this change |
| 196 | + new_model_list[0].update(update_data=get_dataset_scenario(update_batch, 0)) |
| 197 | + |
| 198 | + new_expected_result = get_dataset_scenario(sym_output_batch, 0) |
| 199 | + new_result_0 = new_model_list[0].calculate_power_flow() |
| 200 | + compare_result(new_result_0, new_expected_result, rtol=0.0, atol=1e-8) |
| 201 | + # at index 0 and 2 should be the same objects, check if changing the object at index 0 |
| 202 | + # and obtaining a power flow result is ident to the result at index 2 |
| 203 | + new_result_2 = new_model_list[2].calculate_power_flow() |
| 204 | + compare_result(new_result_2, new_expected_result, rtol=0.0, atol=1e-8) |
| 205 | + |
| 206 | + result = model.calculate_power_flow() |
| 207 | + compare_result(result, sym_output, rtol=0.0, atol=1e-8) |
| 208 | + |
| 209 | + |
179 | 210 | def test_get_indexer(model: PowerGridModel): |
180 | 211 | ids = np.array([2, 2]) |
181 | 212 | expected_indexer = np.array([0, 0]) |
|
0 commit comments