### 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>

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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291`` ``````#include #include #include /* 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-]^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; }``````
Thank you for sharing your article. Great efforts put it to find the list of articles which is very useful to know, Definitely will share the same to other forums.

https://www.credosystemz.com/courses/openstack-training-chennai/
Last edited on