Skip to article frontmatterSkip to article content

A simple molecular dynamics code

1The main assignment

Molecular dynamics code integrate Newton’s equations to follow the dynamics of a systems made of components interacting with each other through a well-defined set of potentials. The mandatory part of this assignment is to write a molecular dynamics code that can be used to simulate a 3D Lennard-Jones system at a given temperature TT and density ρ=N/V\rho = N / V, where NN is the number of particles and V=L3V = L^3 is the volume of the simulation box. Use the algorithms discussed in class and implemented here. The code should

  1. Take as input at least the number of particles, temperature, density and the integration time step, Δt\Delta t.
  2. Initialise the system, for instance by using a function that places the particles on a cubic lattice, as done in the notebook linked above. Do not forget to also initialise the velocities as discussed during the lectures.
  3. Use the Velocity-Verlet integrator for the equations of motion.
  4. Take into account periodic boundary conditions (see below for details).
  5. Make it possible to optionally couple a thermostat to the system. The simplest to implement is the Andersen thermostat.

To make this assignment self-contained, I report here the expression for the LJ potential acting between two particles ii and jj:

VLJ(rij)=4ϵ[(σrij)12(σrij)6],V^{LJ}(r_{ij}) = 4 \epsilon \left[ \left( \frac{\sigma}{r_{ij}} \right)^{12} - \left( \frac{\sigma}{r_{ij}} \right)^{6} \right],

where σ is the particle diameter, ε is the depth of the attractive well, and rij=rij=rirjr_{ij} = |\vec r_{ij}| = |\vec r_i - \vec r_j| is the distance between ii and jj. As discussed in class, non-bonded interactions such as this one are cut off at some distance for performance reasons. A common choice for the LJ potential is rc=2.5σr_c = 2.5 \sigma, but you should use a larger value (e.g. rc=3.5σr_c = 3.5 \sigma to ensure that the correct scaling of the energy fluctuations is observed). When computing the energy (for instance to compare yours results with the values reported below to test your code) don’t forget to also shift the potential: the interaction energy for the (i,j)(i, j) pair should be:

Eij=VLJ(rij)VLJ(rc).E_{ij} = V^{LJ}(r_{ij}) - V^{LJ}(r_c).

By taking the gradient of the expression above with respect to ri\vec r_i and changing the sign we obtain the force acting on ii due to jj, which is

FiLJ(rij)=24ϵr^ijrij[2(σrij)12(σrij)6],\vec F_i^{LJ}(r_{ij}) = 24 \epsilon\frac{\hat r_{ij}}{r_{ij}} \left[ 2 \left( \frac{\sigma}{r_{ij}} \right)^{12} - \left( \frac{\sigma}{r_{ij}} \right)^6\right],

where r^ij=rij/rij\hat r_{ij} = \vec r_{ij} / r_{ij}. As a consequence of Newton’s third law, FjLJ(rij)=FiLJ(rij) \vec F_j^{LJ}(r_{ij}) = -\vec F_i^{LJ}(r_{ij}).

My advice is to make use of reduced units: the particle diameter is σ, the depth of the LJ well is ε, and the mass of a particle is mm, so that time is measured in units of t0=σm/ϵt_0 = \sigma \sqrt{m / \epsilon}.

The documentation accompanying the code should contain a plot that shows that the fluctuations of the total energy is proportional to Δt2\Delta t^2[1].

2Possible extensions

  1. Add neighbour lists to speed up computation.
  2. Add a routine to calculate the pressure of the system. See below for values you can use to check your code.
  3. Extend your code to also support Lennard-Jones dumbbells. Now your basic particle is made of two spheres connected by a bonded interaction. You can check that your code works by comparing the pressure as a function of temperature and density to Fig. 2 of this paper[2], where the bonded interaction acting between the two spheres that compose a dumbbell is Vbond=k(rσ)2V_{\rm bond} = k(r - \sigma)^2, with k=3000ϵ/σ3k = 3000 \epsilon / \sigma^3.

3Additional details

3.1Reference energy data

First of all, check that the average kinetic energy per particle is compatible with the equipartition theorem, i.e. that uk=Uk/N1.5kBT\langle u_k \rangle = \langle U_k \rangle / N \approx 1.5 k_B T.

If the first test is passed, here I report the average and standard deviation of the potential energy per particle, u=U/N\langle u \rangle = \langle U \rangle / N, of some systems you can use to check that your code works. This data has been generated by running simulations of systems composed of N=100N = 100 particles for 106 time steps, with time step Δt=0.001\Delta t = 0.001, cut-off rcr_c, and computing the energy every 1000 time steps.

TT [ϵ/kB\epsilon / k_B]ρ [1/σ31 / \sigma^3]u\langle u \rangle [ε]Std. dev. [ε]
1.50.4-2.240.09
1.50.6-3.320.10
2.00.4-2.100.10
2.00.6-3.110.13

You should not expect to match exactly these numbers, but if you run for the same number of time steps (which could take several minutes, or even tens of minutes, depending on your implementation), you should not observe differences larger than a few percent.

3.2Periodic boundary conditions

Applying the periodic boundary conditions in a cubic box with side LL means that the distance between two particles with coordinates ri\vec{r}_i and rj\vec{r}_j is

r=(r2r1)round(r2r1L)L,\vec{r} = (\vec{r_2} - \vec{r_1}) - {\rm round}\left(\frac{\vec{r_2} - \vec{r_1}}{L}\right)L,

where round()\rm round() is a function that rounds its argument to the nearest integer.

3.3Pressure in a Lennard-Jones system

There are many (mostly old) papers dealing with estimating the equation of state of a Lennard-Jones fluid. In most of them, the pressure computed in an MD simulation is corrected by the following term, which takes into account the truncated tail of the potential in a continuous way:

ΔP=329ρ2((σrc)932(σrc)3).\Delta P = \frac{32}{9} \rho^2 \left( \left(\frac{\sigma}{r_c}\right)^{9} - \frac{3}{2} \left( \frac{\sigma}{r_c}\right)^{3} \right).

With this in mind, you can try to evaluate the pressure, correct it by applying the shift of Eq. (5), and compare the results with those reported in Johnson et al. (1993). However, note that in the paper a large cut-off (rc=4.0σr_c = 4.0\, \sigma) was used.

If you feel that your code is not fast enough, you can use the following values, obtained with ρσ3=0.60\rho\sigma^3 = 0.60, rc=2.5σr_c = 2.5 \, \sigma, Δt=0.001t0\Delta t = 0.001 \, t_0 and simulations of 106 time steps:

TT [ϵ/kB\epsilon / k_B]PP [kBT/σ3k_B T / \sigma^3]PcorrP_{\rm corr}[3] [kBT/σ3k_B T / \sigma^3]
2.02.21.8
3.04.03.6
4.05.75.3
Footnotes
  1. This will be true only if rcr_c is sufficiently large (e.g. 3.5σ3.5 \sigma), and Δt\Delta t is not too large: be careful.

  2. In the figure, the packing fraction η is the density multiplied by the volume of a dumbbell, e.g. η=ρπσ3/3\eta = \rho \pi \sigma^3 / 3, where σ is the diameter of a Lennard-Jones sphere.

  3. The pressure corrected with Eq. (5).

References
  1. Venkatareddy, N., Lin, S.-T., & Maiti, P. K. (2023). Phase behavior of active and passive dumbbells. Physical Review E, 107(3). 10.1103/physreve.107.034607
  2. Johnson, J. K., Zollweg, J. A., & Gubbins, K. E. (1993). The Lennard-Jones equation of state revisited. Molecular Physics, 78(3), 591–618. 10.1080/00268979300100411