Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Binder

Toy example

As the name “molecular dynamics” implies, we will be following the movement/dynamics of atoms and molecules. Conventionally, these movements are created by starting at a specific configuration at time 0, and then integrating Newton’s equations of motion over time. To do so, we need two central pieces of information:

  1. Given the current locations of particles x(t)\mathbf{x}(t) and forces f\mathbf{f} acting on the atoms, obtain the locations at the next time step x(t+1)\mathbf{x}(t+1)

  2. Given the current locations of particles x(t)\mathbf{x}(t), obtain the force f(x)\mathbf{f}(\mathbf{x})

We will explore the details of both aspects in detail in different sections during the course, but will start out with simple functions to create a toy simulation in this section. Let’s start with a few definitions and derivations.

System of point particles

We can describe NN point particles using their positions x(t)\mathbf{x}(t), velocities v(t)\mathbf{v}(t), acceleration a(t)\mathbf{a}(t), jerk j(t)\mathbf{j}(t), and forces f(t)\mathbf{f}(t):

x(t)=(x1,y1,z1,...,xN,yN,zN)\mathbf{x}(t) = (x_1,y_1,z_1, ..., x_N, y_N, z_N)
v(t)=dxdt\mathbf{v}(t) = \frac{\mathrm{d}\mathbf{x}}{\mathrm{d}t}
a(t)=dvdt=d2xdt2\mathbf{a}(t) = \frac{\mathrm{d}\mathbf{v}}{\mathrm{d}t}= \frac{\mathrm{d}^2\mathbf{x}}{\mathrm{d}t^2}
j(t)=dadt=d2vdt2=d3xdt3\mathbf{j}(t) = \frac{\mathrm{d}\mathbf{a}}{\mathrm{d}t}= \frac{\mathrm{d}^2\mathbf{v}}{\mathrm{d}t^2}= \frac{\mathrm{d}^3\mathbf{x}}{\mathrm{d}t^3}
f(t)=(f1x,f1y,f1z,...,fNx,fNy,fNz)\mathbf{f}(t) = (f_1^x,f_1^y,f_1^z, ..., f_N^x, f_N^y, f_N^z)

where fi(t)=miai\mathbf{f}_i(t) = m_i\mathbf{a}_i (from Newton’s second equation of motion force equals mass times acceleration). For conservative force fields, we can derive that the overall energy

E=Ek+Epot=12∑imi∣vi∣2+EpotE = E_k + E_\mathrm{pot} = \frac{1}{2}\sum_im_i|\mathbf{v}_i|^2 + E_\mathrm{pot}

is always conserved, and that the force is the negative gradient of the potential energy

f=−∇Epot\mathbf{f} =- \nabla E_\mathrm{pot}
fi=−∂Epot∂xi\mathbf{f}_i = -\frac{\partial E_\mathrm{pot}}{\partial \mathbf{x}_i}

To be able to calculate forces from positions, we therefore need to have a function for the potential energy (which we can then take the gradient of).

A simple potential: Soft-sphere potential

For this toy example, we will create an artificial potential function derived from a Lennard-Jones potential

Epot(rij)=4ϵ[(σrij)12−(σrij)6]E_\mathrm{pot}(r_{ij}) = 4\epsilon \bigl[\bigl(\frac{\sigma}{r_{ij}}\bigr)^{12} - \bigl(\frac{\sigma}{r_{ij}}\bigr)^{6} \bigr]

where ϵ\epsilon governs the strength of the interaction and σ\sigma defines the length scale, with the minimum (best) energy at 216σ2^{\frac{1}{6}}\sigma. We will shift the potential by ϵ\epsilon up, and then remove the attrative part of the potential:

