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
|
#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <fstream>
#include <chrono>
// 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 double stepsize_half{ stepsize / 2.0 };
const int randomseed{ 1 };
struct coordinate {
double x{ 0 };
double y{ 0 };
};
static double compute_sq_distances(coordinate pos1, coordinate pos2,
double& dx, double& dy) {
// box volume = [-L,L]^n
const 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;
}
static double force_term(double& dist2) {
double pref{ -kappa / (2 * sigma_2) };
double expo;
expo = exp(-dist2 / (2 * sigma_2));
return pref * expo;
}
static void compute_force(std::vector<coordinate>& forces, const std::vector<coordinate>& positions) {
double dx, dy;
double dist2;
int n_part = forces.size();
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], dx, dy);
force = force_term(dist2);
forces[i].x += force * dx;
forces[i].y += force * dy;
forces[j].x += -force * dx;
forces[j].y += -force * dy;
}
}
return;
}
static void B_step(const std::vector <coordinate>& positions, std::vector <coordinate>& velocities, std::vector <coordinate>& forces) {
const int n_part = positions.size();
// Update velocities.
for (int i = 0; i < n_part; ++i) {
velocities[i].x += stepsize_half * forces[i].x;
velocities[i].y += stepsize_half * forces[i].y;
}
return;
}
static void A_step(std::vector <coordinate>& positions, const std::vector <coordinate>& velocities) {
int n_part = positions.size();
for (int i = 0; i < n_part; ++i) {
positions[i].x += stepsize_half * velocities[i].x;
positions[i].y += stepsize_half * velocities[i].y;
}
return;
}
static void O_step(std::vector <coordinate>& velocities, std::mt19937& RNG) {
std::normal_distribution<> normal{ 0,1 };
const int n_part = velocities.size();
const double a = exp(-gamma * stepsize);
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;
}
static void apply_periodic_boundaries(std::vector <coordinate>& positions) {
const int n_part = positions.size();
const double two_L = 2 * L;
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[]) {
// 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);
// 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);
A_step(positions, velocities);
apply_periodic_boundaries(positions);
O_step(velocities, twister);
A_step(positions, velocities);
apply_periodic_boundaries(positions);
compute_force(forces, positions);
B_step(positions, velocities, forces);
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;
}
| |