Lennard Jones

The code (C language) is about molecular dynamics using the potential of Lennard Jones. The code is 100% working but I want to understand certain details: 1) why on line 67 appears 1.0e-6? 2) Why add fij.dx to index i? Why is it subtracted fij.dx from index j (lines 142 to 147)? How did you get this formula? 3) Line 186: where does the 0.9999999 come from and why is it not inside the cubic root? 4) What kind of Verlet algorithm is it? (classic or speed) Can explain the Verlet method?



#include <stdlib.h>
#include <stdio.h>
#include <math.h>

/* propriedades do sistema */
const double rcp = 2.5; /* raio de corte */
int N; /* numero de particulas */
double rho; /* densidade do sistema */
double L; /* aresta da caixa*/
double dt; /* duracao de um passo de tempo */
double runtime; /* quanto tempo demora a correr */
long seed; /* semente do gerador de numeros aleatorios */
double K; /* energia cinetica */
double U; /* energia potencial */
double H; /* energia total */
double T; /* temperatura cinetica */

/* estrutura para as propriedades de um atomo */
struct Atom
{
double rx, ry, rz; /* posicao */
double px, py, pz; /* momento */
double fx, fy, fz; /* forca */
};

/* funcao para configurar o cubo */
double latticex, latticey, latticez;
void makeLatticePosition(double a)
{
static int i = 0;
static int j = 0;
static int k = 0;
latticex = i*a - 0.5*L;
latticey = j*a - 0.5*L;
latticez = k*a - 0.5*L;
i = i + 1;
if ( i*a > L - 1.0e-6 )
{
i = 0;
j = j + 1;

if ( j*a > L - 1.0e-6 )
{
j = 0;
k = k + 1;

if ( k*a > L - 1.0e-6 )
{
i = 0;
j = 0;
k = 0;
}
}
}
}

double makePeriodic(double u)
{
while ( u < -0.5*L )
{
u = u + L;
}

while ( u >= 0.5*L )
{
u = u - L;
}

return u;
}

void computeForces(struct Atom atoms[])
{
int i, j;
double dx, dy, dz;
double r, r2, r2i, r6i;
double fij;
double eij;

U = 0;

for ( i = 0; i < N; i = i + 1 )
{
atoms[i].fx = 0;
atoms[i].fy = 0;
atoms[i].fz = 0;
}

for ( i = 0; i < N-1; i = i + 1 )
{
for ( j = i+1; j < N; j = j + 1 )
{
dx = makePeriodic(atoms[i].rx - atoms[j].rx);
dy = makePeriodic(atoms[i].ry - atoms[j].ry);
dz = makePeriodic(atoms[i].rz - atoms[j].rz);
r2 = dx*dx + dy*dy + dz*dz;

if ( r2 < rcp*rcp )
{
r2i = 1/r2;
r6i = r2i*r2i*r2i;
fij = 48*r2i*r6i*(r6i-0.5);
eij = 4*r6i*(r6i-1);

atoms[i].fx = atoms[i].fx + fij*dx;
atoms[i].fy = atoms[i].fy + fij*dy;
atoms[i].fz = atoms[i].fz + fij*dz;
atoms[j].fx = atoms[j].fx - fij*dx;
atoms[j].fy = atoms[j].fy - fij*dy;
atoms[j].fz = atoms[j].fz - fij*dz;
U = U + eij;
}
}
}
}

double gaussian()
{
static int have = 0;
static double x2;
double fac, y1, y2, x1;

if ( have == 1 )
{
have = 0;
return x2;
}
else
{
y1 = drand48();
y2 = drand48();
fac = sqrt(-2*log(y1));
have = 1;
x1 = fac*sin(2*M_PI*y2);
x2 = fac*cos(2*M_PI*y2);
return x1;
}
}

