From 321de2a4d106d50ff712b22e35e71e47e6dae5ef Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Tue, 24 May 2022 09:09:51 -0500 Subject: [PATCH] Fix reading rotation matrix from XML --- openmc/cell.py | 5 ++++- tests/unit_tests/test_cell.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/openmc/cell.py b/openmc/cell.py index feba18da5..103a34fe6 100644 --- a/openmc/cell.py +++ b/openmc/cell.py @@ -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)) diff --git a/tests/unit_tests/test_cell.py b/tests/unit_tests/test_cell.py index 9234e7e4b..faecf0b63 100644 --- a/tests/unit_tests/test_cell.py +++ b/tests/unit_tests/test_cell.py @@ -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)