From 3370ce1978ebea3dc70de24ffbbe47d41383c282 Mon Sep 17 00:00:00 2001 From: Catherine Yu <76599141+cxtherineyu@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:05:10 -0400 Subject: [PATCH] Print warning if no natural isotopes when using add_element and wrote unit test (#2938) Co-authored-by: Catherine Yu Co-authored-by: Paul Romano --- openmc/element.py | 5 +++++ tests/unit_tests/test_element.py | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/openmc/element.py b/openmc/element.py index f9cf102f7ce..082bee5226d 100644 --- a/openmc/element.py +++ b/openmc/element.py @@ -1,4 +1,5 @@ import re +import warnings import lxml.etree as ET @@ -123,6 +124,10 @@ def expand(self, percent, percent_type, enrichment=None, # Get the nuclides present in nature natural_nuclides = {name for name, abundance in natural_isotopes(self)} + # Issue warning if no existing nuclides + if len(natural_nuclides) == 0: + warnings.warn(f"No naturally occurring isotopes found for {self}.") + # Create dict to store the expanded nuclides and abundances abundances = {} diff --git a/tests/unit_tests/test_element.py b/tests/unit_tests/test_element.py index bacb988b9a9..d3555701e2c 100644 --- a/tests/unit_tests/test_element.py +++ b/tests/unit_tests/test_element.py @@ -1,5 +1,5 @@ import openmc -from pytest import approx, raises +from pytest import approx, raises, warns from openmc.data import NATURAL_ABUNDANCE, atomic_mass @@ -37,6 +37,13 @@ def test_expand_enrichment(): assert isotope[1] == approx(ref[isotope[0]]) +def test_expand_no_isotopes(): + """Test that correct warning is raised for elements with no isotopes""" + with warns(UserWarning, match='No naturally occurring'): + element = openmc.Element('Tc') + element.expand(100.0, 'ao') + + def test_expand_exceptions(): """ Test that correct exceptions are raised for invalid input """