# Ensembles and time integration#

## Choice of ensemble#

When simulating a system using MD, the ensemble of that system must be considered, *i.e.*, the states in which it is possible to find the system and how probable these are.

### Microcanonical ensemble (*NVE*)#

Of the different ensembles, the microcanonical is the simplest to simulate. Also known as the *NVE* ensemble, it represents a completely closed system, in which the number of particles, *N*, volume, *V*, and total energy, *E*, are kept constant. In simulation terms, this just means that the integration is left on its own from the starting position, with energy being converted back and forth between potential and kinetic form while the total sum remains constant. The main problem for *NVE* simulations is that it is hard to maintain energy conservation when using numerical integration methods. As can be seen in the examples in the previous section, the accumulation of numerical errors causes a drift in energy proportional to the time step.

### Canonical ensemble (*NVT*)#

An ensemble that is of more practical use for realistic simulations is the canonical, or *NVT*, ensemble. In this case, the temperature is kept constant instead of the energy, emulating a system in thermal equilibrium with a heat bath. For the simulation, this means two things: First, the average temperature of the system should be constant. Note that this does not mean that the kinetic energy, the time average of which the temperature is proportional to, is constant at all times, just that it varies around the correct value. Second, how it varies should be determined by the fact that the velocities of the individual particles follow the Maxwell–Boltzmann distribution. The task of maintaining these two criteria in an MD simulation is done by a thermostat.

#### Thermostat#

The most basic thermostat is a simple rescaling of the system velocities by a factor

where \(T\) is the current temperature and \(T_0\) is the desired one. While this fulfils the criterion that the temperature should be constant, since the total kinetic energy is kept constant between steps, this also means that the second criterion is not fulfilled. An improved version of this method comes in the form of the Berendsen thermostat, which also includes a coupling parameter, \(\tau\), determining the rate of heat transfer between the heat bath and the system. The rescaling factor then becomes

The coupling parameter needs to be chosen with care, as a \(\tau\) that is equal to the time step, \(\Delta t\), just reproduces the velocity rescaling thermostat and a too high \(\tau\) makes the coupling too weak, with the limit \(\tau \rightarrow \infty\) instead describing the microcanonical ensemble. So while the Berendsen thermostat does not describe a true canonical ensemble, there are coupling parameters between \(\Delta t\) and infinity that give a suitably close approximation for larger systems. As the Berendsen thermostat is quite quick to converge to the correct temperature, it is often used for an initial equilibration, after which a true canonical thermostat is used.

Examples of such thermostats are found in the Andersen and Nosé–Hoover (see here and here) thermostats. The Andersen thermostat takes a stochastic approach, replacing the velocities of a selection of atoms at each step by new velocities taken from the Maxwell–Boltzmann distribution, simulating random collisions. The frequency at which velocities are replaced becomes the coupling parameter of the thermostat, determining the speed at which it converges to the correct temperature. The Nosé–Hoover thermostat, on the other hand, introduces an artificial particle, with a mass and velocity, representing the heat bath. The extended system, containing both the real and artificial systems, is described by a microcanonical ensemble, but due to the coupling between the two, the real system becomes canonical.

### Isothermal-isobaric ensemble (*NPT*)#

In the isothermal-isobaric, or *NPT*, ensemble, the pressure of the system is kept constant instead of the volume. This means that in addition to a thermostat regulating the temperature, a barostat is required to do the same for the pressure.

#### Barostat#

For the three mentioned thermostats, there are corresponding barostats. In the case of the Berendsen barostat, the method is much the same as for the thermostat. Instead of velocities, however, it is the size of the periodic box containing the system and the coordinates within it that are scaled. If the pressure is too low, the box size is decreased and all atoms within it are brought closer to each other, and for a pressure that is too high the opposite is done. The Andersen and Nosé–Hoover barostats adopt a similar approach to the Nosé–Hoover thermostat, creating a coupling to an artificial system. This system acts as a piston, with artificial mass determining the strength of the coupling, trying to compress the real system.

## Time integration#

