From ac2ba93b3a573353a6e2fd051a1a35581f32c257 Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 4 Jan 2023 03:31:07 +0000 Subject: [PATCH] remove cell deepcopying for creating a fresh instance --- openmc/cell.py | 11 ++++++++--- tests/unit_tests/test_cell.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/openmc/cell.py b/openmc/cell.py index 2aa538012f..8169ca4d24 100644 --- a/openmc/cell.py +++ b/openmc/cell.py @@ -1,6 +1,5 @@ from collections import OrderedDict from collections.abc import Iterable -from copy import deepcopy from math import cos, sin, pi from numbers import Real from xml.etree import ElementTree as ET @@ -519,8 +518,14 @@ class Cell(IDManagerMixin): paths = self._paths self._paths = None - clone = deepcopy(self) - clone.id = None + clone = openmc.Cell() + clone.name = self.name + clone.temperature = self.temperature + clone.volume = self.volume + if self.translation is not None: + clone.translation = self.translation + if self.rotation is not None: + clone.rotation = self.rotation clone._num_instances = None # Restore paths on original instance diff --git a/tests/unit_tests/test_cell.py b/tests/unit_tests/test_cell.py index 1c2e1b70e4..eebe0f895c 100644 --- a/tests/unit_tests/test_cell.py +++ b/tests/unit_tests/test_cell.py @@ -58,24 +58,54 @@ def test_clone(): cyl = openmc.ZCylinder() c = openmc.Cell(fill=m, region=-cyl) c.temperature = 650. + c.translation = (1,2,3) + c.rotation = (4,5,6) + c.volume = 100 c2 = c.clone() assert c2.id != c.id assert c2.fill != c.fill assert c2.region != c.region assert c2.temperature == c.temperature + assert all(c2.translation == c.translation) + assert all(c2.rotation == c.rotation) + assert c2.volume == c.volume c3 = c.clone(clone_materials=False) assert c3.id != c.id assert c3.fill == c.fill assert c3.region != c.region assert c3.temperature == c.temperature + assert all(c2.translation == c.translation) + assert all(c2.rotation == c.rotation) c4 = c.clone(clone_regions=False) assert c4.id != c.id assert c4.fill != c.fill assert c4.region == c.region assert c4.temperature == c.temperature + assert all(c2.translation == c.translation) + assert all(c2.rotation == c.rotation) + + c5 = c.clone(clone_materials=False, clone_regions=False) + assert c5.id != c.id + assert c5.fill == c.fill + assert c5.region == c.region + assert c5.temperature == c.temperature + assert all(c2.translation == c.translation) + assert all(c2.rotation == c.rotation) + + # Mutate the original to ensure the changes are not seen in the clones + c.fill = openmc.Material() + c.region = +openmc.ZCylinder() + c.translation = [-1,-2,-3] + c.rotation = [-4,-5,-6] + c.temperature = 1 + assert c5.fill != c.fill + assert c5.region != c.region + assert c5.temperature != c.temperature + assert all(c2.translation != c.translation) + assert all(c2.rotation != c.rotation) def test_temperature(cell_with_lattice):