Fixed issues and added a text loader

This commit is contained in:
Yohai Meiron 2020-04-11 22:01:16 -04:00
parent 4a0ee77276
commit a2219af116
4 changed files with 194 additions and 21 deletions

View file

@ -41,9 +41,9 @@ public:
double r2 = (pos[0]*pos[0] + pos[1]*pos[1] + pos[2]*pos[2] + b*b);
double r = sqrt(r2);
double r3_inv = 1/(r*r2);
acc[0] = G*M*pos[0]*r3_inv;
acc[1] = G*M*pos[1]*r3_inv;
acc[2] = G*M*pos[2]*r3_inv;
acc[0] = -G*M*pos[0]*r3_inv;
acc[1] = -G*M*pos[1]*r3_inv;
acc[2] = -G*M*pos[2]*r3_inv;
}
private:
@ -54,24 +54,32 @@ class Galaxy {
public:
Galaxy(std::string file_name)
{
std::vector<double> t, M_bulge_data, b_bulge_data;
std::vector<double> t_data, M_halo_data, b_halo_data;
t.push_back(0);
t.push_back(1);
t.push_back(2);
M_bulge_data.push_back(9e9);
M_bulge_data.push_back(1e10);
M_bulge_data.push_back(2e10);
interp_M_bulge = new Interp(t, M_bulge_data);
std::cout << "exiting..." << std::endl;
exit(0);
std::ifstream file(file_name);
std::string line;
while (std::getline(file, line)) {
auto pos = line.find('#');
if (pos != std::string::npos) line = line.substr(0, pos);
pos = line.find_first_not_of(" \t");
if (pos == std::string::npos) continue;
double data[3];
sscanf(line.c_str(), "%*s %lf %lf %lf", &data[0], &data[1], &data[2]);
t_data.push_back(data[0]);
M_halo_data.push_back(data[1]);
b_halo_data.push_back(data[2]); // note, this is not half-mass radius
}
interp_M_halo = new Interp(t_data, M_halo_data);
interp_b_halo = new Interp(t_data, b_halo_data);
}
int func(double t, const double y[], double f[], void *params)
{
double M_bulge = (*interp_M_bulge)(t);
Plummer plummer(M_bulge, 0);
double M_halo = (*interp_M_halo)(t);
double b_halo = (*interp_b_halo)(t);
/*printf("xxxxxxxxx %e, %e msun\n", t, M_halo);
printf("xxxxxxxxx %e, %e kpc\n", t, b_halo);
exit(0);*/
Plummer plummer(M_halo, b_halo);
f[0] = y[3]; // vx -> x'
f[1] = y[4]; // vy -> y'
f[2] = y[5]; // vz -> z'
@ -80,8 +88,8 @@ public:
}
private:
Interp *interp_M_bulge;
Interp *interp_b_bulge;
Interp *interp_M_halo;
Interp *interp_b_halo;
} galaxy("file.dat");
// Not very nice to have it as a global variable but GSL will have problem otherwise.
@ -99,7 +107,7 @@ int func(double t, const double y[], double f[], void *params)
extern "C"
int integrate(const double y0[], const double t_max, double y[])
{
double t = 0;
double t = 2.145;
constexpr double h = 1./4096.;
constexpr double epsabs = 1e-7;
constexpr double epsrel = 0;