Templatized read_nd_vector

This commit is contained in:
Adam G Nelson 2018-09-08 06:44:49 -04:00
parent 4b56c86c50
commit 1cf1deb9f0
2 changed files with 36 additions and 166 deletions

View file

@ -14,6 +14,7 @@
#include "xtensor/xarray.hpp"
#include "openmc/position.h"
#include "openmc/error.h"
namespace openmc {
@ -46,31 +47,6 @@ hid_t file_open(const std::string& filename, char mode, bool parallel=false);
void write_string(hid_t group_id, const char* name, const std::string& buffer,
bool indep);
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<double, 1>& result,
bool must_have = false);
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<double, 2>& result,
bool must_have = false);
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<int, 2>& result,
bool must_have = false);
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<double, 3>& result,
bool must_have = false);
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<int, 3>& result,
bool must_have = false);
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<double, 4>& result,
bool must_have = false);
std::vector<hsize_t> attribute_shape(hid_t obj_id, const char* name);
std::vector<std::string> dataset_names(hid_t group_id);
void ensure_exists(hid_t group_id, const char* name);
@ -228,7 +204,7 @@ read_attribute(hid_t obj_id, const char* name, std::vector<std::string>& vec)
}
//==============================================================================
// Templates/overloads for read_dataset
// Templates/overloads for read_dataset and related methods
//==============================================================================
template<typename T>
@ -286,6 +262,40 @@ void read_dataset(hid_t obj_id, const char* name, xt::xarray<T>& arr, bool indep
close_dataset(dset);
}
template <typename T, std::size_t N>
void read_dataset_as_shape(hid_t obj_id, const char* name,
xt::xtensor<T, N>& arr, bool indep=false)
{
hid_t dset = open_dataset(obj_id, name);
// Allocate new array to read data into
std::size_t size = 1;
for (const auto x : arr.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(), arr.shape());
close_dataset(dset);
}
template <typename T, std::size_t N>
void read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<T, N>& result,
bool must_have=false)
{
if (object_exists(obj_id, name)) {
read_dataset_as_shape(obj_id, name, result, true);
} else if (must_have) {
fatal_error(std::string("Must provide " + std::string(name) + "!"));
}
}
//==============================================================================
// Templates/overloads for write_attribute
//==============================================================================

View file

@ -14,7 +14,6 @@
#include "mpi.h"
#include "openmc/message_passing.h"
#endif
#include "openmc/error.h"
namespace openmc {
@ -535,145 +534,6 @@ read_complex(hid_t obj_id, const char* name, std::complex<double>* buffer, bool
}
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<double, 1>& result,
bool must_have)
{
if (object_exists(obj_id, name)) {
int dim1 = result.shape()[0];
double temp_arr[dim1];
read_double(obj_id, name, temp_arr, true);
int temp_idx = 0;
for (int i = 0; i < dim1; i++) {
result(i) = temp_arr[temp_idx++];
}
} else if (must_have) {
fatal_error(std::string("Must provide " + std::string(name) + "!"));
}
}
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<double, 2>& result,
bool must_have)
{
if (object_exists(obj_id, name)) {
int dim1 = result.shape()[0];
int dim2 = result.shape()[1];
double temp_arr[dim1 * dim2];
read_double(obj_id, name, temp_arr, true);
int temp_idx = 0;
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
result(i, j) = temp_arr[temp_idx++];
}
}
} else if (must_have) {
fatal_error(std::string("Must provide " + std::string(name) + "!"));
}
}
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<int, 2>& result,
bool must_have)
{
if (object_exists(obj_id, name)) {
int dim1 = result.shape()[0];
int dim2 = result.shape()[1];
int temp_arr[dim1 * dim2];
read_int(obj_id, name, temp_arr, true);
int temp_idx = 0;
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
result(i, j) = temp_arr[temp_idx++];
}
}
} else if (must_have) {
fatal_error(std::string("Must provide " + std::string(name) + "!"));
}
}
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<double, 3>& result,
bool must_have)
{
if (object_exists(obj_id, name)) {
int dim1 = result.shape()[0];
int dim2 = result.shape()[1];
int dim3 = result.shape()[2];
double temp_arr[dim1 * dim2 * dim3];
read_double(obj_id, name, temp_arr, true);
int temp_idx = 0;
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
for (int k = 0; k < dim3; k++) {
result(i, j, k) = temp_arr[temp_idx++];
}
}
}
} else if (must_have) {
fatal_error(std::string("Must provide " + std::string(name) + "!"));
}
}
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<int, 3>& result,
bool must_have)
{
if (object_exists(obj_id, name)) {
int dim1 = result.shape()[0];
int dim2 = result.shape()[1];
int dim3 = result.shape()[2];
int temp_arr[dim1 * dim2 * dim3];
read_int(obj_id, name, temp_arr, true);
int temp_idx = 0;
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
for (int k = 0; k < dim3; k++) {
result(i, j, k) = temp_arr[temp_idx++];
}
}
}
} else if (must_have) {
fatal_error(std::string("Must provide " + std::string(name) + "!"));
}
}
void
read_nd_vector(hid_t obj_id, const char* name, xt::xtensor<double, 4>& result,
bool must_have)
{
if (object_exists(obj_id, name)) {
int dim1 = result.shape()[0];
int dim2 = result.shape()[1];
int dim3 = result.shape()[2];
int dim4 = result.shape()[3];
double temp_arr[dim1 * dim2 * dim3 * dim4];
read_double(obj_id, name, temp_arr, true);
int temp_idx = 0;
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
for (int k = 0; k < dim3; k++) {
for (int l = 0; l < dim4; l++) {
result(i, j, k, l) = temp_arr[temp_idx++];
}
}
}
}
} else if (must_have) {
fatal_error(std::string("Must provide " + std::string(name) + "!"));
}
}
void
read_tally_results(hid_t group_id, hsize_t n_filter, hsize_t n_score, double* results)
{