From 1cf1deb9f0f3420b5966b18d9714a72d52affa3c Mon Sep 17 00:00:00 2001 From: Adam G Nelson Date: Sat, 8 Sep 2018 06:44:49 -0400 Subject: [PATCH] Templatized read_nd_vector --- include/openmc/hdf5_interface.h | 62 ++++++++------ src/hdf5_interface.cpp | 140 -------------------------------- 2 files changed, 36 insertions(+), 166 deletions(-) diff --git a/include/openmc/hdf5_interface.h b/include/openmc/hdf5_interface.h index b791c8d729..2f5d7cb525 100644 --- a/include/openmc/hdf5_interface.h +++ b/include/openmc/hdf5_interface.h @@ -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& result, - bool must_have = false); - -void -read_nd_vector(hid_t obj_id, const char* name, xt::xtensor& result, - bool must_have = false); - -void -read_nd_vector(hid_t obj_id, const char* name, xt::xtensor& result, - bool must_have = false); - -void -read_nd_vector(hid_t obj_id, const char* name, xt::xtensor& result, - bool must_have = false); - -void -read_nd_vector(hid_t obj_id, const char* name, xt::xtensor& result, - bool must_have = false); - -void -read_nd_vector(hid_t obj_id, const char* name, xt::xtensor& result, - bool must_have = false); - - std::vector attribute_shape(hid_t obj_id, const char* name); std::vector 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& vec) } //============================================================================== -// Templates/overloads for read_dataset +// Templates/overloads for read_dataset and related methods //============================================================================== template @@ -286,6 +262,40 @@ void read_dataset(hid_t obj_id, const char* name, xt::xarray& arr, bool indep close_dataset(dset); } + +template +void read_dataset_as_shape(hid_t obj_id, const char* name, + xt::xtensor& 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::type_id, buffer, indep); + + // Adapt into xarray + arr = xt::adapt(buffer, size, xt::acquire_ownership(), arr.shape()); + + close_dataset(dset); +} + + +template +void read_nd_vector(hid_t obj_id, const char* name, xt::xtensor& 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 //============================================================================== diff --git a/src/hdf5_interface.cpp b/src/hdf5_interface.cpp index 514cf6966d..4badc0441c 100644 --- a/src/hdf5_interface.cpp +++ b/src/hdf5_interface.cpp @@ -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* buffer, bool } -void -read_nd_vector(hid_t obj_id, const char* name, xt::xtensor& 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& 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& 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& 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& 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& 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) {