1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <fstream>
#include <chrono>
struct coordinate{
double x{0};
double y{0};
};
double compute_sq_distances(const coordinate& pos1, const coordinate& pos2, const double& L,
double& dx, double& dy){
// box volume = [-L,L]^n
static double two_L = 2*L;
dx = pos1.x - pos2.x;
dx = dx > L ? dx-two_L : (dx < -L ? dx+two_L : dx);
dy = pos1.y - pos2.y;
dy = dy > L ? dy-two_L : (dy < -L ? dy+two_L : dy);
return dx*dx + dy*dy;
}
double force_term(double& dist2, const double& kappa, const double& sigma_2){
static double pref {-kappa/(2*sigma_2)};
static double expo;
expo = exp(-dist2/(2*sigma_2));
return pref*expo;
}
void compute_force(std:: vector<coordinate>& forces, const std:: vector<coordinate>& positions,
const double& L, const double& kappa, const double& sigma_2){
static double dx, dy;
static double dist2;
static int n_part = forces.size();
static double force;
for (int i=0; i<n_part; ++i){
forces[i].x = forces[i].y = 0;
}
for (int i=0; i<n_part; ++i){
for(int j=i+1; j<n_part; ++j){
dist2 = compute_sq_distances(positions[i], positions[j], L, dx, dy);
force = force_term(dist2, kappa, sigma_2);
forces[i].x += force*dx;
forces[i].y += force*dy;
forces[j].x += -force*dx;
forces[j].y += -force*dy;
}
}
return;
}
void B_step(const std:: vector <coordinate>& positions, std:: vector <coordinate>& velocities, std:: vector <coordinate>& forces,
const double& stepsize, const double& L, const double& kappa, const double& sigma_2){
static const int n_part = positions.size();
// Update velocities.
for (int i=0; i<n_part; ++i){
velocities[i].x += stepsize*forces[i].x;
velocities[i].y += stepsize*forces[i].y;
}
return;
}
void A_step(std:: vector <coordinate>& positions, const std:: vector <coordinate>& velocities, const double& stepsize, const double& L){
static int n_part = positions.size();
for (int i=0; i<n_part; ++i){
positions[i].x += stepsize*velocities[i].x;
positions[i].y += stepsize*velocities[i].y;
}
return;
}
void O_step(std:: vector <coordinate>& velocities, const double& stepsize,
const double& gamma, const double& beta, std:: mt19937& RNG){
static std:: normal_distribution<> normal{0,1};
static const int n_part = velocities.size();
static const double a = exp(-gamma*stepsize);
static const double pref = sqrt(1/beta *(1-a*a));
for (int i=0; i<n_part; ++i){
velocities[i].x = a*velocities[i].x + pref*normal(RNG);
velocities[i].y = a*velocities[i].y + pref*normal(RNG);
}
return;
}
void apply_periodic_boundaries(std:: vector <coordinate>& positions, const double& L){
static const int n_part = positions.size();
static const double two_L = 2*L;
static double x, y;
for (int i=0; i<n_part; ++i){
x = positions[i].x;
positions[i].x = x>L ? x-two_L : (x<-L ? x + two_L : x);
y = positions[i].y;
positions[i].y = y>L ? y-two_L : (y<-L ? y + two_L : y);
}
return;
}
int main(int argc, char* argv[]){
// Hyperparameters.
const double L {10};
const int N_iter {20000};
const int n_meas {10};
const int n_part {1000};
const double beta {300};
const double gamma {0.05};
const double kappa {1./n_part};
const double sigma_2 {1};
const double stepsize {0.1};
const int randomseed {1};
// Prepare simulation.
// Prepare RNG.
std:: mt19937 twister;
std:: seed_seq seq{1,20,3200,403,5*randomseed+1,12000,73667,9474+randomseed,19151-randomseed};
std:: vector < std::uint32_t > seeds(1);
seq.generate(seeds.begin(), seeds.end());
twister.seed(seeds.at(0));
// Set positions.
std:: uniform_real_distribution<double> box_uniform(-L, L);
std:: vector <coordinate> positions(n_part);
for (int i=0; i<n_part; ++i){
positions[i].x = box_uniform(twister);
positions[i].y = box_uniform(twister);
}
// Set forces.
std:: vector <coordinate> forces(n_part);
compute_force(forces, positions, L, kappa, sigma_2);
// Set velocities.
std:: vector <coordinate> velocities(n_part);
// Main loop.
const double stepsize_half = 0.5 * stepsize;
std:: normal_distribution<> normal{0,1};
auto t1 = std:: chrono::high_resolution_clock::now();
for (int i=0; i<=N_iter; ++i){
B_step(positions, velocities, forces, stepsize_half, L, kappa, sigma_2);
A_step(positions, velocities, stepsize_half, L);
apply_periodic_boundaries(positions, L);
O_step(velocities, stepsize, gamma, beta, twister);
A_step(positions, velocities, stepsize_half, L);
apply_periodic_boundaries(positions, L);
compute_force(forces, positions, L, kappa, sigma_2);
B_step(positions, velocities, forces, stepsize_half, L, kappa, sigma_2);
if (i%1000==0) std::cout << "Iteration "<<i<< " done!\n";
}
auto t2 = std:: chrono:: high_resolution_clock:: now();
auto ms_int = std:: chrono:: duration_cast < std:: chrono:: seconds > (t2 - t1);
std:: cout << "Execution took " << ms_int.count() << " seconds!\n";
return 0;
}
| |