-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvVerlet_Bond.cpp
587 lines (397 loc) · 14.1 KB
/
vVerlet_Bond.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//
// 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 1000000
using namespace std;
// Set initial functions for calculating energies + forces
const int n_atoms = 2;
// Simulation temperature
const double temperature = 300; // kelvin
const double k_boltz = 1.987206504191549E-003; // kcal mol-1 K-1
double box_size[3] = {2.0, 2.0, 2.0};
const double r_eq = 1.54; //Angstroms
// Subroutine to apply periodic boundaries
double make_periodic(double x, const double box)
{
while (x < -0.5*box)
{
x = x + box;
}
while (x > 0.5*box)
{
x = x - box;
}
return x;
}
// Subroutine to wrap the coordinates into a box
double wrap_into_box(double x, double box)
{
while (x > box)
{
x = x - box;
}
while (x < 0)
{
x = x + box;
}
return x;
}
// 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, "output%d.pdb", step);
snprintf(filename, 64, "harmonic_%00008d.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]);
for (int i = 0; i < n_atoms; i = i + 1)
{
// coords[i][0] = wrap_into_box(coords[i][0], box_size[0]);
// coords[i][1] = wrap_into_box(coords[i][1], box_size[1]);
// coords[i][2] = wrap_into_box(coords[i][2], box_size[2]);
fprintf(f, "ATOM %5d C C 1 %8.3f%8.3f%8.3f 1.00 0.00 C\n",
i+1, coords[i][0], coords[i][1], coords[i][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)
{
auto **vels = new double *[n_atoms];
default_random_engine gen_random;
normal_distribution<double> P_v(0,Kb*T);
for (int i = 0; i < n_atoms; i++)
{
vels[i] = new double[3];
vels[i][0] = P_v(gen_random);
vels[i][1] = P_v(gen_random);
vels[i][2] = 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;
}
// Calculate r_ij for a pair of atomic coordinates
const double get_radial(double **coords, const double *box_size, int i, int j) //Will be useful only in LJ potential example
{
double delta_x = coords[j][0] - coords[i][0];
double delta_y = coords[j][1] - coords[i][1];
double delta_z = coords[j][2] - coords[i][2];
// Apply periodic boundaries
delta_x = make_periodic(delta_x, box_size[0]);
delta_y = make_periodic(delta_y, box_size[1]);
delta_z = make_periodic(delta_z, box_size[2]);
const double r_ij = (delta_x * delta_x) + (delta_y * delta_y) +
(delta_z * delta_z);
return r_ij;
}
double **calc_3Dforce(double **pos, const double k)
{
auto **f = new double *[n_atoms];
for (int i = 0; i < n_atoms; i++) {
f[i] = new double[3];
f[i][0] = -k * pos[i][0];
f[i][1] = -k * pos[i][1];
f[i][2] = -k * pos[i][2];
}
for (int i = 0; i < n_atoms-1; i = i + 1)
{
for (int j = i+1; j < n_atoms; j = j + 1)
{
double delta_x = pos[i][0] - pos[j][0];
double delta_y = pos[i][1] - pos[j][1];
double delta_z = pos[i][2] - pos[j][2];
// Apply periodic boundaries
delta_x = make_periodic(delta_x, box_size[0]);
delta_y = make_periodic(delta_y, box_size[1]);
delta_z = make_periodic(delta_z, box_size[2]);
const double r2 = (delta_x*delta_x) + (delta_y*delta_y) +
(delta_z*delta_z);
// F_LJ = 48*epsilon*1/r2[ (sigma/r)^12 - 0.5*(sigma/r)^6 ]
const double sig2_over_r2 = 1.0 / r2;
const double sig6_over_r6 = sig2_over_r2*sig2_over_r2*sig2_over_r2;
//const double sig12_over_r12 = sig6_over_r6 * sig6_over_r6;
const double f_x = 48.0 * sig2_over_r2 * sig6_over_r6 * ( sig6_over_r6 - 0.5 );
cout << "force = " << f_x << "\t" << "radial comp = " << r2
<< "\t" << delta_x << "\t" << delta_y << "\t" << delta_z << endl;
f[i][0] = f[i][0] + (f_x * delta_x);
f[i][1] = f[i][1] + (f_x * delta_y);
f[i][2] = f[i][2] + (f_x * delta_z); //update force components for atom i
f[j][0] = f[j][0] - (f_x * delta_x);
f[j][1] = f[j][1] - (f_x * delta_y);
f[j][2] = f[j][2] - (f_x * delta_z); //update force components for atom j
}
}
return f;
}
double **calc_bondforce(double **pos, const double kr, const double r_eq)
{
auto **f = new double *[n_atoms];
for (int t = 0; t < n_atoms; t++){
f[t] = new double[3];
f[t][0] = 0.0;
f[t][1] = 0.0;
f[t][2] = 0.0;
}
for (int i = 0; i < n_atoms-1; i++) {
for (int j = i+1; j < n_atoms; j++){
double delta_x = pos[i][0] - pos[j][0];
double delta_y = pos[i][1] - pos[j][1];
double delta_z = pos[i][2] - pos[j][2];
// Apply periodic boundaries
delta_x = make_periodic(delta_x, box_size[0]);
delta_y = make_periodic(delta_y, box_size[1]);
delta_z = make_periodic(delta_z, box_size[2]);
const double r = sqrt((delta_x*delta_x) + (delta_y*delta_y) +
(delta_z*delta_z));
const double fscalar = -2.0 * kr * (1 - (r_eq / r));
// cout << "force = " << fscalar << "\t" << "radial comp = " << r
// << "\t" << delta_x << "\t" << delta_y << "\t" << delta_z << endl;
f[i][0] = f[i][0] + (fscalar * delta_x);
f[i][1] = f[i][1] + (fscalar * delta_y);
f[i][2] = f[i][2] + (fscalar * delta_z); //update force components for atom i
f[j][0] = f[j][0] - (fscalar * delta_x);
f[j][1] = f[j][1] - (fscalar * delta_y);
f[j][2] = f[j][2] - (fscalar * delta_z); //update force components for atom j
}
}
return f;
}
double calc_potential(double **pos, const double k)
{
double V = 0.0;
for (int i = 0; i < n_atoms; i++)
{
V += 0.5*k*pow(pos[i][0], 2) + 0.5*k*pow(pos[i][1], 2) + 0.5*k*pow(pos[i][2], 2);
}
return V;
}
double calc_bondpotential(double **pos, const double kr, const double r_eq){
double V = 0.0;
for (int i = 0; i < n_atoms-1; i++) {
for (int j = i + 1; j < n_atoms; j++) {
double delta_x = pos[i][0] - pos[j][0];
double delta_y = pos[i][1] - pos[j][1];
double delta_z = pos[i][2] - pos[j][2];
// Apply periodic boundaries
delta_x = make_periodic(delta_x, box_size[0]);
delta_y = make_periodic(delta_y, box_size[1]);
delta_z = make_periodic(delta_z, box_size[2]);
const double r = sqrt((delta_x * delta_x) + (delta_y * delta_y) +
(delta_z * delta_z));
const double E_harmonic = kr * ((r - r_eq) * (r - r_eq));
V += E_harmonic;
}
}
return V;
}
// Subroutine that calculates the potential energies of the atoms
double calc_LJpot(double **coords, const double *box_size)
{
// Loop over all pairs of atoms and calculate
// the LJ energy
double pot_energy = 0;
for (int i = 0; i < n_atoms-1; i = i + 1)
{
for (int j = i+1; j < n_atoms; j = j + 1)
{
double r2 = get_radial(coords, box_size, i, j);
// E_LJ = 4*epsilon[ (sigma/r)^12 - (sigma/r)^6 ]
const double sig2_over_r2 = 1 / r2;
const double sig6_over_r6 = sig2_over_r2*sig2_over_r2*sig2_over_r2;
const double sig12_over_r12 = sig6_over_r6 * sig6_over_r6;
const double e_lj = 4.0 * ( sig12_over_r12 - sig6_over_r6 );
pot_energy = pot_energy + e_lj;
}
}
// return the total energy of the atoms
return pot_energy;
}
double calc_kinetic(double **pos, const int mass)
{
double K = 0.0;
for (int i = 0; i < n_atoms; i++)
{
K += 0.5*mass*pow(pos[i][0], 2) + 0.5*mass*pow(pos[i][1], 2) + 0.5*mass*pow(pos[i][2], 2);
}
return K;
}
//Set functions for Velocity Verlet update of positions and velocities
void position_3Dupdate(double **pos, double **vel, double **F, const int mass, const float dt, const double stepfraction=1.0)
{
for (int i = 0; i < n_atoms; i++)
{
pos[i][0] = pos[i][0] + vel[i][0]*dt*stepfraction + (0.5*dt*dt/mass)*F[i][0];
pos[i][1] = pos[i][1] + vel[i][1]*dt*stepfraction + (0.5*dt*dt/mass)*F[i][0];
pos[i][2] = pos[i][2] + vel[i][2]*dt*stepfraction + (0.5*dt*dt/mass)*F[i][0];
}
}
void velocity_3Dupdate(double **vel, double **F, const int mass, const float dt, const double stepfraction=1.0)
{
for (int i=0; i < n_atoms; i++)
{
vel[i][0] = vel[i][0] + (0.5*dt*stepfraction/mass)*F[i][0];
vel[i][1] = vel[i][1] + (0.5*dt*stepfraction/mass)*F[i][1];
vel[i][2] = vel[i][2] + (0.5*dt*stepfraction/mass)*F[i][2];
}
}
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 double k=10.0)
{
const int length = MDSteps;
auto **x_trj = new double*[n_atoms];
auto **v_trj = new double*[n_atoms];
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) + calc_LJpot(x, box_size);
double U = calc_bondpotential(x, k, r_eq);
Epot[step] = U;
double K = calc_kinetic(v, mass);
Ekin[step] = K;
double Tot = U + K;
Etot[step] = Tot;
//F = calc_3Dforce(x, k);
F = calc_bondforce(x, k, r_eq);
velocity_3Dupdate(v, F, mass, dt, 0.5);
position_3Dupdate(x, v, F, mass, dt, 1.0);
//F = calc_3Dforce(x, k);
F = calc_bondforce(x, k, r_eq);
velocity_3Dupdate(v, F, mass, dt, 0.5);
if (step % 1000 == 0)
{
print_pdb(x, n_atoms, step);
for (int i = 0; i < n_atoms; i++)
{
cout << x[i][0] << "\t" << x[i][1] << "\t" << x[i][2] << endl;
}
cout << "\n" ;
for (int i = 0; i < n_atoms; i++)
{
cout << v[i][0] << "\t" << v[i][1] << "\t" << v[i][2] << endl;
}
cout << "-----------------------------------------------" << endl;
}
for (int i = 0; i < n_atoms; i++){
x_trj[i] = new double[3];
v_trj[i] = new double[3];
x_trj[i] = x[i];
v_trj[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
const double fconstant = 40.0;
double **v_0 = assign_velocities(temperature, k_boltz);
double **x_0 = new double *[n_atoms];
for (int i = 0; i < n_atoms; i++)
{
x_0[i] = new double[3];
x_0[i][0] = rand(0 , box_size[0]);
x_0[i][1] = rand(0 , box_size[1]);
x_0[i][2] = rand(0 , box_size[2]);
}
//double **f_0 = calc_3Dforce(x_0, fconstant);
double **f_0 = calc_bondforce(x_0, fconstant, r_eq);
const double kT = temperature * k_boltz;
int numsteps = MDSteps;
cout << "Initial coordinates and velocities:-" << endl;
for (int i = 0; i < n_atoms; i++)
{
cout << x_0[i][0] << "\t" << x_0[i][1] << "\t" << x_0[i][2] << endl;
}
cout << "-----------------------------------------------------------------------" << endl;
for (int i = 0; i < n_atoms; i++)
{
cout << v_0[i][0] << "\t" << v_0[i][1] << "\t" << v_0[i][2] << endl;
}
MDarray T;
T = VelocityVerletIntegrator(x_0, v_0, f_0, 0.001, 1, fconstant);
//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++)
{
if (t % 1000 == 0)
{
cout << "Step " << t << "/" << numsteps << " - " << numsteps-t << " steps remaining. " << endl;
// for (int i = 0; i < n_atoms; i++)
// {
// cout << T.q_traj[i][0] << "\t" << T.q_traj[i][1] << "\t" << T.q_traj[i][2] << endl;
//
// }
//
// cout << "\n" << endl;
//
// for (int i = 0; i < n_atoms; i++)
// {
// cout << T.p_traj[i][0] << "\t" << T.p_traj[i][1] << "\t" << T.p_traj[i][2] << endl;
//
// }
cout << " " << endl;
cout << "Potential = " << T.e_pot[t] << "\t" << ", Kinetic = " << T.e_kin[t] << endl;
cout << "Total Energy = " << T.e_tot[t] << endl;
cout << " " << endl;
}
}
return 0;
}