mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-28 14:15:42 -04:00
Merge pull request #2352 from joshmay1/universe_cloning
Instantiate new cloned universes instead of deepcopying
This commit is contained in:
commit
89ad9afedf
2 changed files with 57 additions and 2 deletions
|
|
@ -111,6 +111,13 @@ class UniverseBase(ABC, IDManagerMixin):
|
|||
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def _partial_deepcopy(self):
|
||||
"""Deepcopy all parameters of an openmc.UniverseBase object except its cells.
|
||||
This should only be used from the openmc.UniverseBase.clone() context.
|
||||
|
||||
"""
|
||||
|
||||
def clone(self, clone_materials=True, clone_regions=True, memo=None):
|
||||
"""Create a copy of this universe with a new unique ID, and clones
|
||||
all cells within this universe.
|
||||
|
|
@ -138,8 +145,7 @@ class UniverseBase(ABC, IDManagerMixin):
|
|||
|
||||
# If no memoize'd clone exists, instantiate one
|
||||
if self not in memo:
|
||||
clone = deepcopy(self)
|
||||
clone.id = None
|
||||
clone = self._partial_deepcopy()
|
||||
|
||||
# Clone all cells for the universe clone
|
||||
clone._cells = OrderedDict()
|
||||
|
|
@ -611,6 +617,15 @@ class Universe(UniverseBase):
|
|||
if not instances_only:
|
||||
cell._paths.append(cell_path)
|
||||
|
||||
def _partial_deepcopy(self):
|
||||
"""Clone all of the openmc.Universe object's attributes except for its cells,
|
||||
as they are copied within the clone function. This should only to be
|
||||
used within the openmc.UniverseBase.clone() context.
|
||||
"""
|
||||
clone = openmc.Universe(name=self.name)
|
||||
clone.volume = self.volume
|
||||
return clone
|
||||
|
||||
|
||||
class DAGMCUniverse(UniverseBase):
|
||||
"""A reference to a DAGMC file to be used in the model.
|
||||
|
|
@ -947,3 +962,14 @@ class DAGMCUniverse(UniverseBase):
|
|||
out.auto_mat_ids = bool(elem.get('auto_mat_ids'))
|
||||
|
||||
return out
|
||||
|
||||
def _partial_deepcopy(self):
|
||||
"""Clone all of the openmc.DAGMCUniverse object's attributes except for
|
||||
its cells, as they are copied within the clone function. This should
|
||||
only to be used within the openmc.UniverseBase.clone() context.
|
||||
"""
|
||||
clone = openmc.DAGMCUniverse(name=self.name, filename=self.filename)
|
||||
clone.volume = self.volume
|
||||
clone.auto_geom_ids = self.auto_geom_ids
|
||||
clone.auto_mat_ids = self.auto_mat_ids
|
||||
return clone
|
||||
|
|
@ -107,11 +107,13 @@ def test_clone():
|
|||
c2.fill = openmc.Material()
|
||||
c3 = openmc.Cell()
|
||||
u1 = openmc.Universe(name='cool', cells=(c1, c2, c3))
|
||||
u1.volume = 1.
|
||||
|
||||
u2 = u1.clone()
|
||||
assert u2.name == u1.name
|
||||
assert u2.cells != u1.cells
|
||||
assert u2.get_all_materials() != u1.get_all_materials()
|
||||
assert u2.volume == u1.volume
|
||||
|
||||
u2 = u1.clone(clone_materials=False)
|
||||
assert u2.get_all_materials() == u1.get_all_materials()
|
||||
|
|
@ -120,6 +122,33 @@ def test_clone():
|
|||
assert next(iter(u3.cells.values())).region ==\
|
||||
next(iter(u1.cells.values())).region
|
||||
|
||||
# Change attributes, make sure clone stays intact
|
||||
u1.volume = 2.
|
||||
u1.name = "different name"
|
||||
assert u3.volume != u1.volume
|
||||
assert u3.name != u1.name
|
||||
|
||||
# Test cloning a DAGMC universe
|
||||
dagmc_u = openmc.DAGMCUniverse(filename="", name="DAGMC universe")
|
||||
dagmc_u.volume = 1.
|
||||
dagmc_u.auto_geom_ids = True
|
||||
dagmc_u.auto_mat_ids = True
|
||||
dagmc_u1 = dagmc_u.clone()
|
||||
assert dagmc_u1.name == dagmc_u.name
|
||||
assert dagmc_u1.volume == dagmc_u.volume
|
||||
assert dagmc_u1.auto_geom_ids == dagmc_u.auto_geom_ids
|
||||
assert dagmc_u1.auto_mat_ids == dagmc_u.auto_mat_ids
|
||||
|
||||
# Change attributes, check the clone remained intact
|
||||
dagmc_u.name = "another name"
|
||||
dagmc_u.auto_geom_ids = False
|
||||
dagmc_u.auto_mat_ids = False
|
||||
dagmc_u.volume = 2.
|
||||
assert dagmc_u1.name != dagmc_u.name
|
||||
assert dagmc_u1.volume != dagmc_u.volume
|
||||
assert dagmc_u1.auto_geom_ids != dagmc_u.auto_geom_ids
|
||||
assert dagmc_u1.auto_mat_ids != dagmc_u.auto_mat_ids
|
||||
|
||||
|
||||
def test_create_xml(cell_with_lattice):
|
||||
cells = [openmc.Cell() for i in range(5)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue