From 86351747e72b9d90ec9a7b33eef1496d3a41fb7b Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 25 Mar 2020 09:37:00 -0500 Subject: [PATCH] Ensure remove_nuclide removes all matching nuclides. Closes #1532 --- openmc/material.py | 5 ++--- tests/unit_tests/test_material.py | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/openmc/material.py b/openmc/material.py index b0d5cb2229..cbd002c0bd 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -402,10 +402,9 @@ class Material(IDManagerMixin): cv.check_type('nuclide', nuclide, str) # If the Material contains the Nuclide, delete it - for nuc in self._nuclides: + for nuc in reversed(self.nuclides): if nuclide == nuc.name: - self._nuclides.remove(nuc) - break + self.nuclides.remove(nuc) def add_macroscopic(self, macroscopic): """Add a macroscopic to the material. This will also set the diff --git a/tests/unit_tests/test_material.py b/tests/unit_tests/test_material.py index 6f51135ab2..65fdb26239 100644 --- a/tests/unit_tests/test_material.py +++ b/tests/unit_tests/test_material.py @@ -11,8 +11,8 @@ def test_attributes(uo2): assert uo2.depletable -def test_nuclides(uo2): - """Test adding/removing nuclides.""" +def test_add_nuclide(): + """Test adding nuclides.""" m = openmc.Material() m.add_nuclide('U235', 1.0) with pytest.raises(TypeError): @@ -21,7 +21,18 @@ def test_nuclides(uo2): m.add_nuclide(1.0, 'H1') with pytest.raises(ValueError): m.add_nuclide('H1', 1.0, 'oa') - m.remove_nuclide('U235') + + +def test_remove_nuclide(): + """Test removing nuclides.""" + m = openmc.Material() + for nuc, percent in [('H1', 1.0), ('H2', 1.0), ('H1', 2.0), ('H2', 2.0)]: + m.add_nuclide(nuc, percent) + m.remove_nuclide('H1') + assert len(m.nuclides) == 2 + assert all(nuc.name == 'H2' for nuc in m.nuclides) + assert m.nuclides[0].percent == 1.0 + assert m.nuclides[1].percent == 2.0 def test_elements():