From 1cfbd67659292978265468db35a33110585f2b8b Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 9 Sep 2022 14:47:11 +0100 Subject: [PATCH 1/4] added bbox class method to regular mesh --- openmc/mesh.py | 44 +++++++++++++ .../unit_tests/test_mesh_from_bounding_box.py | 63 +++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 tests/unit_tests/test_mesh_from_bounding_box.py diff --git a/openmc/mesh.py b/openmc/mesh.py index 8ac4b15946..cdbd4f1be0 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -279,6 +279,7 @@ class StructuredMesh(MeshBase): return vtk_grid + class RegularMesh(StructuredMesh): """A regular Cartesian mesh in one, two, or three dimensions @@ -509,6 +510,49 @@ class RegularMesh(StructuredMesh): return mesh + @classmethod + def from_bounding_box( + cls, + bounding_box, + dimension=[100, 100, 100], + mesh_id=None, + name='' + ): + """Create mesh from an existing openmc cell, region, universe or + geometry by making use of the objects bounding box property. + + Parameters + ---------- + bounding_box : {openmc.Cell, openmc.Region, openmc.Universe, openmc.Geometry} + The object passed in will be used as a template for this mesh. The + bounding_box of the property of the object passed will be used to + set the lower_left and upper_right of the mesh instance + dimension : Iterable of int + The number of mesh cells in each direction. + mesh_id : int + Unique identifier for the mesh + name : str + Name of the mesh + + Returns + ------- + openmc.RegularMesh + RegularMesh instance + + """ + cv.check_type( + "bounding_box", + bounding_box, + (openmc.Cell, openmc.Region, openmc.Universe, openmc.Geometry), + ) + + mesh = cls(mesh_id, name) + mesh.lower_left = bounding_box.bounding_box[0] + mesh.upper_right = bounding_box.bounding_box[1] + mesh.dimension = dimension + + return mesh + def to_xml_element(self): """Return XML representation of the mesh diff --git a/tests/unit_tests/test_mesh_from_bounding_box.py b/tests/unit_tests/test_mesh_from_bounding_box.py new file mode 100644 index 0000000000..4f3d991281 --- /dev/null +++ b/tests/unit_tests/test_mesh_from_bounding_box.py @@ -0,0 +1,63 @@ +import numpy as np +import openmc +import pytest + + +def test_mesh_from_cell(): + """Tests a RegularMesh can be made from a Cell and the specified dimensions + are propagated through. Cell is not centralized""" + surface = openmc.Sphere(r=10, x0=2, y0=3, z0=5) + cell = openmc.Cell(region=-surface) + + mesh = openmc.RegularMesh.from_bounding_box(cell, dimension=[7, 11, 13]) + assert isinstance(mesh, openmc.RegularMesh) + assert np.array_equal(mesh.dimension, (7, 11, 13)) + assert np.array_equal(mesh.lower_left, cell.bounding_box[0]) + assert np.array_equal(mesh.upper_right, cell.bounding_box[1]) + + +def test_mesh_from_region(): + """Tests a RegularMesh can be made from a Region and the default dimensions + are propagated through. Region is not centralized""" + surface = openmc.Sphere(r=1, x0=-5, y0=-3, z0=-2) + region = -surface + + mesh = openmc.RegularMesh.from_bounding_box(region) + assert isinstance(mesh, openmc.RegularMesh) + assert np.array_equal(mesh.dimension, (100, 100, 100)) # default values + assert np.array_equal(mesh.lower_left, region.bounding_box[0]) + assert np.array_equal(mesh.upper_right, region.bounding_box[1]) + + +def test_mesh_from_universe(): + """Tests a RegularMesh can be made from a Universe and the default dimensions + are propagated through. Universe is centralized""" + surface = openmc.Sphere(r=42) + cell = openmc.Cell(region=-surface) + universe = openmc.Universe(cells=[cell]) + + mesh = openmc.RegularMesh.from_bounding_box(universe) + assert isinstance(mesh, openmc.RegularMesh) + assert np.array_equal(mesh.dimension, (100, 100, 100)) # default values + assert np.array_equal(mesh.lower_left, universe.bounding_box[0]) + assert np.array_equal(mesh.upper_right, universe.bounding_box[1]) + + +def test_mesh_from_geometry(): + """Tests a RegularMesh can be made from a Geometry and the default dimensions + are propagated through. Geometry is centralized""" + surface = openmc.Sphere(r=42) + cell = openmc.Cell(region=-surface) + universe = openmc.Universe(cells=[cell]) + geometry = openmc.Geometry(universe) + + mesh = openmc.RegularMesh.from_bounding_box(geometry) + assert isinstance(mesh, openmc.RegularMesh) + assert np.array_equal(mesh.dimension, (100, 100, 100)) # default values + assert np.array_equal(mesh.lower_left, geometry.bounding_box[0]) + assert np.array_equal(mesh.upper_right, geometry.bounding_box[1]) + + +def test_error_from_unsupported_object(): + with pytest.raises(TypeError): + openmc.RegularMesh.from_bounding_box("vacuum energy") From abc2c7cd7a00615f87245765193eabefb0c7d8cb Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 9 Sep 2022 16:45:49 +0100 Subject: [PATCH 2/4] renamed dounding_box to domain, review suggestion --- openmc/mesh.py | 16 ++++++++-------- ..._bounding_box.py => test_mesh_from_domain.py} | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) rename tests/unit_tests/{test_mesh_from_bounding_box.py => test_mesh_from_domain.py} (87%) diff --git a/openmc/mesh.py b/openmc/mesh.py index cdbd4f1be0..6fefb7b4c3 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -511,9 +511,9 @@ class RegularMesh(StructuredMesh): return mesh @classmethod - def from_bounding_box( + def from_domain( cls, - bounding_box, + domain, dimension=[100, 100, 100], mesh_id=None, name='' @@ -523,9 +523,9 @@ class RegularMesh(StructuredMesh): Parameters ---------- - bounding_box : {openmc.Cell, openmc.Region, openmc.Universe, openmc.Geometry} + domain : {openmc.Cell, openmc.Region, openmc.Universe, openmc.Geometry} The object passed in will be used as a template for this mesh. The - bounding_box of the property of the object passed will be used to + domain of the property of the object passed will be used to set the lower_left and upper_right of the mesh instance dimension : Iterable of int The number of mesh cells in each direction. @@ -541,14 +541,14 @@ class RegularMesh(StructuredMesh): """ cv.check_type( - "bounding_box", - bounding_box, + "domain", + domain, (openmc.Cell, openmc.Region, openmc.Universe, openmc.Geometry), ) mesh = cls(mesh_id, name) - mesh.lower_left = bounding_box.bounding_box[0] - mesh.upper_right = bounding_box.bounding_box[1] + mesh.lower_left = domain.bounding_box[0] + mesh.upper_right = domain.bounding_box[1] mesh.dimension = dimension return mesh diff --git a/tests/unit_tests/test_mesh_from_bounding_box.py b/tests/unit_tests/test_mesh_from_domain.py similarity index 87% rename from tests/unit_tests/test_mesh_from_bounding_box.py rename to tests/unit_tests/test_mesh_from_domain.py index 4f3d991281..2140d56257 100644 --- a/tests/unit_tests/test_mesh_from_bounding_box.py +++ b/tests/unit_tests/test_mesh_from_domain.py @@ -9,7 +9,7 @@ def test_mesh_from_cell(): surface = openmc.Sphere(r=10, x0=2, y0=3, z0=5) cell = openmc.Cell(region=-surface) - mesh = openmc.RegularMesh.from_bounding_box(cell, dimension=[7, 11, 13]) + mesh = openmc.RegularMesh.from_domain(cell, dimension=[7, 11, 13]) assert isinstance(mesh, openmc.RegularMesh) assert np.array_equal(mesh.dimension, (7, 11, 13)) assert np.array_equal(mesh.lower_left, cell.bounding_box[0]) @@ -22,7 +22,7 @@ def test_mesh_from_region(): surface = openmc.Sphere(r=1, x0=-5, y0=-3, z0=-2) region = -surface - mesh = openmc.RegularMesh.from_bounding_box(region) + mesh = openmc.RegularMesh.from_domain(region) assert isinstance(mesh, openmc.RegularMesh) assert np.array_equal(mesh.dimension, (100, 100, 100)) # default values assert np.array_equal(mesh.lower_left, region.bounding_box[0]) @@ -36,7 +36,7 @@ def test_mesh_from_universe(): cell = openmc.Cell(region=-surface) universe = openmc.Universe(cells=[cell]) - mesh = openmc.RegularMesh.from_bounding_box(universe) + mesh = openmc.RegularMesh.from_domain(universe) assert isinstance(mesh, openmc.RegularMesh) assert np.array_equal(mesh.dimension, (100, 100, 100)) # default values assert np.array_equal(mesh.lower_left, universe.bounding_box[0]) @@ -51,7 +51,7 @@ def test_mesh_from_geometry(): universe = openmc.Universe(cells=[cell]) geometry = openmc.Geometry(universe) - mesh = openmc.RegularMesh.from_bounding_box(geometry) + mesh = openmc.RegularMesh.from_domain(geometry) assert isinstance(mesh, openmc.RegularMesh) assert np.array_equal(mesh.dimension, (100, 100, 100)) # default values assert np.array_equal(mesh.lower_left, geometry.bounding_box[0]) @@ -60,4 +60,4 @@ def test_mesh_from_geometry(): def test_error_from_unsupported_object(): with pytest.raises(TypeError): - openmc.RegularMesh.from_bounding_box("vacuum energy") + openmc.RegularMesh.from_domain("vacuum energy") From 88c2ff9198b1ba2671ad614278170f433dd7480f Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 9 Sep 2022 16:48:05 +0100 Subject: [PATCH 3/4] [skip ci] one too many domains --- openmc/mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 6fefb7b4c3..a4c50a6084 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -525,7 +525,7 @@ class RegularMesh(StructuredMesh): ---------- domain : {openmc.Cell, openmc.Region, openmc.Universe, openmc.Geometry} The object passed in will be used as a template for this mesh. The - domain of the property of the object passed will be used to + bounding box of the property of the object passed will be used to set the lower_left and upper_right of the mesh instance dimension : Iterable of int The number of mesh cells in each direction. From 21f1d5142bfc9a9ebbb452a347af7b289779d932 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 9 Sep 2022 17:08:40 +0100 Subject: [PATCH 4/4] renamed tests to reg_mesh prefix --- tests/unit_tests/test_mesh_from_domain.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit_tests/test_mesh_from_domain.py b/tests/unit_tests/test_mesh_from_domain.py index 2140d56257..ce27288adf 100644 --- a/tests/unit_tests/test_mesh_from_domain.py +++ b/tests/unit_tests/test_mesh_from_domain.py @@ -3,7 +3,7 @@ import openmc import pytest -def test_mesh_from_cell(): +def test_reg_mesh_from_cell(): """Tests a RegularMesh can be made from a Cell and the specified dimensions are propagated through. Cell is not centralized""" surface = openmc.Sphere(r=10, x0=2, y0=3, z0=5) @@ -16,7 +16,7 @@ def test_mesh_from_cell(): assert np.array_equal(mesh.upper_right, cell.bounding_box[1]) -def test_mesh_from_region(): +def test_reg_mesh_from_region(): """Tests a RegularMesh can be made from a Region and the default dimensions are propagated through. Region is not centralized""" surface = openmc.Sphere(r=1, x0=-5, y0=-3, z0=-2) @@ -29,7 +29,7 @@ def test_mesh_from_region(): assert np.array_equal(mesh.upper_right, region.bounding_box[1]) -def test_mesh_from_universe(): +def test_reg_mesh_from_universe(): """Tests a RegularMesh can be made from a Universe and the default dimensions are propagated through. Universe is centralized""" surface = openmc.Sphere(r=42) @@ -43,7 +43,7 @@ def test_mesh_from_universe(): assert np.array_equal(mesh.upper_right, universe.bounding_box[1]) -def test_mesh_from_geometry(): +def test_reg_mesh_from_geometry(): """Tests a RegularMesh can be made from a Geometry and the default dimensions are propagated through. Geometry is centralized""" surface = openmc.Sphere(r=42)