From b48bc40b86261ac21dc432cfd9eae6bf2ffaed1e Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Sat, 2 Dec 2023 23:10:11 +0000 Subject: [PATCH 1/2] added get_all_nuclides to geometry --- openmc/geometry.py | 17 +++++++++++++++++ tests/unit_tests/test_geometry.py | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/openmc/geometry.py b/openmc/geometry.py index fb2488f7813..6319d333bb3 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -391,6 +391,23 @@ def get_all_universes(self) -> typing.Dict[int, openmc.Universe]: universes.update(self.root_universe.get_all_universes()) return universes + def get_all_nuclides(self) -> typing.List[str]: + """Return all nuclides within the geometry. + + Returns + ------- + list + sorted list of all the nuclides in the geometry materials + + """ + all_nuclides = [] + for material in self.get_all_materials().values(): + for nuclide in material.get_nuclides(): + if nuclide not in all_nuclides: + all_nuclides.append(nuclide) + return sorted(all_nuclides) + + def get_all_materials(self) -> typing.Dict[int, openmc.Material]: """Return all materials within the geometry. diff --git a/tests/unit_tests/test_geometry.py b/tests/unit_tests/test_geometry.py index ff94bc932ad..f5a1bd7db69 100644 --- a/tests/unit_tests/test_geometry.py +++ b/tests/unit_tests/test_geometry.py @@ -378,3 +378,15 @@ def get_cyl_cell(r1, r2, z1, z2, fill): # There should be 0 remaining redundant surfaces n_redundant_surfs = len(geom.remove_redundant_surfaces().keys()) assert n_redundant_surfs == 0 + +def test_get_all_nuclides(): + m1 = openmc.Material() + m1.add_nuclide('Fe56', 1) + m1.add_nuclide('Be9', 1) + m2 = openmc.Material() + m2.add_nuclide('Be9', 1) + s = openmc.Sphere() + c1 = openmc.Cell(fill=m1, region=-s) + c2 = openmc.Cell(fill=m2, region=+s) + geom = openmc.Geometry([c1, c2]) + assert geom.get_all_nuclides() == ['Be9', 'Fe56'] From 06c5ad0e837256d761d2ce3caed77a6ffbebb3ec Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Tue, 12 Dec 2023 13:46:17 -0600 Subject: [PATCH 2/2] More efficient algorithm in Geometry.get_all_nuclides --- openmc/geometry.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/openmc/geometry.py b/openmc/geometry.py index 6319d333bb3..60f4a390d44 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -397,17 +397,14 @@ def get_all_nuclides(self) -> typing.List[str]: Returns ------- list - sorted list of all the nuclides in the geometry materials + Sorted list of all nuclides in materials appearing in the geometry """ - all_nuclides = [] + all_nuclides = set() for material in self.get_all_materials().values(): - for nuclide in material.get_nuclides(): - if nuclide not in all_nuclides: - all_nuclides.append(nuclide) + all_nuclides |= set(material.get_nuclides()) return sorted(all_nuclides) - def get_all_materials(self) -> typing.Dict[int, openmc.Material]: """Return all materials within the geometry.