335 lines
10 KiB
C++
Executable File
335 lines
10 KiB
C++
Executable File
#include "IO/hdf5_reader.h"
|
|
|
|
#include <hdf5.h>
|
|
#include <hdf5_hl.h>
|
|
|
|
#include "simulation/errors.h"
|
|
#include "constants/sim_constants.h"
|
|
|
|
Hdf5Reader::Hdf5Reader(std::string directory, std::string filename)
|
|
: absolute_filename_((directory + "/" + filename + ".h5").c_str()) {}
|
|
|
|
Hdf5Reader::Hdf5Reader(std::string absolute_filename)
|
|
: absolute_filename_(absolute_filename) {}
|
|
|
|
blitz::Array<double, 3> Hdf5Reader::readD3Double(std::string path) {
|
|
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
|
|
hid_t dataset;
|
|
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
|
|
hid_t dataspace = H5Dget_space(dataset);
|
|
if (dataset < 0) {
|
|
ERROR("The given dataset could not be opened");
|
|
}
|
|
// turn error report back onby hdf5 because of check H5Dopen2
|
|
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
|
|
|
|
const int ndims = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dims[ndims];
|
|
if (ndims != 3) {
|
|
ERROR("The given dataset is not threedimensional. It has dims: " + ndims);
|
|
}
|
|
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
|
|
|
blitz::Array<double, 3> data(dims[0], dims[1], dims[2]);
|
|
|
|
H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
|
data.data());
|
|
|
|
H5Dclose(dataset);
|
|
H5Fclose(file);
|
|
return data.copy();
|
|
}
|
|
|
|
blitz::Array<double, 2> Hdf5Reader::readD2Double(std::string path) {
|
|
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
|
|
hid_t dataset;
|
|
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
|
|
hid_t dataspace = H5Dget_space(dataset);
|
|
if (dataset < 0) {
|
|
ERROR("The given dataset could not be opened");
|
|
}
|
|
// turn error report back onby hdf5 because of check H5Dopen2
|
|
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
|
|
|
|
const int ndims = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dims[ndims];
|
|
if (ndims != 2) {
|
|
ERROR("The given dataset is not two dimensional. It has dims: " + ndims);
|
|
}
|
|
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
|
blitz::Array<double, 2> data(dims[0], dims[1]);
|
|
|
|
H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
|
data.data());
|
|
|
|
H5Sclose(dataspace);
|
|
H5Dclose(dataset);
|
|
H5Fclose(file);
|
|
return data.copy();
|
|
}
|
|
|
|
blitz::Array<double, 1> Hdf5Reader::readD1Double(std::string path) {
|
|
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
// H5Eset_auto(H5E_DEFAULT, NULL, NULL);
|
|
hid_t dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
|
|
hid_t dataspace = H5Dget_space(dataset);
|
|
if (dataset < 0) {
|
|
ERROR("The given dataset could not be opened");
|
|
}
|
|
// turn error report back onby hdf5 because of check H5Dopen2
|
|
// H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
|
|
|
|
const int ndims = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dims[ndims];
|
|
if (ndims != 1) {
|
|
ERROR("The given dataset is not one dimensional. It has dims: " + ndims);
|
|
}
|
|
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
|
|
|
blitz::Array<double, 1> data(dims[0]);
|
|
|
|
H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
|
data.data());
|
|
|
|
H5Sclose(dataspace);
|
|
H5Dclose(dataset);
|
|
H5Fclose(file);
|
|
return data.copy();
|
|
}
|
|
|
|
double Hdf5Reader::readDouble(std::string path) {
|
|
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
// H5Eset_auto(H5E_DEFAULT, NULL, NULL);
|
|
hid_t dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
|
|
hid_t dataspace = H5Dget_space(dataset);
|
|
if (dataset < 0) {
|
|
ERROR("The given dataset could not be opened");
|
|
}
|
|
// turn error report back onby hdf5 because of check H5Dopen2
|
|
// H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
|
|
|
|
const int ndims = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dims[ndims];
|
|
if (ndims != 1) {
|
|
ERROR("The given dataset is not one dimensional. It has dims: " + ndims);
|
|
}
|
|
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
|
|
|
double data;
|
|
|
|
H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
|
&data);
|
|
|
|
H5Sclose(dataspace);
|
|
H5Dclose(dataset);
|
|
H5Fclose(file);
|
|
return data;
|
|
}
|
|
|
|
blitz::Array<double, 4> Hdf5Reader::readD4Double(std::string path) {
|
|
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
|
|
hid_t dataset;
|
|
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
|
|
hid_t dataspace = H5Dget_space(dataset);
|
|
if (dataset < 0) {
|
|
ERROR("The given dataset could not be opened");
|
|
}
|
|
// turn error report back onby hdf5 because of check H5Dopen2
|
|
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
|
|
|
|
const int ndims = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dims[ndims];
|
|
if (ndims != 4) {
|
|
ERROR("The given dataset is not four-dimensional. It has dims: " + ndims);
|
|
}
|
|
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
|
|
|
blitz::Array<double, 4> data(dims[0], dims[1], dims[2], dims[3]);
|
|
|
|
H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
|
data.data());
|
|
|
|
H5Dclose(dataset);
|
|
H5Fclose(file);
|
|
return data.copy();
|
|
}
|
|
|
|
blitz::Array<double, 5> Hdf5Reader::readD5Double(std::string path) {
|
|
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
|
|
hid_t dataset;
|
|
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
|
|
hid_t dataspace = H5Dget_space(dataset);
|
|
if (dataset < 0) {
|
|
ERROR("The given dataset could not be opened");
|
|
}
|
|
// turn error report back onby hdf5 because of check H5Dopen2
|
|
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
|
|
|
|
const int ndims = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dims[ndims];
|
|
if (ndims != 5) {
|
|
ERROR("The given dataset is not five-dimensional. It has dims: " + ndims);
|
|
}
|
|
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
|
|
|
blitz::Array<double, 5> data(dims[0], dims[1], dims[2], dims[3], dims[4]);
|
|
|
|
H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
|
data.data());
|
|
|
|
H5Dclose(dataset);
|
|
H5Fclose(file);
|
|
return data.copy();
|
|
}
|
|
|
|
blitz::Array<long int, 4> Hdf5Reader::readD4LongInt(std::string path) {
|
|
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
|
|
hid_t dataset;
|
|
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
|
|
hid_t dataspace = H5Dget_space(dataset);
|
|
if (dataset < 0) {
|
|
ERROR("The given dataset could not be opened");
|
|
}
|
|
// turn error report back onby hdf5 because of check H5Dopen2
|
|
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
|
|
|
|
const int ndims = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dims[ndims];
|
|
if (ndims != 4) {
|
|
ERROR("The given dataset is not four-dimensional. It has dims: " + ndims);
|
|
}
|
|
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
|
|
|
blitz::Array<long int, 4> data(dims[0], dims[1], dims[2], dims[3]);
|
|
|
|
H5Dread(dataset, H5T_NATIVE_LONG, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
|
data.data());
|
|
|
|
H5Dclose(dataset);
|
|
H5Fclose(file);
|
|
return data.copy();
|
|
}
|
|
|
|
blitz::Array<long int, 5> Hdf5Reader::readD5LongInt(std::string path) {
|
|
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
|
|
hid_t dataset;
|
|
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
|
|
hid_t dataspace = H5Dget_space(dataset);
|
|
if (dataset < 0) {
|
|
ERROR("The given dataset could not be opened");
|
|
}
|
|
// turn error report back onby hdf5 because of check H5Dopen2
|
|
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
|
|
|
|
const int ndims = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dims[ndims];
|
|
if (ndims != 5) {
|
|
ERROR("The given dataset is not five-dimensional. It has dims: " + ndims);
|
|
}
|
|
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
|
|
|
blitz::Array<long int, 5> data(dims[0], dims[1], dims[2], dims[3], dims[4]);
|
|
|
|
H5Dread(dataset, H5T_NATIVE_LONG, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
|
data.data());
|
|
|
|
H5Dclose(dataset);
|
|
H5Fclose(file);
|
|
return data.copy();
|
|
}
|
|
|
|
// reads the engine stringstream with the correct size from the file, such that it should be able
|
|
// to be used for the initialization of the random engine without any further modification
|
|
// path has to only be the path to the group that contains the two dataset "string" and "length"
|
|
std::stringstream Hdf5Reader::readEngineString(std::string path) {
|
|
std::stringstream out;
|
|
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
// H5Eset_auto(H5E_DEFAULT, NULL, NULL);
|
|
hid_t dataset = H5Dopen2(file, (path + "/string").c_str(), H5P_DEFAULT);
|
|
hid_t dataspace = H5Dget_space(dataset);
|
|
if (dataset < 0) {
|
|
ERROR("The given dataset could not be opened");
|
|
}
|
|
// turn error report back onby hdf5 because of check H5Dopen2
|
|
// H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
|
|
// might be beneficial to check if the datatype
|
|
// is the expected one (H5T_STRING)
|
|
hid_t dtype = H5Dget_type(dataset);
|
|
size_t size = H5Tget_size(dtype);
|
|
|
|
if(size != SimConstants::engine_string_length_max) {
|
|
ERROR("Size of the string is not the required size of " +
|
|
std::to_string(SimConstants::engine_string_length_max) +
|
|
", size of string: " + std::to_string(size));
|
|
}
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("RestartInfo: " + std::to_string(H5Tdetect_class(dtype, H5T_STRING)));
|
|
#endif
|
|
|
|
const int ndims = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dims[ndims];
|
|
if (ndims != 1) {
|
|
ERROR("The given dataset is not one dimensional. It has dims: " + ndims);
|
|
}
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3(std::to_string(ndims));
|
|
DEBUG_LEVEL_3("RestartInfo: " + std::to_string(size));
|
|
#endif
|
|
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
|
|
|
char data[SimConstants::engine_string_length_max];
|
|
H5Dread(dataset, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
|
&data);
|
|
|
|
H5Tclose(dtype);
|
|
H5Sclose(dataspace);
|
|
H5Dclose(dataset);
|
|
|
|
dataset = H5Dopen2(file, (path + "/length").c_str(), H5P_DEFAULT);
|
|
dataspace = H5Dget_space(dataset);
|
|
if(dataspace < 0) {
|
|
ERROR("Dataspace for length could not be opened");
|
|
}
|
|
|
|
const int ndimsL = H5Sget_simple_extent_ndims(dataspace);
|
|
hsize_t dimsL[ndimsL];
|
|
if(ndimsL != 1) {
|
|
ERROR("length dataset is not 1-dimensional");
|
|
}
|
|
H5Sget_simple_extent_dims(dataspace, dimsL, NULL);
|
|
|
|
size_t length;
|
|
H5Dread(dataset, H5T_NATIVE_HSIZE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &length);
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3("RestartInfo: read length from file: " + std::to_string(length));
|
|
#endif
|
|
|
|
H5Sclose(dataspace);
|
|
H5Dclose(dataset);
|
|
|
|
H5Fclose(file);
|
|
|
|
std::stringstream temp;
|
|
temp << data;
|
|
out << temp.str().substr(0, length);
|
|
#ifdef debug_level3
|
|
DEBUG_LEVEL_3(std::to_string(out.str().size()));
|
|
#endif
|
|
|
|
return out;
|
|
} |