Skip to content

Commit

Permalink
Merge pull request #945 from smharper/h5py_compatibility
Browse files Browse the repository at this point in the history
Use 'earliest' libver with h5py for backwards-compatibility
  • Loading branch information
paulromano committed Dec 18, 2017
2 parents 598b5d4 + c76e77f commit 2c9b210
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 28 deletions.
2 changes: 1 addition & 1 deletion openmc/data/fission_energy.py
Expand Up @@ -107,7 +107,7 @@ def write_compact_458_library(endf_files, output_name='fission_Q_data.h5',
"""
# Open the output file.
out = h5py.File(output_name, 'w', libver='latest')
out = h5py.File(output_name, 'w', libver='earliest')

# Write comments, if given. This commented out comment is the one used for
# the library distributed with OpenMC.
Expand Down
7 changes: 5 additions & 2 deletions openmc/data/neutron.py
Expand Up @@ -478,7 +478,7 @@ def get_reaction_components(self, mt):
mts = new_mts
return mts

def export_to_hdf5(self, path, mode='a'):
def export_to_hdf5(self, path, mode='a', libver='earliest'):
"""Export incident neutron data to an HDF5 file.
Parameters
Expand All @@ -488,6 +488,9 @@ def export_to_hdf5(self, path, mode='a'):
mode : {'r', r+', 'w', 'x', 'a'}
Mode that is used to open the HDF5 file. This is the second argument
to the :class:`h5py.File` constructor.
libver : {'earliest', 'latest'}
Compatibility mode for the HDF5 file. 'latest' will produce files
that are less backwards compatible but have performance benefits.
"""
# If data come from ENDF, don't allow exporting to HDF5
Expand All @@ -496,7 +499,7 @@ def export_to_hdf5(self, path, mode='a'):
'originated from an ENDF file.')

# Open file and write version
f = h5py.File(path, mode, libver='latest')
f = h5py.File(path, mode, libver=libver)
f.attrs['version'] = np.array(HDF5_VERSION)

# Write basic data
Expand Down
7 changes: 5 additions & 2 deletions openmc/data/thermal.py
Expand Up @@ -245,7 +245,7 @@ def __repr__(self):
def temperatures(self):
return ["{}K".format(int(round(kT / K_BOLTZMANN))) for kT in self.kTs]

def export_to_hdf5(self, path, mode='a'):
def export_to_hdf5(self, path, mode='a', libver='earliest'):
"""Export table to an HDF5 file.
Parameters
Expand All @@ -255,10 +255,13 @@ def export_to_hdf5(self, path, mode='a'):
mode : {'r', r+', 'w', 'x', 'a'}
Mode that is used to open the HDF5 file. This is the second argument
to the :class:`h5py.File` constructor.
libver : {'earliest', 'latest'}
Compatibility mode for the HDF5 file. 'latest' will produce files
that are less backwards compatible but have performance benefits.
"""
# Open file and write version
f = h5py.File(path, mode, libver='latest')
f = h5py.File(path, mode, libver=libver)
f.attrs['version'] = np.array(HDF5_VERSION)

# Write basic data
Expand Down
7 changes: 5 additions & 2 deletions openmc/mgxs/library.py
Expand Up @@ -760,7 +760,7 @@ def get_subdomain_avg_library(self):

def build_hdf5_store(self, filename='mgxs.h5', directory='mgxs',
subdomains='all', nuclides='all', xs_type='macro',
row_column='inout'):
row_column='inout', libver='earliest'):
"""Export the multi-group cross section library to an HDF5 binary file.
This method constructs an HDF5 file which stores the library's
Expand Down Expand Up @@ -794,6 +794,9 @@ def build_hdf5_store(self, filename='mgxs.h5', directory='mgxs',
Store scattering matrices indexed first by incoming group and
second by outgoing group ('inout'), or vice versa ('outin').
Defaults to 'inout'.
libver : {'earliest', 'latest'}
Compatibility mode for the HDF5 file. 'latest' will produce files
that are less backwards compatible but have performance benefits.
Raises
------
Expand Down Expand Up @@ -823,7 +826,7 @@ def build_hdf5_store(self, filename='mgxs.h5', directory='mgxs',
# Add an attribute for the number of energy groups to the HDF5 file
full_filename = os.path.join(directory, filename)
full_filename = full_filename.replace(' ', '-')
f = h5py.File(full_filename, 'w')
f = h5py.File(full_filename, 'w', libver=libver)
f.attrs['# groups'] = self.num_groups
f.close()

Expand Down
8 changes: 6 additions & 2 deletions openmc/mgxs/mgxs.py
Expand Up @@ -1636,7 +1636,8 @@ def print_xs(self, subdomains='all', nuclides='all', xs_type='macro'):

def build_hdf5_store(self, filename='mgxs.h5', directory='mgxs',
subdomains='all', nuclides='all',
xs_type='macro', row_column='inout', append=True):
xs_type='macro', row_column='inout', append=True,
libver='earliest'):
"""Export the multi-group cross section data to an HDF5 binary file.
This method constructs an HDF5 file which stores the multi-group
Expand Down Expand Up @@ -1672,6 +1673,9 @@ def build_hdf5_store(self, filename='mgxs.h5', directory='mgxs',
append : bool
If true, appends to an existing HDF5 file with the same filename
directory (if one exists). Defaults to True.
libver : {'earliest', 'latest'}
Compatibility mode for the HDF5 file. 'latest' will produce files
that are less backwards compatible but have performance benefits.
Raises
------
Expand All @@ -1695,7 +1699,7 @@ def build_hdf5_store(self, filename='mgxs.h5', directory='mgxs',
if append and os.path.isfile(filename):
xs_results = h5py.File(filename, 'a')
else:
xs_results = h5py.File(filename, 'w')
xs_results = h5py.File(filename, 'w', libver=libver)

# Construct a collection of the subdomains to report
if not isinstance(subdomains, string_types):
Expand Down
7 changes: 5 additions & 2 deletions openmc/mgxs_library.py
Expand Up @@ -2504,20 +2504,23 @@ def convert_scatter_format(self, target_format, target_order):

return library

def export_to_hdf5(self, filename='mgxs.h5'):
def export_to_hdf5(self, filename='mgxs.h5', libver='earliest'):
"""Create an hdf5 file that can be used for a simulation.
Parameters
----------
filename : str
Filename of file, default is mgxs.h5.
libver : {'earliest', 'latest'}
Compatibility mode for the HDF5 file. 'latest' will produce files
that are less backwards compatible but have performance benefits.
"""

check_type('filename', filename, string_types)

# Create and write to the HDF5 file
file = h5py.File(filename, "w")
file = h5py.File(filename, "w", libver=libver)
file.attrs['filetype'] = np.string_(_FILETYPE_MGXS_LIBRARY)
file.attrs['version'] = [_VERSION_MGXS_LIBRARY, 0]
file.attrs['energy_groups'] = self.energy_groups.num_groups
Expand Down
28 changes: 18 additions & 10 deletions scripts/openmc-ace-to-hdf5
Expand Up @@ -46,7 +46,8 @@ parser.add_argument('libraries', nargs='*',
help='ACE libraries to convert to HDF5')
parser.add_argument('-d', '--destination', default='.',
help='Directory to create new library in')
parser.add_argument('-m', '--metastable', choices=['mcnp', 'nndc'], default='nndc',
parser.add_argument('-m', '--metastable', choices=['mcnp', 'nndc'],
default='nndc',
help='How to interpret ZAIDs for metastable nuclides')
parser.add_argument('--xml', help='Old-style cross_sections.xml that '
'lists ACE libraries')
Expand All @@ -56,6 +57,10 @@ parser.add_argument('--xsdata', help='Serpent xsdata file that lists '
'ACE libraries')
parser.add_argument('--fission_energy_release', help='HDF5 file containing '
'fission energy release data')
parser.add_argument('--libver', choices=['earliest', 'latest'],
default='earliest', help="Output HDF5 versioning. Use "
"'earliest' for backwards compatibility or 'latest' for "
"performance")
args = parser.parse_args()

if not os.path.isdir(args.destination):
Expand Down Expand Up @@ -150,7 +155,7 @@ for filename in ace_libraries:
# Determine filename
outfile = os.path.join(args.destination,
neutron.name.replace('.', '_') + '.h5')
neutron.export_to_hdf5(outfile, 'w')
neutron.export_to_hdf5(outfile, 'w', libver=args.libver)

# Register with library
library.register_file(outfile)
Expand All @@ -162,10 +167,11 @@ for filename in ace_libraries:
try:
neutron = \
openmc.data.IncidentNeutron.from_hdf5(nuclides[name])
print('Converting {} (ACE) to {} (HDF5)'.format(table.name,
neutron.name))
print('Converting {} (ACE) to {} (HDF5)'
.format(table.name, neutron.name))
neutron.add_temperature_from_ace(table, args.metastable)
neutron.export_to_hdf5(nuclides[name] + '_1', 'w')
neutron.export_to_hdf5(nuclides[name] + '_1', 'w',
libver=args.libver)
os.rename(nuclides[name] + '_1', nuclides[name])
except Exception as e:
print('Failed to convert {}: {}'.format(table.name, e))
Expand All @@ -187,7 +193,7 @@ for filename in ace_libraries:
# Determine filename
outfile = os.path.join(args.destination,
thermal.name.replace('.', '_') + '.h5')
thermal.export_to_hdf5(outfile, 'w')
thermal.export_to_hdf5(outfile, 'w', libver=args.libver)

# Register with library
library.register_file(outfile)
Expand All @@ -198,11 +204,13 @@ for filename in ace_libraries:
else:
# Then we only need to append the data
try:
thermal = openmc.data.ThermalScattering.from_hdf5(nuclides[name])
print('Converting {} (ACE) to {} (HDF5)'.format(table.name,
thermal.name))
thermal = openmc.data.ThermalScattering.from_hdf5(
nuclides[name])
print('Converting {} (ACE) to {} (HDF5)'
.format(table.name,thermal.name))
thermal.add_temperature_from_ace(table)
thermal.export_to_hdf5(nuclides[name] + '_1', 'w')
thermal.export_to_hdf5(nuclides[name] + '_1', 'w',
libver=args.libver)
os.rename(nuclides[name] + '_1', nuclides[name])
except Exception as e:
print('Failed to convert {}: {}'.format(table.name, e))
Expand Down
8 changes: 6 additions & 2 deletions scripts/openmc-convert-mcnp70-data
Expand Up @@ -26,6 +26,10 @@ parser = argparse.ArgumentParser(
)
parser.add_argument('-d', '--destination', default='mcnp_endfb70',
help='Directory to create new library in')
parser.add_argument('--libver', choices=['earliest', 'latest'],
default='earliest', help="Output HDF5 versioning. Use "
"'earliest' for backwards compatibility or 'latest' for "
"performance")
parser.add_argument('mcnpdata', help='Directory containing endf70[a-k] and endf70sab')
args = parser.parse_args()
assert os.path.isdir(args.mcnpdata)
Expand Down Expand Up @@ -62,7 +66,7 @@ for path in sorted(endf70):
# Export HDF5 file
h5_file = os.path.join(args.destination, data.name + '.h5')
print('Writing {}...'.format(h5_file))
data.export_to_hdf5(h5_file, 'w')
data.export_to_hdf5(h5_file, 'w', libver=args.libver)

# Register with library
library.register_file(h5_file)
Expand Down Expand Up @@ -91,7 +95,7 @@ if os.path.exists(endf70sab):
# Export HDF5 file
h5_file = os.path.join(args.destination, data.name + '.h5')
print('Writing {}...'.format(h5_file))
data.export_to_hdf5(h5_file, 'w')
data.export_to_hdf5(h5_file, 'w', libver=args.libver)

# Register with library
library.register_file(h5_file)
Expand Down
6 changes: 5 additions & 1 deletion scripts/openmc-convert-mcnp71-data
Expand Up @@ -28,6 +28,10 @@ parser.add_argument('-d', '--destination', default='mcnp_endfb71',
help='Directory to create new library in')
parser.add_argument('-f', '--fission_energy_release',
help='HDF5 file containing fission energy release data')
parser.add_argument('--libver', choices=['earliest', 'latest'],
default='earliest', help="Output HDF5 versioning. Use "
"'earliest' for backwards compatibility or 'latest' for "
"performance")
parser.add_argument('mcnpdata', help='Directory containing endf71x and ENDF71SaB')
args = parser.parse_args()
assert os.path.isdir(args.mcnpdata)
Expand Down Expand Up @@ -80,7 +84,7 @@ for basename, xs_list in sorted(suffixes.items()):
# Export HDF5 file
h5_file = os.path.join(args.destination, data.name + '.h5')
print('Writing {}...'.format(h5_file))
data.export_to_hdf5(h5_file, 'w')
data.export_to_hdf5(h5_file, 'w', libver=args.libver)

# Register with library
library.register_file(h5_file)
Expand Down
8 changes: 6 additions & 2 deletions scripts/openmc-get-jeff-data
Expand Up @@ -43,6 +43,10 @@ parser.add_argument('-b', '--batch', action='store_true',
help='supresses standard in')
parser.add_argument('-d', '--destination', default='jeff-3.2-hdf5',
help='Directory to create new library in')
parser.add_argument('--libver', choices=['earliest', 'latest'],
default='earliest', help="Output HDF5 versioning. Use "
"'earliest' for backwards compatibility or 'latest' for "
"performance")
args = parser.parse_args()

response = input(download_warning) if not args.batch else 'y'
Expand Down Expand Up @@ -179,7 +183,7 @@ for name, filenames in sorted(tables.items()):
# Export HDF5 file
h5_file = os.path.join(args.destination, data.name + '.h5')
print('Writing {}...'.format(h5_file))
data.export_to_hdf5(h5_file, 'w')
data.export_to_hdf5(h5_file, 'w', libver=args.libver)

# Register with library
library.register_file(h5_file)
Expand Down Expand Up @@ -222,7 +226,7 @@ for name, filenames in sorted(tables.items()):
# Export HDF5 file
h5_file = os.path.join(args.destination, data.name + '.h5')
print('Writing {}...'.format(h5_file))
data.export_to_hdf5(h5_file, 'w')
data.export_to_hdf5(h5_file, 'w', libver=args.libver)

# Register with library
library.register_file(h5_file)
Expand Down
11 changes: 9 additions & 2 deletions scripts/openmc-get-nndc-data
Expand Up @@ -33,6 +33,10 @@ parser = argparse.ArgumentParser(
)
parser.add_argument('-b', '--batch', action='store_true',
help='supresses standard in')
parser.add_argument('--libver', choices=['earliest', 'latest'],
default='earliest', help="Output HDF5 versioning. Use "
"'earliest' for backwards compatibility or 'latest' for "
"performance")
args = parser.parse_args()


Expand Down Expand Up @@ -154,7 +158,10 @@ ace_files = sorted(glob.glob(os.path.join('nndc', '**', '*.ace*')))
data_dir = os.path.dirname(sys.modules['openmc.data'].__file__)
fer_file = os.path.join(data_dir, 'fission_Q_data_endfb71.h5')

# Call the ace-to-hdf5 conversion script
pwd = os.path.dirname(os.path.realpath(__file__))
ace2hdf5 = os.path.join(pwd, 'openmc-ace-to-hdf5')
subprocess.call([ace2hdf5, '-d', 'nndc_hdf5', '--fission_energy_release',
fer_file] + ace_files)
subprocess.call([ace2hdf5,
'-d', 'nndc_hdf5',
'--fission_energy_release', fer_file,
'--libver', args.libver] + ace_files)

0 comments on commit 2c9b210

Please sign in to comment.