From e63b4376628e978610c08081721974cd0b4ae90a Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Mon, 26 Sep 2022 16:11:53 +0100 Subject: [PATCH 1/6] added from domain to CylindricalMesh --- openmc/mesh.py | 60 +++++++++++++++++++++++ tests/unit_tests/test_mesh_from_domain.py | 40 +++++++++++++-- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index a4c50a608..a64a2f8dd 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1137,6 +1137,66 @@ class CylindricalMesh(StructuredMesh): return mesh + @classmethod + def from_domain( + cls, + domain, + 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. phi_grid + is set to have a range of 0 to 2π by default. + + Parameters + ---------- + 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 + set the The r_grid, z_grid ranges. + dimension : Iterable of int + The number of equally spaced mesh cells in each direction (r_grid, + phi_grid, z_grid) + mesh_id : int + Unique identifier for the mesh + name : str + Name of the mesh + + Returns + ------- + openmc.RegularMesh + RegularMesh instance + + """ + cv.check_type( + "domain", + domain, + (openmc.Cell, openmc.Region, openmc.Universe, openmc.Geometry), + ) + + mesh = cls(mesh_id, name) + + # loaded once to avoid reading h5m file repeatedly + cached_bb = domain.bounding_box + max_bounding_box_radius = max( + [ + cached_bb[0][0], + cached_bb[0][1], + cached_bb[1][0], + cached_bb[1][1], + ] + ) + mesh.r_grid = np.linspace(0, max_bounding_box_radius, num=dimension[0]+1) + mesh.phi_grid = np.linspace(0, 2*np.pi, num=dimension[1]+1) + mesh.z_grid = np.linspace( + cached_bb[0][2], + cached_bb[1][2], + num=dimension[2]+1 + ) + + return mesh + def to_xml_element(self): """Return XML representation of the mesh diff --git a/tests/unit_tests/test_mesh_from_domain.py b/tests/unit_tests/test_mesh_from_domain.py index ce27288ad..64b9424b9 100644 --- a/tests/unit_tests/test_mesh_from_domain.py +++ b/tests/unit_tests/test_mesh_from_domain.py @@ -16,6 +16,22 @@ def test_reg_mesh_from_cell(): assert np.array_equal(mesh.upper_right, cell.bounding_box[1]) +def test_cylindrical_mesh_from_cell(): + """Tests a CylindricalMesh can be made from a Cell and the specified + dimensions are propagated through. Cell is not centralized""" + cy_surface = openmc. openmc.ZCylinder(r=50) + z_surface_1 = openmc. openmc.ZPlane(z0=30) + z_surface_2 = openmc. openmc.ZPlane(z0=0) + cell = openmc.Cell(region=-cy_surface & -z_surface_1 & +z_surface_2) + mesh = openmc.CylindricalMesh.from_domain(cell, dimension=[2, 4, 3]) + + assert isinstance(mesh, openmc.CylindricalMesh) + assert np.array_equal(mesh.dimension, (2, 4, 3)) + assert np.array_equal(mesh.r_grid, [0., 25., 50.]) + assert np.array_equal(mesh.phi_grid, [0., 0.5*np.pi, np.pi, 1.5*np.pi, 2.*np.pi]) + assert np.array_equal(mesh.z_grid, [0., 10., 20., 30.]) + + 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""" @@ -29,9 +45,25 @@ def test_reg_mesh_from_region(): assert np.array_equal(mesh.upper_right, region.bounding_box[1]) +def test_cylindrical_mesh_from_region(): + """Tests a CylindricalMesh can be made from a Region and the specified + dimensions are propagated through. Cell is centralized""" + cy_surface = openmc. openmc.ZCylinder(r=6) + z_surface_1 = openmc. openmc.ZPlane(z0=30) + z_surface_2 = openmc. openmc.ZPlane(z0=-30) + cell = openmc.Cell(region=-cy_surface & -z_surface_1 & +z_surface_2) + mesh = openmc.CylindricalMesh.from_domain(cell, dimension=[6, 2, 3]) + + assert isinstance(mesh, openmc.CylindricalMesh) + assert np.array_equal(mesh.dimension, (6, 2, 3)) + assert np.array_equal(mesh.r_grid, [0., 1., 2., 3., 4., 5., 6.]) + assert np.array_equal(mesh.phi_grid, [0., np.pi, 2.*np.pi]) + assert np.array_equal(mesh.z_grid, [-30., -10., 10., 30.]) + + 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""" + """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]) @@ -44,8 +76,8 @@ def test_reg_mesh_from_universe(): 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""" + """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]) From 4e5d9bf5963fb8a857057fdacb16af7a5e0ecce1 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Mon, 26 Sep 2022 16:17:48 +0100 Subject: [PATCH 2/6] fixed typo --- openmc/mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index a64a2f8dd..0dc390ebe 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1154,7 +1154,7 @@ class CylindricalMesh(StructuredMesh): 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 - set the The r_grid, z_grid ranges. + set the r_grid, z_grid ranges. dimension : Iterable of int The number of equally spaced mesh cells in each direction (r_grid, phi_grid, z_grid) From a32a5fbf0d18bf31eaa79b2d11e2bd11f2e4c4bf Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Mon, 3 Oct 2022 16:58:11 +0100 Subject: [PATCH 3/6] review suggestion from @RemDelaporteMathurin --- openmc/mesh.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 0dc390ebe..e0d8eb61b 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1143,6 +1143,7 @@ class CylindricalMesh(StructuredMesh): domain, dimension=[100, 100, 100], mesh_id=None, + phi_grid=[0.0, 2*pi], name='' ): """Create mesh from an existing openmc cell, region, universe or @@ -1160,6 +1161,9 @@ class CylindricalMesh(StructuredMesh): phi_grid, z_grid) mesh_id : int Unique identifier for the mesh + phi_grid : numpy.ndarray + 1-D array of mesh boundary points along the phi-axis in radians. + The default value is [0, 2π], i.e. the full phi range. name : str Name of the mesh @@ -1188,7 +1192,7 @@ class CylindricalMesh(StructuredMesh): ] ) mesh.r_grid = np.linspace(0, max_bounding_box_radius, num=dimension[0]+1) - mesh.phi_grid = np.linspace(0, 2*np.pi, num=dimension[1]+1) + mesh.phi_grid = np.linspace(phi_grid[0], phi_grid[1], num=dimension[1]+1) mesh.z_grid = np.linspace( cached_bb[0][2], cached_bb[1][2], From afbd994d4ff0504bd35722873152813efe907321 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 5 Oct 2022 08:45:59 +0100 Subject: [PATCH 4/6] review suggestions from Remi and Paul MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Paul Romano Co-authored-by: Rémi Delaporte-Mathurin <40028739+RemDelaporteMathurin@users.noreply.github.com> --- openmc/mesh.py | 8 ++++---- tests/unit_tests/test_mesh_from_domain.py | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index e0d8eb61b..57fd8fb21 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1141,9 +1141,9 @@ class CylindricalMesh(StructuredMesh): def from_domain( cls, domain, - dimension=[100, 100, 100], + dimension=(100, 100, 100), mesh_id=None, - phi_grid=[0.0, 2*pi], + phi_grid=(0.0, 2*pi), name='' ): """Create mesh from an existing openmc cell, region, universe or @@ -1152,7 +1152,7 @@ class CylindricalMesh(StructuredMesh): Parameters ---------- - domain : {openmc.Cell, openmc.Region, openmc.Universe, openmc.Geometry} + domain : openmc.Cell or openmc.Region or openmc.Universe or 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 r_grid, z_grid ranges. @@ -1163,7 +1163,7 @@ class CylindricalMesh(StructuredMesh): Unique identifier for the mesh phi_grid : numpy.ndarray 1-D array of mesh boundary points along the phi-axis in radians. - The default value is [0, 2π], i.e. the full phi range. + The default value is (0, 2π), i.e., the full phi range. name : str Name of the mesh diff --git a/tests/unit_tests/test_mesh_from_domain.py b/tests/unit_tests/test_mesh_from_domain.py index 64b9424b9..cbc32b8fa 100644 --- a/tests/unit_tests/test_mesh_from_domain.py +++ b/tests/unit_tests/test_mesh_from_domain.py @@ -19,9 +19,9 @@ def test_reg_mesh_from_cell(): def test_cylindrical_mesh_from_cell(): """Tests a CylindricalMesh can be made from a Cell and the specified dimensions are propagated through. Cell is not centralized""" - cy_surface = openmc. openmc.ZCylinder(r=50) - z_surface_1 = openmc. openmc.ZPlane(z0=30) - z_surface_2 = openmc. openmc.ZPlane(z0=0) + cy_surface = openmc.ZCylinder(r=50) + z_surface_1 = openmc.ZPlane(z0=30) + z_surface_2 = openmc.ZPlane(z0=0) cell = openmc.Cell(region=-cy_surface & -z_surface_1 & +z_surface_2) mesh = openmc.CylindricalMesh.from_domain(cell, dimension=[2, 4, 3]) @@ -48,9 +48,9 @@ def test_reg_mesh_from_region(): def test_cylindrical_mesh_from_region(): """Tests a CylindricalMesh can be made from a Region and the specified dimensions are propagated through. Cell is centralized""" - cy_surface = openmc. openmc.ZCylinder(r=6) - z_surface_1 = openmc. openmc.ZPlane(z0=30) - z_surface_2 = openmc. openmc.ZPlane(z0=-30) + cy_surface = openmc.ZCylinder(r=6) + z_surface_1 = openmc.ZPlane(z0=30) + z_surface_2 = openmc.ZPlane(z0=-30) cell = openmc.Cell(region=-cy_surface & -z_surface_1 & +z_surface_2) mesh = openmc.CylindricalMesh.from_domain(cell, dimension=[6, 2, 3]) From 9a61aed2efe4bb798f16b9018ad3a8edb003d68e Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 5 Oct 2022 09:03:30 +0100 Subject: [PATCH 5/6] doc string related review suggestions --- openmc/mesh.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 57fd8fb21..d4db5011b 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -514,7 +514,7 @@ class RegularMesh(StructuredMesh): def from_domain( cls, domain, - dimension=[100, 100, 100], + dimension=(10, 10, 10), mesh_id=None, name='' ): @@ -1141,14 +1141,12 @@ class CylindricalMesh(StructuredMesh): def from_domain( cls, domain, - dimension=(100, 100, 100), + dimension=(10, 10, 10), mesh_id=None, - phi_grid=(0.0, 2*pi), + phi_grid_bounds=(0.0, 2*pi), name='' ): - """Create mesh from an existing openmc cell, region, universe or - geometry by making use of the objects bounding box property. phi_grid - is set to have a range of 0 to 2π by default. + """Creates a regular CylindricalMesh from an existing openmc domain. Parameters ---------- @@ -1161,9 +1159,9 @@ class CylindricalMesh(StructuredMesh): phi_grid, z_grid) mesh_id : int Unique identifier for the mesh - phi_grid : numpy.ndarray - 1-D array of mesh boundary points along the phi-axis in radians. - The default value is (0, 2π), i.e., the full phi range. + phi_grid_bounds : numpy.ndarray + Mesh bounds points along the phi-axis in radians. The default value + is (0, 2π), i.e., the full phi range. name : str Name of the mesh @@ -1191,8 +1189,16 @@ class CylindricalMesh(StructuredMesh): cached_bb[1][1], ] ) - mesh.r_grid = np.linspace(0, max_bounding_box_radius, num=dimension[0]+1) - mesh.phi_grid = np.linspace(phi_grid[0], phi_grid[1], num=dimension[1]+1) + mesh.r_grid = np.linspace( + 0, + max_bounding_box_radius, + num=dimension[0]+1 + ) + mesh.phi_grid = np.linspace( + phi_grid_bounds[0], + phi_grid_bounds[1], + num=dimension[1]+1 + ) mesh.z_grid = np.linspace( cached_bb[0][2], cached_bb[1][2], From 6a9a1ad2937b40c7a23fc16086518b2f99bac297 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 5 Oct 2022 11:46:00 +0100 Subject: [PATCH 6/6] added phi_grid_bounds to CylindricalMesh tests --- tests/unit_tests/test_mesh_from_domain.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/unit_tests/test_mesh_from_domain.py b/tests/unit_tests/test_mesh_from_domain.py index cbc32b8fa..b4edae196 100644 --- a/tests/unit_tests/test_mesh_from_domain.py +++ b/tests/unit_tests/test_mesh_from_domain.py @@ -40,24 +40,28 @@ def test_reg_mesh_from_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.dimension, (10, 10, 10)) # 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_cylindrical_mesh_from_region(): """Tests a CylindricalMesh can be made from a Region and the specified - dimensions are propagated through. Cell is centralized""" + dimensions and phi_grid_bounds are propagated through. Cell is centralized""" cy_surface = openmc.ZCylinder(r=6) z_surface_1 = openmc.ZPlane(z0=30) z_surface_2 = openmc.ZPlane(z0=-30) cell = openmc.Cell(region=-cy_surface & -z_surface_1 & +z_surface_2) - mesh = openmc.CylindricalMesh.from_domain(cell, dimension=[6, 2, 3]) + mesh = openmc.CylindricalMesh.from_domain( + cell, + dimension=(6, 2, 3), + phi_grid_bounds=(0., np.pi) + ) assert isinstance(mesh, openmc.CylindricalMesh) assert np.array_equal(mesh.dimension, (6, 2, 3)) assert np.array_equal(mesh.r_grid, [0., 1., 2., 3., 4., 5., 6.]) - assert np.array_equal(mesh.phi_grid, [0., np.pi, 2.*np.pi]) + assert np.array_equal(mesh.phi_grid, [0., 0.5*np.pi, np.pi]) assert np.array_equal(mesh.z_grid, [-30., -10., 10., 30.]) @@ -70,7 +74,7 @@ def test_reg_mesh_from_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.dimension, (10, 10, 10)) # default values assert np.array_equal(mesh.lower_left, universe.bounding_box[0]) assert np.array_equal(mesh.upper_right, universe.bounding_box[1]) @@ -85,7 +89,7 @@ def test_reg_mesh_from_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.dimension, (10, 10, 10)) # default values assert np.array_equal(mesh.lower_left, geometry.bounding_box[0]) assert np.array_equal(mesh.upper_right, geometry.bounding_box[1])