Skip to content

Commit

Permalink
Hexagonal Lattice Roundtrip (openmc-dev#3003)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Romano <[email protected]>
  • Loading branch information
2 people authored and church89 committed Jul 18, 2024
1 parent ec7ef1b commit 70676f2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions openmc/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,7 @@ def from_xml_element(cls, elem, get_universe):
alpha -= 1
if not lat.is_valid_index((x, alpha, z)):
# Reached the bottom
j += 1
break
j += 1
else:
Expand All @@ -1593,6 +1594,7 @@ def from_xml_element(cls, elem, get_universe):

# Check if we've reached the bottom
if y == -n_rings:
j += 1
break

while not lat.is_valid_index((alpha, y, z)):
Expand Down
43 changes: 43 additions & 0 deletions tests/unit_tests/test_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,46 @@ def test_unset_universes():
hex_lattice.pitch = (1.,)
with pytest.raises(ValueError):
hex_lattice.create_xml_subelement(elem)


@pytest.mark.parametrize("orientation", ['x', 'y'])
def test_hex_lattice_roundtrip(orientation):
openmc.reset_auto_ids()

# ensure that the lattice universes are the same on all axial levels
def check_lattice_universes(og_lattice, xml_lattice):
for axial_og, axial_rt in zip(og_lattice.universes, xml_lattice.universes):
for ring_og, ring_rt in zip(axial_og, axial_rt):
assert [u.id for u in ring_og] == [u.id for u in ring_rt]

latt = openmc.HexLattice()
latt.pitch = (1.0, 1.0)
latt.center = (0.0, 0.0, 0.0)
latt.orientation = orientation

# fill the lattice with universes in increasing order and repeat for
# the second actial level
lvl_one_univs = [openmc.Universe(cells=[openmc.Cell()]) for _ in range(19)]
lvl_one_univs = [lvl_one_univs[-12:], lvl_one_univs[1:7], lvl_one_univs[:1]]
latt.universes = [lvl_one_univs, lvl_one_univs]

geom = openmc.Geometry([openmc.Cell(fill=latt)])
geom.export_to_xml()

xml_geom = openmc.Geometry.from_xml(materials=openmc.Materials())

xml_latt = xml_geom.get_all_lattices()[latt.id]

check_lattice_universes(latt, xml_latt)

# same test but with unique universes for each axial level
lvl_two_univs = [openmc.Universe(cells=[openmc.Cell()]) for _ in range(19)]
lvl_two_univs = [lvl_two_univs[-12:], lvl_two_univs[1:7], lvl_two_univs[:1]]
latt.universes = [lvl_one_univs, lvl_two_univs]

geom.export_to_xml()

xml_geom = openmc.Geometry.from_xml(materials=openmc.Materials())
xml_latt = xml_geom.get_all_lattices()[latt.id]

check_lattice_universes(latt, xml_latt)

0 comments on commit 70676f2

Please sign in to comment.