From 396ecdf02cb13eed34940c84606f4d8d890790b7 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Thu, 26 Jan 2023 13:55:22 -0500 Subject: [PATCH 1/7] added test that cathces the bug --- tests/unit_tests/test_mesh.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/unit_tests/test_mesh.py diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py new file mode 100644 index 0000000000..08dd65e33f --- /dev/null +++ b/tests/unit_tests/test_mesh.py @@ -0,0 +1,9 @@ +import openmc +import pytest + +def test_raises_error_when_flat(): + with pytest.raises(ValueError): + mesh = openmc.RegularMesh() + mesh.dimension = [50, 50, 1] + mesh.lower_left = [-25, -25, 0] + mesh.upper_right = [25, 25, 0] \ No newline at end of file From 91ebcdb21e80174bba37a6284c73a8972be0e114 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Thu, 26 Jan 2023 14:05:23 -0500 Subject: [PATCH 2/7] better test --- tests/unit_tests/test_mesh.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 08dd65e33f..129e8e21b5 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -1,9 +1,34 @@ import openmc import pytest -def test_raises_error_when_flat(): +@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 = openmc.RegularMesh() - mesh.dimension = [50, 50, 1] - mesh.lower_left = [-25, -25, 0] - mesh.upper_right = [25, 25, 0] \ No newline at end of file + 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] From a9767035561f347053ac0e89b4a2be56233b88dc Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Thu, 26 Jan 2023 14:06:48 -0500 Subject: [PATCH 3/7] Raise error if mesh is flat --- openmc/mesh.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/openmc/mesh.py b/openmc/mesh.py index 157e782633..962751f9b3 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -433,6 +433,9 @@ class RegularMesh(StructuredMesh): cv.check_length('mesh lower_left', lower_left, 1, 3) self._lower_left = lower_left + if self.is_flat(): + raise ValueError("mesh cannot be flat") + @upper_right.setter def upper_right(self, upper_right): cv.check_type('mesh upper_right', upper_right, Iterable, Real) @@ -442,6 +445,9 @@ class RegularMesh(StructuredMesh): if self._width is not None: self._width = None warnings.warn("Unsetting width attribute.") + + if self.is_flat(): + raise ValueError("mesh cannot be flat") @width.setter def width(self, width): @@ -786,6 +792,16 @@ class RegularMesh(StructuredMesh): volume_normalization=volume_normalization ) + def is_flat(self): + """Returns True if the mesh is flat + """ + if None in [self.lower_left, self.upper_right]: + return False + + for val1, val2 in zip(self.lower_left, self.upper_right): + if val1 == val2: + return True + def Mesh(*args, **kwargs): warnings.warn("Mesh has been renamed RegularMesh. Future versions of " "OpenMC will not accept the name Mesh.") From ba1cd67d52425820d318d618faf9d5bf06f62e95 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Fri, 27 Jan 2023 08:32:02 -0500 Subject: [PATCH 4/7] added test to catch bug + fix --- openmc/mesh.py | 2 +- tests/unit_tests/test_mesh.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 962751f9b3..3cdfc87f3b 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -795,7 +795,7 @@ class RegularMesh(StructuredMesh): def is_flat(self): """Returns True if the mesh is flat """ - if None in [self.lower_left, self.upper_right]: + if self.lower_left is None or self.upper_right is None: return False for val1, val2 in zip(self.lower_left, self.upper_right): diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 129e8e21b5..8bc00a2768 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -1,5 +1,6 @@ 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): @@ -32,3 +33,25 @@ def test_raises_error_when_flat(val_left, val_right): with pytest.raises(ValueError): mesh.upper_right = [25, 25, val_right] mesh.lower_left = [-25, -25, val_left] + + +def test_corner_none_returns_false(): + """Checks mesh is not considered flat when one + corner is None + """ + mesh = openmc.RegularMesh() + mesh.lower_left = [-25, -25, -25] + assert not mesh.is_flat() + + mesh = openmc.RegularMesh() + mesh.upper_right = [-25, -25, -25] + assert not mesh.is_flat() + + # test with np array + mesh = openmc.RegularMesh() + mesh.lower_left = np.array([-25, -25, -25]) + assert not mesh.is_flat() + + mesh = openmc.RegularMesh() + mesh.upper_right = np.array([-25, -25, -25]) + assert not mesh.is_flat() \ No newline at end of file From c84336878bdfb38c261094205ab5360949d6743d Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Fri, 27 Jan 2023 08:58:10 -0500 Subject: [PATCH 5/7] replace is_flat by a one-line check --- openmc/mesh.py | 18 ++++-------------- tests/unit_tests/test_mesh.py | 22 ---------------------- 2 files changed, 4 insertions(+), 36 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 3cdfc87f3b..60c5ade40d 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -433,8 +433,8 @@ class RegularMesh(StructuredMesh): cv.check_length('mesh lower_left', lower_left, 1, 3) self._lower_left = lower_left - if self.is_flat(): - raise ValueError("mesh cannot be flat") + if self.upper_right is not None and any(np.isclose(self.upper_right, lower_left)): + raise ValueError("mesh cannot have zero thickness is any dimension") @upper_right.setter def upper_right(self, upper_right): @@ -446,8 +446,8 @@ class RegularMesh(StructuredMesh): self._width = None warnings.warn("Unsetting width attribute.") - if self.is_flat(): - raise ValueError("mesh cannot be flat") + if self.lower_left is not None and any(np.isclose(self.lower_left, upper_right)): + raise ValueError("mesh cannot have zero thickness is any dimension") @width.setter def width(self, width): @@ -792,16 +792,6 @@ class RegularMesh(StructuredMesh): volume_normalization=volume_normalization ) - def is_flat(self): - """Returns True if the mesh is flat - """ - if self.lower_left is None or self.upper_right is None: - return False - - for val1, val2 in zip(self.lower_left, self.upper_right): - if val1 == val2: - return True - def Mesh(*args, **kwargs): warnings.warn("Mesh has been renamed RegularMesh. Future versions of " "OpenMC will not accept the name Mesh.") diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 8bc00a2768..0f9b96b0b3 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -33,25 +33,3 @@ def test_raises_error_when_flat(val_left, val_right): with pytest.raises(ValueError): mesh.upper_right = [25, 25, val_right] mesh.lower_left = [-25, -25, val_left] - - -def test_corner_none_returns_false(): - """Checks mesh is not considered flat when one - corner is None - """ - mesh = openmc.RegularMesh() - mesh.lower_left = [-25, -25, -25] - assert not mesh.is_flat() - - mesh = openmc.RegularMesh() - mesh.upper_right = [-25, -25, -25] - assert not mesh.is_flat() - - # test with np array - mesh = openmc.RegularMesh() - mesh.lower_left = np.array([-25, -25, -25]) - assert not mesh.is_flat() - - mesh = openmc.RegularMesh() - mesh.upper_right = np.array([-25, -25, -25]) - assert not mesh.is_flat() \ No newline at end of file From 198b5bdad44557beaf8ed27b7bc5bac529252528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Delaporte-Mathurin?= <40028739+RemDelaporteMathurin@users.noreply.github.com> Date: Sat, 28 Jan 2023 08:50:39 -0500 Subject: [PATCH 6/7] Update openmc/mesh.py Co-authored-by: Paul Romano --- openmc/mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 60c5ade40d..84350afd37 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -434,7 +434,7 @@ class RegularMesh(StructuredMesh): 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 is any dimension") + raise ValueError("Mesh cannot have zero thickness in any dimension") @upper_right.setter def upper_right(self, upper_right): From a51d31ec78d6c9bebb99fb7b92a68ab145da56fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Delaporte-Mathurin?= <40028739+RemDelaporteMathurin@users.noreply.github.com> Date: Sat, 28 Jan 2023 08:50:45 -0500 Subject: [PATCH 7/7] Update openmc/mesh.py Co-authored-by: Paul Romano --- openmc/mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 84350afd37..dda7257f45 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -447,7 +447,7 @@ class RegularMesh(StructuredMesh): 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 is any dimension") + raise ValueError("Mesh cannot have zero thickness in any dimension") @width.setter def width(self, width):