remove try/except for class-specific 'deepcopy'

This commit is contained in:
josh 2023-01-17 02:43:02 +00:00
parent 06e6d5d347
commit 3d6179fa32

View file

@ -1,6 +1,7 @@
from abc import ABC, abstractmethod
from collections import OrderedDict
from collections.abc import Iterable
from copy import deepcopy
from numbers import Integral, Real
from pathlib import Path
from tempfile import TemporaryDirectory
@ -110,6 +111,12 @@ class UniverseBase(ABC, IDManagerMixin):
"""
def _deepcopy_universe_for_clone(self):
"""Deepcopy an openmc.UniverseBase object. This is a paceholder for any future classes inherited from this one.
This should only to be used within the openmc.UniverseBase.clone() context.
"""
return deepcopy(self)
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.
@ -137,14 +144,7 @@ class UniverseBase(ABC, IDManagerMixin):
# If no memoize'd clone exists, instantiate one
if self not in memo:
clone = openmc.Universe(name=self.name)
clone.volume = self.volume
# Try to set DAGMCUniverse-specific attributes on the clone
try:
clone.auto_geom_ids = self.auto_geom_ids
clone.auto_mat_ids = self.auto_mat_ids
except AttributeError:
pass
clone = self._deepcopy_universe_for_clone()
# Clone all cells for the universe clone
clone._cells = OrderedDict()
@ -616,6 +616,14 @@ class Universe(UniverseBase):
if not instances_only:
cell._paths.append(cell_path)
def _deepcopy_universe_for_clone(self):
"""Clone all of the openmc.Universe object's attributes except for its cells, as they will be handled within the clone function.
This should only to be used within the openmc.UniverseBase.clone() context and is more performant than a deepcopy.
"""
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.
@ -952,3 +960,13 @@ class DAGMCUniverse(UniverseBase):
out.auto_mat_ids = bool(elem.get('auto_mat_ids'))
return out
def _deepcopy_universe_for_clone(self):
"""Clone all of the openmc.DAGMCUniverse object's attributes except for its cells, as they will be handled within the clone function.
This should only to be used within the openmc.UniverseBase.clone() context and is more performant than a deepcopy.
"""
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