added error checking on cylindrical mesh (#2977)

Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
This commit is contained in:
hsameer481 2024-06-10 17:31:00 -04:00 committed by GitHub
parent 12a278b1ac
commit e971bd1213
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 4 deletions

View file

@ -1388,6 +1388,8 @@ class CylindricalMesh(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
@ -1397,7 +1399,12 @@ class CylindricalMesh(StructuredMesh):
@phi_grid.setter
def phi_grid(self, grid):
cv.check_type('mesh phi_grid', grid, Iterable, Real)
self._phi_grid = np.asarray(grid, dtype=float)
cv.check_length('mesh phi_grid', grid, 2)
cv.check_increasing('mesh phi_grid', grid)
grid = np.asarray(grid, dtype=float)
if np.any((grid < 0.0) | (grid > 2*pi)):
raise ValueError("phi_grid values must be in [0, 2π].")
self._phi_grid = grid
@property
def z_grid(self):
@ -1406,6 +1413,8 @@ class CylindricalMesh(StructuredMesh):
@z_grid.setter
def z_grid(self, grid):
cv.check_type('mesh z_grid', grid, Iterable, Real)
cv.check_length('mesh z_grid', grid, 2)
cv.check_increasing('mesh z_grid', grid)
self._z_grid = np.asarray(grid, dtype=float)
@property
@ -1840,7 +1849,10 @@ class SphericalMesh(StructuredMesh):
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)
grid = np.asarray(grid, dtype=float)
if np.any((grid < 0.0) | (grid > pi)):
raise ValueError("theta_grid values must be in [0, π].")
self._theta_grid = grid
@property
def phi_grid(self):
@ -1851,7 +1863,10 @@ class SphericalMesh(StructuredMesh):
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)
grid = np.asarray(grid, dtype=float)
if np.any((grid < 0.0) | (grid > 2*pi)):
raise ValueError("phi_grid values must be in [0, 2π].")
self._phi_grid = grid
@property
def _grids(self):

View file

@ -153,7 +153,7 @@ def test_write_data_to_vtk_round_trip(run_in_tmpdir):
smesh = openmc.SphericalMesh(
r_grid=(0.0, 1.0, 2.0),
theta_grid=(0.0, 2.0, 4.0, 5.0),
theta_grid=(0.0, 0.5, 1.0, 2.0),
phi_grid=(0.0, 3.0, 6.0),
)
rmesh = openmc.RegularMesh()

View file

@ -189,6 +189,42 @@ def test_CylindricalMesh_initiation():
openmc.SphericalMesh(('🧇', '🥞'))
def test_invalid_cylindrical_mesh_errors():
# Test invalid r_grid values
with pytest.raises(ValueError):
openmc.CylindricalMesh(r_grid=[5, 1], phi_grid=[0, pi], z_grid=[0, 10])
with pytest.raises(ValueError):
openmc.CylindricalMesh(r_grid=[1, 2, 4, 3], phi_grid=[0, pi], z_grid=[0, 10])
with pytest.raises(ValueError):
openmc.CylindricalMesh(r_grid=[1], phi_grid=[0, pi], z_grid=[0, 10])
# Test invalid phi_grid values
with pytest.raises(ValueError):
openmc.CylindricalMesh(r_grid=[0, 1, 2], phi_grid=[-1, 3], z_grid=[0, 10])
with pytest.raises(ValueError):
openmc.CylindricalMesh(
r_grid=[0, 1, 2],
phi_grid=[0, 2*pi + 0.1],
z_grid=[0, 10]
)
with pytest.raises(ValueError):
openmc.CylindricalMesh(r_grid=[0, 1, 2], phi_grid=[pi], z_grid=[0, 10])
# Test invalid z_grid values
with pytest.raises(ValueError):
openmc.CylindricalMesh(r_grid=[0, 1, 2], phi_grid=[0, pi], z_grid=[5])
with pytest.raises(ValueError):
openmc.CylindricalMesh(r_grid=[0, 1, 2], phi_grid=[0, pi], z_grid=[5, 1])
with pytest.raises(ValueError):
openmc.CylindricalMesh(r_grid=[1, 2, 4, 3], phi_grid=[0, pi], z_grid=[0, 10, 5])
def test_centroids():
# regular mesh
mesh = openmc.RegularMesh()