Skip to content

Commit 9433f1b

Browse files
Merge pull request #1187 from kornerc/deepcopy
implement `__deepcopy__`
2 parents a9e3d3c + 45223de commit 9433f1b

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/power_grid_model/_core/power_grid_model.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
from enum import IntEnum
10-
from typing import overload
10+
from typing import Any, overload
1111

1212
import numpy as np
1313

@@ -108,9 +108,18 @@ def copy(self) -> "PowerGridModel":
108108
new_model._all_component_count = self._all_component_count
109109
return new_model
110110

111-
def __copy__(self):
111+
def __copy__(self) -> "PowerGridModel":
112112
return self.copy()
113113

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+
114123
def __new__(cls, *_args, **_kwargs):
115124
instance = super().__new__(cls)
116125
instance._model_ptr = ModelPtr()

tests/unit/test_power_grid_model.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: MPL-2.0
44

5-
from copy import copy
5+
from copy import copy, deepcopy
66

77
import numpy as np
88
import pytest
@@ -176,6 +176,37 @@ def test_copy_model(model: PowerGridModel, sym_output):
176176
compare_result(result, sym_output, rtol=0.0, atol=1e-8)
177177

178178

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+
179210
def test_get_indexer(model: PowerGridModel):
180211
ids = np.array([2, 2])
181212
expected_indexer = np.array([0, 0])

0 commit comments

Comments
 (0)