Suppose we have a system of $N$ particles that interact via the Lennard-Jones potential $$V(r)=V_0\left[\left(\frac{r_0}{r}\right)^{12}-2\ \left(\frac{r_0}{r}\right)^{6}\right].$$ No other forces exist. The objective is to perform molecular dynamics simulations with this system.
I'm stuck on an important part. I do not know exactly how to calculate the forces on each atom. I know that to calculate the force on one atom, I must consider the interaction of it with all other atoms. However, I'm stuck as to how I put together these forces. Particularly, I don't know how to get the direction (sign) of the forces right.
Currently, my approach is to consider the $x$, $y$, and $z$ components of a given atom separately in calculating the forces/accelerations. I visit the interaction of one atom with all other atoms and compute the force in $x$, $y$, $z$. For each component, I add them all up, assuming this approach leaves me with a net acceleration in each direction at the end. I think I can use the final values of atm.ax
, atm.ay
, and atm.az
to update the position and velocity of the atom.
void computeForce(Atom atm){
for(int i=0;i<N_particles;i++){
if(i==atm.index)
continue;
else{
dx = PBC1D(atm.x - allAtm[i].x);
dy = PBC1D(atm.y - allAtm[i].y);
dz = PBC1D(atm.z - allAtm[i].z);
atm.ax += force(dx) / m;
atm.ay += force(dy) / m;
atm.az += force(dz) / m;
}
}
(Atom
is a user-defined class; PBC1D
allows for periodic boundary conditions---not important to my question here.)
Although my current approach makes sense to me, I have a feeling that it is incomplete and perhaps very inefficient. Can anyone help me understand if what I'm doing is correct or not? And, perhaps teach me better ways?