Moved config to inside main and changed several arrays into vectors

This commit is contained in:
Yohai Meiron 2020-10-27 23:55:55 -04:00
parent 329dd2ca4d
commit 747f9f9d89
7 changed files with 115 additions and 108 deletions

20
io.cpp
View file

@ -24,7 +24,7 @@ bool is_hdf5(std::string file_name)
return result;
}
void ascii_read(const std::string file_name, int& step_num, int& N, double& t, double **m, double3 **x, double3 **v)
void ascii_read(const std::string file_name, int &step_num, int &N, double& t, std::vector<double> &m, std::vector<double3> &x, std::vector<double3> &v)
{
char rest[512];
int result;
@ -45,19 +45,19 @@ void ascii_read(const std::string file_name, int& step_num, int& N, double& t, d
result = sscanf(str.c_str(), "%lf%s", &t, rest);
if (result!=1) throw std::runtime_error("Error parsing line 3: expected one real number");
*m = new double[N];
*x = new double3[N];
*v = new double3[N];
m.resize(N);
x.resize(N);
v.resize(N);
int i = -1;
while (std::getline(file, str)) {
if (++i > N) throw std::runtime_error("Error parsing line " + std::to_string(i+4) + ": particle out of range");
result = sscanf(str.c_str(), "%*s %lf %lf %lf %lf %lf %lf %lf%s", &(*m)[i], &(*x)[i][0], &(*x)[i][1], &(*x)[i][2], &(*v)[i][0], &(*v)[i][1], &(*v)[i][2], rest);
result = sscanf(str.c_str(), "%*s %lf %lf %lf %lf %lf %lf %lf%s", &m[i], &(x[i].x), &(x[i].y), &(x[i].z), &(v[i].x), &(v[i].y), &(v[i].z), rest);
}
file.close();
}
void ascii_write(const std::string file_name, const int step_num, const int N, const double t, const double *m, const double3 *x, const double3 *v, int precision=10)
void ascii_write(const std::string file_name, const int step_num, const int N, const double t, const std::vector<double> &m, const std::vector<double3> &x, const std::vector<double3> &v, int precision=10)
{
auto file = std::ofstream(file_name);
if (!file.is_open()) throw std::runtime_error("Cannot open file for output");
@ -146,7 +146,7 @@ void h5_read(const std::string file_name, int *step_num, int *N, double *t, doub
#endif
}
void h5_write(const std::string file_name, const int step_num, const int N, const double t, const double *m, const double3 *x, const double3 *v, const std::vector<double>& pot, const double3 *acc, const double3 *jrk, const int extra_mode=0, const bool use_double_precision=true)
void h5_write(const std::string file_name, const int step_num, const int N, const double t, std::vector<double> &m, std::vector<double3> &x, std::vector<double3> &v, const std::vector<double> &pot, const double3 *acc, const double3 *jrk, const int extra_mode=0, const bool use_double_precision=true)
{
#ifdef HAS_HDF5
hid_t file_id, group_id, attribute_id, dataspace_id;
@ -174,9 +174,9 @@ void h5_write(const std::string file_name, const int step_num, const int N, cons
H5Sclose(dataspace_id);
};
write_dataset("MASS", 1, (double*)m);
write_dataset("X", 2, (double*)x);
write_dataset("V", 2, (double*)v);
write_dataset("MASS", 1, m.data());
write_dataset("X", 2, (double*)x.data());
write_dataset("V", 2, (double*)v.data());
bool write_pot = (extra_mode ) & 1;
bool write_acc = (extra_mode >> 1) & 1;