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