phigrape/io.cpp
2020-03-26 15:09:15 -04:00

81 lines
3.4 KiB
C++

#include "hdf5.h"
#include <string>
struct double3 {double x,y,z;};
void h5_write(const std::string file_name, const int step_num, const double t, const int N, const double *m, const double3 *x, const double3 *v, const double *pot, const double3 *acc, const double3 *jrk, const int write_mode=0, const bool use_double_precision=true)
{
char group_name[32], dataset_path[32];
bool write_pot = (write_mode )%2;
bool write_acc = (write_mode>>1)%2;
bool write_jrk = (write_mode>>2)%2;
hid_t file_id, group_id, attribute_id, dataset_id, dataspace_id; /* identifiers */
hsize_t dims[2] = {(hsize_t)N, 3};
herr_t status;
hid_t h5_float_type;
if (use_double_precision) h5_float_type = H5T_IEEE_F64LE;
else h5_float_type = H5T_IEEE_F32LE;
file_id = H5Fcreate(file_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
sprintf(group_name, "/Step#%d", step_num);
group_id = H5Gcreate2(file_id, group_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
dataspace_id = H5Screate(H5S_SCALAR);
attribute_id = H5Acreate2 (group_id, "Time", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
H5Awrite(attribute_id, H5T_NATIVE_DOUBLE, &t);
dataspace_id = H5Screate_simple(1, dims, NULL);
sprintf(dataset_path, "%s/MASS", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, m);
dataspace_id = H5Screate_simple(2, dims, NULL);
sprintf(dataset_path, "%s/X", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, x);
dataspace_id = H5Screate_simple(2, dims, NULL);
sprintf(dataset_path, "%s/V", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, v);
if (write_pot) {
dataspace_id = H5Screate_simple(1, dims, NULL);
sprintf(dataset_path, "%s/POT", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, pot);
}
if (write_acc) {
dataspace_id = H5Screate_simple(2, dims, NULL);
sprintf(dataset_path, "%s/ACC", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, acc);
}
if (write_jrk) {
dataspace_id = H5Screate_simple(2, dims, NULL);
sprintf(dataset_path, "%s/JRK", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, jrk);
}
H5Dclose(dataset_id);
H5Gclose(group_id);
H5Sclose(dataspace_id);
H5Fclose(file_id);
}
int main()
{
double3 aaa;
double pot;
int N = 25;
double3 x[N];
for (int i=0; i<N; i++) {
x[i].x=i+0.01;
x[i].y=i+0.02;
x[i].z=i+0.03;
}
h5_write("new_file.h5", 34, 987.654, N, (double*)x, x, x, (double*)x, x, x, 7);
}