-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvVerlet_3D.cpp
307 lines (210 loc) · 6.08 KB
/
vVerlet_3D.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// Created by Khaled Maksoud on 2019-04-08.
// Example for a Velocity Verlet integration scheme on a single 3D harmonic oscillator
#include <cmath>
#include <vector>
#include <iostream>
#include <random>
#define MDSteps 5000
using namespace std;
// Set initial functions for calculating energies + forces
// Simulation temperature
const double temperature = 300; // kelvin
const double k_boltz = 1.987206504191549E-003; // kcal mol-1 K-1
double box_size[3] = {30.0, 30.0, 30.0};
// Subroutine to print a PDB of the coordinates
void print_pdb(double *coords, const int n_atoms, const int step)
{
char filename[64];
snprintf(filename, 64, "harmonic_%00000d.pdb", step);
FILE *f = fopen(filename, "w");
fprintf(f, "CRYST1 %8.3f %8.3f %8.3f 90.00 90.00 90.00\n", box_size[0], box_size[1], box_size[2]);
fprintf(f, "ATOM %5d Kr Kr 1 %8.3f%8.3f%8.3f 1.00 0.00 Kr\n",
1, coords[0], coords[1], coords[2]);
fprintf(f, "TER\n");
fclose(f);
}
// Function to assign a random set of velocities drawn from the kinetic energy distribution
double *assign_velocities(const double T, const double Kb)
{
static double vels[3];
default_random_engine gen_random;
normal_distribution<double> P_v(0,Kb*T);
for (int i = 0; i < 3; i++)
{
vels[i] = P_v(gen_random);
}
return vels;
}
// function to return a random number between 'start' to 'end'
double rand(const double start, const double end)
{
return (end-start) * (double(rand()) / RAND_MAX) + start;
}
//double get_radial(double *pos, double *vel, bool choose) //Will be useful only in LJ potential example
//{
// double r_x = 0.0;
// double r_v = 0.0;
//
// for (int i = 0; i < 3; i++){
// r_x += pow(pos[i], 2);
// r_v += pow(vel[i], 2);
// }
//
// r_x = sqrt(r_x);
// r_v = sqrt(r_v);
//
// if (choose)
// {
// return r_x;
// }
// else if (choose == false){
// return r_v;
// }
//}
double * calc_3Dforce(double *pos, const int k)
{
static double f[3];
f[0] = -k*pos[0];
f[1] = -k*pos[1];
f[2] = -k*pos[2];
return f;
}
double calc_potential(double *pos, const int k)
{
double V = 0.0;
for (int i = 0; i < 3; i++){
V += 0.5*k*pow(pos[i], 2);
}
return V;
}
double calc_kinetic(double *pos, const int mass)
{
double K = 0.0;
for (int i = 0; i < 3; i++){
K += 0.5*mass*pow(pos[i], 2);
}
return K;
}
//Set functions for Velocity Verlet update of positions and velocities
double * position_3Dupdate(double *pos, double *vel, const float dt, const double stepfraction=1.0)
{
static double q[3];
for (int i = 0; i < 3; i++){
q[i] = pos[i] + vel[i]*dt*stepfraction;
}
return q;
}
double * velocity_3Dupdate(double *vel, double *F, const int mass, const float dt, const double stepfraction=1.0)
{
static double p[3];
for (int i=0; i < 3; i++){
p[i] = vel[i] + (0.5*dt*stepfraction/mass)*F[i];
}
return p;
}
struct MDarray{
double ** q_traj;
double ** p_traj;
double * e_pot;
double * e_kin;
double * e_tot;
};
MDarray VelocityVerletIntegrator(double *x, double *v, double *F, const float dt=0.1, const int mass=1, const int k=10)
{
const int length = MDSteps;
auto **x_trj = new double*[length];
auto **v_trj = new double*[length];
static double Epot[length];
static double Ekin[length];
static double Etot[length];
for (int step = 0; step <= length; step++)
{
double U = calc_potential(x, k);
Epot[step] = U;
double K = calc_kinetic(v, mass);
Ekin[step] = K;
double Tot = U + K;
Etot[step] = Tot;
F = calc_3Dforce(x, k);
v = velocity_3Dupdate(v, F, mass, dt, 0.5);
x = position_3Dupdate(x, v, dt, 1.0);
if (step % 100 == 0){
print_pdb(x, 3, step);
}
F = calc_3Dforce(x, k);
v = velocity_3Dupdate(v, F, mass, dt, 0.5);
x_trj[step] = new double[3];
v_trj[step] = new double[3];
for (int i = 0; i < 3; i++){
x_trj[step][i] = x[i];
v_trj[step][i] = v[i];
}
}
MDarray Traj;
Traj.q_traj = x_trj;
Traj.p_traj = v_trj;
Traj.e_pot = Epot;
Traj.e_kin = Ekin;
Traj.e_tot = Etot;
return Traj;
}
int main(int argc, const char **argv)
{
// The total number of accepted moves
int naccept = 0;
// The total number of rejected moves
int nreject = 0;
//Set initial velocity and position of particle
double *v_0 = assign_velocities(temperature, k_boltz);
double x_0[3] = {rand(0,2), rand(0,2), rand(0,2)};
double f_0[3] = {0.0, 0.0, 0.0};
const double kT = temperature * k_boltz;
int numsteps = MDSteps;
MDarray T;
T = VelocityVerletIntegrator(x_0, v_0, f_0, 0.1, 1, 5);
//Metropolis-Hastings acceptance test for Hamiltonian
const double H_inital = T.e_tot[0];
const double H_final = T.e_tot[-1];
bool accept = false;
if (H_final <= H_inital)
{
accept = true;
}
else
{
double q = exp( H_final - H_inital / kT);
if (q >= rand(0.0, 1.0))
{
accept = true;
}
else
{
accept = false;
}
}
if (accept)
{
//Accept the MD trajectory
naccept += 1;
//double *v_next = T.q_traj[-1];
}
else
{
//Reject the MD trajectory and reset the coordinates
nreject += 1;
//double *v_next = T.q_traj[0];
}
for (int t = 0; t <= numsteps; t++)
{
cout << "Step " << t << "/" << numsteps << " - " << numsteps-t << " steps remaining. " << endl;
for (int i = 0; i < 3; i++)
{
cout << T.q_traj[t][i] << "\t" << T.p_traj[t][i] << endl;
}
cout << " " << endl;
cout << "Potential = " << T.e_pot[t] << "\t" << ", Kinetic = " << T.e_kin[t] << endl;
cout << "Total Energy = " << T.e_tot[t] << endl;
}
return 0;
}