-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
492 lines (372 loc) · 18.4 KB
/
main.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
using namespace std;
#include <iostream>
#include <cmath>
#include <ctime>
#include <string>
#include <fstream>
#include <stdlib.h>
#include "Declearation.h"
#include "Multiparticle.h"
/////////////////////////////////////////////////////////////////////////////################################################################///////////////////////////////////////////////////////////
// Writing functions here
// Update distances
void distance_cal(float pos[864][3], float * dist);
// To calculate force Brute_force for now
float force_cal(float posx, float posy, float posz, float * accx, float * accy, float * accz);
// To update pos and velocity
float update_all(float * posx, float * posy, float * posz, float * velx, float * vely, float * velz, float accx, float accy, float accz,float last_accx, float last_accy, float last_accz, float delta_time);
// For periodic Boundry Condition
void periodic_bc();
// To calculate total energy
void potential_energy ();
void kinetic_energy ();
// To calculate pair dist functions
void pair_dist();
// To calculate velocity dist function
void vel_auto(int step);
// clearing matrix
void clearing();
// scaling velocity
void scaling(float kinetic);
/////////////////////////////////////////////////////////////////////////////################################################################///////////////////////////////////////////////////////////
int main()
{
float potential, kinetic;
// Reading from file
declearation("data.txt");
for (i = 0 ; i<864; i++)
{
init_vel[i][0] = vel[i][0];
init_vel[i][1] = vel[i][1];
init_vel[i][2] = vel[i][2];
}
// calculating dist for further use
// To calculate force on each atom
for ( i = 0; i<864; i++)
{
potential = force_cal(pos[i][0], pos[i][1], pos[i][2], &acc[i][0], &acc[i][1], &acc[i][2]);
}
// Now starting the real loop
// for 100 time steps
for (int step = 0; step < 1000; step++)
{
potential = 0;
kinetic = 0;
distance_cal(pos, dist);
if(step%10 == 0) vel_auto(step);
// In each step going in loop to update everything
for (i = 0; i<864; i++)
{
last_acc[i][0] =acc[i][0];
last_acc[i][1] =acc[i][1];
last_acc[i][2] =acc[i][2];
// To calculate force on each atom
potential = potential + force_cal(pos[i][0], pos[i][1], pos[i][2], &acc[i][0], &acc[i][1], &acc[i][2]);
}
for (i = 0; i<864; i++)
{
// Updating position and velocity
kinetic = kinetic + update_all(&pos[i][0], &pos[i][1], &pos[i][2], &vel[i][0], &vel[i][1] , &vel[i][2] , acc[i][0], acc[i][1], acc[i][2],last_acc[i][0], last_acc[i][1], last_acc[i][2], delta_time);
}
float temp;
temp = 2 * kinetic/(3*864);
for (i = 0; i<864; i++)
{
//cout << pos[i][0] << " " << pos[i][1] << " " << pos[i][2] << " " << kinetic << " " << potential << " " << vel[i][0] << " " << acc[i][0] << " " << (potential/2 + kinetic) << " " << temp << endl;
}
scaling(kinetic);
clearing();
}
pair_dist();
return 0;
}
////////////////////////////////////////////////////////////////////////####################################################################///////////////////////////////////////////////////////////
// Will write functions here
void scaling(float kinetic)
{
float temperature = (2000/4.8443) * kinetic;
float scale = pow((50509664/temperature) , .5);
for (i = 0; i<864; i++)
{
vel[i][0] = vel[i][0] * scale;
vel[i][1] = vel[i][1] * scale;
vel[i][2] = vel[i][2] * scale;
}
}
void vel_auto(int step)
{
float temp, x_comp, y_comp, z_comp,res = 0;
for (i = 0; i<864; i++)
{
x_comp = init_vel[i][0]*vel[i][0];
y_comp = init_vel[i][1]*vel[i][1];
z_comp = init_vel[i][2]*vel[i][2];
temp = pow( (pow(vel[i][0],2) + pow(vel[i][1],2) + pow(vel[i][2],2) ),.5) * pow( (pow(init_vel[i][0],2) + pow(init_vel[i][1],2) + pow(init_vel[i][2],2) ),.5) ;
res = res + (x_comp + y_comp + z_comp)/ temp;
}
cout << res/864 << " " << step * .01 << endl;
}
void pair_dist()
{
// getting an atom from center
for (i = 0; i< 20; i++)
{
int flag = 0;
if ((pos[i][0]-17.39) > 10) flag++;
if ((pos[i][1]-17.39) > 10) flag++;
if ((pos[i][2]-17.39) > 10) flag++;
if (flag == 0) break;
}
float posx = pos[i][0];
float posy = pos[i][1];
float posz = pos[i][2];
for ( i = 0; i<864; i++)
{
float temp = pow( (pow(pos[i][0]-posx,2) + pow(pos[i][1]-posy,2) + pow(pos[i][2]-posz,2) ),.5);
dist[i] = temp;
}
// calculating correlation
float delta_r = 1.0;
float gr , r;
int count;
for (i = 0; i< 100; i++)
{
count = 0;
for (int j=0; j< 864; j++)
{
if (dist[j] > i/7.0 && dist[j] < (i/7.0)+(1/7.0)) count ++;
}
gr = 50 * count/(1.8 * pow((i+1)/7.0,2));
cout << gr << " " << (i+1)/7.0 << endl;
}
}
float update_all(float * posx, float * posy, float * posz, float * velx, float * vely, float * velz, float accx, float accy, float accz,float last_accx, float last_accy, float last_accz, float delta_time)
{
float kinetic;
float mbk = 4.8443;
*posx = *posx + *velx * delta_time + accx * delta_time * delta_time/2;
*posy = *posy + *vely * delta_time + accy * delta_time * delta_time/2;
*posz = *posz + *velz * delta_time + accz * delta_time * delta_time/2;
// update position with periodic box condition
if (*posx > 34.78) *posx = *posx - 34.78;
if (*posx < 0) *posx = *posx + 34.78;
if (*posy > 34.78) *posy = *posy - 34.78;
if (*posy < 0) *posy = *posy + 34.78;
if (*posz > 34.78) *posz = *posz - 34.78;
if (*posz < 0) *posz = *posz + 34.78;
*velx = *velx + (accx + last_accx) * delta_time/2;
*vely = *vely + (accy + last_accy) * delta_time/2;
*velz = *velz + (accz + last_accz) * delta_time/2;
kinetic = mbk * (pow(*velx, 2) + pow(*vely, 2) + pow(*velz, 2))/2000;
return kinetic;
}
// clearing attraction matrix
void clearing()
{
for (int i=0; i<864; i++)
{
for (int j = 0; j<3; j++)
{
acc[i][j] = 0;
}
}
}
// Don't mess with what's written below :p
//*********************************************************************************************************************************************************
//---------------------------------------------------------------------------------------------------------------------------------------------------------
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Update pos
void distance_cal(float pos[864][3], float * dist)
{
for (int i = 0; i<864; i++)
{
float temp = pow( (pow(pos[i][0],2) + pow(pos[i][1],2) + pow(pos[i][2],2) ),.5);
dist[i] = temp;
}
}
// Force calculation
float force_cal(float posx, float posy, float posz, float * accx, float * accy, float * accz)
{
float potential = 0;
// For all Atoms
int flag = 0;
if ((posx-17.39) > 9.6) flag++;
if ((posy-17.39) > 9.6) flag++;
if ((posz-17.39) > 9.6) flag++;
for (int i = 0; i<27; i++)
{
// Mapping to 3D
int num = i;
int cor_x, cor_y, cor_z;
cor_x = (num/9)-1;
cor_y = ((num%9)/3)-1;
cor_z = (num%3)-1;
//cout << cor_x << cor_y << cor_z << endl;
if ((abs(cor_x) + abs(cor_y) + abs(cor_z)) <= flag)
{
for (int j = 0; j<864; j++)
{
float r = pow( (pow( (pos[j][0] + cor_x * 34.78 )- posx, 2) + pow((pos[j][1] + cor_y * 34.78 )- posy, 2) + pow((pos[j][2] + cor_z * 34.78 )- posz, 2) ), .5);
if (r < 7.65 && r > .1)
{
// Beware of units, its acc not force
// letus measure energy in form of kt
potential = potential + 4 * epsilon * ((pow((sigma/r),12)) - (pow((sigma/r),6)));
float force = -4000 * kbm * epsilon * ((pow((sigma/r),13) * 12) - (pow((sigma/r),7) * 6))/ sigma;
*accx = *accx + force * ((pos[j][0] + cor_x * 34.78)-posx)/r;
*accy = *accy + force * ((pos[j][1] + cor_y * 34.78)-posy)/r;
*accz = *accz + force * ((pos[j][2] + cor_z * 34.78 )-posz)/r;
if (potential > 1000000000)
{
//cout << potential << " " << *accz << " " << r << " " << posx << " " << pos[j][0] + cor_x * 34.78 << " " << posy << " " << pos[j][1] + cor_y * 34.78 << " " << posz << " " << pos[j][2] + cor_z * 34.78 << " " << endl;
}
}
}
}
}
return potential;
// For stoms at sides periodic boundry condition Too messy and can't do in this much short time
//////////////////////////////////////##################################For one edge//////////////////////////////////////***********************************//////////////////////////////////////
/*
if (flag == 1)
{
if (abs(posx - 17.389) > 10 )
{
if ((posx - 17.389) > 10 ))
{
for (int i = 0; i<864; i++)
{
if ((pos[i][0] + 34.78) - posx) < 7.65)
{
float r = pow( (pow((pos[i][0] + 34.78) - posx, 2) + pow(pos[i][1] - posy, 2) + pow(pos[i][2]-posz, 2) ), .5);
if (r < 7.65 && r > .1)
{
// Beware of units, its acc not force
float force = 4 * kbm * ( (pow((sigma/r),13) * 12) - (pow((sigma/r),7) * 6))/ sigma;
*accx = *accx + force * ((pos[i][0] + 34.78) - posx);
*accy = *accy + force * (pos[i][1]-posy);
*accz = *accz + force * (pos[i][2]-posz);
//float check = pow( pow((pos[i][0]-posx)/r, 2) + pow((pos[i][1]-posx)/r, 2) + pow((pos[i][2]-posx)/r, 2), .5);
//cout << check << " " << r << " " << posx <<" " << posy << " " << posz <<" " << pos[i][0] << " " << pos[i][1] <<" " << pos[i][2] << endl;
//cout << pow(*accx, 2) + pow(*accy, 2) + pow(*accz, 2) << " " << force * force << endl;
}
}
}
}
else
{
for (int i = 0; i<864; i++)
{
if ((pos[i][0] - posx) > 27)
{
float r = pow( (pow((pos[i][0] - 34.78) - posx, 2) + pow(pos[i][1] - posy, 2) + pow(pos[i][2]-posz, 2) ), .5);
if (r < 7.65 && r > .1)
{
// Beware of units, its acc not force
float force = 4 * kbm * ( (pow((sigma/r),13) * 12) - (pow((sigma/r),7) * 6))/ sigma;
*accx = *accx + force * ((pos[i][0] - 34.78) - posx);
*accy = *accy + force * (pos[i][1]-posy);
*accz = *accz + force * (pos[i][2]-posz);
//float check = pow( pow((pos[i][0]-posx)/r, 2) + pow((pos[i][1]-posx)/r, 2) + pow((pos[i][2]-posx)/r, 2), .5);
//cout << check << " " << r << " " << posx <<" " << posy << " " << posz <<" " << pos[i][0] << " " << pos[i][1] <<" " << pos[i][2] << endl;
//cout << pow(*accx, 2) + pow(*accy, 2) + pow(*accz, 2) << " " << force * force << endl;
}
}
}
}
}
// For y side-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if (abs(posy - 17.389) > 10 )
{
if ((17.389 - posy) > 10 ))
{
for (int i = 0; i<864; i++)
{
if ((pos[i][1] - posy) > 27)
{
float r = pow( (pow((pos[i][0]) - posx, 2) + pow((pos[i][1]- 34.78) - posy, 2) + pow(pos[i][2]-posz, 2) ), .5);
if (r < 7.65 && r > .1)
{
// Beware of units, its acc not force
float force = 4 * kbm * ( (pow((sigma/r),13) * 12) - (pow((sigma/r),7) * 6))/ sigma;
*accx = *accx + force * (pos[i][0] - posx);
*accy = *accy + force * ((pos[i][1] - 34.78) - posy);
*accz = *accz + force * (pos[i][2]-posz);
//float check = pow( pow((pos[i][0]-posx)/r, 2) + pow((pos[i][1]-posx)/r, 2) + pow((pos[i][2]-posx)/r, 2), .5);
//cout << check << " " << r << " " << posx <<" " << posy << " " << posz <<" " << pos[i][0] << " " << pos[i][1] <<" " << pos[i][2] << endl;
//cout << pow(*accx, 2) + pow(*accy, 2) + pow(*accz, 2) << " " << force * force << endl;
}
}
}
}
else
{
for (int i = 0; i<864; i++)
{
if ((posy - pos[i][1]) > 27)
{
float r = pow( (pow((pos[i][0]) - posx, 2) + pow((pos[i][1] + 34.78) - posy, 2) + pow(pos[i][2]-posz, 2) ), .5);
if (r < 7.65 && r > .1)
{
// Beware of units, its acc not force
float force = 4 * kbm * ( (pow((sigma/r),13) * 12) - (pow((sigma/r),7) * 6))/ sigma;
*accx = *accx + force * (pos[i][0] - posx);
*accy = *accy + force * ((pos[i][1] + 34.78) - posy);
*accz = *accz + force * (pos[i][2]-posz);
//float check = pow( pow((pos[i][0]-posx)/r, 2) + pow((pos[i][1]-posx)/r, 2) + pow((pos[i][2]-posx)/r, 2), .5);
//cout << check << " " << r << " " << posx <<" " << posy << " " << posz <<" " << pos[i][0] << " " << pos[i][1] <<" " << pos[i][2] << endl;
//cout << pow(*accx, 2) + pow(*accy, 2) + pow(*accz, 2) << " " << force * force << endl;
}
}
}
}
}
// for z ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if (abs(posz - 17.389) > 10 )
if ((17.389 - posz ) > 10 ))
{
for (int i = 0; i<864; i++)
{
if ((pos[i][2] - posz) > 27)
{
float r = pow( (pow((pos[i][0]) - posx, 2) + pow((pos[i][1]) - posy, 2) + pow((pos[i][2]- 34.78)-posz, 2) ), .5);
if (r < 7.65 && r > .1)
{
// Beware of units, its acc not force
float force = 4 * kbm * ( (pow((sigma/r),13) * 12) - (pow((sigma/r),7) * 6))/ sigma;
*accx = *accx + force * (pos[i][0] - posx);
*accy = *accy + force * (pos[i][1] - posy);
*accz = *accz + force * ((pos[i][2] - 34.78) - posz);
//float check = pow( pow((pos[i][0]-posx)/r, 2) + pow((pos[i][1]-posx)/r, 2) + pow((pos[i][2]-posx)/r, 2), .5);
//cout << check << " " << r << " " << posx <<" " << posy << " " << posz <<" " << pos[i][0] << " " << pos[i][1] <<" " << pos[i][2] << endl;
//cout << pow(*accx, 2) + pow(*accy, 2) + pow(*accz, 2) << " " << force * force << endl;
}
}
}
}
else
{
for (int i = 0; i<864; i++)
{
if ((posy - pos[i][1]) < 27)
{
float r = pow( (pow((pos[i][0]) - posx, 2) + pow((pos[i][1]) - posy, 2) + pow((pos[i][2]- 34.78)-posz, 2) ), .5);
if (r < 7.65 && r > .1)
{
// Beware of units, its acc not force
float force = 4 * kbm * ( (pow((sigma/r),13) * 12) - (pow((sigma/r),7) * 6))/ sigma;
*accx = *accx + force * (pos[i][0] - posx);
*accy = *accy + force * ((pos[i][1] - 34.78) - posy);
*accz = *accz + force * (pos[i][2]-posz);
//float check = pow( pow((pos[i][0]-posx)/r, 2) + pow((pos[i][1]-posx)/r, 2) + pow((pos[i][2]-posx)/r, 2), .5);
//cout << check << " " << r << " " << posx <<" " << posy << " " << posz <<" " << pos[i][0] << " " << pos[i][1] <<" " << pos[i][2] << endl;
//cout << pow(*accx, 2) + pow(*accy, 2) + pow(*accz, 2) << " " << force * force << endl;
}
}
}
}
}
*/
}