From cce80fe7da8b32631601afe72464f76b1ef13335 Mon Sep 17 00:00:00 2001 From: Christopher Billingham Date: Sun, 6 Aug 2023 01:31:38 +0100 Subject: [PATCH] Spherical bb (#2620) Co-authored-by: Jonathan Shimwell Co-authored-by: Paul Romano --- openmc/mesh.py | 26 ++++++++++++++++++++++++++ tests/unit_tests/test_mesh.py | 22 +++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index a878d4f7127..30ad738a20b 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -505,6 +505,9 @@ 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 @@ -1549,6 +1552,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 : 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 : 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 + Axis-aligned bounding box of the cell defined by the upper-right and lower- + left coordinates """ @@ -1629,6 +1641,20 @@ def indices(self): for t in range(1, nt + 1) for r in range(1, nr + 1)) + @property + def lower_left(self): + 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): + 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(self.lower_left, 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 affbcabd206..60d14a27929 100644 --- a/tests/unit_tests/test_mesh.py +++ b/tests/unit_tests/test_mesh.py @@ -38,7 +38,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] @@ -48,6 +48,26 @@ def test_mesh_bounding_box(): np.testing.assert_array_equal(bb.upper_right, np.array([2, 3, 5])) +def test_spherical_mesh_bounding_box(): + # test with mesh at origin (0, 0, 0) + 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, (-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) + 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, (2, 4, 6)) + np.testing.assert_array_equal(bb.upper_right, (4, 6, 8)) + + def test_SphericalMesh_initiation(): # test defaults