added extra error checking on spherical mesh creation (#2973)

Co-authored-by: Jonathan Shimwell <drshimwell@gmail.com>
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
This commit is contained in:
Chris Wagner 2024-05-24 23:27:37 -04:00 committed by GitHub
parent 12ecc17997
commit 18cd81a6aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 0 deletions

View file

@ -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.

View file

@ -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

View file

@ -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(('🧇', '🥞'))