5431355c7b55b3878e71ecf9e371d9dd97109058
[alexxy/gromacs.git] / docs / reference-manual / constraint-algorithms.rst
1 Constraint algorithms
2 ---------------------
3
4 Constraints can be imposed in |Gromacs| using LINCS (default) or the
5 traditional SHAKE method.
6
7
8 .. _shake:
9
10 SHAKE
11 ~~~~~
12
13 The SHAKE \ :ref:`46 <refRyckaert77>` algorithm changes a
14 set of unconstrained coordinates :math:`\mathbf{r}^{'}` to
15 a set of coordinates :math:`\mathbf{r}''` that fulfill a
16 list of distance constraints, using a set
17 :math:`\mathbf{r}` reference, as
18
19 .. math:: {\rm SHAKE}(\mathbf{r}^{'} \rightarrow \mathbf{r}'';\, \mathbf{r})
20
21 This action is consistent with solving a set of Lagrange multipliers in
22 the constrained equations of motion. SHAKE needs a *relative tolerance*;
23 it will continue until all constraints are satisfied within that
24 relative tolerance. An error message is given if SHAKE cannot reset the
25 coordinates because the deviation is too large, or if a given number of
26 iterations is surpassed.
27
28 Assume the equations of motion must fulfill :math:`K` holonomic
29 constraints, expressed as
30
31 .. math:: \sigma_k(\mathbf{r}_1 \ldots \mathbf{r}_N) = 0; \;\; k=1 \ldots K.
32
33 For example,
34 :math:`(\mathbf{r}_1 - \mathbf{r}_2)^2 - b^2 = 0`.
35 Then the forces are defined as
36
37 .. math::
38
39    - \frac{\partial}{\partial \mathbf{r}_i} \left( V + \sum_{k=1}^K \lambda_k
40    \sigma_k \right),
41
42 where :math:`\lambda_k` are Lagrange multipliers which must be solved
43 to fulfill the constraint equations. The second part of this sum
44 determines the *constraint forces* :math:`\mathbf{G}_i`,
45 defined by
46
47 .. math::
48
49    \mathbf{G}_i = -\sum_{k=1}^K \lambda_k \frac{\partial \sigma_k}{\partial
50    \mathbf{r}_i}
51
52 The displacement due to the constraint forces in the leap-frog or
53 Verlet algorithm is equal to
54 :math:`(\mathbf{G}_i/m_i)({{\Delta t}})^2`. Solving the
55 Lagrange multipliers (and hence the displacements) requires the solution
56 of a set of coupled equations of the second degree. These are solved
57 iteratively by SHAKE. :ref:`settle` 
58
59 .. _settle:
60
61 SETTLE
62 ~~~~~~
63
64 For the special case of rigid
65 water molecules, that often make up more than 80% of the simulation
66 system we have implemented the SETTLE algorithm \ :ref:`47 <refMiyamoto92>`
67 (sec. :ref:`constraintalg`).
68
69 For velocity Verlet, an additional round of constraining must be done,
70 to constrain the velocities of the second velocity half step, removing
71 any component of the velocity parallel to the bond vector. This step is
72 called RATTLE, and is covered in more detail in the original Andersen
73 paper \ :ref:`48 <refAndersen1983a>`.
74
75 LINCS
76 ~~~~~
77
78 .. _lincs:
79
80 The LINCS algorithm
81 ^^^^^^^^^^^^^^^^^^^
82
83 LINCS is an algorithm that resets bonds to their correct lengths after
84 an unconstrained update \ :ref:`49 <refHess97>`. The method is non-iterative,
85 as it always uses two steps. Although LINCS is based on matrices, no
86 matrix-matrix multiplications are needed. The method is more stable and
87 faster than SHAKE, but it can only be used with bond constraints and
88 isolated angle constraints, such as the proton angle in OH. Because of
89 its stability, LINCS is especially useful for Brownian dynamics. LINCS
90 has two parameters, which are explained in the subsection parameters.
91 The parallel version of LINCS, P-LINCS, is described in subsection
92 :ref:`plincs`.
93
94 The LINCS formulas
95 ^^^^^^^^^^^^^^^^^^
96
97 We consider a system of :math:`N` particles, with positions given by a
98 :math:`3N` vector :math:`\mathbf{r}(t)`. For molecular
99 dynamics the equations of motion are given by Newton’s Law
100
101 .. math:: {{\mbox{d}}^2 \mathbf{r} \over {\mbox{d}}t^2} = {{\mathbf{M}}^{-1}}\mathbf{F},
102           :label: eqnc1
103
104 where :math:`\mathbf{F}` is the :math:`3N` force vector
105 and :math:`{\mathbf{M}}` is a :math:`3N \times 3N`
106 diagonal matrix, containing the masses of the particles. The system is
107 constrained by :math:`K` time-independent constraint equations
108
109 .. math:: g_i(\mathbf{r}) = | \mathbf{r}_{i_1}-\mathbf{r}_{i_2} | - d_i = 0 ~~~~~~i=1,\ldots,K.
110           :label: eqnc2
111
112 In a numerical integration scheme, LINCS is applied after an
113 unconstrained update, just like SHAKE. The algorithm works in two steps
114 (see figure :numref:`Fig. %s <fig-lincs>`). In the first step, the
115 projections of the new bonds on the old bonds are set to zero. In the
116 second step, a correction is applied for the lengthening of the bonds
117 due to rotation. The numerics for the first step and the second step are
118 very similar. A complete derivation of the algorithm can be found in
119 :ref:`49 <refHess97>`. Only a short description of the first step is given
120 here.
121
122 .. _fig-lincs:
123
124 .. figure:: plots/lincs.*
125    :height: 5.00000cm
126
127    The three position updates needed for one time step. The dashed
128    line is the old bond of length :math:`d`, the solid lines are the new
129    bonds. :math:`l=d \cos \theta` and :math:`p=(2 d^2 - l^2)^{1 \over 2}`.
130
131 A new notation is introduced for the gradient matrix of the constraint
132 equations which appears on the right hand side of this equation:
133
134 .. math::  B_{hi} = {{\partial}g_h \over {\partial}r_i}
135            :label: eqnc3
136
137 Notice that :math:`{\mathbf{B}}` is a :math:`K \times 3N`
138 matrix, it contains the directions of the constraints. The following
139 equation shows how the new constrained coordinates
140 :math:`\mathbf{r}_{n+1}` are related to the unconstrained
141 coordinates :math:`\mathbf{r}_{n+1}^{unc}` by
142
143 .. math::  \begin{array}{c}
144      \mathbf{r}_{n+1}=(\mathbf{I}-\mathbf{T}_n \mathbf{B}_n) \mathbf{r}_{n+1}^{unc} + {\mathbf{T}}_n \mathbf{d}=  
145      \\[2mm]
146      \mathbf{r}_{n+1}^{unc} - 
147    {{\mathbf{M}}^{-1}}\mathbf{B}_n ({\mathbf{B}}_n {{\mathbf{M}}^{-1}}{\mathbf{B}}_n^T)^{-1} ({\mathbf{B}}_n \mathbf{r}_{n+1}^{unc} - \mathbf{d}) 
148    \end{array}
149    :label: eqnm0
150
151 where
152 :math:`{\mathbf{T}}= {{\mathbf{M}}^{-1}}{\mathbf{B}}^T ({\mathbf{B}}{{\mathbf{M}}^{-1}}{\mathbf{B}}^T)^{-1}`.
153 The derivation of this equation from :eq:`eqns. %s <eqnc1>` and
154 :eq:`%s <eqnc2>` can be found in :ref:`49 <refHess97>`.
155
156 This first step does not set the real bond lengths to the prescribed
157 lengths, but the projection of the new bonds onto the old directions of
158 the bonds. To correct for the rotation of bond :math:`i`, the projection
159 of the bond, :math:`p_i`, on the old direction is set to
160
161 .. math::  p_i=\sqrt{2 d_i^2 - l_i^2},
162    :label: eqnm1a
163
164 where :math:`l_i` is the bond length after the first projection. The
165 corrected positions are
166
167 .. math::  \mathbf{r}_{n+1}^*=(\mathbf{I}-\mathbf{T}_n \mathbf{B}_n)\mathbf{r}_{n+1} + {\mathbf{T}}_n \mathbf{p}.
168    :label: eqnm1b
169
170 This correction for rotational effects is actually an iterative
171 process, but during MD only one iteration is applied. The relative
172 constraint deviation after this procedure will be less than 0.0001 for
173 every constraint. In energy minimization, this might not be accurate
174 enough, so the number of iterations is equal to the order of the
175 expansion (see below).
176
177 Half of the CPU time goes to inverting the constraint coupling matrix
178 :math:`{\mathbf{B}}_n {{\mathbf{M}}^{-1}}{\mathbf{B}}_n^T`,
179 which has to be done every time step. This :math:`K \times K` matrix has
180 :math:`1/m_{i_1} + 1/m_{i_2}` on the diagonal. The off-diagonal elements
181 are only non-zero when two bonds are connected, then the element is
182 :math:`\cos \phi /m_c`, where :math:`m_c` is the mass of the atom
183 connecting the two bonds and :math:`\phi` is the angle between the
184 bonds.
185
186 The matrix :math:`\mathbf{T}` is inverted through a power
187 expansion. A :math:`K \times K` matrix :math:`\mathbf{S}`
188 is introduced which is the inverse square root of the diagonal of
189 :math:`\mathbf{B}_n {{\mathbf{M}}^{-1}}{\mathbf{B}}_n^T`.
190 This matrix is used to convert the diagonal elements of the coupling
191 matrix to one:
192
193 .. math:: \begin{array}{c}
194           ({\mathbf{B}}_n {{\mathbf{M}}^{-1}}{\mathbf{B}}_n^T)^{-1}
195           = {\mathbf{S}}{\mathbf{S}}^{-1} ({\mathbf{B}}_n {{\mathbf{M}}^{-1}}{\mathbf{B}}_n^T)^{-1} {\mathbf{S}}^{-1} {\mathbf{S}}\\[2mm]
196           = {\mathbf{S}}({\mathbf{S}}{\mathbf{B}}_n {{\mathbf{M}}^{-1}}{\mathbf{B}}_n^T {\mathbf{S}})^{-1} {\mathbf{S}}=
197           {\mathbf{S}}(\mathbf{I} - \mathbf{A}_n)^{-1} {\mathbf{S}}\end{array}
198           :label: eqnm2
199
200 The matrix :math:`\mathbf{A}_n` is symmetric and sparse
201 and has zeros on the diagonal. Thus a simple trick can be used to
202 calculate the inverse:
203
204 .. math:: (\mathbf{I}-\mathbf{A}_n)^{-1}= 
205           \mathbf{I} + \mathbf{A}_n + \mathbf{A}_n^2 + \mathbf{A}_n^3 + \ldots
206           :label: eqnm3
207
208 This inversion method is only valid if the absolute values of all the
209 eigenvalues of :math:`\mathbf{A}_n` are smaller than one.
210 In molecules with only bond constraints, the connectivity is so low that
211 this will always be true, even if ring structures are present. Problems
212 can arise in angle-constrained molecules. By constraining angles with
213 additional distance constraints, multiple small ring structures are
214 introduced. This gives a high connectivity, leading to large
215 eigenvalues. Therefore LINCS should NOT be used with coupled
216 angle-constraints.
217
218 For molecules with all bonds constrained the eigenvalues of :math:`A`
219 are around 0.4. This means that with each additional order in the
220 expansion :eq:`eqn. %s <eqnm3>` the deviations decrease by a factor 0.4. But for
221 relatively isolated triangles of constraints the largest eigenvalue is
222 around 0.7. Such triangles can occur when removing hydrogen angle
223 vibrations with an additional angle constraint in alcohol groups or when
224 constraining water molecules with LINCS, for instance with flexible
225 constraints. The constraints in such triangles converge twice as slow as
226 the other constraints. Therefore, starting with |Gromacs| 4, additional
227 terms are added to the expansion for such triangles
228
229 .. math:: (\mathbf{I}-\mathbf{A}_n)^{-1} \approx
230           \mathbf{I} + \mathbf{A}_n + \ldots + \mathbf{A}_n^{N_i} +
231           \left(\mathbf{A}^*_n + \ldots + {\mathbf{A}_n^*}^{N_i} \right) \mathbf{A}_n^{N_i}
232           :label: eqnm3ang
233
234 where :math:`N_i` is the normal order of the expansion and
235 :math:`\mathbf{A}^*` only contains the elements of
236 :math:`\mathbf{A}` that couple constraints within rigid
237 triangles, all other elements are zero. In this manner, the accuracy of
238 angle constraints comes close to that of the other constraints, while
239 the series of matrix vector multiplications required for determining the
240 expansion only needs to be extended for a few constraint couplings. This
241 procedure is described in the P-LINCS paper\ :ref:`50 <refHess2008a>`.
242
243 The LINCS Parameters
244 ^^^^^^^^^^^^^^^^^^^^
245
246 The accuracy of LINCS depends on the number of matrices used in the
247 expansion :eq:`eqn. %s <eqnm3>`. For MD calculations a fourth order expansion is
248 enough. For Brownian dynamics with large time steps an eighth order
249 expansion may be necessary. The order is a parameter in the :ref:`mdp` file.
250 The implementation of LINCS is done in such a way that the algorithm
251 will never crash. Even when it is impossible to to reset the constraints
252 LINCS will generate a conformation which fulfills the constraints as
253 well as possible. However, LINCS will generate a warning when in one
254 step a bond rotates over more than a predefined angle. This angle is set
255 by the user in the :ref:`mdp` file.