From ddb72466c751fed45291e7965232b2bf135112be Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Thu, 2 Mar 2017 12:11:27 -0500 Subject: [PATCH] Cell and material counting now works for PyAPI with HexLattice --- openmc/geometry.py | 140 ++++++++++++++++----------------------------- openmc/lattice.py | 19 +++--- 2 files changed, 60 insertions(+), 99 deletions(-) diff --git a/openmc/geometry.py b/openmc/geometry.py index 1dc8f6a4b..22c4307bd 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -450,22 +450,30 @@ class Geometry(object): lattices.sort(key=lambda x: x.id) return lattices - - # FIXME: def count_cell_instances(self, universe=None): + """Count the number of instances for each cell in the Geometry, and + record the count in the :attr:`Cell.num_instances` properties. + Parameters + ---------- + universe : openmc.Universe or None + The Universe to use at each recursive call of this method. + This parameter defaults to None and should not be set + by the user. + + """ if universe is None: universe = self.root_universe + # (Re-)initialize all cell instances to 0 + for cell in self.get_all_cells().values(): + cell.num_instances = 0 + if universe is None: raise RuntimeError( 'Unable to count cell instances without a root universe') - # (Re-)initialize all cell instances to 0 - for cell in self.get_all_cells().values(): - cell.num_instances = 0 - for cell in universe.cells.values(): # Increment the number of cell instances cell.num_instances += 1 @@ -482,63 +490,42 @@ class Geometry(object): elif cell.fill_type == 'lattice': latt = cell.fill - if isinstance(latt, openmc.RectLattice): - # 3D Lattices + # Extract a list of tuples of the valid indices + # into this Lattice's universes array + valid_indices = latt.indices + + # Count instances in each Universe in the Lattice + for index in valid_indices: if latt.ndim == 3: - for j in range(latt.shape[2]): - for k in range(latt.shape[1]): - for m in range(latt.shape[0]): - univ = latt.universes[j][k][m] - self.count_cell_instances(univ) - # 2D Lattices + univ = latt.universes[index[0]][index[1]][index[2]] else: - for k in range(latt.shape[1]): - for m in range(latt.shape[0]): - univ = latt.universes[k][m] - self.count_cell_instances(univ) + univ = latt.universes[index[0]][index[1]] + self.count_cell_instances(univ) - # FIXME: Figure out how to iterate over lattices - elif isinstance(latt, openmc.HexLattice): - - # 3D Lattices - if latt.ndim == 3: - for m in range(latt.num_axial): - for k in range(2*latt.num_rings-1): - for j in range(2*latt.num_rings-1): - if j + k < latt.num_rings + 1: - continue - elif j + k > 3*latt.num_rings - 1: - continue - else: - univ = latt.universes[j][k][m] - self.count_cell_instances(univ) - - # 2D Lattices - else: - for k in range(2*latt.num_rings-1): - for j in range(2*latt.num_rings-1): - if j + k < latt.num_rings + 1: - continue - elif j + k > 3*latt.num_rings - 1: - continue - else: - univ = latt.universes[j][k] - self.count_cell_instances(univ) - - - # FIXME: def count_material_instances(self, universe=None): + """Count the number of instances for each material in the Geometry, and + record the count in the :attr:`Material.num_instances` properties. + + Parameters + ---------- + universe : openmc.Universe or None + The Universe to use at each recursive call of this method. + This parameter defaults to None and should not be set + by the user. + + """ + if universe is None: universe = self.root_universe + # (Re-)initialize all material instances to 0 + for material in self.get_all_materials().values(): + material.num_instances = 0 + if universe is None: raise RuntimeError( 'Unable to count material instances without a root universe') - # (Re-)initialize all material instances to 0 - for material in self.get_all_materials().values(): - material.num_instances = 0 - for cell in universe.cells.values(): # If material-filled, we are finished with all levels @@ -553,45 +540,14 @@ class Geometry(object): elif cell.fill_type == 'lattice': latt = cell.fill - if isinstance(latt, openmc.RectLattice): - # 3D Lattices + # Extract a list of tuples of the valid indices + # into this Lattice's universes array + valid_indices = latt.indices + + # Count instances in each Universe in the Lattice + for index in valid_indices: if latt.ndim == 3: - for j in range(latt.shape[2]): - for k in range(latt.shape[1]): - for m in range(latt.shape[0]): - univ = latt.universes[j][k][m] - self.count_material_instances(univ) - # 2D Lattices + univ = latt.universes[index[0]][index[1]][index[2]] else: - for k in range(latt.shape[1]): - for m in range(latt.shape[0]): - univ = latt.universes[k][m] - self.count_material_instances(univ) - - # FIXME: Figure out how to iterate over hexagonal lattices - elif isinstance(latt, openmc.HexLattice): - - # 3D Lattices - if latt.ndim == 3: - for m in range(latt.num_axial): - for k in range(2 * latt.num_rings - 1): - for j in range(2 * latt.num_rings - 1): - if j + k < latt.num_rings + 1: - continue - elif j + k > 3 * latt.num_rings - 1: - continue - else: - univ = latt.universes[j][k][m] - self.count_material_instances(univ) - - # 2D Lattices - else: - for k in range(2 * latt.num_rings - 1): - for j in range(2 * latt.num_rings - 1): - if j + k < latt.num_rings + 1: - continue - elif j + k > 3 * latt.num_rings - 1: - continue - else: - univ = latt.universes[j][k] - self.count_material_instances(univ) \ No newline at end of file + univ = latt.universes[index[0]][index[1]] + self.count_material_instances(univ) diff --git a/openmc/lattice.py b/openmc/lattice.py index 23838881e..cba1cdece 100644 --- a/openmc/lattice.py +++ b/openmc/lattice.py @@ -922,6 +922,13 @@ class HexLattice(Lattice): for r in range(self.num_rings) for i in range(max(6*(self.num_rings - 1 - r), 1))] + @property + def ndim(self): + if isinstance(self.universes[0][0], openmc.Universe): + return 2 + else: + return 3 + @center.setter def center(self, center): cv.check_type('lattice center', center, Iterable, Real) @@ -950,11 +957,9 @@ class HexLattice(Lattice): # Check to see if the given universes look like a 2D or a 3D array. if isinstance(self._universes[0][0], openmc.Universe): - n_dims = 2 - + pass elif isinstance(self._universes[0][0][0], openmc.Universe): - n_dims = 3 - + pass else: msg = 'HexLattice ID={0:d} does not appear to be either 2D or ' \ '3D. Make sure set_universes was given a two-deep or ' \ @@ -962,14 +967,14 @@ class HexLattice(Lattice): raise RuntimeError(msg) # Set the number of axial positions. - if n_dims == 3: + if self.ndim == 3: self._num_axial = len(self._universes) else: self._num_axial = None # Set the number of rings and make sure this number is consistent for # all axial positions. - if n_dims == 3: + if self.ndim == 3: self._num_rings = len(self._universes[0]) for rings in self._universes: if len(rings) != self._num_rings: @@ -981,7 +986,7 @@ class HexLattice(Lattice): self._num_rings = len(self._universes) # Make sure there are the correct number of elements in each ring. - if n_dims == 3: + if self.ndim == 3: for axial_slice in self._universes: # Check the center ring. if len(axial_slice[-1]) != 1: