From 21dc58bff188fbe122686280af5adcc1ce38a5f3 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Mon, 17 May 2021 17:50:08 +0100 Subject: [PATCH 1/2] changing element to element.title to catch lowercase entries --- openmc/material.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/openmc/material.py b/openmc/material.py index 0d4fec808..eeac3e637 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -521,6 +521,8 @@ class Material(IDManagerMixin): if element is None: msg = 'Element name "{}" not recognised'.format(el) raise ValueError(msg) + else: + element = element.title() if self._macroscopic is not None: msg = 'Unable to add an Element to Material ID="{}" as a ' \ From 90ff8fcb4a312ed1bf2b2563f44b16b915a2f5e3 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 19 May 2021 13:31:45 +0100 Subject: [PATCH 2/2] added checks and tests for cases in add_element --- openmc/material.py | 13 ++++++++++++- tests/unit_tests/test_material.py | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/openmc/material.py b/openmc/material.py index eeac3e637..c6a8e6f44 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -522,7 +522,18 @@ class Material(IDManagerMixin): msg = 'Element name "{}" not recognised'.format(el) raise ValueError(msg) else: - element = element.title() + if element[0].islower(): + msg = 'Element name "{}" should start with an uppercase ' \ + 'letter'.format(element) + raise ValueError(msg) + if len(element) == 2 and element[1].isupper(): + msg = 'Element name "{}" should end with a lowercase ' \ + 'letter'.format(element) + raise ValueError(msg) + # skips the first entry of ATOMIC_SYMBOL which is n for neutron + if element not in list(openmc.data.ATOMIC_SYMBOL.values())[1:]: + msg = 'Element name "{}" not recognised'.format(element) + raise ValueError(msg) if self._macroscopic is not None: msg = 'Unable to add an Element to Material ID="{}" as a ' \ diff --git a/tests/unit_tests/test_material.py b/tests/unit_tests/test_material.py index ee8c54da9..02e53a28d 100644 --- a/tests/unit_tests/test_material.py +++ b/tests/unit_tests/test_material.py @@ -54,6 +54,15 @@ def test_elements(): m.add_element('U', 1.0, enrichment=70.0, enrichment_target='U235') with pytest.raises(ValueError): m.add_element('He', 1.0, enrichment=17.0, enrichment_target='He6') + with pytest.raises(ValueError): + m.add_element('li', 1.0) # should fail as 1st char is lowercase + with pytest.raises(ValueError): + m.add_element('LI', 1.0) # should fail as 2nd char is uppercase + with pytest.raises(ValueError): + m.add_element('Xx', 1.0) # should fail as Xx is not an element + with pytest.raises(ValueError): + m.add_element('n', 1.0) # check to avoid n for neutron being accepted + def test_elements_by_name(): """Test adding elements by name"""