Make get_all_* methods consistent -- always return dict

This commit is contained in:
Paul Romano 2017-02-23 07:39:28 -06:00
parent eecf45411b
commit 7ca46e809c
11 changed files with 81 additions and 113 deletions

View file

@ -433,7 +433,7 @@ class Cell(object):
Returns
-------
nuclides : dict
nuclides : collections.OrderedDict
Dictionary whose keys are nuclide names and values are 2-tuples of
(nuclide, density)
@ -467,7 +467,7 @@ class Cell(object):
Returns
-------
cells : dict
cells : collections.orderedDict
Dictionary whose keys are cell IDs and values are :class:`Cell`
instances
@ -485,20 +485,23 @@ class Cell(object):
Returns
-------
materials : dict
materials : collections.OrderedDict
Dictionary whose keys are material IDs and values are
:class:`Material` instances
"""
materials = OrderedDict()
if self.fill_type == 'material':
materials[self.fill.id] = self.fill
# Append all Cells in each Cell in the Universe to the dictionary
cells = self.get_all_cells()
for cell in cells.values():
materials.update(cell.get_all_materials())
elif self.fill_type == 'distribmat':
for m in self.fill:
if m is not None:
materials[m.id] = m
else:
# Append all Cells in each Cell in the Universe to the dictionary
cells = self.get_all_cells()
for cell in cells.values():
materials.update(cell.get_all_materials())
return materials
@ -508,7 +511,7 @@ class Cell(object):
Returns
-------
universes : dict
universes : collections.OrderedDict
Dictionary whose keys are universe IDs and values are
:class:`Universe` instances

View file

@ -151,102 +151,80 @@ class Geometry(object):
return offset
def get_all_cells(self):
"""Return all cells defined
"""Return all cells in the geometry.
Returns
-------
list of openmc.Cell
Cells in the geometry
collections.OrderedDict
Dictionary mapping cell IDs to :class:`openmc.Cell` instances
"""
all_cells = self.root_universe.get_all_cells()
cells = list(set(all_cells.values()))
cells.sort(key=lambda x: x.id)
return cells
return self.root_universe.get_all_cells()
def get_all_universes(self):
"""Return all universes defined
"""Return all universes in the geometry.
Returns
-------
list of openmc.Universe
Universes in the geometry
collections.OrderedDict
Dictionary mapping universe IDs to :class:`openmc.Universe`
instances
"""
all_universes = self._root_universe.get_all_universes()
universes = list(set(all_universes.values()))
universes.sort(key=lambda x: x.id)
universes = OrderedDict()
universes[self.root_universe.id] = self.root_universe
universes.update(self.root_universe.get_all_universes())
return universes
def get_all_materials(self):
"""Return all materials assigned to a cell
"""Return all materials within the geometry.
Returns
-------
list of openmc.Material
Materials in the geometry
collections.OrderedDict
Dictionary mapping material IDs to :class:`openmc.Material`
instances
"""
material_cells = self.get_all_material_cells()
materials = []
for cell in material_cells:
if cell.fill_type == 'distribmat':
for m in cell.fill:
if m is not None and m not in materials:
materials.append(m)
elif cell.fill_type == 'material':
if cell.fill not in materials:
materials.append(cell.fill)
materials.sort(key=lambda x: x.id)
return materials
return self.root_universe.get_all_materials()
def get_all_material_cells(self):
"""Return all cells filled by a material
Returns
-------
list of openmc.Cell
Cells filled by Materials in the geometry
collections.OrderedDict
Dictionary mapping cell IDs to :class:`openmc.Cell` instances that
are filled with materials or distributed materials.
"""
material_cells = OrderedDict()
all_cells = self.get_all_cells()
material_cells = []
for cell in all_cells:
for cell in self.get_all_cells().values():
if cell.fill_type in ('material', 'distribmat'):
if cell not in material_cells:
material_cells.append(cell)
material_cells[cell.id] = cell
material_cells.sort(key=lambda x: x.id)
return material_cells
def get_all_material_universes(self):
"""Return all universes composed of at least one non-fill cell
"""Return all universes having at least one non-fill cell
Returns
-------
list of openmc.Universe
Universes with non-fill cells
collections.OrderedDict
Dictionary mapping universe IDs to :class:`openmc.Universe`
instances with non-fill cells
"""
material_universes = OrderedDict()
all_universes = self.get_all_universes()
material_universes = []
for universe in all_universes:
cells = universe.cells
for cell in cells:
for universe in self.get_all_universes():
for cell in universe.cells:
if cell.fill_type in ('material', 'distribmat', 'void'):
if universe not in material_universes:
material_universes.append(universe)
material_universes[universe.id] = universe
material_universes.sort(key=lambda x: x.id)
return material_universes
def get_all_lattices(self):
@ -254,20 +232,17 @@ class Geometry(object):
Returns
-------
list of openmc.Lattice
Lattices in the geometry
collections.OrderedDict
Dictionary mapping lattice IDs to :class:`openmc.Lattice` instances
"""
lattices = OrderedDict()
cells = self.get_all_cells()
lattices = []
for cell in cells:
for cell in self.get_all_cells():
if cell.fill_type == 'lattice':
if cell.fill not in lattices:
lattices.append(cell.fill)
lattices[cell.fill.id] = cell.fill
lattices.sort(key=lambda x: x.id)
return lattices
def get_materials_by_name(self, name, case_sensitive=False, matching=False):
@ -293,7 +268,7 @@ class Geometry(object):
if not case_sensitive:
name = name.lower()
all_materials = self.get_all_materials()
all_materials = self.get_all_materials().values()
materials = set()
for material in all_materials:
@ -333,7 +308,7 @@ class Geometry(object):
if not case_sensitive:
name = name.lower()
all_cells = self.get_all_cells()
all_cells = self.get_all_cells().values()
cells = set()
for cell in all_cells:
@ -373,7 +348,7 @@ class Geometry(object):
if not case_sensitive:
name = name.lower()
all_cells = self.get_all_cells()
all_cells = self.get_all_cells().values()
cells = set()
for cell in all_cells:
@ -413,7 +388,7 @@ class Geometry(object):
if not case_sensitive:
name = name.lower()
all_universes = self.get_all_universes()
all_universes = self.get_all_universes().values()
universes = set()
for universe in all_universes:
@ -453,7 +428,7 @@ class Geometry(object):
if not case_sensitive:
name = name.lower()
all_lattices = self.get_all_lattices()
all_lattices = self.get_all_lattices().values()
lattices = set()
for lattice in all_lattices:

