378112531e4d14406e879f7909f74105be3c1a7c
[alexxy/gromacs.git] / docs / reference-manual / algorithms / molecular-dynamics.rst
1 .. _md:
2
3 Molecular Dynamics
4 ------------------
5
6 .. _gmx-md-scheme:
7
8 **THE GLOBAL MD ALGORITHM**
9
10 --------------
11
12
13 | **1. Input initial conditions**
14 | Potential interaction :math:`V` as a function of atom positions
15 | Positions :math:`\mathbf{r}` of all atoms in the system
16 | Velocities :math:`\mathbf{v}` of all atoms in the system
17 | :math:`\Downarrow`
18
19 --------------
20
21
22 | **repeat 2,3,4** for the required number of steps:
23
24 --------------
25
26
27 | **2. Compute forces**
28 | The force on any atom
29 | :math:`\mathbf{F}_i = - \displaystyle\frac{\partial V}{\partial \mathbf{r}_i}`
30 | is computed by calculating the force between non-bonded atom pairs:
31 | :math:`\mathbf{F}_i = \sum_j \mathbf{F}_{ij}`
32 | plus the forces due to bonded interactions (which may depend on 1, 2,
33   3, or 4 atoms), plus restraining and/or external forces.
34 | The potential and kinetic energies and the pressure tensor may be
35   computed.
36 | :math:`\Downarrow`
37 | **3. Update configuration**
38 | The movement of the atoms is simulated by numerically solving Newton’s
39   equations of motion
40 | :math:`\displaystyle \frac {{\mbox{d}}^2\mathbf{r}_i}{{\mbox{d}}t^2} = \frac{\mathbf{F}_i}{m_i}`
41 | or
42 | :math:`\displaystyle   \frac{{\mbox{d}}\mathbf{r}_i}{{\mbox{d}}t} = \mathbf{v}_i ; \;\;   \frac{{\mbox{d}}\mathbf{v}_i}{{\mbox{d}}t} = \frac{\mathbf{F}_i}{m_i}` 
43 | :math:`\Downarrow`
44 | **4.** if required: **Output step**
45 | write positions, velocities, energies, temperature, pressure, etc.
46
47 A global flow scheme for MD is given above.
48 Each MD or EM run requires as input
49 a set of initial coordinates and – optionally – initial velocities of
50 all particles involved. This chapter does not describe how these are
51 obtained; for the setup of an actual MD run check the :ref:`user guide`
52 in Sections :ref:`gmx-sysprep` and :ref:`gmx-getting-started`.
53
54 Initial conditions
55 ~~~~~~~~~~~~~~~~~~
56
57 Topology and force field
58 ^^^^^^^^^^^^^^^^^^^^^^^^
59
60 The system topology, including a description of the force field, must be
61 read in. Force fields and topologies are described in chapter :ref:`ff`
62 and :ref:`top`, respectively. All this information is static; it is never
63 modified during the run.
64
65 Coordinates and velocities
66 ^^^^^^^^^^^^^^^^^^^^^^^^^^
67
68 .. _fig-maxwell:
69
70 .. figure:: plots/maxwell.*
71    :width: 8.00000cm
72
73    A Maxwell-Boltzmann velocity distribution, generated from
74    random numbers.
75
76 Then, before a run starts, the box size and the coordinates and
77 velocities of all particles are required. The box size and shape is
78 determined by three vectors (nine numbers)
79 :math:`\mathbf{b}_1, \mathbf{b}_2, \mathbf{b}_3`,
80 which represent the three basis vectors of the periodic box.
81
82 If the run starts at :math:`t=t_0`, the coordinates at :math:`t=t_0`
83 must be known. The *leap-frog algorithm*, the default algorithm used to
84 update the time step with :math:`{{\Delta t}}` (see :ref:`update`),
85 also requires that the velocities at
86 :math:`t=t_0 - {{\frac{1}{2}}{{\Delta t}}}` are known. If velocities are
87 not available, the program can generate initial atomic velocities
88 :math:`v_i, i=1\ldots 3N` with a Maxwell-Boltzmann distribution
89 (:numref:`Fig. %s <fig-maxwell>`) at a given absolute temperature
90 :math:`T`:
91
92 .. math:: p(v_i) = \sqrt{\frac{m_i}{2 \pi kT}}\exp\left(-\frac{m_i v_i^2}{2kT}\right)
93
94 where :math:`k` is Boltzmann’s constant (see chapter :ref:`defunits`). To
95 accomplish this, normally distributed random numbers are generated by
96 adding twelve random numbers :math:`R_k` in the range
97 :math:`0 \le R_k < 1` and subtracting 6.0 from their sum. The result is
98 then multiplied by the standard deviation of the velocity distribution
99 :math:`\sqrt{kT/m_i}`. Since the resulting total energy will not
100 correspond exactly to the required temperature :math:`T`, a correction
101 is made: first the center-of-mass motion is removed and then all
102 velocities are scaled so that the total energy corresponds exactly to
103 :math:`T` (see :eq:`eqn. %s <eqnET>`).
104
105 Center-of-mass motion
106 ^^^^^^^^^^^^^^^^^^^^^
107
108 The center-of-mass velocity is normally set to zero at every step; there
109 is (usually) no net external force acting on the system and the
110 center-of-mass velocity should remain constant. In practice, however,
111 the update algorithm introduces a very slow change in the center-of-mass
112 velocity, and therefore in the total kinetic energy of the system –
113 especially when temperature coupling is used. If such changes are not
114 quenched, an appreciable center-of-mass motion can develop in long runs,
115 and the temperature will be significantly misinterpreted. Something
116 similar may happen due to overall rotational motion, but only when an
117 isolated cluster is simulated. In periodic systems with filled boxes,
118 the overall rotational motion is coupled to other degrees of freedom and
119 does not cause such problems.
120
121 Neighbor searching
122 ~~~~~~~~~~~~~~~~~~
123
124 As mentioned in chapter :ref:`ff`, internal forces are either generated
125 from fixed (static) lists, or from dynamic lists. The latter consist of
126 non-bonded interactions between any pair of particles. When calculating
127 the non-bonded forces, it is convenient to have all particles in a
128 rectangular box. As shown in :numref:`Fig. %s <fig-pbc>`, it is possible to transform
129 a triclinic box into a rectangular box. The output coordinates are
130 always in a rectangular box, even when a dodecahedron or triclinic box
131 was used for the simulation. :eq:`Equation %s <eqnboxrot>` ensures that we can
132 reset particles in a rectangular box by first shifting them with box
133 vector :math:`{\bf c}`, then with :math:`{\bf b}` and finally with
134 :math:`{\bf a}`. Equations :eq:`%s <eqnboxshift2>`,
135 :eq:`%s <eqnphysicalrc>` and :eq:`%s <eqngridrc>`
136 ensure that we can find the 14 nearest triclinic images within a linear
137 combination that does not involve multiples of box vectors.
138
139 Pair lists generation
140 ^^^^^^^^^^^^^^^^^^^^^
141
142 The non-bonded pair forces need to be calculated only for those pairs
143 :math:`i,j` for which the distance :math:`r_{ij}` between :math:`i` and
144 the nearest image of :math:`j` is less than a given cut-off radius
145 :math:`R_c`. Some of the particle pairs that fulfill this criterion are
146 excluded, when their interaction is already fully accounted for by
147 bonded interactions. |Gromacs| employs a *pair list* that contains those
148 particle pairs for which non-bonded forces must be calculated. The pair
149 list contains particles :math:`i`, a displacement vector for particle
150 :math:`i`, and all particles :math:`j` that are within ``rlist`` of this
151 particular image of particle :math:`i`. The list is updated every
152 ``nstlist`` steps.
153
154 To make the neighbor list, all particles that are close (*i.e.* within
155 the neighbor list cut-off) to a given particle must be found. This
156 searching, usually called neighbor search (NS) or pair search, involves
157 periodic boundary conditions and determining the *image* (see
158 sec. :ref:`pbc`). The search algorithm is :math:`O(N)`, although a
159 simpler :math:`O(N^2)` algorithm is still available under some
160 conditions.
161
162 Cut-off schemes: group versus Verlet
163 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
164
165 From version 4.6, |Gromacs| supports two different cut-off scheme setups:
166 the original one based on particle groups and one using a Verlet buffer.
167 There are some important differences that affect results, performance
168 and feature support. The group scheme can be made to work (almost) like
169 the Verlet scheme, but this will lead to a decrease in performance. The
170 group scheme is especially fast for water molecules, which are abundant
171 in many simulations, but on the most recent x86 processors, this
172 advantage is negated by the better instruction-level parallelism
173 available in the Verlet-scheme implementation. The group scheme is
174 deprecated in version 5.0, and will be removed in a future version. For
175 practical details of choosing and setting up cut-off schemes, please see
176 the User Guide.
177
178 In the group scheme, a neighbor list is generated consisting of pairs of
179 groups of at least one particle. These groups were originally charge
180 groups (see sec. :ref:`chargegroup`), but with a proper treatment of
181 long-range electrostatics, performance in unbuffered simulations is
182 their only advantage. A pair of groups is put into the neighbor list
183 when their center of geometry is within the cut-off distance.
184 Interactions between all particle pairs (one from each charge group) are
185 calculated for a certain number of MD steps, until the neighbor list is
186 updated. This setup is efficient, as the neighbor search only checks
187 distance between charge-group pair, not particle pairs (saves a factor
188 of :math:`3 \times 3 = 9` with a three-particle water model) and the
189 non-bonded force kernels can be optimized for, say, a water molecule
190 “group”. Without explicit buffering, this setup leads to energy drift as
191 some particle pairs which are within the cut-off don’t interact and some
192 outside the cut-off do interact. This can be caused by
193
194 -  particles moving across the cut-off between neighbor search steps,
195    and/or
196
197 -  for charge groups consisting of more than one particle, particle
198    pairs moving in/out of the cut-off when their charge group center of
199    geometry distance is outside/inside of the cut-off.
200
201 Explicitly adding a buffer to the neighbor list will remove such
202 artifacts, but this comes at a high computational cost. How severe the
203 artifacts are depends on the system, the properties in which you are
204 interested, and the cut-off setup.
205
206 The Verlet cut-off scheme uses a buffered pair list by default. It also
207 uses clusters of particles, but these are not static as in the group
208 scheme. Rather, the clusters are defined spatially and consist of 4 or 8
209 particles, which is convenient for stream computing, using e.g. SSE, AVX
210 or CUDA on GPUs. At neighbor search steps, a pair list is created with a
211 Verlet buffer, ie. the pair-list cut-off is larger than the interaction
212 cut-off. In the non-bonded kernels, interactions are only computed when
213 a particle pair is within the cut-off distance at that particular time
214 step. This ensures that as particles move between pair search steps,
215 forces between nearly all particles within the cut-off distance are
216 calculated. We say *nearly* all particles, because |Gromacs| uses a fixed
217 pair list update frequency for efficiency. A particle-pair, whose
218 distance was outside the cut-off, could possibly move enough during this
219 fixed number of steps that its distance is now within the cut-off. This
220 small chance results in a small energy drift, and the size of the chance
221 depends on the temperature. When temperature coupling is used, the
222 buffer size can be determined automatically, given a certain tolerance
223 on the energy drift.
224
225 The Verlet cut-off scheme is implemented in a very efficient fashion
226 based on clusters of particles. The simplest example is a cluster size
227 of 4 particles. The pair list is then constructed based on cluster
228 pairs. The cluster-pair search is much faster searching based on
229 particle pairs, because :math:`4 \times 4 = 16` particle pairs are put
230 in the list at once. The non-bonded force calculation kernel can then
231 calculate many particle-pair interactions at once, which maps nicely to
232 SIMD or SIMT units on modern hardware, which can perform multiple
233 floating operations at once. These non-bonded kernels are much faster
234 than the kernels used in the group scheme for most types of systems,
235 particularly on newer hardware.
236
237 Additionally, when the list buffer is determined automatically as
238 described below, we also apply dynamic pair list pruning. The pair list
239 can be constructed infrequently, but that can lead to a lot of pairs in
240 the list that are outside the cut-off range for all or most of the life
241 time of this pair list. Such pairs can be pruned out by applying a
242 cluster-pair kernel that only determines which clusters are in range.
243 Because of the way the non-bonded data is regularized in |Gromacs|, this
244 kernel is an order of magnitude faster than the search and the
245 interaction kernel. On the GPU this pruning is overlapped with the
246 integration on the CPU, so it is free in most cases. Therefore we can
247 prune every 4-10 integration steps with little overhead and
248 significantly reduce the number of cluster pairs in the interaction
249 kernel. This procedure is applied automatically, unless the user set the
250 pair-list buffer size manually.
251
252 Energy drift and pair-list buffering
253 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
254
255 For a canonical (NVT) ensemble, the average energy error caused by
256 diffusion of :math:`j` particles from outside the pair-list cut-off
257 :math:`r_\ell` to inside the interaction cut-off :math:`r_c` over the
258 lifetime of the list can be determined from the atomic displacements and
259 the shape of the potential at the cut-off. The displacement distribution
260 along one dimension for a freely moving particle with mass :math:`m`
261 over time :math:`t` at temperature :math:`T` is a Gaussian :math:`G(x)`
262 of zero mean and variance :math:`\sigma^2 = t^2 k_B T/m`. For the
263 distance between two particles, the variance changes to
264 :math:`\sigma^2 = \sigma_{12}^2 =
265 t^2 k_B T(1/m_1+1/m_2)`. Note that in practice particles usually
266 interact with (bump into) other particles over time :math:`t` and
267 therefore the real displacement distribution is much narrower. Given a
268 non-bonded interaction cut-off distance of :math:`r_c` and a pair-list
269 cut-off :math:`r_\ell=r_c+r_b` for :math:`r_b` the Verlet buffer size,
270 we can then write the average energy error after time :math:`t` for all
271 missing pair interactions between a single :math:`i` particle of type 1
272 surrounded by all :math:`j` particles that are of type 2 with number
273 density :math:`\rho_2`, when the inter-particle distance changes from
274 :math:`r_0` to :math:`r_t`, as:
275
276 .. math::
277
278    \langle \Delta V \rangle =
279    \int_{0}^{r_c} \int_{r_\ell}^\infty 4 \pi r_0^2 \rho_2 V(r_t) G\!\left(\frac{r_t-r_0}{\sigma}\right) d r_0\, d r_t
280
281 To evaluate this analytically, we need to make some approximations.
282 First we replace :math:`V(r_t)` by a Taylor expansion around
283 :math:`r_c`, then we can move the lower bound of the integral over
284 :math:`r_0` to :math:`-\infty` which will simplify the result:
285
286 .. math::
287
288    \begin{aligned}
289    \langle \Delta V \rangle &\approx&
290    \int_{-\infty}^{r_c} \int_{r_\ell}^\infty 4 \pi r_0^2 \rho_2 \Big[ V'(r_c) (r_t - r_c) +
291    \nonumber\\
292    & &
293    \phantom{\int_{-\infty}^{r_c} \int_{r_\ell}^\infty 4 \pi r_0^2 \rho_2 \Big[}
294    V''(r_c)\frac{1}{2}(r_t - r_c)^2 +
295    \nonumber\\
296    & &
297    \phantom{\int_{-\infty}^{r_c} \int_{r_\ell}^\infty 4 \pi r_0^2 \rho_2 \Big[}
298      V'''(r_c)\frac{1}{6}(r_t - r_c)^3 +
299      \nonumber\\
300    & &
301    \phantom{\int_{-\infty}^{r_c} \int_{r_\ell}^\infty 4 \pi r_0^2 \rho_2 \Big[}
302      O \! \left((r_t - r_c)^4 \right)\Big] G\!\left(\frac{r_t-r_0}{\sigma}\right) d r_0 \, d r_t\end{aligned}
303
304 Replacing the factor :math:`r_0^2` by :math:`(r_\ell + \sigma)^2`,
305 which results in a slight overestimate, allows us to calculate the
306 integrals analytically:
307
308 .. math::
309
310    \begin{aligned}
311    \langle \Delta V \rangle \!
312    &\approx&
313    4 \pi (r_\ell+\sigma)^2 \rho_2
314    \int_{-\infty}^{r_c} \int_{r_\ell}^\infty \Big[ V'(r_c) (r_t - r_c) +
315    \nonumber\\
316    & &
317    \phantom{4 \pi (r_\ell+\sigma)^2 \rho_2 \int_{-\infty}^{r_c} \int_{r_\ell}^\infty \Big[}
318    V''(r_c)\frac{1}{2}(r_t - r_c)^2 +
319    \nonumber\\
320    & &
321    \phantom{4 \pi (r_\ell+\sigma)^2 \rho_2 \int_{-\infty}^{r_c} \int_{r_\ell}^\infty \Big[}
322    V'''(r_c)\frac{1}{6}(r_t - r_c)^3 \Big] G\!\left(\frac{r_t-r_0}{\sigma}\right)
323    d r_0 \, d r_t\\
324    &=&
325    4 \pi (r_\ell+\sigma)^2 \rho_2 \bigg\{
326    \frac{1}{2}V'(r_c)\left[r_b \sigma G\!\left(\frac{r_b}{\sigma}\right) - (r_b^2+\sigma^2)E\!\left(\frac{r_b}{\sigma}\right) \right] +
327    \nonumber\\
328    & &
329    \phantom{4 \pi (r_\ell+\sigma)^2 \rho_2 \bigg\{ }
330    \frac{1}{6}V''(r_c)\left[ \sigma(r_b^2+2\sigma^2) G\!\left(\frac{r_b}{\sigma}\right) - r_b(r_b^2+3\sigma^2 ) E\!\left(\frac{r_b}{\sigma}\right) \right] +
331    \nonumber\\
332    & &
333    \phantom{4 \pi (r_\ell+\sigma)^2 \rho_2 \bigg\{ }
334    \frac{1}{24}V'''(r_c)\bigg[ r_b\sigma(r_b^2+5\sigma^2) G\!\left(\frac{r_b}{\sigma}\right)
335    \nonumber\\
336    & &
337    \phantom{4 \pi (r_\ell+\sigma)^2 \rho_2 \bigg\{ \frac{1}{24}V'''(r_c)\bigg[ }
338     - (r_b^4+6r_b^2\sigma^2+3\sigma^4 ) E\!\left(\frac{r_b}{\sigma}\right) \bigg]
339    \bigg\}\end{aligned}
340
341 where :math:`G(x)` is a Gaussian distribution with 0 mean and unit
342 variance and :math:`E(x)=\frac{1}{2}\mathrm{erfc}(x/\sqrt{2})`. We
343 always want to achieve small energy error, so :math:`\sigma` will be
344 small compared to both :math:`r_c` and :math:`r_\ell`, thus the
345 approximations in the equations above are good, since the Gaussian
346 distribution decays rapidly. The energy error needs to be averaged over
347 all particle pair types and weighted with the particle counts. In
348 |Gromacs| we don’t allow cancellation of error between pair types, so we
349 average the absolute values. To obtain the average energy error per unit
350 time, it needs to be divided by the neighbor-list life time
351 :math:`t = ({\tt nstlist} - 1)\times{\tt dt}`. The function can not be
352 inverted analytically, so we use bisection to obtain the buffer size
353 :math:`r_b` for a target drift. Again we note that in practice the error
354 we usually be much smaller than this estimate, as in the condensed phase
355 particle displacements will be much smaller than for freely moving
356 particles, which is the assumption used here.
357
358 When (bond) constraints are present, some particles will have fewer
359 degrees of freedom. This will reduce the energy errors. For simplicity,
360 we only consider one constraint per particle, the heaviest particle in
361 case a particle is involved in multiple constraints. This simplification
362 overestimates the displacement. The motion of a constrained particle is
363 a superposition of the 3D motion of the center of mass of both particles
364 and a 2D rotation around the center of mass. The displacement in an
365 arbitrary direction of a particle with 2 degrees of freedom is not
366 Gaussian, but rather follows the complementary error function:
367
368 .. math:: \frac{\sqrt{\pi}}{2\sqrt{2}\sigma}\,\mathrm{erfc}\left(\frac{|r|}{\sqrt{2}\,\sigma}\right)
369           :label: eqn2Ddisp
370
371 where :math:`\sigma^2` is again :math:`t^2 k_B T/m`. This distribution
372 can no longer be integrated analytically to obtain the energy error. But
373 we can generate a tight upper bound using a scaled and shifted Gaussian
374 distribution (not shown). This Gaussian distribution can then be used to
375 calculate the energy error as described above. The rotation displacement
376 around the center of mass can not be more than the length of the arm. To
377 take this into account, we scale :math:`\sigma` in
378 :eq:`eqn. %s <eqn2Ddisp>` (details not presented here) to
379 obtain an overestimate of the real displacement. This latter effect
380 significantly reduces the buffer size for longer neighborlist lifetimes
381 in e.g. water, as constrained hydrogens are by far the fastest
382 particles, but they can not move further than 0.1 nm from the heavy atom
383 they are connected to.
384
385 There is one important implementation detail that reduces the energy
386 errors caused by the finite Verlet buffer list size. The derivation
387 above assumes a particle pair-list. However, the |Gromacs| implementation
388 uses a cluster pair-list for efficiency. The pair list consists of pairs
389 of clusters of 4 particles in most cases, also called a
390 :math:`4 \times 4` list, but the list can also be :math:`4 \times 8`
391 (GPU CUDA kernels and AVX 256-bit single precision kernels) or
392 :math:`4 \times 2` (SSE double-precision kernels). This means that the
393 pair-list is effectively much larger than the corresponding
394 :math:`1 \times 1` list. Thus slightly beyond the pair-list cut-off
395 there will still be a large fraction of particle pairs present in the
396 list. This fraction can be determined in a simulation and accurately
397 estimated under some reasonable assumptions. The fraction decreases with
398 increasing pair-list range, meaning that a smaller buffer can be used.
399 For typical all-atom simulations with a cut-off of 0.9 nm this fraction
400 is around 0.9, which gives a reduction in the energy errors of a factor
401 of 10. This reduction is taken into account during the automatic Verlet
402 buffer calculation and results in a smaller buffer size.
403
404 .. _fig-verletdrift:
405
406 .. figure:: plots/verlet-drift.*
407    :width: 9.00000cm
408
409    Energy drift per atom for an SPC/E water system at 300K with a
410    time step of 2 fs and a pair-list update period of 10 steps
411    (pair-list life time: 18 fs). PME was used with
412    ``ewald-rtol`` set to 10\ :math:`^{-5}`; this parameter
413    affects the shape of the potential at the cut-off. Error estimates
414    due to finite Verlet buffer size are shown for a :math:`1 \times 1`
415    atom pair list and :math:`4 \times 4` atom pair list without and with
416    (dashed line) cancellation of positive and negative errors. Real
417    energy drift is shown for simulations using double- and
418    mixed-precision settings. Rounding errors in the SETTLE constraint
419    algorithm from the use of single precision causes the drift to become
420    negative at large buffer size. Note that at zero buffer size, the
421    real drift is small because positive (H-H) and negative (O-H) energy
422    errors cancel.
423
424 In :numref:`Fig. %s <fig-verletdrift>` one can see that for small
425 buffer sizes the drift of the total energy is much smaller than the pair
426 energy error tolerance, due to cancellation of errors. For larger buffer
427 size, the error estimate is a factor of 6 higher than drift of the total
428 energy, or alternatively the buffer estimate is 0.024 nm too large. This
429 is because the protons don’t move freely over 18 fs, but rather vibrate.
430
431 Cut-off artifacts and switched interactions
432 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
433
434 With the Verlet scheme, the pair potentials are shifted to be zero at
435 the cut-off, which makes the potential the integral of the force. This
436 is only possible in the group scheme if the shape of the potential is
437 such that its value is zero at the cut-off distance. However, there can
438 still be energy drift when the forces are non-zero at the cut-off. This
439 effect is extremely small and often not noticeable, as other integration
440 errors (e.g. from constraints) may dominate. To completely avoid cut-off
441 artifacts, the non-bonded forces can be switched exactly to zero at some
442 distance smaller than the neighbor list cut-off (there are several ways
443 to do this in |Gromacs|, see sec. :ref:`modnbint`). One then has a
444 buffer with the size equal to the neighbor list cut-off less the longest
445 interaction cut-off.
446
447 Simple search
448 ^^^^^^^^^^^^^
449
450 Due to :eq:`eqns. %s <eqnboxrot>` and
451 :eq:`%s <eqnsimplerc>`, the vector
452 :math:`{\mathbf{r}_{ij}}` connecting images within the
453 cut-off :math:`R_c` can be found by constructing:
454
455 .. math::
456
457    \begin{aligned}
458    \mathbf{r}'''   & = & \mathbf{r}_j-\mathbf{r}_i \\
459    \mathbf{r}''    & = & \mathbf{r}''' - \mathbf{c}*\mathrm{round}(r'''_z/c_z) \\
460    \mathbf{r}'     & = & \mathbf{r}'' - \mathbf{b}*\mathrm{round}(r''_y/b_y) \\
461    \mathbf{r}_{ij} & = & \mathbf{r}' - \mathbf{a}*\mathrm{round}(r'_x/a_x)
462    \end{aligned}
463
464 When distances between two particles in a triclinic box are needed that
465 do not obey :eq:`eqn. %s <eqnboxrot>`, many shifts of
466 combinations of box vectors need to be considered to find the nearest
467 image.
468
469 .. _fig-grid:
470
471 .. figure:: plots/nstric.*
472    :width: 8.00000cm
473
474    Grid search in two dimensions. The arrows are the box vectors.
475
476 Grid search
477 ^^^^^^^^^^^
478
479 The grid search is schematically depicted in
480 :numref:`Fig. %s <fig-grid>`. All particles are put on the NS grid,
481 with the smallest spacing :math:`\ge` :math:`R_c/2` in each of the
482 directions. In the direction of each box vector, a particle :math:`i`
483 has three images. For each direction the image may be -1,0 or 1,
484 corresponding to a translation over -1, 0 or +1 box vector. We do not
485 search the surrounding NS grid cells for neighbors of :math:`i` and then
486 calculate the image, but rather construct the images first and then
487 search neighbors corresponding to that image of :math:`i`. As
488 :numref:`Fig. %s <fig-grid>` shows, some grid cells may be searched
489 more than once for different images of :math:`i`. This is not a problem,
490 since, due to the minimum image convention, at most one image will “see”
491 the :math:`j`-particle. For every particle, fewer than 125 (5:math:`^3`)
492 neighboring cells are searched. Therefore, the algorithm scales linearly
493 with the number of particles. Although the prefactor is large, the
494 scaling behavior makes the algorithm far superior over the standard
495 :math:`O(N^2)` algorithm when there are more than a few hundred
496 particles. The grid search is equally fast for rectangular and triclinic
497 boxes. Thus for most protein and peptide simulations the rhombic
498 dodecahedron will be the preferred box shape.
499
500 .. _chargegroup:
501
502 Charge groups
503 ^^^^^^^^^^^^^
504
505 Charge groups were originally introduced to reduce cut-off artifacts of
506 Coulomb interactions. When a plain cut-off is used, significant jumps in
507 the potential and forces arise when atoms with (partial) charges move in
508 and out of the cut-off radius. When all chemical moieties have a net
509 charge of zero, these jumps can be reduced by moving groups of atoms
510 with net charge zero, called charge groups, in and out of the neighbor
511 list. This reduces the cut-off effects from the charge-charge level to
512 the dipole-dipole level, which decay much faster. With the advent of
513 full range electrostatics methods, such as particle-mesh Ewald
514 (sec. :ref:`pme`), the use of charge groups is no longer required for
515 accuracy. It might even have a slight negative effect on the accuracy or
516 efficiency, depending on how the neighbor list is made and the
517 interactions are calculated.
518
519 But there is still an important reason for using *charge groups*:
520 efficiency with the group cut-off scheme. Where applicable, neighbor
521 searching is carried out on the basis of charge groups which are defined
522 in the molecular topology. If the nearest image distance between the
523 *geometrical centers* of the atoms of two charge groups is less than the
524 cut-off radius, all atom pairs between the charge groups are included in
525 the pair list. The neighbor searching for a water system, for instance,
526 is :math:`3^2=9` times faster when each molecule is treated as a charge
527 group. Also the highly optimized water force loops (see
528 sec. :ref:`waterloops`) only work when all atoms in a water molecule form
529 a single charge group. Currently the name *neighbor-search group* would
530 be more appropriate, but the name charge group is retained for
531 historical reasons. When developing a new force field, the advice is to
532 use charge groups of 3 to 4 atoms for optimal performance. For all-atom
533 force fields this is relatively easy, as one can simply put hydrogen
534 atoms, and in some case oxygen atoms, in the same charge group as the
535 heavy atom they are connected to; for example: CH\ :math:`_3`,
536 CH\ :math:`_2`, CH, NH\ :math:`_2`, NH, OH, CO\ :math:`_2`, CO.
537
538 With the Verlet cut-off scheme, charge groups are ignored.
539
540 .. _forces:
541
542 Compute forces
543 ~~~~~~~~~~~~~~
544
545 Potential energy
546 ^^^^^^^^^^^^^^^^
547
548 When forces are computed, the potential energy of each interaction term
549 is computed as well. The total potential energy is summed for various
550 contributions, such as Lennard-Jones, Coulomb, and bonded terms. It is
551 also possible to compute these contributions for *energy-monitor groups*
552 of atoms that are separately defined (see sec. :ref:`groupconcept`).
553
554 Kinetic energy and temperature
555 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
556
557 The temperature is given by the total kinetic energy of the
558 :math:`N`-particle system:
559
560 .. math:: E_{kin} = {\frac{1}{2}}\sum_{i=1}^N m_i v_i^2
561
562 From this the absolute temperature :math:`T` can be computed using:
563
564 .. math::  {\frac{1}{2}}N_{\mathrm{df}} kT = E_{\mathrm{kin}}
565            :label: eqnET
566
567 where :math:`k` is Boltzmann’s constant and :math:`N_{df}` is the
568 number of degrees of freedom which can be computed from:
569
570 .. math:: N_{\mathrm{df}}  ~=~     3 N - N_c - N_{\mathrm{com}}
571
572 Here :math:`N_c` is the number of *constraints* imposed on the system.
573 When performing molecular dynamics :math:`N_{\mathrm{com}}=3` additional
574 degrees of freedom must be removed, because the three center-of-mass
575 velocities are constants of the motion, which are usually set to zero.
576 When simulating in vacuo, the rotation around the center of mass can
577 also be removed, in this case :math:`N_{\mathrm{com}}=6`. When more than
578 one temperature-coupling group is used, the number of degrees of freedom
579 for group :math:`i` is:
580
581 .. math:: N^i_{\mathrm{df}}  ~=~  (3 N^i - N^i_c) \frac{3 N - N_c - N_{\mathrm{com}}}{3 N - N_c}
582
583 The kinetic energy can also be written as a tensor, which is necessary
584 for pressure calculation in a triclinic system, or systems where shear
585 forces are imposed:
586
587 .. math:: {\bf E}_{\mathrm{kin}} = {\frac{1}{2}}\sum_i^N m_i {\mathbf{v}_i}\otimes {\mathbf{v}_i}
588
589 Pressure and virial
590 ^^^^^^^^^^^^^^^^^^^
591
592 The pressure tensor **P** is calculated from the difference between
593 kinetic energy :math:`E_{\mathrm{kin}}` and the virial
594 :math:`{\bf \Xi}`:
595
596 .. math:: {\bf P} = \frac{2}{V} ({\bf E}_{\mathrm{kin}}-{\bf \Xi})
597           :label: eqnP
598
599 where :math:`V` is the volume of the computational box. The scalar
600 pressure :math:`P`, which can be used for pressure coupling in the case
601 of isotropic systems, is computed as:
602
603 .. math:: P       = {\rm trace}({\bf P})/3
604
605 The virial :math:`{\bf \Xi}` tensor is defined as:
606
607 .. math:: {\bf \Xi} = -{\frac{1}{2}}\sum_{i<j} \mathbf{r}_ij \otimes \mathbf{F}_ij 
608           :label: eqnXi
609
610 The |Gromacs| implementation of the virial computation is described in
611 sec. :ref:`virial`
612
613 .. _update:
614
615 The leap-frog integrator
616 ~~~~~~~~~~~~~~~~~~~~~~~~
617
618 .. _fig-leapfrog:
619
620 .. figure:: plots/leapfrog.*
621    :width: 8.00000cm
622
623    The Leap-Frog integration method. The algorithm is called Leap-Frog
624    because :math:`\mathbf{r}` and
625    :math:`\mathbf{v}` are leaping like frogs over each
626    other’s backs.
627
628 The default MD integrator in |Gromacs| is the so-called *leap-frog*
629 algorithm \ :ref:`22 <refHockney74>` for the integration of the
630 equations of motion. When extremely accurate integration with
631 temperature and/or pressure coupling is required, the velocity Verlet
632 integrators are also present and may be preferable (see
633 :ref:`vverlet`). The leap-frog algorithm uses positions
634 :math:`\mathbf{r}` at time :math:`t` and velocities
635 :math:`\mathbf{v}` at time
636 :math:`t-{{\frac{1}{2}}{{\Delta t}}}`; it updates positions and
637 velocities using the forces :math:`\mathbf{F}(t)`
638 determined by the positions at time :math:`t` using these relations:
639
640 .. math:: \begin{aligned}
641           \mathbf{v}(t+{{\frac{1}{2}}{{\Delta t}}})  &~=~&   \mathbf{v}(t-{{\frac{1}{2}}{{\Delta t}}})+\frac{{{\Delta t}}}{m}\mathbf{F}(t)   \\
642           \mathbf{r}(t+{{\Delta t}})   &~=~&   \mathbf{r}(t)+{{\Delta t}}\mathbf{v}(t+{{\frac{1}{2}}{{\Delta t}}})\end{aligned}
643           :label: eqnleapfrogv
644
645 The algorithm is visualized in :numref:`Fig. %s <fig-leapfrog>`. It
646 produces trajectories that are identical to the Verlet \ :ref:`23 <refVerlet67>`
647 algorithm, whose position-update relation is
648
649 .. math:: \mathbf{r}(t+{{\Delta t}})~=~2\mathbf{r}(t) - \mathbf{r}(t-{{\Delta t}}) + \frac{1}{m}\mathbf{F}(t){{\Delta t}}^2+O({{\Delta t}}^4)
650
651 The algorithm is of third order in :math:`\mathbf{r}` and
652 is time-reversible. See ref. \ :ref:`24 <refBerendsen86b>` for the
653 merits of this algorithm and comparison with other time integration
654 algorithms.
655
656 The equations of motion are modified for temperature coupling and
657 pressure coupling, and extended to include the conservation of
658 constraints, all of which are described below.
659
660 .. _vverlet:
661
662 The velocity Verlet integrator
663 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
664
665 The velocity Verlet algorithm\ :ref:`25 <refSwope82>` is also implemented in
666 |Gromacs|, though it is not yet fully integrated with all sets of options.
667 In velocity Verlet, positions :math:`\mathbf{r}` and
668 velocities :math:`\mathbf{v}` at time :math:`t` are used
669 to integrate the equations of motion; velocities at the previous half
670 step are not required.
671
672 .. math:: \begin{aligned}
673           \mathbf{v}(t+{{\frac{1}{2}}{{\Delta t}}})  &~=~&   \mathbf{v}(t)+\frac{{{\Delta t}}}{2m}\mathbf{F}(t)   \\
674           \mathbf{r}(t+{{\Delta t}})   &~=~&   \mathbf{r}(t)+{{\Delta t}}\,\mathbf{v}(t+{{\frac{1}{2}}{{\Delta t}}}) \\
675           \mathbf{v}(t+{{\Delta t}})   &~=~&   \mathbf{v}(t+{{\frac{1}{2}}{{\Delta t}}})+\frac{{{\Delta t}}}{2m}\mathbf{F}(t+{{\Delta t}})\end{aligned}
676           :label: eqnvelocityverlet1
677
678 or, equivalently,
679
680 .. math:: \begin{aligned}
681           \mathbf{r}(t+{{\Delta t}})   &~=~&   \mathbf{r}(t)+ {{\Delta t}}\,\mathbf{v} + \frac{{{\Delta t}}^2}{2m}\mathbf{F}(t) \\
682           \mathbf{v}(t+{{\Delta t}})   &~=~&   \mathbf{v}(t)+ \frac{{{\Delta t}}}{2m}\left[\mathbf{F}(t) + \mathbf{F}(t+{{\Delta t}})\right]\end{aligned}
683           :label: eqnvelocityverlet2
684
685 With no temperature or pressure coupling, and with *corresponding*
686 starting points, leap-frog and velocity Verlet will generate identical
687 trajectories, as can easily be verified by hand from the equations
688 above. Given a single starting file with the *same* starting point
689 :math:`\mathbf{x}(0)` and
690 :math:`\mathbf{v}(0)`, leap-frog and velocity Verlet will
691 *not* give identical trajectories, as leap-frog will interpret the
692 velocities as corresponding to :math:`t=-{{\frac{1}{2}}{{\Delta t}}}`,
693 while velocity Verlet will interpret them as corresponding to the
694 timepoint :math:`t=0`.
695
696 Understanding reversible integrators: The Trotter decomposition
697 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
698
699 To further understand the relationship between velocity Verlet and
700 leap-frog integration, we introduce the reversible Trotter formulation
701 of dynamics, which is also useful to understanding implementations of
702 thermostats and barostats in |Gromacs|.
703
704 A system of coupled, first-order differential equations can be evolved
705 from time :math:`t = 0` to time :math:`t` by applying the evolution
706 operator
707
708 .. math::
709
710    \begin{aligned}
711    \Gamma(t) &=& \exp(iLt) \Gamma(0) \nonumber \\
712    iL &=& \dot{\Gamma}\cdot \nabla_{\Gamma},\end{aligned}
713
714 where :math:`L` is the Liouville operator, and :math:`\Gamma` is the
715 multidimensional vector of independent variables (positions and
716 velocities). A short-time approximation to the true operator, accurate
717 at time :math:`{{\Delta t}}= t/P`, is applied :math:`P` times in
718 succession to evolve the system as
719
720 .. math:: \Gamma(t) = \prod_{i=1}^P \exp(iL{{\Delta t}}) \Gamma(0)
721
722 For NVE dynamics, the Liouville operator is
723
724 .. math::
725
726    \begin{aligned}
727    iL = \sum_{i=1}^{N} {{\mathbf{v}}}_i \cdot \nabla_{{{\mathbf{r}}}_i} + \sum_{i=1}^N \frac{1}{m_i}{{\mathbf{F}}}(r_i) \cdot \nabla_{{{\mathbf{v}}}_i}.\end{aligned}
728
729 This can be split into two additive operators
730
731 .. math::
732
733    \begin{aligned}
734    iL_1 &=& \sum_{i=1}^N \frac{1}{m_i}{{\mathbf{F}}}(r_i) \cdot \nabla_{{{\mathbf{v}}}_i} \nonumber \\
735    iL_2 &=& \sum_{i=1}^{N} {{\mathbf{v}}}_i \cdot \nabla_{{{\mathbf{r}}}_i} \end{aligned}
736
737 Then a short-time, symmetric, and thus reversible approximation of the
738 true dynamics will be
739
740 .. math:: \begin{aligned}
741           \exp(iL{{\Delta t}}) = \exp(iL_2{{\frac{1}{2}}{{\Delta t}}}) \exp(iL_1{{\Delta t}}) \exp(iL_2{{\frac{1}{2}}{{\Delta t}}}) + \mathcal{O}({{\Delta t}}^3).
742           \end{aligned}
743           :label: eqNVETrotter
744
745 This corresponds to velocity Verlet integration. The first exponential
746 term over :math:`{{\frac{1}{2}}{{\Delta t}}}` corresponds to a velocity
747 half-step, the second exponential term over :math:`{{\Delta t}}`
748 corresponds to a full velocity step, and the last exponential term over
749 :math:`{{\frac{1}{2}}{{\Delta t}}}` is the final velocity half step. For
750 future times :math:`t = n{{\Delta t}}`, this becomes
751
752 .. math::
753
754    \begin{aligned}
755    \exp(iLn{{\Delta t}}) &\approx&  \left(\exp(iL_2{{\frac{1}{2}}{{\Delta t}}}) \exp(iL_1{{\Delta t}}) \exp(iL_2{{\frac{1}{2}}{{\Delta t}}})\right)^n \nonumber \\
756                 &\approx&  \exp(iL_2{{\frac{1}{2}}{{\Delta t}}}) \bigg(\exp(iL_1{{\Delta t}}) \exp(iL_2{{\Delta t}})\bigg)^{n-1} \nonumber \\
757                 &       &  \;\;\;\; \exp(iL_1{{\Delta t}}) \exp(iL_2{{\frac{1}{2}}{{\Delta t}}}) \end{aligned}
758
759 This formalism allows us to easily see the difference between the
760 different flavors of Verlet integrators. The leap-frog integrator can be
761 seen as starting with :eq:`Eq. %s <eqNVETrotter>` with the
762 :math:`\exp\left(iL_1 {\Delta t}\right)` term, instead of the half-step
763 velocity term, yielding
764
765 .. math::
766
767    \begin{aligned}
768    \exp(iLn{\Delta t}) &=& \exp\left(iL_1 {\Delta t}\right) \exp\left(iL_2 {{\Delta t}}\right) + \mathcal{O}({{\Delta t}}^3).\end{aligned}
769
770 Here, the full step in velocity is between
771 :math:`t-{{\frac{1}{2}}{{\Delta t}}}` and
772 :math:`t+{{\frac{1}{2}}{{\Delta t}}}`, since it is a combination of the
773 velocity half steps in velocity Verlet. For future times
774 :math:`t = n{{\Delta t}}`, this becomes
775
776 .. math::
777
778    \begin{aligned}
779    \exp(iLn{\Delta t}) &\approx& \bigg(\exp\left(iL_1 {\Delta t}\right) \exp\left(iL_2 {{\Delta t}}\right)  \bigg)^{n}.\end{aligned}
780
781 Although at first this does not appear symmetric, as long as the full
782 velocity step is between :math:`t-{{\frac{1}{2}}{{\Delta t}}}` and
783 :math:`t+{{\frac{1}{2}}{{\Delta t}}}`, then this is simply a way of
784 starting velocity Verlet at a different place in the cycle.
785
786 Even though the trajectory and thus potential energies are identical
787 between leap-frog and velocity Verlet, the kinetic energy and
788 temperature will not necessarily be the same. Standard velocity Verlet
789 uses the velocities at the :math:`t` to calculate the kinetic energy and
790 thus the temperature only at time :math:`t`; the kinetic energy is then
791 a sum over all particles
792
793 .. math::
794
795    \begin{aligned}
796    KE_{\mathrm{full}}(t) &=& \sum_i \left(\frac{1}{2m_i}\mathbf{v}_i(t)\right)^2 \nonumber\\ 
797          &=& \sum_i \frac{1}{2m_i}\left(\frac{1}{2}\mathbf{v}_i(t-{{\frac{1}{2}}{{\Delta t}}})+\frac{1}{2}\mathbf{v}_i(t+{{\frac{1}{2}}{{\Delta t}}})\right)^2,\end{aligned}
798
799 with the square on the *outside* of the average. Standard leap-frog
800 calculates the kinetic energy at time :math:`t` based on the average
801 kinetic energies at the timesteps :math:`t+{{\frac{1}{2}}{{\Delta t}}}`
802 and :math:`t-{{\frac{1}{2}}{{\Delta t}}}`, or the sum over all particles
803
804 .. math::
805
806    \begin{aligned}
807    KE_{\mathrm{average}}(t) &=& \sum_i \frac{1}{2m_i}\left(\frac{1}{2}\mathbf{v}_i(t-{{\frac{1}{2}}{{\Delta t}}})^2+\frac{1}{2}\mathbf{v}_i(t+{{\frac{1}{2}}{{\Delta t}}})^2\right),\end{aligned}
808
809 where the square is *inside* the average.
810
811 A non-standard variant of velocity Verlet which averages the kinetic
812 energies :math:`KE(t+{{\frac{1}{2}}{{\Delta t}}})` and
813 :math:`KE(t-{{\frac{1}{2}}{{\Delta t}}})`, exactly like leap-frog, is
814 also now implemented in |Gromacs| (as :ref:`mdp` file option
815 :mdp-value:`integrator=md-vv-avek`). Without temperature and pressure coupling,
816 velocity Verlet with half-step-averaged kinetic energies and leap-frog
817 will be identical up to numerical precision. For temperature- and
818 pressure-control schemes, however, velocity Verlet with
819 half-step-averaged kinetic energies and leap-frog will be different, as
820 will be discussed in the section in thermostats and barostats.
821
822 The half-step-averaged kinetic energy and temperature are slightly more
823 accurate for a given step size; the difference in average kinetic
824 energies using the half-step-averaged kinetic energies (
825 :mdp-value:`integrator=md` and :mdp-value:`integrator=md-vv-avek`
826 ) will be closer to the kinetic energy obtained in the limit
827 of small step size than will the full-step kinetic energy (using
828 :mdp-value:`integrator=md-vv`). For NVE simulations, this difference is usually not
829 significant, since the positions and velocities of the particles are
830 still identical; it makes a difference in the way the the temperature of
831 the simulations are **interpreted**, but **not** in the trajectories that
832 are produced. Although the kinetic energy is more accurate with the
833 half-step-averaged method, meaning that it changes less as the timestep
834 gets large, it is also more noisy. The RMS deviation of the total energy
835 of the system (sum of kinetic plus potential) in the half-step-averaged
836 kinetic energy case will be higher (about twice as high in most cases)
837 than the full-step kinetic energy. The drift will still be the same,
838 however, as again, the trajectories are identical.
839
840 For NVT simulations, however, there **will** be a difference, as discussed
841 in the section on temperature control, since the velocities of the
842 particles are adjusted such that kinetic energies of the simulations,
843 which can be calculated either way, reach the distribution corresponding
844 to the set temperature. In this case, the three methods will not give
845 identical results.
846
847 Because the velocity and position are both defined at the same time
848 :math:`t` the velocity Verlet integrator can be used for some methods,
849 especially rigorously correct pressure control methods, that are not
850 actually possible with leap-frog. The integration itself takes
851 negligibly more time than leap-frog, but twice as many communication
852 calls are currently required. In most cases, and especially for large
853 systems where communication speed is important for parallelization and
854 differences between thermodynamic ensembles vanish in the :math:`1/N`
855 limit, and when only NVT ensembles are required, leap-frog will likely
856 be the preferred integrator. For pressure control simulations where the
857 fine details of the thermodynamics are important, only velocity Verlet
858 allows the true ensemble to be calculated. In either case, simulation
859 with double precision may be required to get fine details of
860 thermodynamics correct.
861
862 Multiple time stepping
863 ~~~~~~~~~~~~~~~~~~~~~~
864
865 Several other simulation packages uses multiple time stepping for bonds
866 and/or the PME mesh forces. In |Gromacs| we have not implemented this
867 (yet), since we use a different philosophy. Bonds can be constrained
868 (which is also a more sound approximation of a physical quantum
869 oscillator), which allows the smallest time step to be increased to the
870 larger one. This not only halves the number of force calculations, but
871 also the update calculations. For even larger time steps, angle
872 vibrations involving hydrogen atoms can be removed using virtual
873 interaction sites (see sec. :ref:`rmfast`), which brings the shortest
874 time step up to PME mesh update frequency of a multiple time stepping
875 scheme.
876
877 .. _temp-coupling:
878
879 Temperature coupling
880 ~~~~~~~~~~~~~~~~~~~~
881
882 While direct use of molecular dynamics gives rise to the NVE (constant
883 number, constant volume, constant energy ensemble), most quantities that
884 we wish to calculate are actually from a constant temperature (NVT)
885 ensemble, also called the canonical ensemble. |Gromacs| can use the
886 *weak-coupling* scheme of Berendsen \ :ref:`26 <refBerendsen84>`, stochastic
887 randomization through the Andersen thermostat \ :ref:`27 <refAndersen80>`, the
888 extended ensemble Nosé-Hoover scheme \ :ref:`28 <refNose84>`, :ref:`29 <refHoover85>`, or a
889 velocity-rescaling scheme \ :ref:`30 <refBussi2007a>` to
890 simulate constant temperature, with advantages of each of the schemes
891 laid out below.
892
893 There are several other reasons why it might be necessary to control the
894 temperature of the system (drift during equilibration, drift as a result
895 of force truncation and integration errors, heating due to external or
896 frictional forces), but this is not entirely correct to do from a
897 thermodynamic standpoint, and in some cases only masks the symptoms
898 (increase in temperature of the system) rather than the underlying
899 problem (deviations from correct physics in the dynamics). For larger
900 systems, errors in ensemble averages and structural properties incurred
901 by using temperature control to remove slow drifts in temperature appear
902 to be negligible, but no completely comprehensive comparisons have been
903 carried out, and some caution must be taking in interpreting the
904 results.
905
906 When using temperature and/or pressure coupling the total energy is no
907 longer conserved. Instead there is a conserved energy quantity the
908 formula of which will depend on the combination or temperature and
909 pressure coupling algorithm used. For all coupling algorithms, except
910 for Andersen temperature coupling and Parrinello-Rahman pressure
911 coupling combined with shear stress, the conserved energy quantity is
912 computed and stored in the energy and log file. Note that this quantity
913 will not be conserved when external forces are applied to the system,
914 such as pulling on group with a changing distance or an electric field.
915 Furthermore, how well the energy is conserved depends on the accuracy of
916 all algorithms involved in the simulation. Usually the algorithms that
917 cause most drift are constraints and the pair-list buffer, depending on
918 the parameters used.
919
920 Berendsen temperature coupling
921 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
922
923 The Berendsen algorithm mimics weak coupling with first-order kinetics
924 to an external heat bath with given temperature :math:`T_0`. See
925 ref. \ :ref:`31 <refBerendsen91>` for a comparison with the Nosé-Hoover scheme. The
926 effect of this algorithm is that a deviation of the system temperature
927 from :math:`T_0` is slowly corrected according to:
928
929 .. math::  \frac{{\mbox{d}}T}{{\mbox{d}}t} = \frac{T_0-T}{\tau}
930            :label: eqnTcoupling
931
932 which means that a temperature deviation decays exponentially with a
933 time constant :math:`\tau`. This method of coupling has the advantage
934 that the strength of the coupling can be varied and adapted to the user
935 requirement: for equilibration purposes the coupling time can be taken
936 quite short (*e.g.* 0.01 ps), but for reliable equilibrium runs it can
937 be taken much longer (*e.g.* 0.5 ps) in which case it hardly influences
938 the conservative dynamics.
939
940 The Berendsen thermostat suppresses the fluctuations of the kinetic
941 energy. This means that one does not generate a proper canonical
942 ensemble, so rigorously, the sampling will be incorrect. This error
943 scales with :math:`1/N`, so for very large systems most ensemble
944 averages will not be affected significantly, except for the distribution
945 of the kinetic energy itself. However, fluctuation properties, such as
946 the heat capacity, will be affected. A similar thermostat which does
947 produce a correct ensemble is the velocity rescaling
948 thermostat \ :ref:`30 <refBussi2007a>` described below.
949
950 The heat flow into or out of the system is affected by scaling the
951 velocities of each particle every step, or every :math:`n_\mathrm{TC}`
952 steps, with a time-dependent factor :math:`\lambda`, given by:
953
954 .. math::  \lambda = \left[ 1 + \frac{n_\mathrm{TC} \Delta t}{\tau_T}
955            \left\{\frac{T_0}{T(t -  {{\frac{1}{2}}{{\Delta t}}})} - 1 \right\} \right]^{1/2}
956            :label: eqnlambda
957
958 The parameter :math:`\tau_T` is close, but not exactly equal, to the
959 time constant :math:`\tau` of the temperature coupling
960 (:eq:`eqn. %s <eqnTcoupling>`):
961
962 .. math:: \tau = 2 C_V \tau_T / N_{df} k
963
964 where :math:`C_V` is the total heat capacity of the system, :math:`k`
965 is Boltzmann’s constant, and :math:`N_{df}` is the total number of
966 degrees of freedom. The reason that :math:`\tau \neq \tau_T` is that the
967 kinetic energy change caused by scaling the velocities is partly
968 redistributed between kinetic and potential energy and hence the change
969 in temperature is less than the scaling energy. In practice, the ratio
970 :math:`\tau / \tau_T` ranges from 1 (gas) to 2 (harmonic solid) to 3
971 (water). When we use the term *temperature coupling time constant*, we
972 mean the parameter :math:`\tau_T`. **Note** that in practice the scaling
973 factor :math:`\lambda` is limited to the range of 0.8
974 :math:`<= \lambda <=` 1.25, to avoid scaling by very large numbers which
975 may crash the simulation. In normal use, :math:`\lambda` will always be
976 much closer to 1.0.
977
978 The thermostat modifies the kinetic energy at each scaling step by:
979
980 .. math:: \Delta E_k = (\lambda - 1)^2 E_k
981
982 The sum of these changes over the run needs to subtracted from the
983 total energy to obtain the conserved energy quantity.
984
985 Velocity-rescaling temperature coupling
986 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
987
988 The velocity-rescaling thermostat \ :ref:`30 <refBussi2007a>`
989 is essentially a Berendsen thermostat (see above) with an additional
990 stochastic term that ensures a correct kinetic energy distribution by
991 modifying it according to
992
993 .. math::  {\mbox{d}}K = (K_0 - K) \frac{{\mbox{d}}t}{\tau_T} + 2 \sqrt{\frac{K K_0}{N_f}} \frac{{\mbox{d}}W}{\sqrt{\tau_T}},
994            :label: eqnvrescale
995
996 where :math:`K` is the kinetic energy, :math:`N_f` the number of
997 degrees of freedom and :math:`{\mbox{d}}W` a Wiener process. There are
998 no additional parameters, except for a random seed. This thermostat
999 produces a correct canonical ensemble and still has the advantage of the
1000 Berendsen thermostat: first order decay of temperature deviations and no
1001 oscillations.
1002
1003 Andersen thermostat
1004 ^^^^^^^^^^^^^^^^^^^
1005
1006 One simple way to maintain a thermostatted ensemble is to take an
1007 :math:`NVE` integrator and periodically re-select the velocities of the
1008 particles from a Maxwell-Boltzmann distribution \ :ref:`27 <refAndersen80>`. This
1009 can either be done by randomizing all the velocities simultaneously
1010 (massive collision) every :math:`\tau_T/{{\Delta t}}` steps
1011 (``andersen-massive``), or by randomizing every particle
1012 with some small probability every timestep (``andersen``),
1013 equal to :math:`{{\Delta t}}/\tau`, where in both cases
1014 :math:`{{\Delta t}}` is the timestep and :math:`\tau_T` is a
1015 characteristic coupling time scale. Because of the way constraints
1016 operate, all particles in the same constraint group must be randomized
1017 simultaneously. Because of parallelization issues, the
1018 ``andersen`` version cannot currently (5.0) be used in
1019 systems with constraints. ``andersen-massive`` can be used
1020 regardless of constraints. This thermostat is also currently only
1021 possible with velocity Verlet algorithms, because it operates directly
1022 on the velocities at each timestep.
1023
1024 This algorithm completely avoids some of the ergodicity issues of other
1025 thermostatting algorithms, as energy cannot flow back and forth between
1026 energetically decoupled components of the system as in velocity scaling
1027 motions. However, it can slow down the kinetics of system by randomizing
1028 correlated motions of the system, including slowing sampling when
1029 :math:`\tau_T` is at moderate levels (less than 10 ps). This algorithm
1030 should therefore generally not be used when examining kinetics or
1031 transport properties of the system \ :ref:`32 <refBasconi2013>`.
1032
1033 Nosé-Hoover temperature coupling
1034 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1035
1036 The Berendsen weak-coupling algorithm is extremely efficient for
1037 relaxing a system to the target temperature, but once the system has
1038 reached equilibrium it might be more important to probe a correct
1039 canonical ensemble. This is unfortunately not the case for the
1040 weak-coupling scheme.
1041
1042 To enable canonical ensemble simulations, |Gromacs| also supports the
1043 extended-ensemble approach first proposed by Nosé :ref:`28 <refNose84>` and later
1044 modified by Hoover \ :ref:`29 <refHoover85>`. The system Hamiltonian is extended by
1045 introducing a thermal reservoir and a friction term in the equations of
1046 motion. The friction force is proportional to the product of each
1047 particle’s velocity and a friction parameter, :math:`\xi`. This friction
1048 parameter (or *heat bath* variable) is a fully dynamic quantity with its
1049 own momentum (:math:`p_{\xi}`) and equation of motion; the time
1050 derivative is calculated from the difference between the current kinetic
1051 energy and the reference temperature.
1052
1053 In this formulation, the particles´ equations of motion in
1054 the global :ref:`MD scheme <gmx-md-scheme>` are replaced by:
1055
1056 .. math:: \frac {{\mbox{d}}^2\mathbf{r}_i}{{\mbox{d}}t^2} = \frac{\mathbf{F}_i}{m_i} - 
1057           \frac{p_{\xi}}{Q}\frac{{\mbox{d}}\mathbf{r}_i}{{\mbox{d}}t} ,
1058           :label: eqnNHeqnofmotion
1059
1060 where the equation of motion for the heat bath parameter :math:`\xi` is:
1061
1062 .. math:: \frac {{\mbox{d}}p_{\xi}}{{\mbox{d}}t} = \left( T - T_0 \right).
1063
1064 The reference temperature is denoted :math:`T_0`, while :math:`T` is
1065 the current instantaneous temperature of the system. The strength of the
1066 coupling is determined by the constant :math:`Q` (usually called the
1067 *mass parameter* of the reservoir) in combination with the reference
1068 temperature.  [1]_
1069
1070 The conserved quantity for the Nosé-Hoover equations of motion is not
1071 the total energy, but rather
1072
1073 .. math::
1074
1075    \begin{aligned}
1076    H = \sum_{i=1}^{N} \frac{{{\mathbf{p}}}_i}{2m_i} + U\left({{\mathbf{r}}}_1,{{\mathbf{r}}}_2,\ldots,{{\mathbf{r}}}_N\right) +\frac{p_{\xi}^2}{2Q} + N_fkT\xi,\end{aligned}
1077
1078 where :math:`N_f` is the total number of degrees of freedom.
1079
1080 In our opinion, the mass parameter is a somewhat awkward way of
1081 describing coupling strength, especially due to its dependence on
1082 reference temperature (and some implementations even include the number
1083 of degrees of freedom in your system when defining :math:`Q`). To
1084 maintain the coupling strength, one would have to change :math:`Q` in
1085 proportion to the change in reference temperature. For this reason, we
1086 prefer to let the |Gromacs| user work instead with the period
1087 :math:`\tau_T` of the oscillations of kinetic energy between the system
1088 and the reservoir instead. It is directly related to :math:`Q` and
1089 :math:`T_0` via:
1090
1091 .. math:: Q = \frac {\tau_T^2 T_0}{4 \pi^2}.
1092
1093 This provides a much more intuitive way of selecting the Nosé-Hoover
1094 coupling strength (similar to the weak-coupling relaxation), and in
1095 addition :math:`\tau_T` is independent of system size and reference
1096 temperature.
1097
1098 It is however important to keep the difference between the weak-coupling
1099 scheme and the Nosé-Hoover algorithm in mind: Using weak coupling you
1100 get a strongly damped *exponential relaxation*, while the Nosé-Hoover
1101 approach produces an *oscillatory relaxation*. The actual time it takes
1102 to relax with Nosé-Hoover coupling is several times larger than the
1103 period of the oscillations that you select. These oscillations (in
1104 contrast to exponential relaxation) also means that the time constant
1105 normally should be 4–5 times larger than the relaxation time used with
1106 weak coupling, but your mileage may vary.
1107
1108 Nosé-Hoover dynamics in simple systems such as collections of harmonic
1109 oscillators, can be *nonergodic*, meaning that only a subsection of
1110 phase space is ever sampled, even if the simulations were to run for
1111 infinitely long. For this reason, the Nosé-Hoover chain approach was
1112 developed, where each of the Nosé-Hoover thermostats has its own
1113 Nosé-Hoover thermostat controlling its temperature. In the limit of an
1114 infinite chain of thermostats, the dynamics are guaranteed to be
1115 ergodic. Using just a few chains can greatly improve the ergodicity, but
1116 recent research has shown that the system will still be nonergodic, and
1117 it is still not entirely clear what the practical effect of
1118 this \ :ref:`33 <refCooke2008>`. Currently, the default number of chains is 10, but
1119 this can be controlled by the user. In the case of chains, the equations
1120 are modified in the following way to include a chain of thermostatting
1121 particles \ :ref:`34 <refMartyna1992>`:
1122
1123 .. math::  \begin{aligned}
1124            \frac {{\mbox{d}}^2\mathbf{r}_i}{{\mbox{d}}t^2} &~=~& \frac{\mathbf{F}_i}{m_i} - \frac{p_{{\xi}_1}}{Q_1} \frac{{\mbox{d}}\mathbf{r}_i}{{\mbox{d}}t} \nonumber \\
1125            \frac {{\mbox{d}}p_{{\xi}_1}}{{\mbox{d}}t} &~=~& \left( T - T_0 \right) - p_{{\xi}_1} \frac{p_{{\xi}_2}}{Q_2} \nonumber \\
1126            \frac {{\mbox{d}}p_{{\xi}_{i=2\ldots N}}}{{\mbox{d}}t} &~=~& \left(\frac{p_{\xi_{i-1}}^2}{Q_{i-1}} -kT\right) - p_{\xi_i} \frac{p_{\xi_{i+1}}}{Q_{i+1}} \nonumber \\
1127            \frac {{\mbox{d}}p_{\xi_N}}{{\mbox{d}}t} &~=~& \left(\frac{p_{\xi_{N-1}}^2}{Q_{N-1}}-kT\right)
1128            \end{aligned}
1129            :label: eqnNHchaineqnofmotion
1130
1131 The conserved quantity for Nosé-Hoover chains is
1132
1133 .. math::
1134
1135    \begin{aligned}
1136    H = \sum_{i=1}^{N} \frac{{{\mathbf{p}}}_i}{2m_i} + U\left({{\mathbf{r}}}_1,{{\mathbf{r}}}_2,\ldots,{{\mathbf{r}}}_N\right) +\sum_{k=1}^M\frac{p^2_{\xi_k}}{2Q^{\prime}_k} + N_fkT\xi_1 + kT\sum_{k=2}^M \xi_k \end{aligned}
1137
1138 The values and velocities of the Nosé-Hoover thermostat variables are
1139 generally not included in the output, as they take up a fair amount of
1140 space and are generally not important for analysis of simulations, but
1141 by setting an :ref:`mdp` option the values of all the positions and velocities
1142 of all Nosé-Hoover particles in the chain are written to the :ref:`edr` file.
1143 Leap-frog simulations currently can only have Nosé-Hoover chain lengths
1144 of 1, but this will likely be updated in later version.
1145
1146 As described in the integrator section, for temperature coupling, the
1147 temperature that the algorithm attempts to match to the reference
1148 temperature is calculated differently in velocity Verlet and leap-frog
1149 dynamics. Velocity Verlet (*md-vv*) uses the full-step kinetic energy,
1150 while leap-frog and *md-vv-avek* use the half-step-averaged kinetic
1151 energy.
1152
1153 We can examine the Trotter decomposition again to better understand the
1154 differences between these constant-temperature integrators. In the case
1155 of Nosé-Hoover dynamics (for simplicity, using a chain with :math:`N=1`,
1156 with more details in Ref. \ :ref:`35 <refMartyna1996>`), we split the Liouville
1157 operator as
1158
1159 .. math:: iL = iL_1 + iL_2 + iL_{\mathrm{NHC}},
1160
1161 where
1162
1163 .. math::
1164
1165    \begin{aligned}
1166    iL_1 &=& \sum_{i=1}^N \left[\frac{{{\mathbf{p}}}_i}{m_i}\right]\cdot \frac{\partial}{\partial {{\mathbf{r}}}_i} \nonumber \\
1167    iL_2 &=& \sum_{i=1}^N {{\mathbf{F}}}_i\cdot \frac{\partial}{\partial {{\mathbf{p}}}_i} \nonumber \\
1168    iL_{\mathrm{NHC}} &=& \sum_{i=1}^N-\frac{p_{\xi}}{Q}{{\mathbf{v}}}_i\cdot \nabla_{{{\mathbf{v}}}_i} +\frac{p_{\xi}}{Q}\frac{\partial }{\partial \xi} + \left( T - T_0 \right)\frac{\partial }{\partial p_{\xi}}\end{aligned}
1169
1170 For standard velocity Verlet with Nosé-Hoover temperature control, this
1171 becomes
1172
1173 .. math::
1174
1175    \begin{aligned}
1176    \exp(iL{\Delta t}) &=& \exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right) \exp\left(iL_2 {\Delta t}/2\right) \nonumber \\
1177    &&\exp\left(iL_1 {\Delta t}\right) \exp\left(iL_2 {\Delta t}/2\right) \exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right) + \mathcal{O}({{\Delta t}}^3).\end{aligned}
1178
1179 For half-step-averaged temperature control using *md-vv-avek*, this
1180 decomposition will not work, since we do not have the full step
1181 temperature until after the second velocity step. However, we can
1182 construct an alternate decomposition that is still reversible, by
1183 switching the place of the NHC and velocity portions of the
1184 decomposition:
1185
1186 .. math::  \begin{aligned}
1187    \exp(iL{\Delta t}) &=& \exp\left(iL_2 {\Delta t}/2\right) \exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right)\exp\left(iL_1 {\Delta t}\right)\nonumber \\
1188    &&\exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right) \exp\left(iL_2 {\Delta t}/2\right)+ \mathcal{O}({{\Delta t}}^3)
1189    \end{aligned}
1190    :label: eqhalfstepNHCintegrator
1191
1192 This formalism allows us to easily see the difference between the
1193 different flavors of velocity Verlet integrator. The leap-frog
1194 integrator can be seen as starting with
1195 :eq:`Eq. %s <eqhalfstepNHCintegrator>` just before the
1196 :math:`\exp\left(iL_1 {\Delta t}\right)` term, yielding:
1197
1198 .. math::
1199
1200    \begin{aligned}
1201    \exp(iL{\Delta t}) &=&  \exp\left(iL_1 {\Delta t}\right) \exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right) \nonumber \\
1202    &&\exp\left(iL_2 {\Delta t}\right) \exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right) + \mathcal{O}({{\Delta t}}^3)\end{aligned}
1203
1204 and then using some algebra tricks to solve for some quantities are
1205 required before they are actually calculated \ :ref:`36 <refHolian95>`.
1206
1207 Group temperature coupling
1208 ^^^^^^^^^^^^^^^^^^^^^^^^^^
1209
1210 In |Gromacs| temperature coupling can be performed on groups of atoms,
1211 typically a protein and solvent. The reason such algorithms were
1212 introduced is that energy exchange between different components is not
1213 perfect, due to different effects including cut-offs etc. If now the
1214 whole system is coupled to one heat bath, water (which experiences the
1215 largest cut-off noise) will tend to heat up and the protein will cool
1216 down. Typically 100 K differences can be obtained. With the use of
1217 proper electrostatic methods (PME) these difference are much smaller but
1218 still not negligible. The parameters for temperature coupling in groups
1219 are given in the :ref:`mdp` file. Recent investigation has shown that small
1220 temperature differences between protein and water may actually be an
1221 artifact of the way temperature is calculated when there are finite
1222 timesteps, and very large differences in temperature are likely a sign
1223 of something else seriously going wrong with the system, and should be
1224 investigated carefully \ :ref:`37 <refEastwood2010>`.
1225
1226 One special case should be mentioned: it is possible to
1227 temperature-couple only part of the system, leaving other parts without
1228 temperature coupling. This is done by specifying :math:`{-1}` for the
1229 time constant :math:`\tau_T` for the group that should not be
1230 thermostatted. If only part of the system is thermostatted, the system
1231 will still eventually converge to an NVT system. In fact, one suggestion
1232 for minimizing errors in the temperature caused by discretized timesteps
1233 is that if constraints on the water are used, then only the water
1234 degrees of freedom should be thermostatted, not protein degrees of
1235 freedom, as the higher frequency modes in the protein can cause larger
1236 deviations from the *true* temperature, the temperature obtained with
1237 small timesteps \ :ref:`37 <refEastwood2010>`.
1238
1239 Pressure coupling
1240 ~~~~~~~~~~~~~~~~~
1241
1242 In the same spirit as the temperature coupling, the system can also be
1243 coupled to a *pressure bath.* |Gromacs| supports both the Berendsen
1244 algorithm \ :ref:`26 <refBerendsen84>` that scales coordinates and box
1245 vectors every step, the extended-ensemble Parrinello-Rahman
1246 approach \ :ref:`38 <refParrinello81>`, :ref:`39 <refNose83>`, and for the
1247 velocity Verlet variants, the Martyna-Tuckerman-Tobias-Klein (MTTK)
1248 implementation of pressure control \ :ref:`35 <refMartyna1996>`.
1249 Parrinello-Rahman and Berendsen can be combined with any of the
1250 temperature coupling methods above. MTTK can only be used with
1251 Nosé-Hoover temperature control. From 5.1 afterwards, it can only used
1252 when the system does not have constraints.
1253
1254 Berendsen pressure coupling
1255 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1256
1257 The Berendsen algorithm rescales the coordinates and box vectors every
1258 step, or every :math:`n_\mathrm{PC}` steps, with a matrix :math:`\mu`,
1259 which has the effect of a first-order kinetic relaxation of the pressure
1260 towards a given reference pressure :math:`{\bf P}_0` according to
1261
1262 .. math:: \frac{{\mbox{d}}{\bf P}}{{\mbox{d}}t} = \frac{{\bf P}_0-{\bf P}}{\tau_p}.
1263
1264 The scaling matrix :math:`\mu` is given by
1265
1266 .. math::  \mu_{ij}
1267    = \delta_{ij} - \frac{n_\mathrm{PC}\Delta t}{3\, \tau_p} \beta_{ij} \{P_{0ij} - P_{ij}(t) \}.
1268    :label: eqnmu
1269
1270 Here, :math:`\beta` is the isothermal compressibility of the system. In
1271 most cases this will be a diagonal matrix, with equal elements on the
1272 diagonal, the value of which is generally not known. It suffices to take
1273 a rough estimate because the value of :math:`\beta` only influences the
1274 non-critical time constant of the pressure relaxation without affecting
1275 the average pressure itself. For water at 1 atm and 300 K
1276 :math:`\beta = 4.6 \times 10^{-10}`
1277 Pa\ :math:`^{-1} = 4.6 \times 10^{-5}` bar\ :math:`^{-1}`, which is
1278 :math:`7.6 \times 10^{-4}` MD units (see chapter :ref:`defunits`). Most
1279 other liquids have similar values. When scaling completely
1280 anisotropically, the system has to be rotated in order to obey
1281 :eq:`eqn. %s <eqnboxrot>`. This rotation is approximated in first order in the
1282 scaling, which is usually less than :math:`10^{-4}`. The actual scaling
1283 matrix :math:`\mu'` is
1284
1285 .. math::
1286
1287    \mathbf{\mu'} = 
1288    \left(\begin{array}{ccc}
1289    \mu_{xx} & \mu_{xy} + \mu_{yx} & \mu_{xz} + \mu_{zx} \\
1290    0        & \mu_{yy}            & \mu_{yz} + \mu_{zy} \\
1291    0        & 0                   & \mu_{zz}
1292    \end{array}\right).
1293
1294 The velocities are neither scaled nor rotated. Since the equations of
1295 motion are modified by pressure coupling, the conserved energy quantity
1296 also needs to be modified. For first order pressure coupling, the work
1297 the barostat applies to the system every step needs to be subtracted
1298 from the total energy to obtain the conserved energy quantity:
1299
1300 .. math::
1301
1302    - \sum_{i,j} (\mu_{ij} -\delta_{ij}) P_{ij} V =
1303    \sum_{i,j} 2(\mu_{ij} -\delta_{ij}) \Xi_{ij}
1304
1305 where :math:`\delta_{ij}` is the Kronecker delta and :math:`{\bf \Xi}`
1306 is the virial. Note that the factor 2 originates from the factor
1307 :math:`\frac{1}{2}` in the virial definition
1308 (:eq:`eqn. %s <eqnXi>`).
1309
1310 In |Gromacs|, the Berendsen scaling can also be done isotropically, which
1311 means that instead of :math:`\mathbf{P}` a diagonal matrix
1312 with elements of size trace\ :math:`(\mathbf{P})/3` is
1313 used. For systems with interfaces, semi-isotropic scaling can be useful.
1314 In this case, the :math:`x/y`-directions are scaled isotropically and
1315 the :math:`z` direction is scaled independently. The compressibility in
1316 the :math:`x/y` or :math:`z`-direction can be set to zero, to scale only
1317 in the other direction(s).
1318
1319 If you allow full anisotropic deformations and use constraints you might
1320 have to scale more slowly or decrease your timestep to avoid errors from
1321 the constraint algorithms. It is important to note that although the
1322 Berendsen pressure control algorithm yields a simulation with the
1323 correct average pressure, it does not yield the exact NPT ensemble, and
1324 it is not yet clear exactly what errors this approximation may yield.
1325
1326 Parrinello-Rahman pressure coupling
1327 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1328
1329 In cases where the fluctuations in pressure or volume are important *per
1330 se* (*e.g.* to calculate thermodynamic properties), especially for small
1331 systems, it may be a problem that the exact ensemble is not well defined
1332 for the weak-coupling scheme, and that it does not simulate the true NPT
1333 ensemble.
1334
1335 |Gromacs| also supports constant-pressure simulations using the
1336 Parrinello-Rahman approach \ :ref:`38 <refParrinello81>`,
1337 :ref:`39 <refNose83>`, which is similar to the Nosé-Hoover temperature
1338 coupling, and in theory gives the true NPT ensemble. With the
1339 Parrinello-Rahman barostat, the box vectors as represented by the matrix
1340 obey the matrix equation of motion [2]_
1341
1342 .. math:: \frac{{\mbox{d}}\mathbf{b}^2}{{\mbox{d}}t^2}= V \mathbf{W}^{-1} \mathbf{b}'^{-1} \left( \mathbf{P} - \mathbf{P}_{ref}\right).
1343
1344 The volume of the box is denoted :math:`V`, and
1345 :math:`\mathbf{W}` is a matrix parameter that determines
1346 the strength of the coupling. The matrices and :math:`_{ref}` are the
1347 current and reference pressures, respectively.
1348
1349 The equations of motion for the particles are also changed, just as for
1350 the Nosé-Hoover coupling. In most cases you would combine the
1351 Parrinello-Rahman barostat with the Nosé-Hoover thermostat, but to keep
1352 it simple we only show the Parrinello-Rahman modification here. The
1353 modified Hamiltonian, which will be conserved, is:
1354
1355 .. math::
1356
1357    E_\mathrm{pot} + E_\mathrm{kin} +  \sum_i P_{ii} V +
1358    \sum_{i,j} \frac{1}{2} W_{ij}  \left( \frac{{\mbox{d}}b_{ij}}{{\mbox{d}}t} \right)^2
1359
1360 The equations of motion for the atoms, obtained from the Hamiltonian
1361 are:
1362
1363 .. math::
1364
1365    \begin{aligned}
1366     \frac {{\mbox{d}}^2\mathbf{r}_i}{{\mbox{d}}t^2} & = & \frac{\mathbf{F}_i}{m_i} -
1367    \mathbf{M} \frac{{\mbox{d}}\mathbf{r}_i}{{\mbox{d}}t} , \\ \mathbf{M} & = & \mathbf{b}^{-1} \left[
1368      \mathbf{b} \frac{{\mbox{d}}\mathbf{b}'}{{\mbox{d}}t} + \frac{{\mbox{d}}\mathbf{b}}{{\mbox{d}}t} \mathbf{b}'
1369      \right] \mathbf{b}'^{-1}.
1370      \end{aligned}
1371
1372 This extra term has the appearance of a friction, but it should be
1373 noted that it is ficticious, and rather an effect of the
1374 Parrinello-Rahman equations of motion being defined with all particle
1375 coordinates represented relative to the box vectors, while |Gromacs| uses
1376 normal Cartesian coordinates for positions, velocities and forces. It is
1377 worth noting that the kinetic energy too should formally be calculated
1378 based on velocities relative to the box vectors. This can have an effect
1379 e.g. for external constant stress, but for now we only support coupling
1380 to constant external pressures, and for any normal simulation the
1381 velocities of box vectors should be extremely small compared to particle
1382 velocities. Gang Liu has done some work on deriving this for Cartesian
1383 coordinates\ :ref:`40 <refLiu2015>` that we will try to implement at some
1384 point in the future together with support for external stress.
1385
1386 The (inverse) mass parameter matrix
1387 :math:`\mathbf{W}^{-1}` determines the strength of the
1388 coupling, and how the box can be deformed. The box restriction
1389 (:eq:`%s <eqnboxrot>`) will be fulfilled automatically if the corresponding
1390 elements of :math:`\mathbf{W}^{-1}` are zero. Since the
1391 coupling strength also depends on the size of your box, we prefer to
1392 calculate it automatically in |Gromacs|. You only have to provide the
1393 approximate isothermal compressibilities :math:`\beta` and the pressure
1394 time constant :math:`\tau_p` in the input file (:math:`L` is the largest
1395 box matrix element):
1396
1397 .. math::
1398
1399    \left(
1400    \mathbf{W}^{-1} \right)_{ij} = \frac{4 \pi^2 \beta_{ij}}{3 \tau_p^2 L}.
1401
1402 Just as for the Nosé-Hoover thermostat, you should realize that the
1403 Parrinello-Rahman time constant is *not* equivalent to the relaxation
1404 time used in the Berendsen pressure coupling algorithm. In most cases
1405 you will need to use a 4–5 times larger time constant with
1406 Parrinello-Rahman coupling. If your pressure is very far from
1407 equilibrium, the Parrinello-Rahman coupling may result in very large box
1408 oscillations that could even crash your run. In that case you would have
1409 to increase the time constant, or (better) use the weak-coupling scheme
1410 to reach the target pressure, and then switch to Parrinello-Rahman
1411 coupling once the system is in equilibrium. Additionally, using the
1412 leap-frog algorithm, the pressure at time :math:`t` is not available
1413 until after the time step has completed, and so the pressure from the
1414 previous step must be used, which makes the algorithm not directly
1415 reversible, and may not be appropriate for high precision thermodynamic
1416 calculations.
1417
1418 Surface-tension coupling
1419 ^^^^^^^^^^^^^^^^^^^^^^^^
1420
1421 When a periodic system consists of more than one phase, separated by
1422 surfaces which are parallel to the :math:`xy`-plane, the surface tension
1423 and the :math:`z`-component of the pressure can be coupled to a pressure
1424 bath. Presently, this only works with the Berendsen pressure coupling
1425 algorithm in |Gromacs|. The average surface tension :math:`\gamma(t)` can
1426 be calculated from the difference between the normal and the lateral
1427 pressure
1428
1429 .. math::
1430
1431    \begin{aligned}
1432    \gamma(t) & = & 
1433    \frac{1}{n} \int_0^{L_z}
1434    \left\{ P_{zz}(z,t) - \frac{P_{xx}(z,t) + P_{yy}(z,t)}{2} \right\} \mbox{d}z \\
1435    & = &
1436    \frac{L_z}{n} \left\{ P_{zz}(t) - \frac{P_{xx}(t) + P_{yy}(t)}{2} \right\},\end{aligned}
1437
1438 where :math:`L_z` is the height of the box and :math:`n` is the number
1439 of surfaces. The pressure in the z-direction is corrected by scaling the
1440 height of the box with :math:`\mu_{zz}`
1441
1442 .. math:: \Delta P_{zz} = \frac{\Delta t}{\tau_p} \{ P_{0zz} - P_{zz}(t) \}
1443
1444 .. math:: \mu_{zz} = 1 + \beta_{zz} \Delta P_{zz}
1445
1446 This is similar to normal pressure coupling, except that the factor of
1447 :math:`1/3` is missing. The pressure correction in the
1448 :math:`z`-direction is then used to get the correct convergence for the
1449 surface tension to the reference value :math:`\gamma_0`. The correction
1450 factor for the box length in the :math:`x`/:math:`y`-direction is
1451
1452 .. math::
1453
1454    \mu_{x/y} = 1 + \frac{\Delta t}{2\,\tau_p} \beta_{x/y}
1455            \left( \frac{n \gamma_0}{\mu_{zz} L_z}
1456            - \left\{ P_{zz}(t)+\Delta P_{zz} - \frac{P_{xx}(t) + P_{yy}(t)}{2} \right\} 
1457            \right)
1458
1459 The value of :math:`\beta_{zz}` is more critical than with normal
1460 pressure coupling. Normally an incorrect compressibility will just scale
1461 :math:`\tau_p`, but with surface tension coupling it affects the
1462 convergence of the surface tension. When :math:`\beta_{zz}` is set to
1463 zero (constant box height), :math:`\Delta P_{zz}` is also set to zero,
1464 which is necessary for obtaining the correct surface tension.
1465
1466 MTTK pressure control algorithms
1467 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1468
1469 As mentioned in the previous section, one weakness of leap-frog
1470 integration is in constant pressure simulations, since the pressure
1471 requires a calculation of both the virial and the kinetic energy at the
1472 full time step; for leap-frog, this information is not available until
1473 *after* the full timestep. Velocity Verlet does allow the calculation,
1474 at the cost of an extra round of global communication, and can compute,
1475 mod any integration errors, the true NPT ensemble.
1476
1477 The full equations, combining both pressure coupling and temperature
1478 coupling, are taken from Martyna *et al.*  :ref:`35 <refMartyna1996>` and
1479 Tuckerman \ :ref:`41 <refTuckerman2006>` and are referred to here as MTTK
1480 equations (Martyna-Tuckerman-Tobias-Klein). We introduce for convenience
1481 :math:`\epsilon = (1/3)\ln (V/V_0)`, where :math:`V_0` is a reference
1482 volume. The momentum of :math:`\epsilon` is
1483 :math:`{v_{\epsilon}}= p_{\epsilon}/W =
1484 \dot{\epsilon} = \dot{V}/3V`, and define :math:`\alpha = 1 + 3/N_{dof}`
1485 (see Ref \ :ref:`41 <refTuckerman2006>`)
1486
1487 The isobaric equations are
1488
1489 .. math::
1490
1491    \begin{aligned}
1492    \dot{{{\mathbf{r}}}}_i &=& \frac{{{\mathbf{p}}}_i}{m_i} + \frac{{p_{\epsilon}}}{W} {{\mathbf{r}}}_i \nonumber \\
1493    \frac{\dot{{{\mathbf{p}}}}_i}{m_i} &=& \frac{1}{m_i}{{\mathbf{F}}}_i - \alpha\frac{{p_{\epsilon}}}{W} \frac{{{\mathbf{p}}}_i}{m_i} \nonumber \\
1494    \dot{\epsilon} &=& \frac{{p_{\epsilon}}}{W} \nonumber \\
1495    \frac{\dot{{p_{\epsilon}}}}{W} &=& \frac{3V}{W}(P_{\mathrm{int}} - P) + (\alpha-1)\left(\sum_{n=1}^N\frac{{{\mathbf{p}}}_i^2}{m_i}\right),\\\end{aligned}
1496
1497 where
1498
1499 .. math::
1500
1501    \begin{aligned}
1502    P_{\mathrm{int}} &=& P_{\mathrm{kin}} -P_{\mathrm{vir}} = \frac{1}{3V}\left[\sum_{i=1}^N \left(\frac{{{\mathbf{p}}}_i^2}{2m_i} - {{\mathbf{r}}}_i \cdot {{\mathbf{F}}}_i\
1503    \right)\right].\end{aligned}
1504
1505 The terms including :math:`\alpha` are required to make phase space
1506 incompressible \ :ref:`41 <refTuckerman2006>`. The :math:`\epsilon`
1507 acceleration term can be rewritten as
1508
1509 .. math::
1510
1511    \begin{aligned}
1512    \frac{\dot{{p_{\epsilon}}}}{W} &=& \frac{3V}{W}\left(\alpha P_{\mathrm{kin}} - P_{\mathrm{vir}} - P\right)\end{aligned}
1513
1514 In terms of velocities, these equations become
1515
1516 .. math::
1517
1518    \begin{aligned}
1519    \dot{{{\mathbf{r}}}}_i &=& {{\mathbf{v}}}_i + {v_{\epsilon}}{{\mathbf{r}}}_i \nonumber \\
1520    \dot{{{\mathbf{v}}}}_i &=& \frac{1}{m_i}{{\mathbf{F}}}_i - \alpha{v_{\epsilon}}{{\mathbf{v}}}_i \nonumber \\
1521    \dot{\epsilon} &=& {v_{\epsilon}}\nonumber \\
1522    \dot{{v_{\epsilon}}} &=& \frac{3V}{W}(P_{\mathrm{int}} - P) + (\alpha-1)\left( \sum_{n=1}^N \frac{1}{2} m_i {{\mathbf{v}}}_i^2\right)\nonumber \\
1523    P_{\mathrm{int}} &=& P_{\mathrm{kin}} - P_{\mathrm{vir}} = \frac{1}{3V}\left[\sum_{i=1}^N \left(\frac{1}{2} m_i{{\mathbf{v}}}_i^2 - {{\mathbf{r}}}_i \cdot {{\mathbf{F}}}_i\right)\right]\end{aligned}
1524
1525 For these equations, the conserved quantity is
1526
1527 .. math::
1528
1529    \begin{aligned}
1530    H = \sum_{i=1}^{N} \frac{{{\mathbf{p}}}_i^2}{2m_i} + U\left({{\mathbf{r}}}_1,{{\mathbf{r}}}_2,\ldots,{{\mathbf{r}}}_N\right) + \frac{p_\epsilon}{2W} + PV\end{aligned}
1531
1532 The next step is to add temperature control. Adding Nosé-Hoover chains,
1533 including to the barostat degree of freedom, where we use :math:`\eta`
1534 for the barostat Nosé-Hoover variables, and :math:`Q^{\prime}` for the
1535 coupling constants of the thermostats of the barostats, we get
1536
1537 .. math::
1538
1539    \begin{aligned}
1540    \dot{{{\mathbf{r}}}}_i &=& \frac{{{\mathbf{p}}}_i}{m_i} + \frac{{p_{\epsilon}}}{W} {{\mathbf{r}}}_i \nonumber \\
1541    \frac{\dot{{{\mathbf{p}}}}_i}{m_i} &=& \frac{1}{m_i}{{\mathbf{F}}}_i - \alpha\frac{{p_{\epsilon}}}{W} \frac{{{\mathbf{p}}}_i}{m_i} - \frac{p_{\xi_1}}{Q_1}\frac{{{\mathbf{p}}}_i}{m_i}\nonumber \\
1542    \dot{\epsilon} &=& \frac{{p_{\epsilon}}}{W} \nonumber \\
1543    \frac{\dot{{p_{\epsilon}}}}{W} &=& \frac{3V}{W}(\alpha P_{\mathrm{kin}} - P_{\mathrm{vir}} - P) -\frac{p_{\eta_1}}{Q^{\prime}_1}{p_{\epsilon}}\nonumber \\
1544    \dot{\xi}_k &=& \frac{p_{\xi_k}}{Q_k} \nonumber \\ 
1545    \dot{\eta}_k &=& \frac{p_{\eta_k}}{Q^{\prime}_k} \nonumber \\
1546    \dot{p}_{\xi_k} &=& G_k - \frac{p_{\xi_{k+1}}}{Q_{k+1}} \;\;\;\; k=1,\ldots, M-1 \nonumber \\ 
1547    \dot{p}_{\eta_k} &=& G^\prime_k - \frac{p_{\eta_{k+1}}}{Q^\prime_{k+1}} \;\;\;\; k=1,\ldots, M-1 \nonumber \\
1548    \dot{p}_{\xi_M} &=& G_M \nonumber \\
1549    \dot{p}_{\eta_M} &=& G^\prime_M, \nonumber \\\end{aligned}
1550
1551 where
1552
1553 .. math::
1554
1555    \begin{aligned}
1556    P_{\mathrm{int}} &=& P_{\mathrm{kin}} - P_{\mathrm{vir}} = \frac{1}{3V}\left[\sum_{i=1}^N \left(\frac{{{\mathbf{p}}}_i^2}{2m_i} - {{\mathbf{r}}}_i \cdot {{\mathbf{F}}}_i\right)\right] \nonumber \\
1557    G_1  &=& \sum_{i=1}^N \frac{{{\mathbf{p}}}^2_i}{m_i} - N_f kT \nonumber \\
1558    G_k  &=&  \frac{p^2_{\xi_{k-1}}}{2Q_{k-1}} - kT \;\; k = 2,\ldots,M \nonumber \\
1559    G^\prime_1 &=& \frac{{p_{\epsilon}}^2}{2W} - kT \nonumber \\
1560    G^\prime_k &=& \frac{p^2_{\eta_{k-1}}}{2Q^\prime_{k-1}} - kT \;\; k = 2,\ldots,M\end{aligned}
1561
1562 The conserved quantity is now
1563
1564 .. math::
1565
1566    \begin{aligned}
1567    H = \sum_{i=1}^{N} \frac{{{\mathbf{p}}}_i}{2m_i} + U\left({{\mathbf{r}}}_1,{{\mathbf{r}}}_2,\ldots,{{\mathbf{r}}}_N\right) + \frac{p^2_\epsilon}{2W} + PV + \nonumber \\
1568    \sum_{k=1}^M\frac{p^2_{\xi_k}}{2Q_k} +\sum_{k=1}^M\frac{p^2_{\eta_k}}{2Q^{\prime}_k} + N_fkT\xi_1 +  kT\sum_{i=2}^M \xi_k + kT\sum_{k=1}^M \eta_k\end{aligned}
1569
1570 Returning to the Trotter decomposition formalism, for pressure control
1571 and temperature control \ :ref:`35 <refMartyna1996>` we get:
1572
1573 .. math::
1574
1575    \begin{aligned}
1576    iL = iL_1 + iL_2 + iL_{\epsilon,1} + iL_{\epsilon,2} + iL_{\mathrm{NHC-baro}} + iL_{\mathrm{NHC}}\end{aligned}
1577
1578 where “NHC-baro” corresponds to the Nosè-Hoover chain of the barostat,
1579 and NHC corresponds to the NHC of the particles,
1580
1581 .. math::
1582
1583    \begin{aligned}
1584    iL_1 &=& \sum_{i=1}^N \left[\frac{{{\mathbf{p}}}_i}{m_i} + \frac{{p_{\epsilon}}}{W}{{\mathbf{r}}}_i\right]\cdot \frac{\partial}{\partial {{\mathbf{r}}}_i} \\
1585    iL_2 &=& \sum_{i=1}^N {{\mathbf{F}}}_i - \alpha \frac{{p_{\epsilon}}}{W}{{\mathbf{p}}}_i \cdot \frac{\partial}{\partial {{\mathbf{p}}}_i} \\
1586    iL_{\epsilon,1} &=& \frac{p_\epsilon}{W} \frac{\partial}{\partial \epsilon}\\
1587    iL_{\epsilon,2} &=& G_{\epsilon} \frac{\partial}{\partial p_\epsilon}\end{aligned}
1588
1589 and where
1590
1591 .. math::
1592
1593    \begin{aligned}
1594    G_{\epsilon} = 3V\left(\alpha P_{\mathrm{kin}} - P_{\mathrm{vir}} - P\right)\end{aligned}
1595
1596 Using the Trotter decomposition, we get
1597
1598 .. math::
1599
1600    \begin{aligned}
1601    \exp(iL{\Delta t}) &=& \exp\left(iL_{\mathrm{NHC-baro}}{\Delta t}/2\right)\exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right) \nonumber \nonumber \\
1602    &&\exp\left(iL_{\epsilon,2}{\Delta t}/2\right) \exp\left(iL_2 {\Delta t}/2\right) \nonumber \nonumber \\
1603    &&\exp\left(iL_{\epsilon,1}{\Delta t}\right) \exp\left(iL_1 {\Delta t}\right) \nonumber \nonumber \\
1604    &&\exp\left(iL_2 {\Delta t}/2\right) \exp\left(iL_{\epsilon,2}{\Delta t}/2\right) \nonumber \nonumber \\
1605    &&\exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right)\exp\left(iL_{\mathrm{NHC-baro}}{\Delta t}/2\right) + \mathcal{O}({\Delta t}^3)\end{aligned}
1606
1607 The action of :math:`\exp\left(iL_1 {\Delta t}\right)` comes from the
1608 solution of the the differential equation
1609 :math:`\dot{{{\mathbf{r}}}}_i = {{\mathbf{v}}}_i + {v_{\epsilon}}{{\mathbf{r}}}_i`
1610 with
1611 :math:`{{\mathbf{v}}}_i = {{\mathbf{p}}}_i/m_i`
1612 and :math:`{v_{\epsilon}}` constant with initial condition
1613 :math:`{{\mathbf{r}}}_i(0)`, evaluate at
1614 :math:`t=\Delta t`. This yields the evolution
1615
1616 .. math:: {{\mathbf{r}}}_i({\Delta t}) = {{\mathbf{r}}}_i(0)e^{{v_{\epsilon}}{\Delta t}} + \Delta t {{\mathbf{v}}}_i(0) e^{{v_{\epsilon}}{\Delta t}/2} {\frac{\sinh{\left( {v_{\epsilon}}{\Delta t}/2\right)}}{{v_{\epsilon}}{\Delta t}/2}}.
1617
1618 The action of :math:`\exp\left(iL_2 {\Delta t}/2\right)` comes from the
1619 solution of the differential equation
1620 :math:`\dot{{{\mathbf{v}}}}_i = \frac{{{\mathbf{F}}}_i}{m_i} -
1621 \alpha{v_{\epsilon}}{{\mathbf{v}}}_i`, yielding
1622
1623 .. math:: {{\mathbf{v}}}_i({\Delta t}/2) = {{\mathbf{v}}}_i(0)e^{-\alpha{v_{\epsilon}}{\Delta t}/2} + \frac{\Delta t}{2m_i}{{\mathbf{F}}}_i(0) e^{-\alpha{v_{\epsilon}}{\Delta t}/4}{\frac{\sinh{\left( \alpha{v_{\epsilon}}{\Delta t}/4\right)}}{\alpha{v_{\epsilon}}{\Delta t}/4}}.
1624
1625 *md-vv-avek* uses the full step kinetic energies for determining the
1626 pressure with the pressure control, but the half-step-averaged kinetic
1627 energy for the temperatures, which can be written as a Trotter
1628 decomposition as
1629
1630 .. math::
1631
1632    \begin{aligned}
1633    \exp(iL{\Delta t}) &=& \exp\left(iL_{\mathrm{NHC-baro}}{\Delta t}/2\right)\nonumber \exp\left(iL_{\epsilon,2}{\Delta t}/2\right) \exp\left(iL_2 {\Delta t}/2\right) \nonumber \\
1634    &&\exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right) \exp\left(iL_{\epsilon,1}{\Delta t}\right) \exp\left(iL_1 {\Delta t}\right) \exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right) \nonumber \\
1635    &&\exp\left(iL_2 {\Delta t}/2\right) \exp\left(iL_{\epsilon,2}{\Delta t}/2\right) \exp\left(iL_{\mathrm{NHC-baro}}{\Delta t}/2\right) + \mathcal{O}({\Delta t}^3)\end{aligned}
1636
1637 With constraints, the equations become significantly more complicated,
1638 in that each of these equations need to be solved iteratively for the
1639 constraint forces. Before |Gromacs| 5.1, these iterative constraints were
1640 solved as described in \ :ref:`42 <refYu2010>`. From |Gromacs| 5.1 onward,
1641 MTTK with constraints has been removed because of numerical stability
1642 issues with the iterations.
1643
1644 Infrequent evaluation of temperature and pressure coupling
1645 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1646
1647 Temperature and pressure control require global communication to compute
1648 the kinetic energy and virial, which can become costly if performed
1649 every step for large systems. We can rearrange the Trotter decomposition
1650 to give alternate symplectic, reversible integrator with the coupling
1651 steps every :math:`n` steps instead of every steps. These new
1652 integrators will diverge if the coupling time step is too large, as the
1653 auxiliary variable integrations will not converge. However, in most
1654 cases, long coupling times are more appropriate, as they disturb the
1655 dynamics less \ :ref:`35 <refMartyna1996>`.
1656
1657 Standard velocity Verlet with Nosé-Hoover temperature control has a
1658 Trotter expansion
1659
1660 .. math::
1661
1662    \begin{aligned}
1663    \exp(iL{\Delta t}) &\approx& \exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right) \exp\left(iL_2 {\Delta t}/2\right) \nonumber \\
1664    &&\exp\left(iL_1 {\Delta t}\right) \exp\left(iL_2 {\Delta t}/2\right) \exp\left(iL_{\mathrm{NHC}}{\Delta t}/2\right).\end{aligned}
1665
1666 If the Nosé-Hoover chain is sufficiently slow with respect to the
1667 motions of the system, we can write an alternate integrator over
1668 :math:`n` steps for velocity Verlet as
1669
1670 .. math::
1671
1672    \begin{aligned}
1673    \exp(iL{\Delta t}) &\approx& (\exp\left(iL_{\mathrm{NHC}}(n{\Delta t}/2)\right)\left[\exp\left(iL_2 {\Delta t}/2\right)\right. \nonumber \\
1674    &&\left.\exp\left(iL_1 {\Delta t}\right) \exp\left(iL_2 {\Delta t}/2\right)\right]^n \exp\left(iL_{\mathrm{NHC}}(n{\Delta t}/2)\right).\end{aligned}
1675
1676 For pressure control, this becomes
1677
1678 .. math::
1679
1680    \begin{aligned}
1681    \exp(iL{\Delta t}) &\approx& \exp\left(iL_{\mathrm{NHC-baro}}(n{\Delta t}/2)\right)\exp\left(iL_{\mathrm{NHC}}(n{\Delta t}/2)\right) \nonumber \nonumber \\
1682    &&\exp\left(iL_{\epsilon,2}(n{\Delta t}/2)\right) \left[\exp\left(iL_2 {\Delta t}/2\right)\right. \nonumber \nonumber \\
1683    &&\exp\left(iL_{\epsilon,1}{\Delta t}\right) \exp\left(iL_1 {\Delta t}\right) \nonumber \nonumber \\
1684    &&\left.\exp\left(iL_2 {\Delta t}/2\right)\right]^n \exp\left(iL_{\epsilon,2}(n{\Delta t}/2)\right) \nonumber \nonumber \\
1685    &&\exp\left(iL_{\mathrm{NHC}}(n{\Delta t}/2)\right)\exp\left(iL_{\mathrm{NHC-baro}}(n{\Delta t}/2)\right),\end{aligned}
1686
1687 where the box volume integration occurs every step, but the auxiliary
1688 variable integrations happen every :math:`n` steps.
1689
1690 The complete update algorithm
1691 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1692 .. _gmx_md_update:
1693
1694 **THE UPDATE ALGORITHM**
1695
1696 --------------
1697
1698  
1699  Given:
1700  Positions :math:`\mathbf{r}` of all atoms at time
1701  :math:`t`
1702  Velocities :math:`\mathbf{v}` of all atoms at time
1703  :math:`t-{{\frac{1}{2}}{{\Delta t}}}`
1704  Accelerations :math:`\mathbf{F}/m` on all atoms at time
1705  :math:`t`.
1706  (Forces are computed disregarding any constraints)
1707  Total kinetic energy and virial at :math:`t-{{\Delta t}}`
1708  :math:`\Downarrow`
1709
1710  1. Compute the scaling factors :math:`\lambda` and :math:`\mu`
1711  according to :eq:`eqns. %s <eqnlambda>` and :eq:`%s <eqnmu>`
1712  :math:`\Downarrow`
1713
1714  2. Update and scale velocities:
1715  :math:`\mathbf{v}' =  \lambda (\mathbf{v} +
1716  \mathbf{a} \Delta t)`
1717  :math:`\Downarrow`
1718
1719  3. Compute new unconstrained coordinates:
1720  :math:`\mathbf{r}' = \mathbf{r} + \mathbf{v}'
1721  \Delta t`
1722  :math:`\Downarrow`
1723
1724  4. Apply constraint algorithm to coordinates:
1725  constrain(\ :math:`\mathbf{r}^{'} \rightarrow  \mathbf{r}'';
1726  \,  \mathbf{r}`)
1727  :math:`\Downarrow`
1728
1729  5. Correct velocities for constraints:
1730  :math:`\mathbf{v} = (\mathbf{r}'' -
1731  \mathbf{r}) / \Delta t`
1732  :math:`\Downarrow`
1733
1734  6. Scale coordinates and box:
1735  :math:`\mathbf{r} = \mu \mathbf{r}''; \mathbf{b} =
1736  \mu  \mathbf{b}`
1737
1738 The complete algorithm for the update of velocities and coordinates is
1739 given using leap-frog in :ref:`the outline above <gmx_md_update>`
1740 The SHAKE algorithm of step 4 is explained below.
1741
1742 |Gromacs| has a provision to *freeze* (prevent motion of) selected
1743 particles, which must be defined as a *freeze group*. This is
1744 implemented using a *freeze factor* :math:`\mathbf{f}_g`,
1745 which is a vector, and differs for each freeze group (see
1746 sec. :ref:`groupconcept`). This vector contains only zero (freeze) or one
1747 (don’t freeze). When we take this freeze factor and the external
1748 acceleration :math:`\mathbf{a}_h` into account the update
1749 algorithm for the velocities becomes
1750
1751 .. math:: \mathbf{v}(t+{\frac{\Delta t}{2}})~=~\mathbf{f}_g * \lambda * \left[ \mathbf{v}(t-{\frac{\Delta t}{2}}) +\frac{\mathbf{F}(t)}{m}\Delta t + \mathbf{a}_h \Delta t \right],
1752
1753 where :math:`g` and :math:`h` are group indices which differ per atom.
1754
1755 Output step
1756 ~~~~~~~~~~~
1757
1758 The most important output of the MD run is the *trajectory file*, which
1759 contains particle coordinates and (optionally) velocities at regular
1760 intervals. The trajectory file contains frames that could include
1761 positions, velocities and/or forces, as well as information about the
1762 dimensions of the simulation volume, integration step, integration time,
1763 etc. The interpretation of the time varies with the integrator chosen,
1764 as described above. For Velocity Verlet integrators, velocities labeled
1765 at time :math:`t` are for that time. For other integrators (e.g.
1766 leap-frog, stochastic dynamics), the velocities labeled at time
1767 :math:`t` are for time :math:`t - {{\frac{1}{2}}{{\Delta t}}}`.
1768
1769 Since the trajectory files are lengthy, one should not save every step!
1770 To retain all information it suffices to write a frame every 15 steps,
1771 since at least 30 steps are made per period of the highest frequency in
1772 the system, and Shannon’s sampling theorem states that two samples per
1773 period of the highest frequency in a band-limited signal contain all
1774 available information. But that still gives very long files! So, if the
1775 highest frequencies are not of interest, 10 or 20 samples per ps may
1776 suffice. Be aware of the distortion of high-frequency motions by the
1777 *stroboscopic effect*, called *aliasing*: higher frequencies are
1778 mirrored with respect to the sampling frequency and appear as lower
1779 frequencies.
1780
1781 |Gromacs| can also write reduced-precision coordinates for a subset of the
1782 simulation system to a special compressed trajectory file format. All
1783 the other tools can read and write this format. See the User Guide for
1784 details on how to set up your :ref:`mdp` file to have :ref:`mdrun <gmx mdrun>` use this feature.
1785
1786 .. [1]
1787    Note that some derivations, an alternative notation
1788    :math:`\xi_{\mathrm{alt}} = v_{\xi} = p_{\xi}/Q` is used.
1789
1790 .. [2]
1791    The box matrix representation in corresponds to the transpose of the
1792    box matrix representation in the paper by Nosé and Klein. Because of
1793    this, some of our equations will look slightly different.
1794