-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJerSim_v2.c
412 lines (312 loc) · 12 KB
/
JerSim_v2.c
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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Defintions */
#define PartNo 50 // Number of particles
#define Dim 2 // Box dimension
#define TwoPi 6.283185307 // 2Pi
#define Filenamelength 100 // Max characters for a file name eg. test1
#define sig 1
#define eps 1
#define r_shift 2.5*sig
/*-----------------*/
/* Box-Muller transformation is used to produce random samples for the Maxwell distribution which will be used to provide the intial values of the speed of the particles */
double sampgauss(double temp){
double u1, u2, z0;
u1 = drand48();
u2 = drand48();
z0 = sqrt(-2.0 * log(u1)) * cos(TwoPi * u2);
return (sqrt(temp) * z0);
}
/* L-J potential force */
void lj_force (double pos[PartNo][Dim], double acc[PartNo][Dim]){
double f_lj, r;
int i, j, d;
for (i=0; i<PartNo; i++){
for (d=0; d < Dim; d++){ // Removed zero'd acceleration
for (j = i+1; j < PartNo; j++){
r = sqrt(pow(pos[i][0]-pos[j][0],2) + pow(pos[i][1]-pos[j][1],2)); // Distance calculation
if (r <= 2.5*sig) { // Lower truncation
f_lj = (-4*eps*(1/(r+r_shift))*(6*pow(sig,6)/pow(r+r_shift,6)-12*pow(sig,12)/pow(r+r_shift,12))) * (pos[i][d]-pos[j][d])/r;
}
else if (r > 2.5*sig){
f_lj = 0.0;
}
acc[i][d] += f_lj;
acc[j][d] -= f_lj;
}
}
}
}
/* L-J potential */
void lj_pot (double pos[PartNo][Dim], double *v_pot){
int i, j;
double r;
*v_pot = 0.0;
for (i=0; i<PartNo; i++){
for (j=i+1; j < PartNo; j++){
r = sqrt(pow(pos[i][0]-pos[j][0],2) + pow(pos[i][1]-pos[j][1],2));
if (r <= 2.5*sig){
*v_pot += 4*eps*((pow(sig,12)/pow(r+r_shift,12))-(pow(sig,6)/pow(r+r_shift,6))) + 0.0163*eps;
}
else if (r > 2.5*sig){
*v_pot += 0.0;
}
}
}
}
/* Periodic interactions: creating periodic images of particles */
int per_int (double pos[PartNo][Dim], double boxdims[Dim], double per_arr[3*PartNo][Dim]){
int i, k=0;
for (i = 0; i < PartNo; i++)
{
// Corners
if (pos[i][0] < 2.5*sig && pos[i][1] < 2.5*sig) // Bottom left
{
per_arr[k][0] = pos[i][0] + boxdims[0];
per_arr[k][1] = pos[i][1] + boxdims[1];
k++;
}
if (pos[i][0] > boxdims[0] - 2.5*sig && pos[i][1] < 2.5*sig) // Bottom right
{
per_arr[k][0] = pos[i][0] - boxdims[0];
per_arr[k][1] = pos[i][1] + boxdims[1];
k++;
}
if (pos[i][0] < 2.5*sig && pos[i][1] > boxdims[1] - 2.5*sig) // Top left
{
per_arr[k][0] = pos[i][0] + boxdims[0];
per_arr[k][1] = pos[i][1] - boxdims[1];
k++;
}
if (pos[i][0] > boxdims[0] - 2.5*sig && pos[i][1] > boxdims[1] - 2.5*sig) // Top right
{
per_arr[k][0] = pos[i][0] - boxdims[0];
per_arr[k][1] = pos[i][1] - boxdims[1];
k++;
}
// Edges
if (pos[i][0] < boxdims[0] && pos [i][1] < 2.5*sig) // Bottom
{
per_arr[k][0] = pos[i][0];
per_arr[k][1] = pos[i][1] + boxdims[1];
k++;
}
if (pos[i][0] < 2.5*sig && pos[i][1] < boxdims[1]) // Left
{
per_arr[k][0] = pos[i][0] + boxdims[0];
per_arr[k][1] = pos[i][1];
k++;
}
if (pos[i][0] < boxdims[0] && pos[i][1] > boxdims[1] - 2.5*sig) // Top
{
per_arr[k][0] = pos[i][0];
per_arr[k][1] = pos[i][1] - boxdims[1];
k++;
}
if (pos[i][0] > boxdims[0] - 2.5*sig && pos[i][1] > boxdims[1]) // Right
{
per_arr[k][0] = pos[i][0] - boxdims[0];
per_arr[k][1] = pos[i][1];
k++;
}
}
return k;
}
/* Periodic force */
void per_forces (double pos[PartNo][Dim], double acc[PartNo][Dim], double per_arr[3*PartNo][Dim], double boxdims[Dim]){
int i, k, l, d;
double r, f_lj;
l = per_int(pos, boxdims, per_arr);
for (k = 0; k < l ; k++){
for (i = 0; i < PartNo; i++) {
r = sqrt(pow(pos[i][0]-per_arr[k][0],2) + pow(pos[i][1]-per_arr[k][1],2));
for (d = 0; d < Dim; d++) {
if (r <= 2.5*sig) { // Lower truncation
f_lj = (-4*eps*(1/(r+r_shift))*(6*pow(sig,6)/pow(r+r_shift,6)-12*pow(sig,12)/pow(r+r_shift,12))) * (pos[i][d]-per_arr[k][d])/r;// segfault was here you had pos[i][d]-pos[k][d] instead of pos[i][d]-per_arr[k][d]
}
else if (r > 2.5*sig){
f_lj = 0.0;
}
acc[i][d] += f_lj;
}
}
}
}
void lj_per_pot (double pos[PartNo][Dim], double *v_pot, double per_arr[3*PartNo][Dim], double boxdims[Dim]){
int i, l, k;
double r;
l = per_int(pos, boxdims, per_arr);
for (k=0; k<l; k++){
for (i=0; i < PartNo; i++){
r = sqrt(pow(pos[i][0]-per_arr[k][0],2) + pow(pos[i][1]-per_arr[k][1],2));
if (r <= 2.5*sig){
*v_pot += 0.5*(4*eps*((pow(sig,12)/pow(r+r_shift,12))-(pow(sig,6)/pow(r+r_shift,6))) + 0.0163*eps);
}
else if (r > 2.5*sig){
*v_pot += 0.0;
}
}
}
}
/* Initialisation of the position and velocities of the particles. (Changes the entries in the array that calls it??) ./a.out 0.0001 0.5 100 1.1 1.2 1.0 datafile will run each of the elements seperated by commas*/
/* Keeping the centre of velocity constant so the box doesn't drift */
void initpart (double pos[PartNo][Dim], double vel[PartNo][Dim], double temperature, double boxdims[Dim]){
int i, d;
double VCoM[Dim];
srand48((long)time(NULL));
for (i=0; i<PartNo; i++) {
for (d=0; d<Dim; d++) {
pos[i][d] = drand48()*boxdims[d]; // Places the particle at a random position within the boxes length
vel[i][d] = sampgauss(temperature); // Sample velocity from the Maxwell distribution using Box-Muller transform. *This can be hard-coded to test the interaction potentials later on.
VCoM[d] += vel[i][d];
}
}
for ( d = 0; d < Dim; d++){
VCoM[d] /= PartNo;
}
for ( i = 0; i < PartNo; i++){
for ( d = 0; d < Dim; d++){
vel[i][d] -= VCoM[d];
}
}
}
/* Integration algorithm */
void move_part(double pos[PartNo][Dim], double vel[PartNo][Dim], double acc[PartNo][Dim], double dt, double *t, double *KinE, double boxdims[Dim], double per_arr[3*PartNo][Dim]){
int i, d;
double newvel, newpos, newacc;
*KinE = 0.0;
for (i=0; i<PartNo; i++){
for (d=0; d<Dim; d++){
newpos = pos[i][d] + vel[i][d] * dt;
vel[i][d] += 0.5 * acc[i][d] * dt;
while (newpos > boxdims[d]){
newpos -= boxdims[d]; // Check the positions of the particles in the loop and updates it.
}
while (newpos < 0)
{
newpos += boxdims[d];
}
pos[i][d] = newpos;
acc[i][d] = 0.0;
}
}
per_forces(pos, acc, per_arr, boxdims);
lj_force(pos,acc);
for (i=0; i<PartNo; i++){
for (d=0; d<Dim; d++){
vel[i][d] += 0.5 * dt * acc[i][d];
}
*KinE += 0.5*(vel[i][0]*vel[i][0] + vel[i][1]*vel[i][1]);
}
*t += dt;
}
void box_p_v (double pos[PartNo][Dim], double acc[PartNo][Dim], double temperature, double pressure, double n_step, double per_arr[3*PartNo][Dim], double boxdims[Dim]){
int i, j, d;
double temp_pv;
for ( i = 0; i < PartNo; i++)
{
for ( j = 0; j < count; i++)
{
/* code */
}
}
}
/* The energies are written to a simple text file: timestamp, kinetic energy and potetial energy. */
void writeenergies(double KinE, double t, char filename[Filenamelength], double v_pot){
char filenameupdated[Filenamelength];
FILE *KineFile;
strcpy(filenameupdated, filename);
strcat(filenameupdated, "_E_list.txt"); // data written to filename that is pure text space seperated into 4 columns
KineFile = fopen(filenameupdated, "a");
fprintf(KineFile, "%f %f %f %f\n", t, KinE, v_pot, KinE + v_pot);
fclose(KineFile);
}
/* Creating a mathematica to animate the motions of the particles
void writepositions(double pos[PartNo][Dim], char filename[Filenamelength], double boxdims[Dim]){
int i;
char filenameupdated[Filenamelength];
FILE *posfile;
strcpy(filenameupdated, filename);
strcat(filenameupdated, "_mathematica.nb"); // data written to filename that is formated and designated a mathematica notebook
posfile = fopen(filenameupdated, "a");
fprintf(posfile, "ListPlot[{");
for (i=0; i<(PartNo-1); i++) {
fprintf(posfile, "{ %f, %f },\n", pos[i][0], pos[i][1]);
}
fprintf(posfile, "{ %f, %f }\n", pos[PartNo-1][0], pos[PartNo-1][1]);
fprintf(posfile, "}, PlotStyle -> PointSize[Large], \n PlotRange -> {{0, %f}, {0, %f}}, AspectRatio -> 1],\n", boxdims[0], boxdims[1]);
fclose(posfile);
}
/* void tidyupmathematicafile(double pos[PartNo][Dim], char filename[Filenamelength], double boxdims[Dim]){
int i;
char filenameupdated[Filenamelength];
FILE *posfile;
strcpy(filenameupdated, filename);
strcat(filenameupdated, "_mathematica.nb");
posfile = fopen(filenameupdated, "a");
fprintf(posfile, "ListPlot[{");
for (i=0; i<(PartNo-1); i++) {
fprintf(posfile, "{ %f, %f },\n", pos[i][0], pos[i][1]);
}
fprintf(posfile, "{ %f, %f }\n", pos[PartNo-1][0], pos[PartNo-1][1]);
fprintf(posfile, "}, PlotStyle -> PointSize[Large], \n PlotRange -> {{0, %f}, {0, %f}}, AspectRatio -> 1]}]\n", boxdims[0], boxdims[1]);
fclose(posfile);
}
void startmathematica(char filename[Filenamelength]){
char filenameupdated[Filenamelength];
FILE *posfile;
strcpy(filenameupdated, filename);
strcat(filenameupdated, "_mathematica.nb");
posfile = fopen(filenameupdated, "w");
fprintf(posfile, "ListAnimate[{");
fclose(posfile);
}
*/
/*Main function */
int main(int argc, const char *argv[]) {
double boxdims[Dim]; // Array for x and y dimensions of the box
double initi_temp; // Temperature for distribution sampling
double curr_time=0.0, deltat, endtime, steps; // Time initialisation
double VCoM[Dim] = {0}; // Centre-of-velocity
double Kinetic=0.0; // Temporary storage of kinetic energy
double Potential=0.0; // Storage of potential energy
double pressure = 0.0; // Storage of pressure
double positions[PartNo][Dim]; // 2D array for particle positions
double velocities[PartNo][Dim]; // 2D array for particle vel
double accelerations[PartNo][Dim] = {0}; // 2D array for particle accelerations
double per_arr[3*PartNo][Dim]; // 2D array for periodic ghost particles
char outputfilename[Filenamelength];
int outputinterval, stepssinceoutput=0;
steps = atof(argv[2])/atof(argv[1]);
deltat = atof(argv[1]);
endtime = atof(argv[2]);
outputinterval = atof(argv[3]);
boxdims[0] = atof(argv[4]);
boxdims[1] = atof(argv[5]);
initi_temp = atof(argv[6]);
strcpy(outputfilename, argv[7]);
printf("code has read in delta t= %.4e end time=%.4e interval for data=%d\n box x=%.4e box y=%.4e initial t=%.4e file=%s\n",deltat, endtime, outputinterval, boxdims[0], boxdims[1], initi_temp, outputfilename);
printf("n = %f\n", steps);
initpart(positions, velocities, initi_temp, boxdims);
lj_force(positions, accelerations);
per_forces(positions, accelerations, per_arr, boxdims);
box_p_v(positions, accelerations, initi_temp, pressure, steps, per_arr, boxdims);
//startmathematica(outputfilename);
while (curr_time < endtime) {
lj_force(positions, accelerations);
per_forces(positions, accelerations, per_arr, boxdims);
move_part(positions, velocities, accelerations, deltat, &curr_time, &Kinetic, boxdims, per_arr);
stepssinceoutput += 1;
if (stepssinceoutput == outputinterval) {
stepssinceoutput=0;
lj_pot(positions, &Potential);
lj_per_pot(positions, &Potential, per_arr, boxdims);
// writepositions (positions, outputfilename, boxdims);
writeenergies (Kinetic, curr_time, outputfilename, Potential);
}
}
//tidyupmathematicafile(positions, outputfilename, boxdims);
}