From fbcb15f8c5468d0ab07ddab98b02de0b04561467 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Fri, 23 Sep 2016 06:26:33 -0500 Subject: [PATCH] Simplify usage of DataLibrary.register_file() --- data/convert_mcnp_70.py | 2 +- data/get_jeff_data.py | 2 +- docs/source/pythonapi/index.rst | 1 + openmc/data/library.py | 32 +++++++++++++++++++++++++++++++- openmc/geometry.py | 8 ++++++-- openmc/material.py | 7 ++++++- openmc/plots.py | 8 ++++++-- openmc/settings.py | 10 +++++++--- scripts/openmc-ace-to-hdf5 | 2 +- 9 files changed, 60 insertions(+), 12 deletions(-) diff --git a/data/convert_mcnp_70.py b/data/convert_mcnp_70.py index 6a6e2d61c..e68bd848e 100755 --- a/data/convert_mcnp_70.py +++ b/data/convert_mcnp_70.py @@ -81,7 +81,7 @@ if os.path.exists(endf70sab): data.export_to_hdf5(h5_file, 'w') # Register with library - library.register_file(h5_file, 'thermal') + library.register_file(h5_file) # Write cross_sections.xml libpath = os.path.join(args.destination, 'cross_sections.xml') diff --git a/data/get_jeff_data.py b/data/get_jeff_data.py index 5595e0683..d0e5839b9 100755 --- a/data/get_jeff_data.py +++ b/data/get_jeff_data.py @@ -219,7 +219,7 @@ for name, filenames in sorted(tables.items()): data.export_to_hdf5(h5_file, 'w') # Register with library - library.register_file(h5_file, 'thermal') + library.register_file(h5_file) # Write cross_sections.xml libpath = os.path.join(args.destination, 'cross_sections.xml') diff --git a/docs/source/pythonapi/index.rst b/docs/source/pythonapi/index.rst index d8eb5200a..16c90bcbc 100644 --- a/docs/source/pythonapi/index.rst +++ b/docs/source/pythonapi/index.rst @@ -366,6 +366,7 @@ Core Classes openmc.data.ThermalScattering openmc.data.CoherentElastic openmc.data.FissionEnergyRelease + openmc.data.DataLibrary Angle-Energy Distributions -------------------------- diff --git a/openmc/data/library.py b/openmc/data/library.py index 49d1c78f6..0485af064 100644 --- a/openmc/data/library.py +++ b/openmc/data/library.py @@ -8,20 +8,50 @@ from openmc.clean_xml import clean_xml_indentation class DataLibrary(EqualityMixin): + """Collection of cross section data libraries. + + Attributes + ---------- + libraries : list of dict + List in which each item is a dictionary summarizing cross section data + from a single file. The dictionary has keys 'path', 'type', and + 'materials'. + + """ + def __init__(self): self.libraries = [] - def register_file(self, filename, filetype='neutron'): + def register_file(self, filename): + """Register a file with the data library. + + Parameters + ---------- + filename : str + Path to the file to be registered. + + """ h5file = h5py.File(filename, 'r') materials = [] + filetype = 'neutron' for name in h5file: + if name.startswith('c_'): + filetype = 'thermal' materials.append(name) library = {'path': filename, 'type': filetype, 'materials': materials} self.libraries.append(library) def export_to_xml(self, path='cross_sections.xml'): + """Export cross section data library to an XML file. + + Parameters + ---------- + path : str + Path to file to write. Defaults to 'cross_sections.xml'. + + """ root = ET.Element('cross_sections') # Determine common directory for library paths diff --git a/openmc/geometry.py b/openmc/geometry.py index 6d9330dc6..4d822cc8e 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -65,10 +65,14 @@ class Geometry(object): cell.add_volume_information(volume_calc) def export_to_xml(self, path='geometry.xml'): - """Create a geometry.xml file that can be used for a simulation. + """Export geometry to an XML file. + + Parameters + ---------- + path : str + Path to file to write. Defaults to 'geometry.xml'. """ - # Clear OpenMC written IDs used to optimize XML generation openmc.universe.WRITTEN_IDS = {} diff --git a/openmc/material.py b/openmc/material.py index 2861d5be6..0639db562 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -812,7 +812,12 @@ class Materials(cv.CheckedList): self._materials_file.append(xml_element) def export_to_xml(self, path='materials.xml'): - """Create a materials.xml file that can be used for a simulation. + """Export material collection to an XML file. + + Parameters + ---------- + path : str + Path to file to write. Defaults to 'materials.xml'. """ diff --git a/openmc/plots.py b/openmc/plots.py index f5c1f2b2a..6915604a3 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -617,10 +617,14 @@ class Plots(cv.CheckedList): self._plots_file.append(xml_element) def export_to_xml(self, path='plots.xml'): - """Create a plots.xml file that can be used by OpenMC. + """Export plot specifications to an XML file. + + Parameters + ---------- + path : str + Path to file to write. Defaults to 'plots.xml'. """ - # Reset xml element tree self._plots_file.clear() diff --git a/openmc/settings.py b/openmc/settings.py index 15898a2b8..f255a8bb1 100644 --- a/openmc/settings.py +++ b/openmc/settings.py @@ -15,8 +15,7 @@ if sys.version_info[0] >= 3: class Settings(object): - """Settings file used for an OpenMC simulation. Corresponds directly to the - settings.xml input file. + """Settings used for an OpenMC simulation. Attributes ---------- @@ -1119,7 +1118,12 @@ class Settings(object): elem.append(r.to_xml_element()) def export_to_xml(self, path='settings.xml'): - """Create a settings.xml file that can be used for a simulation. + """Export simulation settings to an XML file. + + Parameters + ---------- + path : str + Path to file to write. Defaults to 'settings.xml'. """ diff --git a/scripts/openmc-ace-to-hdf5 b/scripts/openmc-ace-to-hdf5 index ff8c19962..dd115d175 100755 --- a/scripts/openmc-ace-to-hdf5 +++ b/scripts/openmc-ace-to-hdf5 @@ -190,7 +190,7 @@ for filename in ace_libraries: thermal.export_to_hdf5(outfile, 'w') # Register with library - library.register_file(outfile, 'thermal') + library.register_file(outfile) # Add data to list nuclides[name] = outfile