Week 3
Better time propagation¶
The normal Euler methods for integrating ordinary differential equations (ODEs) have a major downside for the molecular dynamics simulations we are doing: they do not conserve the total energy of the system very well. This is a problem, as our goal is to simulate a system with conserved energy (the microcanonical ensemble). We'll therefore derive and move on to a better algorithm.
The Verlet algorithm¶
The algorithm we will focus on was introduced at the very beginning of the field of molecular dynamics (see the classic paper of Verlet from 1967). It directly makes use of the fact that we have a second order ODE:
Note: in the above we are we working in dimensionless units and in addition we do not explicitly write .
For this equation we can easily derive a finite time-step algorithm by using the Taylor expansion of :
Adding those two equations together and using , we find
However, in the first time-step we have no information about . We therefore need to replace it by .
In this version of the Verlet algorithm, only the positions enter. Velocities (that we need for the kinetic energy!) have to be estimated from
Velocity-Verlet¶
The algorithm can be slightly modified to directly use velocities. For that we take the Taylor expansion of and replace and to arrive at
Additionally, we need now an equation for . We do a Taylor expansion for (you see, in the end it's always Taylor expansion!)
We know that but we don't know . So for this we Taylor expand (again!!)
from which we find . Inserting this back we finally find the velocit-Verlet algorithm:
Case study: the 2 body gravitational problem¶
We illustrate the difference between the two integration methods studying the simple case of a (3D) 2-body gravitational problem. Specifically, that of the earth and the sun. In this case, it turns out that using Euler's method generally results in the earth slowly spiralling into the sun: As we know, this is not a very good representation of reality. Moreover, we see that something is off in the total energy of the system: even though energy is supposed to be conserved, we see fluctuations in the total energy that are large on the scale of the individual energies in the system. This is a clear sign of something being wrong.
If we instead implement the velocity-Verlet algorithm (using the same timestep as in the Euler case!) we find a much more reassuring picture: Here the earth is in a stable orbit around the sun, and the fluctuations in the total energy of the system are now two orders of magnitude lower, much smaller than the individual energy contributions.
If you would like to further explore this example, the notebook that generates these figures can be downloaded here. Note that while in principle it will run for N bodies, this piece of code is not well suited for a molecular dynamics simulation! The usage of a class is debatable even when particles are not anonymous and identical, there are many nested loops, and various other parts of the code remain unoptimized. Try not to make these same mistakes!
Why are these algorithms actually better?¶
The difference to the Euler methods is actually very subtle, if you look for example at the velocity-Verlet algorithm. It turns out that these methods are part of a class of so-called symplectic integrators that are particularly suited for the time-evolution of Hamiltonian systems (which is the case for classical mechanics as we use here). If you want to learn more, we refer you to Jos' book or Wikipedia
Milestones
- Extend your code to more than 2 particles.
- Implement the velocity-Verlet algorithm.
- Investigate the conservation of energy in the system. Plot the evolution of the kinetic and potential energy, as well as their sum.
- Compare the results of energy conservation using the Euler and Verlet algorithms.
- Make an initial attempt to structure your code properly.
After week 3, you should have a code that simulates, for given initial positions and velocities, the classical motion of interacting particles with periodic boundary conditions. The simulation should conserve energy.