diff --git a/openmc/cell.py b/openmc/cell.py index ca5798773..7948ea912 100644 --- a/openmc/cell.py +++ b/openmc/cell.py @@ -110,9 +110,9 @@ class Cell(object): self._offsets = None self._distribcell_index = None self._distribcell_paths = None + self._paths = [] self._volume = None self._atoms = None - self._num_instances = None def __contains__(self, point): if self.region is None: @@ -233,13 +233,16 @@ class Cell(object): def volume(self): return self._volume + @property + def paths(self): + if not self._paths: + raise ValueError('Cell instance paths have not been determined. ' + 'Call the Geometry.determine_paths() method.') + return self._paths + @property def num_instances(self): - if self._num_instances is None: - raise ValueError('The number of cell instances is unknown. Call ' - 'the Geometry.count_cell_instances() method.') - else: - return self._num_instances + return len(self.paths) @id.setter def id(self, cell_id): diff --git a/openmc/geometry.py b/openmc/geometry.py index 408c253e3..77f047e6e 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -450,24 +450,15 @@ class Geometry(object): lattices.sort(key=lambda x: x.id) return lattices - def count_cell_instances(self): + def determine_paths(self): """Count the number of instances for each cell in the Geometry, and record the count in the :attr:`Cell.num_instances` properties.""" # (Re-)initialize all cell instances to 0 for cell in self.get_all_cells().values(): - cell._num_instances = 0 - - # Recursively traverse the CSG tree to count all cell instances - self.root_universe._count_cell_instances() - - def count_material_instances(self): - """Count the number of instances for each material in the Geometry, and - record the count in the :attr:`Material.num_instances` properties.""" - - # (Re-)initialize all material instances to 0 + cell._paths = [] for material in self.get_all_materials().values(): - material._num_instances = 0 + material._paths = [] # Recursively traverse the CSG tree to count all cell instances - self.root_universe._count_material_instances() \ No newline at end of file + self.root_universe._determine_paths() diff --git a/openmc/material.py b/openmc/material.py index 007e01591..5b5f8947c 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -90,9 +90,9 @@ class Material(object): self._density = None self._density_units = '' self._depletable = False + self._paths = [] self._volume = None self._atoms = {} - self._num_instances = None # A list of tuples (nuclide, percent, percent type) self._nuclides = [] @@ -200,13 +200,16 @@ class Material(object): def depletable(self): return self._depletable + @property + def paths(self): + if not self._paths: + raise ValueError('Material instance paths have not been determined. ' + 'Call the Geometry.determine_paths() method.') + return self._paths + @property def num_instances(self): - if self._num_instances is None: - raise ValueError('The number of cell instances is unknown. Call ' - 'the Geometry.count_cell_instances() method.') - else: - return self._num_instances + return len(self.paths) @property def elements(self): diff --git a/openmc/universe.py b/openmc/universe.py index 4257c1d15..fd941a224 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -520,17 +520,18 @@ class Universe(object): cell_element.set("universe", str(self._id)) xml_element.append(cell_element) - def _count_cell_instances(self): + def _determine_paths(self, path=''): """Count the number of instances for each cell in the universe, and record the count in the :attr:`Cell.num_instances` properties.""" + univ_path = path + 'u{}'.format(self.id) + for cell in self.cells.values(): - # Increment the number of cell instances - cell._num_instances += 1 + cell_path = '{}->c{}'.format(univ_path, cell.id) # If universe-filled, recursively count cells in filling universe if cell.fill_type == 'universe': - cell.fill._count_cell_instances() + cell.fill._determine_paths(cell_path + '->') # If lattice-filled, recursively call for all universes in lattice elif cell.fill_type == 'lattice': @@ -538,34 +539,22 @@ class Universe(object): # Count instances in each universe in the lattice for index in latt.indices: + latt_path = '{}->l{}{}->'.format(cell_path, latt.id, index) if latt.ndim == 3: univ = latt.universes[index[0]][index[1]][index[2]] else: univ = latt.universes[index[0]][index[1]] - univ._count_cell_instances() + univ._determine_paths(latt_path) - def _count_material_instances(self): - """Count the number of instances for each material in the Universe, and - record the count in the :attr:`Material.num_instances` properties.""" + else: + if cell.fill_type == 'material': + mat = cell.fill + elif cell.fill_type == 'distribmat': + mat = cell.fill[len(cell._paths)] + else: + mat = None - for cell in self.cells.values(): + if mat is not None: + mat._paths.append(cell_path) - # If material-filled, we are finished with all levels - if cell.fill_type == 'material': - cell.fill._num_instances += 1 - - # If universe-filled, recursively count materials in filling universe - elif cell.fill_type == 'universe': - cell.fill._count_material_instances() - - # If lattice-filled, recursively call for all universes in lattice - elif cell.fill_type == 'lattice': - latt = cell.fill - - # Count instances in each universe in the lattice - for index in latt.indices: - if latt.ndim == 3: - univ = latt.universes[index[0]][index[1]][index[2]] - else: - univ = latt.universes[index[0]][index[1]] - univ._count_material_instances() + cell._paths.append(univ_path)