diff --git a/openmc/cell.py b/openmc/cell.py index 9993175efd..ba7a41435b 100644 --- a/openmc/cell.py +++ b/openmc/cell.py @@ -504,6 +504,20 @@ class Cell(object): return universes + def clone(self): + """Create a copy of this cell with a new unique ID, and clones + the cell's region and fill.""" + + clone = copy.deepcopy(self) + clone.id = None + + if self.region is not None: + clone.region = self.region.clone() + if self.fill is not None: + clone.fill = self.fill.clone() + + return clone + def create_xml_subelement(self, xml_element): element = ET.Element("cell") element.set("id", str(self.id)) diff --git a/openmc/lattice.py b/openmc/lattice.py index 08ca24a192..9f3f303859 100644 --- a/openmc/lattice.py +++ b/openmc/lattice.py @@ -414,6 +414,25 @@ class Lattice(object): return [] return [(self, idx)] + u.find(p) + def clone(self): + """Create a copy of this lattice with a new unique ID, and clones + all universes within this lattice.""" + + clone = copy.deepcopy(self) + clone.id = None + + # Clone all unique universes in the lattice + univ_clones = self.get_unique_universes() + for univ_id in univ_clones: + univ_clones[univ_id] = univ_clones[univ_id].clone() + + # Assign universe clones to the lattice clone + for index in self.indices: + univ_id = self.universe[index].id + clone.universes[index] = univ_clones[univ_id] + + return clone + class RectLattice(Lattice): """A lattice consisting of rectangular prisms. diff --git a/openmc/material.py b/openmc/material.py index 26b1bce8fc..cb1482b8e0 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -1100,6 +1100,13 @@ class Materials(cv.CheckedList): for material in self: material.make_isotropic_in_lab() + def clone(self): + """Create a copy of this material with a new unique ID.""" + + clone = copy.deepcopy(self) + clone.id = None + return clone + def _create_material_subelements(self, root_element): for material in self: root_element.append(material.to_xml_element(self.cross_sections)) diff --git a/openmc/region.py b/openmc/region.py index a2a92c15a3..1ac4d8c948 100644 --- a/openmc/region.py +++ b/openmc/region.py @@ -221,6 +221,12 @@ class Region(object): # at the end return output[0] + @abstractmethod + def clone(self): + """Create a copy of this region - each of the surfaces in the + region's nodes will be cloned and will have new unique IDs.""" + return False + class Intersection(Region): r"""Intersection of two or more regions. @@ -300,6 +306,15 @@ class Intersection(Region): check_type('nodes', nodes, Iterable, Region) self._nodes = nodes + @abstractmethod + def clone(self): + """Create a copy of this region - each of the surfaces in the + intersection's nodes will be cloned and will have new unique IDs.""" + + clone = copy.deepcopy(self) + clone.nodes = [n.clone() for n in self.nodes] + return clone + class Union(Region): r"""Union of two or more regions. @@ -377,6 +392,14 @@ class Union(Region): check_type('nodes', nodes, Iterable, Region) self._nodes = nodes + def clone(self): + """Create a copy of this region - each of the surfaces in the + union's nodes will be cloned and will have new unique IDs.""" + + clone = copy.deepcopy(self) + clone.nodes = [n.clone() for n in self.nodes] + return clone + class Complement(Region): """Complement of a region. @@ -473,3 +496,11 @@ class Complement(Region): for region in self.node: surfaces = region.get_surfaces(surfaces) return surfaces + + def clone(self): + """Create a copy of this region - each of the surfaces in the + complement's node will be cloned and will have new unique IDs.""" + + clone = copy.deepcopy(self) + clone.node = self.node.clone() + return clone diff --git a/openmc/surface.py b/openmc/surface.py index 9c6be78a77..b0aa250c03 100644 --- a/openmc/surface.py +++ b/openmc/surface.py @@ -171,6 +171,13 @@ class Surface(object): return (np.array([-np.inf, -np.inf, -np.inf]), np.array([np.inf, np.inf, np.inf])) + def clone(self): + """Create a copy of this surface with a new unique ID.""" + + clone = copy.deepcopy(self) + clone.id = None + return clone + def to_xml_element(self): """Return XML representation of the surface @@ -1822,6 +1829,15 @@ class Halfspace(Region): self.surface = surface self.side = side + @abstractmethod + def clone(self): + """Create a copy of this halfspace, with a cloned surface with a + unique ID.""" + + clone = copy.deepcopy(self) + clone.surface = self.surface.clone() + return clone + def __and__(self, other): if isinstance(other, Intersection): return Intersection(self, *other.nodes) diff --git a/openmc/universe.py b/openmc/universe.py index 1c631e7e07..0f5e2da140 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -517,6 +517,20 @@ class Universe(object): return universes + def clone(self): + """Create a copy of this universe with a new unique ID, and clones + all cells within this universe.""" + + clone = copy.deepcopy(self) + clone.id = None + + # Clone all cells for the universe clone + clone._cells = OrderedDict() + for cell in self._cells.values(): + clone.add_cell(cell.clone()) + + return clone + def create_xml_subelement(self, xml_element): # Iterate over all Cells for cell_id, cell in self._cells.items():