diff --git a/openmc/universe.py b/openmc/universe.py index 0e60974c0d..16a251dee8 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 dbca3ca253..d2458195ac 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']