diff --git a/openmc/data/function.py b/openmc/data/function.py index b5aa2117d3..7a73987df2 100644 --- a/openmc/data/function.py +++ b/openmc/data/function.py @@ -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. diff --git a/src/endf.cpp b/src/endf.cpp index 3ccfdbd214..c0c1d2e7e8 100644 --- a/src/endf.cpp +++ b/src/endf.cpp @@ -102,6 +102,8 @@ unique_ptr read_function(hid_t group, const char* name) func = make_unique(obj_id); } else if (func_type == "IncoherentElastic") { func = make_unique(obj_id); + } else if (func_type == "Sum") { + func = make_unique(obj_id); } else { throw std::runtime_error {"Unknown function type " + func_type + " for dataset " + object_name(obj_id)};