Fix reading rotation matrix from XML

This commit is contained in:
Paul Romano 2022-05-24 09:09:51 -05:00
parent d20064945c
commit 321de2a4d1
2 changed files with 21 additions and 1 deletions

View file

@ -690,7 +690,10 @@ class Cell(IDManagerMixin):
for key in ('temperature', 'rotation', 'translation'):
value = get_text(elem, key)
if value is not None:
setattr(c, key, [float(x) for x in value.split()])
values = [float(x) for x in value.split()]
if key == 'rotation' and len(values) == 9:
values = np.array(values).reshape(3, 3)
setattr(c, key, values)
# Add this cell to appropriate universe
univ_id = int(get_text(elem, 'universe', 0))

View file

@ -297,3 +297,20 @@ def test_to_xml_element(cell_with_lattice):
elem = c.create_xml_subelement(root)
assert elem.get('region') == str(c.region)
assert elem.get('temperature') == str(c.temperature)
@pytest.mark.parametrize("rotation", [
(90, 45, 0),
[[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, -1.0, 0.0]]
])
def test_rotation_from_xml(rotation):
# Make sure rotation attribute (matrix) round trips through XML correctly
s = openmc.ZCylinder(r=10.0)
cell = openmc.Cell(region=-s)
cell.rotation = rotation
root = ET.Element('geometry')
elem = cell.create_xml_subelement(root)
new_cell = openmc.Cell.from_xml_element(
elem, {s.id: s}, {'void': None}, openmc.Universe
)
np.testing.assert_allclose(new_cell.rotation, cell.rotation)