mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-28 06:05:58 -04:00
Add templated versions of read_attribute and read_dataset
This commit is contained in:
parent
3ea64d545c
commit
e1edaecb03
1 changed files with 104 additions and 4 deletions
|
|
@ -1,9 +1,6 @@
|
|||
#ifndef OPENMC_HDF5_INTERFACE_H
|
||||
#define OPENMC_HDF5_INTERFACE_H
|
||||
|
||||
#include "hdf5.h"
|
||||
#include "hdf5_hl.h"
|
||||
|
||||
#include <array>
|
||||
#include <complex>
|
||||
#include <cstddef>
|
||||
|
|
@ -11,8 +8,12 @@
|
|||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#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<typename T>
|
||||
void read_attribute(hid_t obj_id, const char* name, T& buffer)
|
||||
{
|
||||
read_attr(obj_id, name, H5TypeMap<T>::type_id, buffer);
|
||||
}
|
||||
|
||||
// vector version
|
||||
template<typename T>
|
||||
void read_attribute(hid_t obj_id, const char* name, std::vector<T>& 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<T>::type_id, vec.data());
|
||||
}
|
||||
|
||||
// Generic array version
|
||||
template<typename T>
|
||||
void read_attribute(hid_t obj_id, const char* name, xt::xarray<T>& 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<T>::type_id, buffer);
|
||||
|
||||
// Adapt array into xarray
|
||||
arr = xt::adapt(buffer, size, xt::acquire_ownership(), shape);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void read_dataset(hid_t obj_id, const char* name, T buffer, bool indep=false)
|
||||
{
|
||||
read_dataset(obj_id, name, H5TypeMap<T>::type_id, &buffer, indep);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void read_dataset(hid_t dset, std::vector<T>& vec, bool indep=false)
|
||||
{
|
||||
// Get shape of dataset
|
||||
std::vector<hsize_t> shape = object_shape(dset);
|
||||
|
||||
// Resize vector to appropriate size
|
||||
vec.resize(shape[0]);
|
||||
|
||||
// Read data into vector
|
||||
read_dataset(dset, nullptr, H5TypeMap<T>::type_id, vec.data(), indep);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void read_dataset(hid_t obj_id, const char* name, std::vector<T>& vec, bool indep=false)
|
||||
{
|
||||
hid_t dset = open_dataset(obj_id, name);
|
||||
read_dataset(dset, vec, indep);
|
||||
close_dataset(dset);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void read_dataset(hid_t dset, xt::xarray<T>& arr, bool indep=false)
|
||||
{
|
||||
// Get shape of dataset
|
||||
std::vector<hsize_t> 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<T>::type_id, buffer, indep);
|
||||
|
||||
// Adapt into xarray
|
||||
arr = xt::adapt(buffer, size, xt::acquire_ownership(), shape);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void read_dataset(hid_t obj_id, const char* name, xt::xarray<T>& 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<typename T> inline void
|
||||
write_attribute(hid_t obj_id, const char* name, T buffer)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue