Skip to content

Commit c5a7173

Browse files
Avoid duplicate materials written to XML (#3536)
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
1 parent 75b5813 commit c5a7173

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

openmc/material.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,7 @@ def _write_xml(self, file, header=True, level=0, spaces_per_level=2,
19271927
file.write(ET.tostring(element, encoding="unicode"))
19281928

19291929
# Write the <material> elements.
1930-
for material in sorted(self, key=lambda x: x.id):
1930+
for material in sorted(set(self), key=lambda x: x.id):
19311931
element = material.to_xml_element(nuclides_to_ignore=nuclides_to_ignore)
19321932
clean_indentation(element, level=level+1)
19331933
element.tail = element.tail.strip(' ')

tests/unit_tests/test_materials.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,19 @@ def test_materials_deplete():
6161
# Ni59 is one of the main activation product of Ni60 in the first irradiation
6262
# step. It then decays in the second cooling step (flux = 0)
6363
assert Ni59_mat_1_step_1 > 0.0 and Ni59_mat_1_step_1 > Ni59_mat_1_step_2
64+
65+
66+
def test_export_duplicate_materials_to_xml(run_in_tmpdir):
67+
"""
68+
Test exporting Materials to xml with a duplicate and checking that only
69+
unique entities are exported.
70+
"""
71+
my_mat = openmc.Material(name="my_mat")
72+
my_mat2 = openmc.Material(name="my_mat2")
73+
74+
materials = openmc.Materials([my_mat, my_mat2, my_mat])
75+
76+
materials.export_to_xml("materials.xml")
77+
78+
materials_in = openmc.Materials.from_xml("materials.xml")
79+
assert len(materials_in) == 2

0 commit comments

Comments
 (0)