Implement to/from_hdf5 methods for Python Sum class

This commit is contained in:
Paul Romano 2021-10-13 07:28:35 -05:00
parent 8bb2002d8f
commit e453901c84
2 changed files with 41 additions and 0 deletions

View file

@ -578,6 +578,45 @@ class Sum(EqualityMixin):
cv.check_type('functions', functions, Iterable, Callable)
self._functions = functions
def to_hdf5(self, group, name='xy'):
"""Write sum of functions to an HDF5 group
Parameters
----------
group : h5py.Group
HDF5 group to write to
name : str
Name of the dataset to create
"""
sum_group = group.create_group(name)
sum_group.attrs['type'] = np.string_(type(self).__name__)
sum_group.attrs['n'] = len(self.functions)
for i, f in enumerate(self.functions):
f.to_hdf5(sum_group, f'func_{i+1}')
@classmethod
def from_hdf5(cls, group):
"""Generate sum of functions from an HDF5 group
Parameters
----------
group : h5py.Group
Group to read from
Returns
-------
openmc.data.Sum
Functions read from the group
"""
n = group.attrs['n']
functions = [
Function1D.from_hdf5(group[f'func_{i+1}'])
for i in range(n)
]
return cls(functions)
class Regions1D(EqualityMixin):
r"""Piecewise composition of multiple functions.

View file

@ -102,6 +102,8 @@ unique_ptr<Function1D> read_function(hid_t group, const char* name)
func = make_unique<CoherentElasticXS>(obj_id);
} else if (func_type == "IncoherentElastic") {
func = make_unique<IncoherentElasticXS>(obj_id);
} else if (func_type == "Sum") {
func = make_unique<Sum1D>(obj_id);
} else {
throw std::runtime_error {"Unknown function type " + func_type +
" for dataset " + object_name(obj_id)};