Skip to content

Commit

Permalink
Merge pull request #2363 from RemDelaporteMathurin/fix_flat_mesh
Browse files Browse the repository at this point in the history
Raise error when mesh is flat
  • Loading branch information
shimwell authored Feb 2, 2023
2 parents b8a3586 + a51d31e commit 5c672f5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions openmc/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ def lower_left(self, lower_left):
cv.check_length('mesh lower_left', lower_left, 1, 3)
self._lower_left = lower_left

if self.upper_right is not None and any(np.isclose(self.upper_right, lower_left)):
raise ValueError("Mesh cannot have zero thickness in any dimension")

@upper_right.setter
def upper_right(self, upper_right):
cv.check_type('mesh upper_right', upper_right, Iterable, Real)
Expand All @@ -442,6 +445,9 @@ def upper_right(self, upper_right):
if self._width is not None:
self._width = None
warnings.warn("Unsetting width attribute.")

if self.lower_left is not None and any(np.isclose(self.lower_left, upper_right)):
raise ValueError("Mesh cannot have zero thickness in any dimension")

@width.setter
def width(self, width):
Expand Down
35 changes: 35 additions & 0 deletions tests/unit_tests/test_mesh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import openmc
import pytest
import numpy as np

@pytest.mark.parametrize("val_left,val_right", [(0, 0), (-1., -1.), (2.0, 2)])
def test_raises_error_when_flat(val_left, val_right):
"""Checks that an error is raised when a mesh is flat"""
mesh = openmc.RegularMesh()

# Same X
with pytest.raises(ValueError):
mesh.lower_left = [val_left, -25, -25]
mesh.upper_right = [val_right, 25, 25]

with pytest.raises(ValueError):
mesh.upper_right = [val_right, 25, 25]
mesh.lower_left = [val_left, -25, -25]

# Same Y
with pytest.raises(ValueError):
mesh.lower_left = [-25, val_left, -25]
mesh.upper_right = [25, val_right, 25]

with pytest.raises(ValueError):
mesh.upper_right = [25, val_right, 25]
mesh.lower_left = [-25, val_left, -25]

# Same Z
with pytest.raises(ValueError):
mesh.lower_left = [-25, -25, val_left]
mesh.upper_right = [25, 25, val_right]

with pytest.raises(ValueError):
mesh.upper_right = [25, 25, val_right]
mesh.lower_left = [-25, -25, val_left]

0 comments on commit 5c672f5

Please sign in to comment.