File size: 4,091 Bytes
be3cca2 | 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 | #ifndef GAIA_REST2_PARALLEL_HPP
#define GAIA_REST2_PARALLEL_HPP
#include <vector>
#include <random>
#include <cmath>
#include <iostream>
#ifdef _OPENMP
#include <omp.h>
#endif
namespace gaia {
namespace rest2 {
struct Vec3 {
double x, y, z;
Vec3(double x=0, double y=0, double z=0) : x(x), y(y), z(z) {}
};
class Replica {
public:
Replica(double temp, int seed) : temp(temp), seed(seed) {
kT = 0.001987204258 * temp;
energy = 0.0;
positions.resize(100);
std::mt19937 gen(seed);
std::uniform_real_distribution<double> dist(-1.0, 1.0);
for (auto& p : positions) {
p = Vec3(dist(gen), dist(gen), dist(gen));
}
}
void step(int n_steps) {
std::mt19937 gen(seed + 1);
std::uniform_real_distribution<double> dist(-0.01, 0.01);
for (int s = 0; s < n_steps; s++) {
for (auto& p : positions) {
p.x += dist(gen);
p.y += dist(gen);
p.z += dist(gen);
}
}
energy = 0.0;
for (const auto& p : positions) {
energy += p.x*p.x + p.y*p.y + p.z*p.z;
}
energy *= 0.5 * kT;
}
double get_energy() const { return energy; }
double get_kT() const { return kT; }
double get_temperature() const { return temp; }
void swap(Replica& other) {
std::swap(positions, other.positions);
std::swap(energy, other.energy);
std::swap(temp, other.temp);
std::swap(kT, other.kT);
}
private:
double temp;
double kT;
int seed;
double energy;
std::vector<Vec3> positions;
};
class ParallelREST2 {
public:
ParallelREST2(int n_replicas, double T_min, double T_max, int seed)
: n_replicas(n_replicas), T_min(T_min), T_max(T_max) {
replicas.reserve(n_replicas);
for (int i = 0; i < n_replicas; i++) {
double temp = T_min + (T_max - T_min) * i / (n_replicas - 1);
replicas.emplace_back(temp, seed + i);
}
}
void run(int n_steps, int exchange_interval = 10) {
int exchange_count = 0;
int accepted_count = 0;
for (int step = 0; step < n_steps; step++) {
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int i = 0; i < n_replicas; i++) {
replicas[i].step(1);
}
if (step % exchange_interval == 0 && step > 0) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, n_replicas - 2);
int i = dist(gen);
int j = i + 1;
double beta_i = 1.0 / replicas[i].get_kT();
double beta_j = 1.0 / replicas[j].get_kT();
double delta = (beta_i - beta_j) *
(replicas[j].get_energy() - replicas[i].get_energy());
if (delta < 0 || std::exp(-delta) > uniform_random()) {
replicas[i].swap(replicas[j]);
accepted_count++;
}
exchange_count++;
}
}
double acceptance = (double)accepted_count / exchange_count;
std::cout << "REST2 Exchange acceptance: " << acceptance * 100 << "%" << std::endl;
}
void print_temperatures() {
std::cout << "Replica temperatures:" << std::endl;
for (int i = 0; i < n_replicas; i++) {
std::cout << " " << i << ": " << replicas[i].get_temperature() << " K" << std::endl;
}
}
private:
double uniform_random() {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_real_distribution<double> dist(0.0, 1.0);
return dist(gen);
}
int n_replicas;
double T_min, T_max;
std::vector<Replica> replicas;
};
} // namespace rest2
} // namespace gaia
#endif
|