2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
45 #include "gromacs/math/utilities.h"
48 #include "gromacs/utility/smalloc.h"
53 #include "gmx_fatal.h"
59 #include "nonbonded.h"
62 #include "gromacs/simd/simd.h"
63 #include "gromacs/simd/simd_math.h"
64 #include "gromacs/simd/vector_operations.h"
66 /* Find a better place for this? */
67 const int cmap_coeff_matrix[] = {
68 1, 0, -3, 2, 0, 0, 0, 0, -3, 0, 9, -6, 2, 0, -6, 4,
69 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, -9, 6, -2, 0, 6, -4,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, -6, 0, 0, -6, 4,
71 0, 0, 3, -2, 0, 0, 0, 0, 0, 0, -9, 6, 0, 0, 6, -4,
72 0, 0, 0, 0, 1, 0, -3, 2, -2, 0, 6, -4, 1, 0, -3, 2,
73 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 3, -2, 1, 0, -3, 2,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 2, 0, 0, 3, -2,
75 0, 0, 0, 0, 0, 0, 3, -2, 0, 0, -6, 4, 0, 0, 3, -2,
76 0, 1, -2, 1, 0, 0, 0, 0, 0, -3, 6, -3, 0, 2, -4, 2,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, -6, 3, 0, -2, 4, -2,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 3, 0, 0, 2, -2,
79 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 3, -3, 0, 0, -2, 2,
80 0, 0, 0, 0, 0, 1, -2, 1, 0, -2, 4, -2, 0, 1, -2, 1,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 2, -1, 0, 1, -2, 1,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, -1, 1,
83 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 2, -2, 0, 0, -1, 1
88 int glatnr(int *global_atom_index, int i)
92 if (global_atom_index == NULL)
98 atnr = global_atom_index[i] + 1;
104 static int pbc_rvec_sub(const t_pbc *pbc, const rvec xi, const rvec xj, rvec dx)
108 return pbc_dx_aiuc(pbc, xi, xj, dx);
112 rvec_sub(xi, xj, dx);
117 #ifdef GMX_SIMD_HAVE_REAL
119 /* SIMD PBC data structure, containing 1/boxdiag and the box vectors */
121 gmx_simd_real_t inv_bzz;
122 gmx_simd_real_t inv_byy;
123 gmx_simd_real_t inv_bxx;
132 /* Set the SIMD pbc data from a normal t_pbc struct */
133 static void set_pbc_simd(const t_pbc *pbc, pbc_simd_t *pbc_simd)
138 /* Setting inv_bdiag to 0 effectively turns off PBC */
139 clear_rvec(inv_bdiag);
142 for (d = 0; d < pbc->ndim_ePBC; d++)
144 inv_bdiag[d] = 1.0/pbc->box[d][d];
148 pbc_simd->inv_bzz = gmx_simd_set1_r(inv_bdiag[ZZ]);
149 pbc_simd->inv_byy = gmx_simd_set1_r(inv_bdiag[YY]);
150 pbc_simd->inv_bxx = gmx_simd_set1_r(inv_bdiag[XX]);
154 pbc_simd->bzx = gmx_simd_set1_r(pbc->box[ZZ][XX]);
155 pbc_simd->bzy = gmx_simd_set1_r(pbc->box[ZZ][YY]);
156 pbc_simd->bzz = gmx_simd_set1_r(pbc->box[ZZ][ZZ]);
157 pbc_simd->byx = gmx_simd_set1_r(pbc->box[YY][XX]);
158 pbc_simd->byy = gmx_simd_set1_r(pbc->box[YY][YY]);
159 pbc_simd->bxx = gmx_simd_set1_r(pbc->box[XX][XX]);
163 pbc_simd->bzx = gmx_simd_setzero_r();
164 pbc_simd->bzy = gmx_simd_setzero_r();
165 pbc_simd->bzz = gmx_simd_setzero_r();
166 pbc_simd->byx = gmx_simd_setzero_r();
167 pbc_simd->byy = gmx_simd_setzero_r();
168 pbc_simd->bxx = gmx_simd_setzero_r();
172 /* Correct distance vector *dx,*dy,*dz for PBC using SIMD */
173 static gmx_inline void
174 pbc_dx_simd(gmx_simd_real_t *dx, gmx_simd_real_t *dy, gmx_simd_real_t *dz,
175 const pbc_simd_t *pbc)
179 sh = gmx_simd_round_r(gmx_simd_mul_r(*dz, pbc->inv_bzz));
180 *dx = gmx_simd_fnmadd_r(sh, pbc->bzx, *dx);
181 *dy = gmx_simd_fnmadd_r(sh, pbc->bzy, *dy);
182 *dz = gmx_simd_fnmadd_r(sh, pbc->bzz, *dz);
184 sh = gmx_simd_round_r(gmx_simd_mul_r(*dy, pbc->inv_byy));
185 *dx = gmx_simd_fnmadd_r(sh, pbc->byx, *dx);
186 *dy = gmx_simd_fnmadd_r(sh, pbc->byy, *dy);
188 sh = gmx_simd_round_r(gmx_simd_mul_r(*dx, pbc->inv_bxx));
189 *dx = gmx_simd_fnmadd_r(sh, pbc->bxx, *dx);
192 #endif /* GMX_SIMD_HAVE_REAL */
195 * Morse potential bond by Frank Everdij
197 * Three parameters needed:
199 * b0 = equilibrium distance in nm
200 * be = beta in nm^-1 (actually, it's nu_e*Sqrt(2*pi*pi*mu/D_e))
201 * cb = well depth in kJ/mol
203 * Note: the potential is referenced to be +cb at infinite separation
204 * and zero at the equilibrium distance!
207 real morse_bonds(int nbonds,
208 const t_iatom forceatoms[], const t_iparams forceparams[],
209 const rvec x[], rvec f[], rvec fshift[],
210 const t_pbc *pbc, const t_graph *g,
211 real lambda, real *dvdlambda,
212 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
213 int gmx_unused *global_atom_index)
215 const real one = 1.0;
216 const real two = 2.0;
217 real dr, dr2, temp, omtemp, cbomtemp, fbond, vbond, fij, vtot;
218 real b0, be, cb, b0A, beA, cbA, b0B, beB, cbB, L1;
220 int i, m, ki, type, ai, aj;
224 for (i = 0; (i < nbonds); )
226 type = forceatoms[i++];
227 ai = forceatoms[i++];
228 aj = forceatoms[i++];
230 b0A = forceparams[type].morse.b0A;
231 beA = forceparams[type].morse.betaA;
232 cbA = forceparams[type].morse.cbA;
234 b0B = forceparams[type].morse.b0B;
235 beB = forceparams[type].morse.betaB;
236 cbB = forceparams[type].morse.cbB;
238 L1 = one-lambda; /* 1 */
239 b0 = L1*b0A + lambda*b0B; /* 3 */
240 be = L1*beA + lambda*beB; /* 3 */
241 cb = L1*cbA + lambda*cbB; /* 3 */
243 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
244 dr2 = iprod(dx, dx); /* 5 */
245 dr = dr2*gmx_invsqrt(dr2); /* 10 */
246 temp = exp(-be*(dr-b0)); /* 12 */
250 /* bonds are constrainted. This may _not_ include bond constraints if they are lambda dependent */
251 *dvdlambda += cbB-cbA;
255 omtemp = one-temp; /* 1 */
256 cbomtemp = cb*omtemp; /* 1 */
257 vbond = cbomtemp*omtemp; /* 1 */
258 fbond = -two*be*temp*cbomtemp*gmx_invsqrt(dr2); /* 9 */
259 vtot += vbond; /* 1 */
261 *dvdlambda += (cbB - cbA) * omtemp * omtemp - (2-2*omtemp)*omtemp * cb * ((b0B-b0A)*be - (beB-beA)*(dr-b0)); /* 15 */
265 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
269 for (m = 0; (m < DIM); m++) /* 15 */
274 fshift[ki][m] += fij;
275 fshift[CENTRAL][m] -= fij;
281 real cubic_bonds(int nbonds,
282 const t_iatom forceatoms[], const t_iparams forceparams[],
283 const rvec x[], rvec f[], rvec fshift[],
284 const t_pbc *pbc, const t_graph *g,
285 real gmx_unused lambda, real gmx_unused *dvdlambda,
286 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
287 int gmx_unused *global_atom_index)
289 const real three = 3.0;
290 const real two = 2.0;
292 real dr, dr2, dist, kdist, kdist2, fbond, vbond, fij, vtot;
294 int i, m, ki, type, ai, aj;
298 for (i = 0; (i < nbonds); )
300 type = forceatoms[i++];
301 ai = forceatoms[i++];
302 aj = forceatoms[i++];
304 b0 = forceparams[type].cubic.b0;
305 kb = forceparams[type].cubic.kb;
306 kcub = forceparams[type].cubic.kcub;
308 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
309 dr2 = iprod(dx, dx); /* 5 */
316 dr = dr2*gmx_invsqrt(dr2); /* 10 */
321 vbond = kdist2 + kcub*kdist2*dist;
322 fbond = -(two*kdist + three*kdist2*kcub)/dr;
324 vtot += vbond; /* 21 */
328 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
331 for (m = 0; (m < DIM); m++) /* 15 */
336 fshift[ki][m] += fij;
337 fshift[CENTRAL][m] -= fij;
343 real FENE_bonds(int nbonds,
344 const t_iatom forceatoms[], const t_iparams forceparams[],
345 const rvec x[], rvec f[], rvec fshift[],
346 const t_pbc *pbc, const t_graph *g,
347 real gmx_unused lambda, real gmx_unused *dvdlambda,
348 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
349 int *global_atom_index)
351 const real half = 0.5;
352 const real one = 1.0;
354 real dr, dr2, bm2, omdr2obm2, fbond, vbond, fij, vtot;
356 int i, m, ki, type, ai, aj;
360 for (i = 0; (i < nbonds); )
362 type = forceatoms[i++];
363 ai = forceatoms[i++];
364 aj = forceatoms[i++];
366 bm = forceparams[type].fene.bm;
367 kb = forceparams[type].fene.kb;
369 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
370 dr2 = iprod(dx, dx); /* 5 */
382 "r^2 (%f) >= bm^2 (%f) in FENE bond between atoms %d and %d",
384 glatnr(global_atom_index, ai),
385 glatnr(global_atom_index, aj));
388 omdr2obm2 = one - dr2/bm2;
390 vbond = -half*kb*bm2*log(omdr2obm2);
391 fbond = -kb/omdr2obm2;
393 vtot += vbond; /* 35 */
397 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
400 for (m = 0; (m < DIM); m++) /* 15 */
405 fshift[ki][m] += fij;
406 fshift[CENTRAL][m] -= fij;
412 real harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
415 const real half = 0.5;
416 real L1, kk, x0, dx, dx2;
417 real v, f, dvdlambda;
420 kk = L1*kA+lambda*kB;
421 x0 = L1*xA+lambda*xB;
428 dvdlambda = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
435 /* That was 19 flops */
439 real bonds(int nbonds,
440 const t_iatom forceatoms[], const t_iparams forceparams[],
441 const rvec x[], rvec f[], rvec fshift[],
442 const t_pbc *pbc, const t_graph *g,
443 real lambda, real *dvdlambda,
444 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
445 int gmx_unused *global_atom_index)
447 int i, m, ki, ai, aj, type;
448 real dr, dr2, fbond, vbond, fij, vtot;
453 for (i = 0; (i < nbonds); )
455 type = forceatoms[i++];
456 ai = forceatoms[i++];
457 aj = forceatoms[i++];
459 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
460 dr2 = iprod(dx, dx); /* 5 */
461 dr = dr2*gmx_invsqrt(dr2); /* 10 */
463 *dvdlambda += harmonic(forceparams[type].harmonic.krA,
464 forceparams[type].harmonic.krB,
465 forceparams[type].harmonic.rA,
466 forceparams[type].harmonic.rB,
467 dr, lambda, &vbond, &fbond); /* 19 */
475 vtot += vbond; /* 1*/
476 fbond *= gmx_invsqrt(dr2); /* 6 */
480 fprintf(debug, "BONDS: dr = %10g vbond = %10g fbond = %10g\n",
486 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
489 for (m = 0; (m < DIM); m++) /* 15 */
494 fshift[ki][m] += fij;
495 fshift[CENTRAL][m] -= fij;
501 real restraint_bonds(int nbonds,
502 const t_iatom forceatoms[], const t_iparams forceparams[],
503 const rvec x[], rvec f[], rvec fshift[],
504 const t_pbc *pbc, const t_graph *g,
505 real lambda, real *dvdlambda,
506 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
507 int gmx_unused *global_atom_index)
509 int i, m, ki, ai, aj, type;
510 real dr, dr2, fbond, vbond, fij, vtot;
512 real low, dlow, up1, dup1, up2, dup2, k, dk;
520 for (i = 0; (i < nbonds); )
522 type = forceatoms[i++];
523 ai = forceatoms[i++];
524 aj = forceatoms[i++];
526 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
527 dr2 = iprod(dx, dx); /* 5 */
528 dr = dr2*gmx_invsqrt(dr2); /* 10 */
530 low = L1*forceparams[type].restraint.lowA + lambda*forceparams[type].restraint.lowB;
531 dlow = -forceparams[type].restraint.lowA + forceparams[type].restraint.lowB;
532 up1 = L1*forceparams[type].restraint.up1A + lambda*forceparams[type].restraint.up1B;
533 dup1 = -forceparams[type].restraint.up1A + forceparams[type].restraint.up1B;
534 up2 = L1*forceparams[type].restraint.up2A + lambda*forceparams[type].restraint.up2B;
535 dup2 = -forceparams[type].restraint.up2A + forceparams[type].restraint.up2B;
536 k = L1*forceparams[type].restraint.kA + lambda*forceparams[type].restraint.kB;
537 dk = -forceparams[type].restraint.kA + forceparams[type].restraint.kB;
546 *dvdlambda += 0.5*dk*drh2 - k*dlow*drh;
559 *dvdlambda += 0.5*dk*drh2 - k*dup1*drh;
564 vbond = k*(up2 - up1)*(0.5*(up2 - up1) + drh);
565 fbond = -k*(up2 - up1);
566 *dvdlambda += dk*(up2 - up1)*(0.5*(up2 - up1) + drh)
567 + k*(dup2 - dup1)*(up2 - up1 + drh)
568 - k*(up2 - up1)*dup2;
576 vtot += vbond; /* 1*/
577 fbond *= gmx_invsqrt(dr2); /* 6 */
581 fprintf(debug, "BONDS: dr = %10g vbond = %10g fbond = %10g\n",
587 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
590 for (m = 0; (m < DIM); m++) /* 15 */
595 fshift[ki][m] += fij;
596 fshift[CENTRAL][m] -= fij;
603 real polarize(int nbonds,
604 const t_iatom forceatoms[], const t_iparams forceparams[],
605 const rvec x[], rvec f[], rvec fshift[],
606 const t_pbc *pbc, const t_graph *g,
607 real lambda, real *dvdlambda,
608 const t_mdatoms *md, t_fcdata gmx_unused *fcd,
609 int gmx_unused *global_atom_index)
611 int i, m, ki, ai, aj, type;
612 real dr, dr2, fbond, vbond, fij, vtot, ksh;
617 for (i = 0; (i < nbonds); )
619 type = forceatoms[i++];
620 ai = forceatoms[i++];
621 aj = forceatoms[i++];
622 ksh = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].polarize.alpha;
625 fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
628 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
629 dr2 = iprod(dx, dx); /* 5 */
630 dr = dr2*gmx_invsqrt(dr2); /* 10 */
632 *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /* 19 */
639 vtot += vbond; /* 1*/
640 fbond *= gmx_invsqrt(dr2); /* 6 */
644 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
647 for (m = 0; (m < DIM); m++) /* 15 */
652 fshift[ki][m] += fij;
653 fshift[CENTRAL][m] -= fij;
659 real anharm_polarize(int nbonds,
660 const t_iatom forceatoms[], const t_iparams forceparams[],
661 const rvec x[], rvec f[], rvec fshift[],
662 const t_pbc *pbc, const t_graph *g,
663 real lambda, real *dvdlambda,
664 const t_mdatoms *md, t_fcdata gmx_unused *fcd,
665 int gmx_unused *global_atom_index)
667 int i, m, ki, ai, aj, type;
668 real dr, dr2, fbond, vbond, fij, vtot, ksh, khyp, drcut, ddr, ddr3;
673 for (i = 0; (i < nbonds); )
675 type = forceatoms[i++];
676 ai = forceatoms[i++];
677 aj = forceatoms[i++];
678 ksh = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].anharm_polarize.alpha; /* 7*/
679 khyp = forceparams[type].anharm_polarize.khyp;
680 drcut = forceparams[type].anharm_polarize.drcut;
683 fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
686 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
687 dr2 = iprod(dx, dx); /* 5 */
688 dr = dr2*gmx_invsqrt(dr2); /* 10 */
690 *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /* 19 */
701 vbond += khyp*ddr*ddr3;
702 fbond -= 4*khyp*ddr3;
704 fbond *= gmx_invsqrt(dr2); /* 6 */
705 vtot += vbond; /* 1*/
709 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
712 for (m = 0; (m < DIM); m++) /* 15 */
717 fshift[ki][m] += fij;
718 fshift[CENTRAL][m] -= fij;
724 real water_pol(int nbonds,
725 const t_iatom forceatoms[], const t_iparams forceparams[],
726 const rvec x[], rvec f[], rvec gmx_unused fshift[],
727 const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
728 real gmx_unused lambda, real gmx_unused *dvdlambda,
729 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
730 int gmx_unused *global_atom_index)
732 /* This routine implements anisotropic polarizibility for water, through
733 * a shell connected to a dummy with spring constant that differ in the
734 * three spatial dimensions in the molecular frame.
736 int i, m, aO, aH1, aH2, aD, aS, type, type0, ki;
738 rvec dOH1, dOH2, dHH, dOD, dDS, nW, kk, dx, kdx, proj;
742 real vtot, fij, r_HH, r_OD, r_nW, tx, ty, tz, qS;
747 type0 = forceatoms[0];
749 qS = md->chargeA[aS];
750 kk[XX] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_x;
751 kk[YY] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_y;
752 kk[ZZ] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_z;
753 r_HH = 1.0/forceparams[type0].wpol.rHH;
754 r_OD = 1.0/forceparams[type0].wpol.rOD;
757 fprintf(debug, "WPOL: qS = %10.5f aS = %5d\n", qS, aS);
758 fprintf(debug, "WPOL: kk = %10.3f %10.3f %10.3f\n",
759 kk[XX], kk[YY], kk[ZZ]);
760 fprintf(debug, "WPOL: rOH = %10.3f rHH = %10.3f rOD = %10.3f\n",
761 forceparams[type0].wpol.rOH,
762 forceparams[type0].wpol.rHH,
763 forceparams[type0].wpol.rOD);
765 for (i = 0; (i < nbonds); i += 6)
767 type = forceatoms[i];
770 gmx_fatal(FARGS, "Sorry, type = %d, type0 = %d, file = %s, line = %d",
771 type, type0, __FILE__, __LINE__);
773 aO = forceatoms[i+1];
774 aH1 = forceatoms[i+2];
775 aH2 = forceatoms[i+3];
776 aD = forceatoms[i+4];
777 aS = forceatoms[i+5];
779 /* Compute vectors describing the water frame */
780 pbc_rvec_sub(pbc, x[aH1], x[aO], dOH1);
781 pbc_rvec_sub(pbc, x[aH2], x[aO], dOH2);
782 pbc_rvec_sub(pbc, x[aH2], x[aH1], dHH);
783 pbc_rvec_sub(pbc, x[aD], x[aO], dOD);
784 ki = pbc_rvec_sub(pbc, x[aS], x[aD], dDS);
785 cprod(dOH1, dOH2, nW);
787 /* Compute inverse length of normal vector
788 * (this one could be precomputed, but I'm too lazy now)
790 r_nW = gmx_invsqrt(iprod(nW, nW));
791 /* This is for precision, but does not make a big difference,
794 r_OD = gmx_invsqrt(iprod(dOD, dOD));
796 /* Normalize the vectors in the water frame */
798 svmul(r_HH, dHH, dHH);
799 svmul(r_OD, dOD, dOD);
801 /* Compute displacement of shell along components of the vector */
802 dx[ZZ] = iprod(dDS, dOD);
803 /* Compute projection on the XY plane: dDS - dx[ZZ]*dOD */
804 for (m = 0; (m < DIM); m++)
806 proj[m] = dDS[m]-dx[ZZ]*dOD[m];
809 /*dx[XX] = iprod(dDS,nW);
810 dx[YY] = iprod(dDS,dHH);*/
811 dx[XX] = iprod(proj, nW);
812 for (m = 0; (m < DIM); m++)
814 proj[m] -= dx[XX]*nW[m];
816 dx[YY] = iprod(proj, dHH);
821 fprintf(debug, "WPOL: dx2=%10g dy2=%10g dz2=%10g sum=%10g dDS^2=%10g\n",
822 sqr(dx[XX]), sqr(dx[YY]), sqr(dx[ZZ]), iprod(dx, dx), iprod(dDS, dDS));
823 fprintf(debug, "WPOL: dHH=(%10g,%10g,%10g)\n", dHH[XX], dHH[YY], dHH[ZZ]);
824 fprintf(debug, "WPOL: dOD=(%10g,%10g,%10g), 1/r_OD = %10g\n",
825 dOD[XX], dOD[YY], dOD[ZZ], 1/r_OD);
826 fprintf(debug, "WPOL: nW =(%10g,%10g,%10g), 1/r_nW = %10g\n",
827 nW[XX], nW[YY], nW[ZZ], 1/r_nW);
828 fprintf(debug, "WPOL: dx =%10g, dy =%10g, dz =%10g\n",
829 dx[XX], dx[YY], dx[ZZ]);
830 fprintf(debug, "WPOL: dDSx=%10g, dDSy=%10g, dDSz=%10g\n",
831 dDS[XX], dDS[YY], dDS[ZZ]);
834 /* Now compute the forces and energy */
835 kdx[XX] = kk[XX]*dx[XX];
836 kdx[YY] = kk[YY]*dx[YY];
837 kdx[ZZ] = kk[ZZ]*dx[ZZ];
838 vtot += iprod(dx, kdx);
842 ivec_sub(SHIFT_IVEC(g, aS), SHIFT_IVEC(g, aD), dt);
846 for (m = 0; (m < DIM); m++)
848 /* This is a tensor operation but written out for speed */
858 fshift[ki][m] += fij;
859 fshift[CENTRAL][m] -= fij;
864 fprintf(debug, "WPOL: vwpol=%g\n", 0.5*iprod(dx, kdx));
865 fprintf(debug, "WPOL: df = (%10g, %10g, %10g)\n", df[XX], df[YY], df[ZZ]);
873 static real do_1_thole(const rvec xi, const rvec xj, rvec fi, rvec fj,
874 const t_pbc *pbc, real qq,
875 rvec fshift[], real afac)
878 real r12sq, r12_1, r12n, r12bar, v0, v1, fscal, ebar, fff;
881 t = pbc_rvec_sub(pbc, xi, xj, r12); /* 3 */
883 r12sq = iprod(r12, r12); /* 5 */
884 r12_1 = gmx_invsqrt(r12sq); /* 5 */
885 r12bar = afac/r12_1; /* 5 */
886 v0 = qq*ONE_4PI_EPS0*r12_1; /* 2 */
887 ebar = exp(-r12bar); /* 5 */
888 v1 = (1-(1+0.5*r12bar)*ebar); /* 4 */
889 fscal = ((v0*r12_1)*v1 - v0*0.5*afac*ebar*(r12bar+1))*r12_1; /* 9 */
892 fprintf(debug, "THOLE: v0 = %.3f v1 = %.3f r12= % .3f r12bar = %.3f fscal = %.3f ebar = %.3f\n", v0, v1, 1/r12_1, r12bar, fscal, ebar);
895 for (m = 0; (m < DIM); m++)
901 fshift[CENTRAL][m] -= fff;
904 return v0*v1; /* 1 */
908 real thole_pol(int nbonds,
909 const t_iatom forceatoms[], const t_iparams forceparams[],
910 const rvec x[], rvec f[], rvec fshift[],
911 const t_pbc *pbc, const t_graph gmx_unused *g,
912 real gmx_unused lambda, real gmx_unused *dvdlambda,
913 const t_mdatoms *md, t_fcdata gmx_unused *fcd,
914 int gmx_unused *global_atom_index)
916 /* Interaction between two pairs of particles with opposite charge */
917 int i, type, a1, da1, a2, da2;
918 real q1, q2, qq, a, al1, al2, afac;
921 for (i = 0; (i < nbonds); )
923 type = forceatoms[i++];
924 a1 = forceatoms[i++];
925 da1 = forceatoms[i++];
926 a2 = forceatoms[i++];
927 da2 = forceatoms[i++];
928 q1 = md->chargeA[da1];
929 q2 = md->chargeA[da2];
930 a = forceparams[type].thole.a;
931 al1 = forceparams[type].thole.alpha1;
932 al2 = forceparams[type].thole.alpha2;
934 afac = a*pow(al1*al2, -1.0/6.0);
935 V += do_1_thole(x[a1], x[a2], f[a1], f[a2], pbc, qq, fshift, afac);
936 V += do_1_thole(x[da1], x[a2], f[da1], f[a2], pbc, -qq, fshift, afac);
937 V += do_1_thole(x[a1], x[da2], f[a1], f[da2], pbc, -qq, fshift, afac);
938 V += do_1_thole(x[da1], x[da2], f[da1], f[da2], pbc, qq, fshift, afac);
944 real bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
945 rvec r_ij, rvec r_kj, real *costh,
947 /* Return value is the angle between the bonds i-j and j-k */
952 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
953 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
955 *costh = cos_angle(r_ij, r_kj); /* 25 */
956 th = acos(*costh); /* 10 */
961 real angles(int nbonds,
962 const t_iatom forceatoms[], const t_iparams forceparams[],
963 const rvec x[], rvec f[], rvec fshift[],
964 const t_pbc *pbc, const t_graph *g,
965 real lambda, real *dvdlambda,
966 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
967 int gmx_unused *global_atom_index)
969 int i, ai, aj, ak, t1, t2, type;
971 real cos_theta, cos_theta2, theta, dVdt, va, vtot;
972 ivec jt, dt_ij, dt_kj;
975 for (i = 0; i < nbonds; )
977 type = forceatoms[i++];
978 ai = forceatoms[i++];
979 aj = forceatoms[i++];
980 ak = forceatoms[i++];
982 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
983 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
985 *dvdlambda += harmonic(forceparams[type].harmonic.krA,
986 forceparams[type].harmonic.krB,
987 forceparams[type].harmonic.rA*DEG2RAD,
988 forceparams[type].harmonic.rB*DEG2RAD,
989 theta, lambda, &va, &dVdt); /* 21 */
992 cos_theta2 = sqr(cos_theta);
1002 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1003 sth = st*cos_theta; /* 1 */
1007 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1008 theta*RAD2DEG, va, dVdt);
1011 nrij2 = iprod(r_ij, r_ij); /* 5 */
1012 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1014 nrij_1 = gmx_invsqrt(nrij2); /* 10 */
1015 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1017 cik = st*nrij_1*nrkj_1; /* 2 */
1018 cii = sth*nrij_1*nrij_1; /* 2 */
1019 ckk = sth*nrkj_1*nrkj_1; /* 2 */
1021 for (m = 0; m < DIM; m++)
1023 f_i[m] = -(cik*r_kj[m] - cii*r_ij[m]);
1024 f_k[m] = -(cik*r_ij[m] - ckk*r_kj[m]);
1025 f_j[m] = -f_i[m] - f_k[m];
1032 copy_ivec(SHIFT_IVEC(g, aj), jt);
1034 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1035 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1036 t1 = IVEC2IS(dt_ij);
1037 t2 = IVEC2IS(dt_kj);
1039 rvec_inc(fshift[t1], f_i);
1040 rvec_inc(fshift[CENTRAL], f_j);
1041 rvec_inc(fshift[t2], f_k);
1048 #ifdef GMX_SIMD_HAVE_REAL
1050 /* As angles, but using SIMD to calculate many dihedrals at once.
1051 * This routines does not calculate energies and shift forces.
1053 static gmx_inline void
1054 angles_noener_simd(int nbonds,
1055 const t_iatom forceatoms[], const t_iparams forceparams[],
1056 const rvec x[], rvec f[],
1057 const t_pbc *pbc, const t_graph gmx_unused *g,
1058 real gmx_unused lambda,
1059 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1060 int gmx_unused *global_atom_index)
1064 int type, ai[GMX_SIMD_REAL_WIDTH], aj[GMX_SIMD_REAL_WIDTH];
1065 int ak[GMX_SIMD_REAL_WIDTH];
1066 real coeff_array[2*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *coeff;
1067 real dr_array[2*DIM*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *dr;
1068 real f_buf_array[6*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *f_buf;
1069 gmx_simd_real_t k_S, theta0_S;
1070 gmx_simd_real_t rijx_S, rijy_S, rijz_S;
1071 gmx_simd_real_t rkjx_S, rkjy_S, rkjz_S;
1072 gmx_simd_real_t one_S;
1073 gmx_simd_real_t min_one_plus_eps_S;
1074 gmx_simd_real_t rij_rkj_S;
1075 gmx_simd_real_t nrij2_S, nrij_1_S;
1076 gmx_simd_real_t nrkj2_S, nrkj_1_S;
1077 gmx_simd_real_t cos_S, invsin_S;
1078 gmx_simd_real_t theta_S;
1079 gmx_simd_real_t st_S, sth_S;
1080 gmx_simd_real_t cik_S, cii_S, ckk_S;
1081 gmx_simd_real_t f_ix_S, f_iy_S, f_iz_S;
1082 gmx_simd_real_t f_kx_S, f_ky_S, f_kz_S;
1083 pbc_simd_t pbc_simd;
1085 /* Ensure register memory alignment */
1086 coeff = gmx_simd_align_r(coeff_array);
1087 dr = gmx_simd_align_r(dr_array);
1088 f_buf = gmx_simd_align_r(f_buf_array);
1090 set_pbc_simd(pbc, &pbc_simd);
1092 one_S = gmx_simd_set1_r(1.0);
1094 /* The smallest number > -1 */
1095 min_one_plus_eps_S = gmx_simd_set1_r(-1.0 + 2*GMX_REAL_EPS);
1097 /* nbonds is the number of angles times nfa1, here we step GMX_SIMD_REAL_WIDTH angles */
1098 for (i = 0; (i < nbonds); i += GMX_SIMD_REAL_WIDTH*nfa1)
1100 /* Collect atoms for GMX_SIMD_REAL_WIDTH angles.
1101 * iu indexes into forceatoms, we should not let iu go beyond nbonds.
1104 for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
1106 type = forceatoms[iu];
1107 ai[s] = forceatoms[iu+1];
1108 aj[s] = forceatoms[iu+2];
1109 ak[s] = forceatoms[iu+3];
1111 coeff[s] = forceparams[type].harmonic.krA;
1112 coeff[GMX_SIMD_REAL_WIDTH+s] = forceparams[type].harmonic.rA*DEG2RAD;
1114 /* If you can't use pbc_dx_simd below for PBC, e.g. because
1115 * you can't round in SIMD, use pbc_rvec_sub here.
1117 /* Store the non PBC corrected distances packed and aligned */
1118 for (m = 0; m < DIM; m++)
1120 dr[s + m *GMX_SIMD_REAL_WIDTH] = x[ai[s]][m] - x[aj[s]][m];
1121 dr[s + (DIM+m)*GMX_SIMD_REAL_WIDTH] = x[ak[s]][m] - x[aj[s]][m];
1124 /* At the end fill the arrays with identical entries */
1125 if (iu + nfa1 < nbonds)
1131 k_S = gmx_simd_load_r(coeff);
1132 theta0_S = gmx_simd_load_r(coeff+GMX_SIMD_REAL_WIDTH);
1134 rijx_S = gmx_simd_load_r(dr + 0*GMX_SIMD_REAL_WIDTH);
1135 rijy_S = gmx_simd_load_r(dr + 1*GMX_SIMD_REAL_WIDTH);
1136 rijz_S = gmx_simd_load_r(dr + 2*GMX_SIMD_REAL_WIDTH);
1137 rkjx_S = gmx_simd_load_r(dr + 3*GMX_SIMD_REAL_WIDTH);
1138 rkjy_S = gmx_simd_load_r(dr + 4*GMX_SIMD_REAL_WIDTH);
1139 rkjz_S = gmx_simd_load_r(dr + 5*GMX_SIMD_REAL_WIDTH);
1141 pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, &pbc_simd);
1142 pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, &pbc_simd);
1144 rij_rkj_S = gmx_simd_iprod_r(rijx_S, rijy_S, rijz_S,
1145 rkjx_S, rkjy_S, rkjz_S);
1147 nrij2_S = gmx_simd_norm2_r(rijx_S, rijy_S, rijz_S);
1148 nrkj2_S = gmx_simd_norm2_r(rkjx_S, rkjy_S, rkjz_S);
1150 nrij_1_S = gmx_simd_invsqrt_r(nrij2_S);
1151 nrkj_1_S = gmx_simd_invsqrt_r(nrkj2_S);
1153 cos_S = gmx_simd_mul_r(rij_rkj_S, gmx_simd_mul_r(nrij_1_S, nrkj_1_S));
1155 /* To allow for 180 degrees, we take the max of cos and -1 + 1bit,
1156 * so we can safely get the 1/sin from 1/sqrt(1 - cos^2).
1157 * This also ensures that rounding errors would cause the argument
1158 * of gmx_simd_acos_r to be < -1.
1159 * Note that we do not take precautions for cos(0)=1, so the outer
1160 * atoms in an angle should not be on top of each other.
1162 cos_S = gmx_simd_max_r(cos_S, min_one_plus_eps_S);
1164 theta_S = gmx_simd_acos_r(cos_S);
1166 invsin_S = gmx_simd_invsqrt_r(gmx_simd_sub_r(one_S, gmx_simd_mul_r(cos_S, cos_S)));
1168 st_S = gmx_simd_mul_r(gmx_simd_mul_r(k_S, gmx_simd_sub_r(theta0_S, theta_S)),
1170 sth_S = gmx_simd_mul_r(st_S, cos_S);
1172 cik_S = gmx_simd_mul_r(st_S, gmx_simd_mul_r(nrij_1_S, nrkj_1_S));
1173 cii_S = gmx_simd_mul_r(sth_S, gmx_simd_mul_r(nrij_1_S, nrij_1_S));
1174 ckk_S = gmx_simd_mul_r(sth_S, gmx_simd_mul_r(nrkj_1_S, nrkj_1_S));
1176 f_ix_S = gmx_simd_mul_r(cii_S, rijx_S);
1177 f_ix_S = gmx_simd_fnmadd_r(cik_S, rkjx_S, f_ix_S);
1178 f_iy_S = gmx_simd_mul_r(cii_S, rijy_S);
1179 f_iy_S = gmx_simd_fnmadd_r(cik_S, rkjy_S, f_iy_S);
1180 f_iz_S = gmx_simd_mul_r(cii_S, rijz_S);
1181 f_iz_S = gmx_simd_fnmadd_r(cik_S, rkjz_S, f_iz_S);
1182 f_kx_S = gmx_simd_mul_r(ckk_S, rkjx_S);
1183 f_kx_S = gmx_simd_fnmadd_r(cik_S, rijx_S, f_kx_S);
1184 f_ky_S = gmx_simd_mul_r(ckk_S, rkjy_S);
1185 f_ky_S = gmx_simd_fnmadd_r(cik_S, rijy_S, f_ky_S);
1186 f_kz_S = gmx_simd_mul_r(ckk_S, rkjz_S);
1187 f_kz_S = gmx_simd_fnmadd_r(cik_S, rijz_S, f_kz_S);
1189 gmx_simd_store_r(f_buf + 0*GMX_SIMD_REAL_WIDTH, f_ix_S);
1190 gmx_simd_store_r(f_buf + 1*GMX_SIMD_REAL_WIDTH, f_iy_S);
1191 gmx_simd_store_r(f_buf + 2*GMX_SIMD_REAL_WIDTH, f_iz_S);
1192 gmx_simd_store_r(f_buf + 3*GMX_SIMD_REAL_WIDTH, f_kx_S);
1193 gmx_simd_store_r(f_buf + 4*GMX_SIMD_REAL_WIDTH, f_ky_S);
1194 gmx_simd_store_r(f_buf + 5*GMX_SIMD_REAL_WIDTH, f_kz_S);
1200 for (m = 0; m < DIM; m++)
1202 f[ai[s]][m] += f_buf[s + m*GMX_SIMD_REAL_WIDTH];
1203 f[aj[s]][m] -= f_buf[s + m*GMX_SIMD_REAL_WIDTH] + f_buf[s + (DIM+m)*GMX_SIMD_REAL_WIDTH];
1204 f[ak[s]][m] += f_buf[s + (DIM+m)*GMX_SIMD_REAL_WIDTH];
1209 while (s < GMX_SIMD_REAL_WIDTH && iu < nbonds);
1213 #endif /* GMX_SIMD_HAVE_REAL */
1215 real linear_angles(int nbonds,
1216 const t_iatom forceatoms[], const t_iparams forceparams[],
1217 const rvec x[], rvec f[], rvec fshift[],
1218 const t_pbc *pbc, const t_graph *g,
1219 real lambda, real *dvdlambda,
1220 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1221 int gmx_unused *global_atom_index)
1223 int i, m, ai, aj, ak, t1, t2, type;
1225 real L1, kA, kB, aA, aB, dr, dr2, va, vtot, a, b, klin;
1226 ivec jt, dt_ij, dt_kj;
1227 rvec r_ij, r_kj, r_ik, dx;
1231 for (i = 0; (i < nbonds); )
1233 type = forceatoms[i++];
1234 ai = forceatoms[i++];
1235 aj = forceatoms[i++];
1236 ak = forceatoms[i++];
1238 kA = forceparams[type].linangle.klinA;
1239 kB = forceparams[type].linangle.klinB;
1240 klin = L1*kA + lambda*kB;
1242 aA = forceparams[type].linangle.aA;
1243 aB = forceparams[type].linangle.aB;
1244 a = L1*aA+lambda*aB;
1247 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
1248 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
1249 rvec_sub(r_ij, r_kj, r_ik);
1252 for (m = 0; (m < DIM); m++)
1254 dr = -a * r_ij[m] - b * r_kj[m];
1259 f_j[m] = -(f_i[m]+f_k[m]);
1265 *dvdlambda += 0.5*(kB-kA)*dr2 + klin*(aB-aA)*iprod(dx, r_ik);
1271 copy_ivec(SHIFT_IVEC(g, aj), jt);
1273 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1274 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1275 t1 = IVEC2IS(dt_ij);
1276 t2 = IVEC2IS(dt_kj);
1278 rvec_inc(fshift[t1], f_i);
1279 rvec_inc(fshift[CENTRAL], f_j);
1280 rvec_inc(fshift[t2], f_k);
1285 real urey_bradley(int nbonds,
1286 const t_iatom forceatoms[], const t_iparams forceparams[],
1287 const rvec x[], rvec f[], rvec fshift[],
1288 const t_pbc *pbc, const t_graph *g,
1289 real lambda, real *dvdlambda,
1290 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1291 int gmx_unused *global_atom_index)
1293 int i, m, ai, aj, ak, t1, t2, type, ki;
1294 rvec r_ij, r_kj, r_ik;
1295 real cos_theta, cos_theta2, theta;
1296 real dVdt, va, vtot, dr, dr2, vbond, fbond, fik;
1297 real kthA, th0A, kUBA, r13A, kthB, th0B, kUBB, r13B;
1298 ivec jt, dt_ij, dt_kj, dt_ik;
1301 for (i = 0; (i < nbonds); )
1303 type = forceatoms[i++];
1304 ai = forceatoms[i++];
1305 aj = forceatoms[i++];
1306 ak = forceatoms[i++];
1307 th0A = forceparams[type].u_b.thetaA*DEG2RAD;
1308 kthA = forceparams[type].u_b.kthetaA;
1309 r13A = forceparams[type].u_b.r13A;
1310 kUBA = forceparams[type].u_b.kUBA;
1311 th0B = forceparams[type].u_b.thetaB*DEG2RAD;
1312 kthB = forceparams[type].u_b.kthetaB;
1313 r13B = forceparams[type].u_b.r13B;
1314 kUBB = forceparams[type].u_b.kUBB;
1316 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
1317 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
1319 *dvdlambda += harmonic(kthA, kthB, th0A, th0B, theta, lambda, &va, &dVdt); /* 21 */
1322 ki = pbc_rvec_sub(pbc, x[ai], x[ak], r_ik); /* 3 */
1323 dr2 = iprod(r_ik, r_ik); /* 5 */
1324 dr = dr2*gmx_invsqrt(dr2); /* 10 */
1326 *dvdlambda += harmonic(kUBA, kUBB, r13A, r13B, dr, lambda, &vbond, &fbond); /* 19 */
1328 cos_theta2 = sqr(cos_theta); /* 1 */
1336 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1337 sth = st*cos_theta; /* 1 */
1341 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1342 theta*RAD2DEG, va, dVdt);
1345 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1346 nrij2 = iprod(r_ij, r_ij);
1348 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
1349 cii = sth/nrij2; /* 10 */
1350 ckk = sth/nrkj2; /* 10 */
1352 for (m = 0; (m < DIM); m++) /* 39 */
1354 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
1355 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
1356 f_j[m] = -f_i[m]-f_k[m];
1363 copy_ivec(SHIFT_IVEC(g, aj), jt);
1365 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1366 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1367 t1 = IVEC2IS(dt_ij);
1368 t2 = IVEC2IS(dt_kj);
1370 rvec_inc(fshift[t1], f_i);
1371 rvec_inc(fshift[CENTRAL], f_j);
1372 rvec_inc(fshift[t2], f_k);
1374 /* Time for the bond calculations */
1380 vtot += vbond; /* 1*/
1381 fbond *= gmx_invsqrt(dr2); /* 6 */
1385 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, ak), dt_ik);
1386 ki = IVEC2IS(dt_ik);
1388 for (m = 0; (m < DIM); m++) /* 15 */
1390 fik = fbond*r_ik[m];
1393 fshift[ki][m] += fik;
1394 fshift[CENTRAL][m] -= fik;
1400 real quartic_angles(int nbonds,
1401 const t_iatom forceatoms[], const t_iparams forceparams[],
1402 const rvec x[], rvec f[], rvec fshift[],
1403 const t_pbc *pbc, const t_graph *g,
1404 real gmx_unused lambda, real gmx_unused *dvdlambda,
1405 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1406 int gmx_unused *global_atom_index)
1408 int i, j, ai, aj, ak, t1, t2, type;
1410 real cos_theta, cos_theta2, theta, dt, dVdt, va, dtp, c, vtot;
1411 ivec jt, dt_ij, dt_kj;
1414 for (i = 0; (i < nbonds); )
1416 type = forceatoms[i++];
1417 ai = forceatoms[i++];
1418 aj = forceatoms[i++];
1419 ak = forceatoms[i++];
1421 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
1422 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
1424 dt = theta - forceparams[type].qangle.theta*DEG2RAD; /* 2 */
1427 va = forceparams[type].qangle.c[0];
1429 for (j = 1; j <= 4; j++)
1431 c = forceparams[type].qangle.c[j];
1440 cos_theta2 = sqr(cos_theta); /* 1 */
1449 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1450 sth = st*cos_theta; /* 1 */
1454 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1455 theta*RAD2DEG, va, dVdt);
1458 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1459 nrij2 = iprod(r_ij, r_ij);
1461 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
1462 cii = sth/nrij2; /* 10 */
1463 ckk = sth/nrkj2; /* 10 */
1465 for (m = 0; (m < DIM); m++) /* 39 */
1467 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
1468 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
1469 f_j[m] = -f_i[m]-f_k[m];
1476 copy_ivec(SHIFT_IVEC(g, aj), jt);
1478 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1479 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1480 t1 = IVEC2IS(dt_ij);
1481 t2 = IVEC2IS(dt_kj);
1483 rvec_inc(fshift[t1], f_i);
1484 rvec_inc(fshift[CENTRAL], f_j);
1485 rvec_inc(fshift[t2], f_k);
1491 real dih_angle(const rvec xi, const rvec xj, const rvec xk, const rvec xl,
1493 rvec r_ij, rvec r_kj, rvec r_kl, rvec m, rvec n,
1494 real *sign, int *t1, int *t2, int *t3)
1498 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
1499 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
1500 *t3 = pbc_rvec_sub(pbc, xk, xl, r_kl); /* 3 */
1502 cprod(r_ij, r_kj, m); /* 9 */
1503 cprod(r_kj, r_kl, n); /* 9 */
1504 phi = gmx_angle(m, n); /* 49 (assuming 25 for atan2) */
1505 ipr = iprod(r_ij, n); /* 5 */
1506 (*sign) = (ipr < 0.0) ? -1.0 : 1.0;
1507 phi = (*sign)*phi; /* 1 */
1513 #ifdef GMX_SIMD_HAVE_REAL
1515 /* As dih_angle above, but calculates 4 dihedral angles at once using SIMD,
1516 * also calculates the pre-factor required for the dihedral force update.
1517 * Note that bv and buf should be register aligned.
1519 static gmx_inline void
1520 dih_angle_simd(const rvec *x,
1521 const int *ai, const int *aj, const int *ak, const int *al,
1522 const pbc_simd_t *pbc,
1524 gmx_simd_real_t *phi_S,
1525 gmx_simd_real_t *mx_S, gmx_simd_real_t *my_S, gmx_simd_real_t *mz_S,
1526 gmx_simd_real_t *nx_S, gmx_simd_real_t *ny_S, gmx_simd_real_t *nz_S,
1527 gmx_simd_real_t *nrkj_m2_S,
1528 gmx_simd_real_t *nrkj_n2_S,
1533 gmx_simd_real_t rijx_S, rijy_S, rijz_S;
1534 gmx_simd_real_t rkjx_S, rkjy_S, rkjz_S;
1535 gmx_simd_real_t rklx_S, rkly_S, rklz_S;
1536 gmx_simd_real_t cx_S, cy_S, cz_S;
1537 gmx_simd_real_t cn_S;
1538 gmx_simd_real_t s_S;
1539 gmx_simd_real_t ipr_S;
1540 gmx_simd_real_t iprm_S, iprn_S;
1541 gmx_simd_real_t nrkj2_S, nrkj_1_S, nrkj_2_S, nrkj_S;
1542 gmx_simd_real_t toler_S;
1543 gmx_simd_real_t p_S, q_S;
1544 gmx_simd_real_t nrkj2_min_S;
1545 gmx_simd_real_t real_eps_S;
1547 /* Used to avoid division by zero.
1548 * We take into acount that we multiply the result by real_eps_S.
1550 nrkj2_min_S = gmx_simd_set1_r(GMX_REAL_MIN/(2*GMX_REAL_EPS));
1552 /* The value of the last significant bit (GMX_REAL_EPS is half of that) */
1553 real_eps_S = gmx_simd_set1_r(2*GMX_REAL_EPS);
1555 for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
1557 /* If you can't use pbc_dx_simd below for PBC, e.g. because
1558 * you can't round in SIMD, use pbc_rvec_sub here.
1560 for (m = 0; m < DIM; m++)
1562 dr[s + (0*DIM + m)*GMX_SIMD_REAL_WIDTH] = x[ai[s]][m] - x[aj[s]][m];
1563 dr[s + (1*DIM + m)*GMX_SIMD_REAL_WIDTH] = x[ak[s]][m] - x[aj[s]][m];
1564 dr[s + (2*DIM + m)*GMX_SIMD_REAL_WIDTH] = x[ak[s]][m] - x[al[s]][m];
1568 rijx_S = gmx_simd_load_r(dr + 0*GMX_SIMD_REAL_WIDTH);
1569 rijy_S = gmx_simd_load_r(dr + 1*GMX_SIMD_REAL_WIDTH);
1570 rijz_S = gmx_simd_load_r(dr + 2*GMX_SIMD_REAL_WIDTH);
1571 rkjx_S = gmx_simd_load_r(dr + 3*GMX_SIMD_REAL_WIDTH);
1572 rkjy_S = gmx_simd_load_r(dr + 4*GMX_SIMD_REAL_WIDTH);
1573 rkjz_S = gmx_simd_load_r(dr + 5*GMX_SIMD_REAL_WIDTH);
1574 rklx_S = gmx_simd_load_r(dr + 6*GMX_SIMD_REAL_WIDTH);
1575 rkly_S = gmx_simd_load_r(dr + 7*GMX_SIMD_REAL_WIDTH);
1576 rklz_S = gmx_simd_load_r(dr + 8*GMX_SIMD_REAL_WIDTH);
1578 pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, pbc);
1579 pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, pbc);
1580 pbc_dx_simd(&rklx_S, &rkly_S, &rklz_S, pbc);
1582 gmx_simd_cprod_r(rijx_S, rijy_S, rijz_S,
1583 rkjx_S, rkjy_S, rkjz_S,
1586 gmx_simd_cprod_r(rkjx_S, rkjy_S, rkjz_S,
1587 rklx_S, rkly_S, rklz_S,
1590 gmx_simd_cprod_r(*mx_S, *my_S, *mz_S,
1591 *nx_S, *ny_S, *nz_S,
1592 &cx_S, &cy_S, &cz_S);
1594 cn_S = gmx_simd_sqrt_r(gmx_simd_norm2_r(cx_S, cy_S, cz_S));
1596 s_S = gmx_simd_iprod_r(*mx_S, *my_S, *mz_S, *nx_S, *ny_S, *nz_S);
1598 /* Determine the dihedral angle, the sign might need correction */
1599 *phi_S = gmx_simd_atan2_r(cn_S, s_S);
1601 ipr_S = gmx_simd_iprod_r(rijx_S, rijy_S, rijz_S,
1602 *nx_S, *ny_S, *nz_S);
1604 iprm_S = gmx_simd_norm2_r(*mx_S, *my_S, *mz_S);
1605 iprn_S = gmx_simd_norm2_r(*nx_S, *ny_S, *nz_S);
1607 nrkj2_S = gmx_simd_norm2_r(rkjx_S, rkjy_S, rkjz_S);
1609 /* Avoid division by zero. When zero, the result is multiplied by 0
1610 * anyhow, so the 3 max below do not affect the final result.
1612 nrkj2_S = gmx_simd_max_r(nrkj2_S, nrkj2_min_S);
1613 nrkj_1_S = gmx_simd_invsqrt_r(nrkj2_S);
1614 nrkj_2_S = gmx_simd_mul_r(nrkj_1_S, nrkj_1_S);
1615 nrkj_S = gmx_simd_mul_r(nrkj2_S, nrkj_1_S);
1617 toler_S = gmx_simd_mul_r(nrkj2_S, real_eps_S);
1619 /* Here the plain-C code uses a conditional, but we can't do that in SIMD.
1620 * So we take a max with the tolerance instead. Since we multiply with
1621 * m or n later, the max does not affect the results.
1623 iprm_S = gmx_simd_max_r(iprm_S, toler_S);
1624 iprn_S = gmx_simd_max_r(iprn_S, toler_S);
1625 *nrkj_m2_S = gmx_simd_mul_r(nrkj_S, gmx_simd_inv_r(iprm_S));
1626 *nrkj_n2_S = gmx_simd_mul_r(nrkj_S, gmx_simd_inv_r(iprn_S));
1628 /* Set sign of phi_S with the sign of ipr_S; phi_S is currently positive */
1629 *phi_S = gmx_simd_xor_sign_r(*phi_S, ipr_S);
1630 p_S = gmx_simd_iprod_r(rijx_S, rijy_S, rijz_S,
1631 rkjx_S, rkjy_S, rkjz_S);
1632 p_S = gmx_simd_mul_r(p_S, nrkj_2_S);
1634 q_S = gmx_simd_iprod_r(rklx_S, rkly_S, rklz_S,
1635 rkjx_S, rkjy_S, rkjz_S);
1636 q_S = gmx_simd_mul_r(q_S, nrkj_2_S);
1638 gmx_simd_store_r(p, p_S);
1639 gmx_simd_store_r(q, q_S);
1642 #endif /* GMX_SIMD_HAVE_REAL */
1645 void do_dih_fup(int i, int j, int k, int l, real ddphi,
1646 rvec r_ij, rvec r_kj, rvec r_kl,
1647 rvec m, rvec n, rvec f[], rvec fshift[],
1648 const t_pbc *pbc, const t_graph *g,
1649 const rvec x[], int t1, int t2, int t3)
1652 rvec f_i, f_j, f_k, f_l;
1653 rvec uvec, vvec, svec, dx_jl;
1654 real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1655 real a, b, p, q, toler;
1656 ivec jt, dt_ij, dt_kj, dt_lj;
1658 iprm = iprod(m, m); /* 5 */
1659 iprn = iprod(n, n); /* 5 */
1660 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1661 toler = nrkj2*GMX_REAL_EPS;
1662 if ((iprm > toler) && (iprn > toler))
1664 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1665 nrkj_2 = nrkj_1*nrkj_1; /* 1 */
1666 nrkj = nrkj2*nrkj_1; /* 1 */
1667 a = -ddphi*nrkj/iprm; /* 11 */
1668 svmul(a, m, f_i); /* 3 */
1669 b = ddphi*nrkj/iprn; /* 11 */
1670 svmul(b, n, f_l); /* 3 */
1671 p = iprod(r_ij, r_kj); /* 5 */
1672 p *= nrkj_2; /* 1 */
1673 q = iprod(r_kl, r_kj); /* 5 */
1674 q *= nrkj_2; /* 1 */
1675 svmul(p, f_i, uvec); /* 3 */
1676 svmul(q, f_l, vvec); /* 3 */
1677 rvec_sub(uvec, vvec, svec); /* 3 */
1678 rvec_sub(f_i, svec, f_j); /* 3 */
1679 rvec_add(f_l, svec, f_k); /* 3 */
1680 rvec_inc(f[i], f_i); /* 3 */
1681 rvec_dec(f[j], f_j); /* 3 */
1682 rvec_dec(f[k], f_k); /* 3 */
1683 rvec_inc(f[l], f_l); /* 3 */
1687 copy_ivec(SHIFT_IVEC(g, j), jt);
1688 ivec_sub(SHIFT_IVEC(g, i), jt, dt_ij);
1689 ivec_sub(SHIFT_IVEC(g, k), jt, dt_kj);
1690 ivec_sub(SHIFT_IVEC(g, l), jt, dt_lj);
1691 t1 = IVEC2IS(dt_ij);
1692 t2 = IVEC2IS(dt_kj);
1693 t3 = IVEC2IS(dt_lj);
1697 t3 = pbc_rvec_sub(pbc, x[l], x[j], dx_jl);
1704 rvec_inc(fshift[t1], f_i);
1705 rvec_dec(fshift[CENTRAL], f_j);
1706 rvec_dec(fshift[t2], f_k);
1707 rvec_inc(fshift[t3], f_l);
1712 /* As do_dih_fup above, but without shift forces */
1714 do_dih_fup_noshiftf(int i, int j, int k, int l, real ddphi,
1715 rvec r_ij, rvec r_kj, rvec r_kl,
1716 rvec m, rvec n, rvec f[])
1718 rvec f_i, f_j, f_k, f_l;
1719 rvec uvec, vvec, svec, dx_jl;
1720 real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1721 real a, b, p, q, toler;
1722 ivec jt, dt_ij, dt_kj, dt_lj;
1724 iprm = iprod(m, m); /* 5 */
1725 iprn = iprod(n, n); /* 5 */
1726 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1727 toler = nrkj2*GMX_REAL_EPS;
1728 if ((iprm > toler) && (iprn > toler))
1730 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1731 nrkj_2 = nrkj_1*nrkj_1; /* 1 */
1732 nrkj = nrkj2*nrkj_1; /* 1 */
1733 a = -ddphi*nrkj/iprm; /* 11 */
1734 svmul(a, m, f_i); /* 3 */
1735 b = ddphi*nrkj/iprn; /* 11 */
1736 svmul(b, n, f_l); /* 3 */
1737 p = iprod(r_ij, r_kj); /* 5 */
1738 p *= nrkj_2; /* 1 */
1739 q = iprod(r_kl, r_kj); /* 5 */
1740 q *= nrkj_2; /* 1 */
1741 svmul(p, f_i, uvec); /* 3 */
1742 svmul(q, f_l, vvec); /* 3 */
1743 rvec_sub(uvec, vvec, svec); /* 3 */
1744 rvec_sub(f_i, svec, f_j); /* 3 */
1745 rvec_add(f_l, svec, f_k); /* 3 */
1746 rvec_inc(f[i], f_i); /* 3 */
1747 rvec_dec(f[j], f_j); /* 3 */
1748 rvec_dec(f[k], f_k); /* 3 */
1749 rvec_inc(f[l], f_l); /* 3 */
1753 /* As do_dih_fup_noshiftf above, but with pre-calculated pre-factors */
1754 static gmx_inline void
1755 do_dih_fup_noshiftf_precalc(int i, int j, int k, int l,
1757 real f_i_x, real f_i_y, real f_i_z,
1758 real mf_l_x, real mf_l_y, real mf_l_z,
1761 rvec f_i, f_j, f_k, f_l;
1762 rvec uvec, vvec, svec;
1770 svmul(p, f_i, uvec);
1771 svmul(q, f_l, vvec);
1772 rvec_sub(uvec, vvec, svec);
1773 rvec_sub(f_i, svec, f_j);
1774 rvec_add(f_l, svec, f_k);
1775 rvec_inc(f[i], f_i);
1776 rvec_dec(f[j], f_j);
1777 rvec_dec(f[k], f_k);
1778 rvec_inc(f[l], f_l);
1782 real dopdihs(real cpA, real cpB, real phiA, real phiB, int mult,
1783 real phi, real lambda, real *V, real *F)
1785 real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1786 real L1 = 1.0 - lambda;
1787 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1788 real dph0 = (phiB - phiA)*DEG2RAD;
1789 real cp = L1*cpA + lambda*cpB;
1791 mdphi = mult*phi - ph0;
1793 ddphi = -cp*mult*sdphi;
1794 v1 = 1.0 + cos(mdphi);
1797 dvdlambda = (cpB - cpA)*v1 + cp*dph0*sdphi;
1804 /* That was 40 flops */
1808 dopdihs_noener(real cpA, real cpB, real phiA, real phiB, int mult,
1809 real phi, real lambda, real *F)
1811 real mdphi, sdphi, ddphi;
1812 real L1 = 1.0 - lambda;
1813 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1814 real cp = L1*cpA + lambda*cpB;
1816 mdphi = mult*phi - ph0;
1818 ddphi = -cp*mult*sdphi;
1822 /* That was 20 flops */
1826 dopdihs_mdphi(real cpA, real cpB, real phiA, real phiB, int mult,
1827 real phi, real lambda, real *cp, real *mdphi)
1829 real L1 = 1.0 - lambda;
1830 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1832 *cp = L1*cpA + lambda*cpB;
1834 *mdphi = mult*phi - ph0;
1837 static real dopdihs_min(real cpA, real cpB, real phiA, real phiB, int mult,
1838 real phi, real lambda, real *V, real *F)
1839 /* similar to dopdihs, except for a minus sign *
1840 * and a different treatment of mult/phi0 */
1842 real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1843 real L1 = 1.0 - lambda;
1844 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1845 real dph0 = (phiB - phiA)*DEG2RAD;
1846 real cp = L1*cpA + lambda*cpB;
1848 mdphi = mult*(phi-ph0);
1850 ddphi = cp*mult*sdphi;
1851 v1 = 1.0-cos(mdphi);
1854 dvdlambda = (cpB-cpA)*v1 + cp*dph0*sdphi;
1861 /* That was 40 flops */
1864 real pdihs(int nbonds,
1865 const t_iatom forceatoms[], const t_iparams forceparams[],
1866 const rvec x[], rvec f[], rvec fshift[],
1867 const t_pbc *pbc, const t_graph *g,
1868 real lambda, real *dvdlambda,
1869 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1870 int gmx_unused *global_atom_index)
1872 int i, type, ai, aj, ak, al;
1874 rvec r_ij, r_kj, r_kl, m, n;
1875 real phi, sign, ddphi, vpd, vtot;
1879 for (i = 0; (i < nbonds); )
1881 type = forceatoms[i++];
1882 ai = forceatoms[i++];
1883 aj = forceatoms[i++];
1884 ak = forceatoms[i++];
1885 al = forceatoms[i++];
1887 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1888 &sign, &t1, &t2, &t3); /* 84 */
1889 *dvdlambda += dopdihs(forceparams[type].pdihs.cpA,
1890 forceparams[type].pdihs.cpB,
1891 forceparams[type].pdihs.phiA,
1892 forceparams[type].pdihs.phiB,
1893 forceparams[type].pdihs.mult,
1894 phi, lambda, &vpd, &ddphi);
1897 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
1898 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
1901 fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
1902 ai, aj, ak, al, phi);
1909 void make_dp_periodic(real *dp) /* 1 flop? */
1911 /* dp cannot be outside (-pi,pi) */
1916 else if (*dp < -M_PI)
1923 /* As pdihs above, but without calculating energies and shift forces */
1925 pdihs_noener(int nbonds,
1926 const t_iatom forceatoms[], const t_iparams forceparams[],
1927 const rvec x[], rvec f[],
1928 const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
1930 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1931 int gmx_unused *global_atom_index)
1933 int i, type, ai, aj, ak, al;
1935 rvec r_ij, r_kj, r_kl, m, n;
1936 real phi, sign, ddphi_tot, ddphi;
1938 for (i = 0; (i < nbonds); )
1940 ai = forceatoms[i+1];
1941 aj = forceatoms[i+2];
1942 ak = forceatoms[i+3];
1943 al = forceatoms[i+4];
1945 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1946 &sign, &t1, &t2, &t3);
1950 /* Loop over dihedrals working on the same atoms,
1951 * so we avoid recalculating angles and force distributions.
1955 type = forceatoms[i];
1956 dopdihs_noener(forceparams[type].pdihs.cpA,
1957 forceparams[type].pdihs.cpB,
1958 forceparams[type].pdihs.phiA,
1959 forceparams[type].pdihs.phiB,
1960 forceparams[type].pdihs.mult,
1961 phi, lambda, &ddphi);
1966 while (i < nbonds &&
1967 forceatoms[i+1] == ai &&
1968 forceatoms[i+2] == aj &&
1969 forceatoms[i+3] == ak &&
1970 forceatoms[i+4] == al);
1972 do_dih_fup_noshiftf(ai, aj, ak, al, ddphi_tot, r_ij, r_kj, r_kl, m, n, f);
1977 #ifdef GMX_SIMD_HAVE_REAL
1979 /* As pdihs_noner above, but using SIMD to calculate many dihedrals at once */
1981 pdihs_noener_simd(int nbonds,
1982 const t_iatom forceatoms[], const t_iparams forceparams[],
1983 const rvec x[], rvec f[],
1984 const t_pbc *pbc, const t_graph gmx_unused *g,
1985 real gmx_unused lambda,
1986 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1987 int gmx_unused *global_atom_index)
1991 int type, ai[GMX_SIMD_REAL_WIDTH], aj[GMX_SIMD_REAL_WIDTH], ak[GMX_SIMD_REAL_WIDTH], al[GMX_SIMD_REAL_WIDTH];
1993 real dr_array[3*DIM*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *dr;
1994 real buf_array[7*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *buf;
1995 real *cp, *phi0, *mult, *phi, *p, *q, *sf_i, *msf_l;
1996 gmx_simd_real_t phi0_S, phi_S;
1997 gmx_simd_real_t mx_S, my_S, mz_S;
1998 gmx_simd_real_t nx_S, ny_S, nz_S;
1999 gmx_simd_real_t nrkj_m2_S, nrkj_n2_S;
2000 gmx_simd_real_t cp_S, mdphi_S, mult_S;
2001 gmx_simd_real_t sin_S, cos_S;
2002 gmx_simd_real_t mddphi_S;
2003 gmx_simd_real_t sf_i_S, msf_l_S;
2004 pbc_simd_t pbc_simd;
2006 /* Ensure SIMD register alignment */
2007 dr = gmx_simd_align_r(dr_array);
2008 buf = gmx_simd_align_r(buf_array);
2010 /* Extract aligned pointer for parameters and variables */
2011 cp = buf + 0*GMX_SIMD_REAL_WIDTH;
2012 phi0 = buf + 1*GMX_SIMD_REAL_WIDTH;
2013 mult = buf + 2*GMX_SIMD_REAL_WIDTH;
2014 p = buf + 3*GMX_SIMD_REAL_WIDTH;
2015 q = buf + 4*GMX_SIMD_REAL_WIDTH;
2016 sf_i = buf + 5*GMX_SIMD_REAL_WIDTH;
2017 msf_l = buf + 6*GMX_SIMD_REAL_WIDTH;
2019 set_pbc_simd(pbc, &pbc_simd);
2021 /* nbonds is the number of dihedrals times nfa1, here we step GMX_SIMD_REAL_WIDTH dihs */
2022 for (i = 0; (i < nbonds); i += GMX_SIMD_REAL_WIDTH*nfa1)
2024 /* Collect atoms quadruplets for GMX_SIMD_REAL_WIDTH dihedrals.
2025 * iu indexes into forceatoms, we should not let iu go beyond nbonds.
2028 for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
2030 type = forceatoms[iu];
2031 ai[s] = forceatoms[iu+1];
2032 aj[s] = forceatoms[iu+2];
2033 ak[s] = forceatoms[iu+3];
2034 al[s] = forceatoms[iu+4];
2036 cp[s] = forceparams[type].pdihs.cpA;
2037 phi0[s] = forceparams[type].pdihs.phiA*DEG2RAD;
2038 mult[s] = forceparams[type].pdihs.mult;
2040 /* At the end fill the arrays with identical entries */
2041 if (iu + nfa1 < nbonds)
2047 /* Caclulate GMX_SIMD_REAL_WIDTH dihedral angles at once */
2048 dih_angle_simd(x, ai, aj, ak, al, &pbc_simd,
2051 &mx_S, &my_S, &mz_S,
2052 &nx_S, &ny_S, &nz_S,
2057 cp_S = gmx_simd_load_r(cp);
2058 phi0_S = gmx_simd_load_r(phi0);
2059 mult_S = gmx_simd_load_r(mult);
2061 mdphi_S = gmx_simd_sub_r(gmx_simd_mul_r(mult_S, phi_S), phi0_S);
2063 /* Calculate GMX_SIMD_REAL_WIDTH sines at once */
2064 gmx_simd_sincos_r(mdphi_S, &sin_S, &cos_S);
2065 mddphi_S = gmx_simd_mul_r(gmx_simd_mul_r(cp_S, mult_S), sin_S);
2066 sf_i_S = gmx_simd_mul_r(mddphi_S, nrkj_m2_S);
2067 msf_l_S = gmx_simd_mul_r(mddphi_S, nrkj_n2_S);
2069 /* After this m?_S will contain f[i] */
2070 mx_S = gmx_simd_mul_r(sf_i_S, mx_S);
2071 my_S = gmx_simd_mul_r(sf_i_S, my_S);
2072 mz_S = gmx_simd_mul_r(sf_i_S, mz_S);
2074 /* After this m?_S will contain -f[l] */
2075 nx_S = gmx_simd_mul_r(msf_l_S, nx_S);
2076 ny_S = gmx_simd_mul_r(msf_l_S, ny_S);
2077 nz_S = gmx_simd_mul_r(msf_l_S, nz_S);
2079 gmx_simd_store_r(dr + 0*GMX_SIMD_REAL_WIDTH, mx_S);
2080 gmx_simd_store_r(dr + 1*GMX_SIMD_REAL_WIDTH, my_S);
2081 gmx_simd_store_r(dr + 2*GMX_SIMD_REAL_WIDTH, mz_S);
2082 gmx_simd_store_r(dr + 3*GMX_SIMD_REAL_WIDTH, nx_S);
2083 gmx_simd_store_r(dr + 4*GMX_SIMD_REAL_WIDTH, ny_S);
2084 gmx_simd_store_r(dr + 5*GMX_SIMD_REAL_WIDTH, nz_S);
2090 do_dih_fup_noshiftf_precalc(ai[s], aj[s], ak[s], al[s],
2092 dr[ XX *GMX_SIMD_REAL_WIDTH+s],
2093 dr[ YY *GMX_SIMD_REAL_WIDTH+s],
2094 dr[ ZZ *GMX_SIMD_REAL_WIDTH+s],
2095 dr[(DIM+XX)*GMX_SIMD_REAL_WIDTH+s],
2096 dr[(DIM+YY)*GMX_SIMD_REAL_WIDTH+s],
2097 dr[(DIM+ZZ)*GMX_SIMD_REAL_WIDTH+s],
2102 while (s < GMX_SIMD_REAL_WIDTH && iu < nbonds);
2106 /* This is mostly a copy of pdihs_noener_simd above, but with using
2107 * the RB potential instead of a harmonic potential.
2108 * This function can replace rbdihs() when no energy and virial are needed.
2111 rbdihs_noener_simd(int nbonds,
2112 const t_iatom forceatoms[], const t_iparams forceparams[],
2113 const rvec x[], rvec f[],
2114 const t_pbc *pbc, const t_graph gmx_unused *g,
2115 real gmx_unused lambda,
2116 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2117 int gmx_unused *global_atom_index)
2121 int type, ai[GMX_SIMD_REAL_WIDTH], aj[GMX_SIMD_REAL_WIDTH], ak[GMX_SIMD_REAL_WIDTH], al[GMX_SIMD_REAL_WIDTH];
2123 real dr_array[3*DIM*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *dr;
2124 real buf_array[(NR_RBDIHS + 4)*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *buf;
2125 real *parm, *phi, *p, *q, *sf_i, *msf_l;
2127 gmx_simd_real_t phi_S;
2128 gmx_simd_real_t ddphi_S, cosfac_S;
2129 gmx_simd_real_t mx_S, my_S, mz_S;
2130 gmx_simd_real_t nx_S, ny_S, nz_S;
2131 gmx_simd_real_t nrkj_m2_S, nrkj_n2_S;
2132 gmx_simd_real_t parm_S, c_S;
2133 gmx_simd_real_t sin_S, cos_S;
2134 gmx_simd_real_t sf_i_S, msf_l_S;
2135 pbc_simd_t pbc_simd;
2137 gmx_simd_real_t pi_S = gmx_simd_set1_r(M_PI);
2138 gmx_simd_real_t one_S = gmx_simd_set1_r(1.0);
2140 /* Ensure SIMD register alignment */
2141 dr = gmx_simd_align_r(dr_array);
2142 buf = gmx_simd_align_r(buf_array);
2144 /* Extract aligned pointer for parameters and variables */
2146 p = buf + (NR_RBDIHS + 0)*GMX_SIMD_REAL_WIDTH;
2147 q = buf + (NR_RBDIHS + 1)*GMX_SIMD_REAL_WIDTH;
2148 sf_i = buf + (NR_RBDIHS + 2)*GMX_SIMD_REAL_WIDTH;
2149 msf_l = buf + (NR_RBDIHS + 3)*GMX_SIMD_REAL_WIDTH;
2151 set_pbc_simd(pbc, &pbc_simd);
2153 /* nbonds is the number of dihedrals times nfa1, here we step GMX_SIMD_REAL_WIDTH dihs */
2154 for (i = 0; (i < nbonds); i += GMX_SIMD_REAL_WIDTH*nfa1)
2156 /* Collect atoms quadruplets for GMX_SIMD_REAL_WIDTH dihedrals.
2157 * iu indexes into forceatoms, we should not let iu go beyond nbonds.
2160 for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
2162 type = forceatoms[iu];
2163 ai[s] = forceatoms[iu+1];
2164 aj[s] = forceatoms[iu+2];
2165 ak[s] = forceatoms[iu+3];
2166 al[s] = forceatoms[iu+4];
2168 /* We don't need the first parameter, since that's a constant
2169 * which only affects the energies, not the forces.
2171 for (j = 1; j < NR_RBDIHS; j++)
2173 parm[j*GMX_SIMD_REAL_WIDTH + s] =
2174 forceparams[type].rbdihs.rbcA[j];
2177 /* At the end fill the arrays with identical entries */
2178 if (iu + nfa1 < nbonds)
2184 /* Caclulate GMX_SIMD_REAL_WIDTH dihedral angles at once */
2185 dih_angle_simd(x, ai, aj, ak, al, &pbc_simd,
2188 &mx_S, &my_S, &mz_S,
2189 &nx_S, &ny_S, &nz_S,
2194 /* Change to polymer convention */
2195 phi_S = gmx_simd_sub_r(phi_S, pi_S);
2197 gmx_simd_sincos_r(phi_S, &sin_S, &cos_S);
2199 ddphi_S = gmx_simd_setzero_r();
2202 for (j = 1; j < NR_RBDIHS; j++)
2204 parm_S = gmx_simd_load_r(parm + j*GMX_SIMD_REAL_WIDTH);
2205 ddphi_S = gmx_simd_fmadd_r(gmx_simd_mul_r(c_S, parm_S), cosfac_S, ddphi_S);
2206 cosfac_S = gmx_simd_mul_r(cosfac_S, cos_S);
2207 c_S = gmx_simd_add_r(c_S, one_S);
2210 /* Note that here we do not use the minus sign which is present
2211 * in the normal RB code. This is corrected for through (m)sf below.
2213 ddphi_S = gmx_simd_mul_r(ddphi_S, sin_S);
2215 sf_i_S = gmx_simd_mul_r(ddphi_S, nrkj_m2_S);
2216 msf_l_S = gmx_simd_mul_r(ddphi_S, nrkj_n2_S);
2218 /* After this m?_S will contain f[i] */
2219 mx_S = gmx_simd_mul_r(sf_i_S, mx_S);
2220 my_S = gmx_simd_mul_r(sf_i_S, my_S);
2221 mz_S = gmx_simd_mul_r(sf_i_S, mz_S);
2223 /* After this m?_S will contain -f[l] */
2224 nx_S = gmx_simd_mul_r(msf_l_S, nx_S);
2225 ny_S = gmx_simd_mul_r(msf_l_S, ny_S);
2226 nz_S = gmx_simd_mul_r(msf_l_S, nz_S);
2228 gmx_simd_store_r(dr + 0*GMX_SIMD_REAL_WIDTH, mx_S);
2229 gmx_simd_store_r(dr + 1*GMX_SIMD_REAL_WIDTH, my_S);
2230 gmx_simd_store_r(dr + 2*GMX_SIMD_REAL_WIDTH, mz_S);
2231 gmx_simd_store_r(dr + 3*GMX_SIMD_REAL_WIDTH, nx_S);
2232 gmx_simd_store_r(dr + 4*GMX_SIMD_REAL_WIDTH, ny_S);
2233 gmx_simd_store_r(dr + 5*GMX_SIMD_REAL_WIDTH, nz_S);
2239 do_dih_fup_noshiftf_precalc(ai[s], aj[s], ak[s], al[s],
2241 dr[ XX *GMX_SIMD_REAL_WIDTH+s],
2242 dr[ YY *GMX_SIMD_REAL_WIDTH+s],
2243 dr[ ZZ *GMX_SIMD_REAL_WIDTH+s],
2244 dr[(DIM+XX)*GMX_SIMD_REAL_WIDTH+s],
2245 dr[(DIM+YY)*GMX_SIMD_REAL_WIDTH+s],
2246 dr[(DIM+ZZ)*GMX_SIMD_REAL_WIDTH+s],
2251 while (s < GMX_SIMD_REAL_WIDTH && iu < nbonds);
2255 #endif /* GMX_SIMD_HAVE_REAL */
2258 real idihs(int nbonds,
2259 const t_iatom forceatoms[], const t_iparams forceparams[],
2260 const rvec x[], rvec f[], rvec fshift[],
2261 const t_pbc *pbc, const t_graph *g,
2262 real lambda, real *dvdlambda,
2263 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2264 int gmx_unused *global_atom_index)
2266 int i, type, ai, aj, ak, al;
2268 real phi, phi0, dphi0, ddphi, sign, vtot;
2269 rvec r_ij, r_kj, r_kl, m, n;
2270 real L1, kk, dp, dp2, kA, kB, pA, pB, dvdl_term;
2275 for (i = 0; (i < nbonds); )
2277 type = forceatoms[i++];
2278 ai = forceatoms[i++];
2279 aj = forceatoms[i++];
2280 ak = forceatoms[i++];
2281 al = forceatoms[i++];
2283 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2284 &sign, &t1, &t2, &t3); /* 84 */
2286 /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2287 * force changes if we just apply a normal harmonic.
2288 * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2289 * This means we will never have the periodicity problem, unless
2290 * the dihedral is Pi away from phiO, which is very unlikely due to
2293 kA = forceparams[type].harmonic.krA;
2294 kB = forceparams[type].harmonic.krB;
2295 pA = forceparams[type].harmonic.rA;
2296 pB = forceparams[type].harmonic.rB;
2298 kk = L1*kA + lambda*kB;
2299 phi0 = (L1*pA + lambda*pB)*DEG2RAD;
2300 dphi0 = (pB - pA)*DEG2RAD;
2304 make_dp_periodic(&dp);
2311 dvdl_term += 0.5*(kB - kA)*dp2 - kk*dphi0*dp;
2313 do_dih_fup(ai, aj, ak, al, (real)(-ddphi), r_ij, r_kj, r_kl, m, n,
2314 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2319 fprintf(debug, "idih: (%d,%d,%d,%d) phi=%g\n",
2320 ai, aj, ak, al, phi);
2325 *dvdlambda += dvdl_term;
2330 /*! \brief returns dx, rdist, and dpdl for functions posres() and fbposres()
2332 static void posres_dx(const rvec x, const rvec pos0A, const rvec pos0B,
2333 const rvec comA_sc, const rvec comB_sc,
2335 t_pbc *pbc, int refcoord_scaling, int npbcdim,
2336 rvec dx, rvec rdist, rvec dpdl)
2339 real posA, posB, L1, ref = 0.;
2344 for (m = 0; m < DIM; m++)
2350 switch (refcoord_scaling)
2354 rdist[m] = L1*posA + lambda*posB;
2355 dpdl[m] = posB - posA;
2358 /* Box relative coordinates are stored for dimensions with pbc */
2359 posA *= pbc->box[m][m];
2360 posB *= pbc->box[m][m];
2361 assert(npbcdim <= DIM);
2362 for (d = m+1; d < npbcdim; d++)
2364 posA += pos0A[d]*pbc->box[d][m];
2365 posB += pos0B[d]*pbc->box[d][m];
2367 ref = L1*posA + lambda*posB;
2369 dpdl[m] = posB - posA;
2372 ref = L1*comA_sc[m] + lambda*comB_sc[m];
2373 rdist[m] = L1*posA + lambda*posB;
2374 dpdl[m] = comB_sc[m] - comA_sc[m] + posB - posA;
2377 gmx_fatal(FARGS, "No such scaling method implemented");
2382 ref = L1*posA + lambda*posB;
2384 dpdl[m] = posB - posA;
2387 /* We do pbc_dx with ref+rdist,
2388 * since with only ref we can be up to half a box vector wrong.
2390 pos[m] = ref + rdist[m];
2395 pbc_dx(pbc, x, pos, dx);
2399 rvec_sub(x, pos, dx);
2403 /*! \brief Adds forces of flat-bottomed positions restraints to f[]
2404 * and fixes vir_diag. Returns the flat-bottomed potential. */
2405 real fbposres(int nbonds,
2406 const t_iatom forceatoms[], const t_iparams forceparams[],
2407 const rvec x[], rvec f[], rvec vir_diag,
2409 int refcoord_scaling, int ePBC, rvec com)
2410 /* compute flat-bottomed positions restraints */
2412 int i, ai, m, d, type, npbcdim = 0, fbdim;
2413 const t_iparams *pr;
2415 real ref = 0, dr, dr2, rpot, rfb, rfb2, fact, invdr;
2416 rvec com_sc, rdist, pos, dx, dpdl, fm;
2419 npbcdim = ePBC2npbcdim(ePBC);
2421 if (refcoord_scaling == erscCOM)
2424 for (m = 0; m < npbcdim; m++)
2426 assert(npbcdim <= DIM);
2427 for (d = m; d < npbcdim; d++)
2429 com_sc[m] += com[d]*pbc->box[d][m];
2435 for (i = 0; (i < nbonds); )
2437 type = forceatoms[i++];
2438 ai = forceatoms[i++];
2439 pr = &forceparams[type];
2441 /* same calculation as for normal posres, but with identical A and B states, and lambda==0 */
2442 posres_dx(x[ai], forceparams[type].fbposres.pos0, forceparams[type].fbposres.pos0,
2443 com_sc, com_sc, 0.0,
2444 pbc, refcoord_scaling, npbcdim,
2450 kk = pr->fbposres.k;
2451 rfb = pr->fbposres.r;
2454 /* with rfb<0, push particle out of the sphere/cylinder/layer */
2462 switch (pr->fbposres.geom)
2464 case efbposresSPHERE:
2465 /* spherical flat-bottom posres */
2468 ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2472 v = 0.5*kk*sqr(dr - rfb);
2473 fact = -kk*(dr-rfb)/dr; /* Force pointing to the center pos0 */
2474 svmul(fact, dx, fm);
2477 case efbposresCYLINDER:
2478 /* cylidrical flat-bottom posres in x-y plane. fm[ZZ] = 0. */
2479 dr2 = sqr(dx[XX])+sqr(dx[YY]);
2481 ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2486 v = 0.5*kk*sqr(dr - rfb);
2487 fm[XX] = -kk*(dr-rfb)*dx[XX]*invdr; /* Force pointing to the center */
2488 fm[YY] = -kk*(dr-rfb)*dx[YY]*invdr;
2491 case efbposresX: /* fbdim=XX */
2492 case efbposresY: /* fbdim=YY */
2493 case efbposresZ: /* fbdim=ZZ */
2494 /* 1D flat-bottom potential */
2495 fbdim = pr->fbposres.geom - efbposresX;
2497 if ( ( dr > rfb && bInvert == FALSE ) || ( 0 < dr && dr < rfb && bInvert == TRUE ) )
2499 v = 0.5*kk*sqr(dr - rfb);
2500 fm[fbdim] = -kk*(dr - rfb);
2502 else if ( (dr < (-rfb) && bInvert == FALSE ) || ( (-rfb) < dr && dr < 0 && bInvert == TRUE ))
2504 v = 0.5*kk*sqr(dr + rfb);
2505 fm[fbdim] = -kk*(dr + rfb);
2512 for (m = 0; (m < DIM); m++)
2515 /* Here we correct for the pbc_dx which included rdist */
2516 vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm[m];
2524 real posres(int nbonds,
2525 const t_iatom forceatoms[], const t_iparams forceparams[],
2526 const rvec x[], rvec f[], rvec vir_diag,
2528 real lambda, real *dvdlambda,
2529 int refcoord_scaling, int ePBC, rvec comA, rvec comB)
2531 int i, ai, m, d, type, ki, npbcdim = 0;
2532 const t_iparams *pr;
2535 real posA, posB, ref = 0;
2536 rvec comA_sc, comB_sc, rdist, dpdl, pos, dx;
2537 gmx_bool bForceValid = TRUE;
2539 if ((f == NULL) || (vir_diag == NULL)) /* should both be null together! */
2541 bForceValid = FALSE;
2544 npbcdim = ePBC2npbcdim(ePBC);
2546 if (refcoord_scaling == erscCOM)
2548 clear_rvec(comA_sc);
2549 clear_rvec(comB_sc);
2550 for (m = 0; m < npbcdim; m++)
2552 assert(npbcdim <= DIM);
2553 for (d = m; d < npbcdim; d++)
2555 comA_sc[m] += comA[d]*pbc->box[d][m];
2556 comB_sc[m] += comB[d]*pbc->box[d][m];
2564 for (i = 0; (i < nbonds); )
2566 type = forceatoms[i++];
2567 ai = forceatoms[i++];
2568 pr = &forceparams[type];
2570 /* return dx, rdist, and dpdl */
2571 posres_dx(x[ai], forceparams[type].posres.pos0A, forceparams[type].posres.pos0B,
2572 comA_sc, comB_sc, lambda,
2573 pbc, refcoord_scaling, npbcdim,
2576 for (m = 0; (m < DIM); m++)
2578 kk = L1*pr->posres.fcA[m] + lambda*pr->posres.fcB[m];
2580 vtot += 0.5*kk*dx[m]*dx[m];
2582 0.5*(pr->posres.fcB[m] - pr->posres.fcA[m])*dx[m]*dx[m]
2585 /* Here we correct for the pbc_dx which included rdist */
2589 vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm;
2597 static real low_angres(int nbonds,
2598 const t_iatom forceatoms[], const t_iparams forceparams[],
2599 const rvec x[], rvec f[], rvec fshift[],
2600 const t_pbc *pbc, const t_graph *g,
2601 real lambda, real *dvdlambda,
2604 int i, m, type, ai, aj, ak, al;
2606 real phi, cos_phi, cos_phi2, vid, vtot, dVdphi;
2607 rvec r_ij, r_kl, f_i, f_k = {0, 0, 0};
2608 real st, sth, nrij2, nrkl2, c, cij, ckl;
2611 t2 = 0; /* avoid warning with gcc-3.3. It is never used uninitialized */
2614 ak = al = 0; /* to avoid warnings */
2615 for (i = 0; i < nbonds; )
2617 type = forceatoms[i++];
2618 ai = forceatoms[i++];
2619 aj = forceatoms[i++];
2620 t1 = pbc_rvec_sub(pbc, x[aj], x[ai], r_ij); /* 3 */
2623 ak = forceatoms[i++];
2624 al = forceatoms[i++];
2625 t2 = pbc_rvec_sub(pbc, x[al], x[ak], r_kl); /* 3 */
2634 cos_phi = cos_angle(r_ij, r_kl); /* 25 */
2635 phi = acos(cos_phi); /* 10 */
2637 *dvdlambda += dopdihs_min(forceparams[type].pdihs.cpA,
2638 forceparams[type].pdihs.cpB,
2639 forceparams[type].pdihs.phiA,
2640 forceparams[type].pdihs.phiB,
2641 forceparams[type].pdihs.mult,
2642 phi, lambda, &vid, &dVdphi); /* 40 */
2646 cos_phi2 = sqr(cos_phi); /* 1 */
2649 st = -dVdphi*gmx_invsqrt(1 - cos_phi2); /* 12 */
2650 sth = st*cos_phi; /* 1 */
2651 nrij2 = iprod(r_ij, r_ij); /* 5 */
2652 nrkl2 = iprod(r_kl, r_kl); /* 5 */
2654 c = st*gmx_invsqrt(nrij2*nrkl2); /* 11 */
2655 cij = sth/nrij2; /* 10 */
2656 ckl = sth/nrkl2; /* 10 */
2658 for (m = 0; m < DIM; m++) /* 18+18 */
2660 f_i[m] = (c*r_kl[m]-cij*r_ij[m]);
2665 f_k[m] = (c*r_ij[m]-ckl*r_kl[m]);
2673 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
2676 rvec_inc(fshift[t1], f_i);
2677 rvec_dec(fshift[CENTRAL], f_i);
2682 ivec_sub(SHIFT_IVEC(g, ak), SHIFT_IVEC(g, al), dt);
2685 rvec_inc(fshift[t2], f_k);
2686 rvec_dec(fshift[CENTRAL], f_k);
2691 return vtot; /* 184 / 157 (bZAxis) total */
2694 real angres(int nbonds,
2695 const t_iatom forceatoms[], const t_iparams forceparams[],
2696 const rvec x[], rvec f[], rvec fshift[],
2697 const t_pbc *pbc, const t_graph *g,
2698 real lambda, real *dvdlambda,
2699 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2700 int gmx_unused *global_atom_index)
2702 return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2703 lambda, dvdlambda, FALSE);
2706 real angresz(int nbonds,
2707 const t_iatom forceatoms[], const t_iparams forceparams[],
2708 const rvec x[], rvec f[], rvec fshift[],
2709 const t_pbc *pbc, const t_graph *g,
2710 real lambda, real *dvdlambda,
2711 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2712 int gmx_unused *global_atom_index)
2714 return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2715 lambda, dvdlambda, TRUE);
2718 real dihres(int nbonds,
2719 const t_iatom forceatoms[], const t_iparams forceparams[],
2720 const rvec x[], rvec f[], rvec fshift[],
2721 const t_pbc *pbc, const t_graph *g,
2722 real lambda, real *dvdlambda,
2723 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2724 int gmx_unused *global_atom_index)
2727 int ai, aj, ak, al, i, k, type, t1, t2, t3;
2728 real phi0A, phi0B, dphiA, dphiB, kfacA, kfacB, phi0, dphi, kfac;
2729 real phi, ddphi, ddp, ddp2, dp, sign, d2r, fc, L1;
2730 rvec r_ij, r_kj, r_kl, m, n;
2737 for (i = 0; (i < nbonds); )
2739 type = forceatoms[i++];
2740 ai = forceatoms[i++];
2741 aj = forceatoms[i++];
2742 ak = forceatoms[i++];
2743 al = forceatoms[i++];
2745 phi0A = forceparams[type].dihres.phiA*d2r;
2746 dphiA = forceparams[type].dihres.dphiA*d2r;
2747 kfacA = forceparams[type].dihres.kfacA;
2749 phi0B = forceparams[type].dihres.phiB*d2r;
2750 dphiB = forceparams[type].dihres.dphiB*d2r;
2751 kfacB = forceparams[type].dihres.kfacB;
2753 phi0 = L1*phi0A + lambda*phi0B;
2754 dphi = L1*dphiA + lambda*dphiB;
2755 kfac = L1*kfacA + lambda*kfacB;
2757 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2758 &sign, &t1, &t2, &t3);
2763 fprintf(debug, "dihres[%d]: %d %d %d %d : phi=%f, dphi=%f, kfac=%f\n",
2764 k++, ai, aj, ak, al, phi0, dphi, kfac);
2766 /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2767 * force changes if we just apply a normal harmonic.
2768 * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2769 * This means we will never have the periodicity problem, unless
2770 * the dihedral is Pi away from phiO, which is very unlikely due to
2774 make_dp_periodic(&dp);
2780 else if (dp < -dphi)
2792 vtot += 0.5*kfac*ddp2;
2795 *dvdlambda += 0.5*(kfacB - kfacA)*ddp2;
2796 /* lambda dependence from changing restraint distances */
2799 *dvdlambda -= kfac*ddp*((dphiB - dphiA)+(phi0B - phi0A));
2803 *dvdlambda += kfac*ddp*((dphiB - dphiA)-(phi0B - phi0A));
2805 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
2806 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2813 real unimplemented(int gmx_unused nbonds,
2814 const t_iatom gmx_unused forceatoms[], const t_iparams gmx_unused forceparams[],
2815 const rvec gmx_unused x[], rvec gmx_unused f[], rvec gmx_unused fshift[],
2816 const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
2817 real gmx_unused lambda, real gmx_unused *dvdlambda,
2818 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2819 int gmx_unused *global_atom_index)
2821 gmx_impl("*** you are using a not implemented function");
2823 return 0.0; /* To make the compiler happy */
2826 real restrangles(int nbonds,
2827 const t_iatom forceatoms[], const t_iparams forceparams[],
2828 const rvec x[], rvec f[], rvec fshift[],
2829 const t_pbc *pbc, const t_graph *g,
2830 real gmx_unused lambda, real gmx_unused *dvdlambda,
2831 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2832 int gmx_unused *global_atom_index)
2834 int i, d, ai, aj, ak, type, m;
2838 ivec jt, dt_ij, dt_kj;
2840 real prefactor, ratio_ante, ratio_post;
2841 rvec delta_ante, delta_post, vec_temp;
2844 for (i = 0; (i < nbonds); )
2846 type = forceatoms[i++];
2847 ai = forceatoms[i++];
2848 aj = forceatoms[i++];
2849 ak = forceatoms[i++];
2851 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2852 pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2853 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], delta_post);
2856 /* This function computes factors needed for restricted angle potential.
2857 * The restricted angle potential is used in coarse-grained simulations to avoid singularities
2858 * when three particles align and the dihedral angle and dihedral potential
2859 * cannot be calculated. This potential is calculated using the formula:
2860 real restrangles(int nbonds,
2861 const t_iatom forceatoms[],const t_iparams forceparams[],
2862 const rvec x[],rvec f[],rvec fshift[],
2863 const t_pbc *pbc,const t_graph *g,
2864 real gmx_unused lambda,real gmx_unused *dvdlambda,
2865 const t_mdatoms gmx_unused *md,t_fcdata gmx_unused *fcd,
2866 int gmx_unused *global_atom_index)
2868 int i, d, ai, aj, ak, type, m;
2872 ivec jt,dt_ij,dt_kj;
2874 real prefactor, ratio_ante, ratio_post;
2875 rvec delta_ante, delta_post, vec_temp;
2878 for(i=0; (i<nbonds); )
2880 type = forceatoms[i++];
2881 ai = forceatoms[i++];
2882 aj = forceatoms[i++];
2883 ak = forceatoms[i++];
2885 * \f[V_{\rm ReB}(\theta_i) = \frac{1}{2} k_{\theta} \frac{(\cos\theta_i - \cos\theta_0)^2}
2886 * {\sin^2\theta_i}\f] ({eq:ReB} and ref \cite{MonicaGoga2013} from the manual).
2887 * For more explanations see comments file "restcbt.h". */
2889 compute_factors_restangles(type, forceparams, delta_ante, delta_post,
2890 &prefactor, &ratio_ante, &ratio_post, &v);
2892 /* Forces are computed per component */
2893 for (d = 0; d < DIM; d++)
2895 f_i[d] = prefactor * (ratio_ante * delta_ante[d] - delta_post[d]);
2896 f_j[d] = prefactor * ((ratio_post + 1.0) * delta_post[d] - (ratio_ante + 1.0) * delta_ante[d]);
2897 f_k[d] = prefactor * (delta_ante[d] - ratio_post * delta_post[d]);
2900 /* Computation of potential energy */
2906 for (m = 0; (m < DIM); m++)
2915 copy_ivec(SHIFT_IVEC(g, aj), jt);
2916 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
2917 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
2918 t1 = IVEC2IS(dt_ij);
2919 t2 = IVEC2IS(dt_kj);
2922 rvec_inc(fshift[t1], f_i);
2923 rvec_inc(fshift[CENTRAL], f_j);
2924 rvec_inc(fshift[t2], f_k);
2930 real restrdihs(int nbonds,
2931 const t_iatom forceatoms[], const t_iparams forceparams[],
2932 const rvec x[], rvec f[], rvec fshift[],
2933 const t_pbc *pbc, const t_graph *g,
2934 real gmx_unused lambda, real gmx_unused *dvlambda,
2935 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2936 int gmx_unused *global_atom_index)
2938 int i, d, type, ai, aj, ak, al;
2939 rvec f_i, f_j, f_k, f_l;
2941 ivec jt, dt_ij, dt_kj, dt_lj;
2944 rvec delta_ante, delta_crnt, delta_post, vec_temp;
2945 real factor_phi_ai_ante, factor_phi_ai_crnt, factor_phi_ai_post;
2946 real factor_phi_aj_ante, factor_phi_aj_crnt, factor_phi_aj_post;
2947 real factor_phi_ak_ante, factor_phi_ak_crnt, factor_phi_ak_post;
2948 real factor_phi_al_ante, factor_phi_al_crnt, factor_phi_al_post;
2953 for (i = 0; (i < nbonds); )
2955 type = forceatoms[i++];
2956 ai = forceatoms[i++];
2957 aj = forceatoms[i++];
2958 ak = forceatoms[i++];
2959 al = forceatoms[i++];
2961 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2962 pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2963 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
2964 t3 = pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
2965 pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
2967 /* This function computes factors needed for restricted angle potential.
2968 * The restricted angle potential is used in coarse-grained simulations to avoid singularities
2969 * when three particles align and the dihedral angle and dihedral potential cannot be calculated.
2970 * This potential is calculated using the formula:
2971 * \f[V_{\rm ReB}(\theta_i) = \frac{1}{2} k_{\theta}
2972 * \frac{(\cos\theta_i - \cos\theta_0)^2}{\sin^2\theta_i}\f]
2973 * ({eq:ReB} and ref \cite{MonicaGoga2013} from the manual).
2974 * For more explanations see comments file "restcbt.h" */
2976 compute_factors_restrdihs(type, forceparams,
2977 delta_ante, delta_crnt, delta_post,
2978 &factor_phi_ai_ante, &factor_phi_ai_crnt, &factor_phi_ai_post,
2979 &factor_phi_aj_ante, &factor_phi_aj_crnt, &factor_phi_aj_post,
2980 &factor_phi_ak_ante, &factor_phi_ak_crnt, &factor_phi_ak_post,
2981 &factor_phi_al_ante, &factor_phi_al_crnt, &factor_phi_al_post,
2982 &prefactor_phi, &v);
2985 /* Computation of forces per component */
2986 for (d = 0; d < DIM; d++)
2988 f_i[d] = prefactor_phi * (factor_phi_ai_ante * delta_ante[d] + factor_phi_ai_crnt * delta_crnt[d] + factor_phi_ai_post * delta_post[d]);
2989 f_j[d] = prefactor_phi * (factor_phi_aj_ante * delta_ante[d] + factor_phi_aj_crnt * delta_crnt[d] + factor_phi_aj_post * delta_post[d]);
2990 f_k[d] = prefactor_phi * (factor_phi_ak_ante * delta_ante[d] + factor_phi_ak_crnt * delta_crnt[d] + factor_phi_ak_post * delta_post[d]);
2991 f_l[d] = prefactor_phi * (factor_phi_al_ante * delta_ante[d] + factor_phi_al_crnt * delta_crnt[d] + factor_phi_al_post * delta_post[d]);
2993 /* Computation of the energy */
2999 /* Updating the forces */
3001 rvec_inc(f[ai], f_i);
3002 rvec_inc(f[aj], f_j);
3003 rvec_inc(f[ak], f_k);
3004 rvec_inc(f[al], f_l);
3007 /* Updating the fshift forces for the pressure coupling */
3010 copy_ivec(SHIFT_IVEC(g, aj), jt);
3011 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3012 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3013 ivec_sub(SHIFT_IVEC(g, al), jt, dt_lj);
3014 t1 = IVEC2IS(dt_ij);
3015 t2 = IVEC2IS(dt_kj);
3016 t3 = IVEC2IS(dt_lj);
3020 t3 = pbc_rvec_sub(pbc, x[al], x[aj], dx_jl);
3027 rvec_inc(fshift[t1], f_i);
3028 rvec_inc(fshift[CENTRAL], f_j);
3029 rvec_inc(fshift[t2], f_k);
3030 rvec_inc(fshift[t3], f_l);
3038 real cbtdihs(int nbonds,
3039 const t_iatom forceatoms[], const t_iparams forceparams[],
3040 const rvec x[], rvec f[], rvec fshift[],
3041 const t_pbc *pbc, const t_graph *g,
3042 real gmx_unused lambda, real gmx_unused *dvdlambda,
3043 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3044 int gmx_unused *global_atom_index)
3046 int type, ai, aj, ak, al, i, d;
3050 rvec f_i, f_j, f_k, f_l;
3051 ivec jt, dt_ij, dt_kj, dt_lj;
3053 rvec delta_ante, delta_crnt, delta_post;
3054 rvec f_phi_ai, f_phi_aj, f_phi_ak, f_phi_al;
3055 rvec f_theta_ante_ai, f_theta_ante_aj, f_theta_ante_ak;
3056 rvec f_theta_post_aj, f_theta_post_ak, f_theta_post_al;
3062 for (i = 0; (i < nbonds); )
3064 type = forceatoms[i++];
3065 ai = forceatoms[i++];
3066 aj = forceatoms[i++];
3067 ak = forceatoms[i++];
3068 al = forceatoms[i++];
3071 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
3072 pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
3073 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], vec_temp);
3074 pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
3075 t3 = pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
3076 pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
3078 /* \brief Compute factors for CBT potential
3079 * The combined bending-torsion potential goes to zero in a very smooth manner, eliminating the numerical
3080 * instabilities, when three coarse-grained particles align and the dihedral angle and standard
3081 * dihedral potentials cannot be calculated. The CBT potential is calculated using the formula:
3082 * \f[V_{\rm CBT}(\theta_{i-1}, \theta_i, \phi_i) = k_{\phi} \sin^3\theta_{i-1} \sin^3\theta_{i}
3083 * \sum_{n=0}^4 { a_n \cos^n\phi_i}\f] ({eq:CBT} and ref \cite{MonicaGoga2013} from the manual).
3084 * It contains in its expression not only the dihedral angle \f$\phi\f$
3085 * but also \f[\theta_{i-1}\f] (theta_ante bellow) and \f[\theta_{i}\f] (theta_post bellow)
3086 * --- the adjacent bending angles.
3087 * For more explanations see comments file "restcbt.h". */
3089 compute_factors_cbtdihs(type, forceparams, delta_ante, delta_crnt, delta_post,
3090 f_phi_ai, f_phi_aj, f_phi_ak, f_phi_al,
3091 f_theta_ante_ai, f_theta_ante_aj, f_theta_ante_ak,
3092 f_theta_post_aj, f_theta_post_ak, f_theta_post_al,
3096 /* Acumulate the resuts per beads */
3097 for (d = 0; d < DIM; d++)
3099 f_i[d] = f_phi_ai[d] + f_theta_ante_ai[d];
3100 f_j[d] = f_phi_aj[d] + f_theta_ante_aj[d] + f_theta_post_aj[d];
3101 f_k[d] = f_phi_ak[d] + f_theta_ante_ak[d] + f_theta_post_ak[d];
3102 f_l[d] = f_phi_al[d] + f_theta_post_al[d];
3105 /* Compute the potential energy */
3110 /* Updating the forces */
3111 rvec_inc(f[ai], f_i);
3112 rvec_inc(f[aj], f_j);
3113 rvec_inc(f[ak], f_k);
3114 rvec_inc(f[al], f_l);
3117 /* Updating the fshift forces for the pressure coupling */
3120 copy_ivec(SHIFT_IVEC(g, aj), jt);
3121 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3122 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3123 ivec_sub(SHIFT_IVEC(g, al), jt, dt_lj);
3124 t1 = IVEC2IS(dt_ij);
3125 t2 = IVEC2IS(dt_kj);
3126 t3 = IVEC2IS(dt_lj);
3130 t3 = pbc_rvec_sub(pbc, x[al], x[aj], dx_jl);
3137 rvec_inc(fshift[t1], f_i);
3138 rvec_inc(fshift[CENTRAL], f_j);
3139 rvec_inc(fshift[t2], f_k);
3140 rvec_inc(fshift[t3], f_l);
3146 real rbdihs(int nbonds,
3147 const t_iatom forceatoms[], const t_iparams forceparams[],
3148 const rvec x[], rvec f[], rvec fshift[],
3149 const t_pbc *pbc, const t_graph *g,
3150 real lambda, real *dvdlambda,
3151 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3152 int gmx_unused *global_atom_index)
3154 const real c0 = 0.0, c1 = 1.0, c2 = 2.0, c3 = 3.0, c4 = 4.0, c5 = 5.0;
3155 int type, ai, aj, ak, al, i, j;
3157 rvec r_ij, r_kj, r_kl, m, n;
3158 real parmA[NR_RBDIHS];
3159 real parmB[NR_RBDIHS];
3160 real parm[NR_RBDIHS];
3161 real cos_phi, phi, rbp, rbpBA;
3162 real v, sign, ddphi, sin_phi;
3164 real L1 = 1.0-lambda;
3168 for (i = 0; (i < nbonds); )
3170 type = forceatoms[i++];
3171 ai = forceatoms[i++];
3172 aj = forceatoms[i++];
3173 ak = forceatoms[i++];
3174 al = forceatoms[i++];
3176 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
3177 &sign, &t1, &t2, &t3); /* 84 */
3179 /* Change to polymer convention */
3186 phi -= M_PI; /* 1 */
3190 /* Beware of accuracy loss, cannot use 1-sqrt(cos^2) ! */
3193 for (j = 0; (j < NR_RBDIHS); j++)
3195 parmA[j] = forceparams[type].rbdihs.rbcA[j];
3196 parmB[j] = forceparams[type].rbdihs.rbcB[j];
3197 parm[j] = L1*parmA[j]+lambda*parmB[j];
3199 /* Calculate cosine powers */
3200 /* Calculate the energy */
3201 /* Calculate the derivative */
3204 dvdl_term += (parmB[0]-parmA[0]);
3209 rbpBA = parmB[1]-parmA[1];
3210 ddphi += rbp*cosfac;
3213 dvdl_term += cosfac*rbpBA;
3215 rbpBA = parmB[2]-parmA[2];
3216 ddphi += c2*rbp*cosfac;
3219 dvdl_term += cosfac*rbpBA;
3221 rbpBA = parmB[3]-parmA[3];
3222 ddphi += c3*rbp*cosfac;
3225 dvdl_term += cosfac*rbpBA;
3227 rbpBA = parmB[4]-parmA[4];
3228 ddphi += c4*rbp*cosfac;
3231 dvdl_term += cosfac*rbpBA;
3233 rbpBA = parmB[5]-parmA[5];
3234 ddphi += c5*rbp*cosfac;
3237 dvdl_term += cosfac*rbpBA;
3239 ddphi = -ddphi*sin_phi; /* 11 */
3241 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
3242 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
3245 *dvdlambda += dvdl_term;
3250 int cmap_setup_grid_index(int ip, int grid_spacing, int *ipm1, int *ipp1, int *ipp2)
3256 ip = ip + grid_spacing - 1;
3258 else if (ip > grid_spacing)
3260 ip = ip - grid_spacing - 1;
3269 im1 = grid_spacing - 1;
3271 else if (ip == grid_spacing-2)
3275 else if (ip == grid_spacing-1)
3289 real cmap_dihs(int nbonds,
3290 const t_iatom forceatoms[], const t_iparams forceparams[],
3291 const gmx_cmap_t *cmap_grid,
3292 const rvec x[], rvec f[], rvec fshift[],
3293 const t_pbc *pbc, const t_graph *g,
3294 real gmx_unused lambda, real gmx_unused *dvdlambda,
3295 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3296 int gmx_unused *global_atom_index)
3298 int i, j, k, n, idx;
3299 int ai, aj, ak, al, am;
3300 int a1i, a1j, a1k, a1l, a2i, a2j, a2k, a2l;
3302 int t11, t21, t31, t12, t22, t32;
3303 int iphi1, ip1m1, ip1p1, ip1p2;
3304 int iphi2, ip2m1, ip2p1, ip2p2;
3306 int pos1, pos2, pos3, pos4, tmp;
3308 real ty[4], ty1[4], ty2[4], ty12[4], tc[16], tx[16];
3309 real phi1, psi1, cos_phi1, sin_phi1, sign1, xphi1;
3310 real phi2, psi2, cos_phi2, sin_phi2, sign2, xphi2;
3311 real dx, xx, tt, tu, e, df1, df2, ddf1, ddf2, ddf12, vtot;
3312 real ra21, rb21, rg21, rg1, rgr1, ra2r1, rb2r1, rabr1;
3313 real ra22, rb22, rg22, rg2, rgr2, ra2r2, rb2r2, rabr2;
3314 real fg1, hg1, fga1, hgb1, gaa1, gbb1;
3315 real fg2, hg2, fga2, hgb2, gaa2, gbb2;
3318 rvec r1_ij, r1_kj, r1_kl, m1, n1;
3319 rvec r2_ij, r2_kj, r2_kl, m2, n2;
3320 rvec f1_i, f1_j, f1_k, f1_l;
3321 rvec f2_i, f2_j, f2_k, f2_l;
3322 rvec a1, b1, a2, b2;
3323 rvec f1, g1, h1, f2, g2, h2;
3324 rvec dtf1, dtg1, dth1, dtf2, dtg2, dth2;
3325 ivec jt1, dt1_ij, dt1_kj, dt1_lj;
3326 ivec jt2, dt2_ij, dt2_kj, dt2_lj;
3330 int loop_index[4][4] = {
3337 /* Total CMAP energy */
3340 for (n = 0; n < nbonds; )
3342 /* Five atoms are involved in the two torsions */
3343 type = forceatoms[n++];
3344 ai = forceatoms[n++];
3345 aj = forceatoms[n++];
3346 ak = forceatoms[n++];
3347 al = forceatoms[n++];
3348 am = forceatoms[n++];
3350 /* Which CMAP type is this */
3351 cmapA = forceparams[type].cmap.cmapA;
3352 cmapd = cmap_grid->cmapdata[cmapA].cmap;
3360 phi1 = dih_angle(x[a1i], x[a1j], x[a1k], x[a1l], pbc, r1_ij, r1_kj, r1_kl, m1, n1,
3361 &sign1, &t11, &t21, &t31); /* 84 */
3363 cos_phi1 = cos(phi1);
3365 a1[0] = r1_ij[1]*r1_kj[2]-r1_ij[2]*r1_kj[1];
3366 a1[1] = r1_ij[2]*r1_kj[0]-r1_ij[0]*r1_kj[2];
3367 a1[2] = r1_ij[0]*r1_kj[1]-r1_ij[1]*r1_kj[0]; /* 9 */
3369 b1[0] = r1_kl[1]*r1_kj[2]-r1_kl[2]*r1_kj[1];
3370 b1[1] = r1_kl[2]*r1_kj[0]-r1_kl[0]*r1_kj[2];
3371 b1[2] = r1_kl[0]*r1_kj[1]-r1_kl[1]*r1_kj[0]; /* 9 */
3373 tmp = pbc_rvec_sub(pbc, x[a1l], x[a1k], h1);
3375 ra21 = iprod(a1, a1); /* 5 */
3376 rb21 = iprod(b1, b1); /* 5 */
3377 rg21 = iprod(r1_kj, r1_kj); /* 5 */
3383 rabr1 = sqrt(ra2r1*rb2r1);
3385 sin_phi1 = rg1 * rabr1 * iprod(a1, h1) * (-1);
3387 if (cos_phi1 < -0.5 || cos_phi1 > 0.5)
3389 phi1 = asin(sin_phi1);
3399 phi1 = -M_PI - phi1;
3405 phi1 = acos(cos_phi1);
3413 xphi1 = phi1 + M_PI; /* 1 */
3415 /* Second torsion */
3421 phi2 = dih_angle(x[a2i], x[a2j], x[a2k], x[a2l], pbc, r2_ij, r2_kj, r2_kl, m2, n2,
3422 &sign2, &t12, &t22, &t32); /* 84 */
3424 cos_phi2 = cos(phi2);
3426 a2[0] = r2_ij[1]*r2_kj[2]-r2_ij[2]*r2_kj[1];
3427 a2[1] = r2_ij[2]*r2_kj[0]-r2_ij[0]*r2_kj[2];
3428 a2[2] = r2_ij[0]*r2_kj[1]-r2_ij[1]*r2_kj[0]; /* 9 */
3430 b2[0] = r2_kl[1]*r2_kj[2]-r2_kl[2]*r2_kj[1];
3431 b2[1] = r2_kl[2]*r2_kj[0]-r2_kl[0]*r2_kj[2];
3432 b2[2] = r2_kl[0]*r2_kj[1]-r2_kl[1]*r2_kj[0]; /* 9 */
3434 tmp = pbc_rvec_sub(pbc, x[a2l], x[a2k], h2);
3436 ra22 = iprod(a2, a2); /* 5 */
3437 rb22 = iprod(b2, b2); /* 5 */
3438 rg22 = iprod(r2_kj, r2_kj); /* 5 */
3444 rabr2 = sqrt(ra2r2*rb2r2);
3446 sin_phi2 = rg2 * rabr2 * iprod(a2, h2) * (-1);
3448 if (cos_phi2 < -0.5 || cos_phi2 > 0.5)
3450 phi2 = asin(sin_phi2);
3460 phi2 = -M_PI - phi2;
3466 phi2 = acos(cos_phi2);
3474 xphi2 = phi2 + M_PI; /* 1 */
3476 /* Range mangling */
3479 xphi1 = xphi1 + 2*M_PI;
3481 else if (xphi1 >= 2*M_PI)
3483 xphi1 = xphi1 - 2*M_PI;
3488 xphi2 = xphi2 + 2*M_PI;
3490 else if (xphi2 >= 2*M_PI)
3492 xphi2 = xphi2 - 2*M_PI;
3495 /* Number of grid points */
3496 dx = 2*M_PI / cmap_grid->grid_spacing;
3498 /* Where on the grid are we */
3499 iphi1 = (int)(xphi1/dx);
3500 iphi2 = (int)(xphi2/dx);
3502 iphi1 = cmap_setup_grid_index(iphi1, cmap_grid->grid_spacing, &ip1m1, &ip1p1, &ip1p2);
3503 iphi2 = cmap_setup_grid_index(iphi2, cmap_grid->grid_spacing, &ip2m1, &ip2p1, &ip2p2);
3505 pos1 = iphi1*cmap_grid->grid_spacing+iphi2;
3506 pos2 = ip1p1*cmap_grid->grid_spacing+iphi2;
3507 pos3 = ip1p1*cmap_grid->grid_spacing+ip2p1;
3508 pos4 = iphi1*cmap_grid->grid_spacing+ip2p1;
3510 ty[0] = cmapd[pos1*4];
3511 ty[1] = cmapd[pos2*4];
3512 ty[2] = cmapd[pos3*4];
3513 ty[3] = cmapd[pos4*4];
3515 ty1[0] = cmapd[pos1*4+1];
3516 ty1[1] = cmapd[pos2*4+1];
3517 ty1[2] = cmapd[pos3*4+1];
3518 ty1[3] = cmapd[pos4*4+1];
3520 ty2[0] = cmapd[pos1*4+2];
3521 ty2[1] = cmapd[pos2*4+2];
3522 ty2[2] = cmapd[pos3*4+2];
3523 ty2[3] = cmapd[pos4*4+2];
3525 ty12[0] = cmapd[pos1*4+3];
3526 ty12[1] = cmapd[pos2*4+3];
3527 ty12[2] = cmapd[pos3*4+3];
3528 ty12[3] = cmapd[pos4*4+3];
3530 /* Switch to degrees */
3531 dx = 360.0 / cmap_grid->grid_spacing;
3532 xphi1 = xphi1 * RAD2DEG;
3533 xphi2 = xphi2 * RAD2DEG;
3535 for (i = 0; i < 4; i++) /* 16 */
3538 tx[i+4] = ty1[i]*dx;
3539 tx[i+8] = ty2[i]*dx;
3540 tx[i+12] = ty12[i]*dx*dx;
3544 for (i = 0; i < 4; i++) /* 1056 */
3546 for (j = 0; j < 4; j++)
3549 for (k = 0; k < 16; k++)
3551 xx = xx + cmap_coeff_matrix[k*16+idx]*tx[k];
3559 tt = (xphi1-iphi1*dx)/dx;
3560 tu = (xphi2-iphi2*dx)/dx;
3569 for (i = 3; i >= 0; i--)
3571 l1 = loop_index[i][3];
3572 l2 = loop_index[i][2];
3573 l3 = loop_index[i][1];
3575 e = tt * e + ((tc[i*4+3]*tu+tc[i*4+2])*tu + tc[i*4+1])*tu+tc[i*4];
3576 df1 = tu * df1 + (3.0*tc[l1]*tt+2.0*tc[l2])*tt+tc[l3];
3577 df2 = tt * df2 + (3.0*tc[i*4+3]*tu+2.0*tc[i*4+2])*tu+tc[i*4+1];
3578 ddf1 = tu * ddf1 + 2.0*3.0*tc[l1]*tt+2.0*tc[l2];
3579 ddf2 = tt * ddf2 + 2.0*3.0*tc[4*i+3]*tu+2.0*tc[4*i+2];
3582 ddf12 = tc[5] + 2.0*tc[9]*tt + 3.0*tc[13]*tt*tt + 2.0*tu*(tc[6]+2.0*tc[10]*tt+3.0*tc[14]*tt*tt) +
3583 3.0*tu*tu*(tc[7]+2.0*tc[11]*tt+3.0*tc[15]*tt*tt);
3588 ddf1 = ddf1 * fac * fac;
3589 ddf2 = ddf2 * fac * fac;
3590 ddf12 = ddf12 * fac * fac;
3595 /* Do forces - first torsion */
3596 fg1 = iprod(r1_ij, r1_kj);
3597 hg1 = iprod(r1_kl, r1_kj);
3598 fga1 = fg1*ra2r1*rgr1;
3599 hgb1 = hg1*rb2r1*rgr1;
3603 for (i = 0; i < DIM; i++)
3605 dtf1[i] = gaa1 * a1[i];
3606 dtg1[i] = fga1 * a1[i] - hgb1 * b1[i];
3607 dth1[i] = gbb1 * b1[i];
3609 f1[i] = df1 * dtf1[i];
3610 g1[i] = df1 * dtg1[i];
3611 h1[i] = df1 * dth1[i];
3614 f1_j[i] = -f1[i] - g1[i];
3615 f1_k[i] = h1[i] + g1[i];
3618 f[a1i][i] = f[a1i][i] + f1_i[i];
3619 f[a1j][i] = f[a1j][i] + f1_j[i]; /* - f1[i] - g1[i] */
3620 f[a1k][i] = f[a1k][i] + f1_k[i]; /* h1[i] + g1[i] */
3621 f[a1l][i] = f[a1l][i] + f1_l[i]; /* h1[i] */
3624 /* Do forces - second torsion */
3625 fg2 = iprod(r2_ij, r2_kj);
3626 hg2 = iprod(r2_kl, r2_kj);
3627 fga2 = fg2*ra2r2*rgr2;
3628 hgb2 = hg2*rb2r2*rgr2;
3632 for (i = 0; i < DIM; i++)
3634 dtf2[i] = gaa2 * a2[i];
3635 dtg2[i] = fga2 * a2[i] - hgb2 * b2[i];
3636 dth2[i] = gbb2 * b2[i];
3638 f2[i] = df2 * dtf2[i];
3639 g2[i] = df2 * dtg2[i];
3640 h2[i] = df2 * dth2[i];
3643 f2_j[i] = -f2[i] - g2[i];
3644 f2_k[i] = h2[i] + g2[i];
3647 f[a2i][i] = f[a2i][i] + f2_i[i]; /* f2[i] */
3648 f[a2j][i] = f[a2j][i] + f2_j[i]; /* - f2[i] - g2[i] */
3649 f[a2k][i] = f[a2k][i] + f2_k[i]; /* h2[i] + g2[i] */
3650 f[a2l][i] = f[a2l][i] + f2_l[i]; /* - h2[i] */
3656 copy_ivec(SHIFT_IVEC(g, a1j), jt1);
3657 ivec_sub(SHIFT_IVEC(g, a1i), jt1, dt1_ij);
3658 ivec_sub(SHIFT_IVEC(g, a1k), jt1, dt1_kj);
3659 ivec_sub(SHIFT_IVEC(g, a1l), jt1, dt1_lj);
3660 t11 = IVEC2IS(dt1_ij);
3661 t21 = IVEC2IS(dt1_kj);
3662 t31 = IVEC2IS(dt1_lj);
3664 copy_ivec(SHIFT_IVEC(g, a2j), jt2);
3665 ivec_sub(SHIFT_IVEC(g, a2i), jt2, dt2_ij);
3666 ivec_sub(SHIFT_IVEC(g, a2k), jt2, dt2_kj);
3667 ivec_sub(SHIFT_IVEC(g, a2l), jt2, dt2_lj);
3668 t12 = IVEC2IS(dt2_ij);
3669 t22 = IVEC2IS(dt2_kj);
3670 t32 = IVEC2IS(dt2_lj);
3674 t31 = pbc_rvec_sub(pbc, x[a1l], x[a1j], h1);
3675 t32 = pbc_rvec_sub(pbc, x[a2l], x[a2j], h2);
3683 rvec_inc(fshift[t11], f1_i);
3684 rvec_inc(fshift[CENTRAL], f1_j);
3685 rvec_inc(fshift[t21], f1_k);
3686 rvec_inc(fshift[t31], f1_l);
3688 rvec_inc(fshift[t21], f2_i);
3689 rvec_inc(fshift[CENTRAL], f2_j);
3690 rvec_inc(fshift[t22], f2_k);
3691 rvec_inc(fshift[t32], f2_l);
3698 /***********************************************************
3700 * G R O M O S 9 6 F U N C T I O N S
3702 ***********************************************************/
3703 real g96harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
3706 const real half = 0.5;
3707 real L1, kk, x0, dx, dx2;
3708 real v, f, dvdlambda;
3711 kk = L1*kA+lambda*kB;
3712 x0 = L1*xA+lambda*xB;
3719 dvdlambda = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
3726 /* That was 21 flops */
3729 real g96bonds(int nbonds,
3730 const t_iatom forceatoms[], const t_iparams forceparams[],
3731 const rvec x[], rvec f[], rvec fshift[],
3732 const t_pbc *pbc, const t_graph *g,
3733 real lambda, real *dvdlambda,
3734 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3735 int gmx_unused *global_atom_index)
3737 int i, m, ki, ai, aj, type;
3738 real dr2, fbond, vbond, fij, vtot;
3743 for (i = 0; (i < nbonds); )
3745 type = forceatoms[i++];
3746 ai = forceatoms[i++];
3747 aj = forceatoms[i++];
3749 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
3750 dr2 = iprod(dx, dx); /* 5 */
3752 *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3753 forceparams[type].harmonic.krB,
3754 forceparams[type].harmonic.rA,
3755 forceparams[type].harmonic.rB,
3756 dr2, lambda, &vbond, &fbond);
3758 vtot += 0.5*vbond; /* 1*/
3762 fprintf(debug, "G96-BONDS: dr = %10g vbond = %10g fbond = %10g\n",
3763 sqrt(dr2), vbond, fbond);
3769 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3772 for (m = 0; (m < DIM); m++) /* 15 */
3777 fshift[ki][m] += fij;
3778 fshift[CENTRAL][m] -= fij;
3784 real g96bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
3785 rvec r_ij, rvec r_kj,
3787 /* Return value is the angle between the bonds i-j and j-k */
3791 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
3792 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
3794 costh = cos_angle(r_ij, r_kj); /* 25 */
3799 real g96angles(int nbonds,
3800 const t_iatom forceatoms[], const t_iparams forceparams[],
3801 const rvec x[], rvec f[], rvec fshift[],
3802 const t_pbc *pbc, const t_graph *g,
3803 real lambda, real *dvdlambda,
3804 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3805 int gmx_unused *global_atom_index)
3807 int i, ai, aj, ak, type, m, t1, t2;
3809 real cos_theta, dVdt, va, vtot;
3810 real rij_1, rij_2, rkj_1, rkj_2, rijrkj_1;
3812 ivec jt, dt_ij, dt_kj;
3815 for (i = 0; (i < nbonds); )
3817 type = forceatoms[i++];
3818 ai = forceatoms[i++];
3819 aj = forceatoms[i++];
3820 ak = forceatoms[i++];
3822 cos_theta = g96bond_angle(x[ai], x[aj], x[ak], pbc, r_ij, r_kj, &t1, &t2);
3824 *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3825 forceparams[type].harmonic.krB,
3826 forceparams[type].harmonic.rA,
3827 forceparams[type].harmonic.rB,
3828 cos_theta, lambda, &va, &dVdt);
3831 rij_1 = gmx_invsqrt(iprod(r_ij, r_ij));
3832 rkj_1 = gmx_invsqrt(iprod(r_kj, r_kj));
3833 rij_2 = rij_1*rij_1;
3834 rkj_2 = rkj_1*rkj_1;
3835 rijrkj_1 = rij_1*rkj_1; /* 23 */
3840 fprintf(debug, "G96ANGLES: costheta = %10g vth = %10g dV/dct = %10g\n",
3841 cos_theta, va, dVdt);
3844 for (m = 0; (m < DIM); m++) /* 42 */
3846 f_i[m] = dVdt*(r_kj[m]*rijrkj_1 - r_ij[m]*rij_2*cos_theta);
3847 f_k[m] = dVdt*(r_ij[m]*rijrkj_1 - r_kj[m]*rkj_2*cos_theta);
3848 f_j[m] = -f_i[m]-f_k[m];
3856 copy_ivec(SHIFT_IVEC(g, aj), jt);
3858 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3859 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3860 t1 = IVEC2IS(dt_ij);
3861 t2 = IVEC2IS(dt_kj);
3863 rvec_inc(fshift[t1], f_i);
3864 rvec_inc(fshift[CENTRAL], f_j);
3865 rvec_inc(fshift[t2], f_k); /* 9 */
3871 real cross_bond_bond(int nbonds,
3872 const t_iatom forceatoms[], const t_iparams forceparams[],
3873 const rvec x[], rvec f[], rvec fshift[],
3874 const t_pbc *pbc, const t_graph *g,
3875 real gmx_unused lambda, real gmx_unused *dvdlambda,
3876 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3877 int gmx_unused *global_atom_index)
3879 /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3882 int i, ai, aj, ak, type, m, t1, t2;
3884 real vtot, vrr, s1, s2, r1, r2, r1e, r2e, krr;
3886 ivec jt, dt_ij, dt_kj;
3889 for (i = 0; (i < nbonds); )
3891 type = forceatoms[i++];
3892 ai = forceatoms[i++];
3893 aj = forceatoms[i++];
3894 ak = forceatoms[i++];
3895 r1e = forceparams[type].cross_bb.r1e;
3896 r2e = forceparams[type].cross_bb.r2e;
3897 krr = forceparams[type].cross_bb.krr;
3899 /* Compute distance vectors ... */
3900 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3901 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3903 /* ... and their lengths */
3907 /* Deviations from ideality */
3911 /* Energy (can be negative!) */
3916 svmul(-krr*s2/r1, r_ij, f_i);
3917 svmul(-krr*s1/r2, r_kj, f_k);
3919 for (m = 0; (m < DIM); m++) /* 12 */
3921 f_j[m] = -f_i[m] - f_k[m];
3930 copy_ivec(SHIFT_IVEC(g, aj), jt);
3932 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3933 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3934 t1 = IVEC2IS(dt_ij);
3935 t2 = IVEC2IS(dt_kj);
3937 rvec_inc(fshift[t1], f_i);
3938 rvec_inc(fshift[CENTRAL], f_j);
3939 rvec_inc(fshift[t2], f_k); /* 9 */
3945 real cross_bond_angle(int nbonds,
3946 const t_iatom forceatoms[], const t_iparams forceparams[],
3947 const rvec x[], rvec f[], rvec fshift[],
3948 const t_pbc *pbc, const t_graph *g,
3949 real gmx_unused lambda, real gmx_unused *dvdlambda,
3950 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3951 int gmx_unused *global_atom_index)
3953 /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3956 int i, ai, aj, ak, type, m, t1, t2, t3;
3957 rvec r_ij, r_kj, r_ik;
3958 real vtot, vrt, s1, s2, s3, r1, r2, r3, r1e, r2e, r3e, krt, k1, k2, k3;
3960 ivec jt, dt_ij, dt_kj;
3963 for (i = 0; (i < nbonds); )
3965 type = forceatoms[i++];
3966 ai = forceatoms[i++];
3967 aj = forceatoms[i++];
3968 ak = forceatoms[i++];
3969 r1e = forceparams[type].cross_ba.r1e;
3970 r2e = forceparams[type].cross_ba.r2e;
3971 r3e = forceparams[type].cross_ba.r3e;
3972 krt = forceparams[type].cross_ba.krt;
3974 /* Compute distance vectors ... */
3975 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3976 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3977 t3 = pbc_rvec_sub(pbc, x[ai], x[ak], r_ik);
3979 /* ... and their lengths */
3984 /* Deviations from ideality */
3989 /* Energy (can be negative!) */
3990 vrt = krt*s3*(s1+s2);
3996 k3 = -krt*(s1+s2)/r3;
3997 for (m = 0; (m < DIM); m++)
3999 f_i[m] = k1*r_ij[m] + k3*r_ik[m];
4000 f_k[m] = k2*r_kj[m] - k3*r_ik[m];
4001 f_j[m] = -f_i[m] - f_k[m];
4004 for (m = 0; (m < DIM); m++) /* 12 */
4014 copy_ivec(SHIFT_IVEC(g, aj), jt);
4016 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
4017 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
4018 t1 = IVEC2IS(dt_ij);
4019 t2 = IVEC2IS(dt_kj);
4021 rvec_inc(fshift[t1], f_i);
4022 rvec_inc(fshift[CENTRAL], f_j);
4023 rvec_inc(fshift[t2], f_k); /* 9 */
4029 static real bonded_tab(const char *type, int table_nr,
4030 const bondedtable_t *table, real kA, real kB, real r,
4031 real lambda, real *V, real *F)
4033 real k, tabscale, *VFtab, rt, eps, eps2, Yt, Ft, Geps, Heps2, Fp, VV, FF;
4035 real v, f, dvdlambda;
4037 k = (1.0 - lambda)*kA + lambda*kB;
4039 tabscale = table->scale;
4040 VFtab = table->data;
4046 gmx_fatal(FARGS, "A tabulated %s interaction table number %d is out of the table range: r %f, between table indices %d and %d, table length %d",
4047 type, table_nr, r, n0, n0+1, table->n);
4054 Geps = VFtab[nnn+2]*eps;
4055 Heps2 = VFtab[nnn+3]*eps2;
4056 Fp = Ft + Geps + Heps2;
4058 FF = Fp + Geps + 2.0*Heps2;
4060 *F = -k*FF*tabscale;
4062 dvdlambda = (kB - kA)*VV;
4066 /* That was 22 flops */
4069 real tab_bonds(int nbonds,
4070 const t_iatom forceatoms[], const t_iparams forceparams[],
4071 const rvec x[], rvec f[], rvec fshift[],
4072 const t_pbc *pbc, const t_graph *g,
4073 real lambda, real *dvdlambda,
4074 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
4075 int gmx_unused *global_atom_index)
4077 int i, m, ki, ai, aj, type, table;
4078 real dr, dr2, fbond, vbond, fij, vtot;
4083 for (i = 0; (i < nbonds); )
4085 type = forceatoms[i++];
4086 ai = forceatoms[i++];
4087 aj = forceatoms[i++];
4089 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
4090 dr2 = iprod(dx, dx); /* 5 */
4091 dr = dr2*gmx_invsqrt(dr2); /* 10 */
4093 table = forceparams[type].tab.table;
4095 *dvdlambda += bonded_tab("bond", table,
4096 &fcd->bondtab[table],
4097 forceparams[type].tab.kA,
4098 forceparams[type].tab.kB,
4099 dr, lambda, &vbond, &fbond); /* 22 */
4107 vtot += vbond; /* 1*/
4108 fbond *= gmx_invsqrt(dr2); /* 6 */
4112 fprintf(debug, "TABBONDS: dr = %10g vbond = %10g fbond = %10g\n",
4118 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
4121 for (m = 0; (m < DIM); m++) /* 15 */
4126 fshift[ki][m] += fij;
4127 fshift[CENTRAL][m] -= fij;
4133 real tab_angles(int nbonds,
4134 const t_iatom forceatoms[], const t_iparams forceparams[],
4135 const rvec x[], rvec f[], rvec fshift[],
4136 const t_pbc *pbc, const t_graph *g,
4137 real lambda, real *dvdlambda,
4138 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
4139 int gmx_unused *global_atom_index)
4141 int i, ai, aj, ak, t1, t2, type, table;
4143 real cos_theta, cos_theta2, theta, dVdt, va, vtot;
4144 ivec jt, dt_ij, dt_kj;
4147 for (i = 0; (i < nbonds); )
4149 type = forceatoms[i++];
4150 ai = forceatoms[i++];
4151 aj = forceatoms[i++];
4152 ak = forceatoms[i++];
4154 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
4155 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
4157 table = forceparams[type].tab.table;
4159 *dvdlambda += bonded_tab("angle", table,
4160 &fcd->angletab[table],
4161 forceparams[type].tab.kA,
4162 forceparams[type].tab.kB,
4163 theta, lambda, &va, &dVdt); /* 22 */
4166 cos_theta2 = sqr(cos_theta); /* 1 */
4175 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
4176 sth = st*cos_theta; /* 1 */
4180 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
4181 theta*RAD2DEG, va, dVdt);
4184 nrkj2 = iprod(r_kj, r_kj); /* 5 */
4185 nrij2 = iprod(r_ij, r_ij);
4187 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
4188 cii = sth/nrij2; /* 10 */
4189 ckk = sth/nrkj2; /* 10 */
4191 for (m = 0; (m < DIM); m++) /* 39 */
4193 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
4194 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
4195 f_j[m] = -f_i[m]-f_k[m];
4202 copy_ivec(SHIFT_IVEC(g, aj), jt);
4204 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
4205 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
4206 t1 = IVEC2IS(dt_ij);
4207 t2 = IVEC2IS(dt_kj);
4209 rvec_inc(fshift[t1], f_i);
4210 rvec_inc(fshift[CENTRAL], f_j);
4211 rvec_inc(fshift[t2], f_k);
4217 real tab_dihs(int nbonds,
4218 const t_iatom forceatoms[], const t_iparams forceparams[],
4219 const rvec x[], rvec f[], rvec fshift[],
4220 const t_pbc *pbc, const t_graph *g,
4221 real lambda, real *dvdlambda,
4222 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
4223 int gmx_unused *global_atom_index)
4225 int i, type, ai, aj, ak, al, table;
4227 rvec r_ij, r_kj, r_kl, m, n;
4228 real phi, sign, ddphi, vpd, vtot;
4231 for (i = 0; (i < nbonds); )
4233 type = forceatoms[i++];
4234 ai = forceatoms[i++];
4235 aj = forceatoms[i++];
4236 ak = forceatoms[i++];
4237 al = forceatoms[i++];
4239 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
4240 &sign, &t1, &t2, &t3); /* 84 */
4242 table = forceparams[type].tab.table;
4244 /* Hopefully phi+M_PI never results in values < 0 */
4245 *dvdlambda += bonded_tab("dihedral", table,
4246 &fcd->dihtab[table],
4247 forceparams[type].tab.kA,
4248 forceparams[type].tab.kB,
4249 phi+M_PI, lambda, &vpd, &ddphi);
4252 do_dih_fup(ai, aj, ak, al, -ddphi, r_ij, r_kj, r_kl, m, n,
4253 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
4256 fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
4257 ai, aj, ak, al, phi);
4264 /* Return if this is a potential calculated in bondfree.c,
4265 * i.e. an interaction that actually calculates a potential and
4266 * works on multiple atoms (not e.g. a connection or a position restraint).
4268 static gmx_inline gmx_bool ftype_is_bonded_potential(int ftype)
4271 (interaction_function[ftype].flags & IF_BOND) &&
4272 !(ftype == F_CONNBONDS || ftype == F_POSRES || ftype == F_FBPOSRES) &&
4273 (ftype < F_GB12 || ftype > F_GB14);
4276 static void divide_bondeds_over_threads(t_idef *idef, int nthreads)
4283 idef->nthreads = nthreads;
4285 if (F_NRE*(nthreads+1) > idef->il_thread_division_nalloc)
4287 idef->il_thread_division_nalloc = F_NRE*(nthreads+1);
4288 snew(idef->il_thread_division, idef->il_thread_division_nalloc);
4291 for (ftype = 0; ftype < F_NRE; ftype++)
4293 if (ftype_is_bonded_potential(ftype))
4295 nat1 = interaction_function[ftype].nratoms + 1;
4297 for (t = 0; t <= nthreads; t++)
4299 /* Divide the interactions equally over the threads.
4300 * When the different types of bonded interactions
4301 * are distributed roughly equally over the threads,
4302 * this should lead to well localized output into
4303 * the force buffer on each thread.
4304 * If this is not the case, a more advanced scheme
4305 * (not implemented yet) will do better.
4307 il_nr_thread = (((idef->il[ftype].nr/nat1)*t)/nthreads)*nat1;
4309 /* Ensure that distance restraint pairs with the same label
4310 * end up on the same thread.
4311 * This is slighlty tricky code, since the next for iteration
4312 * may have an initial il_nr_thread lower than the final value
4313 * in the previous iteration, but this will anyhow be increased
4314 * to the approriate value again by this while loop.
4316 while (ftype == F_DISRES &&
4318 il_nr_thread < idef->il[ftype].nr &&
4319 idef->iparams[idef->il[ftype].iatoms[il_nr_thread]].disres.label ==
4320 idef->iparams[idef->il[ftype].iatoms[il_nr_thread-nat1]].disres.label)
4322 il_nr_thread += nat1;
4325 idef->il_thread_division[ftype*(nthreads+1)+t] = il_nr_thread;
4332 calc_bonded_reduction_mask(const t_idef *idef,
4337 int ftype, nb, nat1, nb0, nb1, i, a;
4341 for (ftype = 0; ftype < F_NRE; ftype++)
4343 if (ftype_is_bonded_potential(ftype))
4345 nb = idef->il[ftype].nr;
4348 nat1 = interaction_function[ftype].nratoms + 1;
4350 /* Divide this interaction equally over the threads.
4351 * This is not stored: should match division in calc_bonds.
4353 nb0 = idef->il_thread_division[ftype*(nt+1)+t];
4354 nb1 = idef->il_thread_division[ftype*(nt+1)+t+1];
4356 for (i = nb0; i < nb1; i += nat1)
4358 for (a = 1; a < nat1; a++)
4360 mask |= (1U << (idef->il[ftype].iatoms[i+a]>>shift));
4370 void setup_bonded_threading(t_forcerec *fr, t_idef *idef)
4372 #define MAX_BLOCK_BITS 32
4376 assert(fr->nthreads >= 1);
4378 /* Divide the bonded interaction over the threads */
4379 divide_bondeds_over_threads(idef, fr->nthreads);
4381 if (fr->nthreads == 1)
4388 /* We divide the force array in a maximum of 32 blocks.
4389 * Minimum force block reduction size is 2^6=64.
4392 while (fr->natoms_force > (int)(MAX_BLOCK_BITS*(1U<<fr->red_ashift)))
4398 fprintf(debug, "bonded force buffer block atom shift %d bits\n",
4402 /* Determine to which blocks each thread's bonded force calculation
4403 * contributes. Store this is a mask for each thread.
4405 #pragma omp parallel for num_threads(fr->nthreads) schedule(static)
4406 for (t = 1; t < fr->nthreads; t++)
4408 fr->f_t[t].red_mask =
4409 calc_bonded_reduction_mask(idef, fr->red_ashift, t, fr->nthreads);
4412 /* Determine the maximum number of blocks we need to reduce over */
4415 for (t = 0; t < fr->nthreads; t++)
4418 for (b = 0; b < MAX_BLOCK_BITS; b++)
4420 if (fr->f_t[t].red_mask & (1U<<b))
4422 fr->red_nblock = max(fr->red_nblock, b+1);
4428 fprintf(debug, "thread %d flags %x count %d\n",
4429 t, fr->f_t[t].red_mask, c);
4435 fprintf(debug, "Number of blocks to reduce: %d of size %d\n",
4436 fr->red_nblock, 1<<fr->red_ashift);
4437 fprintf(debug, "Reduction density %.2f density/#thread %.2f\n",
4438 ctot*(1<<fr->red_ashift)/(double)fr->natoms_force,
4439 ctot*(1<<fr->red_ashift)/(double)(fr->natoms_force*fr->nthreads));
4443 static void zero_thread_forces(f_thread_t *f_t, int n,
4444 int nblock, int blocksize)
4446 int b, a0, a1, a, i, j;
4448 if (n > f_t->f_nalloc)
4450 f_t->f_nalloc = over_alloc_large(n);
4451 srenew(f_t->f, f_t->f_nalloc);
4454 if (f_t->red_mask != 0)
4456 for (b = 0; b < nblock; b++)
4458 if (f_t->red_mask && (1U<<b))
4461 a1 = min((b+1)*blocksize, n);
4462 for (a = a0; a < a1; a++)
4464 clear_rvec(f_t->f[a]);
4469 for (i = 0; i < SHIFTS; i++)
4471 clear_rvec(f_t->fshift[i]);
4473 for (i = 0; i < F_NRE; i++)
4477 for (i = 0; i < egNR; i++)
4479 for (j = 0; j < f_t->grpp.nener; j++)
4481 f_t->grpp.ener[i][j] = 0;
4484 for (i = 0; i < efptNR; i++)
4490 static void reduce_thread_force_buffer(int n, rvec *f,
4491 int nthreads, f_thread_t *f_t,
4492 int nblock, int block_size)
4494 /* The max thread number is arbitrary,
4495 * we used a fixed number to avoid memory management.
4496 * Using more than 16 threads is probably never useful performance wise.
4498 #define MAX_BONDED_THREADS 256
4501 if (nthreads > MAX_BONDED_THREADS)
4503 gmx_fatal(FARGS, "Can not reduce bonded forces on more than %d threads",
4504 MAX_BONDED_THREADS);
4507 /* This reduction can run on any number of threads,
4508 * independently of nthreads.
4510 #pragma omp parallel for num_threads(nthreads) schedule(static)
4511 for (b = 0; b < nblock; b++)
4513 rvec *fp[MAX_BONDED_THREADS];
4517 /* Determine which threads contribute to this block */
4519 for (ft = 1; ft < nthreads; ft++)
4521 if (f_t[ft].red_mask & (1U<<b))
4523 fp[nfb++] = f_t[ft].f;
4528 /* Reduce force buffers for threads that contribute */
4530 a1 = (b+1)*block_size;
4532 for (a = a0; a < a1; a++)
4534 for (fb = 0; fb < nfb; fb++)
4536 rvec_inc(f[a], fp[fb][a]);
4543 static void reduce_thread_forces(int n, rvec *f, rvec *fshift,
4544 real *ener, gmx_grppairener_t *grpp, real *dvdl,
4545 int nthreads, f_thread_t *f_t,
4546 int nblock, int block_size,
4547 gmx_bool bCalcEnerVir,
4552 /* Reduce the bonded force buffer */
4553 reduce_thread_force_buffer(n, f, nthreads, f_t, nblock, block_size);
4556 /* When necessary, reduce energy and virial using one thread only */
4561 for (i = 0; i < SHIFTS; i++)
4563 for (t = 1; t < nthreads; t++)
4565 rvec_inc(fshift[i], f_t[t].fshift[i]);
4568 for (i = 0; i < F_NRE; i++)
4570 for (t = 1; t < nthreads; t++)
4572 ener[i] += f_t[t].ener[i];
4575 for (i = 0; i < egNR; i++)
4577 for (j = 0; j < f_t[1].grpp.nener; j++)
4579 for (t = 1; t < nthreads; t++)
4582 grpp->ener[i][j] += f_t[t].grpp.ener[i][j];
4588 for (i = 0; i < efptNR; i++)
4591 for (t = 1; t < nthreads; t++)
4593 dvdl[i] += f_t[t].dvdl[i];
4600 static real calc_one_bond(FILE *fplog, int thread,
4601 int ftype, const t_idef *idef,
4602 rvec x[], rvec f[], rvec fshift[],
4604 const t_pbc *pbc, const t_graph *g,
4605 gmx_grppairener_t *grpp,
4607 real *lambda, real *dvdl,
4608 const t_mdatoms *md, t_fcdata *fcd,
4609 gmx_bool bCalcEnerVir,
4610 int *global_atom_index, gmx_bool bPrintSepPot)
4612 int nat1, nbonds, efptFTYPE;
4617 if (IS_RESTRAINT_TYPE(ftype))
4619 efptFTYPE = efptRESTRAINT;
4623 efptFTYPE = efptBONDED;
4626 nat1 = interaction_function[ftype].nratoms + 1;
4627 nbonds = idef->il[ftype].nr/nat1;
4628 iatoms = idef->il[ftype].iatoms;
4630 nb0 = idef->il_thread_division[ftype*(idef->nthreads+1)+thread];
4631 nbn = idef->il_thread_division[ftype*(idef->nthreads+1)+thread+1] - nb0;
4633 if (!IS_LISTED_LJ_C(ftype))
4635 if (ftype == F_CMAP)
4637 v = cmap_dihs(nbn, iatoms+nb0,
4638 idef->iparams, &idef->cmap_grid,
4639 (const rvec*)x, f, fshift,
4640 pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4641 md, fcd, global_atom_index);
4643 #ifdef GMX_SIMD_HAVE_REAL
4644 else if (ftype == F_ANGLES &&
4645 !bCalcEnerVir && fr->efep == efepNO)
4647 /* No energies, shift forces, dvdl */
4648 angles_noener_simd(nbn, idef->il[ftype].iatoms+nb0,
4651 pbc, g, lambda[efptFTYPE], md, fcd,
4656 else if (ftype == F_PDIHS &&
4657 !bCalcEnerVir && fr->efep == efepNO)
4659 /* No energies, shift forces, dvdl */
4660 #ifdef GMX_SIMD_HAVE_REAL
4665 (nbn, idef->il[ftype].iatoms+nb0,
4668 pbc, g, lambda[efptFTYPE], md, fcd,
4672 #ifdef GMX_SIMD_HAVE_REAL
4673 else if (ftype == F_RBDIHS &&
4674 !bCalcEnerVir && fr->efep == efepNO)
4676 /* No energies, shift forces, dvdl */
4677 rbdihs_noener_simd(nbn, idef->il[ftype].iatoms+nb0,
4680 pbc, g, lambda[efptFTYPE], md, fcd,
4687 v = interaction_function[ftype].ifunc(nbn, iatoms+nb0,
4689 (const rvec*)x, f, fshift,
4690 pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4691 md, fcd, global_atom_index);
4695 fprintf(fplog, " %-23s #%4d V %12.5e dVdl %12.5e\n",
4696 interaction_function[ftype].longname,
4697 nbonds, v, lambda[efptFTYPE]);
4702 v = do_nonbonded_listed(ftype, nbn, iatoms+nb0, idef->iparams, (const rvec*)x, f, fshift,
4703 pbc, g, lambda, dvdl, md, fr, grpp, global_atom_index);
4707 fprintf(fplog, " %-5s + %-15s #%4d dVdl %12.5e\n",
4708 interaction_function[ftype].longname,
4709 interaction_function[F_LJ14].longname, nbonds, dvdl[efptVDW]);
4710 fprintf(fplog, " %-5s + %-15s #%4d dVdl %12.5e\n",
4711 interaction_function[ftype].longname,
4712 interaction_function[F_COUL14].longname, nbonds, dvdl[efptCOUL]);
4718 inc_nrnb(nrnb, interaction_function[ftype].nrnb_ind, nbonds);
4724 void calc_bonds(FILE *fplog, const gmx_multisim_t *ms,
4726 rvec x[], history_t *hist,
4727 rvec f[], t_forcerec *fr,
4728 const t_pbc *pbc, const t_graph *g,
4729 gmx_enerdata_t *enerd, t_nrnb *nrnb,
4731 const t_mdatoms *md,
4732 t_fcdata *fcd, int *global_atom_index,
4733 t_atomtypes gmx_unused *atype, gmx_genborn_t gmx_unused *born,
4735 gmx_bool bPrintSepPot, gmx_int64_t step)
4737 gmx_bool bCalcEnerVir;
4739 real v, dvdl[efptNR], dvdl_dum[efptNR]; /* The dummy array is to have a place to store the dhdl at other values
4740 of lambda, which will be thrown away in the end*/
4741 const t_pbc *pbc_null;
4745 assert(fr->nthreads == idef->nthreads);
4747 bCalcEnerVir = (force_flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY));
4749 for (i = 0; i < efptNR; i++)
4763 fprintf(fplog, "Step %s: bonded V and dVdl for this rank\n",
4764 gmx_step_str(step, buf));
4770 p_graph(debug, "Bondage is fun", g);
4774 /* Do pre force calculation stuff which might require communication */
4775 if (idef->il[F_ORIRES].nr)
4777 enerd->term[F_ORIRESDEV] =
4778 calc_orires_dev(ms, idef->il[F_ORIRES].nr,
4779 idef->il[F_ORIRES].iatoms,
4780 idef->iparams, md, (const rvec*)x,
4781 pbc_null, fcd, hist);
4783 if (idef->il[F_DISRES].nr)
4785 calc_disres_R_6(idef->il[F_DISRES].nr,
4786 idef->il[F_DISRES].iatoms,
4787 idef->iparams, (const rvec*)x, pbc_null,
4790 if (fcd->disres.nsystems > 1)
4792 gmx_sum_sim(2*fcd->disres.nres, fcd->disres.Rt_6, ms);
4797 #pragma omp parallel for num_threads(fr->nthreads) schedule(static)
4798 for (thread = 0; thread < fr->nthreads; thread++)
4805 gmx_grppairener_t *grpp;
4810 fshift = fr->fshift;
4812 grpp = &enerd->grpp;
4817 zero_thread_forces(&fr->f_t[thread], fr->natoms_force,
4818 fr->red_nblock, 1<<fr->red_ashift);
4820 ft = fr->f_t[thread].f;
4821 fshift = fr->f_t[thread].fshift;
4822 epot = fr->f_t[thread].ener;
4823 grpp = &fr->f_t[thread].grpp;
4824 dvdlt = fr->f_t[thread].dvdl;
4826 /* Loop over all bonded force types to calculate the bonded forces */
4827 for (ftype = 0; (ftype < F_NRE); ftype++)
4829 if (idef->il[ftype].nr > 0 && ftype_is_bonded_potential(ftype))
4831 v = calc_one_bond(fplog, thread, ftype, idef, x,
4832 ft, fshift, fr, pbc_null, g, grpp,
4833 nrnb, lambda, dvdlt,
4834 md, fcd, bCalcEnerVir,
4835 global_atom_index, bPrintSepPot);
4840 if (fr->nthreads > 1)
4842 reduce_thread_forces(fr->natoms_force, f, fr->fshift,
4843 enerd->term, &enerd->grpp, dvdl,
4844 fr->nthreads, fr->f_t,
4845 fr->red_nblock, 1<<fr->red_ashift,
4847 force_flags & GMX_FORCE_DHDL);
4849 if (force_flags & GMX_FORCE_DHDL)
4851 for (i = 0; i < efptNR; i++)
4853 enerd->dvdl_nonlin[i] += dvdl[i];
4857 /* Copy the sum of violations for the distance restraints from fcd */
4860 enerd->term[F_DISRESVIOL] = fcd->disres.sumviol;
4865 void calc_bonds_lambda(FILE *fplog,
4869 const t_pbc *pbc, const t_graph *g,
4870 gmx_grppairener_t *grpp, real *epot, t_nrnb *nrnb,
4872 const t_mdatoms *md,
4874 int *global_atom_index)
4876 int i, ftype, nr_nonperturbed, nr;
4878 real dvdl_dum[efptNR];
4880 const t_pbc *pbc_null;
4892 /* Copy the whole idef, so we can modify the contents locally */
4894 idef_fe.nthreads = 1;
4895 snew(idef_fe.il_thread_division, F_NRE*(idef_fe.nthreads+1));
4897 /* We already have the forces, so we use temp buffers here */
4898 snew(f, fr->natoms_force);
4899 snew(fshift, SHIFTS);
4901 /* Loop over all bonded force types to calculate the bonded energies */
4902 for (ftype = 0; (ftype < F_NRE); ftype++)
4904 if (ftype_is_bonded_potential(ftype))
4906 /* Set the work range of thread 0 to the perturbed bondeds only */
4907 nr_nonperturbed = idef->il[ftype].nr_nonperturbed;
4908 nr = idef->il[ftype].nr;
4909 idef_fe.il_thread_division[ftype*2+0] = nr_nonperturbed;
4910 idef_fe.il_thread_division[ftype*2+1] = nr;
4912 /* This is only to get the flop count correct */
4913 idef_fe.il[ftype].nr = nr - nr_nonperturbed;
4915 if (nr - nr_nonperturbed > 0)
4917 v = calc_one_bond(fplog, 0, ftype, &idef_fe,
4918 x, f, fshift, fr, pbc_null, g,
4919 grpp, nrnb, lambda, dvdl_dum,
4921 global_atom_index, FALSE);
4930 sfree(idef_fe.il_thread_division);