-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
164 lines (137 loc) · 4.38 KB
/
functions.cpp
File metadata and controls
164 lines (137 loc) · 4.38 KB
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
#include "particle.h"
#include <vector>
#include <cmath>
#include <random>
#include<fstream>
void init_positions(std::vector<Particle>& p, double L){
int N = p.size();
int k = (int)std::ceil(std::sqrt((double)N));
double delta = L / k;
for (int i = 0; i < N; ++i) {
p[i].x = (i % k + 0.5) * delta;
p[i].y = (i / k + 0.5) * delta;
}
}
void init_velocities(std::vector<Particle>& p, double T){
static std::mt19937 gen(12345);
std::normal_distribution<double> normal(0.0, 1.0);
int N = p.size();
double sigma = std::sqrt(T);
for (int i = 0; i < N; i++){
p[i].vx = sigma * normal(gen);
p[i].vy = sigma * normal(gen);
}
double vxcm = 0.0, vycm = 0.0;
for (int i = 0; i < N; i++){
vxcm += p[i].vx;
vycm += p[i].vy;
}
vxcm /= N;
vycm /= N;
for (int i = 0; i < N; i++){
p[i].vx -= vxcm;
p[i].vy -= vycm;
}
}
double compute_forces(std::vector<Particle>& p, double L, double rcut){
int N = p.size();
double Epot = 0.0;
double rcut2 = rcut * rcut;
for (auto& pi : p){
pi.fx = pi.fy = 0.0;
}
double inv_rcut = 1.0 / rcut;
double inv_rcut6 = std::pow(inv_rcut, 6);
double inv_rcut12 = inv_rcut6 * inv_rcut6;
double Ucut = 4.0 * (inv_rcut12 - inv_rcut6);
double dUcut = -48.0 * inv_rcut12 * inv_rcut + 24.0 * inv_rcut6 * inv_rcut;
for (int i = 0; i < N; i++){
for (int j = i + 1; j < N; j++){
double dx = p[i].x - p[j].x;
double dy = p[i].y - p[j].y;
if (dx > L/2) dx -= L;
if (dx < -L/2) dx += L;
if (dy > L/2) dy -= L;
if (dy < -L/2) dy += L;
double r2 = dx*dx + dy*dy;
if (r2 < rcut2 && r2 > 0.0){
double r = std::sqrt(r2);
double inv_r = 1.0 / r;
double inv_r6 = std::pow(inv_r, 6);
double inv_r12 = inv_r6 * inv_r6;
double U = 4.0 * (inv_r12 - inv_r6);
double dU = -48.0 * inv_r12 * inv_r + 24.0 * inv_r6 * inv_r;
double Ushift = U - Ucut - dUcut * (r - rcut);
double F = -(dU - dUcut) / r;
p[i].fx += F * dx;
p[i].fy += F * dy;
p[j].fx -= F * dx;
p[j].fy -= F * dy;
Epot += Ushift;
}
}
}
for (auto& pi : p){
pi.ax = pi.fx / pi.mass;
pi.ay = pi.fy / pi.mass;
}
return Epot;
}
double kinetic_energy(const std::vector<Particle>& p){
double Ek = 0.0;
for (const auto& pi : p){
Ek += 0.5 * (pi.vx*pi.vx + pi.vy*pi.vy);
}
return Ek;
}
void time_it(std::vector<Particle>& p, double dt, double L, double rcut, double Q, double T_target){
static double xi = 0.0;
int N = p.size();
int nd = 2 * N;
for (auto& pi : p) {
pi.vxp = pi.vx + 0.5 * dt * (pi.ax - xi * pi.vx);
pi.vyp = pi.vy + 0.5 * dt * (pi.ay - xi * pi.vy);
}
for (auto& pi : p) {
pi.x += pi.vxp * dt;
pi.y += pi.vyp * dt;
if (pi.x < 0) pi.x += L;
if (pi.x >= L) pi.x -= L;
if (pi.y < 0) pi.y += L;
if (pi.y >= L) pi.y -= L;
}
compute_forces(p, L, rcut);
for (int k = 0; k < 5; k++){
double Ek = kinetic_energy(p);
xi += dt * (2.0 * Ek - nd * T_target) / Q;
for (auto& pi : p) {
pi.vx = (pi.vxp + 0.5 * dt * pi.ax) / (1.0 + 0.5 * dt * xi);
pi.vy = (pi.vyp + 0.5 * dt * pi.ay) / (1.0 + 0.5 * dt * xi);
}
}
}
void velocity_distribution(std::vector<double>& vhist,const std::vector<Particle>& particles, double dv){
int nv = vhist.size();
for (const auto& p : particles){
double v = std::sqrt(p.vx * p.vx + p.vy * p.vy);
int k = static_cast<int>(std::floor(v / dv));
if (k >= 0 && k < nv) {
vhist[k] += 1.0;
}
}
}
void to_file(std::string filename, std::vector<Particle>& particles){
std::ofstream out(filename);
for (size_t i = 0; i < particles.size(); i++){
out << i << " "
<< particles[i].x << " "
<< particles[i].y << " "
<< particles[i].vx << " "
<< particles[i].vy << " "
<< particles[i].ax << " "
<< particles[i].ay << " "
<< particles[i].fx << " "
<< particles[i].fy << "\n";
}
out.close();
}