Epot(rij<216σ)=4ϵ[(σrij)12−(σrij)6]+ϵE_\mathrm{pot}(r_{ij}<2^{\frac{1}{6}}\sigma) = 4\epsilon \bigl[\bigl(\frac{\sigma}{r_{ij}}\bigr)^{12} - \bigl(\frac{\sigma}{r_{ij}}\bigr)^{6} \bigr] + \epsilon
Epot(rij>=216σ)=0E_\mathrm{pot}(r_{ij}>=2^{\frac{1}{6}}\sigma) = 0

We thus end up with “soft spheres”, which repell each other in a soft manner (not like a hard, rigid repulsion), but do not attract each other at all. In this example, we will furthermore work in dimensionless units, where we choose σ\sigma, mm, ϵ\epsilon and ϵ/kB\epsilon/k_B as units of lenth, masss, energy, and temperature respectively. We thus get

Epot(rij<216)=4[(1rij)12−(1rij)6]+1E_\mathrm{pot}(r_{ij}<2^{\frac{1}{6}}) = 4 \bigl[\bigl(\frac{1}{r_{ij}}\bigr)^{12} - \bigl(\frac{1}{r_{ij}}\bigr)^{6} \bigr] + 1
Epot(rij>=216)=0E_\mathrm{pot}(r_{ij}>=2^{\frac{1}{6}}) = 0
<Figure size 640x480 with 1 Axes>

To run a simulation, we will put atoms on a regular grid and draw the velocities from a random distribution with magnitudes corresponding to a specified temperature:

[(-5.0, 5.0), (-5.0, 5.0)]
<Figure size 500x500 with 1 Axes>

In the above plot, we see the particles in blue, their velocities as black vectors. We now need to write a function for the soft-sphere potential, as well as the negative gradient of it (the force), which can be obtained analytically as

fij=−48[(1rij)14−0.5(1rij)8]rij\mathbf{f}_{ij} = -48 \bigl[\bigl(\frac{1}{r_{ij}}\bigr)^{14} - 0.5\bigl(\frac{1}{r_{ij}}\bigr)^{8} \bigr] \mathbf{r}_{ij}

We will use periodic boundary conditions. That means, instead of making hard boundaries that the particles bounce off of, they can exit the box and re-enter at the same time, similar to a piece of paper you roll up:

PBC.png

Specifically, this means that for a particle at the other side of the box (on any axis more than half of the box size away):

PBC2.png

we instead take the “image” of the particle on the other side:

PBC3.png

We can now compute forces, and plot them as red arrows (they are currently all zero since the particles are further apart than the cutoff):

[(-5.0, 5.0), (-5.0, 5.0)]
/opt/conda/lib/python3.12/site-packages/matplotlib/quiver.py:695: RuntimeWarning: divide by zero encountered in scalar divide
  length = a * (widthu_per_lenu / (self.scale * self.width))
/opt/conda/lib/python3.12/site-packages/matplotlib/quiver.py:695: RuntimeWarning: invalid value encountered in multiply
  length = a * (widthu_per_lenu / (self.scale * self.width))
<Figure size 500x500 with 1 Axes>

Let us now integrate Newton’s equations of motions, e.g. follow the velocity vector for a very short amount of time to update the positions and then obtain new velocities and forces. We will spend a whole section on integrators, so do not worry about the details here too much, but in principle an integrator works similarly to what is taught at high-school about e.g. throwing a ball: The current position, velocity, and acceleration make up the new position.

ball4.jpg
[(-5.0, 5.0), (-5.0, 5.0)]
<Figure size 500x500 with 1 Axes>

We can see that whereever the spheres of two particles overlap, that there is a strong force away from that overlap. Let us run the simulation for 100 more steps and plot the configuration after the 100 steps:

[(-5.0, 5.0), (-5.0, 5.0)]
<Figure size 500x500 with 1 Axes>

We have now departed from a grid-like structure into a more random, unordered structure. Since the particles cannot leave the box and do not like to overlap, they naturally form a dense packing in 2D, where each particle is roughly surrounded by six other particles.

Loading...
<Figure size 500x500 with 1 Axes>