-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargon.cpp
515 lines (447 loc) · 13 KB
/
argon.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
//DATA OUTPUT
//thermdata.dat: 1.Etot - 2.Ekin - 3.Epot
//data.dat: 1.Etot - 2.Ekin - 3.Epot - 4.diffusion - 5.temp - 6.pressure
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <fstream>
#include <stdlib.h>
#include <ctime>
#include <cstring>
#include <sys/stat.h>
#define n 8 //unit cells per direction
#define rc2 9 //Cutoff length squared
#define drv2 5 //Extra length for nn list
#define rdfdr 0.001 //interval for radial distribution function
#define rdfcutoff 5 //cutoff radius for radial distribution function
#define dt 0.0005 //timestep
#define iterations 250000 //number of iterations
#define thermiter 25000 //number of thermalization iterations
#define rescaleiter 200 //number of iterations between temperature rescaling
#define rdfiter 1000 //number of iterations between rdf storage
#define loopsperdatastore 50 //loops before data is stored
#define loopsperthermdatastore 50 //loops before thermalization data is stored
#define loopsperimage 10000 //loops before another printed image is made
#define listinterval 50 //Update verlet list every listinterval loops
#define notices 5
#define N 4*n*n*n //number of particles
#define pi 3.141592653589793
#define ecut 4*(1/pow(rc2,6) - 1/pow(rc2,3))
#define rv2 rc2 + drv2 //Verlet list cutoff distance
double T; //Temperature
double rho; //Density
double L; //Box length
double pos[N][3]={}; //positions
double posinit[N][3]={}; //Positions right after thermalization (For diffusion)
double vel[N][3]={}; //velocities
double force[N][3]={}; //forces
double epot=0; //potential energy
double ekin=0; //kinetic energy
double einit=0; //initial energy
double t=0;
double tmeasurestart=0; //Actual measurement starting time
double diffusion=0; //Diffusion
double pressure=0;
const int rdfsize=rdfcutoff/rdfdr;
double rdfarr[rdfsize];
clock_t begin;
int nlist[N]={}; //Verlet particle number list
double list[N][N]={};
char folder[100];
char filename[100];
double instantaneoustemp();
void updatelist();
void initialize();
void correctvelocities();
void calcrdf(); //Calculate the radial distribution function
void calcforce();
void calcekin(); //To calculate the initial kinetic energy
double dist(int i, int j,int coord=3); //if coord=3, it returns the norm squared, else it returns the distance in coord dimension
void printdata(int type=0,double time=0);
void printimage();
void output(int stage);
double normalrand();
void displace();
double mod(double num, double div);
using namespace std;
//using namespace arma;
int main(int argc, char * argv[])
{
if (argc!=3)
{
cout << "Must have two parameters.\nExiting...\n";
return 1;
}
T=atof(argv[1]);
rho=atof(argv[2]);
L = pow(N/rho,1/3.0);
cout << "\n\n\n\n";
cout << "Molecular Dynamics simulation of Argon\n\n";
cout << "Number of particles: " << N << endl;
cout << "Density: " << rho << endl;
cout << "Box length: " << L << endl;
cout << "Temperature: " << T << endl;
cout << "dt: " << dt << " s" << endl;
cout << "Thermalization iterations: " << thermiter << "\t iterations: " << iterations << endl;
cout << "Thermalization time: " << dt * thermiter << " seconds\n";
cout << "simulation time: " << dt * iterations << " seconds\n";
//Create folders and files
sprintf(folder,"data/%g-%g-%d-%g/",(double)T,(double)rho,n,dt);
mkdir(folder, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
sprintf(filename,"%simages",folder);
mkdir(filename, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
sprintf(filename,"%sdata.dat",folder);
ofstream file(filename);
file.close();
sprintf(filename,"%sthermdata.dat",folder);
file.open(filename);
file.close();
sprintf(filename,"%srdf.dat",folder);
file.open(filename);
file.close();
//Check if not too many images will be made
if ( (iterations + thermiter) / loopsperimage > 1000)
{
cout << "Too many images will be made, exiting...\n";
return 1;
}
srand48((long)time(NULL));
initialize();
begin = clock(); //Start timer
updatelist(); //Create first verlet list
calcforce(); //Calculate initial force and potential energy
calcekin(); //Calculate initial kinetic energy
printimage();
output(0);//Also write output to file
cout << "Starting thermalization\n";
for (int perc=0; perc < notices; perc++)
{
for (int loop = 0; loop < thermiter/notices; loop++)
{
if (loop%listinterval==0) updatelist();
if (loop%loopsperimage==0) printimage();
if (loop%rescaleiter==0) correctvelocities(); //Rescale velocities for correct temperature
displace();
t += dt;
if (loop%loopsperthermdatastore==0) printdata(1);
}
cout << "Percent done: " << (perc+1) * 100 / notices << "%";
cout << "\ttime simulated: " << double(clock() - begin) / CLOCKS_PER_SEC << " s";
double timeleft = (double(notices - (perc+1)) / (perc+1) * double(clock() - begin)) / CLOCKS_PER_SEC;
cout << "\t time left: " << floor(timeleft/60) << "m " << round(mod(timeleft,60)) << "s\n";
}
output(1); //Write thermalization time to file
cout << "\nStarting Simulation\n";
tmeasurestart=t; //Measurement starts at this time
for (int i=0;i<N;i++) //Initial positions for diffusion
for (int j=0;j<3;j++)
posinit[i][j] = pos[i][j];
correctvelocities();
cout << "Temperature equals " << instantaneoustemp() << endl;
einit = epot + ekin;
for (int perc=0; perc < notices; perc++)
{
for (int loop = 0; loop < iterations/notices; loop++)
{
if (loop%listinterval==0) updatelist();
if (loop%loopsperimage==0) printimage();
displace();
t += dt;
if (loop%loopsperdatastore==0) printdata(0,t-tmeasurestart);
if (loop%rdfiter==0) calcrdf();
}
cout << "Percent done: " << (perc+1) * 100 / notices << "%";
cout << "\ttime simulated: " << double(clock() - begin) / CLOCKS_PER_SEC << " s";
double total = double( (thermiter + iterations));
double current = (double)(thermiter + double(perc+1)/notices * iterations);
double timeleft = ((total/current - 1) * double(clock() - begin)) / CLOCKS_PER_SEC;
cout << "\t time left: " << floor(timeleft/60) << "m " << round(mod(timeleft,60)) << "s\n";
cout << "diffusion: " << diffusion << "\tpressure: " << pressure << endl;
}
diffusion=0;
for (int i=0;i<N;i++)
for (int j=0;j<3;j++)
diffusion+=(pos[i][j] - posinit[i][j]) * (pos[i][j] - posinit[i][j]);
diffusion/= 6. * N * (t-tmeasurestart);
cout << "\nSimulation Finished\n";
cout << "Initial energy: " << einit << "\tFinal energy: " << epot + ekin << "\tDifference(error): " << epot + ekin-einit << endl;
cout << "Diffusion = " << diffusion << endl;
output(2);
return 0;
}
double instantaneoustemp()
{
double sum=0;
for (int i = 0; i < N; i++)
for (int j = 0; j < 3; j++)
sum+= vel[i][j] * vel[i][j];
return sum / (3 * (N-1));
}
void correctvelocities()
{
double lambda = sqrt( (N-1) * 3 * T / (2 * N * ekin));
for (int i=0;i<N;i++)
for (int j=0;j<3;j++)
vel[i][j] *= lambda;
}
void calcrdf()
{
memset(rdfarr,0,sizeof(rdfarr));
for(int i=0;i<N;i++)
{
for (int j=0;j<N;j++)
{
if (i!=j)
{
int k=round(sqrt(dist(i,j,3))/rdfdr);
if (k < rdfsize)
rdfarr[k] += 1;
}
}
}
for (int i=0;i<rdfsize;i++)
rdfarr[i] *= 2. * L*L*L / ( N * (N-1) * (4*pi * ((i+0.5)*rdfdr) * ((i+0.5)*rdfdr) *rdfdr));
printdata(2);
}
void updatelist()
{
memset(nlist,0,sizeof(nlist));
for (int i=0; i<N-1; i++)
for (int j=i+1; j<N; j++)
if( dist(i,j,3) < rv2)
{
list[i][nlist[i]] = j;
nlist[i]++;
}
}
void displace()
{
ekin=0;
for (int p=0;p<N;p++)
{
for (int i=0;i<3;i++)
{
vel[p][i] += force[p][i]*dt/2.;
pos[p][i] = pos[p][i] + vel[p][i] * dt;
}
}
calcforce();
for (int p=0;p<N;p++)
for (int i=0;i<3;i++)
{
vel[p][i] += force[p][i] * dt / 2.;
ekin += 0.5 * vel[p][i] * vel[p][i];
}
ekin /= double(N) ; //To make it per particle
}
void calcforce()
{
memset(force,0,sizeof(force));
epot = 0;
pressure=0;
for (int i=0;i<N;i++)
{
for (int pn=0; pn<nlist[i]; pn++)
{
int j=list[i][pn];//This is the correct particle number
//cout << "i,j=" << i << "," << j << "\t";
//cin.ignore();
double dr[3];
dr[0]=dist(i,j,0);
dr[1]=dist(i,j,1);
dr[2]=dist(i,j,2);
double dr2 = (dr[0]*dr[0] + dr[1]*dr[1] + dr[2]*dr[2]);
if (dr2 < rc2)
{
double r2i = 1./dr2;
double r6i = r2i * r2i * r2i;
double f=48*r2i*r6i*(r6i-0.5); //Lennard-Jones Potential
for (int k=0;k<3;k++)
{
force[i][k] += f*dr[k];
force[j][k] -= f*dr[k];
}
pressure += f * dr2;
epot += 4*r6i *(r6i - 1) - ecut;
}
}
}
epot /= N;
pressure = 1 + pressure/(double(N)*3*T);
}
void calcekin()
{
for (int p=0; p<N; p++)
for (int i=0;i<3;i++)
ekin += 0.5 * vel[p][i] * vel[p][i];
ekin /= double(N);
}
double dist(int i, int j, int coord)
{
double dr=0;
if (coord!=3) //Returns distance (may be negative)
{
dr = pos[i][coord] - pos[j][coord];
dr -= round(dr / L) * L;
return dr;
}
else //Returns the distance squared (always positive)
{
double dx;
for (int k = 0; k < 3 ; k++)
{
dx = pos[i][k] - pos[j][k];
dx -= round(dx / L) * L;
dr += dx * dx;
}
return dr;
}
}
void printdata(int type,double time)
{
ofstream file;
if (type==1)
{
//sprintf(filename,"data/%g-%g-%d/thermdata.dat",(double)T,(double)rho,n);
sprintf(filename,"%sthermdata.dat",folder);
file.open(filename,fstream::app);
file << ekin + epot << " " << ekin << " " << epot << endl;
}
else if (type==2)
{
sprintf(filename,"%srdf.dat",folder);
file.open(filename,fstream::app);
for (int i=0;i<rdfsize;i++)
{
file << rdfarr[i] << " ";
}
file << endl;
}
else
{
//sprintf(filename,"data/%g-%g-%d/data.dat",(double)T,(double)rho,n);
sprintf(filename,"%sdata.dat",folder);
file.open(filename,fstream::app);
//Calculate diffusion
diffusion=0;
for (int i=0;i<N;i++)
for (int j=0;j<3;j++)
diffusion+=(pos[i][j] - posinit[i][j]) * (pos[i][j] - posinit[i][j]);
diffusion/= 6. * N * time;
file << ekin + epot << " " << ekin << " " << epot << " " << diffusion << " " << instantaneoustemp() << " " << pressure << endl;
}
file.close();
}
void printimage()
{
static int filecount=1;
char filename[100];
sprintf(filename,"%simages/fcc-%04d.dat",folder,filecount);
ofstream file(filename);
file << N << endl;
file << "0 " << L << endl;
file << "0 " << L << endl;
file << "0 " << L << endl;
for (int i=0;i<N;i++)
file << mod(pos[i][0],L) << " " << mod(pos[i][1],L) << " " << mod(pos[i][2],L) << " 1" << endl;
file.close();
/*Temporary check for maximum velocity
double maxvel=0;
for (int i=0;i<N;i++)
for (int coord=0;coord<3;coord++)
if (abs(vel[i][coord])>maxvel) maxvel = vel[i][coord];
cout << "Maximum velocity = " << maxvel << endl;
*/
filecount++;
}
void output(int stage)
{
//char filename[100];
//sprintf(filename,"data/%g-%g-%d/output.txt",(double)T,(double)rho,n);
sprintf(filename,"%soutput.txt",folder);
ofstream file(filename,fstream::app);
if (stage ==0)
{
file << "\n\n\n\n";
file << "Molecular Dynamics simulation of Argon\n\n";
file << "Number of particles: " << N << endl;
file << "Density: " << rho << endl;
file << "Box length: " << L << endl;
file << "Temperature: " << T << endl;
file << "Thermalization iterations: " << thermiter << "\t measurement iterations: " << iterations << endl;
file << "dt: " << dt << "s" << endl;
file << "Thermalization time: " << dt * thermiter << " seconds\n";
file << "simulation time: " << dt * iterations << " seconds\n";
file << "Initial energy: " << einit << "\n\n";
}
else if (stage ==1)
{
file << "Thermalization took " << double(clock() - begin) / CLOCKS_PER_SEC << " s\n";
file << "Current energy: " << ekin+epot << "\tekin=" << ekin << "epot=" << epot << "\n\n";
}
else if (stage == 2)
{
file << "Simulation took " << double(clock() - begin) / CLOCKS_PER_SEC << " s\n";
file << "Initial energy: " << einit << "\tFinal energy: " << epot + ekin << "\tDifference(error): " << epot + ekin-einit << endl;
file << "Kinetic energy: " << ekin << "\tPotential energy: " << epot << endl;
file << "Diffusion = " << diffusion << endl;
}
}
void initialize()
{
double d=pow(4/rho,1/3.0);
double sumv[3]={}; //velocity centre of mass
int i=0;
for (int z=0;z<2*n;z++)
for (int y=0;y<n;y++)
for (int x=0;x<n;x++)
{
pos[i][0] = (x * d + (d/2) * (z % 2));
pos[i][1] = y * d;
pos[i][2] = z * d/2;
for (int j=0;j<3;j++)
{
vel[i][j]=normalrand();
sumv[j]+=vel[i][j];
}
i++;
pos[i][0] = (x + 1/2.) * d + (d/2) * (z % 2);
pos[i][1] = (y + 1/2.) * d;
pos[i][2] = z * d / 2;
for (int j=0;j<3;j++)
{
vel[i][j]=normalrand();
sumv[j] +=vel[i][j];
}
i++;
}
printf("Centre of mass equals: (%g,%g,%g), now correcting\n",sumv[0],sumv[1],sumv[2]);
for (i=0;i<N;i++)
for (int j=0;j<3;j++)
vel[i][j] -=sumv[j]/double(N);
//FCC Lattice is created
}
double normalrand()
{
static double second;
static bool extra=0;
if (extra==1)
{
extra=0;
return second;
}
else
{
double x = drand48();
double y = drand48();
double u = sqrt(T) * sqrt(-2*log(x))*cos(2*pi*y);
second = sqrt(T) * sqrt(-2*log(x))*sin(2*pi*y);
extra = 1;
return u;
}
}
double mod(double num,double div)
{
return num - floor(num/div) * div;
}