void initialize(struct Atom atoms[])
{
double scale, a;
int i;

/* gerar posicoes */
a = L/(int)(cbrt(N)+0.99999999999);

for ( i = 0; i < N; i = i + 1 )
{
makeLatticePosition(a);
atoms[i].rx = latticex;
atoms[i].ry = latticey;
atoms[i].rz = latticez;
}

srand48(seed);
scale = sqrt(T);
K = 0;

for ( i = 0; i < N; i = i + 1 )
{
atoms[i].px = scale*gaussian();
atoms[i].py = scale*gaussian();
atoms[i].pz = scale*gaussian();
K = K
+ atoms[i].px*atoms[i].px
+ atoms[i].py*atoms[i].py
+ atoms[i].pz*atoms[i].pz;
}

T = K/(3*N);
K = K/2;

computeForces(atoms);

H = U + K;

printf("# time E U K T <[H-<H>]^2>\n");
}

void integrateStep(struct Atom atoms[])
{
int i;

for ( i = 0; i < N; i = i + 1 )
{
atoms[i].px = atoms[i].px + 0.5*dt*atoms[i].fx;
atoms[i].py = atoms[i].py + 0.5*dt*atoms[i].fy;
atoms[i].pz = atoms[i].pz + 0.5*dt*atoms[i].fz;
}


FILE *outfile;

outfile=fopen("out.txt","a");

for ( i = 0; i < N; i = i + 1 )
{
atoms[i].rx = atoms[i].rx + dt*atoms[i].px;
atoms[i].ry = atoms[i].ry + dt*atoms[i].py;
atoms[i].rz = atoms[i].rz + dt*atoms[i].pz;

fprintf(outfile, "%s %8.6f %8.6f %8.6f\n",
"H", atoms[i].rx, atoms[i].ry, atoms[i].rz);
}

fclose(outfile);

computeForces(atoms);

K = 0;

for ( i = 0; i < N; i = i + 1 )
{
atoms[i].px = atoms[i].px + 0.5*dt*atoms[i].fx;
atoms[i].py = atoms[i].py + 0.5*dt*atoms[i].fy;
atoms[i].pz = atoms[i].pz + 0.5*dt*atoms[i].fz;
K = K
+ atoms[i].px*atoms[i].px
+ atoms[i].py*atoms[i].py
+ atoms[i].pz*atoms[i].pz;
}

T = K/(3*N);
K = K/2;
H = U + K;
}

void run()
{
struct Atom atoms[N];
int numSteps = (int)(runtime/dt + 0.5);
int count;
int numPoints = 0;
double sumH = 0;
double sumH2 = 0;
double avgH, avgH2, fluctH;

initialize(atoms);

for ( count = 0; count < numSteps; count = count + 1 )
{
integrateStep(atoms);

sumH = sumH + H;
sumH2 = sumH2 + H*H;
numPoints = numPoints + 1;

avgH = sumH/numPoints;
avgH2 = sumH2/numPoints;
fluctH = sqrt(avgH2 - avgH*avgH);

printf("%8.6f %8.6f %8.6f %8.6f %8.6f %8.6f\n",
count*dt, H/N, U/N, K/N, T, fluctH/N);
}
}

int main()
{
printf("#Enter number of particles (N): ");
fflush(stdout);
scanf("%d", &N);

printf("#Enter density (rho): "); fflush(stdout);
scanf("%lf", &rho);

printf("#Enter initial temperature (T): ");
fflush(stdout);
scanf("%lf", &T);

printf("#Enter runtime: ");
fflush(stdout);
scanf("%lf", &runtime);

printf("#Enter time step (dt): ");
fflush(stdout);
scanf("%lf", &dt);

printf("#Enter random seed: ");
fflush(stdout);
scanf("%ld", &seed);

L = cbrt(N/rho);

printf("\n#N=%d L=%lf T=%lf runtime=%lf dt=%lf seed=%ld",
N, L, T, runtime, dt, seed);

run();

return 0;
}
Last edited on
I've reposted your code with code tags, which you should do from now on if possible. The line numbers you list don't match the code you describe. Do you mind posing your questions with the line numbers shown here please.
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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