View file

@ -192,11 +192,11 @@ class Library(object):
def domains(self):
if self._domains == 'all':
if self.domain_type == 'material':
return self.geometry.get_all_materials()
return list(self.geometry.get_all_materials().values())
elif self.domain_type in ['cell', 'distribcell']:
return self.geometry.get_all_material_cells()
return list(self.geometry.get_all_material_cells().values())
elif self.domain_type == 'universe':
return self.geometry.get_all_universes()
return list(self.geometry.get_all_universes().values())
elif self.domain_type == 'mesh':
raise ValueError('Unable to get domains for Mesh domain type')
else:
@ -316,13 +316,13 @@ class Library(object):
else:
if self.domain_type == 'material':
cv.check_iterable_type('domain', domains, openmc.Material)
all_domains = self.geometry.get_all_materials()
all_domains = self.geometry.get_all_materials().values()
elif self.domain_type in ['cell', 'distribcell']:
cv.check_iterable_type('domain', domains, openmc.Cell)
all_domains = self.geometry.get_all_material_cells()
all_domains = self.geometry.get_all_material_cells().values()
elif self.domain_type == 'universe':
cv.check_iterable_type('domain', domains, openmc.Universe)
all_domains = self.geometry.get_all_universes()
all_domains = self.geometry.get_all_universes().values()
elif self.domain_type == 'mesh':
cv.check_iterable_type('domain', domains, openmc.Mesh)
@ -1342,7 +1342,7 @@ class Library(object):
materials = openmc.Materials()
# Get all Cells from the Geometry for differentiation
all_cells = geometry.get_all_material_cells()
all_cells = geometry.get_all_material_cells().values()
# Create the xsdata object and add it to the mgxs_file
for i, domain in enumerate(self.domains):

View file