As described in previous chapters, the potential energy of a system can be obtained for a given set of nuclear positions either through QM methods or through more approximate means such as molecular dynamics (MD). Given this knowledge, it is now possible to make the atoms move. Following the Born–Oppenheimer approximation, the behaviour of the nuclei should be described using MD, but such calculations are too resource demanding for any larger systems. As such, this work deals only with classical MD, in which the nuclei move according to Newton’s equations.

### Euler integration#

The first thing that is needed is the acceleration of each nucleus, obtained from the derivative of the potential energy function with respect to the nuclear coordinates. For the force field energy, a simple analytical function of \(3N\) coordinates, this is easily done, but for the QM energy things are more complicated, possibly requiring numerical differentiation. Based on these derivatives, the acceleration for atom \(j\) can easily be found using Newton’s second law:

where \(m_j\) is the mass of atom \(j\) and \(\mathbf{F}_j\) are the forces acting on it. If the atomic coordinates at a given time \(t = t_i\) are \(\mathbf{R}(t_i)\), the positions after a small time step, \(\Delta t\), can be expressed as a Taylor expansion around \(t = t_i\):

where each partial derivative is evaluated at \(t = t_i\). Given a fixed set of time steps, each of length \(\Delta t\), a series of positions are obtained:

where \(\mathbf{v}_i\), \(\mathbf{a}_i\) and \(\mathbf{j}_i\) are the velocities, acceleration and jerk of time step \(i\), respectively. Truncating this to the first order gives

which can be differentiated with respect to time to obtain the velocities for step \(i+1\):

These two equations, together with the expression for the acceleration make up the Euler method. For each step, the acceleration is obtained from Newton’s law, then the velocities are calculated with above expression and finally a new set of coordinates \(\mathbf{R}_{i+1}\) before the process is repeated, step by step. This is an extremely simple method, with a local truncation error in order of \(\Delta t^2\) for each step and a global error of first order for the trajectory as a whole. This can be seen from the fact that a trajectory of length \(T\) requires \(M = T / \Delta t\) steps. For each step, an error of order \(\Delta t^2\) is added, leading to a total error of \(T/ \Delta t \hspace{1mm} \mathcal{O}(\Delta t^2) = \mathcal{O}(\Delta t)\), *i.e.* first order. A simple example of integration using the Euler method is shown here:

### Verlet integration#

A better choice of integrator can be found in the Verlet algorithm. In this algorithm, the coordinates of the previous time step, \(\mathbf{R}_{i-1}\), are required, which can be obtained by replacing \(\Delta t\) with \(-\Delta t\) in the expression for \(\mathbf{R}_{i+1}\):

If MD equations are added together, the velocity and jerk terms cancel and the expression for \(\mathbf{R}_{i+1}\) can be written as

Neglecting higher order terms and using the acceleration from Newton’s second law, this expression makes up the Verlet algorithm, with a local truncation error in the order of \(\Delta t^4\). The global error, however, is of second order, due to the presence of two previous coordinates in the expression. THe improvement over the Euler method is clear:

### Velocity Verlet#

In any simulation that deals with temperature, the kinetic energy of the system becomes a factor, most commonly dealt with through the atomic velocities. As the Verlet algorithm neither calculates nor uses these velocities, measurements and alterations of the temperature become difficult. For this reason, the velocity Verlet algorithm was created. This uses the Taylor expansion of the \(\mathbf{R}_{i+1}\) expression up to the second order for the coordinates:

Differentiating this expression with respect to time gives the expression for the velocity of the next step:

An expression for \(\mathbf{j}_i\) can be found by differentiating once more and truncating after the second term:

This can be rearranged into

which can be inserted in the expression for \(\mathbf{v}_{i+1}\), resulting in

This make up the velocity Verlet algorithm. First, we can calculate the coordinates of the next step, which are used to calculate the acceleration of that step. Finally, the velocities are calculated from above, completing the step.

As is evident from these equations, the local truncation error of the velocity Verlet algorithm is in the order of \(\mathcal{O}(\Delta t^3)\), both for coordinates and velocities, but the global error is of second order, same as for the standard Verlet algorithm. This can be seen in the velocity Verlet illustration: