Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / gmxlib / restcbt.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include "gmxpre.h"
36
37 #include <math.h>
38
39 #include "gromacs/math/units.h"
40 #include "gromacs/math/utilities.h"
41 #include "gromacs/math/vec.h"
42 #include "gromacs/topology/idef.h"
43
44 /* This function computes factors needed for restricted angle potential.
45  * For explanations on formula used see file "restcbt.h" */
46
47 void compute_factors_restangles(int type, const t_iparams forceparams[],
48                                 rvec delta_ante,  rvec delta_post,
49                                 real *prefactor, real *ratio_ante, real *ratio_post, real *v)
50 {
51     real theta_equil, k_bending;
52     real cosine_theta_equil;
53     real c_ante, c_cros, c_post;
54     real norm;
55     real delta_cosine, cosine_theta;
56     real sine_theta_sq;
57     real term_theta_theta_equil;
58
59     k_bending          = forceparams[type].harmonic.krA;
60     theta_equil        =  forceparams[type].harmonic.rA*DEG2RAD;
61     theta_equil        = M_PI - theta_equil;
62     cosine_theta_equil = cos(theta_equil);
63
64     c_ante = iprod(delta_ante, delta_ante);
65     c_cros = iprod(delta_ante, delta_post);
66     c_post = iprod(delta_post, delta_post);
67
68     norm          = gmx_invsqrt(c_ante * c_post);
69     cosine_theta  = c_cros * norm;
70     sine_theta_sq = 1 - cosine_theta * cosine_theta;
71
72     *ratio_ante = c_cros / c_ante;
73     *ratio_post = c_cros / c_post;
74
75     delta_cosine           = cosine_theta - cosine_theta_equil;
76     term_theta_theta_equil = 1 - cosine_theta * cosine_theta_equil;
77     *prefactor             = -(k_bending) * delta_cosine * norm * term_theta_theta_equil / (sine_theta_sq * sine_theta_sq);
78
79     *v = k_bending * 0.5 * delta_cosine * delta_cosine / sine_theta_sq;
80
81 }
82
83
84 /* Compute factors for restricted dihedral potential
85  * For explanations on formula used see file "restcbt.h" */
86 void compute_factors_restrdihs(int type,  const t_iparams forceparams[],
87                                rvec delta_ante, rvec delta_crnt, rvec delta_post,
88                                real *factor_phi_ai_ante, real *factor_phi_ai_crnt, real *factor_phi_ai_post,
89                                real *factor_phi_aj_ante, real *factor_phi_aj_crnt, real *factor_phi_aj_post,
90                                real *factor_phi_ak_ante, real *factor_phi_ak_crnt, real *factor_phi_ak_post,
91                                real *factor_phi_al_ante, real *factor_phi_al_crnt, real *factor_phi_al_post,
92                                real *prefactor_phi, real *v)
93 {
94
95     real phi0, sine_phi0, cosine_phi0;
96     real k_torsion;
97     real c_self_ante, c_self_crnt, c_self_post;
98     real c_cros_ante, c_cros_acrs, c_cros_post;
99     real c_prod, d_post, d_ante;
100     real sine_phi_sq, cosine_phi;
101     real delta_cosine, term_phi_phi0;
102     real ratio_phi_ante, ratio_phi_post;
103     real cos_phi, norm_phi;
104
105     /* Read parameters phi0 and k_torsion */
106     phi0        = forceparams[type].pdihs.phiA * DEG2RAD;
107     cosine_phi0 = cos(phi0);
108     sine_phi0   = sin(phi0);
109     k_torsion   = forceparams[type].pdihs.cpA;
110
111     /* Computation of the cosine of the dihedral angle. The scalar ("dot") product  method
112      * is used. c_*_* cummulate the scalar products of the differences of particles
113      * positions while c_prod, d_ante and d_post are differences of products of scalar
114      * terms that are parts of the derivatives of forces */
115     c_self_ante = iprod(delta_ante, delta_ante);
116     c_self_crnt = iprod(delta_crnt, delta_crnt);
117     c_self_post = iprod(delta_post, delta_post);
118     c_cros_ante = iprod(delta_ante, delta_crnt);
119     c_cros_acrs = iprod(delta_ante, delta_post);
120     c_cros_post = iprod(delta_crnt, delta_post);
121     c_prod      = c_cros_ante * c_cros_post - c_self_crnt * c_cros_acrs;
122     d_ante      = c_self_ante * c_self_crnt - c_cros_ante * c_cros_ante;
123     d_post      = c_self_post * c_self_crnt - c_cros_post * c_cros_post;
124
125     /*  When three consecutive beads align, we obtain values close to zero.
126      *  Here we avoid small values to prevent round-off errors. */
127     if (d_ante < GMX_REAL_EPS)
128     {
129         d_ante = GMX_REAL_EPS;
130     }
131     if (d_post < GMX_REAL_EPS)
132     {
133         d_post = GMX_REAL_EPS;
134     }
135
136     /* Computes the square of the sinus of phi  in sine_phi_sq */
137     norm_phi    = gmx_invsqrt(d_ante * d_post);
138     cosine_phi  = c_prod * norm_phi;
139     sine_phi_sq = 1.0 - cosine_phi * cosine_phi;
140
141     /*  It is possible that cosine_phi is slightly bigger than 1.0 due to round-off errors. */
142     if (sine_phi_sq < 0.0)
143     {
144         sine_phi_sq = 0.0;
145     }
146
147     /* Computation of the differences of cosines (delta_cosine) and a term (term_phi_phi0)
148      * that is part of the common prefactor_phi */
149
150     delta_cosine  = cosine_phi - cosine_phi0;
151     term_phi_phi0 = 1 - cosine_phi * cosine_phi0;
152
153
154     /*      Computation of ratios */
155     ratio_phi_ante = c_prod / d_ante;
156     ratio_phi_post = c_prod / d_post;
157
158     /*      Computation of the prefactor - common term for all forces */
159     *prefactor_phi = -(k_torsion) * delta_cosine * norm_phi * term_phi_phi0 / (sine_phi_sq * sine_phi_sq);
160
161     /* Computation of force factors.  Factors factor_phi_*  are coming from the
162      * derivatives of the torsion angle (phi) with respect to the beads ai, aj, al, ak,
163      * (four) coordinates and they are multiplied in the force computations with the
164      * differences of the particles positions stored in parameters delta_ante,
165      * delta_crnt, delta_post. For formulas see file "restcbt.h" */
166
167     *factor_phi_ai_ante = ratio_phi_ante * c_self_crnt;
168     *factor_phi_ai_crnt = -c_cros_post - ratio_phi_ante * c_cros_ante;
169     *factor_phi_ai_post = c_self_crnt;
170     *factor_phi_aj_ante = -c_cros_post - ratio_phi_ante * (c_self_crnt + c_cros_ante);
171     *factor_phi_aj_crnt = c_cros_post + c_cros_acrs * 2.0 + ratio_phi_ante * (c_self_ante + c_cros_ante) + ratio_phi_post * c_self_post;
172     *factor_phi_aj_post = -(c_cros_ante + c_self_crnt) - ratio_phi_post * c_cros_post;
173     *factor_phi_ak_ante = c_cros_post + c_self_crnt + ratio_phi_ante * c_cros_ante;
174     *factor_phi_ak_crnt = -(c_cros_ante + c_cros_acrs * 2.0)- ratio_phi_ante * c_self_ante - ratio_phi_post * (c_self_post + c_cros_post);
175     *factor_phi_ak_post = c_cros_ante + ratio_phi_post * (c_self_crnt + c_cros_post);
176     *factor_phi_al_ante = -c_self_crnt;
177     *factor_phi_al_crnt = c_cros_ante + ratio_phi_post * c_cros_post;
178     *factor_phi_al_post = -ratio_phi_post * c_self_crnt;
179
180     /* Contribution to energy  - see formula in file "restcbt.h"*/
181     *v = k_torsion * 0.5 * delta_cosine * delta_cosine / sine_phi_sq;
182 }
183
184
185
186 /* Compute factors for CBT potential
187  * For explanations on formula used see file "restcbt.h" */
188
189 void compute_factors_cbtdihs(int type,  const t_iparams forceparams[],
190                              rvec delta_ante, rvec delta_crnt, rvec delta_post,
191                              rvec f_phi_ai, rvec f_phi_aj, rvec f_phi_ak, rvec f_phi_al,
192                              rvec f_theta_ante_ai, rvec f_theta_ante_aj, rvec f_theta_ante_ak,
193                              rvec f_theta_post_aj, rvec f_theta_post_ak, rvec f_theta_post_al,
194                              real * v)
195 {
196     int  j, d;
197     real torsion_coef[NR_CBTDIHS];
198     real c_self_ante, c_self_crnt, c_self_post;
199     real c_cros_ante, c_cros_acrs, c_cros_post;
200     real c_prod, d_ante,  d_post;
201     real norm_phi, norm_theta_ante, norm_theta_post;
202     real cosine_phi, cosine_theta_ante, cosine_theta_post;
203     real sine_theta_ante_sq, sine_theta_post_sq;
204     real sine_theta_ante, sine_theta_post;
205     real prefactor_phi;
206     real ratio_phi_ante, ratio_phi_post;
207     real r1, r2;
208     real factor_phi_ai_ante, factor_phi_ai_crnt, factor_phi_ai_post;
209     real factor_phi_aj_ante, factor_phi_aj_crnt, factor_phi_aj_post;
210     real factor_phi_ak_ante, factor_phi_ak_crnt, factor_phi_ak_post;
211     real factor_phi_al_ante, factor_phi_al_crnt, factor_phi_al_post;
212     real prefactor_theta_ante, ratio_theta_ante_ante, ratio_theta_ante_crnt;
213     real prefactor_theta_post, ratio_theta_post_crnt, ratio_theta_post_post;
214
215     /* The formula for combined bending-torsion potential (see file "restcbt.h") contains
216      * in its expression not only the dihedral angle \f[\phi\f] but also \f[\theta_{i-1}\f]
217      * (theta_ante bellow) and \f[\theta_{i}\f] (theta_post bellow)--- the adjacent bending
218      * angles. The forces for the particles ai, aj, ak, al have components coming from the
219      * derivatives of the potential with respect to all three angles.
220      * This function is organised in 4 parts
221      * PART 1 - Computes force factors common to all the derivatives for the four particles
222      * PART 2 - Computes the force components due to the derivatives of dihedral angle Phi
223      * PART 3 - Computes the force components due to the derivatives of bending angle Theta_Ante
224      * PART 4 - Computes the force components due to the derivatives of bending angle Theta_Post
225      * Bellow we will respct thuis structure */
226
227
228     /* PART 1 - COMPUTES FORCE FACTORS COMMON TO ALL DERIVATIVES FOR THE FOUR PARTICLES */
229
230
231     for (j = 0; (j < NR_CBTDIHS); j++)
232     {
233         torsion_coef[j]  = forceparams[type].cbtdihs.cbtcA[j];
234     }
235
236     /* Computation of the cosine of the dihedral angle. The scalar ("dot") product  method
237      * is used. c_*_* cummulate the scalar products of the differences of particles
238      * positions while c_prod, d_ante and d_post are differences of products of scalar
239      * terms that are parts of the derivatives of forces */
240
241     c_self_ante = iprod(delta_ante, delta_ante);
242     c_self_crnt = iprod(delta_crnt, delta_crnt);
243     c_self_post = iprod(delta_post, delta_post);
244     c_cros_ante = iprod(delta_ante, delta_crnt);
245     c_cros_acrs = iprod(delta_ante, delta_post);
246     c_cros_post = iprod(delta_crnt, delta_post);
247     c_prod      = c_cros_ante * c_cros_post - c_self_crnt * c_cros_acrs;
248     d_ante      = c_self_ante * c_self_crnt - c_cros_ante * c_cros_ante;
249     d_post      = c_self_post * c_self_crnt - c_cros_post * c_cros_post;
250
251     /*  When three consecutive beads align, we obtain values close to zero.
252        Here we avoid small values to prevent round-off errors. */
253     if (d_ante < GMX_REAL_EPS)
254     {
255         d_ante = GMX_REAL_EPS;
256     }
257     if (d_post < GMX_REAL_EPS)
258     {
259         d_post = GMX_REAL_EPS;
260     }
261
262     /* Computations of cosines */
263     norm_phi           = gmx_invsqrt(d_ante * d_post);
264     norm_theta_ante    = gmx_invsqrt(c_self_ante * c_self_crnt);
265     norm_theta_post    = gmx_invsqrt(c_self_crnt * c_self_post);
266     cosine_phi         = c_prod * norm_phi;
267     cosine_theta_ante  = c_cros_ante * norm_theta_ante;
268     cosine_theta_post  = c_cros_post * norm_theta_post;
269     sine_theta_ante_sq = 1 - cosine_theta_ante * cosine_theta_ante;
270     sine_theta_post_sq = 1 - cosine_theta_post * cosine_theta_post;
271
272     /*  It is possible that cosine_theta is slightly bigger than 1.0 due to round-off errors. */
273     if (sine_theta_ante_sq < 0.0)
274     {
275         sine_theta_ante_sq = 0.0;
276     }
277     if (sine_theta_post_sq < 0.0)
278     {
279         sine_theta_post_sq = 0.0;
280     }
281
282     sine_theta_ante = sqrt(sine_theta_ante_sq);
283     sine_theta_post = sqrt(sine_theta_post_sq);
284
285     /* PART 2 - COMPUTES FORCE COMPONENTS DUE TO DERIVATIVES TO DIHEDRAL ANGLE PHI */
286
287     /*      Computation of ratios */
288     ratio_phi_ante = c_prod / d_ante;
289     ratio_phi_post = c_prod / d_post;
290
291     /*       Computation of the prefactor */
292     /*      Computing 2nd power */
293     r1 = cosine_phi;
294
295     prefactor_phi = -torsion_coef[0] * norm_phi * (torsion_coef[2] + torsion_coef[3] * 2.0 * cosine_phi + torsion_coef[4] * 3.0 * (r1 * r1) + 4*torsion_coef[5]*r1*r1*r1) *
296         sine_theta_ante_sq * sine_theta_ante * sine_theta_post_sq * sine_theta_post;
297
298     /* Computation of factors (important for gaining speed). Factors factor_phi_*  are coming from the
299      * derivatives of the torsion angle (phi) with respect to the beads ai, aj, al, ak,
300      * (four) coordinates and they are multiplied in the force computations with the
301      * differences of the particles positions stored in parameters delta_ante,
302      * delta_crnt, delta_post. For formulas see file "restcbt.h" */
303
304     factor_phi_ai_ante = ratio_phi_ante * c_self_crnt;
305     factor_phi_ai_crnt = -c_cros_post - ratio_phi_ante * c_cros_ante;
306     factor_phi_ai_post = c_self_crnt;
307     factor_phi_aj_ante = -c_cros_post - ratio_phi_ante * (c_self_crnt + c_cros_ante);
308     factor_phi_aj_crnt = c_cros_post + c_cros_acrs * 2.0 + ratio_phi_ante * (c_self_ante + c_cros_ante) +  ratio_phi_post * c_self_post;
309     factor_phi_aj_post = -(c_cros_ante + c_self_crnt) - ratio_phi_post * c_cros_post;
310     factor_phi_ak_ante = c_cros_post + c_self_crnt + ratio_phi_ante * c_cros_ante;
311     factor_phi_ak_crnt = -(c_cros_ante + c_cros_acrs * 2.0) - ratio_phi_ante * c_self_ante - ratio_phi_post * (c_self_post + c_cros_post);
312     factor_phi_ak_post = c_cros_ante + ratio_phi_post * (c_self_crnt + c_cros_post);
313     factor_phi_al_ante = -c_self_crnt;
314     factor_phi_al_crnt = c_cros_ante + ratio_phi_post * c_cros_post;
315     factor_phi_al_post = -ratio_phi_post * c_self_crnt;
316
317     /* Computation of forces due to the derivatives of dihedral angle phi*/
318     for (d = 0; d < DIM; d++)
319     {
320         f_phi_ai[d] = prefactor_phi * (factor_phi_ai_ante * delta_ante[d] + factor_phi_ai_crnt * delta_crnt[d] + factor_phi_ai_post * delta_post[d]);
321         f_phi_aj[d] = prefactor_phi * (factor_phi_aj_ante * delta_ante[d] + factor_phi_aj_crnt * delta_crnt[d] + factor_phi_aj_post * delta_post[d]);
322         f_phi_ak[d] = prefactor_phi * (factor_phi_ak_ante * delta_ante[d] + factor_phi_ak_crnt * delta_crnt[d] + factor_phi_ak_post * delta_post[d]);
323         f_phi_al[d] = prefactor_phi * (factor_phi_al_ante * delta_ante[d] + factor_phi_al_crnt * delta_crnt[d] + factor_phi_al_post * delta_post[d]);
324     }
325
326     /* PART 3 - COMPUTES THE FORCE COMPONENTS DUE TO THE DERIVATIVES OF BENDING ANGLE THETHA_ANTHE */
327     /*      Computation of ratios */
328     ratio_theta_ante_ante = c_cros_ante / c_self_ante;
329     ratio_theta_ante_crnt = c_cros_ante / c_self_crnt;
330
331     /*      Computation of the prefactor */
332     /*      Computing 2nd power */
333     r1 = cosine_phi;
334     /*      Computing 3rd power */
335     r2 = cosine_phi;
336
337     prefactor_theta_ante = -torsion_coef[0] * norm_theta_ante * ( torsion_coef[1] + torsion_coef[2] * cosine_phi + torsion_coef[3] * (r1 * r1) +
338                                                                   torsion_coef[4] * (r2 * (r2 * r2))+ torsion_coef[5] * (r2 * (r2 * (r2 * r2)))) * (-3.0) * cosine_theta_ante * sine_theta_ante * sine_theta_post_sq * sine_theta_post;
339
340
341     /*      Computation of forces due to the derivatives of bending angle theta_ante */
342     for (d = 0; d < DIM; d++)
343     {
344         f_theta_ante_ai[d] = prefactor_theta_ante * (ratio_theta_ante_ante * delta_ante[d] - delta_crnt[d]);
345         f_theta_ante_aj[d] = prefactor_theta_ante * ((ratio_theta_ante_crnt + 1.0) * delta_crnt[d] - (ratio_theta_ante_ante + 1.0) * delta_ante[d]);
346         f_theta_ante_ak[d] = prefactor_theta_ante * (delta_ante[d] - ratio_theta_ante_crnt * delta_crnt[d]);
347     }
348
349     /* PART 4 - COMPUTES THE FORCE COMPONENTS DUE TO THE DERIVATIVES OF THE BENDING ANGLE THETA_POST */
350
351     /*      Computation of ratios */
352     ratio_theta_post_crnt = c_cros_post / c_self_crnt;
353     ratio_theta_post_post = c_cros_post / c_self_post;
354
355     /*     Computation of the prefactor */
356     /*      Computing 2nd power */
357     r1 = cosine_phi;
358     /*      Computing 3rd power */
359     r2 = cosine_phi;
360
361     prefactor_theta_post = -torsion_coef[0] * norm_theta_post * (torsion_coef[1] + torsion_coef[2] * cosine_phi + torsion_coef[3] * (r1 * r1) +
362                                                                  torsion_coef[4] * (r2 * (r2 * r2)) + torsion_coef[5] * (r2 * (r2 * (r2 * r2)))) * sine_theta_ante_sq * sine_theta_ante * (-3.0) * cosine_theta_post * sine_theta_post;
363
364
365     /*      Computation of forces due to the derivatives of bending angle Theta_Post */
366     for (d = 0; d < DIM; d++)
367     {
368         f_theta_post_aj[d] = prefactor_theta_post * (ratio_theta_post_crnt * delta_crnt[d] - delta_post[d]);
369         f_theta_post_ak[d] = prefactor_theta_post * ((ratio_theta_post_post + 1.0) * delta_post[d] - (ratio_theta_post_crnt + 1.0) * delta_crnt[d]);
370         f_theta_post_al[d] = prefactor_theta_post * (delta_crnt[d] - ratio_theta_post_post * delta_post[d]);
371     }
372     r1 = cosine_phi;
373     r2 = cosine_phi;
374
375     /* Contribution to energy - for formula see file "restcbt.h" */
376     *v = torsion_coef[0] * (torsion_coef[1] + torsion_coef[2] * cosine_phi + torsion_coef[3] * (r1 * r1) +
377                             torsion_coef[4] * (r2 * (r2 * r2)) + torsion_coef[5] * (r2 * (r2 * (r2 * r2)))) * sine_theta_ante_sq *
378         sine_theta_ante * sine_theta_post_sq * sine_theta_post;
379
380
381 }