/* propriedades do sistema */
const double rcp = 2.5; /* raio de corte */
int N; /* numero de particulas */
double rho; /* densidade do sistema */
double L; /* aresta da caixa*/
double dt; /* duracao de um passo de tempo */
double runtime; /* quanto tempo demora a correr */
long seed; /* semente do gerador de numeros aleatorios */
double K; /* energia cinetica */
double U; /* energia potencial */
double H; /* energia total */
double T; /* temperatura cinetica */

/* estrutura para as propriedades de um atomo */
struct Atom
{
	double rx, ry, rz; /* posicao */
	double px, py, pz; /* momento */
	double fx, fy, fz; /* forca */
};

/* funcao para configurar o cubo */
double latticex, latticey, latticez;
void makeLatticePosition(double a)
{
	static int i = 0;
	static int j = 0;
	static int k = 0;
	latticex = i*a - 0.5*L;
	latticey = j*a - 0.5*L;
	latticez = k*a - 0.5*L;
	i = i + 1;
	if ( i*a > L - 1.0e-6 )
	{
		i = 0;
		j = j + 1;

		if ( j*a > L - 1.0e-6 )
		{
			j = 0;
			k = k + 1;

			if ( k*a > L - 1.0e-6 )
			{
				i = 0;
				j = 0;
				k = 0;
			}
		}
	}
}

double makePeriodic(double u)
{
	while ( u < -0.5*L )
	{
		u = u + L;
	}

	while ( u >= 0.5*L )
	{
		u = u - L;
	}

	return u;
}

void computeForces(struct Atom atoms[])
{
	int i, j;
	double dx, dy, dz;
	double r, r2, r2i, r6i;
	double fij;
	double eij;

	U = 0;

	for ( i = 0; i < N; i = i + 1 )
	{
		atoms[i].fx = 0;
		atoms[i].fy = 0;
		atoms[i].fz = 0;
	}

	for ( i = 0; i < N-1; i = i + 1 )
	{
		for ( j = i+1; j < N; j = j + 1 )
		{
			dx = makePeriodic(atoms[i].rx - atoms[j].rx);
			dy = makePeriodic(atoms[i].ry - atoms[j].ry);
			dz = makePeriodic(atoms[i].rz - atoms[j].rz);
			r2 = dx*dx + dy*dy + dz*dz;

			if ( r2 < rcp*rcp )
			{
				r2i = 1/r2;
				r6i = r2i*r2i*r2i;
				fij = 48*r2i*r6i*(r6i-0.5);
				eij = 4*r6i*(r6i-1);

				atoms[i].fx = atoms[i].fx + fij*dx;
				atoms[i].fy = atoms[i].fy + fij*dy;
				atoms[i].fz = atoms[i].fz + fij*dz;
				atoms[j].fx = atoms[j].fx - fij*dx;
				atoms[j].fy = atoms[j].fy - fij*dy;
				atoms[j].fz = atoms[j].fz - fij*dz;
				U = U + eij;
			}
		}
	}
}

double gaussian()
{
	static int have = 0;
	static double x2;
	double fac, y1, y2, x1;

	if ( have == 1 )
	{
		have = 0;
		return x2;
	}
	else
	{
		y1 = drand48();
		y2 = drand48();
		fac = sqrt(-2*log(y1));
		have = 1;
		x1 = fac*sin(2*M_PI*y2);
		x2 = fac*cos(2*M_PI*y2);
		return x1;
	}
}

void initialize(struct Atom atoms[])
{
	double scale, a;
	int i;

	/* gerar posicoes */
	a = L/(int)(cbrt(N)+0.99999999999);

	for ( i = 0; i < N; i = i + 1 )
	{
		makeLatticePosition(a);
		atoms[i].rx = latticex;
		atoms[i].ry = latticey;
		atoms[i].rz = latticez;
	}

	srand48(seed);
	scale = sqrt(T);
	K = 0;

	for ( i = 0; i < N; i = i + 1 )
	{
		atoms[i].px = scale*gaussian();
		atoms[i].py = scale*gaussian();
		atoms[i].pz = scale*gaussian();
		K = K
			+ atoms[i].px*atoms[i].px
			+ atoms[i].py*atoms[i].py
			+ atoms[i].pz*atoms[i].pz;
	}

	T = K/(3*N);
	K = K/2;

	computeForces(atoms);

	H = U + K;

	printf("# time E U K T <[H-<H>]^2>\n");
}

