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