diff --git a/openmc/universe.py b/openmc/universe.py index 0e60974c0..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 """ @@ -699,6 +704,21 @@ class DAGMCUniverse(UniverseBase): def auto_mat_ids(self): return self._auto_mat_ids + @property + def material_names(self): + 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: + 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 + # 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']