From a6f4df0698c2f7dc004303383d882af4244b8597 Mon Sep 17 00:00:00 2001 From: caderche2014 Date: Sun, 23 Jul 2023 10:52:34 +0100 Subject: [PATCH 1/9] added spherical bounding box functionality with tests. Modified name of existing test --- openmc/mesh.py | 48 +++++++++++++++++++++++++++++++++++ tests/unit_tests/test_mesh.py | 30 ++++++++++++++++++++-- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 8e9d3fadbe0..24980bb2474 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1005,6 +1005,12 @@ class RectilinearMesh(StructuredMesh): indices : Iterable of tuple An iterable of mesh indices for each mesh element, e.g. [(1, 1, 1), (2, 1, 1), ...] + lower_left : Iterable of float + The lower-left corner of the structured mesh. If only two coordinate + are given, it is assumed that the mesh is an x-y mesh. + upper_right : Iterable of float + The upper-right corner of the structured mesh. If only two coordinate + are given, it is assumed that the mesh is an x-y mesh. """ @@ -1511,6 +1517,12 @@ class SphericalMesh(StructuredMesh): indices : Iterable of tuple An iterable of mesh indices for each mesh element, e.g. [(1, 1, 1), (2, 1, 1), ...] + lower_left : Iterable of float + The lower-left corner of the structured mesh. If only two coordinate + are given, it is assumed that the mesh is an x-y mesh. + upper_right : Iterable of float + The upper-right corner of the structured mesh. If only two coordinate + are given, it is assumed that the mesh is an x-y mesh. """ @@ -1583,6 +1595,42 @@ def indices(self): for t in range(1, nt + 1) for r in range(1, nr + 1)) + @property + def lower_left(self): + return (self.origin[0]-self.r_grid[-1], self.origin[1]-self.r_grid[-1], self.origin[2]-self.r_grid[-1]) + + @lower_left.setter + def lower_left(self, lower_left): + cv.check_type('mesh lower_left', lower_left, Iterable, Real) + 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") + + @property + def upper_right(self): + return (self.origin[0]+self.r_grid[-1], self.origin[1]+self.r_grid[-1], self.origin[2]+self.r_grid[-1]) + + @upper_right.setter + def upper_right(self, upper_right): + cv.check_type('mesh upper_right', upper_right, Iterable, Real) + cv.check_length('mesh upper_right', upper_right, 1, 3) + self._upper_right = 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") + + @property + def bounding_box(self): + return openmc.BoundingBox( + np.array(self.upper_right), np.array(self.lower_left) + ) + def __repr__(self): fmt = '{0: <16}{1}{2}\n' string = super().__repr__() diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 4a09df0455d..fe4b514bccc 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -35,12 +35,38 @@ def test_raises_error_when_flat(val_left, val_right): mesh.lower_left = [-25, -25, val_left] -def test_mesh_bounding_box(): +def test_mesh_regular_bounding_box(): mesh = openmc.RegularMesh() mesh.lower_left = [-2, -3 ,-5] mesh.upper_right = [2, 3, 5] - bb = mesh.bounding_box + bb = mesh.bounding_box() assert isinstance(bb, openmc.BoundingBox) np.testing.assert_array_equal(bb.lower_left, np.array([-2, -3 ,-5])) np.testing.assert_array_equal(bb.upper_right, np.array([2, 3, 5])) +def test_mesh_spherical_bounding_box(): + + + mesh = openmc.SphericalMesh() + mesh.r_grid = np.array([0.1, 0.2, 0.5, 1.]) + + #test with mesh at origin 0, 0, 0 + mesh.origin = (0., 0., 0.) + assert mesh.upper_right == (1., 1., 1.) + assert mesh.lower_left == (-1., -1., -1.) + bb = mesh.bounding_box + assert isinstance(bb, openmc.BoundingBox) + np.testing.assert_array_equal(bb.lower_left, np.array([-1., -1., -1.])) + np.testing.assert_array_equal(bb.upper_right, np.array([1., 1., 1.])) + + # test with mesh at origin 3, 5, 7 + mesh.origin = (3., 5., 7.) + assert mesh.upper_right == (4., 6., 8.) + assert mesh.lower_left == (2., 4., 6.) + bb = mesh.bounding_box + assert isinstance(bb, openmc.BoundingBox) + np.testing.assert_array_equal(bb.lower_left, np.array([2., 4., 6.])) + np.testing.assert_array_equal(bb.upper_right, np.array([4., 6., 8.])) + + + From d4cca699e4d41f1d9f439d3724c958f00ec84c8c Mon Sep 17 00:00:00 2001 From: caderche2014 Date: Sun, 23 Jul 2023 11:27:35 +0100 Subject: [PATCH 2/9] Second commit to spherical bounding box --- openmc/mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 24980bb2474..880050fedc4 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1595,7 +1595,7 @@ def indices(self): for t in range(1, nt + 1) for r in range(1, nr + 1)) - @property + @property def lower_left(self): return (self.origin[0]-self.r_grid[-1], self.origin[1]-self.r_grid[-1], self.origin[2]-self.r_grid[-1]) From e2680e44979c30910f1b1d46ef859e6c8c505c8f Mon Sep 17 00:00:00 2001 From: caderche2014 Date: Sun, 23 Jul 2023 12:24:28 +0100 Subject: [PATCH 3/9] Replaced mesh.bounding_box() with mesh.bounding_box because bb is not callable --- tests/unit_tests/test_mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index fe4b514bccc..498eeb75d35 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -39,7 +39,7 @@ def test_mesh_regular_bounding_box(): mesh = openmc.RegularMesh() mesh.lower_left = [-2, -3 ,-5] mesh.upper_right = [2, 3, 5] - bb = mesh.bounding_box() + bb = mesh.bounding_box assert isinstance(bb, openmc.BoundingBox) np.testing.assert_array_equal(bb.lower_left, np.array([-2, -3 ,-5])) np.testing.assert_array_equal(bb.upper_right, np.array([2, 3, 5])) From 3d2aa4f67fedfb7276f50c324cb403f56e3f1656 Mon Sep 17 00:00:00 2001 From: caderche2014 Date: Sun, 23 Jul 2023 13:23:33 +0100 Subject: [PATCH 4/9] Removed floats from literals in test, and swaped upper_right and lower_left in BB --- openmc/mesh.py | 4 ++-- tests/unit_tests/test_mesh.py | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 880050fedc4..836aa8df0ce 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -663,7 +663,7 @@ def _grids(self): @property def bounding_box(self): return openmc.BoundingBox( - np.array(self.lower_left), np.array(self.upper_right) + np.array(self.lower_left), np.array(self.upper_right) ) def __repr__(self): @@ -1628,7 +1628,7 @@ def upper_right(self, upper_right): @property def bounding_box(self): return openmc.BoundingBox( - np.array(self.upper_right), np.array(self.lower_left) + np.array(self.lower_left), np.array(self.upper_right) ) def __repr__(self): diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 498eeb75d35..157572f5d9a 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -51,22 +51,22 @@ def test_mesh_spherical_bounding_box(): mesh.r_grid = np.array([0.1, 0.2, 0.5, 1.]) #test with mesh at origin 0, 0, 0 - mesh.origin = (0., 0., 0.) - assert mesh.upper_right == (1., 1., 1.) - assert mesh.lower_left == (-1., -1., -1.) + mesh.origin = (0, 0, 0) + assert mesh.upper_right == (1, 1, 1) + assert mesh.lower_left == (-1, -1, -1) bb = mesh.bounding_box assert isinstance(bb, openmc.BoundingBox) - np.testing.assert_array_equal(bb.lower_left, np.array([-1., -1., -1.])) - np.testing.assert_array_equal(bb.upper_right, np.array([1., 1., 1.])) + np.testing.assert_array_equal(bb.lower_left, np.array([-1, -1, -1])) + np.testing.assert_array_equal(bb.upper_right, np.array([1, 1, 1])) # test with mesh at origin 3, 5, 7 - mesh.origin = (3., 5., 7.) - assert mesh.upper_right == (4., 6., 8.) - assert mesh.lower_left == (2., 4., 6.) + mesh.origin = (3, 5, 7) + assert mesh.upper_right == (4, 6, 8) + assert mesh.lower_left == (2, 4, 6) bb = mesh.bounding_box assert isinstance(bb, openmc.BoundingBox) - np.testing.assert_array_equal(bb.lower_left, np.array([2., 4., 6.])) - np.testing.assert_array_equal(bb.upper_right, np.array([4., 6., 8.])) + np.testing.assert_array_equal(bb.lower_left, np.array([2, 4, 6])) + np.testing.assert_array_equal(bb.upper_right, np.array([4, 6, 8])) From 66d6de6ac0b0abf1f2208408d8682f9ed08284c8 Mon Sep 17 00:00:00 2001 From: caderache2014 Date: Thu, 3 Aug 2023 12:53:41 +0100 Subject: [PATCH 5/9] Rewriting spherical bounding box changes without setters --- openmc/mesh.py | 28 ++++++++++++++++++++++++++++ tests/unit_tests/test_mesh.py | 26 +++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 8e9d3fadbe0..cf0254643ac 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -504,12 +504,16 @@ class RegularMesh(StructuredMesh): upper_right : Iterable of float The upper-right corner of the structured mesh. If only two coordinate are given, it is assumed that the mesh is an x-y mesh. + bounding_box: openmc.BoundingBox + Axis-aligned bounding box of the cell defined by the upper-right and lower- + left coordinates width : Iterable of float The width of mesh cells in each direction. indices : Iterable of tuple An iterable of mesh indices for each mesh element, e.g. [(1, 1, 1), (2, 1, 1), ...] + """ def __init__(self, mesh_id: typing.Optional[int] = None, name: str = ''): @@ -1511,6 +1515,15 @@ class SphericalMesh(StructuredMesh): indices : Iterable of tuple An iterable of mesh indices for each mesh element, e.g. [(1, 1, 1), (2, 1, 1), ...] + lower_left : Iterable of float + The lower-left corner of the structured mesh. If only two coordinate + are given, it is assumed that the mesh is an x-y mesh. + upper_right : Iterable of float + The upper-right corner of the structured mesh. If only two coordinate + are given, it is assumed that the mesh is an x-y mesh. + bounding_box: openmc.BoundingBox + Axis-aligned bounding box of the cell defined by the upper-right and lower- + left coordinates """ @@ -1583,6 +1596,21 @@ def indices(self): for t in range(1, nt + 1) for r in range(1, nr + 1)) + @property + def lower_left(self): + return (self.origin[0]-self.r_grid[-1], self.origin[1]-self.r_grid[-1], self.origin[2]-self.r_grid[-1]) + + @property + def upper_right(self): + return (self.origin[0]+self.r_grid[-1], self.origin[1]+self.r_grid[-1], self.origin[2]+self.r_grid[-1]) + + @property + def bounding_box(self): + return openmc.BoundingBox( + np.array(self.lower_left), np.array(self.upper_right) + ) + + def __repr__(self): fmt = '{0: <16}{1}{2}\n' string = super().__repr__() diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 4a09df0455d..e628b35432d 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -35,7 +35,7 @@ def test_raises_error_when_flat(val_left, val_right): mesh.lower_left = [-25, -25, val_left] -def test_mesh_bounding_box(): +def test_regular_mesh_bounding_box(): mesh = openmc.RegularMesh() mesh.lower_left = [-2, -3 ,-5] mesh.upper_right = [2, 3, 5] @@ -44,3 +44,27 @@ def test_mesh_bounding_box(): np.testing.assert_array_equal(bb.lower_left, np.array([-2, -3 ,-5])) np.testing.assert_array_equal(bb.upper_right, np.array([2, 3, 5])) + +def test_spherical_mesh_bounding_box(): + mesh = openmc.SphericalMesh() + mesh.r_grid = np.array([0.1, 0.2, 0.5, 1.]) + + #test with mesh at origin 0, 0, 0 + mesh.origin = (0, 0, 0) + assert mesh.upper_right == (1, 1, 1) + assert mesh.lower_left == (-1, -1, -1) + bb = mesh.bounding_box + assert isinstance(bb, openmc.BoundingBox) + np.testing.assert_array_equal(bb.lower_left, np.array([-1, -1, -1])) + np.testing.assert_array_equal(bb.upper_right, np.array([1, 1, 1])) + + # test with mesh at origin 3, 5, 7 + mesh.origin = (3, 5, 7) + assert mesh.upper_right == (4, 6, 8) + assert mesh.lower_left == (2, 4, 6) + bb = mesh.bounding_box + assert isinstance(bb, openmc.BoundingBox) + np.testing.assert_array_equal(bb.lower_left, np.array([2, 4, 6])) + np.testing.assert_array_equal(bb.upper_right, np.array([4, 6, 8])) + + From 507f5843a164f25ab363e811e2b61fc62f8c9b3f Mon Sep 17 00:00:00 2001 From: Christopher Billingham Date: Fri, 4 Aug 2023 10:46:55 +0100 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Jonathan Shimwell --- openmc/mesh.py | 2 +- tests/unit_tests/test_mesh.py | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 4dfe2eaf0aa..34598f703b1 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -667,7 +667,7 @@ def _grids(self): @property def bounding_box(self): return openmc.BoundingBox( - np.array(self.lower_left), np.array(self.upper_right) + np.array(self.lower_left), np.array(self.upper_right) ) def __repr__(self): diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 2185dd030a6..0ea3c7726bf 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -47,7 +47,6 @@ def test_regular_mesh_bounding_box(): def test_spherical_mesh_bounding_box(): - mesh = openmc.SphericalMesh() mesh.r_grid = np.array([0.1, 0.2, 0.5, 1.]) #test with mesh at origin 0, 0, 0 @@ -67,6 +66,3 @@ def test_spherical_mesh_bounding_box(): assert isinstance(bb, openmc.BoundingBox) np.testing.assert_array_equal(bb.lower_left, np.array([2, 4, 6])) np.testing.assert_array_equal(bb.upper_right, np.array([4, 6, 8])) - - - From 01130acdaca11b2bd0b2e577fa4d0745c1837e95 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 4 Aug 2023 11:38:35 +0100 Subject: [PATCH 7/9] Apply suggestions from code review Co-authored-by: Christopher Billingham --- openmc/mesh.py | 1 - tests/unit_tests/test_mesh.py | 1 - 2 files changed, 2 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 34598f703b1..f68f2496f2f 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -513,7 +513,6 @@ class RegularMesh(StructuredMesh): An iterable of mesh indices for each mesh element, e.g. [(1, 1, 1), (2, 1, 1), ...] - """ def __init__(self, mesh_id: typing.Optional[int] = None, name: str = ''): diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 0ea3c7726bf..fcf7c3d7694 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -36,7 +36,6 @@ def test_raises_error_when_flat(val_left, val_right): def test_regular_mesh_bounding_box(): - mesh = openmc.RegularMesh() mesh.lower_left = [-2, -3 ,-5] mesh.upper_right = [2, 3, 5] From 1a59fb9b7ec36e17f4aa82fb232bf356ee1c9865 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Sat, 5 Aug 2023 16:24:41 -0400 Subject: [PATCH 8/9] Small edits for SphericlaMesh bounding_box --- openmc/mesh.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index f68f2496f2f..5d474243f0b 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1008,12 +1008,6 @@ class RectilinearMesh(StructuredMesh): indices : Iterable of tuple An iterable of mesh indices for each mesh element, e.g. [(1, 1, 1), (2, 1, 1), ...] - lower_left : Iterable of float - The lower-left corner of the structured mesh. If only two coordinate - are given, it is assumed that the mesh is an x-y mesh. - upper_right : Iterable of float - The upper-right corner of the structured mesh. If only two coordinate - are given, it is assumed that the mesh is an x-y mesh. """ @@ -1520,13 +1514,13 @@ class SphericalMesh(StructuredMesh): indices : Iterable of tuple An iterable of mesh indices for each mesh element, e.g. [(1, 1, 1), (2, 1, 1), ...] - lower_left : Iterable of float + lower_left : numpy.ndarray The lower-left corner of the structured mesh. If only two coordinate are given, it is assumed that the mesh is an x-y mesh. - upper_right : Iterable of float + upper_right : numpy.ndarray The upper-right corner of the structured mesh. If only two coordinate are given, it is assumed that the mesh is an x-y mesh. - bounding_box: openmc.BoundingBox + bounding_box : openmc.BoundingBox Axis-aligned bounding box of the cell defined by the upper-right and lower- left coordinates @@ -1603,17 +1597,17 @@ def indices(self): @property def lower_left(self): - return (self.origin[0]-self.r_grid[-1], self.origin[1]-self.r_grid[-1], self.origin[2]-self.r_grid[-1]) + r = self.r_grid[-1] + return np.array((self.origin[0] - r, self.origin[1] - r, self.origin[2] - r)) @property def upper_right(self): - return (self.origin[0]+self.r_grid[-1], self.origin[1]+self.r_grid[-1], self.origin[2]+self.r_grid[-1]) + r = self.r_grid[-1] + return np.array(self.origin[0] + r, self.origin[1] + r, self.origin[2] + r) @property def bounding_box(self): - return openmc.BoundingBox( - np.array(self.lower_left), np.array(self.upper_right) - ) + return openmc.BoundingBox(self.lower_left, self.upper_right) def __repr__(self): fmt = '{0: <16}{1}{2}\n' From efb3b27de4f5ae5f3cbc14e79bd2a8c2a0f4aec3 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Sat, 5 Aug 2023 16:29:26 -0400 Subject: [PATCH 9/9] Fix use of SphericalMesh in test --- openmc/mesh.py | 2 +- tests/unit_tests/test_mesh.py | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index e97b5e92d47..30ad738a20b 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1649,7 +1649,7 @@ def lower_left(self): @property def upper_right(self): r = self.r_grid[-1] - return np.array(self.origin[0] + r, self.origin[1] + r, self.origin[2] + r) + return np.array((self.origin[0] + r, self.origin[1] + r, self.origin[2] + r)) @property def bounding_box(self): diff --git a/tests/unit_tests/test_mesh.py b/tests/unit_tests/test_mesh.py index 173fb971db1..60d14a27929 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -49,25 +49,23 @@ def test_regular_mesh_bounding_box(): def test_spherical_mesh_bounding_box(): - mesh = openmc.SphericalMesh() - mesh.r_grid = np.array([0.1, 0.2, 0.5, 1.]) # test with mesh at origin (0, 0, 0) - mesh.origin = (0, 0, 0) - assert mesh.upper_right == (1, 1, 1) - assert mesh.lower_left == (-1, -1, -1) + mesh = openmc.SphericalMesh([0.1, 0.2, 0.5, 1.], origin=(0., 0., 0.)) + np.testing.assert_array_equal(mesh.upper_right, (1, 1, 1)) + np.testing.assert_array_equal(mesh.lower_left, (-1, -1, -1)) bb = mesh.bounding_box assert isinstance(bb, openmc.BoundingBox) - np.testing.assert_array_equal(bb.lower_left, np.array([-1, -1, -1])) - np.testing.assert_array_equal(bb.upper_right, np.array([1, 1, 1])) + np.testing.assert_array_equal(bb.lower_left, (-1, -1, -1)) + np.testing.assert_array_equal(bb.upper_right, (1, 1, 1)) # test with mesh at origin (3, 5, 7) mesh.origin = (3, 5, 7) - assert mesh.upper_right == (4, 6, 8) - assert mesh.lower_left == (2, 4, 6) + np.testing.assert_array_equal(mesh.upper_right, (4, 6, 8)) + np.testing.assert_array_equal(mesh.lower_left, (2, 4, 6)) bb = mesh.bounding_box assert isinstance(bb, openmc.BoundingBox) - np.testing.assert_array_equal(bb.lower_left, np.array([2, 4, 6])) - np.testing.assert_array_equal(bb.upper_right, np.array([4, 6, 8])) + np.testing.assert_array_equal(bb.lower_left, (2, 4, 6)) + np.testing.assert_array_equal(bb.upper_right, (4, 6, 8)) def test_SphericalMesh_initiation():