diff --git a/openmc/checkvalue.py b/openmc/checkvalue.py index 840306486f..42df3e5efb 100644 --- a/openmc/checkvalue.py +++ b/openmc/checkvalue.py @@ -171,6 +171,29 @@ def check_length(name, value, length_min, length_max=None): raise ValueError(msg) +def check_increasing(name: str, value, equality: bool = False): + """Ensure that a list's elements are strictly or loosely increasing. + + Parameters + ---------- + name : str + Description of value being checked + value : iterable + Object to check if increasing + equality : bool, optional + Whether equality is allowed. Defaults to False. + + """ + if equality: + if not np.all(np.diff(value) >= 0.0): + raise ValueError(f'Unable to set "{name}" to "{value}" since its ' + 'elements must be increasing.') + elif not equality: + if not np.all(np.diff(value) > 0.0): + raise ValueError(f'Unable to set "{name}" to "{value}" since its ' + 'elements must be strictly increasing.') + + def check_value(name, value, accepted_values): """Ensure that an object's value is contained in a set of acceptable values. diff --git a/openmc/mesh.py b/openmc/mesh.py index 8777da3255..48263c2055 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1820,6 +1820,8 @@ class SphericalMesh(StructuredMesh): @r_grid.setter def r_grid(self, grid): cv.check_type('mesh r_grid', grid, Iterable, Real) + cv.check_length('mesh r_grid', grid, 2) + cv.check_increasing('mesh r_grid', grid) self._r_grid = np.asarray(grid, dtype=float) @property @@ -1829,6 +1831,8 @@ class SphericalMesh(StructuredMesh): @theta_grid.setter def theta_grid(self, grid): cv.check_type('mesh theta_grid', grid, Iterable, Real) + cv.check_length('mesh theta_grid', grid, 2) + cv.check_increasing('mesh theta_grid', grid) self._theta_grid = np.asarray(grid, dtype=float) @property @@ -1838,6 +1842,8 @@ class SphericalMesh(StructuredMesh): @phi_grid.setter def phi_grid(self, grid): cv.check_type('mesh phi_grid', grid, Iterable, Real) + cv.check_length('mesh phi_grid', grid, 2) + cv.check_increasing('mesh phi_grid', grid) self._phi_grid = np.asarray(grid, dtype=float) @property diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index c499306506..5bacd5fc69 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -132,6 +132,27 @@ def test_SphericalMesh_initiation(): mesh.r_grid = (0, 11) assert (mesh.r_grid == np.array([0., 11.])).all() + # test invalid r_grid values + with pytest.raises(ValueError): + openmc.SphericalMesh(r_grid=[1, 1]) + + with pytest.raises(ValueError): + openmc.SphericalMesh(r_grid=[0]) + + # test invalid theta_grid values + with pytest.raises(ValueError): + openmc.SphericalMesh(r_grid=[1, 2], theta_grid=[1, 1]) + + with pytest.raises(ValueError): + openmc.SphericalMesh(r_grid=[1, 2], theta_grid=[0]) + + # test invalid phi_grid values + with pytest.raises(ValueError): + openmc.SphericalMesh(r_grid=[1, 2], phi_grid=[1, 1]) + + with pytest.raises(ValueError): + openmc.SphericalMesh(r_grid=[1, 2], phi_grid=[0]) + # waffles and pancakes are unfortunately not valid radii with pytest.raises(TypeError): openmc.SphericalMesh(('🧇', '🥞'))