Added new reading routines (engine state,
4d double, 4d int); fixed error in RestartInfo output; added the maximum length of the engine state string into sim_constants.h
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#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()) {}
|
||||
@@ -99,4 +100,139 @@ blitz::Array<double, 1> Hdf5Reader::readD1Double(std::string path) {
|
||||
H5Dclose(dataset);
|
||||
H5Fclose(file);
|
||||
return data.copy();
|
||||
}
|
||||
|
||||
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<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();
|
||||
}
|
||||
|
||||
|
||||
// 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 6730, size of string: "
|
||||
+ std::to_string(size));
|
||||
}
|
||||
DEBUG_LEVEL_3("RestartInfo: " + std::to_string(H5Tdetect_class(dtype, H5T_STRING)));
|
||||
|
||||
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);
|
||||
}
|
||||
DEBUG_LEVEL_3(std::to_string(ndims));
|
||||
DEBUG_LEVEL_3("RestartInfo: " + std::to_string(size));
|
||||
H5Sget_simple_extent_dims(dataspace, dims, NULL);
|
||||
|
||||
char data[SimConstants::engine_string_length_max];
|
||||
H5Dread(dataset, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT,
|
||||
&data);
|
||||
|
||||
std::stringstream temp;
|
||||
temp << 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);
|
||||
DEBUG_LEVEL_3("RestartInfo: read length from file: " + std::to_string(length));
|
||||
|
||||
H5Sclose(dataspace);
|
||||
H5Dclose(dataset);
|
||||
|
||||
H5Fclose(file);
|
||||
|
||||
out << temp.str().substr(0, length);
|
||||
DEBUG_LEVEL_3(std::to_string(out.str().size()));
|
||||
|
||||
return out;
|
||||
}
|
||||
Reference in New Issue
Block a user