From 30689cf47be90d30aba944a8b6302f215f7bd62e Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Tue, 10 Mar 2015 02:20:03 -0400 Subject: [PATCH 1/9] Begun coding hex lattice output format for #331 --- src/utils/openmc/universe.py | 177 +++++++++++++++++++++++++++++------ 1 file changed, 147 insertions(+), 30 deletions(-) diff --git a/src/utils/openmc/universe.py b/src/utils/openmc/universe.py index 57956a4991..c3635aabf6 100644 --- a/src/utils/openmc/universe.py +++ b/src/utils/openmc/universe.py @@ -735,11 +735,15 @@ class Lattice(object): pitch.text = '{0} {1} {2}'.format(self._pitch[0], \ self._pitch[1], \ self._pitch[2]) - else: + elif len(self._pitch) == 2: pitch = ET.SubElement(lattice_subelement, "pitch") pitch.text = '{0} {1}'.format(self._pitch[0], \ self._pitch[1]) + else: + pitch = ET.SubElement(lattice_subelement, "pitch") + pitch.text = '{0}'.format(self._pitch[0]) + # Export the Lattice outer Universe (if specified) if self._outer is not None: outer = ET.SubElement(lattice_subelement, "outer") @@ -1050,6 +1054,38 @@ class HexLattice(Lattice): self._center = center + # NOTE to wboyd: this method needs to be hex/rect specific because only + # one pitch is needed to describe a 2D hex lattice. + def set_pitch(self, pitch): + + if not isinstance(pitch, (tuple, list, np.ndarray)): + msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ + 'it is not a Python tuple/list or NumPy ' \ + 'array'.format(self._id, pitch) + raise ValueError(msg) + + + elif len(pitch) != 1 and len(pitch) != 2: + msg = 'Unable to set Lattice ID={0} pitch to {1} since it does ' \ + 'not contain 2 or 3 coordinates'.format(self._id, pitch) + raise ValueError(msg) + + for dim in pitch: + + if not is_integer(dim) and not is_float(dim): + msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ + 'it is not an an integer or floating point ' \ + 'value'.format(self._id, dim) + raise ValueError(msg) + + elif dim < 0: + msg = 'Unable to set Lattice ID={0} pitch to {1} since it ' \ + 'is a negative value'.format(self._id, dim) + raise ValueError(msg) + + self._pitch = pitch + + def set_universes(self, universes): super(HexLattice, self).set_universes(universes) @@ -1058,7 +1094,8 @@ class HexLattice(Lattice): # The sub-lists are ordered from outermost ring to innermost ring. # The Universes within each sub-list are ordered from the "top" in a # clockwise fashion. - self.set_num_rings(universes.shape[0]) + #self.set_num_rings(universes.shape[0]) + self.set_num_rings(len(universes)) def __repr__(self): @@ -1127,43 +1164,123 @@ class HexLattice(Lattice): universe_ids = '\n' # 3D Lattices - if len(self._dimension) == 3: - for z in range(self._dimension[2]): - for y in range(self._dimension[1]): - for x in range(self._dimension[0]): - - universe = self._universes[x][y][z] - - # Append Universe ID to the Lattice XML subelement - universe_ids += '{0} '.format(universe._id) - - # Create XML subelement for this Universe - universe.create_xml_subelement(xml_element) - - # Add newline character when we reach end of row of cells - universe_ids += '\n' - - # Add newline character when we reach end of row of cells - universe_ids += '\n' + #if len(self._dimension) == 3: + if self._num_axial is not None: + pass +# for z in range(self._dimension[2]): +# for y in range(self._dimension[1]): +# for x in range(self._dimension[0]): +# +# universe = self._universes[x][y][z] +# +# # Append Universe ID to the Lattice XML subelement +# universe_ids += '{0} '.format(universe._id) +# +# # Create XML subelement for this Universe +# universe.create_xml_subelement(xml_element) +# +# # Add newline character when we reach end of row of cells +# universe_ids += '\n' +# +# # Add newline character when we reach end of row of cells +# universe_ids += '\n' # 2D Lattices else: - for y in range(self._dimension[1]): - for x in range(self._dimension[0]): + # Initialize the list for each row. + rows = [ [] for i in range(1 + 4 * (self._num_rings-1)) ] + middle = self._num_rings - 1 + middle = 2 * (self._num_rings - 1) - universe = self._universes[x][y] + # Start with the degenerate first ring. + universe = self._universes[-1][0] + rows[middle] = [str(universe._id)] + universe.create_xml_subelement(xml_element) - # Append Universe ID to Lattice XML subelement - universe_ids += '{0} '.format(universe._id) + # Add universes one ring at a time. + for r in range(1, self._num_rings): + # r_prime increments down while r increments up. + r_prime = self._num_rings - 1 - r + theta = 0 + y = middle + 2*r - # Create XML subelement for this Universe + # Climb down the top-right. + for i in range(r): + # Add the universe. + universe = self._universes[r_prime][theta] + rows[y].append(str(universe._id)) universe.create_xml_subelement(xml_element) - # Add newline character when we reach end of row of cells - universe_ids += '\n' + # Translate the indecies. + y -= 1 + theta += 1 - # Remove trailing newline character from Universe IDs string - universe_ids = universe_ids.rstrip('\n') + # Climb down the right. + for i in range(r): + # Add the universe. + universe = self._universes[r_prime][theta] + rows[y].append(str(universe._id)) + universe.create_xml_subelement(xml_element) + + # Translate the indecies. + y -= 2 + theta += 1 + + # Climb down the bottom-right. + for i in range(r): + # Add the universe. + universe = self._universes[r_prime][theta] + rows[y].append(str(universe._id)) + universe.create_xml_subelement(xml_element) + + # Translate the indecies. + y -= 1 + theta += 1 + + # Make sure we reached the bottom. + assert y == middle - 2*r + + # Climb up the bottom-left. + for i in range(r): + # Add the universe. + universe = self._universes[r_prime][theta] + rows[y].insert(0, str(universe._id)) + universe.create_xml_subelement(xml_element) + + # Translate the indecies. + y += 1 + theta += 1 + + # Climb up the left. + for i in range(r): + # Add the universe. + universe = self._universes[r_prime][theta] + rows[y].insert(0, str(universe._id)) + universe.create_xml_subelement(xml_element) + + # Translate the indecies. + y += 2 + theta += 1 + + # Climb up the top-left. + for i in range(r): + # Add the universe. + universe = self._universes[r_prime][theta] + rows[y].insert(0, str(universe._id)) + universe.create_xml_subelement(xml_element) + + # Translate the indecies. + y += 1 + theta += 1 + + # Make sure we reached the top and used all the universes. + assert y == middle + 2*r + assert theta == len(self._universes[r_prime]) + + # Collapse the list of lists of IDs into one string. + print(rows) + rows = [' '.join(x) for x in rows] + universe_ids = '\n'.join(rows) universes = ET.SubElement(lattice_subelement, "universes") universes.text = universe_ids From 09a2fc042ec6aa8d4bcaf33a438cf55af95bd7dd Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Tue, 10 Mar 2015 02:28:02 -0400 Subject: [PATCH 2/9] Added hexagonal example for #331 --- .../python/lattice/hexagonal/build-xml.py | 175 ++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 examples/python/lattice/hexagonal/build-xml.py diff --git a/examples/python/lattice/hexagonal/build-xml.py b/examples/python/lattice/hexagonal/build-xml.py new file mode 100644 index 0000000000..2895f93f02 --- /dev/null +++ b/examples/python/lattice/hexagonal/build-xml.py @@ -0,0 +1,175 @@ +import openmc + + +############################################################################### +# Simulation Input File Parameters +############################################################################### + +# OpenMC simulation parameters +batches = 20 +inactive = 10 +particles = 10000 + + +############################################################################### +# Exporting to OpenMC materials.xml File +############################################################################### + +# Instantiate some Nuclides +h1 = openmc.Nuclide('H-1') +o16 = openmc.Nuclide('O-16') +u235 = openmc.Nuclide('U-235') + +# Instantiate some Materials and register the appropriate Nuclides +fuel = openmc.Material(material_id=1, name='fuel') +fuel.set_density('g/cc', 4.5) +fuel.add_nuclide(u235, 1.) + +moderator = openmc.Material(material_id=2, name='moderator') +moderator.set_density('g/cc', 1.0) +moderator.add_nuclide(h1, 2.) +moderator.add_nuclide(o16, 1.) +moderator.add_s_alpha_beta('HH2O', '71t') + +# Instantiate a MaterialsFile, register all Materials, and export to XML +materials_file = openmc.MaterialsFile() +materials_file.set_default_xs('71c') +materials_file.add_materials([moderator, fuel]) +materials_file.export_to_xml() + + +############################################################################### +# Exporting to OpenMC geometry.xml File +############################################################################### + +# Instantiate Surfaces +left = openmc.XPlane(surface_id=1, x0=-2, name='left') +right = openmc.XPlane(surface_id=2, x0=2, name='right') +bottom = openmc.YPlane(surface_id=3, y0=-2, name='bottom') +top = openmc.YPlane(surface_id=4, y0=2, name='top') +fuel1 = openmc.ZCylinder(surface_id=5, x0=0, y0=0, R=0.4) +fuel2 = openmc.ZCylinder(surface_id=6, x0=0, y0=0, R=0.3) +fuel3 = openmc.ZCylinder(surface_id=7, x0=0, y0=0, R=0.2) + +left.set_boundary_type('vacuum') +right.set_boundary_type('vacuum') +top.set_boundary_type('vacuum') +bottom.set_boundary_type('vacuum') + +# Instantiate Cells +cell1 = openmc.Cell(cell_id=1, name='Cell 1') +cell2 = openmc.Cell(cell_id=101, name='cell 2') +cell3 = openmc.Cell(cell_id=102, name='cell 3') +cell4 = openmc.Cell(cell_id=201, name='cell 4') +cell5 = openmc.Cell(cell_id=202, name='cell 5') +cell6 = openmc.Cell(cell_id=301, name='cell 6') +cell7 = openmc.Cell(cell_id=302, name='cell 7') + +# Register Surfaces with Cells +cell1.add_surface(left, halfspace=+1) +cell1.add_surface(right, halfspace=-1) +cell1.add_surface(bottom, halfspace=+1) +cell1.add_surface(top, halfspace=-1) +cell2.add_surface(fuel1, halfspace=-1) +cell3.add_surface(fuel1, halfspace=+1) +cell4.add_surface(fuel2, halfspace=-1) +cell5.add_surface(fuel2, halfspace=+1) +cell6.add_surface(fuel3, halfspace=-1) +cell7.add_surface(fuel3, halfspace=+1) + +# Register Materials with Cells +cell2.set_fill(fuel) +cell3.set_fill(moderator) +cell4.set_fill(fuel) +cell5.set_fill(moderator) +cell6.set_fill(fuel) +cell7.set_fill(moderator) + +# Instantiate Universe +univ1 = openmc.Universe(universe_id=1) +univ2 = openmc.Universe(universe_id=2) +univ3 = openmc.Universe(universe_id=3) +root = openmc.Universe(universe_id=0, name='root universe') + +# Register Cells with Universe +univ1.add_cells([cell2, cell3]) +univ2.add_cells([cell4, cell5]) +univ3.add_cells([cell6, cell7]) +root.add_cell(cell1) + +# Instantiate a Lattice +lattice = openmc.HexLattice(lattice_id=5) +lattice.set_num_rings(2) +lattice.set_center([0., 0.]) +lattice.set_pitch([1.]) +lattice.set_universes([ + [univ2, univ2, univ2, univ2, univ2, univ2], + [univ1]]) + +# Fill Cell with the Lattice +cell1.set_fill(lattice) + +# Instantiate a Geometry and register the root Universe +geometry = openmc.Geometry() +geometry.set_root_universe(root) + +# Instantiate a GeometryFile, register Geometry, and export to XML +geometry_file = openmc.GeometryFile() +geometry_file.set_geometry(geometry) +geometry_file.export_to_xml() + + +############################################################################### +# Exporting to OpenMC settings.xml File +############################################################################### + +# Instantiate a SettingsFile, set all runtime parameters, and export to XML +settings_file = openmc.SettingsFile() +settings_file.set_batches(batches) +settings_file.set_inactive(inactive) +settings_file.set_particles(particles) +settings_file.set_source_space('box', [-1, -1, -1, 1, 1, 1]) +settings_file.export_to_xml() + + +############################################################################### +# Exporting to OpenMC plots.xml File +############################################################################### + +plot = openmc.Plot(plot_id=1) +plot.set_origin([0, 0, 0]) +plot.set_width([4, 4]) +plot.set_pixels([400, 400]) +plot.set_color('mat') + +# Instantiate a PlotsFile, add Plot, and export to XML +plot_file = openmc.PlotsFile() +plot_file.add_plot(plot) +plot_file.export_to_xml() + + +############################################################################### +# Exporting to OpenMC tallies.xml File +############################################################################### + +# Instantiate a tally mesh +mesh = openmc.Mesh(mesh_id=1) +mesh.set_type('rectangular') +mesh.set_dimension([4, 4]) +mesh.set_lower_left([-2, -2]) +mesh.set_width([1, 1]) + +# Instantiate tally Filter +mesh_filter = openmc.Filter() +mesh_filter.set_mesh(mesh) + +# Instantiate the Tally +tally = openmc.Tally(tally_id=1) +tally.add_filter(mesh_filter) +tally.add_score('total') + +# Instantiate a TalliesFile, register Tally/Mesh, and export to XML +tallies_file = openmc.TalliesFile() +tallies_file.add_mesh(mesh) +tallies_file.add_tally(tally) +tallies_file.export_to_xml() From de3f6f176cf94123a27ff5a3e3c2cfe89d8e8a67 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Tue, 10 Mar 2015 21:34:08 -0400 Subject: [PATCH 3/9] Fully implement 2D and 3D hex lattices for #331 --- .../python/lattice/hexagonal/build-xml.py | 27 +- src/utils/openmc/universe.py | 386 +++++++++--------- 2 files changed, 222 insertions(+), 191 deletions(-) diff --git a/examples/python/lattice/hexagonal/build-xml.py b/examples/python/lattice/hexagonal/build-xml.py index 2895f93f02..8e90e1f2d1 100644 --- a/examples/python/lattice/hexagonal/build-xml.py +++ b/examples/python/lattice/hexagonal/build-xml.py @@ -99,12 +99,29 @@ root.add_cell(cell1) # Instantiate a Lattice lattice = openmc.HexLattice(lattice_id=5) -lattice.set_num_rings(2) -lattice.set_center([0., 0.]) -lattice.set_pitch([1.]) +lattice.set_num_rings(3) +#lattice.set_center([0., 0.]) +#lattice.set_pitch([1.]) +#lattice.set_universes([ +# [univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2], +# [univ2, univ2, univ2, univ2, univ2, univ2], +# [univ1]]) +lattice.set_center([0., 0., 0.]) +lattice.set_pitch([1., 1.]) +lattice.set_num_axial(2) lattice.set_universes([ - [univ2, univ2, univ2, univ2, univ2, univ2], - [univ1]]) + [ + [univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2], + [univ2, univ2, univ2, univ2, univ2, univ2], + [univ1] + ], + [ + [univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2], + [univ2, univ2, univ2, univ2, univ2, univ2], + [univ1] + ] + ]) +lattice.set_outer(univ1) # Fill Cell with the Lattice cell1.set_fill(lattice) diff --git a/src/utils/openmc/universe.py b/src/utils/openmc/universe.py index c3635aabf6..b62f15ddb2 100644 --- a/src/utils/openmc/universe.py +++ b/src/utils/openmc/universe.py @@ -610,36 +610,6 @@ class Lattice(object): self._name = name - def set_pitch(self, pitch): - - if not isinstance(pitch, (tuple, list, np.ndarray)): - msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, pitch) - raise ValueError(msg) - - - elif len(pitch) != 2 and len(pitch) != 3: - msg = 'Unable to set Lattice ID={0} pitch to {1} since it does ' \ - 'not contain 2 or 3 coordinates'.format(self._id, pitch) - raise ValueError(msg) - - for dim in pitch: - - if not is_integer(dim) and not is_float(dim): - msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ - 'it is not an an integer or floating point ' \ - 'value'.format(self._id, dim) - raise ValueError(msg) - - elif dim < 0: - msg = 'Unable to set Lattice ID={0} pitch to {1} since it ' \ - 'is a negative value'.format(self._id, dim) - raise ValueError(msg) - - self._pitch = pitch - - def set_outer(self, outer): if not isinstance(outer, Universe): @@ -716,41 +686,6 @@ class Lattice(object): return all_universes - def create_xml_subelement(self, xml_element): - - # Determine if XML element already contains subelement for this Lattice - path = './lattice[@id=\'{0}\']'.format(self._id) - test = xml_element.find(path) - - # If the element does contain the Lattice subelement, then return - if not test is None: - return - - lattice_subelement = ET.Element("lattice") - lattice_subelement.set("id", str(self._id)) - - # Export the Lattice cell pitch - if len(self._pitch) == 3: - pitch = ET.SubElement(lattice_subelement, "pitch") - pitch.text = '{0} {1} {2}'.format(self._pitch[0], \ - self._pitch[1], \ - self._pitch[2]) - elif len(self._pitch) == 2: - pitch = ET.SubElement(lattice_subelement, "pitch") - pitch.text = '{0} {1}'.format(self._pitch[0], \ - self._pitch[1]) - - else: - pitch = ET.SubElement(lattice_subelement, "pitch") - pitch.text = '{0}'.format(self._pitch[0]) - - # Export the Lattice outer Universe (if specified) - if self._outer is not None: - outer = ET.SubElement(lattice_subelement, "outer") - outer.text = '{0}'.format(self._outer._id) - - return lattice_subelement - ################################################################################ @@ -838,6 +773,36 @@ class RectLattice(Lattice): self._offsets = offsets + def set_pitch(self, pitch): + + if not isinstance(pitch, (tuple, list, np.ndarray)): + msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ + 'it is not a Python tuple/list or NumPy ' \ + 'array'.format(self._id, pitch) + raise ValueError(msg) + + + elif len(pitch) != 2 and len(pitch) != 3: + msg = 'Unable to set Lattice ID={0} pitch to {1} since it does ' \ + 'not contain 2 or 3 coordinates'.format(self._id, pitch) + raise ValueError(msg) + + for dim in pitch: + + if not is_integer(dim) and not is_float(dim): + msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ + 'it is not an an integer or floating point ' \ + 'value'.format(self._id, dim) + raise ValueError(msg) + + elif dim < 0: + msg = 'Unable to set Lattice ID={0} pitch to {1} since it ' \ + 'is a negative value'.format(self._id, dim) + raise ValueError(msg) + + self._pitch = pitch + + def get_offset(self, path, filter_offset): # Get the current element and remove it from the list @@ -915,8 +880,24 @@ class RectLattice(Lattice): if not test is None: return - lattice_subelement = \ - super(RectLattice, self).create_xml_subelement(xml_element) + lattice_subelement = ET.Element("lattice") + lattice_subelement.set("id", str(self._id)) + + # Export the Lattice cell pitch + if len(self._pitch) == 3: + pitch = ET.SubElement(lattice_subelement, "pitch") + pitch.text = '{0} {1} {2}'.format(self._pitch[0], \ + self._pitch[1], \ + self._pitch[2]) + else: + pitch = ET.SubElement(lattice_subelement, "pitch") + pitch.text = '{0} {1}'.format(self._pitch[0], \ + self._pitch[1]) + + # Export the Lattice outer Universe (if specified) + if self._outer is not None: + outer = ET.SubElement(lattice_subelement, "outer") + outer.text = '{0}'.format(self._outer._id) # Export Lattice cell dimensions if len(self._dimension) == 3: @@ -1054,8 +1035,6 @@ class HexLattice(Lattice): self._center = center - # NOTE to wboyd: this method needs to be hex/rect specific because only - # one pitch is needed to describe a 2D hex lattice. def set_pitch(self, pitch): if not isinstance(pitch, (tuple, list, np.ndarray)): @@ -1094,8 +1073,13 @@ class HexLattice(Lattice): # The sub-lists are ordered from outermost ring to innermost ring. # The Universes within each sub-list are ordered from the "top" in a # clockwise fashion. + # TODO: Does this need some work to handle numpy arrays? I'm not sure + # we can use numpy arrays at all because they will assume a constant + # number of columns, but the columns will depend on the row in this + # ragged format. #self.set_num_rings(universes.shape[0]) - self.set_num_rings(len(universes)) + #self.set_num_rings(len(universes)) + def __repr__(self): @@ -1135,18 +1119,34 @@ class HexLattice(Lattice): def create_xml_subelement(self, xml_element): # Determine if XML element already contains subelement for this Lattice - path = './lattice[@id=\'{0}\']'.format(self._id) + path = './hex_lattice[@id=\'{0}\']'.format(self._id) test = xml_element.find(path) # If the element does contain the Lattice subelement, then return if not test is None: return - lattice_subelement = \ - super(HexLattice, self).create_xml_subelement(xml_element) + lattice_subelement = ET.Element("hex_lattice") + lattice_subelement.set("id", str(self._id)) + + # Export the Lattice cell pitch + if len(self._pitch) == 2: + pitch = ET.SubElement(lattice_subelement, "pitch") + pitch.text = '{0} {1}'.format(self._pitch[0], \ + self._pitch[1]) + else: + pitch = ET.SubElement(lattice_subelement, "pitch") + pitch.text = '{0}'.format(self._pitch[0]) + + # Export the Lattice outer Universe (if specified) + if self._outer is not None: + outer = ET.SubElement(lattice_subelement, "outer") + outer.text = '{0}'.format(self._outer._id) lattice_subelement.set("n_rings", str(self._num_rings)) - lattice_subelement.set("n_axial", str(self._num_axial)) + + if self._num_axial is not None: + lattice_subelement.set("n_axial", str(self._num_axial)) # Export Lattice cell center if len(self._center) == 3: @@ -1159,134 +1159,148 @@ class HexLattice(Lattice): dimension.text = '{0} {1}'.format(self._center[0], \ self._center[1]) - # FIXME: The following must be revised for hexagonal lattice ordering - # Export the Lattice nested Universe IDs - column major for Fortran - universe_ids = '\n' + # Export the Lattice nested Universe IDs. # 3D Lattices - #if len(self._dimension) == 3: if self._num_axial is not None: - pass -# for z in range(self._dimension[2]): -# for y in range(self._dimension[1]): -# for x in range(self._dimension[0]): -# -# universe = self._universes[x][y][z] -# -# # Append Universe ID to the Lattice XML subelement -# universe_ids += '{0} '.format(universe._id) -# -# # Create XML subelement for this Universe -# universe.create_xml_subelement(xml_element) -# -# # Add newline character when we reach end of row of cells -# universe_ids += '\n' -# -# # Add newline character when we reach end of row of cells -# universe_ids += '\n' + slices = [] + for z in range(self._num_axial): + # Initialize the center universe. + universe = self._universes[z][-1][0] + universe.create_xml_subelement(xml_element) + + # Initialize the remaining universes. + for r in range(self._num_rings-1): + for theta in range(2*(self._num_rings - r)): + universe = self._universes[z][r][theta] + universe.create_xml_subelement(xml_element) + + # Get a string representation of the universe IDs. + slices.append(self._repr_axial_slice(self._universes[z])) + + # Collapse the list of axial slices into a single string. + universe_ids = '\n'.join(slices) # 2D Lattices else: - # Initialize the list for each row. - rows = [ [] for i in range(1 + 4 * (self._num_rings-1)) ] - middle = self._num_rings - 1 - middle = 2 * (self._num_rings - 1) - - # Start with the degenerate first ring. + # Initialize the center universe. universe = self._universes[-1][0] - rows[middle] = [str(universe._id)] universe.create_xml_subelement(xml_element) - # Add universes one ring at a time. - for r in range(1, self._num_rings): - # r_prime increments down while r increments up. - r_prime = self._num_rings - 1 - r - theta = 0 - y = middle + 2*r - - # Climb down the top-right. - for i in range(r): - # Add the universe. - universe = self._universes[r_prime][theta] - rows[y].append(str(universe._id)) + # Initialize the remaining universes. + for r in range(self._num_rings-1): + for theta in range(2*(self._num_rings - r)): + universe = self._universes[r][theta] universe.create_xml_subelement(xml_element) - # Translate the indecies. - y -= 1 - theta += 1 - - # Climb down the right. - for i in range(r): - # Add the universe. - universe = self._universes[r_prime][theta] - rows[y].append(str(universe._id)) - universe.create_xml_subelement(xml_element) - - # Translate the indecies. - y -= 2 - theta += 1 - - # Climb down the bottom-right. - for i in range(r): - # Add the universe. - universe = self._universes[r_prime][theta] - rows[y].append(str(universe._id)) - universe.create_xml_subelement(xml_element) - - # Translate the indecies. - y -= 1 - theta += 1 - - # Make sure we reached the bottom. - assert y == middle - 2*r - - # Climb up the bottom-left. - for i in range(r): - # Add the universe. - universe = self._universes[r_prime][theta] - rows[y].insert(0, str(universe._id)) - universe.create_xml_subelement(xml_element) - - # Translate the indecies. - y += 1 - theta += 1 - - # Climb up the left. - for i in range(r): - # Add the universe. - universe = self._universes[r_prime][theta] - rows[y].insert(0, str(universe._id)) - universe.create_xml_subelement(xml_element) - - # Translate the indecies. - y += 2 - theta += 1 - - # Climb up the top-left. - for i in range(r): - # Add the universe. - universe = self._universes[r_prime][theta] - rows[y].insert(0, str(universe._id)) - universe.create_xml_subelement(xml_element) - - # Translate the indecies. - y += 1 - theta += 1 - - # Make sure we reached the top and used all the universes. - assert y == middle + 2*r - assert theta == len(self._universes[r_prime]) - - # Collapse the list of lists of IDs into one string. - print(rows) - rows = [' '.join(x) for x in rows] - universe_ids = '\n'.join(rows) + # Get a string representation of the universe IDs. + universe_ids = self._repr_axial_slice(self._universes) universes = ET.SubElement(lattice_subelement, "universes") - universes.text = universe_ids + universes.text = '\n' + universe_ids if len(self._name) > 0: xml_element.append(ET.Comment(self._name)) # Append the XML subelement for this Lattice to the XML element xml_element.append(lattice_subelement) + + + def _repr_axial_slice(self, universes): + """Return string representation for the given 2D group of universes. + + The 'universes' argument should be a list of lists of universes where + each sub-list represents a single ring. The first list should be the + outer ring. + """ + + assert len(universes) == self._num_rings + + # Initialize the list for each row. + rows = [ [] for i in range(1 + 4 * (self._num_rings-1)) ] + middle = self._num_rings - 1 + middle = 2 * (self._num_rings - 1) + + # Start with the degenerate first ring. + universe = universes[-1][0] + rows[middle] = [str(universe._id)] + + # Add universes one ring at a time. + for r in range(1, self._num_rings): + # r_prime increments down while r increments up. + r_prime = self._num_rings - 1 - r + theta = 0 + y = middle + 2*r + + # Climb down the top-right. + for i in range(r): + # Add the universe. + universe = universes[r_prime][theta] + rows[y].append(str(universe._id)) + + # Translate the indecies. + y -= 1 + theta += 1 + + # Climb down the right. + for i in range(r): + # Add the universe. + universe = universes[r_prime][theta] + rows[y].append(str(universe._id)) + + # Translate the indecies. + y -= 2 + theta += 1 + + # Climb down the bottom-right. + for i in range(r): + # Add the universe. + universe = universes[r_prime][theta] + rows[y].append(str(universe._id)) + + # Translate the indecies. + y -= 1 + theta += 1 + + # Make sure we reached the bottom. + assert y == middle - 2*r + + # Climb up the bottom-left. + for i in range(r): + # Add the universe. + universe = universes[r_prime][theta] + rows[y].insert(0, str(universe._id)) + + # Translate the indecies. + y += 1 + theta += 1 + + # Climb up the left. + for i in range(r): + # Add the universe. + universe = universes[r_prime][theta] + rows[y].insert(0, str(universe._id)) + + # Translate the indecies. + y += 2 + theta += 1 + + # Climb up the top-left. + for i in range(r): + # Add the universe. + universe = universes[r_prime][theta] + rows[y].insert(0, str(universe._id)) + + # Translate the indecies. + y += 1 + theta += 1 + + # Make sure we reached the top and used all the universes. + assert y == middle + 2*r + assert theta == len(universes[r_prime]) + + # Collapse the list of lists of IDs into one string. + rows = [' '.join(x) for x in rows] + universe_ids = '\n'.join(rows) + + return universe_ids From 19afca24ec599d06ec4ffc4c434f7b360732024e Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Wed, 11 Mar 2015 00:09:37 -0400 Subject: [PATCH 4/9] Added hex lattice length checking for #331 --- .../python/lattice/hexagonal/build-xml.py | 21 +----- src/utils/openmc/universe.py | 71 +++++++++++++++++-- 2 files changed, 66 insertions(+), 26 deletions(-) diff --git a/examples/python/lattice/hexagonal/build-xml.py b/examples/python/lattice/hexagonal/build-xml.py index 8e90e1f2d1..7e79aaf1ca 100644 --- a/examples/python/lattice/hexagonal/build-xml.py +++ b/examples/python/lattice/hexagonal/build-xml.py @@ -99,28 +99,11 @@ root.add_cell(cell1) # Instantiate a Lattice lattice = openmc.HexLattice(lattice_id=5) -lattice.set_num_rings(3) -#lattice.set_center([0., 0.]) -#lattice.set_pitch([1.]) -#lattice.set_universes([ -# [univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2], -# [univ2, univ2, univ2, univ2, univ2, univ2], -# [univ1]]) lattice.set_center([0., 0., 0.]) lattice.set_pitch([1., 1.]) -lattice.set_num_axial(2) lattice.set_universes([ - [ - [univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2], - [univ2, univ2, univ2, univ2, univ2, univ2], - [univ1] - ], - [ - [univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2, univ2], - [univ2, univ2, univ2, univ2, univ2, univ2], - [univ1] - ] - ]) + [ [univ2]*12, [univ2]*6, [univ1] ], + [ [univ2]*12, [univ2]*6, [univ1] ]]) lattice.set_outer(univ1) # Fill Cell with the Lattice diff --git a/src/utils/openmc/universe.py b/src/utils/openmc/universe.py index b62f15ddb2..e667c84b73 100644 --- a/src/utils/openmc/universe.py +++ b/src/utils/openmc/universe.py @@ -1073,13 +1073,70 @@ class HexLattice(Lattice): # The sub-lists are ordered from outermost ring to innermost ring. # The Universes within each sub-list are ordered from the "top" in a # clockwise fashion. - # TODO: Does this need some work to handle numpy arrays? I'm not sure - # we can use numpy arrays at all because they will assume a constant - # number of columns, but the columns will depend on the row in this - # ragged format. - #self.set_num_rings(universes.shape[0]) - #self.set_num_rings(len(universes)) + # Check to see if the given universes look like a 2D or a 3D array. + if isinstance(self._universes[0][0], Universe): + n_dims = 2 + + elif isinstance(self._universes[0][0][0], Universe): + n_dims = 3 + + 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 ' \ + 'three-deep iterable of universes.'.format(self._id) + raise RuntimeError(msg) + + # Set the number of axial positions. + if n_dims == 3: + self.set_num_axial(self._universes.shape[0]) + + 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: + self.set_num_rings(len(self._universes[0])) + for rings in self._universes: + if len(rings) != self._num_rings: + msg = 'HexLattice ID={0:d} has an inconsistent number of ' \ + 'rings per axial positon'.format(self._id) + raise ValueError(msg) + + else: + self.set_num_rings(self._universes.shape[0]) + + # Make sure there are the correct number of elements in each ring. + z = 0 + while True: + if n_dims == 3: + axial_slice = self._universes[z] + else: + axial_slice = self._universes + + # Check the center ring. + if len(axial_slice[-1]) != 1: + msg = 'HexLattice ID={0:d} has the wrong number of elements ' \ + 'in the innermost ring. Only 1 element is allowed in ' \ + 'the innermost ring.'.format(self._id) + raise ValueError(msg) + + # Check the outer rings. + for r in range(self._num_rings-1): + if len(axial_slice[r]) != 6*(self._num_rings - 1 - r): + msg = 'HexLattice ID={0:d} has the wrong number of ' \ + 'elements in ring number {1:d} (counting from the ' \ + 'outermost ring). This ring should have {2:d} ' \ + 'elements.'.format(self._id, r, + 6*(self._num_rings - 1 - r)) + raise ValueError(msg) + + if n_dims == 3: + z += 1 + if z == self._num_axial: break + else: + break def __repr__(self): @@ -1171,7 +1228,7 @@ class HexLattice(Lattice): # Initialize the remaining universes. for r in range(self._num_rings-1): - for theta in range(2*(self._num_rings - r)): + for theta in range(6*(self._num_rings - 1 - r)): universe = self._universes[z][r][theta] universe.create_xml_subelement(xml_element) From f82103a3a9941d8d8c6edbd8ce6d4127aa1eb4fc Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Wed, 11 Mar 2015 01:05:50 -0400 Subject: [PATCH 5/9] Added universe ID padding for hex lattices in #331 --- src/utils/openmc/universe.py | 39 ++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/utils/openmc/universe.py b/src/utils/openmc/universe.py index e667c84b73..04d54054bc 100644 --- a/src/utils/openmc/universe.py +++ b/src/utils/openmc/universe.py @@ -1273,14 +1273,21 @@ class HexLattice(Lattice): assert len(universes) == self._num_rings + # Find the largest universe ID and count the number of digits so we can + # properly pad the output string later. + largest_id = max([max([univ._id for univ in ring]) + for ring in universes]) + n_digits = len(str(largest_id)) + pad = ' '*n_digits + id_form = '{: ^' + str(n_digits) + 'd}' + # Initialize the list for each row. rows = [ [] for i in range(1 + 4 * (self._num_rings-1)) ] - middle = self._num_rings - 1 middle = 2 * (self._num_rings - 1) # Start with the degenerate first ring. universe = universes[-1][0] - rows[middle] = [str(universe._id)] + rows[middle] = [id_form.format(universe._id)] # Add universes one ring at a time. for r in range(1, self._num_rings): @@ -1293,7 +1300,7 @@ class HexLattice(Lattice): for i in range(r): # Add the universe. universe = universes[r_prime][theta] - rows[y].append(str(universe._id)) + rows[y].append(id_form.format(universe._id)) # Translate the indecies. y -= 1 @@ -1303,7 +1310,7 @@ class HexLattice(Lattice): for i in range(r): # Add the universe. universe = universes[r_prime][theta] - rows[y].append(str(universe._id)) + rows[y].append(id_form.format(universe._id)) # Translate the indecies. y -= 2 @@ -1313,7 +1320,7 @@ class HexLattice(Lattice): for i in range(r): # Add the universe. universe = universes[r_prime][theta] - rows[y].append(str(universe._id)) + rows[y].append(id_form.format(universe._id)) # Translate the indecies. y -= 1 @@ -1326,7 +1333,7 @@ class HexLattice(Lattice): for i in range(r): # Add the universe. universe = universes[r_prime][theta] - rows[y].insert(0, str(universe._id)) + rows[y].insert(0, id_form.format(universe._id)) # Translate the indecies. y += 1 @@ -1336,7 +1343,7 @@ class HexLattice(Lattice): for i in range(r): # Add the universe. universe = universes[r_prime][theta] - rows[y].insert(0, str(universe._id)) + rows[y].insert(0, id_form.format(universe._id)) # Translate the indecies. y += 2 @@ -1346,7 +1353,7 @@ class HexLattice(Lattice): for i in range(r): # Add the universe. universe = universes[r_prime][theta] - rows[y].insert(0, str(universe._id)) + rows[y].insert(0, id_form.format(universe._id)) # Translate the indecies. y += 1 @@ -1356,8 +1363,18 @@ class HexLattice(Lattice): assert y == middle + 2*r assert theta == len(universes[r_prime]) - # Collapse the list of lists of IDs into one string. - rows = [' '.join(x) for x in rows] - universe_ids = '\n'.join(rows) + # Join each row into a single string. + rows = [pad.join(x) for x in rows] + # Pad the beginning of the rows so they line up properly. + for y in range(self._num_rings - 1): + rows[y] = (self._num_rings - 1 - y)*pad + rows[y] + rows[-1 - y] = (self._num_rings - 1 - y)*pad + rows[-1 - y] + + for y in range(self._num_rings % 2, self._num_rings, 2): + rows[middle + y] = pad + rows[middle + y] + if y != 0: rows[middle - y] = pad + rows[middle - y] + + # Join the rows together and return the string. + universe_ids = '\n'.join(rows) return universe_ids From a996bccbef36a15dff8637625109388f282596c2 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Fri, 13 Mar 2015 15:01:44 -0400 Subject: [PATCH 6/9] Improved hex lattice example for #331 --- .../python/lattice/hexagonal/build-xml.py | 109 ++++++++---------- 1 file changed, 46 insertions(+), 63 deletions(-) diff --git a/examples/python/lattice/hexagonal/build-xml.py b/examples/python/lattice/hexagonal/build-xml.py index 7e79aaf1ca..6e0a58bc1b 100644 --- a/examples/python/lattice/hexagonal/build-xml.py +++ b/examples/python/lattice/hexagonal/build-xml.py @@ -19,6 +19,7 @@ particles = 10000 h1 = openmc.Nuclide('H-1') o16 = openmc.Nuclide('O-16') u235 = openmc.Nuclide('U-235') +fe56 = openmc.Nuclide('Fe-56') # Instantiate some Materials and register the appropriate Nuclides fuel = openmc.Material(material_id=1, name='fuel') @@ -31,10 +32,14 @@ moderator.add_nuclide(h1, 2.) moderator.add_nuclide(o16, 1.) moderator.add_s_alpha_beta('HH2O', '71t') +iron = openmc.Material(material_id=3, name='iron') +iron.set_density('g/cc', 7.9) +iron.add_nuclide(fe56, 1.) + # Instantiate a MaterialsFile, register all Materials, and export to XML materials_file = openmc.MaterialsFile() materials_file.set_default_xs('71c') -materials_file.add_materials([moderator, fuel]) +materials_file.add_materials([moderator, fuel, iron]) materials_file.export_to_xml() @@ -43,13 +48,11 @@ materials_file.export_to_xml() ############################################################################### # Instantiate Surfaces -left = openmc.XPlane(surface_id=1, x0=-2, name='left') -right = openmc.XPlane(surface_id=2, x0=2, name='right') -bottom = openmc.YPlane(surface_id=3, y0=-2, name='bottom') -top = openmc.YPlane(surface_id=4, y0=2, name='top') -fuel1 = openmc.ZCylinder(surface_id=5, x0=0, y0=0, R=0.4) -fuel2 = openmc.ZCylinder(surface_id=6, x0=0, y0=0, R=0.3) -fuel3 = openmc.ZCylinder(surface_id=7, x0=0, y0=0, R=0.2) +left = openmc.XPlane(surface_id=1, x0=-3, name='left') +right = openmc.XPlane(surface_id=2, x0=3, name='right') +bottom = openmc.YPlane(surface_id=3, y0=-4, name='bottom') +top = openmc.YPlane(surface_id=4, y0=4, name='top') +fuel_surf = openmc.ZCylinder(surface_id=5, x0=0, y0=0, R=0.4) left.set_boundary_type('vacuum') right.set_boundary_type('vacuum') @@ -60,51 +63,48 @@ bottom.set_boundary_type('vacuum') cell1 = openmc.Cell(cell_id=1, name='Cell 1') cell2 = openmc.Cell(cell_id=101, name='cell 2') cell3 = openmc.Cell(cell_id=102, name='cell 3') -cell4 = openmc.Cell(cell_id=201, name='cell 4') -cell5 = openmc.Cell(cell_id=202, name='cell 5') -cell6 = openmc.Cell(cell_id=301, name='cell 6') -cell7 = openmc.Cell(cell_id=302, name='cell 7') +cell4 = openmc.Cell(cell_id=500, name='cell 4') +cell5 = openmc.Cell(cell_id=600, name='cell 5') +cell6 = openmc.Cell(cell_id=601, name='cell 6') # Register Surfaces with Cells cell1.add_surface(left, halfspace=+1) cell1.add_surface(right, halfspace=-1) cell1.add_surface(bottom, halfspace=+1) cell1.add_surface(top, halfspace=-1) -cell2.add_surface(fuel1, halfspace=-1) -cell3.add_surface(fuel1, halfspace=+1) -cell4.add_surface(fuel2, halfspace=-1) -cell5.add_surface(fuel2, halfspace=+1) -cell6.add_surface(fuel3, halfspace=-1) -cell7.add_surface(fuel3, halfspace=+1) +cell2.add_surface(fuel_surf, halfspace=-1) +cell3.add_surface(fuel_surf, halfspace=+1) +cell5.add_surface(fuel_surf, halfspace=-1) +cell6.add_surface(fuel_surf, halfspace=+1) # Register Materials with Cells cell2.set_fill(fuel) cell3.set_fill(moderator) -cell4.set_fill(fuel) -cell5.set_fill(moderator) -cell6.set_fill(fuel) -cell7.set_fill(moderator) +cell4.set_fill(moderator) +cell5.set_fill(iron) +cell6.set_fill(moderator) # Instantiate Universe univ1 = openmc.Universe(universe_id=1) -univ2 = openmc.Universe(universe_id=2) -univ3 = openmc.Universe(universe_id=3) +univ2 = openmc.Universe(universe_id=3) +univ3 = openmc.Universe(universe_id=4) root = openmc.Universe(universe_id=0, name='root universe') # Register Cells with Universe univ1.add_cells([cell2, cell3]) -univ2.add_cells([cell4, cell5]) -univ3.add_cells([cell6, cell7]) +univ2.add_cells([cell4]) +univ3.add_cells([cell5, cell6]) root.add_cell(cell1) # Instantiate a Lattice lattice = openmc.HexLattice(lattice_id=5) lattice.set_center([0., 0., 0.]) -lattice.set_pitch([1., 1.]) +lattice.set_pitch([1., 2.]) lattice.set_universes([ - [ [univ2]*12, [univ2]*6, [univ1] ], - [ [univ2]*12, [univ2]*6, [univ1] ]]) -lattice.set_outer(univ1) + [ [univ2] + [univ3]*11, [univ2] + [univ3]*5, [univ3] ], + [ [univ2] + [univ1]*11, [univ2] + [univ1]*5, [univ1] ], + [ [univ2] + [univ3]*11, [univ2] + [univ3]*5, [univ3] ]]) +lattice.set_outer(univ2) # Fill Cell with the Lattice cell1.set_fill(lattice) @@ -136,40 +136,23 @@ settings_file.export_to_xml() # Exporting to OpenMC plots.xml File ############################################################################### -plot = openmc.Plot(plot_id=1) -plot.set_origin([0, 0, 0]) -plot.set_width([4, 4]) -plot.set_pixels([400, 400]) -plot.set_color('mat') +plot_xy = openmc.Plot(plot_id=1) +plot_xy.set_filename('plot_xy') +plot_xy.set_origin([0, 0, 0]) +plot_xy.set_width([6, 6]) +plot_xy.set_pixels([400, 400]) +plot_xy.set_color('mat') + +plot_yz = openmc.Plot(plot_id=2) +plot_yz.set_filename('plot_yz') +plot_yz.set_basis('yz') +plot_yz.set_origin([0, 0, 0]) +plot_yz.set_width([8, 8]) +plot_yz.set_pixels([400, 400]) +plot_yz.set_color('mat') # Instantiate a PlotsFile, add Plot, and export to XML plot_file = openmc.PlotsFile() -plot_file.add_plot(plot) +plot_file.add_plot(plot_xy) +plot_file.add_plot(plot_yz) plot_file.export_to_xml() - - -############################################################################### -# Exporting to OpenMC tallies.xml File -############################################################################### - -# Instantiate a tally mesh -mesh = openmc.Mesh(mesh_id=1) -mesh.set_type('rectangular') -mesh.set_dimension([4, 4]) -mesh.set_lower_left([-2, -2]) -mesh.set_width([1, 1]) - -# Instantiate tally Filter -mesh_filter = openmc.Filter() -mesh_filter.set_mesh(mesh) - -# Instantiate the Tally -tally = openmc.Tally(tally_id=1) -tally.add_filter(mesh_filter) -tally.add_score('total') - -# Instantiate a TalliesFile, register Tally/Mesh, and export to XML -tallies_file = openmc.TalliesFile() -tallies_file.add_mesh(mesh) -tallies_file.add_tally(tally) -tallies_file.export_to_xml() From 014913cb37eac657744f5a4959d88f6d21ee3f80 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Fri, 13 Mar 2015 15:45:11 -0400 Subject: [PATCH 7/9] Fixes for hex lattice representation in #331 --- src/utils/openmc/universe.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/utils/openmc/universe.py b/src/utils/openmc/universe.py index 04d54054bc..a9c0b505c6 100644 --- a/src/utils/openmc/universe.py +++ b/src/utils/openmc/universe.py @@ -1157,18 +1157,22 @@ class HexLattice(Lattice): string += '{0: <16}{1}{2}\n'.format('\tOuter', '=\t', self._outer) - string += '{0: <16}\n'.format('\tUniverses') + string += '{0: <16}'.format('\tUniverses') - # FIXME: This loop must be revised for hexagonal lattice ordering - # Lattice nested Universe IDs - column major for Fortran - for i, universe in enumerate(np.ravel(self._universes)): - string += '{0} '.format(universe._id) + z = 0 + while True: + if self._num_axial is not None: + axial_slice = self._universes[z] + else: + axial_slice = self._universes - # Add a newline character every time we reach end of row of cells - if (i+1) % self._dimension[-1] == 0: - string += '\n' + string += '\n' + self._repr_axial_slice(axial_slice) - string = string.rstrip('\n') + if self._num_axial is not None: + z += 1 + if z == self._num_axial: break + else: + break return string @@ -1363,8 +1367,8 @@ class HexLattice(Lattice): assert y == middle + 2*r assert theta == len(universes[r_prime]) - # Join each row into a single string. - rows = [pad.join(x) for x in rows] + # Flip the rows and join each row into a single string. + rows = [pad.join(x) for x in rows[::-1]] # Pad the beginning of the rows so they line up properly. for y in range(self._num_rings - 1): From 87a5bf1e62fa3c89ff9210c601b9ddef38a157d8 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Mon, 16 Mar 2015 21:15:53 -0400 Subject: [PATCH 8/9] Minor hex lattice improvements for #331 --- src/utils/openmc/universe.py | 92 ++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/src/utils/openmc/universe.py b/src/utils/openmc/universe.py index a9c0b505c6..b63d7ac5ee 100644 --- a/src/utils/openmc/universe.py +++ b/src/utils/openmc/universe.py @@ -1108,35 +1108,43 @@ class HexLattice(Lattice): self.set_num_rings(self._universes.shape[0]) # Make sure there are the correct number of elements in each ring. - z = 0 - while True: - if n_dims == 3: - axial_slice = self._universes[z] - else: - axial_slice = self._universes + if n_dims == 3: + for axial_slice in self._universes: + # Check the center ring. + if len(axial_slice[-1]) != 1: + msg = 'HexLattice ID={0:d} has the wrong number of ' \ + 'elements in the innermost ring. Only 1 element is ' \ + 'allowed in the innermost ring.'.format(self._id) + raise ValueError(msg) - # Check the center ring. - if len(axial_slice[-1]) != 1: - msg = 'HexLattice ID={0:d} has the wrong number of elements ' \ - 'in the innermost ring. Only 1 element is allowed in ' \ - 'the innermost ring.'.format(self._id) - raise ValueError(msg) + # Check the outer rings. + for r in range(self._num_rings-1): + if len(axial_slice[r]) != 6*(self._num_rings - 1 - r): + msg = 'HexLattice ID={0:d} has the wrong number of ' \ + 'elements in ring number {1:d} (counting from the '\ + 'outermost ring). This ring should have {2:d} ' \ + 'elements.'.format(self._id, r, + 6*(self._num_rings - 1 - r)) + raise ValueError(msg) - # Check the outer rings. - for r in range(self._num_rings-1): - if len(axial_slice[r]) != 6*(self._num_rings - 1 - r): - msg = 'HexLattice ID={0:d} has the wrong number of ' \ - 'elements in ring number {1:d} (counting from the ' \ - 'outermost ring). This ring should have {2:d} ' \ - 'elements.'.format(self._id, r, - 6*(self._num_rings - 1 - r)) - raise ValueError(msg) + else: + axial_slice = self._universes + # Check the center ring. + if len(axial_slice[-1]) != 1: + msg = 'HexLattice ID={0:d} has the wrong number of ' \ + 'elements in the innermost ring. Only 1 element is ' \ + 'allowed in the innermost ring.'.format(self._id) + raise ValueError(msg) - if n_dims == 3: - z += 1 - if z == self._num_axial: break - else: - break + # Check the outer rings. + for r in range(self._num_rings-1): + if len(axial_slice[r]) != 6*(self._num_rings - 1 - r): + msg = 'HexLattice ID={0:d} has the wrong number of ' \ + 'elements in ring number {1:d} (counting from the '\ + 'outermost ring). This ring should have {2:d} ' \ + 'elements.'.format(self._id, r, + 6*(self._num_rings - 1 - r)) + raise ValueError(msg) def __repr__(self): @@ -1157,22 +1165,14 @@ class HexLattice(Lattice): string += '{0: <16}{1}{2}\n'.format('\tOuter', '=\t', self._outer) - string += '{0: <16}'.format('\tUniverses') + string += '{0: <16}\n'.format('\tUniverses') - z = 0 - while True: - if self._num_axial is not None: - axial_slice = self._universes[z] - else: - axial_slice = self._universes + if self._num_axial is not None: + slices = [self._repr_axial_slice(x) for x in self._universes] + string += '\n'.join(slices) - string += '\n' + self._repr_axial_slice(axial_slice) - - if self._num_axial is not None: - z += 1 - if z == self._num_axial: break - else: - break + else: + string += self._repr_axial_slice(self._universes) return string @@ -1306,7 +1306,7 @@ class HexLattice(Lattice): universe = universes[r_prime][theta] rows[y].append(id_form.format(universe._id)) - # Translate the indecies. + # Translate the indices. y -= 1 theta += 1 @@ -1316,7 +1316,7 @@ class HexLattice(Lattice): universe = universes[r_prime][theta] rows[y].append(id_form.format(universe._id)) - # Translate the indecies. + # Translate the indices. y -= 2 theta += 1 @@ -1326,7 +1326,7 @@ class HexLattice(Lattice): universe = universes[r_prime][theta] rows[y].append(id_form.format(universe._id)) - # Translate the indecies. + # Translate the indices. y -= 1 theta += 1 @@ -1339,7 +1339,7 @@ class HexLattice(Lattice): universe = universes[r_prime][theta] rows[y].insert(0, id_form.format(universe._id)) - # Translate the indecies. + # Translate the indices. y += 1 theta += 1 @@ -1349,7 +1349,7 @@ class HexLattice(Lattice): universe = universes[r_prime][theta] rows[y].insert(0, id_form.format(universe._id)) - # Translate the indecies. + # Translate the indices. y += 2 theta += 1 @@ -1359,7 +1359,7 @@ class HexLattice(Lattice): universe = universes[r_prime][theta] rows[y].insert(0, id_form.format(universe._id)) - # Translate the indecies. + # Translate the indices. y += 1 theta += 1 From 2b9fdc6c094d491731a01bf60bf73936f434e325 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Mon, 16 Mar 2015 21:18:57 -0400 Subject: [PATCH 9/9] Minor hex lattice improvements for #331 --- src/utils/openmc/universe.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/utils/openmc/universe.py b/src/utils/openmc/universe.py index b63d7ac5ee..e70b6effd5 100644 --- a/src/utils/openmc/universe.py +++ b/src/utils/openmc/universe.py @@ -1275,8 +1275,6 @@ class HexLattice(Lattice): outer ring. """ - assert len(universes) == self._num_rings - # Find the largest universe ID and count the number of digits so we can # properly pad the output string later. largest_id = max([max([univ._id for univ in ring]) @@ -1330,9 +1328,6 @@ class HexLattice(Lattice): y -= 1 theta += 1 - # Make sure we reached the bottom. - assert y == middle - 2*r - # Climb up the bottom-left. for i in range(r): # Add the universe. @@ -1363,10 +1358,6 @@ class HexLattice(Lattice): y += 1 theta += 1 - # Make sure we reached the top and used all the universes. - assert y == middle + 2*r - assert theta == len(universes[r_prime]) - # Flip the rows and join each row into a single string. rows = [pad.join(x) for x in rows[::-1]]