diff --git a/openmc/dagmc.py b/openmc/dagmc.py index 3cb48ddf176..fd4258225f3 100644 --- a/openmc/dagmc.py +++ b/openmc/dagmc.py @@ -223,21 +223,19 @@ def auto_mat_ids(self, val): @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:'): - # if name ends with _comp remove it, it is not parsed - if candidate_tag.endswith('_comp'): - candidate_tag = candidate_tag[:-5] - # 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:]) - + with h5py.File(self.filename) as dagmc_file_contents: + material_tags_hex = dagmc_file_contents['/tstt/tags/NAME'].get('values') + 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:'): + # if name ends with _comp remove it, it is not parsed + if candidate_tag.endswith('_comp'): + candidate_tag = candidate_tag[:-5] + # 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)) def _n_geom_elements(self, geom_type):