void integrateStep(struct Atom atoms[])
{
	int i;

	for ( i = 0; i < N; i = i + 1 )
	{
		atoms[i].px = atoms[i].px + 0.5*dt*atoms[i].fx;
		atoms[i].py = atoms[i].py + 0.5*dt*atoms[i].fy;
		atoms[i].pz = atoms[i].pz + 0.5*dt*atoms[i].fz;
	}


	FILE *outfile;

	outfile=fopen("out.txt","a");

	for ( i = 0; i < N; i = i + 1 )
	{
		atoms[i].rx = atoms[i].rx + dt*atoms[i].px;
		atoms[i].ry = atoms[i].ry + dt*atoms[i].py;
		atoms[i].rz = atoms[i].rz + dt*atoms[i].pz;

		fprintf(outfile, "%s %8.6f %8.6f %8.6f\n",
		"H", atoms[i].rx, atoms[i].ry, atoms[i].rz);
	}

	fclose(outfile);

	computeForces(atoms);

	K = 0;

	for ( i = 0; i < N; i = i + 1 )
	{
		atoms[i].px = atoms[i].px + 0.5*dt*atoms[i].fx;
		atoms[i].py = atoms[i].py + 0.5*dt*atoms[i].fy;
		atoms[i].pz = atoms[i].pz + 0.5*dt*atoms[i].fz;
		K = K
			+ atoms[i].px*atoms[i].px
			+ atoms[i].py*atoms[i].py
			+ atoms[i].pz*atoms[i].pz;
	}

	T = K/(3*N);
	K = K/2;
	H = U + K;
}

void run()
{
	struct Atom atoms[N];
	int numSteps = (int)(runtime/dt + 0.5);
	int count;
	int numPoints = 0;
	double sumH = 0;
	double sumH2 = 0;
	double avgH, avgH2, fluctH;

	initialize(atoms);

	for ( count = 0; count < numSteps; count = count + 1 )
	{
		integrateStep(atoms);

		sumH = sumH + H;
		sumH2 = sumH2 + H*H;
		numPoints = numPoints + 1;

		avgH = sumH/numPoints;
		avgH2 = sumH2/numPoints;
		fluctH = sqrt(avgH2 - avgH*avgH);

		printf("%8.6f %8.6f %8.6f %8.6f %8.6f %8.6f\n",
		count*dt, H/N, U/N, K/N, T, fluctH/N);
	}
}

int main()
{
	printf("#Enter number of particles (N): ");
	fflush(stdout);
	scanf("%d", &N);

	printf("#Enter density (rho): "); fflush(stdout);
	scanf("%lf", &rho);

	printf("#Enter initial temperature (T): ");
	fflush(stdout);
	scanf("%lf", &T);

	printf("#Enter runtime: ");
	fflush(stdout);
	scanf("%lf", &runtime);

	printf("#Enter time step (dt): ");
	fflush(stdout);
	scanf("%lf", &dt);

	printf("#Enter random seed: ");
	fflush(stdout);
	scanf("%ld", &seed);

	L = cbrt(N/rho);

	printf("\n#N=%d L=%lf T=%lf runtime=%lf dt=%lf seed=%ld",
	N, L, T, runtime, dt, seed);

	run();

	return 0;
}
3) Line 186: where does the 0.9999999 come from
1
2
3
4
int N; /* numero de particulas */
double L; /* aresta da caixa*/

a = L / (int)( cbrt(N) + 0.99999999999 );

What if cbrt(1) is slightly less than 1?

In that case (int)cbrt(N) is 0 and L/0 is bad.

However, in that case (int)( cbrt(N) + 0.99999999999 ) equals 1.
Topic archived. No new replies allowed.