From ac4056e96d267976d48b3bae2b1a6e23e7367a2a Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 11 Jun 2019 12:23:38 -0400 Subject: [PATCH] Support depletion_chain in DataLibrary to/from xml and register DataLibrary.from_xml looks for depletion_chain node in the cross sections xml file. If found the path to this file is added to the list of libraries. An empty list of materials is used for consistency with other libraries. Type and path attributes are consistent. DataLibrary.export_to_xml does not write the materials attribute if the value stored in that library evaluates to False, e.g. for the empty list stored in the depletion chain. DataLibrary.register_file has a conditional wherein a passed xml file is treated as the depletion_chain and hdf5 files are treated as material/isotopic cross section files. --- openmc/data/library.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/openmc/data/library.py b/openmc/data/library.py index e090b2062f..d1a0d778d9 100644 --- a/openmc/data/library.py +++ b/openmc/data/library.py @@ -51,6 +51,8 @@ class DataLibrary(EqualityMixin): ---------- filename : str or Path Path to the file to be registered. + If an ``xml`` file, treat as the depletion chain file without + materials. """ if not isinstance(filename, pathlib.Path): @@ -58,9 +60,17 @@ class DataLibrary(EqualityMixin): else: path = filename - with h5py.File(path, 'r') as h5file: - filetype = h5file.attrs['filetype'].decode()[5:] - materials = list(h5file) + if path.suffix == '.xml': + filetype = 'depletion_chain' + materials = [] + elif path.suffix == '.h5': + with h5py.File(path, 'r') as h5file: + filetype = h5file.attrs['filetype'].decode()[5:] + materials = list(h5file) + else: + raise ValueError( + "File type {} not supported by {}" + .format(path.name, self.__class__.__name__)) library = {'path': path.name, 'type': filetype, 'materials': materials} self.libraries.append(library) @@ -88,7 +98,8 @@ class DataLibrary(EqualityMixin): for library in self.libraries: lib_element = ET.SubElement(root, "library") - lib_element.set('materials', ' '.join(library['materials'])) + if library['materials']: + lib_element.set('materials', ' '.join(library['materials'])) lib_element.set('path', os.path.relpath(library['path'], common_dir)) lib_element.set('type', library['type']) @@ -108,7 +119,7 @@ class DataLibrary(EqualityMixin): ---------- path : str, optional Path to XML file to read. If not provided, the - `OPENMC_CROSS_SECTIONS` environment variable will be used. + :envvar:`OPENMC_CROSS_SECTIONS` environment variable will be used. Returns ------- @@ -148,4 +159,13 @@ class DataLibrary(EqualityMixin): 'materials': materials} data.libraries.append(library) + # get depletion chain data + + dep_node = root.find("depletion_chain") + if dep_node is not None: + filename = os.path.join(directory, dep_node.attrib['path']) + library = {'path': filename, 'type': 'depletion_chain', + 'materials': []} + data.libraries.append(library) + return data