From b3d0f20a0010dd930e4f403848d72164f32b294e Mon Sep 17 00:00:00 2001 From: yardasol Date: Wed, 27 Jul 2022 13:15:10 -0500 Subject: [PATCH] added add_nuclides method --- openmc/material.py | 14 ++++++++++++++ tests/unit_tests/test_material.py | 28 +++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/openmc/material.py b/openmc/material.py index a837b1f17..cd181539c 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -403,6 +403,20 @@ class Material(IDManagerMixin): self._nuclides.append(NuclideTuple(nuclide, percent, percent_type)) + def add_nuclides(self, nuclides: dict): + """ Add multiple nuclides to a material + + Parameters + ---------- + nuclides : dict of str to tuple + Dictionary mapping nuclide names to a tuple containing their + atom or weight percent. + + """ + + for nuclide, (percent, percent_type) in nuclides.items(): + self.add_nuclide(nuclide, percent, percent_type) + def remove_nuclide(self, nuclide: str): """Remove a nuclide from the material diff --git a/tests/unit_tests/test_material.py b/tests/unit_tests/test_material.py index fd0e4f624..8dde30485 100644 --- a/tests/unit_tests/test_material.py +++ b/tests/unit_tests/test_material.py @@ -25,6 +25,32 @@ def test_add_nuclide(): with pytest.raises(ValueError): m.add_nuclide('H1', 1.0, 'oa') +def test_add_nuclides(): + """Test adding multipe nuclides at once""" + m = openmc.Material() + nuclides = {'H1': (2.0, 'ao'), + 'O16': (1.0, 'wo')} + m.add_nuclides(nuclides) + + # wt-% + m = openmc.Material() + nuclides = {'H1': (2.0, 'wo'), + 'O16': (1.0, 'wo')} + m.add_nuclides(nuclides) + + # mixed + m = openmc.Material() + nuclides = {'H1': (2.0, 'ao'), + 'O16': (1.0, 'wo')} + m.add_nuclides(nuclides) + + with pytest.raises(TypeError): + m.add_nuclides({'H1': ('1.0', 'ao')}) + with pytest.raises(TypeError): + m.add_nuclides({1.0: ('H1', 'wo')}) + with pytest.raises(ValueError): + m.add_nuclides({'H1': (1.0, 'oa')}) + def test_remove_nuclide(): """Test removing nuclides.""" @@ -441,7 +467,7 @@ def test_activity_of_tritium(): m1.add_nuclide("H3", 1) m1.set_density('g/cm3', 1) m1.volume = 1 - assert pytest.approx(m1.activity) == 3.559778e14 + assert pytest.approx(m1.activity) == 3.559778e14 def test_activity_of_metastable():