diff --git a/src/hdf5_interface.h b/src/hdf5_interface.h index d9b744685c..58d45e0eee 100644 --- a/src/hdf5_interface.h +++ b/src/hdf5_interface.h @@ -1,9 +1,6 @@ #ifndef OPENMC_HDF5_INTERFACE_H #define OPENMC_HDF5_INTERFACE_H -#include "hdf5.h" -#include "hdf5_hl.h" - #include #include #include @@ -11,8 +8,12 @@ #include #include -#include "position.h" +#include "hdf5.h" +#include "hdf5_hl.h" +#include "xtensor/xadapt.hpp" +#include "xtensor/xarray.hpp" +#include "position.h" namespace openmc { @@ -148,6 +149,105 @@ struct H5TypeMap { static const hid_t type_id; }; // Template functions used to provide simple interface to lower-level functions //============================================================================== +// Scalar version +template +void read_attribute(hid_t obj_id, const char* name, T& buffer) +{ + read_attr(obj_id, name, H5TypeMap::type_id, buffer); +} + +// vector version +template +void read_attribute(hid_t obj_id, const char* name, std::vector& vec) +{ + // Get shape of attribute array + auto shape = attribute_shape(obj_id, name); + + // Allocate new array to read data into + std::size_t size = 1; + for (const auto x : shape) + size *= x; + vec.resize(size); + + // Read data from attribute + read_attr(obj_id, name, H5TypeMap::type_id, vec.data()); +} + +// Generic array version +template +void read_attribute(hid_t obj_id, const char* name, xt::xarray& arr) +{ + // Get shape of attribute array + auto shape = attribute_shape(obj_id, name); + + // Allocate new array to read data into + std::size_t size = 1; + for (const auto x : shape) + size *= x; + T* buffer = new T[size]; + + // Read data from attribute + read_attr(obj_id, name, H5TypeMap::type_id, buffer); + + // Adapt array into xarray + arr = xt::adapt(buffer, size, xt::acquire_ownership(), shape); +} + +template +void read_dataset(hid_t obj_id, const char* name, T buffer, bool indep=false) +{ + read_dataset(obj_id, name, H5TypeMap::type_id, &buffer, indep); +} + +template +void read_dataset(hid_t dset, std::vector& vec, bool indep=false) +{ + // Get shape of dataset + std::vector shape = object_shape(dset); + + // Resize vector to appropriate size + vec.resize(shape[0]); + + // Read data into vector + read_dataset(dset, nullptr, H5TypeMap::type_id, vec.data(), indep); +} + +template +void read_dataset(hid_t obj_id, const char* name, std::vector& vec, bool indep=false) +{ + hid_t dset = open_dataset(obj_id, name); + read_dataset(dset, vec, indep); + close_dataset(dset); +} + +template +void read_dataset(hid_t dset, xt::xarray& arr, bool indep=false) +{ + // Get shape of dataset + std::vector shape = object_shape(dset); + + // Allocate new array to read data into + std::size_t size = 1; + for (const auto x : shape) + size *= x; + T* buffer = new T[size]; + + // Read data from attribute + read_dataset(dset, nullptr, H5TypeMap::type_id, buffer, indep); + + // Adapt into xarray + arr = xt::adapt(buffer, size, xt::acquire_ownership(), shape); +} + +template +void read_dataset(hid_t obj_id, const char* name, xt::xarray& arr, bool indep=false) +{ + // Open dataset and read array + hid_t dset = open_dataset(obj_id, name); + read_dataset(dset, arr, indep); + close_dataset(dset); +} + template inline void write_attribute(hid_t obj_id, const char* name, T buffer) {