275 lines
No EOL
10 KiB
C++
275 lines
No EOL
10 KiB
C++
#include <array>
|
|
#include <atomic>
|
|
#include <Eigen/Geometry>
|
|
#include <fstream>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <mutex>
|
|
#include <random>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <time.h> // We just want to print the timestamp
|
|
#include <unistd.h>
|
|
|
|
using double3 = Eigen::Vector3d;
|
|
|
|
// Our units are {kiloparsec, solar mass, gigayear}
|
|
constexpr double G = 4.498317481097514e-06;
|
|
|
|
extern "C"
|
|
void integrate(const double y0[], const double t_max, const double stride_size, double y[]);
|
|
|
|
std::string git_version() {
|
|
std::string result;
|
|
std::ifstream git_log(".git/logs/HEAD");
|
|
if (git_log) {
|
|
std::string prev_line, line;
|
|
getline(git_log, prev_line);
|
|
while (getline(git_log, line)) {
|
|
prev_line = line;
|
|
}
|
|
if (prev_line.length() > 51) result = prev_line.substr(41, 10);
|
|
else result = "UNKNOWN";
|
|
} else result = "UNKNOWN";
|
|
return result;
|
|
}
|
|
|
|
std::string iso_time(std::chrono::system_clock::time_point time)
|
|
{
|
|
auto time_c = std::chrono::system_clock::to_time_t(time);
|
|
auto timeinfo = gmtime(&time_c);
|
|
char time_string[256];
|
|
strftime(time_string,256,"%FT%TZ", timeinfo);
|
|
return std::string(time_string);
|
|
}
|
|
|
|
class In_box_cyl {
|
|
public:
|
|
In_box_cyl(const double pos_tol, const double vel_tol, const double3 target_pos, const double3 target_vel)
|
|
: pos_tol(pos_tol), vel_tol(vel_tol)
|
|
{
|
|
target_d = sqrt(target_pos[0]*target_pos[0] + target_pos[1]*target_pos[1]);
|
|
target_z = target_pos[2];
|
|
target_vd = (target_pos[0]*target_vel[0] + target_pos[1]*target_vel[1])/target_d;
|
|
target_vt = (target_pos[0]*target_vel[1] - target_pos[1]*target_vel[0])/target_d;
|
|
target_vz = target_vel[2];
|
|
}
|
|
bool operator()(const double w[6]) {
|
|
const double d = sqrt(w[0]*w[0] + w[1]*w[1]);
|
|
const double& z = w[2];
|
|
const double vd = (w[0]*w[3] + w[1]*w[4])/d;
|
|
const double vt = (w[0]*w[4] - w[1]*w[3])/d;
|
|
const double& vz = w[5];
|
|
return (abs(d - target_d) < pos_tol)
|
|
&& (abs(z - target_z) < pos_tol)
|
|
&& (abs(vd - target_vd) < vel_tol)
|
|
&& (abs(vt - target_vt) < vel_tol)
|
|
&& (abs(vz - target_vz) < vel_tol);
|
|
}
|
|
friend std::ostream& operator<<(std::ostream& stream, const In_box_cyl& obj)
|
|
{
|
|
stream.setf(std::ios::scientific);
|
|
stream << "# Acceptor type: box in cylindrical coordinates\n";
|
|
stream << "# Acceptor parameters:\n";
|
|
stream << "# pos_tol = " << std::setw(13) << obj.pos_tol << '\n';
|
|
stream << "# vel_tol = " << std::setw(13) << obj.vel_tol << '\n';
|
|
stream << "# target_d = " << std::setw(13) << obj.target_d << '\n';
|
|
stream << "# target_z = " << std::setw(13) << obj.target_z << '\n';
|
|
stream << "# target_vd = " << std::setw(13) << obj.target_vd << '\n';
|
|
stream << "# target_vt = " << std::setw(13) << obj.target_vt << '\n';
|
|
stream << "# target_vz = " << std::setw(13) << obj.target_vz << '\n';
|
|
}
|
|
private:
|
|
double pos_tol, vel_tol;
|
|
double target_d, target_z, target_vd, target_vt, target_vz;
|
|
};
|
|
|
|
class Uniform_sphere_escape_velocity_cutoff {
|
|
public:
|
|
Uniform_sphere_escape_velocity_cutoff(const double r_max, const double rho_0, const double b)
|
|
: r_max(r_max), rho_0(rho_0), b(b)
|
|
{
|
|
uniform_distribution = std::uniform_real_distribution<double>(0, 1);
|
|
}
|
|
template<typename Generator>
|
|
std::array<double,6> operator()(Generator& generator)
|
|
{
|
|
std::array<double,6> result;
|
|
|
|
double r = pow(uniform_distribution(generator), 1./3.)*r_max;
|
|
double cos_theta = uniform_distribution(generator)*2-1;
|
|
double sin_theta = sqrt(1 - cos_theta*cos_theta);
|
|
double phi = uniform_distribution(generator)*2*M_PI;
|
|
result[0] = r*cos(phi)*sin_theta;
|
|
result[1] = r*sin(phi)*sin_theta;
|
|
result[2] = r*cos_theta;
|
|
|
|
double potential = nfw_potential(r);
|
|
if (potential > 0) throw std::runtime_error("Cannot deal with a positive potential");
|
|
double v_max = sqrt(-2*potential);
|
|
double v_magnitude;
|
|
do {
|
|
for (int i=3; i<6; i++) result[i] = uniform_distribution(generator);
|
|
v_magnitude = sqrt(result[3]*result[3]+result[4]*result[4]+result[5]*result[5]);
|
|
} while (v_magnitude >= v_max);
|
|
|
|
return result;
|
|
}
|
|
friend std::ostream& operator<<(std::ostream& stream, const Uniform_sphere_escape_velocity_cutoff& obj)
|
|
{
|
|
stream.setf(std::ios::scientific);
|
|
stream << "# Initial conditions type: uniform sphere with escape velocity cutoff\n";
|
|
stream << "# Initial conditions parameters:\n";
|
|
stream << "# r_max = " << std::setw(13) << obj.r_max << '\n';
|
|
}
|
|
private:
|
|
inline double nfw_potential(double r)
|
|
{
|
|
return -4*M_PI*G*rho_0*b*b*b*log(1+r/b)/r;
|
|
}
|
|
double r_max, rho_0, b;
|
|
std::uniform_real_distribution<double> uniform_distribution;
|
|
};
|
|
|
|
class Box_initial_conditions {
|
|
public:
|
|
Box_initial_conditions(double x_max, double v_max)
|
|
: x_max(x_max), v_max(v_max)
|
|
{
|
|
position_distribution = std::uniform_real_distribution(-x_max, x_max);
|
|
velocity_distribution = std::uniform_real_distribution(-v_max, v_max);
|
|
}
|
|
template<typename Generator>
|
|
std::array<double,6> operator()(Generator& generator)
|
|
{
|
|
std::array<double,6> result;
|
|
for (int i=0; i<3; i++) result[i] = position_distribution(generator);
|
|
for (int i=3; i<6; i++) result[i] = velocity_distribution(generator);
|
|
return result;
|
|
}
|
|
friend std::ostream& operator<<(std::ostream& stream, const Box_initial_conditions& obj)
|
|
{
|
|
stream.setf(std::ios::scientific);
|
|
stream << "# Initial conditions type: uniform box\n";
|
|
stream << "# Initial conditions parameters:\n";
|
|
stream << "# x_max = " << std::setw(13) << obj.x_max << '\n';
|
|
stream << "# v_max = " << std::setw(13) << obj.v_max << '\n';
|
|
}
|
|
private:
|
|
double x_max, v_max;
|
|
std::uniform_real_distribution<> position_distribution, velocity_distribution;
|
|
};
|
|
|
|
|
|
template<typename Acceptor, typename Initial_conditions>
|
|
class Scattering_experiment {
|
|
public:
|
|
Scattering_experiment(const std::string file_name, const double t_max, const unsigned long long n_experiments, Initial_conditions& initial_conditions, Acceptor& accept)
|
|
: bunch_size(256), n_experiments(n_experiments), report_interval_seconds(300), t_max(t_max), accept(accept), initial_conditions(initial_conditions)
|
|
{
|
|
auto time = std::chrono::system_clock::now();
|
|
file = std::ofstream(file_name/*, std::ofstream::app*/);
|
|
file << "#############################################\n";
|
|
file << "# Cosmological replay scattering experiment #\n";
|
|
file << "#############################################\n";
|
|
file << "# Time: " << iso_time(time) << '\n';
|
|
file << "# Last Git commit: " << git_version() << '\n';
|
|
char hostname[256];
|
|
gethostname(hostname, 256);
|
|
file << "# Hostname: " << hostname << '\n';
|
|
const auto pid = getpid();
|
|
file << "# PID: " << pid << '\n';
|
|
const unsigned long time_count = time.time_since_epoch().count();
|
|
const unsigned long big_prime = 840580612873L;
|
|
unique_id = time_count + big_prime*pid; // TODO hash it
|
|
file << "# Unique identifier: " << unique_id << '\n';
|
|
n_threads = std::thread::hardware_concurrency();
|
|
file << "# Concurrency: " << n_threads << '\n';
|
|
file << initial_conditions;
|
|
file << accept;
|
|
file.flush();
|
|
file.setf(std::ios::scientific | std::ios::right);
|
|
file.precision(16);
|
|
// TODO information about galaxy model
|
|
}
|
|
~Scattering_experiment() {
|
|
file.close();
|
|
}
|
|
void thread_task(int tid)
|
|
{
|
|
auto thread_begin_time = std::chrono::system_clock::now();
|
|
auto report_interval = std::chrono::seconds(report_interval_seconds);
|
|
auto next_report_time = thread_begin_time + report_interval;
|
|
|
|
std::default_random_engine generator(unique_id + tid);
|
|
|
|
while (counter < n_experiments) {
|
|
for (int i=0; i<bunch_size; i++) {
|
|
auto ic = initial_conditions(generator);
|
|
double y[12];
|
|
|
|
integrate(ic.data(), t_max, t_max, y);
|
|
|
|
if (accept(y+6)) {
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
for (int j=0; j<12; j++) file << std::setw(24) << y[j];
|
|
file << std::endl;
|
|
}
|
|
}
|
|
counter += bunch_size;
|
|
if (tid == 0) {
|
|
auto now = std::chrono::system_clock::now();
|
|
if (now >= next_report_time) {
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
file << "# " << iso_time(now) << " counter = " << counter << std::endl;
|
|
next_report_time = now + report_interval;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
void launch()
|
|
{
|
|
counter = 0;
|
|
for (int tid=0; tid<n_threads; tid++) {
|
|
threads.push_back(std::thread(&Scattering_experiment::thread_task, this, tid));
|
|
}
|
|
for (auto& t : threads) {
|
|
t.join();
|
|
}
|
|
std::cout << counter << '\n';
|
|
}
|
|
std::ofstream file;
|
|
std::mutex mtx;
|
|
int n_threads;
|
|
std::vector<std::thread> threads;
|
|
std::atomic<unsigned long long> counter;
|
|
unsigned long long n_experiments;
|
|
unsigned long unique_id;
|
|
int bunch_size;
|
|
int report_interval_seconds;
|
|
double t_max;
|
|
Acceptor accept;
|
|
Initial_conditions initial_conditions;
|
|
};
|
|
|
|
int main()
|
|
{
|
|
std::cout << "Hi\n";
|
|
|
|
const double t_max = 11.65551390; // Gyr
|
|
double3 target_pos = {-9.1817914423447837E+03, -2.3161972143758221E+03, 4.2321813890923086E+03}; // pc
|
|
double3 target_vel = { 1.6995805137819784E+02, 1.3476658021349482E+02, -1.3342192079702110E+02}; // km/s
|
|
// Unit adjustment
|
|
target_pos *= 0.001; // Now in kpc
|
|
target_vel *= 1.0226911647958985; // Now in kpc/Gyr
|
|
|
|
In_box_cyl accept(5, 50, target_pos, target_vel);
|
|
Uniform_sphere_escape_velocity_cutoff uniform_sphere_escape_velocity_cutoff(75, 1.82777387E+07, 9.86242602E+00);
|
|
|
|
Scattering_experiment scattering_experiment("results.dat", t_max, 1000000000L, uniform_sphere_escape_velocity_cutoff, accept);
|
|
scattering_experiment.launch();
|
|
|
|
std::cout << "Bye\n";
|
|
|
|
return 0;
|
|
} |