Precision fix for rescbt code.
[alexxy/gromacs.git] / src / gromacs / listed_forces / restcbt.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2017,2019, 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 /*! \internal \file
36  *
37  * \brief
38  * This file contains function definitions necessary
39  * for computations of forces due to restricted angle, restricted dihedral and
40  * combined bending-torsion potentials.
41  *
42  * \author Nicolae Goga
43  *
44  * \ingroup module_listed_forces
45  */
46 #include "gmxpre.h"
47
48 #include "restcbt.h"
49
50 #include <cmath>
51
52 #include "gromacs/math/functions.h"
53 #include "gromacs/math/units.h"
54 #include "gromacs/math/utilities.h"
55 #include "gromacs/math/vec.h"
56 #include "gromacs/topology/idef.h"
57
58 /* This function computes factors needed for restricted angle potential.
59  * For explanations on formula used see file "restcbt.h" */
60
61 void compute_factors_restangles(int type, const t_iparams forceparams[],
62                                 rvec delta_ante,  rvec delta_post,
63                                 double *prefactor, double *ratio_ante, double *ratio_post, real *v)
64 {
65     // These variables are double to make the code
66     // reproducible.
67     double theta_equil, k_bending;
68     double cosine_theta_equil;
69     double c_ante, c_cros, c_post;
70     double norm;
71     double delta_cosine, cosine_theta;
72     double sine_theta_sq;
73     double term_theta_theta_equil;
74
75     k_bending          = forceparams[type].harmonic.krA;
76     theta_equil        =  forceparams[type].harmonic.rA*DEG2RAD;
77     theta_equil        = M_PI - theta_equil;
78     cosine_theta_equil = cos(theta_equil);
79
80     c_ante = iprod(delta_ante, delta_ante);
81     c_cros = iprod(delta_ante, delta_post);
82     c_post = iprod(delta_post, delta_post);
83
84     norm          = gmx::invsqrt(c_ante * c_post);
85     cosine_theta  = c_cros * norm;
86     sine_theta_sq = 1 - cosine_theta * cosine_theta;
87
88     *ratio_ante = c_cros / c_ante;
89     *ratio_post = c_cros / c_post;
90
91     delta_cosine           = cosine_theta - cosine_theta_equil;
92     term_theta_theta_equil = 1 - cosine_theta * cosine_theta_equil;
93     *prefactor             = -(k_bending) * delta_cosine * norm * term_theta_theta_equil / (sine_theta_sq * sine_theta_sq);
94
95     *v = k_bending * 0.5 * delta_cosine * delta_cosine / sine_theta_sq;
96
97 }
98
99
100 /* Compute factors for restricted dihedral potential
101  * For explanations on formula used see file "restcbt.h" */
102 void compute_factors_restrdihs(int type,  const t_iparams forceparams[],
103                                rvec delta_ante, rvec delta_crnt, rvec delta_post,
104                                real *factor_phi_ai_ante, real *factor_phi_ai_crnt, real *factor_phi_ai_post,
105                                real *factor_phi_aj_ante, real *factor_phi_aj_crnt, real *factor_phi_aj_post,
106                                real *factor_phi_ak_ante, real *factor_phi_ak_crnt, real *factor_phi_ak_post,
107                                real *factor_phi_al_ante, real *factor_phi_al_crnt, real *factor_phi_al_post,
108                                real *prefactor_phi, real *v)
109 {
110
111     real phi0, cosine_phi0;
112     real k_torsion;
113     real c_self_ante, c_self_crnt, c_self_post;
114     real c_cros_ante, c_cros_acrs, c_cros_post;
115     real c_prod, d_post, d_ante;
116     real sine_phi_sq, cosine_phi;
117     real delta_cosine, term_phi_phi0;
118     real ratio_phi_ante, ratio_phi_post;
119     real norm_phi;
120
121     /* Read parameters phi0 and k_torsion */
122     phi0        = forceparams[type].pdihs.phiA * DEG2RAD;
123     cosine_phi0 = cos(phi0);
124     k_torsion   = forceparams[type].pdihs.cpA;
125
126     /* Computation of the cosine of the dihedral angle. The scalar ("dot") product  method
127      * is used. c_*_* cummulate the scalar products of the differences of particles
128      * positions while c_prod, d_ante and d_post are differences of products of scalar
129      * terms that are parts of the derivatives of forces */
130     c_self_ante = iprod(delta_ante, delta_ante);
131     c_self_crnt = iprod(delta_crnt, delta_crnt);
132     c_self_post = iprod(delta_post, delta_post);
133     c_cros_ante = iprod(delta_ante, delta_crnt);
134     c_cros_acrs = iprod(delta_ante, delta_post);
135     c_cros_post = iprod(delta_crnt, delta_post);
136     c_prod      = c_cros_ante * c_cros_post - c_self_crnt * c_cros_acrs;
137     d_ante      = c_self_ante * c_self_crnt - c_cros_ante * c_cros_ante;
138     d_post      = c_self_post * c_self_crnt - c_cros_post * c_cros_post;
139
140     /*  When three consecutive beads align, we obtain values close to zero.
141      *  Here we avoid small values to prevent round-off errors. */
142     if (d_ante < GMX_REAL_EPS)
143     {
144         d_ante = GMX_REAL_EPS;
145     }
146     if (d_post < GMX_REAL_EPS)
147     {
148         d_post = GMX_REAL_EPS;
149     }
150
151     /* Computes the square of the sinus of phi  in sine_phi_sq */
152     norm_phi    = gmx::invsqrt(d_ante * d_post);
153     cosine_phi  = c_prod * norm_phi;
154     sine_phi_sq = 1.0 - cosine_phi * cosine_phi;
155
156     /*  It is possible that cosine_phi is slightly bigger than 1.0 due to round-off errors. */
157     if (sine_phi_sq < 0.0)
158     {
159         sine_phi_sq = 0.0;
160     }
161
162     /* Computation of the differences of cosines (delta_cosine) and a term (term_phi_phi0)
163      * that is part of the common prefactor_phi */
164
165     delta_cosine  = cosine_phi - cosine_phi0;
166     term_phi_phi0 = 1 - cosine_phi * cosine_phi0;
167
168
169     /*      Computation of ratios */
170     ratio_phi_ante = c_prod / d_ante;
171     ratio_phi_post = c_prod / d_post;
172
173     /*      Computation of the prefactor - common term for all forces */
174     *prefactor_phi = -(k_torsion) * delta_cosine * norm_phi * term_phi_phi0 / (sine_phi_sq * sine_phi_sq);
175
176     /* Computation of force factors.  Factors factor_phi_*  are coming from the
177      * derivatives of the torsion angle (phi) with respect to the beads ai, aj, al, ak,
178      * (four) coordinates and they are multiplied in the force computations with the
179      * differences of the particles positions stored in parameters delta_ante,
180      * delta_crnt, delta_post. For formulas see file "restcbt.h" */
181
182     *factor_phi_ai_ante = ratio_phi_ante * c_self_crnt;
183     *factor_phi_ai_crnt = -c_cros_post - ratio_phi_ante * c_cros_ante;
184     *factor_phi_ai_post = c_self_crnt;
185     *factor_phi_aj_ante = -c_cros_post - ratio_phi_ante * (c_self_crnt + c_cros_ante);
186     *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;
187     *factor_phi_aj_post = -(c_cros_ante + c_self_crnt) - ratio_phi_post * c_cros_post;
188     *factor_phi_ak_ante = c_cros_post + c_self_crnt + ratio_phi_ante * c_cros_ante;
189     *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);
190     *factor_phi_ak_post = c_cros_ante + ratio_phi_post * (c_self_crnt + c_cros_post);
191     *factor_phi_al_ante = -c_self_crnt;
192     *factor_phi_al_crnt = c_cros_ante + ratio_phi_post * c_cros_post;
193     *factor_phi_al_post = -ratio_phi_post * c_self_crnt;
194
195     /* Contribution to energy  - see formula in file "restcbt.h"*/
196     *v = k_torsion * 0.5 * delta_cosine * delta_cosine / sine_phi_sq;
197 }
198
199
200
201 /* Compute factors for CBT potential
202  * For explanations on formula used see file "restcbt.h" */
203
204 void compute_factors_cbtdihs(int type,  const t_iparams forceparams[],
205                              rvec delta_ante, rvec delta_crnt, rvec delta_post,
206                              rvec f_phi_ai, rvec f_phi_aj, rvec f_phi_ak, rvec f_phi_al,
207                              rvec f_theta_ante_ai, rvec f_theta_ante_aj, rvec f_theta_ante_ak,
208                              rvec f_theta_post_aj, rvec f_theta_post_ak, rvec f_theta_post_al,
209                              real * v)
210 {
211     int  j, d;
212     real torsion_coef[NR_CBTDIHS];
213     real c_self_ante, c_self_crnt, c_self_post;
214     real c_cros_ante, c_cros_acrs, c_cros_post;
215     real c_prod, d_ante,  d_post;
216     real norm_phi, norm_theta_ante, norm_theta_post;
217     real cosine_phi, cosine_theta_ante, cosine_theta_post;
218     real sine_theta_ante_sq, sine_theta_post_sq;
219     real sine_theta_ante, sine_theta_post;
220     real prefactor_phi;
221     real ratio_phi_ante, ratio_phi_post;
222     real r1, r2;
223     real factor_phi_ai_ante, factor_phi_ai_crnt, factor_phi_ai_post;
224     real factor_phi_aj_ante, factor_phi_aj_crnt, factor_phi_aj_post;
225     real factor_phi_ak_ante, factor_phi_ak_crnt, factor_phi_ak_post;
226     real factor_phi_al_ante, factor_phi_al_crnt, factor_phi_al_post;
227     real prefactor_theta_ante, ratio_theta_ante_ante, ratio_theta_ante_crnt;
228     real prefactor_theta_post, ratio_theta_post_crnt, ratio_theta_post_post;
229
230     /* The formula for combined bending-torsion potential (see file "restcbt.h") contains
231      * in its expression not only the dihedral angle \f[\phi\f] but also \f[\theta_{i-1}\f]
232      * (theta_ante bellow) and \f[\theta_{i}\f] (theta_post bellow)--- the adjacent bending
233      * angles. The forces for the particles ai, aj, ak, al have components coming from the
234      * derivatives of the potential with respect to all three angles.
235      * This function is organised in 4 parts
236      * PART 1 - Computes force factors common to all the derivatives for the four particles
237      * PART 2 - Computes the force components due to the derivatives of dihedral angle Phi
238      * PART 3 - Computes the force components due to the derivatives of bending angle Theta_Ante
239      * PART 4 - Computes the force components due to the derivatives of bending angle Theta_Post
240      * Bellow we will respct thuis structure */
241
242
243     /* PART 1 - COMPUTES FORCE FACTORS COMMON TO ALL DERIVATIVES FOR THE FOUR PARTICLES */
244
245
246     for (j = 0; (j < NR_CBTDIHS); j++)
247     {
248         torsion_coef[j]  = forceparams[type].cbtdihs.cbtcA[j];
249     }
250
251     /* Computation of the cosine of the dihedral angle. The scalar ("dot") product  method
252      * is used. c_*_* cummulate the scalar products of the differences of particles
253      * positions while c_prod, d_ante and d_post are differences of products of scalar
254      * terms that are parts of the derivatives of forces */
255
256     c_self_ante = iprod(delta_ante, delta_ante);
257     c_self_crnt = iprod(delta_crnt, delta_crnt);
258     c_self_post = iprod(delta_post, delta_post);
259     c_cros_ante = iprod(delta_ante, delta_crnt);
260     c_cros_acrs = iprod(delta_ante, delta_post);
261     c_cros_post = iprod(delta_crnt, delta_post);
262     c_prod      = c_cros_ante * c_cros_post - c_self_crnt * c_cros_acrs;
263     d_ante      = c_self_ante * c_self_crnt - c_cros_ante * c_cros_ante;
264     d_post      = c_self_post * c_self_crnt - c_cros_post * c_cros_post;
265
266     /*  When three consecutive beads align, we obtain values close to zero.
267        Here we avoid small values to prevent round-off errors. */
268     if (d_ante < GMX_REAL_EPS)
269     {
270         d_ante = GMX_REAL_EPS;
271     }
272     if (d_post < GMX_REAL_EPS)
273     {
274         d_post = GMX_REAL_EPS;
275     }
276
277     /* Computations of cosines */
278     norm_phi           = gmx::invsqrt(d_ante * d_post);
279     norm_theta_ante    = gmx::invsqrt(c_self_ante * c_self_crnt);
280     norm_theta_post    = gmx::invsqrt(c_self_crnt * c_self_post);
281     cosine_phi         = c_prod * norm_phi;
282     cosine_theta_ante  = c_cros_ante * norm_theta_ante;
283     cosine_theta_post  = c_cros_post * norm_theta_post;
284     sine_theta_ante_sq = 1 - cosine_theta_ante * cosine_theta_ante;
285     sine_theta_post_sq = 1 - cosine_theta_post * cosine_theta_post;
286
287     /*  It is possible that cosine_theta is slightly bigger than 1.0 due to round-off errors. */
288     if (sine_theta_ante_sq < 0.0)
289     {
290         sine_theta_ante_sq = 0.0;
291     }
292     if (sine_theta_post_sq < 0.0)
293     {
294         sine_theta_post_sq = 0.0;
295     }
296
297     sine_theta_ante = std::sqrt(sine_theta_ante_sq);
298     sine_theta_post = std::sqrt(sine_theta_post_sq);
299
300     /* PART 2 - COMPUTES FORCE COMPONENTS DUE TO DERIVATIVES TO DIHEDRAL ANGLE PHI */
301
302     /*      Computation of ratios */
303     ratio_phi_ante = c_prod / d_ante;
304     ratio_phi_post = c_prod / d_post;
305
306     /*       Computation of the prefactor */
307     /*      Computing 2nd power */
308     r1 = cosine_phi;
309
310     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) *
311         sine_theta_ante_sq * sine_theta_ante * sine_theta_post_sq * sine_theta_post;
312
313     /* Computation of factors (important for gaining speed). Factors factor_phi_*  are coming from the
314      * derivatives of the torsion angle (phi) with respect to the beads ai, aj, al, ak,
315      * (four) coordinates and they are multiplied in the force computations with the
316      * differences of the particles positions stored in parameters delta_ante,
317      * delta_crnt, delta_post. For formulas see file "restcbt.h" */
318
319     factor_phi_ai_ante = ratio_phi_ante * c_self_crnt;
320     factor_phi_ai_crnt = -c_cros_post - ratio_phi_ante * c_cros_ante;
321     factor_phi_ai_post = c_self_crnt;
322     factor_phi_aj_ante = -c_cros_post - ratio_phi_ante * (c_self_crnt + c_cros_ante);
323     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;
324     factor_phi_aj_post = -(c_cros_ante + c_self_crnt) - ratio_phi_post * c_cros_post;
325     factor_phi_ak_ante = c_cros_post + c_self_crnt + ratio_phi_ante * c_cros_ante;
326     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);
327     factor_phi_ak_post = c_cros_ante + ratio_phi_post * (c_self_crnt + c_cros_post);
328     factor_phi_al_ante = -c_self_crnt;
329     factor_phi_al_crnt = c_cros_ante + ratio_phi_post * c_cros_post;
330     factor_phi_al_post = -ratio_phi_post * c_self_crnt;
331
332     /* Computation of forces due to the derivatives of dihedral angle phi*/
333     for (d = 0; d < DIM; d++)
334     {
335         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]);
336         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]);
337         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]);
338         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]);
339     }
340
341     /* PART 3 - COMPUTES THE FORCE COMPONENTS DUE TO THE DERIVATIVES OF BENDING ANGLE THETHA_ANTHE */
342     /*      Computation of ratios */
343     ratio_theta_ante_ante = c_cros_ante / c_self_ante;
344     ratio_theta_ante_crnt = c_cros_ante / c_self_crnt;
345
346     /*      Computation of the prefactor */
347     /*      Computing 2nd power */
348     r1 = cosine_phi;
349     /*      Computing 3rd power */
350     r2 = cosine_phi;
351
352     prefactor_theta_ante = -torsion_coef[0] * norm_theta_ante * ( torsion_coef[1] + torsion_coef[2] * cosine_phi + torsion_coef[3] * (r1 * r1) +
353                                                                   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;
354
355
356     /*      Computation of forces due to the derivatives of bending angle theta_ante */
357     for (d = 0; d < DIM; d++)
358     {
359         f_theta_ante_ai[d] = prefactor_theta_ante * (ratio_theta_ante_ante * delta_ante[d] - delta_crnt[d]);
360         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]);
361         f_theta_ante_ak[d] = prefactor_theta_ante * (delta_ante[d] - ratio_theta_ante_crnt * delta_crnt[d]);
362     }
363
364     /* PART 4 - COMPUTES THE FORCE COMPONENTS DUE TO THE DERIVATIVES OF THE BENDING ANGLE THETA_POST */
365
366     /*      Computation of ratios */
367     ratio_theta_post_crnt = c_cros_post / c_self_crnt;
368     ratio_theta_post_post = c_cros_post / c_self_post;
369
370     /*     Computation of the prefactor */
371     /*      Computing 2nd power */
372     r1 = cosine_phi;
373     /*      Computing 3rd power */
374     r2 = cosine_phi;
375
376     prefactor_theta_post = -torsion_coef[0] * norm_theta_post * (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 * sine_theta_ante * (-3.0) * cosine_theta_post * sine_theta_post;
378
379
380     /*      Computation of forces due to the derivatives of bending angle Theta_Post */
381     for (d = 0; d < DIM; d++)
382     {
383         f_theta_post_aj[d] = prefactor_theta_post * (ratio_theta_post_crnt * delta_crnt[d] - delta_post[d]);
384         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]);
385         f_theta_post_al[d] = prefactor_theta_post * (delta_crnt[d] - ratio_theta_post_post * delta_post[d]);
386     }
387     r1 = cosine_phi;
388     r2 = cosine_phi;
389
390     /* Contribution to energy - for formula see file "restcbt.h" */
391     *v = torsion_coef[0] * (torsion_coef[1] + torsion_coef[2] * cosine_phi + torsion_coef[3] * (r1 * r1) +
392                             torsion_coef[4] * (r2 * (r2 * r2)) + torsion_coef[5] * (r2 * (r2 * (r2 * r2)))) * sine_theta_ante_sq *
393         sine_theta_ante * sine_theta_post_sq * sine_theta_post;
394
395
396 }