From 3cfd9dff9db5d54252192bf49d48e2affe9adf53 Mon Sep 17 00:00:00 2001 From: Adam G Nelson Date: Mon, 10 Dec 2018 20:01:03 -0500 Subject: [PATCH] Added xtensor read_dataset --- include/openmc/hdf5_interface.h | 28 ++++++++++++++++++++++++++++ src/urr.cpp | 21 ++++++--------------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/include/openmc/hdf5_interface.h b/include/openmc/hdf5_interface.h index 0c940342c..5b5d2a999 100644 --- a/include/openmc/hdf5_interface.h +++ b/include/openmc/hdf5_interface.h @@ -271,6 +271,34 @@ void read_dataset(hid_t obj_id, const char* name, xt::xarray& arr, bool indep close_dataset(dset); } + +template +void read_dataset(hid_t obj_id, const char* name, xt::xtensor& arr, bool indep=false) +{ + // Open dataset and read array + hid_t dset = open_dataset(obj_id, name); + + // Get shape of dataset + std::vector hsize_t_shape = object_shape(dset); + close_dataset(dset); + + // cast from hsize_t to size_t + std::vector shape(hsize_t_shape.size()); + for (int i = 0; i < shape.size(); i++) { + shape[i] = static_cast(hsize_t_shape[i]); + } + + // Allocate new xarray to read data into + xt::xarray xarr(shape); + + // Read data from the dataset + read_dataset(obj_id, name, xarr); + + // Copy into xtensor + arr = xarr; + +} + template void read_dataset_as_shape(hid_t obj_id, const char* name, xt::xtensor& arr, bool indep=false) diff --git a/src/urr.cpp b/src/urr.cpp index 4a6643bfc..28ad2505f 100644 --- a/src/urr.cpp +++ b/src/urr.cpp @@ -18,23 +18,14 @@ UrrData::UrrData(hid_t group_id) read_attribute(group_id, "multiply_smooth", temp_multiply_smooth); multiply_smooth_ = (temp_multiply_smooth == 1); - // read the enrgies at which tables exist - hid_t dset = open_dataset(group_id, "energy"); - hsize_t dims[1]; - get_shape(dset, dims); - close_dataset(dset); - n_energy_ = static_cast(dims[0]); - energy_ = xt::xtensor({dims[0]}, 0.); - read_dataset_as_shape(group_id, "energy", energy_); + // read the energies at which tables exist + read_dataset(group_id, "energy", energy_); + + // Set n_energy_ + n_energy_ = energy_.shape()[0]; // Read URR tables - dset = open_dataset(group_id, "table"); - hsize_t dims3[3]; - get_shape(dset, dims3); - close_dataset(dset); - xt::xarray temp_arr({dims3[0], dims3[1], dims3[2]}, 0.); - read_dataset(group_id, "table", temp_arr); - prob_ = temp_arr; + read_dataset(group_id, "table", prob_); } } \ No newline at end of file