From 3d6179fa32a1b07d2fc860158c77bb550da0ee73 Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 17 Jan 2023 02:43:02 +0000 Subject: [PATCH] remove try/except for class-specific 'deepcopy' --- openmc/universe.py | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/openmc/universe.py b/openmc/universe.py index 15bea0ba9..8babddfeb 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -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 \ No newline at end of file