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