Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added error checking on cylindrical mesh #2977

Merged
merged 5 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions openmc/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,8 @@ def r_grid(self):
@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
Expand All @@ -1390,7 +1392,12 @@ def phi_grid(self):
@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):
Expand All @@ -1399,6 +1406,8 @@ def z_grid(self):
@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
Expand Down Expand Up @@ -1833,7 +1842,10 @@ 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)
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):
Expand All @@ -1844,7 +1856,10 @@ 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)
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):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/mesh_to_vtk/test_vtk_dims.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
36 changes: 36 additions & 0 deletions tests/unit_tests/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down