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