-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd.cpp
More file actions
331 lines (252 loc) · 7.83 KB
/
Copy pathmd.cpp
File metadata and controls
331 lines (252 loc) · 7.83 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
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
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
int main(){
////////////////////////////////////////////////////////////////////////////////////////////////
// Input Parameters ////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
double dt = 0.005;
int m = 30;
int N = pow(m,2); // no. of particles
double T0 = 1.0; // target temperature
double rho = 0.5; // number density
double A = N/rho; // area
double L = m/pow(rho, 0.5);
double rc = 2.5; // cut-off
int t = 103000; // no. of timesteps
int equi = 3000;
int runtime = t - equi; // timesteps without equilibration
////////////////////////////////////////////////////////////////////////////////////////////////
// Declare Variables and Arrays ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
double rx[N];
double ry[N];
double vx[N];
double vy[N];
double fx[N];
double fy[N];
double vvx; // average x-velocity
double vvy; // average y-velocity
double x;
double y;
double r;
double ff; // force
double ffx;
double ffy;
double u; // potential energy
double k; // kinetic energy
double T; // temperature
double Pc; // interaction part of pressure
double Pk; // kinetic part of pressure
double P; // total pressure
int i;
int j;
int n;
double sum;
double sumx;
double sumy;
double diff;
std::vector<double> energy;
std::vector<double> pressure;
std::vector<double> bavg1;
std::vector<double> bavg2;
double avg1;
double var1;
double avg2;
double var2;
double tb;
double blockavg;
double blockvar;
double s;
////////////////////////////////////////////////////////////////////////////////////////////////
// Set up Lattice and Initialise Velocities ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
std::cout << "\nSetting up lattice." << std::endl;
n = 0;
for (i = 1; i <= m; i++){ // set up lattice
for (j = 1; j <= m; j++){
ry[n] = (L/m)*(j - 0.5);
rx[n] = (L/m)*(i - 0.5);
n++;
}
}
for (i = 0; i <= N-1; i++){ // give random values as initial velocities
vx[i] = sqrt(i);
vy[i] = cos(5*i);
}
std::cout << "\nInitialising Velocities." << std::endl;
sumx = 0.0;
sumy = 0.0;
for (i = 0; i <= N-1; i++){ // calculate sum of all velocities
sumx = sumx + vx[i];
sumy = sumy + vy[i];
}
vvx = sumx/N; // calculate average velocity
vvy = sumy/N;
T = 0.0;
for (i = 0; i <= N-1; i++){ // find intial temperature
T = T + 0.5*(pow(vx[i], 2) + pow(vy[i], 2));
}
for (i = 0; i <= N-1; i++){ // initialise velocity by scaling wrt temperature
vx[i] = vx[i] - vvx; // sets box momentum to zero
vy[i] = vy[i] - vvy;
vx[i] = vx[i]*sqrt(T0/T); // scale wrt target temperature and calculated temperature
vy[i] = vy[i]*sqrt(T0/T);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Force Loop //////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
std::ofstream file1;
std::ofstream file2;
std::ofstream file3;
std::ofstream file4;
std::ofstream file5;
std::ofstream file6;
file1.open("U.txt");
file2.open("K.txt");
file3.open("E.txt");
file4.open("P.txt");
file5.open("sE.txt");
file6.open("sP.txt");
std::cout << "\nCalculating Velocities, Positions, Energies and Pressure."
<< std::endl;
std::cout << "Please wait..." << std::endl;
sumx = 0.0;
sumy = 0.0;
for (int step = 0; step < t; step++){
for (n = 0; n <= N-1; n++){ // initialise force
fx[n] = 0;
fy[n] = 0;
}
////////////////////////////////////////////////////////////////////////////////////////////
// Calculate Force and Potential Energy ///////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
// initialise potential energy and pressure
u = 0;
Pc = 0;
Pk = 0;
P = 0;
for (i = 0; i <= N-2; i++){
for (j = i + 1; j <= N-1; j++){
x = rx[i] - rx[j];
y = ry[i] - ry[j];
// periodic boundary conditions
if (x >= L/2){ x = x - L; }
if (x < -L/2){ x = x + L; }
if (y >= L/2){ y = y - L; }
if (y < -L/2){ y = y + L; }
r = sqrt(pow(x, 2) + pow(y, 2));
if (r < rc){ // check if r less than cut-off
u = u + 4*(1/pow(r,12) - 1/pow(r,6)); // calculate potential energy
ff = 48/pow(r, 14) - 24/pow(r, 8); // calculate force
ffx = x*ff;
ffy = y*ff;
fx[i] = fx[i] + ffx;
fy[i] = fy[i] + ffy;
fx[j] = fx[j] - ffx;
fy[j] = fy[j] - ffy;
Pc = Pc + r*ff;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// Apply Verlet Algorithm, calculate Kinetic Energy and Temperature/////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
k = 0; // initialise kinetic energy
for (i = 0; i <= N-1; i++){
vx[i] = vx[i] + dt*fx[i]; // update velocity using acceleration
vy[i] = vy[i] + dt*fy[i];
k = k + 0.5*(pow(vx[i], 2) + pow(vy[i], 2)); // calculate kinetic energy
rx[i] = rx[i] + dt*vx[i]; // update position using velocity
ry[i] = ry[i] + dt*vy[i];
}
u = u/N; // average potential energy
k = k/N; // average kinetic energy
T = k; // temperature
Pk = rho*T;
Pc = 0.5*Pc*rho/N;
P = Pk + Pc;
////////////////////////////////////////////////////////////////////////////////////////////
// Write to Text Files /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
if (step >= equi){
sumx += u+k;
sumy += P;
energy.push_back(u+k);
pressure.push_back(P);
file1 << u << "\n";
file2 << k << "\n";
file3 << u+k << "\n";
file4 << P << "\n";
}
}
file1.close();
file2.close();
file3.close();
file4.close();
////////////////////////////////////////////////////////////////////////////////////////////////
// Analysis ////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
std::cout << "\nCalculating Mean and Variance for Energy and Pressure." << std::endl;
avg1 = sumx/runtime;
avg2 = sumy/runtime;
diff = 0.0;
for (i = 0; i < energy.size(); i++) {
diff += pow(energy[i] - avg1, 2);
}
var1 = diff/runtime;
diff = 0.0;
for (i = 0; i < pressure.size(); i++) {
diff += pow(pressure[i] - avg2, 2);
}
var2 = diff/runtime;
std::cout << "\nCalculating Block Averages and Block Variances for Energy and Pressure."
<< std::endl;
for (;;){
std::cout << "Enter the block size or enter 0 to end the simulation."
<< std::endl;
std::cin >> tb;
if (tb == 0){
break;
}
n = 0;
for (i = 0; i < energy.size(); i += tb) {
sum = 0.0;
for (j = 0; j < tb; j++) {
sum += energy[n];
n++;
}
blockavg = sum / tb;
bavg1.push_back(blockavg);
}
diff = 0.0;
for (i = 0; i < bavg1.size(); i++){
diff += pow(bavg1[i] - avg1, 2);
}
blockvar = diff*tb/runtime;
s = blockvar*tb/var1;
file5 << s << "\n";
n = 0;
for (i = 0; i < pressure.size(); i += tb) {
sum = 0.0;
for (j = 0; j < tb; j++) {
sum += pressure[n];
n++;
}
blockavg = sum / tb;
bavg2.push_back(blockavg);
}
diff = 0.0;
for (i = 0; i < bavg2.size(); i++){
diff += pow(bavg2[i] - avg2, 2);
}
blockvar = diff*tb/runtime;
s = blockvar*tb/var2;
file6 << s << "\n";
bavg1.clear();
bavg2.clear();
}
file5.close();
file6.close();
}