Completed HDF5 support

This commit is contained in:
Yohai Meiron 2020-03-31 19:04:18 -04:00
parent b2943be7c1
commit 5cb4282be4
8 changed files with 258 additions and 185 deletions

View file

@ -53,7 +53,10 @@ Coded by : Peter Berczik
Version number : 19.04
Last redaction : 2019.04.16 12:55
*****************************************************************************/
#include "double3.h"
#include "config.h"
#include "io.h"
Config *config;
#define NORM // Physical normalization
@ -177,84 +180,6 @@ Config *config;
#define N_MAX (6*MB)
#define N_MAX_loc (2*MB)
struct double3 {
double data[3];
double3() {}
double3(const double x, const double y, const double z)
{
data[0] = x;
data[1] = y;
data[2] = z;
}
double& operator[](int i) {return data[i];}
double3& operator=(const double3& a)
{
data[0] = a.data[0];
data[1] = a.data[1];
data[2] = a.data[2];
return *this;
}
double3& operator+=(const double3& a)
{
data[0] += a.data[0];
data[1] += a.data[1];
data[2] += a.data[2];
return *this;
}
double3& operator-=(const double3& a)
{
data[0] -= a.data[0];
data[1] -= a.data[1];
data[2] -= a.data[2];
return *this;
}
double3& operator/=(const double& c)
{
data[0] /= c;
data[1] /= c;
data[2] /= c;
return *this;
}
double norm2() const
{
return data[0]*data[0]+data[1]*data[1]+data[2]*data[2];
}
double norm() const
{
return sqrt(data[0]*data[0]+data[1]*data[1]+data[2]*data[2]);
}
operator double*() {return data;}
};
double3 operator*(const double& c, const double3& a)
{
return double3(a.data[0]*c, a.data[1]*c, a.data[2]*c);
}
double3 operator*(const double3& a, const double& c)
{
return double3(a.data[0]*c, a.data[1]*c, a.data[2]*c);
}
double operator*(const double3& a, const double3& b)
{
return a.data[0]*b.data[0]+a.data[1]*b.data[1]+a.data[2]*b.data[2];
}
double3 operator/(const double3& a, const double& c)
{
return double3(a.data[0]/c, a.data[1]/c, a.data[2]/c);
}
double3 operator+(const double3& a, const double3& b)
{
return double3(a.data[0]+b.data[0], a.data[1]+b.data[1], a.data[2]+b.data[2]);
}
double3 operator-(const double3& a, const double3& b)
{
return double3(a.data[0]-b.data[0], a.data[1]-b.data[1], a.data[2]-b.data[2]);
}
@ -366,16 +291,6 @@ void get_CPU_time(double *time_real, double *time_user, double *time_syst)
*time_user = *time_real;
}
void read_data(char inp_fname[], int *diskstep, int *N, double *time_cur, int ind[], double m[], double3 x[], double3 v[])
{
auto inp = fopen(inp_fname, "r");
fscanf(inp, "%d \n", diskstep);
fscanf(inp, "%d \n", N);
fscanf(inp, "%lE \n", time_cur);
for (int i=0; i<*N; i++) fscanf(inp,"%d %lE %lE %lE %lE %lE %lE %lE \n", &ind[i], &m[i], &x[i][0], &x[i][1], &x[i][2], &v[i][0], &v[i][1], &v[i][2]);
fclose(inp);
}
void write_snap_data(char out_fname[], int diskstep, int N, double time_cur, int ind[], double m[], double3 x[], double3 v[])
{
auto out = fopen(out_fname, "w");
@ -962,6 +877,17 @@ int main(int argc, char *argv[])
C_NB = config->pn_c;
std::copy(config->pn_usage.begin(), config->pn_usage.end(), usedOrNot);
if (is_hdf5(config->input_file_name)) {
#ifndef HAS_HDF5
fprintf(stderr, "ERROR: input file is in HDF5 format, but the code was compiled without HDF5 support\n")
return -1;
#endif
h5_read(config->input_file_name, &diskstep, &N, &time_cur, m, x, v);
}
else
ascii_read(config->input_file_name, &diskstep, &N, &time_cur, m, x, v);
std::iota(ind, ind+N, 0);
if (myRank == rootRank) {
//TODO move it out of (myRank == rootRank) so you don't need to communicate them.
@ -995,12 +921,6 @@ int main(int argc, char *argv[])
fscanf(inp,"%lE %lE %lE", &m_ext, &r_ext, &g_ext);
#endif
/* read the global data for particles to the rootRank */
read_data(inp_fname, &diskstep, &N, &time_cur, ind, m, x, v);
/* possible coordinate & velocity limits for ALL particles !!! */
printf("\n");
printf("Begin the calculation of phi-GRAPE program on %03d processors\n", n_proc);
printf("\n");
@ -1994,7 +1914,8 @@ int main(int argc, char *argv[])
/* write cont data */
write_snap_data((char*)"data.con", diskstep, N, time_cur, ind, m, x, v);
if (config->output_hdf5) h5_write("data.con", diskstep, N, time_cur, m, x, v, pot, a, adot, 0, true);
else ascii_write("data.con", diskstep, N, time_cur, m, x, v, 16);
/* possible OUT for timing !!! */
@ -2049,8 +1970,10 @@ int main(int argc, char *argv[])
if (myRank == rootRank) {
diskstep++;
char out_fname[256];
sprintf(out_fname, "%06d.dat", diskstep);
write_snap_data(out_fname, diskstep, N, time_cur, ind, m, x, v);
sprintf(out_fname, "%06d", diskstep);
if (config->output_hdf5) h5_write(std::string(out_fname) + ".h5", diskstep, N, time_cur, m, x, v, pot, a, adot, 0, true);
else ascii_write(std::string(out_fname) + ".dat", diskstep, N, time_cur, m, x, v, 10);
// TODO custom precision
} /* if (myRank == rootRank) */
#ifdef ETICS_DUMP