From 06fb7c3b5a8fc19957898152d75fdd09562cd0cc Mon Sep 17 00:00:00 2001 From: Jon Shimwell Date: Thu, 28 Nov 2024 18:40:06 +0100 Subject: [PATCH 1/5] added method to get meshes --- openmc/statepoint.py | 49 ++++++++++++++++++++++++ tests/unit_tests/test_statepoint.py | 58 +++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 tests/unit_tests/test_statepoint.py diff --git a/openmc/statepoint.py b/openmc/statepoint.py index 13aac0caf7e..fe30d8964bc 100644 --- a/openmc/statepoint.py +++ b/openmc/statepoint.py @@ -644,6 +644,55 @@ def get_tally(self, scores=[], filters=[], nuclides=[], return tally + def get_mesh( + self, + id: int | None = None, + name: str | None = None, + mesh_type: openmc.MeshBase | None = None, + ): + """Return a Mesh object wihich matches all of the input parameters. + Parameters + ---------- + name : str, optional + The name specified for the Tally (default is None). + id : Integral, optional + The id specified for the Tally (default is None). + mesh_type : openmc.Mesh, optional + The type of MeshBase, for example openmc.RegularMesh (default is None). + + Returns + ------- + mesh : openmc.Mesh + A mesh matching the specified criteria + + Raises + ------ + LookupError + If a mesh meeting all of the input parameters cannot be found in + the statepoint. + + """ + + mesh = None + + for test_mesh in self.meshes.values(): + if ( + (id and id != test_mesh.id) + or (name and name != test_mesh.name) + or (mesh_type and not isinstance(test_mesh, mesh_type)) + ): + continue + + # If the current mesh met user's request, break loop and return it + mesh = test_mesh + break + + # If we did not find the mesh, return an error message + if mesh is None: + raise LookupError("Unable to get Mesh") + + return mesh + def link_with_summary(self, summary): """Links Tallies and Filters with Summary model information. diff --git a/tests/unit_tests/test_statepoint.py b/tests/unit_tests/test_statepoint.py new file mode 100644 index 00000000000..e07f5d25923 --- /dev/null +++ b/tests/unit_tests/test_statepoint.py @@ -0,0 +1,58 @@ +import openmc +import pytest + + +def test_get_mesh(): + """tests getting the correct mesh from the statepoint file""" + + model = openmc.Model() + + h1 = openmc.Material() + h1.add_nuclide("H1", 1.0) + h1.set_density("g/cm3", 1.0) + model.materials = openmc.Materials([h1]) + + sphere = openmc.Sphere(r=10, boundary_type="vacuum") + cell = openmc.Cell(fill=h1, region=-sphere) + model.geometry = openmc.Geometry([cell]) + + model.settings.run_mode = "fixed source" + model.settings.particles = 10 + model.settings.batches = 2 + + model.settings.source = openmc.IndependentSource() + + mesh = openmc.RegularMesh() + mesh.id = 42 + mesh.name = "custom_name" + mesh.dimension = (2, 2, 1) + mesh.lower_left = (-10, -10, -10) + mesh.upper_right = (10, 10, 10) + + tally = openmc.Tally() + tally.scores = ["flux"] + tally.filters = [openmc.MeshFilter(mesh)] + model.tallies = openmc.Tallies([tally]) + + statepoint_fn = model.run() + + statepoint = openmc.StatePoint(statepoint_fn) + + # checks that the mesh is not found in the statepoint file + with pytest.raises(LookupError): + statepoint.get_mesh(id=999) + with pytest.raises(LookupError): + statepoint.get_mesh(mesh_type=openmc.CylindricalMesh) + with pytest.raises(LookupError): + statepoint.get_mesh(id=42, mesh_type=openmc.CylindricalMesh) + with pytest.raises(LookupError): + statepoint.get_mesh(id=999, mesh_type=openmc.RegularMesh) + + # checks that the mesh returned is the one with the id 42 + assert statepoint.get_mesh(id=42).id == 42 + assert statepoint.get_mesh(mesh_type=openmc.RegularMesh).id == 42 + assert statepoint.get_mesh(id=42, mesh_type=openmc.RegularMesh).id == 42 + + # TODO add this test when in a bug fix PR as fixed the problem that he mesh is + # returned from the statepoint has no name set + # assert statepoint.get_mesh(name='custom_name').id == 42 From 9188e4be868e98e1781deb5531c4e60de497c450 Mon Sep 17 00:00:00 2001 From: Jon Shimwell Date: Thu, 28 Nov 2024 18:43:40 +0100 Subject: [PATCH 2/5] typo fix --- openmc/statepoint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/statepoint.py b/openmc/statepoint.py index fe30d8964bc..71ae5b3dcab 100644 --- a/openmc/statepoint.py +++ b/openmc/statepoint.py @@ -650,7 +650,7 @@ def get_mesh( name: str | None = None, mesh_type: openmc.MeshBase | None = None, ): - """Return a Mesh object wihich matches all of the input parameters. + """Return a Mesh object that matches all of the input parameters. Parameters ---------- name : str, optional From e7f19f1e45bb038a59ab3d80a77199480b549539 Mon Sep 17 00:00:00 2001 From: Jon Shimwell Date: Thu, 28 Nov 2024 18:44:19 +0100 Subject: [PATCH 3/5] typo fix --- openmc/statepoint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/openmc/statepoint.py b/openmc/statepoint.py index 71ae5b3dcab..d86e4bbaf58 100644 --- a/openmc/statepoint.py +++ b/openmc/statepoint.py @@ -651,6 +651,7 @@ def get_mesh( mesh_type: openmc.MeshBase | None = None, ): """Return a Mesh object that matches all of the input parameters. + Parameters ---------- name : str, optional From 35640bf239e0c4c2a560481cd40d8ee9726af7fd Mon Sep 17 00:00:00 2001 From: Jon Shimwell Date: Thu, 28 Nov 2024 18:45:28 +0100 Subject: [PATCH 4/5] typo fix --- openmc/statepoint.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/openmc/statepoint.py b/openmc/statepoint.py index d86e4bbaf58..7d6f5c0e6f0 100644 --- a/openmc/statepoint.py +++ b/openmc/statepoint.py @@ -655,9 +655,9 @@ def get_mesh( Parameters ---------- name : str, optional - The name specified for the Tally (default is None). - id : Integral, optional - The id specified for the Tally (default is None). + The name specified for the mesh (default is None). + id : int, optional + The id specified for the mesh (default is None). mesh_type : openmc.Mesh, optional The type of MeshBase, for example openmc.RegularMesh (default is None). @@ -669,8 +669,8 @@ def get_mesh( Raises ------ LookupError - If a mesh meeting all of the input parameters cannot be found in - the statepoint. + If a mesh meeting all of the input parameters cannot be found in the + statepoint. """ From d8358b7d965f669934809725ef27272cf67cedb5 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Sat, 14 Dec 2024 12:40:11 +0100 Subject: [PATCH 5/5] [skip ci] adding check for name --- tests/unit_tests/test_statepoint.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/unit_tests/test_statepoint.py b/tests/unit_tests/test_statepoint.py index e07f5d25923..c825bf12178 100644 --- a/tests/unit_tests/test_statepoint.py +++ b/tests/unit_tests/test_statepoint.py @@ -47,12 +47,11 @@ def test_get_mesh(): statepoint.get_mesh(id=42, mesh_type=openmc.CylindricalMesh) with pytest.raises(LookupError): statepoint.get_mesh(id=999, mesh_type=openmc.RegularMesh) + with pytest.raises(LookupError): + statepoint.get_mesh(name='non_existent_name') # checks that the mesh returned is the one with the id 42 assert statepoint.get_mesh(id=42).id == 42 assert statepoint.get_mesh(mesh_type=openmc.RegularMesh).id == 42 assert statepoint.get_mesh(id=42, mesh_type=openmc.RegularMesh).id == 42 - - # TODO add this test when in a bug fix PR as fixed the problem that he mesh is - # returned from the statepoint has no name set - # assert statepoint.get_mesh(name='custom_name').id == 42 + assert statepoint.get_mesh(name='custom_name').id == 42