From 3891cef4f14bbe88292f6df61fef749423c7dba3 Mon Sep 17 00:00:00 2001 From: shimwell Date: Sat, 24 Sep 2022 20:09:01 +0100 Subject: [PATCH 1/3] added ability to find material names in DAG --- openmc/universe.py | 30 +++++++++++++++++++++++++++ tests/unit_tests/dagmc/test_bounds.py | 9 ++++++++ 2 files changed, 39 insertions(+) diff --git a/openmc/universe.py b/openmc/universe.py index 0e60974c0..16a251dee 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -699,6 +699,36 @@ class DAGMCUniverse(UniverseBase): def auto_mat_ids(self): return self._auto_mat_ids + @property + def material_names(self): + """Return the names of the materials that are contained within the + DAGMC h5m file. This is useful when naming openmc.Material() objects + as each material name present in the DAGMC h5m file must have a + matching openmc.Material() with the same name. + + Returns + ------- + materials : List[str] + Sorted list of material names present in the DAGMC h5m file + + """ + + dagmc_file_contents = h5py.File(self.filename) + material_tags_hex=dagmc_file_contents['/tstt/tags/NAME'].get('values') + material_tags_ascii=[] + for tag in material_tags_hex: + raw_tag= np.array2string(tag) + tag_in_hex=raw_tag.replace("\\x00", '').replace("\\x", '') + tag_in_hex =tag_in_hex.lstrip("b'").rstrip("'") + candidate_tag = bytes.fromhex(tag_in_hex).decode() + # tags might be for temperature or reflective surfaces + if candidate_tag.startswith('mat:'): + # removes first 4 characters as openmc.Material name should be + # set without the 'mat:' part of the tag + material_tags_ascii.append(candidate_tag[4:]) + + return sorted(set(material_tags_ascii)) + @auto_mat_ids.setter def auto_mat_ids(self, val): cv.check_type('DAGMC automatic material ids', val, bool) diff --git a/tests/unit_tests/dagmc/test_bounds.py b/tests/unit_tests/dagmc/test_bounds.py index dbca3ca25..d2458195a 100644 --- a/tests/unit_tests/dagmc/test_bounds.py +++ b/tests/unit_tests/dagmc/test_bounds.py @@ -71,3 +71,12 @@ def test_bounded_universe(request): surfaces = list(cells[0][1].region.get_surfaces().items()) assert surfaces[0][1].type == "sphere" assert surfaces[0][1].id == 43 + + +def test_material_names(request): + """Checks that the DAGMCUniverse.material_names() returns a list of the + name present in the dagmc.h5m file in the expected order""" + + u = openmc.DAGMCUniverse(Path(request.fspath).parent / "dagmc.h5m") + + assert u.material_names == ['41', 'Graveyard', 'no-void fuel'] From 27f2cb1b5ea4f5ada5a77de3e3d9312f4bfb421d Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Mon, 26 Sep 2022 08:45:44 +0100 Subject: [PATCH 2/3] @pshriwise review suggestions Co-authored-by: Patrick Shriwise --- openmc/universe.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/openmc/universe.py b/openmc/universe.py index 16a251dee..34618a70a 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -708,7 +708,7 @@ class DAGMCUniverse(UniverseBase): Returns ------- - materials : List[str] + materials : list of str Sorted list of material names present in the DAGMC h5m file """ @@ -717,10 +717,7 @@ class DAGMCUniverse(UniverseBase): material_tags_hex=dagmc_file_contents['/tstt/tags/NAME'].get('values') material_tags_ascii=[] for tag in material_tags_hex: - raw_tag= np.array2string(tag) - tag_in_hex=raw_tag.replace("\\x00", '').replace("\\x", '') - tag_in_hex =tag_in_hex.lstrip("b'").rstrip("'") - candidate_tag = bytes.fromhex(tag_in_hex).decode() + candidate_tag = tag.tobytes().decode().replace('\x00', '') # tags might be for temperature or reflective surfaces if candidate_tag.startswith('mat:'): # removes first 4 characters as openmc.Material name should be From e31576fc097f2d8c7b08790794eae6709a7e6e0f Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Tue, 27 Sep 2022 13:46:33 +0100 Subject: [PATCH 3/3] moved doc string to class attributes --- openmc/universe.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/openmc/universe.py b/openmc/universe.py index 34618a70a..2ca4ad388 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -647,6 +647,11 @@ class DAGMCUniverse(UniverseBase): bounding_box : 2-tuple of numpy.array Lower-left and upper-right coordinates of an axis-aligned bounding box of the universe. + material_name : list of str + Return a sorted list of materials names that are contained within the + DAGMC h5m file. This is useful when naming openmc.Material() objects + as each material name present in the DAGMC h5m file must have a + matching openmc.Material() with the same name. .. versionadded:: 0.13.1 """ @@ -701,18 +706,6 @@ class DAGMCUniverse(UniverseBase): @property def material_names(self): - """Return the names of the materials that are contained within the - DAGMC h5m file. This is useful when naming openmc.Material() objects - as each material name present in the DAGMC h5m file must have a - matching openmc.Material() with the same name. - - Returns - ------- - materials : list of str - Sorted list of material names present in the DAGMC h5m file - - """ - dagmc_file_contents = h5py.File(self.filename) material_tags_hex=dagmc_file_contents['/tstt/tags/NAME'].get('values') material_tags_ascii=[]