@ -894,7 +894,7 @@ class MGXS(object):
"""
cv.check_type('statepoint', statepoint, openmc.statepoint.StatePoint)
cv.check_type('statepoint', statepoint, openmc.StatePoint)
if statepoint.summary is None:
msg = 'Unable to load data from a statepoint which has not been ' \
@ -904,16 +904,13 @@ class MGXS(object):
# Override the domain object that loaded from an OpenMC summary file
# NOTE: This is necessary for micro cross-sections which require
# the isotopic number densities as computed by OpenMC
su = statepoint.summary
if self.domain_type == 'cell' or self.domain_type == 'distribcell':
cells = {c.id: c for c in su.geometry.get_all_cells()}
self.domain = cells[self.domain.id]
geom = statepoint.summary.geometry
if self.domain_type in ('cell', 'distribcell'):
self.domain = geom.get_all_cells()[self.domain.id]
elif self.domain_type == 'universe':
universes = {u.id: u for u in su.geometry.get_all_universes()}
self.domain = universes[self.domain.id]
self.domain = geom.get_all_universes()[self.domain.id]
elif self.domain_type == 'material':
mats = {m.id: m for m in su.materials}
self.domain = mats[self.domain.id]
self.domain = geom.get_all_materials()[self.domain.id]
elif self.domain_type == 'mesh':
self.domain = statepoint.meshes[self.domain.id]
else:

View file

@ -351,11 +351,11 @@ class Plot(object):
# Generate random colors for each feature
self.col_spec = {}
for domain in domains:
for domain_id in domains:
r = np.random.randint(0, 256)
g = np.random.randint(0, 256)
b = np.random.randint(0, 256)
self.col_spec[domain] = (r, g, b)
self.col_spec[domain_id] = (r, g, b)
def highlight_domains(self, geometry, domains, seed=1,
alpha=0.5, background='gray'):

View file

@ -640,7 +640,7 @@ class StatePoint(object):
'is not a Summary object'.format(summary)
raise ValueError(msg)
cell_dict = {c.id: c for c in summary.geometry.get_all_cells()}
cells = summary.geometry.get_all_cells()
for tally_id, tally in self.tallies.items():
tally.with_summary = True
@ -648,7 +648,7 @@ class StatePoint(object):
for tally_filter in tally.filters:
if isinstance(tally_filter, (openmc.DistribcellFilter)):
cell_id = tally_filter.bins[0]
cell = cell_dict[cell_id]
cell = cells[cell_id]
tally_filter.distribcell_paths = cell.distribcell_paths
self._summary = summary

View file

@ -440,7 +440,7 @@ class Universe(object):
Returns
-------
materials : Collections.OrderedDict
materials : collections.OrderedDict
Dictionary whose keys are material IDs and values are
:class:`Material` instances
@ -465,14 +465,9 @@ class Universe(object):
:class:`Universe` instances
"""
# Get all Cells in this Universe
cells = self.get_all_cells()
# Append all Universes within each Cell to the dictionary
universes = OrderedDict()
# Append all Universes containing each Cell to the dictionary
for cell in cells.values():
for cell in self.get_all_cells().values():
universes.update(cell.get_all_universes())
return universes

View file

@ -89,9 +89,9 @@ class AsymmetricLatticeTestHarness(PyAPITestHarness):
outstr += '\n'.join(map('{:.8e}'.format, tally.std_dev.flatten())) + '\n'
# Extract fuel assembly lattices from the summary
cell_dict = {c.id: c for c in sp.summary.geometry.get_all_cells()}
core = cell_dict[1]
fuel = cell_dict[80]
cells = sp.summary.geometry.get_all_cells()
core = cells[1]
fuel = cells[80]
fuel = fuel.fill
core = core.fill

View file

@ -116,8 +116,7 @@ class DistribmatTestHarness(PyAPITestHarness):
def _get_results(self):
outstr = super(DistribmatTestHarness, self)._get_results()
su = openmc.Summary('summary.h5')
cells = {c.id: c for c in su.geometry.get_all_cells()}
outstr += str(cells[11])
outstr += str(su.geometry.get_all_cells()[11])
return outstr
def _cleanup(self):
@ -128,5 +127,5 @@ class DistribmatTestHarness(PyAPITestHarness):
if __name__ == '__main__':
harness = DistribmatTestHarness('statepoint.5.*')
harness = DistribmatTestHarness('statepoint.5.h5')
harness.main()

View file

@ -34,7 +34,7 @@ class MGXSTestHarness(PyAPITestHarness):
self.mgxs_lib.num_delayed_groups = 6
self.mgxs_lib.legendre_order = 3
self.mgxs_lib.domain_type = 'distribcell'
cells = self.mgxs_lib.geometry.get_all_material_cells()
cells = self.mgxs_lib.geometry.get_all_material_cells().values()
self.mgxs_lib.domains = [c for c in cells if c.name == 'fuel']
self.mgxs_lib.build_library()

View file

@ -107,8 +107,7 @@ class MultipoleTestHarness(PyAPITestHarness):
def _get_results(self):
outstr = super(MultipoleTestHarness, self)._get_results()
su = openmc.Summary('summary.h5')
cells = {c.id: c for c in su.geometry.get_all_cells()}
outstr += str(cells[11])
outstr += str(su.geometry.get_all_cells()[11])
return outstr
def _cleanup(self):
@ -119,5 +118,5 @@ class MultipoleTestHarness(PyAPITestHarness):
if __name__ == '__main__':
harness = MultipoleTestHarness('statepoint.5.*')
harness = MultipoleTestHarness('statepoint.5.h5')
harness.main()