Added Cell.num_instances property and Geometry.count_cell_instances() method

This commit is contained in:
Will Boyd 2017-03-01 12:57:30 -05:00 committed by Paul Romano
parent 8be6a58603
commit 3bed097f1b
2 changed files with 88 additions and 0 deletions

View file

@ -90,6 +90,10 @@ class Cell(object):
Volume of the cell in cm^3. This can either be set manually or
calculated in a stochastic volume calculation and added via the
:meth:`Cell.add_volume_information` method.
num_instances : int
The number of instances of this cell throughout the geometry. This
property is initialized by calling the
:meth:`Geometry.count_cell_instances()` method.
"""
@ -108,6 +112,7 @@ class Cell(object):
self._distribcell_paths = None
self._volume = None
self._atoms = None
self._num_instances = None
def __contains__(self, point):
if self.region is None:
@ -228,6 +233,10 @@ class Cell(object):
def volume(self):
return self._volume
@property
def num_instances(self):
return self._num_instances
@id.setter
def id(self, cell_id):
if cell_id is None:
@ -344,6 +353,11 @@ class Cell(object):
string_types)
self._distribcell_paths = distribcell_paths
@num_instances.setter
def num_instances(self, num_instances):
cv.check_type('num_instances', num_instances, Integral)
self._num_instances = num_instances
def add_surface(self, surface, halfspace):
"""Add a half-space to the list of half-spaces whose intersection defines the
cell.

View file

@ -449,3 +449,77 @@ class Geometry(object):
lattices = list(lattices)
lattices.sort(key=lambda x: x.id)
return lattices
# FIXME:
def count_cell_instances(self, universe=None):
if universe is None:
universe = self.root_universe
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
# If material-filled, we are finished with all levels
if cell.fill_type == 'material':
continue
# If universe-filled, recursively count cells in filling universe
elif cell.fill_type == 'universe':
self.count_cell_instances(cell.fill)
# If lattice-filled, recursively call for all universes in lattice
elif cell.fill_type == 'lattice':
latt = cell.fill
if isinstance(latt, openmc.RectLattice):
# 3D Lattices
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
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)
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)