diff --git a/openmc/material.py b/openmc/material.py index 0d4fec808..c6a8e6f44 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -521,6 +521,19 @@ class Material(IDManagerMixin): if element is None: msg = 'Element name "{}" not recognised'.format(el) raise ValueError(msg) + else: + 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"""