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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
53 #include "gmx_fatal.h"
59 #include "nonbonded.h"
61 /* Include the SIMD macro file and then check for support */
62 #include "gmx_simd_macros.h"
63 #if defined GMX_HAVE_SIMD_MACROS && defined GMX_SIMD_HAVE_TRIGONOMETRIC
65 #include "gmx_simd_vec.h"
68 /* Find a better place for this? */
69 const int cmap_coeff_matrix[] = {
70 1, 0, -3, 2, 0, 0, 0, 0, -3, 0, 9, -6, 2, 0, -6, 4,
71 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, -9, 6, -2, 0, 6, -4,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, -6, 0, 0, -6, 4,
73 0, 0, 3, -2, 0, 0, 0, 0, 0, 0, -9, 6, 0, 0, 6, -4,
74 0, 0, 0, 0, 1, 0, -3, 2, -2, 0, 6, -4, 1, 0, -3, 2,
75 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 3, -2, 1, 0, -3, 2,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 2, 0, 0, 3, -2,
77 0, 0, 0, 0, 0, 0, 3, -2, 0, 0, -6, 4, 0, 0, 3, -2,
78 0, 1, -2, 1, 0, 0, 0, 0, 0, -3, 6, -3, 0, 2, -4, 2,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, -6, 3, 0, -2, 4, -2,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 3, 0, 0, 2, -2,
81 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 3, -3, 0, 0, -2, 2,
82 0, 0, 0, 0, 0, 1, -2, 1, 0, -2, 4, -2, 0, 1, -2, 1,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 2, -1, 0, 1, -2, 1,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, -1, 1,
85 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 2, -2, 0, 0, -1, 1
90 int glatnr(int *global_atom_index, int i)
94 if (global_atom_index == NULL)
100 atnr = global_atom_index[i] + 1;
106 static int pbc_rvec_sub(const t_pbc *pbc, const rvec xi, const rvec xj, rvec dx)
110 return pbc_dx_aiuc(pbc, xi, xj, dx);
114 rvec_sub(xi, xj, dx);
121 /* SIMD PBC data structure, containing 1/boxdiag and the box vectors */
134 /* Set the SIMD pbc data from a normal t_pbc struct */
135 static void set_pbc_simd(const t_pbc *pbc, pbc_simd_t *pbc_simd)
140 /* Setting inv_bdiag to 0 effectively turns off PBC */
141 clear_rvec(inv_bdiag);
144 for (d = 0; d < pbc->ndim_ePBC; d++)
146 inv_bdiag[d] = 1.0/pbc->box[d][d];
150 pbc_simd->inv_bzz = gmx_set1_pr(inv_bdiag[ZZ]);
151 pbc_simd->inv_byy = gmx_set1_pr(inv_bdiag[YY]);
152 pbc_simd->inv_bxx = gmx_set1_pr(inv_bdiag[XX]);
156 pbc_simd->bzx = gmx_set1_pr(pbc->box[ZZ][XX]);
157 pbc_simd->bzy = gmx_set1_pr(pbc->box[ZZ][YY]);
158 pbc_simd->bzz = gmx_set1_pr(pbc->box[ZZ][ZZ]);
159 pbc_simd->byx = gmx_set1_pr(pbc->box[YY][XX]);
160 pbc_simd->byy = gmx_set1_pr(pbc->box[YY][YY]);
161 pbc_simd->bxx = gmx_set1_pr(pbc->box[XX][XX]);
165 pbc_simd->bzx = gmx_setzero_pr();
166 pbc_simd->bzy = gmx_setzero_pr();
167 pbc_simd->bzz = gmx_setzero_pr();
168 pbc_simd->byx = gmx_setzero_pr();
169 pbc_simd->byy = gmx_setzero_pr();
170 pbc_simd->bxx = gmx_setzero_pr();
174 /* Correct distance vector *dx,*dy,*dz for PBC using SIMD */
175 static gmx_inline void
176 pbc_dx_simd(gmx_mm_pr *dx, gmx_mm_pr *dy, gmx_mm_pr *dz,
177 const pbc_simd_t *pbc)
181 sh = gmx_round_pr(gmx_mul_pr(*dz, pbc->inv_bzz));
182 *dx = gmx_nmsub_pr(sh, pbc->bzx, *dx);
183 *dy = gmx_nmsub_pr(sh, pbc->bzy, *dy);
184 *dz = gmx_nmsub_pr(sh, pbc->bzz, *dz);
186 sh = gmx_round_pr(gmx_mul_pr(*dy, pbc->inv_byy));
187 *dx = gmx_nmsub_pr(sh, pbc->byx, *dx);
188 *dy = gmx_nmsub_pr(sh, pbc->byy, *dy);
190 sh = gmx_round_pr(gmx_mul_pr(*dx, pbc->inv_bxx));
191 *dx = gmx_nmsub_pr(sh, pbc->bxx, *dx);
194 #endif /* SIMD_BONDEDS */
197 * Morse potential bond by Frank Everdij
199 * Three parameters needed:
201 * b0 = equilibrium distance in nm
202 * be = beta in nm^-1 (actually, it's nu_e*Sqrt(2*pi*pi*mu/D_e))
203 * cb = well depth in kJ/mol
205 * Note: the potential is referenced to be +cb at infinite separation
206 * and zero at the equilibrium distance!
209 real morse_bonds(int nbonds,
210 const t_iatom forceatoms[], const t_iparams forceparams[],
211 const rvec x[], rvec f[], rvec fshift[],
212 const t_pbc *pbc, const t_graph *g,
213 real lambda, real *dvdlambda,
214 const t_mdatoms *md, t_fcdata *fcd,
215 int *global_atom_index)
217 const real one = 1.0;
218 const real two = 2.0;
219 real dr, dr2, temp, omtemp, cbomtemp, fbond, vbond, fij, vtot;
220 real b0, be, cb, b0A, beA, cbA, b0B, beB, cbB, L1;
222 int i, m, ki, type, ai, aj;
226 for (i = 0; (i < nbonds); )
228 type = forceatoms[i++];
229 ai = forceatoms[i++];
230 aj = forceatoms[i++];
232 b0A = forceparams[type].morse.b0A;
233 beA = forceparams[type].morse.betaA;
234 cbA = forceparams[type].morse.cbA;
236 b0B = forceparams[type].morse.b0B;
237 beB = forceparams[type].morse.betaB;
238 cbB = forceparams[type].morse.cbB;
240 L1 = one-lambda; /* 1 */
241 b0 = L1*b0A + lambda*b0B; /* 3 */
242 be = L1*beA + lambda*beB; /* 3 */
243 cb = L1*cbA + lambda*cbB; /* 3 */
245 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
246 dr2 = iprod(dx, dx); /* 5 */
247 dr = dr2*gmx_invsqrt(dr2); /* 10 */
248 temp = exp(-be*(dr-b0)); /* 12 */
252 /* bonds are constrainted. This may _not_ include bond constraints if they are lambda dependent */
253 *dvdlambda += cbB-cbA;
257 omtemp = one-temp; /* 1 */
258 cbomtemp = cb*omtemp; /* 1 */
259 vbond = cbomtemp*omtemp; /* 1 */
260 fbond = -two*be*temp*cbomtemp*gmx_invsqrt(dr2); /* 9 */
261 vtot += vbond; /* 1 */
263 *dvdlambda += (cbB - cbA) * omtemp * omtemp - (2-2*omtemp)*omtemp * cb * ((b0B-b0A)*be - (beB-beA)*(dr-b0)); /* 15 */
267 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
271 for (m = 0; (m < DIM); m++) /* 15 */
276 fshift[ki][m] += fij;
277 fshift[CENTRAL][m] -= fij;
283 real cubic_bonds(int nbonds,
284 const t_iatom forceatoms[], const t_iparams forceparams[],
285 const rvec x[], rvec f[], rvec fshift[],
286 const t_pbc *pbc, const t_graph *g,
287 real lambda, real *dvdlambda,
288 const t_mdatoms *md, t_fcdata *fcd,
289 int *global_atom_index)
291 const real three = 3.0;
292 const real two = 2.0;
294 real dr, dr2, dist, kdist, kdist2, fbond, vbond, fij, vtot;
296 int i, m, ki, type, ai, aj;
300 for (i = 0; (i < nbonds); )
302 type = forceatoms[i++];
303 ai = forceatoms[i++];
304 aj = forceatoms[i++];
306 b0 = forceparams[type].cubic.b0;
307 kb = forceparams[type].cubic.kb;
308 kcub = forceparams[type].cubic.kcub;
310 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
311 dr2 = iprod(dx, dx); /* 5 */
318 dr = dr2*gmx_invsqrt(dr2); /* 10 */
323 vbond = kdist2 + kcub*kdist2*dist;
324 fbond = -(two*kdist + three*kdist2*kcub)/dr;
326 vtot += vbond; /* 21 */
330 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
333 for (m = 0; (m < DIM); m++) /* 15 */
338 fshift[ki][m] += fij;
339 fshift[CENTRAL][m] -= fij;
345 real FENE_bonds(int nbonds,
346 const t_iatom forceatoms[], const t_iparams forceparams[],
347 const rvec x[], rvec f[], rvec fshift[],
348 const t_pbc *pbc, const t_graph *g,
349 real lambda, real *dvdlambda,
350 const t_mdatoms *md, t_fcdata *fcd,
351 int *global_atom_index)
353 const real half = 0.5;
354 const real one = 1.0;
356 real dr, dr2, bm2, omdr2obm2, fbond, vbond, fij, vtot;
358 int i, m, ki, type, ai, aj;
362 for (i = 0; (i < nbonds); )
364 type = forceatoms[i++];
365 ai = forceatoms[i++];
366 aj = forceatoms[i++];
368 bm = forceparams[type].fene.bm;
369 kb = forceparams[type].fene.kb;
371 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
372 dr2 = iprod(dx, dx); /* 5 */
384 "r^2 (%f) >= bm^2 (%f) in FENE bond between atoms %d and %d",
386 glatnr(global_atom_index, ai),
387 glatnr(global_atom_index, aj));
390 omdr2obm2 = one - dr2/bm2;
392 vbond = -half*kb*bm2*log(omdr2obm2);
393 fbond = -kb/omdr2obm2;
395 vtot += vbond; /* 35 */
399 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
402 for (m = 0; (m < DIM); m++) /* 15 */
407 fshift[ki][m] += fij;
408 fshift[CENTRAL][m] -= fij;
414 real harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
417 const real half = 0.5;
418 real L1, kk, x0, dx, dx2;
419 real v, f, dvdlambda;
422 kk = L1*kA+lambda*kB;
423 x0 = L1*xA+lambda*xB;
430 dvdlambda = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
437 /* That was 19 flops */
441 real bonds(int nbonds,
442 const t_iatom forceatoms[], const t_iparams forceparams[],
443 const rvec x[], rvec f[], rvec fshift[],
444 const t_pbc *pbc, const t_graph *g,
445 real lambda, real *dvdlambda,
446 const t_mdatoms *md, t_fcdata *fcd,
447 int *global_atom_index)
449 int i, m, ki, ai, aj, type;
450 real dr, dr2, fbond, vbond, fij, vtot;
455 for (i = 0; (i < nbonds); )
457 type = forceatoms[i++];
458 ai = forceatoms[i++];
459 aj = forceatoms[i++];
461 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
462 dr2 = iprod(dx, dx); /* 5 */
463 dr = dr2*gmx_invsqrt(dr2); /* 10 */
465 *dvdlambda += harmonic(forceparams[type].harmonic.krA,
466 forceparams[type].harmonic.krB,
467 forceparams[type].harmonic.rA,
468 forceparams[type].harmonic.rB,
469 dr, lambda, &vbond, &fbond); /* 19 */
477 vtot += vbond; /* 1*/
478 fbond *= gmx_invsqrt(dr2); /* 6 */
482 fprintf(debug, "BONDS: dr = %10g vbond = %10g fbond = %10g\n",
488 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
491 for (m = 0; (m < DIM); m++) /* 15 */
496 fshift[ki][m] += fij;
497 fshift[CENTRAL][m] -= fij;
503 real restraint_bonds(int nbonds,
504 const t_iatom forceatoms[], const t_iparams forceparams[],
505 const rvec x[], rvec f[], rvec fshift[],
506 const t_pbc *pbc, const t_graph *g,
507 real lambda, real *dvdlambda,
508 const t_mdatoms *md, t_fcdata *fcd,
509 int *global_atom_index)
511 int i, m, ki, ai, aj, type;
512 real dr, dr2, fbond, vbond, fij, vtot;
514 real low, dlow, up1, dup1, up2, dup2, k, dk;
522 for (i = 0; (i < nbonds); )
524 type = forceatoms[i++];
525 ai = forceatoms[i++];
526 aj = forceatoms[i++];
528 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
529 dr2 = iprod(dx, dx); /* 5 */
530 dr = dr2*gmx_invsqrt(dr2); /* 10 */
532 low = L1*forceparams[type].restraint.lowA + lambda*forceparams[type].restraint.lowB;
533 dlow = -forceparams[type].restraint.lowA + forceparams[type].restraint.lowB;
534 up1 = L1*forceparams[type].restraint.up1A + lambda*forceparams[type].restraint.up1B;
535 dup1 = -forceparams[type].restraint.up1A + forceparams[type].restraint.up1B;
536 up2 = L1*forceparams[type].restraint.up2A + lambda*forceparams[type].restraint.up2B;
537 dup2 = -forceparams[type].restraint.up2A + forceparams[type].restraint.up2B;
538 k = L1*forceparams[type].restraint.kA + lambda*forceparams[type].restraint.kB;
539 dk = -forceparams[type].restraint.kA + forceparams[type].restraint.kB;
548 *dvdlambda += 0.5*dk*drh2 - k*dlow*drh;
561 *dvdlambda += 0.5*dk*drh2 - k*dup1*drh;
566 vbond = k*(up2 - up1)*(0.5*(up2 - up1) + drh);
567 fbond = -k*(up2 - up1);
568 *dvdlambda += dk*(up2 - up1)*(0.5*(up2 - up1) + drh)
569 + k*(dup2 - dup1)*(up2 - up1 + drh)
570 - k*(up2 - up1)*dup2;
578 vtot += vbond; /* 1*/
579 fbond *= gmx_invsqrt(dr2); /* 6 */
583 fprintf(debug, "BONDS: dr = %10g vbond = %10g fbond = %10g\n",
589 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
592 for (m = 0; (m < DIM); m++) /* 15 */
597 fshift[ki][m] += fij;
598 fshift[CENTRAL][m] -= fij;
605 real polarize(int nbonds,
606 const t_iatom forceatoms[], const t_iparams forceparams[],
607 const rvec x[], rvec f[], rvec fshift[],
608 const t_pbc *pbc, const t_graph *g,
609 real lambda, real *dvdlambda,
610 const t_mdatoms *md, t_fcdata *fcd,
611 int *global_atom_index)
613 int i, m, ki, ai, aj, type;
614 real dr, dr2, fbond, vbond, fij, vtot, ksh;
619 for (i = 0; (i < nbonds); )
621 type = forceatoms[i++];
622 ai = forceatoms[i++];
623 aj = forceatoms[i++];
624 ksh = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].polarize.alpha;
627 fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
630 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
631 dr2 = iprod(dx, dx); /* 5 */
632 dr = dr2*gmx_invsqrt(dr2); /* 10 */
634 *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /* 19 */
641 vtot += vbond; /* 1*/
642 fbond *= gmx_invsqrt(dr2); /* 6 */
646 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
649 for (m = 0; (m < DIM); m++) /* 15 */
654 fshift[ki][m] += fij;
655 fshift[CENTRAL][m] -= fij;
661 real anharm_polarize(int nbonds,
662 const t_iatom forceatoms[], const t_iparams forceparams[],
663 const rvec x[], rvec f[], rvec fshift[],
664 const t_pbc *pbc, const t_graph *g,
665 real lambda, real *dvdlambda,
666 const t_mdatoms *md, t_fcdata *fcd,
667 int *global_atom_index)
669 int i, m, ki, ai, aj, type;
670 real dr, dr2, fbond, vbond, fij, vtot, ksh, khyp, drcut, ddr, ddr3;
675 for (i = 0; (i < nbonds); )
677 type = forceatoms[i++];
678 ai = forceatoms[i++];
679 aj = forceatoms[i++];
680 ksh = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].anharm_polarize.alpha; /* 7*/
681 khyp = forceparams[type].anharm_polarize.khyp;
682 drcut = forceparams[type].anharm_polarize.drcut;
685 fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
688 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
689 dr2 = iprod(dx, dx); /* 5 */
690 dr = dr2*gmx_invsqrt(dr2); /* 10 */
692 *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /* 19 */
703 vbond += khyp*ddr*ddr3;
704 fbond -= 4*khyp*ddr3;
706 fbond *= gmx_invsqrt(dr2); /* 6 */
707 vtot += vbond; /* 1*/
711 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
714 for (m = 0; (m < DIM); m++) /* 15 */
719 fshift[ki][m] += fij;
720 fshift[CENTRAL][m] -= fij;
726 real water_pol(int nbonds,
727 const t_iatom forceatoms[], const t_iparams forceparams[],
728 const rvec x[], rvec f[], rvec fshift[],
729 const t_pbc *pbc, const t_graph *g,
730 real lambda, real *dvdlambda,
731 const t_mdatoms *md, t_fcdata *fcd,
732 int *global_atom_index)
734 /* This routine implements anisotropic polarizibility for water, through
735 * a shell connected to a dummy with spring constant that differ in the
736 * three spatial dimensions in the molecular frame.
738 int i, m, aO, aH1, aH2, aD, aS, type, type0;
739 rvec dOH1, dOH2, dHH, dOD, dDS, nW, kk, dx, kdx, proj;
743 real vtot, fij, r_HH, r_OD, r_nW, tx, ty, tz, qS;
748 type0 = forceatoms[0];
750 qS = md->chargeA[aS];
751 kk[XX] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_x;
752 kk[YY] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_y;
753 kk[ZZ] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_z;
754 r_HH = 1.0/forceparams[type0].wpol.rHH;
755 r_OD = 1.0/forceparams[type0].wpol.rOD;
758 fprintf(debug, "WPOL: qS = %10.5f aS = %5d\n", qS, aS);
759 fprintf(debug, "WPOL: kk = %10.3f %10.3f %10.3f\n",
760 kk[XX], kk[YY], kk[ZZ]);
761 fprintf(debug, "WPOL: rOH = %10.3f rHH = %10.3f rOD = %10.3f\n",
762 forceparams[type0].wpol.rOH,
763 forceparams[type0].wpol.rHH,
764 forceparams[type0].wpol.rOD);
766 for (i = 0; (i < nbonds); i += 6)
768 type = forceatoms[i];
771 gmx_fatal(FARGS, "Sorry, type = %d, type0 = %d, file = %s, line = %d",
772 type, type0, __FILE__, __LINE__);
774 aO = forceatoms[i+1];
775 aH1 = forceatoms[i+2];
776 aH2 = forceatoms[i+3];
777 aD = forceatoms[i+4];
778 aS = forceatoms[i+5];
780 /* Compute vectors describing the water frame */
781 rvec_sub(x[aH1], x[aO], dOH1);
782 rvec_sub(x[aH2], x[aO], dOH2);
783 rvec_sub(x[aH2], x[aH1], dHH);
784 rvec_sub(x[aD], x[aO], dOD);
785 rvec_sub(x[aS], x[aD], dDS);
786 cprod(dOH1, dOH2, nW);
788 /* Compute inverse length of normal vector
789 * (this one could be precomputed, but I'm too lazy now)
791 r_nW = gmx_invsqrt(iprod(nW, nW));
792 /* This is for precision, but does not make a big difference,
795 r_OD = gmx_invsqrt(iprod(dOD, dOD));
797 /* Normalize the vectors in the water frame */
799 svmul(r_HH, dHH, dHH);
800 svmul(r_OD, dOD, dOD);
802 /* Compute displacement of shell along components of the vector */
803 dx[ZZ] = iprod(dDS, dOD);
804 /* Compute projection on the XY plane: dDS - dx[ZZ]*dOD */
805 for (m = 0; (m < DIM); m++)
807 proj[m] = dDS[m]-dx[ZZ]*dOD[m];
810 /*dx[XX] = iprod(dDS,nW);
811 dx[YY] = iprod(dDS,dHH);*/
812 dx[XX] = iprod(proj, nW);
813 for (m = 0; (m < DIM); m++)
815 proj[m] -= dx[XX]*nW[m];
817 dx[YY] = iprod(proj, dHH);
822 fprintf(debug, "WPOL: dx2=%10g dy2=%10g dz2=%10g sum=%10g dDS^2=%10g\n",
823 sqr(dx[XX]), sqr(dx[YY]), sqr(dx[ZZ]), iprod(dx, dx), iprod(dDS, dDS));
824 fprintf(debug, "WPOL: dHH=(%10g,%10g,%10g)\n", dHH[XX], dHH[YY], dHH[ZZ]);
825 fprintf(debug, "WPOL: dOD=(%10g,%10g,%10g), 1/r_OD = %10g\n",
826 dOD[XX], dOD[YY], dOD[ZZ], 1/r_OD);
827 fprintf(debug, "WPOL: nW =(%10g,%10g,%10g), 1/r_nW = %10g\n",
828 nW[XX], nW[YY], nW[ZZ], 1/r_nW);
829 fprintf(debug, "WPOL: dx =%10g, dy =%10g, dz =%10g\n",
830 dx[XX], dx[YY], dx[ZZ]);
831 fprintf(debug, "WPOL: dDSx=%10g, dDSy=%10g, dDSz=%10g\n",
832 dDS[XX], dDS[YY], dDS[ZZ]);
835 /* Now compute the forces and energy */
836 kdx[XX] = kk[XX]*dx[XX];
837 kdx[YY] = kk[YY]*dx[YY];
838 kdx[ZZ] = kk[ZZ]*dx[ZZ];
839 vtot += iprod(dx, kdx);
840 for (m = 0; (m < DIM); m++)
842 /* This is a tensor operation but written out for speed */
856 fprintf(debug, "WPOL: vwpol=%g\n", 0.5*iprod(dx, kdx));
857 fprintf(debug, "WPOL: df = (%10g, %10g, %10g)\n", df[XX], df[YY], df[ZZ]);
865 static real do_1_thole(const rvec xi, const rvec xj, rvec fi, rvec fj,
866 const t_pbc *pbc, real qq,
867 rvec fshift[], real afac)
870 real r12sq, r12_1, r12n, r12bar, v0, v1, fscal, ebar, fff;
873 t = pbc_rvec_sub(pbc, xi, xj, r12); /* 3 */
875 r12sq = iprod(r12, r12); /* 5 */
876 r12_1 = gmx_invsqrt(r12sq); /* 5 */
877 r12bar = afac/r12_1; /* 5 */
878 v0 = qq*ONE_4PI_EPS0*r12_1; /* 2 */
879 ebar = exp(-r12bar); /* 5 */
880 v1 = (1-(1+0.5*r12bar)*ebar); /* 4 */
881 fscal = ((v0*r12_1)*v1 - v0*0.5*afac*ebar*(r12bar+1))*r12_1; /* 9 */
884 fprintf(debug, "THOLE: v0 = %.3f v1 = %.3f r12= % .3f r12bar = %.3f fscal = %.3f ebar = %.3f\n", v0, v1, 1/r12_1, r12bar, fscal, ebar);
887 for (m = 0; (m < DIM); m++)
893 fshift[CENTRAL][m] -= fff;
896 return v0*v1; /* 1 */
900 real thole_pol(int nbonds,
901 const t_iatom forceatoms[], const t_iparams forceparams[],
902 const rvec x[], rvec f[], rvec fshift[],
903 const t_pbc *pbc, const t_graph *g,
904 real lambda, real *dvdlambda,
905 const t_mdatoms *md, t_fcdata *fcd,
906 int *global_atom_index)
908 /* Interaction between two pairs of particles with opposite charge */
909 int i, type, a1, da1, a2, da2;
910 real q1, q2, qq, a, al1, al2, afac;
913 for (i = 0; (i < nbonds); )
915 type = forceatoms[i++];
916 a1 = forceatoms[i++];
917 da1 = forceatoms[i++];
918 a2 = forceatoms[i++];
919 da2 = forceatoms[i++];
920 q1 = md->chargeA[da1];
921 q2 = md->chargeA[da2];
922 a = forceparams[type].thole.a;
923 al1 = forceparams[type].thole.alpha1;
924 al2 = forceparams[type].thole.alpha2;
926 afac = a*pow(al1*al2, -1.0/6.0);
927 V += do_1_thole(x[a1], x[a2], f[a1], f[a2], pbc, qq, fshift, afac);
928 V += do_1_thole(x[da1], x[a2], f[da1], f[a2], pbc, -qq, fshift, afac);
929 V += do_1_thole(x[a1], x[da2], f[a1], f[da2], pbc, -qq, fshift, afac);
930 V += do_1_thole(x[da1], x[da2], f[da1], f[da2], pbc, qq, fshift, afac);
936 real bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
937 rvec r_ij, rvec r_kj, real *costh,
939 /* Return value is the angle between the bonds i-j and j-k */
944 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
945 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
947 *costh = cos_angle(r_ij, r_kj); /* 25 */
948 th = acos(*costh); /* 10 */
953 real angles(int nbonds,
954 const t_iatom forceatoms[], const t_iparams forceparams[],
955 const rvec x[], rvec f[], rvec fshift[],
956 const t_pbc *pbc, const t_graph *g,
957 real lambda, real *dvdlambda,
958 const t_mdatoms *md, t_fcdata *fcd,
959 int *global_atom_index)
961 int i, ai, aj, ak, t1, t2, type;
963 real cos_theta, cos_theta2, theta, dVdt, va, vtot;
964 ivec jt, dt_ij, dt_kj;
967 for (i = 0; i < nbonds; )
969 type = forceatoms[i++];
970 ai = forceatoms[i++];
971 aj = forceatoms[i++];
972 ak = forceatoms[i++];
974 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
975 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
977 *dvdlambda += harmonic(forceparams[type].harmonic.krA,
978 forceparams[type].harmonic.krB,
979 forceparams[type].harmonic.rA*DEG2RAD,
980 forceparams[type].harmonic.rB*DEG2RAD,
981 theta, lambda, &va, &dVdt); /* 21 */
984 cos_theta2 = sqr(cos_theta);
994 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
995 sth = st*cos_theta; /* 1 */
999 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1000 theta*RAD2DEG, va, dVdt);
1003 nrij2 = iprod(r_ij, r_ij); /* 5 */
1004 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1006 nrij_1 = gmx_invsqrt(nrij2); /* 10 */
1007 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1009 cik = st*nrij_1*nrkj_1; /* 2 */
1010 cii = sth*nrij_1*nrij_1; /* 2 */
1011 ckk = sth*nrkj_1*nrkj_1; /* 2 */
1013 for (m = 0; m < DIM; m++)
1015 f_i[m] = -(cik*r_kj[m] - cii*r_ij[m]);
1016 f_k[m] = -(cik*r_ij[m] - ckk*r_kj[m]);
1017 f_j[m] = -f_i[m] - f_k[m];
1024 copy_ivec(SHIFT_IVEC(g, aj), jt);
1026 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1027 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1028 t1 = IVEC2IS(dt_ij);
1029 t2 = IVEC2IS(dt_kj);
1031 rvec_inc(fshift[t1], f_i);
1032 rvec_inc(fshift[CENTRAL], f_j);
1033 rvec_inc(fshift[t2], f_k);
1042 /* As angles, but using SIMD to calculate many dihedrals at once.
1043 * This routines does not calculate energies and shift forces.
1045 static gmx_inline void
1046 angles_noener_simd(int nbonds,
1047 const t_iatom forceatoms[], const t_iparams forceparams[],
1048 const rvec x[], rvec f[],
1049 const t_pbc *pbc, const t_graph *g,
1051 const t_mdatoms *md, t_fcdata *fcd,
1052 int *global_atom_index)
1054 #define UNROLL GMX_SIMD_WIDTH_HERE
1057 int type, ai[UNROLL], aj[UNROLL], ak[UNROLL];
1058 real coeff_array[2*UNROLL+UNROLL], *coeff;
1059 real dr_array[2*DIM*UNROLL+UNROLL], *dr;
1060 real f_buf_array[6*UNROLL+UNROLL], *f_buf;
1061 gmx_mm_pr k_S, theta0_S;
1062 gmx_mm_pr rijx_S, rijy_S, rijz_S;
1063 gmx_mm_pr rkjx_S, rkjy_S, rkjz_S;
1065 gmx_mm_pr rij_rkj_S;
1066 gmx_mm_pr nrij2_S, nrij_1_S;
1067 gmx_mm_pr nrkj2_S, nrkj_1_S;
1068 gmx_mm_pr cos_S, sin_S;
1070 gmx_mm_pr st_S, sth_S;
1071 gmx_mm_pr cik_S, cii_S, ckk_S;
1072 gmx_mm_pr f_ix_S, f_iy_S, f_iz_S;
1073 gmx_mm_pr f_kx_S, f_ky_S, f_kz_S;
1074 pbc_simd_t pbc_simd;
1076 /* Ensure register memory alignment */
1077 coeff = gmx_simd_align_real(coeff_array);
1078 dr = gmx_simd_align_real(dr_array);
1079 f_buf = gmx_simd_align_real(f_buf_array);
1081 set_pbc_simd(pbc,&pbc_simd);
1083 one_S = gmx_set1_pr(1.0);
1085 /* nbonds is the number of angles times nfa1, here we step UNROLL angles */
1086 for (i = 0; (i < nbonds); i += UNROLL*nfa1)
1088 /* Collect atoms for UNROLL angles.
1089 * iu indexes into forceatoms, we should not let iu go beyond nbonds.
1092 for (s = 0; s < UNROLL; s++)
1094 type = forceatoms[iu];
1095 ai[s] = forceatoms[iu+1];
1096 aj[s] = forceatoms[iu+2];
1097 ak[s] = forceatoms[iu+3];
1099 coeff[s] = forceparams[type].harmonic.krA;
1100 coeff[UNROLL+s] = forceparams[type].harmonic.rA*DEG2RAD;
1102 /* If you can't use pbc_dx_simd below for PBC, e.g. because
1103 * you can't round in SIMD, use pbc_rvec_sub here.
1105 /* Store the non PBC corrected distances packed and aligned */
1106 for (m = 0; m < DIM; m++)
1108 dr[s + m *UNROLL] = x[ai[s]][m] - x[aj[s]][m];
1109 dr[s + (DIM+m)*UNROLL] = x[ak[s]][m] - x[aj[s]][m];
1112 /* At the end fill the arrays with identical entries */
1113 if (iu + nfa1 < nbonds)
1119 k_S = gmx_load_pr(coeff);
1120 theta0_S = gmx_load_pr(coeff+UNROLL);
1122 rijx_S = gmx_load_pr(dr + 0*UNROLL);
1123 rijy_S = gmx_load_pr(dr + 1*UNROLL);
1124 rijz_S = gmx_load_pr(dr + 2*UNROLL);
1125 rkjx_S = gmx_load_pr(dr + 3*UNROLL);
1126 rkjy_S = gmx_load_pr(dr + 4*UNROLL);
1127 rkjz_S = gmx_load_pr(dr + 5*UNROLL);
1129 pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, &pbc_simd);
1130 pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, &pbc_simd);
1132 rij_rkj_S = gmx_iprod_pr(rijx_S, rijy_S, rijz_S,
1133 rkjx_S, rkjy_S, rkjz_S);
1135 nrij2_S = gmx_norm2_pr(rijx_S, rijy_S, rijz_S);
1136 nrkj2_S = gmx_norm2_pr(rkjx_S, rkjy_S, rkjz_S);
1138 nrij_1_S = gmx_invsqrt_pr(nrij2_S);
1139 nrkj_1_S = gmx_invsqrt_pr(nrkj2_S);
1141 cos_S = gmx_mul_pr(rij_rkj_S, gmx_mul_pr(nrij_1_S, nrkj_1_S));
1143 theta_S = gmx_acos_pr(cos_S);
1145 sin_S = gmx_invsqrt_pr(gmx_max_pr(gmx_sub_pr(one_S, gmx_mul_pr(cos_S, cos_S)),
1147 st_S = gmx_mul_pr(gmx_mul_pr(k_S, gmx_sub_pr(theta0_S, theta_S)),
1149 sth_S = gmx_mul_pr(st_S, cos_S);
1151 cik_S = gmx_mul_pr(st_S, gmx_mul_pr(nrij_1_S, nrkj_1_S));
1152 cii_S = gmx_mul_pr(sth_S, gmx_mul_pr(nrij_1_S, nrij_1_S));
1153 ckk_S = gmx_mul_pr(sth_S, gmx_mul_pr(nrkj_1_S, nrkj_1_S));
1155 f_ix_S = gmx_mul_pr(cii_S, rijx_S);
1156 f_ix_S = gmx_nmsub_pr(cik_S, rkjx_S, f_ix_S);
1157 f_iy_S = gmx_mul_pr(cii_S, rijy_S);
1158 f_iy_S = gmx_nmsub_pr(cik_S, rkjy_S, f_iy_S);
1159 f_iz_S = gmx_mul_pr(cii_S, rijz_S);
1160 f_iz_S = gmx_nmsub_pr(cik_S, rkjz_S, f_iz_S);
1161 f_kx_S = gmx_mul_pr(ckk_S, rkjx_S);
1162 f_kx_S = gmx_nmsub_pr(cik_S, rijx_S, f_kx_S);
1163 f_ky_S = gmx_mul_pr(ckk_S, rkjy_S);
1164 f_ky_S = gmx_nmsub_pr(cik_S, rijy_S, f_ky_S);
1165 f_kz_S = gmx_mul_pr(ckk_S, rkjz_S);
1166 f_kz_S = gmx_nmsub_pr(cik_S, rijz_S, f_kz_S);
1168 gmx_store_pr(f_buf + 0*UNROLL, f_ix_S);
1169 gmx_store_pr(f_buf + 1*UNROLL, f_iy_S);
1170 gmx_store_pr(f_buf + 2*UNROLL, f_iz_S);
1171 gmx_store_pr(f_buf + 3*UNROLL, f_kx_S);
1172 gmx_store_pr(f_buf + 4*UNROLL, f_ky_S);
1173 gmx_store_pr(f_buf + 5*UNROLL, f_kz_S);
1179 for (m = 0; m < DIM; m++)
1181 f[ai[s]][m] += f_buf[s + m*UNROLL];
1182 f[aj[s]][m] -= f_buf[s + m*UNROLL] + f_buf[s + (DIM+m)*UNROLL];
1183 f[ak[s]][m] += f_buf[s + (DIM+m)*UNROLL];
1188 while (s < UNROLL && iu < nbonds);
1193 #endif /* SIMD_BONDEDS */
1195 real linear_angles(int nbonds,
1196 const t_iatom forceatoms[], const t_iparams forceparams[],
1197 const rvec x[], rvec f[], rvec fshift[],
1198 const t_pbc *pbc, const t_graph *g,
1199 real lambda, real *dvdlambda,
1200 const t_mdatoms *md, t_fcdata *fcd,
1201 int *global_atom_index)
1203 int i, m, ai, aj, ak, t1, t2, type;
1205 real L1, kA, kB, aA, aB, dr, dr2, va, vtot, a, b, klin;
1206 ivec jt, dt_ij, dt_kj;
1207 rvec r_ij, r_kj, r_ik, dx;
1211 for (i = 0; (i < nbonds); )
1213 type = forceatoms[i++];
1214 ai = forceatoms[i++];
1215 aj = forceatoms[i++];
1216 ak = forceatoms[i++];
1218 kA = forceparams[type].linangle.klinA;
1219 kB = forceparams[type].linangle.klinB;
1220 klin = L1*kA + lambda*kB;
1222 aA = forceparams[type].linangle.aA;
1223 aB = forceparams[type].linangle.aB;
1224 a = L1*aA+lambda*aB;
1227 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
1228 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
1229 rvec_sub(r_ij, r_kj, r_ik);
1232 for (m = 0; (m < DIM); m++)
1234 dr = -a * r_ij[m] - b * r_kj[m];
1239 f_j[m] = -(f_i[m]+f_k[m]);
1245 *dvdlambda += 0.5*(kB-kA)*dr2 + klin*(aB-aA)*iprod(dx, r_ik);
1251 copy_ivec(SHIFT_IVEC(g, aj), jt);
1253 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1254 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1255 t1 = IVEC2IS(dt_ij);
1256 t2 = IVEC2IS(dt_kj);
1258 rvec_inc(fshift[t1], f_i);
1259 rvec_inc(fshift[CENTRAL], f_j);
1260 rvec_inc(fshift[t2], f_k);
1265 real urey_bradley(int nbonds,
1266 const t_iatom forceatoms[], const t_iparams forceparams[],
1267 const rvec x[], rvec f[], rvec fshift[],
1268 const t_pbc *pbc, const t_graph *g,
1269 real lambda, real *dvdlambda,
1270 const t_mdatoms *md, t_fcdata *fcd,
1271 int *global_atom_index)
1273 int i, m, ai, aj, ak, t1, t2, type, ki;
1274 rvec r_ij, r_kj, r_ik;
1275 real cos_theta, cos_theta2, theta;
1276 real dVdt, va, vtot, dr, dr2, vbond, fbond, fik;
1277 real kthA, th0A, kUBA, r13A, kthB, th0B, kUBB, r13B;
1278 ivec jt, dt_ij, dt_kj, dt_ik;
1281 for (i = 0; (i < nbonds); )
1283 type = forceatoms[i++];
1284 ai = forceatoms[i++];
1285 aj = forceatoms[i++];
1286 ak = forceatoms[i++];
1287 th0A = forceparams[type].u_b.thetaA*DEG2RAD;
1288 kthA = forceparams[type].u_b.kthetaA;
1289 r13A = forceparams[type].u_b.r13A;
1290 kUBA = forceparams[type].u_b.kUBA;
1291 th0B = forceparams[type].u_b.thetaB*DEG2RAD;
1292 kthB = forceparams[type].u_b.kthetaB;
1293 r13B = forceparams[type].u_b.r13B;
1294 kUBB = forceparams[type].u_b.kUBB;
1296 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
1297 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
1299 *dvdlambda += harmonic(kthA, kthB, th0A, th0B, theta, lambda, &va, &dVdt); /* 21 */
1302 ki = pbc_rvec_sub(pbc, x[ai], x[ak], r_ik); /* 3 */
1303 dr2 = iprod(r_ik, r_ik); /* 5 */
1304 dr = dr2*gmx_invsqrt(dr2); /* 10 */
1306 *dvdlambda += harmonic(kUBA, kUBB, r13A, r13B, dr, lambda, &vbond, &fbond); /* 19 */
1308 cos_theta2 = sqr(cos_theta); /* 1 */
1316 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1317 sth = st*cos_theta; /* 1 */
1321 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1322 theta*RAD2DEG, va, dVdt);
1325 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1326 nrij2 = iprod(r_ij, r_ij);
1328 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
1329 cii = sth/nrij2; /* 10 */
1330 ckk = sth/nrkj2; /* 10 */
1332 for (m = 0; (m < DIM); m++) /* 39 */
1334 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
1335 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
1336 f_j[m] = -f_i[m]-f_k[m];
1343 copy_ivec(SHIFT_IVEC(g, aj), jt);
1345 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1346 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1347 t1 = IVEC2IS(dt_ij);
1348 t2 = IVEC2IS(dt_kj);
1350 rvec_inc(fshift[t1], f_i);
1351 rvec_inc(fshift[CENTRAL], f_j);
1352 rvec_inc(fshift[t2], f_k);
1354 /* Time for the bond calculations */
1360 vtot += vbond; /* 1*/
1361 fbond *= gmx_invsqrt(dr2); /* 6 */
1365 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, ak), dt_ik);
1366 ki = IVEC2IS(dt_ik);
1368 for (m = 0; (m < DIM); m++) /* 15 */
1370 fik = fbond*r_ik[m];
1373 fshift[ki][m] += fik;
1374 fshift[CENTRAL][m] -= fik;
1380 real quartic_angles(int nbonds,
1381 const t_iatom forceatoms[], const t_iparams forceparams[],
1382 const rvec x[], rvec f[], rvec fshift[],
1383 const t_pbc *pbc, const t_graph *g,
1384 real lambda, real *dvdlambda,
1385 const t_mdatoms *md, t_fcdata *fcd,
1386 int *global_atom_index)
1388 int i, j, ai, aj, ak, t1, t2, type;
1390 real cos_theta, cos_theta2, theta, dt, dVdt, va, dtp, c, vtot;
1391 ivec jt, dt_ij, dt_kj;
1394 for (i = 0; (i < nbonds); )
1396 type = forceatoms[i++];
1397 ai = forceatoms[i++];
1398 aj = forceatoms[i++];
1399 ak = forceatoms[i++];
1401 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
1402 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
1404 dt = theta - forceparams[type].qangle.theta*DEG2RAD; /* 2 */
1407 va = forceparams[type].qangle.c[0];
1409 for (j = 1; j <= 4; j++)
1411 c = forceparams[type].qangle.c[j];
1420 cos_theta2 = sqr(cos_theta); /* 1 */
1429 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1430 sth = st*cos_theta; /* 1 */
1434 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1435 theta*RAD2DEG, va, dVdt);
1438 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1439 nrij2 = iprod(r_ij, r_ij);
1441 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
1442 cii = sth/nrij2; /* 10 */
1443 ckk = sth/nrkj2; /* 10 */
1445 for (m = 0; (m < DIM); m++) /* 39 */
1447 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
1448 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
1449 f_j[m] = -f_i[m]-f_k[m];
1456 copy_ivec(SHIFT_IVEC(g, aj), jt);
1458 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1459 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1460 t1 = IVEC2IS(dt_ij);
1461 t2 = IVEC2IS(dt_kj);
1463 rvec_inc(fshift[t1], f_i);
1464 rvec_inc(fshift[CENTRAL], f_j);
1465 rvec_inc(fshift[t2], f_k);
1471 real dih_angle(const rvec xi, const rvec xj, const rvec xk, const rvec xl,
1473 rvec r_ij, rvec r_kj, rvec r_kl, rvec m, rvec n,
1474 real *sign, int *t1, int *t2, int *t3)
1478 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
1479 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
1480 *t3 = pbc_rvec_sub(pbc, xk, xl, r_kl); /* 3 */
1482 cprod(r_ij, r_kj, m); /* 9 */
1483 cprod(r_kj, r_kl, n); /* 9 */
1484 phi = gmx_angle(m, n); /* 49 (assuming 25 for atan2) */
1485 ipr = iprod(r_ij, n); /* 5 */
1486 (*sign) = (ipr < 0.0) ? -1.0 : 1.0;
1487 phi = (*sign)*phi; /* 1 */
1495 /* As dih_angle above, but calculates 4 dihedral angles at once using SIMD,
1496 * also calculates the pre-factor required for the dihedral force update.
1497 * Note that bv and buf should be register aligned.
1499 static gmx_inline void
1500 dih_angle_simd(const rvec *x,
1501 const int *ai, const int *aj, const int *ak, const int *al,
1502 const pbc_simd_t *pbc,
1505 gmx_mm_pr *mx_S, gmx_mm_pr *my_S, gmx_mm_pr *mz_S,
1506 gmx_mm_pr *nx_S, gmx_mm_pr *ny_S, gmx_mm_pr *nz_S,
1507 gmx_mm_pr *nrkj_m2_S,
1508 gmx_mm_pr *nrkj_n2_S,
1512 #define UNROLL GMX_SIMD_WIDTH_HERE
1514 gmx_mm_pr rijx_S, rijy_S, rijz_S;
1515 gmx_mm_pr rkjx_S, rkjy_S, rkjz_S;
1516 gmx_mm_pr rklx_S, rkly_S, rklz_S;
1517 gmx_mm_pr cx_S, cy_S, cz_S;
1521 gmx_mm_pr iprm_S, iprn_S;
1522 gmx_mm_pr nrkj2_S, nrkj_1_S, nrkj_2_S, nrkj_S;
1524 gmx_mm_pr fmin_S = gmx_set1_pr(GMX_FLOAT_MIN);
1526 for (s = 0; s < UNROLL; s++)
1528 /* If you can't use pbc_dx_simd below for PBC, e.g. because
1529 * you can't round in SIMD, use pbc_rvec_sub here.
1531 for (m = 0; m < DIM; m++)
1533 dr[s + (0*DIM + m)*UNROLL] = x[ai[s]][m] - x[aj[s]][m];
1534 dr[s + (1*DIM + m)*UNROLL] = x[ak[s]][m] - x[aj[s]][m];
1535 dr[s + (2*DIM + m)*UNROLL] = x[ak[s]][m] - x[al[s]][m];
1539 rijx_S = gmx_load_pr(dr + 0*UNROLL);
1540 rijy_S = gmx_load_pr(dr + 1*UNROLL);
1541 rijz_S = gmx_load_pr(dr + 2*UNROLL);
1542 rkjx_S = gmx_load_pr(dr + 3*UNROLL);
1543 rkjy_S = gmx_load_pr(dr + 4*UNROLL);
1544 rkjz_S = gmx_load_pr(dr + 5*UNROLL);
1545 rklx_S = gmx_load_pr(dr + 6*UNROLL);
1546 rkly_S = gmx_load_pr(dr + 7*UNROLL);
1547 rklz_S = gmx_load_pr(dr + 8*UNROLL);
1549 pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, pbc);
1550 pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, pbc);
1551 pbc_dx_simd(&rklx_S, &rkly_S, &rklz_S, pbc);
1553 gmx_cprod_pr(rijx_S, rijy_S, rijz_S,
1554 rkjx_S, rkjy_S, rkjz_S,
1557 gmx_cprod_pr(rkjx_S, rkjy_S, rkjz_S,
1558 rklx_S, rkly_S, rklz_S,
1561 gmx_cprod_pr(*mx_S, *my_S, *mz_S,
1562 *nx_S, *ny_S, *nz_S,
1563 &cx_S, &cy_S, &cz_S);
1565 cn_S = gmx_sqrt_pr(gmx_norm2_pr(cx_S, cy_S, cz_S));
1567 s_S = gmx_iprod_pr(*mx_S, *my_S, *mz_S, *nx_S, *ny_S, *nz_S);
1569 /* Determine the dihedral angle, the sign might need correction */
1570 *phi_S = gmx_atan2_pr(cn_S, s_S);
1572 ipr_S = gmx_iprod_pr(rijx_S, rijy_S, rijz_S,
1573 *nx_S, *ny_S, *nz_S);
1575 iprm_S = gmx_norm2_pr(*mx_S, *my_S, *mz_S);
1576 iprn_S = gmx_norm2_pr(*nx_S, *ny_S, *nz_S);
1578 nrkj2_S = gmx_norm2_pr(rkjx_S, rkjy_S, rkjz_S);
1580 /* Avoid division by zero. When zero, the result is multiplied by 0
1581 * anyhow, so the 3 max below do not affect the final result.
1583 nrkj2_S = gmx_max_pr(nrkj2_S, fmin_S);
1584 nrkj_1_S = gmx_invsqrt_pr(nrkj2_S);
1585 nrkj_2_S = gmx_mul_pr(nrkj_1_S, nrkj_1_S);
1586 nrkj_S = gmx_mul_pr(nrkj2_S, nrkj_1_S);
1588 iprm_S = gmx_max_pr(iprm_S, fmin_S);
1589 iprn_S = gmx_max_pr(iprn_S, fmin_S);
1590 *nrkj_m2_S = gmx_mul_pr(nrkj_S, gmx_inv_pr(iprm_S));
1591 *nrkj_n2_S = gmx_mul_pr(nrkj_S, gmx_inv_pr(iprn_S));
1593 /* Set sign of phi_S with the sign of ipr_S; phi_S is currently positive */
1594 *phi_S = gmx_cpsgn_nonneg_pr(ipr_S, *phi_S);
1596 p_S = gmx_iprod_pr(rijx_S, rijy_S, rijz_S,
1597 rkjx_S, rkjy_S, rkjz_S);
1598 p_S = gmx_mul_pr(p_S, nrkj_2_S);
1600 q_S = gmx_iprod_pr(rklx_S, rkly_S, rklz_S,
1601 rkjx_S, rkjy_S, rkjz_S);
1602 q_S = gmx_mul_pr(q_S, nrkj_2_S);
1604 gmx_store_pr(p, p_S);
1605 gmx_store_pr(q, q_S);
1609 #endif /* SIMD_BONDEDS */
1612 void do_dih_fup(int i, int j, int k, int l, real ddphi,
1613 rvec r_ij, rvec r_kj, rvec r_kl,
1614 rvec m, rvec n, rvec f[], rvec fshift[],
1615 const t_pbc *pbc, const t_graph *g,
1616 const rvec x[], int t1, int t2, int t3)
1619 rvec f_i, f_j, f_k, f_l;
1620 rvec uvec, vvec, svec, dx_jl;
1621 real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1622 real a, b, p, q, toler;
1623 ivec jt, dt_ij, dt_kj, dt_lj;
1625 iprm = iprod(m, m); /* 5 */
1626 iprn = iprod(n, n); /* 5 */
1627 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1628 toler = nrkj2*GMX_REAL_EPS;
1629 if ((iprm > toler) && (iprn > toler))
1631 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1632 nrkj_2 = nrkj_1*nrkj_1; /* 1 */
1633 nrkj = nrkj2*nrkj_1; /* 1 */
1634 a = -ddphi*nrkj/iprm; /* 11 */
1635 svmul(a, m, f_i); /* 3 */
1636 b = ddphi*nrkj/iprn; /* 11 */
1637 svmul(b, n, f_l); /* 3 */
1638 p = iprod(r_ij, r_kj); /* 5 */
1639 p *= nrkj_2; /* 1 */
1640 q = iprod(r_kl, r_kj); /* 5 */
1641 q *= nrkj_2; /* 1 */
1642 svmul(p, f_i, uvec); /* 3 */
1643 svmul(q, f_l, vvec); /* 3 */
1644 rvec_sub(uvec, vvec, svec); /* 3 */
1645 rvec_sub(f_i, svec, f_j); /* 3 */
1646 rvec_add(f_l, svec, f_k); /* 3 */
1647 rvec_inc(f[i], f_i); /* 3 */
1648 rvec_dec(f[j], f_j); /* 3 */
1649 rvec_dec(f[k], f_k); /* 3 */
1650 rvec_inc(f[l], f_l); /* 3 */
1654 copy_ivec(SHIFT_IVEC(g, j), jt);
1655 ivec_sub(SHIFT_IVEC(g, i), jt, dt_ij);
1656 ivec_sub(SHIFT_IVEC(g, k), jt, dt_kj);
1657 ivec_sub(SHIFT_IVEC(g, l), jt, dt_lj);
1658 t1 = IVEC2IS(dt_ij);
1659 t2 = IVEC2IS(dt_kj);
1660 t3 = IVEC2IS(dt_lj);
1664 t3 = pbc_rvec_sub(pbc, x[l], x[j], dx_jl);
1671 rvec_inc(fshift[t1], f_i);
1672 rvec_dec(fshift[CENTRAL], f_j);
1673 rvec_dec(fshift[t2], f_k);
1674 rvec_inc(fshift[t3], f_l);
1679 /* As do_dih_fup above, but without shift forces */
1681 do_dih_fup_noshiftf(int i, int j, int k, int l, real ddphi,
1682 rvec r_ij, rvec r_kj, rvec r_kl,
1683 rvec m, rvec n, rvec f[])
1685 rvec f_i, f_j, f_k, f_l;
1686 rvec uvec, vvec, svec, dx_jl;
1687 real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1688 real a, b, p, q, toler;
1689 ivec jt, dt_ij, dt_kj, dt_lj;
1691 iprm = iprod(m, m); /* 5 */
1692 iprn = iprod(n, n); /* 5 */
1693 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1694 toler = nrkj2*GMX_REAL_EPS;
1695 if ((iprm > toler) && (iprn > toler))
1697 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1698 nrkj_2 = nrkj_1*nrkj_1; /* 1 */
1699 nrkj = nrkj2*nrkj_1; /* 1 */
1700 a = -ddphi*nrkj/iprm; /* 11 */
1701 svmul(a, m, f_i); /* 3 */
1702 b = ddphi*nrkj/iprn; /* 11 */
1703 svmul(b, n, f_l); /* 3 */
1704 p = iprod(r_ij, r_kj); /* 5 */
1705 p *= nrkj_2; /* 1 */
1706 q = iprod(r_kl, r_kj); /* 5 */
1707 q *= nrkj_2; /* 1 */
1708 svmul(p, f_i, uvec); /* 3 */
1709 svmul(q, f_l, vvec); /* 3 */
1710 rvec_sub(uvec, vvec, svec); /* 3 */
1711 rvec_sub(f_i, svec, f_j); /* 3 */
1712 rvec_add(f_l, svec, f_k); /* 3 */
1713 rvec_inc(f[i], f_i); /* 3 */
1714 rvec_dec(f[j], f_j); /* 3 */
1715 rvec_dec(f[k], f_k); /* 3 */
1716 rvec_inc(f[l], f_l); /* 3 */
1720 /* As do_dih_fup_noshiftf above, but with pre-calculated pre-factors */
1721 static gmx_inline void
1722 do_dih_fup_noshiftf_precalc(int i, int j, int k, int l,
1724 real f_i_x, real f_i_y, real f_i_z,
1725 real mf_l_x, real mf_l_y, real mf_l_z,
1728 rvec f_i, f_j, f_k, f_l;
1729 rvec uvec, vvec, svec;
1737 svmul(p, f_i, uvec);
1738 svmul(q, f_l, vvec);
1739 rvec_sub(uvec, vvec, svec);
1740 rvec_sub(f_i, svec, f_j);
1741 rvec_add(f_l, svec, f_k);
1742 rvec_inc(f[i], f_i);
1743 rvec_dec(f[j], f_j);
1744 rvec_dec(f[k], f_k);
1745 rvec_inc(f[l], f_l);
1749 real dopdihs(real cpA, real cpB, real phiA, real phiB, int mult,
1750 real phi, real lambda, real *V, real *F)
1752 real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1753 real L1 = 1.0 - lambda;
1754 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1755 real dph0 = (phiB - phiA)*DEG2RAD;
1756 real cp = L1*cpA + lambda*cpB;
1758 mdphi = mult*phi - ph0;
1760 ddphi = -cp*mult*sdphi;
1761 v1 = 1.0 + cos(mdphi);
1764 dvdlambda = (cpB - cpA)*v1 + cp*dph0*sdphi;
1771 /* That was 40 flops */
1775 dopdihs_noener(real cpA, real cpB, real phiA, real phiB, int mult,
1776 real phi, real lambda, real *F)
1778 real mdphi, sdphi, ddphi;
1779 real L1 = 1.0 - lambda;
1780 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1781 real cp = L1*cpA + lambda*cpB;
1783 mdphi = mult*phi - ph0;
1785 ddphi = -cp*mult*sdphi;
1789 /* That was 20 flops */
1793 dopdihs_mdphi(real cpA, real cpB, real phiA, real phiB, int mult,
1794 real phi, real lambda, real *cp, real *mdphi)
1796 real L1 = 1.0 - lambda;
1797 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1799 *cp = L1*cpA + lambda*cpB;
1801 *mdphi = mult*phi - ph0;
1804 static real dopdihs_min(real cpA, real cpB, real phiA, real phiB, int mult,
1805 real phi, real lambda, real *V, real *F)
1806 /* similar to dopdihs, except for a minus sign *
1807 * and a different treatment of mult/phi0 */
1809 real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1810 real L1 = 1.0 - lambda;
1811 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1812 real dph0 = (phiB - phiA)*DEG2RAD;
1813 real cp = L1*cpA + lambda*cpB;
1815 mdphi = mult*(phi-ph0);
1817 ddphi = cp*mult*sdphi;
1818 v1 = 1.0-cos(mdphi);
1821 dvdlambda = (cpB-cpA)*v1 + cp*dph0*sdphi;
1828 /* That was 40 flops */
1831 real pdihs(int nbonds,
1832 const t_iatom forceatoms[], const t_iparams forceparams[],
1833 const rvec x[], rvec f[], rvec fshift[],
1834 const t_pbc *pbc, const t_graph *g,
1835 real lambda, real *dvdlambda,
1836 const t_mdatoms *md, t_fcdata *fcd,
1837 int *global_atom_index)
1839 int i, type, ai, aj, ak, al;
1841 rvec r_ij, r_kj, r_kl, m, n;
1842 real phi, sign, ddphi, vpd, vtot;
1846 for (i = 0; (i < nbonds); )
1848 type = forceatoms[i++];
1849 ai = forceatoms[i++];
1850 aj = forceatoms[i++];
1851 ak = forceatoms[i++];
1852 al = forceatoms[i++];
1854 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1855 &sign, &t1, &t2, &t3); /* 84 */
1856 *dvdlambda += dopdihs(forceparams[type].pdihs.cpA,
1857 forceparams[type].pdihs.cpB,
1858 forceparams[type].pdihs.phiA,
1859 forceparams[type].pdihs.phiB,
1860 forceparams[type].pdihs.mult,
1861 phi, lambda, &vpd, &ddphi);
1864 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
1865 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
1868 fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
1869 ai, aj, ak, al, phi);
1876 void make_dp_periodic(real *dp) /* 1 flop? */
1878 /* dp cannot be outside (-pi,pi) */
1883 else if (*dp < -M_PI)
1890 /* As pdihs above, but without calculating energies and shift forces */
1892 pdihs_noener(int nbonds,
1893 const t_iatom forceatoms[], const t_iparams forceparams[],
1894 const rvec x[], rvec f[],
1895 const t_pbc *pbc, const t_graph *g,
1897 const t_mdatoms *md, t_fcdata *fcd,
1898 int *global_atom_index)
1900 int i, type, ai, aj, ak, al;
1902 rvec r_ij, r_kj, r_kl, m, n;
1903 real phi, sign, ddphi_tot, ddphi;
1905 for (i = 0; (i < nbonds); )
1907 ai = forceatoms[i+1];
1908 aj = forceatoms[i+2];
1909 ak = forceatoms[i+3];
1910 al = forceatoms[i+4];
1912 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1913 &sign, &t1, &t2, &t3);
1917 /* Loop over dihedrals working on the same atoms,
1918 * so we avoid recalculating angles and force distributions.
1922 type = forceatoms[i];
1923 dopdihs_noener(forceparams[type].pdihs.cpA,
1924 forceparams[type].pdihs.cpB,
1925 forceparams[type].pdihs.phiA,
1926 forceparams[type].pdihs.phiB,
1927 forceparams[type].pdihs.mult,
1928 phi, lambda, &ddphi);
1933 while (i < nbonds &&
1934 forceatoms[i+1] == ai &&
1935 forceatoms[i+2] == aj &&
1936 forceatoms[i+3] == ak &&
1937 forceatoms[i+4] == al);
1939 do_dih_fup_noshiftf(ai, aj, ak, al, ddphi_tot, r_ij, r_kj, r_kl, m, n, f);
1946 /* As pdihs_noner above, but using SIMD to calculate many dihedrals at once */
1948 pdihs_noener_simd(int nbonds,
1949 const t_iatom forceatoms[], const t_iparams forceparams[],
1950 const rvec x[], rvec f[],
1951 const t_pbc *pbc, const t_graph *g,
1953 const t_mdatoms *md, t_fcdata *fcd,
1954 int *global_atom_index)
1956 #define UNROLL GMX_SIMD_WIDTH_HERE
1959 int type, ai[UNROLL], aj[UNROLL], ak[UNROLL], al[UNROLL];
1960 int t1[UNROLL], t2[UNROLL], t3[UNROLL];
1962 real dr_array[3*DIM*UNROLL+UNROLL], *dr;
1963 real buf_array[7*UNROLL+UNROLL], *buf;
1964 real *cp, *phi0, *mult, *phi, *p, *q, *sf_i, *msf_l;
1965 gmx_mm_pr phi0_S, phi_S;
1966 gmx_mm_pr mx_S, my_S, mz_S;
1967 gmx_mm_pr nx_S, ny_S, nz_S;
1968 gmx_mm_pr nrkj_m2_S, nrkj_n2_S;
1969 gmx_mm_pr cp_S, mdphi_S, mult_S;
1970 gmx_mm_pr sin_S, cos_S;
1972 gmx_mm_pr sf_i_S, msf_l_S;
1973 pbc_simd_t pbc_simd;
1975 /* Ensure SIMD register alignment */
1976 dr = gmx_simd_align_real(dr_array);
1977 buf = gmx_simd_align_real(buf_array);
1979 /* Extract aligned pointer for parameters and variables */
1980 cp = buf + 0*UNROLL;
1981 phi0 = buf + 1*UNROLL;
1982 mult = buf + 2*UNROLL;
1985 sf_i = buf + 5*UNROLL;
1986 msf_l = buf + 6*UNROLL;
1988 set_pbc_simd(pbc, &pbc_simd);
1990 /* nbonds is the number of dihedrals times nfa1, here we step UNROLL dihs */
1991 for (i = 0; (i < nbonds); i += UNROLL*nfa1)
1993 /* Collect atoms quadruplets for UNROLL dihedrals.
1994 * iu indexes into forceatoms, we should not let iu go beyond nbonds.
1997 for (s = 0; s < UNROLL; s++)
1999 type = forceatoms[iu];
2000 ai[s] = forceatoms[iu+1];
2001 aj[s] = forceatoms[iu+2];
2002 ak[s] = forceatoms[iu+3];
2003 al[s] = forceatoms[iu+4];
2005 cp[s] = forceparams[type].pdihs.cpA;
2006 phi0[s] = forceparams[type].pdihs.phiA*DEG2RAD;
2007 mult[s] = forceparams[type].pdihs.mult;
2009 /* At the end fill the arrays with identical entries */
2010 if (iu + nfa1 < nbonds)
2016 /* Caclulate UNROLL dihedral angles at once */
2017 dih_angle_simd(x, ai, aj, ak, al, &pbc_simd,
2020 &mx_S, &my_S, &mz_S,
2021 &nx_S, &ny_S, &nz_S,
2026 cp_S = gmx_load_pr(cp);
2027 phi0_S = gmx_load_pr(phi0);
2028 mult_S = gmx_load_pr(mult);
2030 mdphi_S = gmx_sub_pr(gmx_mul_pr(mult_S, phi_S), phi0_S);
2032 /* Calculate UNROLL sines at once */
2033 gmx_sincos_pr(mdphi_S, &sin_S, &cos_S);
2034 mddphi_S = gmx_mul_pr(gmx_mul_pr(cp_S, mult_S), sin_S);
2035 sf_i_S = gmx_mul_pr(mddphi_S, nrkj_m2_S);
2036 msf_l_S = gmx_mul_pr(mddphi_S, nrkj_n2_S);
2038 /* After this m?_S will contain f[i] */
2039 mx_S = gmx_mul_pr(sf_i_S, mx_S);
2040 my_S = gmx_mul_pr(sf_i_S, my_S);
2041 mz_S = gmx_mul_pr(sf_i_S, mz_S);
2043 /* After this m?_S will contain -f[l] */
2044 nx_S = gmx_mul_pr(msf_l_S, nx_S);
2045 ny_S = gmx_mul_pr(msf_l_S, ny_S);
2046 nz_S = gmx_mul_pr(msf_l_S, nz_S);
2048 gmx_store_pr(dr + 0*UNROLL, mx_S);
2049 gmx_store_pr(dr + 1*UNROLL, my_S);
2050 gmx_store_pr(dr + 2*UNROLL, mz_S);
2051 gmx_store_pr(dr + 3*UNROLL, nx_S);
2052 gmx_store_pr(dr + 4*UNROLL, ny_S);
2053 gmx_store_pr(dr + 5*UNROLL, nz_S);
2059 do_dih_fup_noshiftf_precalc(ai[s], aj[s], ak[s], al[s],
2064 dr[(DIM+XX)*UNROLL+s],
2065 dr[(DIM+YY)*UNROLL+s],
2066 dr[(DIM+ZZ)*UNROLL+s],
2071 while (s < UNROLL && iu < nbonds);
2076 #endif /* SIMD_BONDEDS */
2079 real idihs(int nbonds,
2080 const t_iatom forceatoms[], const t_iparams forceparams[],
2081 const rvec x[], rvec f[], rvec fshift[],
2082 const t_pbc *pbc, const t_graph *g,
2083 real lambda, real *dvdlambda,
2084 const t_mdatoms *md, t_fcdata *fcd,
2085 int *global_atom_index)
2087 int i, type, ai, aj, ak, al;
2089 real phi, phi0, dphi0, ddphi, sign, vtot;
2090 rvec r_ij, r_kj, r_kl, m, n;
2091 real L1, kk, dp, dp2, kA, kB, pA, pB, dvdl_term;
2096 for (i = 0; (i < nbonds); )
2098 type = forceatoms[i++];
2099 ai = forceatoms[i++];
2100 aj = forceatoms[i++];
2101 ak = forceatoms[i++];
2102 al = forceatoms[i++];
2104 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2105 &sign, &t1, &t2, &t3); /* 84 */
2107 /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2108 * force changes if we just apply a normal harmonic.
2109 * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2110 * This means we will never have the periodicity problem, unless
2111 * the dihedral is Pi away from phiO, which is very unlikely due to
2114 kA = forceparams[type].harmonic.krA;
2115 kB = forceparams[type].harmonic.krB;
2116 pA = forceparams[type].harmonic.rA;
2117 pB = forceparams[type].harmonic.rB;
2119 kk = L1*kA + lambda*kB;
2120 phi0 = (L1*pA + lambda*pB)*DEG2RAD;
2121 dphi0 = (pB - pA)*DEG2RAD;
2125 make_dp_periodic(&dp);
2132 dvdl_term += 0.5*(kB - kA)*dp2 - kk*dphi0*dp;
2134 do_dih_fup(ai, aj, ak, al, (real)(-ddphi), r_ij, r_kj, r_kl, m, n,
2135 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2140 fprintf(debug, "idih: (%d,%d,%d,%d) phi=%g\n",
2141 ai, aj, ak, al, phi);
2146 *dvdlambda += dvdl_term;
2151 real posres(int nbonds,
2152 const t_iatom forceatoms[], const t_iparams forceparams[],
2153 const rvec x[], rvec f[], rvec vir_diag,
2155 real lambda, real *dvdlambda,
2156 int refcoord_scaling, int ePBC, rvec comA, rvec comB)
2158 int i, ai, m, d, type, ki, npbcdim = 0;
2159 const t_iparams *pr;
2162 real posA, posB, ref = 0;
2163 rvec comA_sc, comB_sc, rdist, dpdl, pos, dx;
2164 gmx_bool bForceValid = TRUE;
2166 if ((f == NULL) || (vir_diag == NULL)) /* should both be null together! */
2168 bForceValid = FALSE;
2171 npbcdim = ePBC2npbcdim(ePBC);
2173 if (refcoord_scaling == erscCOM)
2175 clear_rvec(comA_sc);
2176 clear_rvec(comB_sc);
2177 for (m = 0; m < npbcdim; m++)
2179 for (d = m; d < npbcdim; d++)
2181 comA_sc[m] += comA[d]*pbc->box[d][m];
2182 comB_sc[m] += comB[d]*pbc->box[d][m];
2190 for (i = 0; (i < nbonds); )
2192 type = forceatoms[i++];
2193 ai = forceatoms[i++];
2194 pr = &forceparams[type];
2196 for (m = 0; m < DIM; m++)
2198 posA = forceparams[type].posres.pos0A[m];
2199 posB = forceparams[type].posres.pos0B[m];
2202 switch (refcoord_scaling)
2206 rdist[m] = L1*posA + lambda*posB;
2207 dpdl[m] = posB - posA;
2210 /* Box relative coordinates are stored for dimensions with pbc */
2211 posA *= pbc->box[m][m];
2212 posB *= pbc->box[m][m];
2213 for (d = m+1; d < npbcdim; d++)
2215 posA += forceparams[type].posres.pos0A[d]*pbc->box[d][m];
2216 posB += forceparams[type].posres.pos0B[d]*pbc->box[d][m];
2218 ref = L1*posA + lambda*posB;
2220 dpdl[m] = posB - posA;
2223 ref = L1*comA_sc[m] + lambda*comB_sc[m];
2224 rdist[m] = L1*posA + lambda*posB;
2225 dpdl[m] = comB_sc[m] - comA_sc[m] + posB - posA;
2228 gmx_fatal(FARGS, "No such scaling method implemented");
2233 ref = L1*posA + lambda*posB;
2235 dpdl[m] = posB - posA;
2238 /* We do pbc_dx with ref+rdist,
2239 * since with only ref we can be up to half a box vector wrong.
2241 pos[m] = ref + rdist[m];
2246 pbc_dx(pbc, x[ai], pos, dx);
2250 rvec_sub(x[ai], pos, dx);
2253 for (m = 0; (m < DIM); m++)
2255 kk = L1*pr->posres.fcA[m] + lambda*pr->posres.fcB[m];
2257 vtot += 0.5*kk*dx[m]*dx[m];
2259 0.5*(pr->posres.fcB[m] - pr->posres.fcA[m])*dx[m]*dx[m]
2262 /* Here we correct for the pbc_dx which included rdist */
2266 vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm;
2274 static real low_angres(int nbonds,
2275 const t_iatom forceatoms[], const t_iparams forceparams[],
2276 const rvec x[], rvec f[], rvec fshift[],
2277 const t_pbc *pbc, const t_graph *g,
2278 real lambda, real *dvdlambda,
2281 int i, m, type, ai, aj, ak, al;
2283 real phi, cos_phi, cos_phi2, vid, vtot, dVdphi;
2284 rvec r_ij, r_kl, f_i, f_k = {0, 0, 0};
2285 real st, sth, nrij2, nrkl2, c, cij, ckl;
2288 t2 = 0; /* avoid warning with gcc-3.3. It is never used uninitialized */
2291 ak = al = 0; /* to avoid warnings */
2292 for (i = 0; i < nbonds; )
2294 type = forceatoms[i++];
2295 ai = forceatoms[i++];
2296 aj = forceatoms[i++];
2297 t1 = pbc_rvec_sub(pbc, x[aj], x[ai], r_ij); /* 3 */
2300 ak = forceatoms[i++];
2301 al = forceatoms[i++];
2302 t2 = pbc_rvec_sub(pbc, x[al], x[ak], r_kl); /* 3 */
2311 cos_phi = cos_angle(r_ij, r_kl); /* 25 */
2312 phi = acos(cos_phi); /* 10 */
2314 *dvdlambda += dopdihs_min(forceparams[type].pdihs.cpA,
2315 forceparams[type].pdihs.cpB,
2316 forceparams[type].pdihs.phiA,
2317 forceparams[type].pdihs.phiB,
2318 forceparams[type].pdihs.mult,
2319 phi, lambda, &vid, &dVdphi); /* 40 */
2323 cos_phi2 = sqr(cos_phi); /* 1 */
2326 st = -dVdphi*gmx_invsqrt(1 - cos_phi2); /* 12 */
2327 sth = st*cos_phi; /* 1 */
2328 nrij2 = iprod(r_ij, r_ij); /* 5 */
2329 nrkl2 = iprod(r_kl, r_kl); /* 5 */
2331 c = st*gmx_invsqrt(nrij2*nrkl2); /* 11 */
2332 cij = sth/nrij2; /* 10 */
2333 ckl = sth/nrkl2; /* 10 */
2335 for (m = 0; m < DIM; m++) /* 18+18 */
2337 f_i[m] = (c*r_kl[m]-cij*r_ij[m]);
2342 f_k[m] = (c*r_ij[m]-ckl*r_kl[m]);
2350 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
2353 rvec_inc(fshift[t1], f_i);
2354 rvec_dec(fshift[CENTRAL], f_i);
2359 ivec_sub(SHIFT_IVEC(g, ak), SHIFT_IVEC(g, al), dt);
2362 rvec_inc(fshift[t2], f_k);
2363 rvec_dec(fshift[CENTRAL], f_k);
2368 return vtot; /* 184 / 157 (bZAxis) total */
2371 real angres(int nbonds,
2372 const t_iatom forceatoms[], const t_iparams forceparams[],
2373 const rvec x[], rvec f[], rvec fshift[],
2374 const t_pbc *pbc, const t_graph *g,
2375 real lambda, real *dvdlambda,
2376 const t_mdatoms *md, t_fcdata *fcd,
2377 int *global_atom_index)
2379 return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2380 lambda, dvdlambda, FALSE);
2383 real angresz(int nbonds,
2384 const t_iatom forceatoms[], const t_iparams forceparams[],
2385 const rvec x[], rvec f[], rvec fshift[],
2386 const t_pbc *pbc, const t_graph *g,
2387 real lambda, real *dvdlambda,
2388 const t_mdatoms *md, t_fcdata *fcd,
2389 int *global_atom_index)
2391 return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2392 lambda, dvdlambda, TRUE);
2395 real dihres(int nbonds,
2396 const t_iatom forceatoms[], const t_iparams forceparams[],
2397 const rvec x[], rvec f[], rvec fshift[],
2398 const t_pbc *pbc, const t_graph *g,
2399 real lambda, real *dvdlambda,
2400 const t_mdatoms *md, t_fcdata *fcd,
2401 int *global_atom_index)
2404 int ai, aj, ak, al, i, k, type, t1, t2, t3;
2405 real phi0A, phi0B, dphiA, dphiB, kfacA, kfacB, phi0, dphi, kfac;
2406 real phi, ddphi, ddp, ddp2, dp, sign, d2r, fc, L1;
2407 rvec r_ij, r_kj, r_kl, m, n;
2414 for (i = 0; (i < nbonds); )
2416 type = forceatoms[i++];
2417 ai = forceatoms[i++];
2418 aj = forceatoms[i++];
2419 ak = forceatoms[i++];
2420 al = forceatoms[i++];
2422 phi0A = forceparams[type].dihres.phiA*d2r;
2423 dphiA = forceparams[type].dihres.dphiA*d2r;
2424 kfacA = forceparams[type].dihres.kfacA;
2426 phi0B = forceparams[type].dihres.phiB*d2r;
2427 dphiB = forceparams[type].dihres.dphiB*d2r;
2428 kfacB = forceparams[type].dihres.kfacB;
2430 phi0 = L1*phi0A + lambda*phi0B;
2431 dphi = L1*dphiA + lambda*dphiB;
2432 kfac = L1*kfacA + lambda*kfacB;
2434 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2435 &sign, &t1, &t2, &t3);
2440 fprintf(debug, "dihres[%d]: %d %d %d %d : phi=%f, dphi=%f, kfac=%f\n",
2441 k++, ai, aj, ak, al, phi0, dphi, kfac);
2443 /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2444 * force changes if we just apply a normal harmonic.
2445 * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2446 * This means we will never have the periodicity problem, unless
2447 * the dihedral is Pi away from phiO, which is very unlikely due to
2451 make_dp_periodic(&dp);
2457 else if (dp < -dphi)
2469 vtot += 0.5*kfac*ddp2;
2472 *dvdlambda += 0.5*(kfacB - kfacA)*ddp2;
2473 /* lambda dependence from changing restraint distances */
2476 *dvdlambda -= kfac*ddp*((dphiB - dphiA)+(phi0B - phi0A));
2480 *dvdlambda += kfac*ddp*((dphiB - dphiA)-(phi0B - phi0A));
2482 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
2483 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2490 real unimplemented(int nbonds,
2491 const t_iatom forceatoms[], const t_iparams forceparams[],
2492 const rvec x[], rvec f[], rvec fshift[],
2493 const t_pbc *pbc, const t_graph *g,
2494 real lambda, real *dvdlambda,
2495 const t_mdatoms *md, t_fcdata *fcd,
2496 int *global_atom_index)
2498 gmx_impl("*** you are using a not implemented function");
2500 return 0.0; /* To make the compiler happy */
2503 real rbdihs(int nbonds,
2504 const t_iatom forceatoms[], const t_iparams forceparams[],
2505 const rvec x[], rvec f[], rvec fshift[],
2506 const t_pbc *pbc, const t_graph *g,
2507 real lambda, real *dvdlambda,
2508 const t_mdatoms *md, t_fcdata *fcd,
2509 int *global_atom_index)
2511 const real c0 = 0.0, c1 = 1.0, c2 = 2.0, c3 = 3.0, c4 = 4.0, c5 = 5.0;
2512 int type, ai, aj, ak, al, i, j;
2514 rvec r_ij, r_kj, r_kl, m, n;
2515 real parmA[NR_RBDIHS];
2516 real parmB[NR_RBDIHS];
2517 real parm[NR_RBDIHS];
2518 real cos_phi, phi, rbp, rbpBA;
2519 real v, sign, ddphi, sin_phi;
2521 real L1 = 1.0-lambda;
2525 for (i = 0; (i < nbonds); )
2527 type = forceatoms[i++];
2528 ai = forceatoms[i++];
2529 aj = forceatoms[i++];
2530 ak = forceatoms[i++];
2531 al = forceatoms[i++];
2533 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2534 &sign, &t1, &t2, &t3); /* 84 */
2536 /* Change to polymer convention */
2543 phi -= M_PI; /* 1 */
2547 /* Beware of accuracy loss, cannot use 1-sqrt(cos^2) ! */
2550 for (j = 0; (j < NR_RBDIHS); j++)
2552 parmA[j] = forceparams[type].rbdihs.rbcA[j];
2553 parmB[j] = forceparams[type].rbdihs.rbcB[j];
2554 parm[j] = L1*parmA[j]+lambda*parmB[j];
2556 /* Calculate cosine powers */
2557 /* Calculate the energy */
2558 /* Calculate the derivative */
2561 dvdl_term += (parmB[0]-parmA[0]);
2566 rbpBA = parmB[1]-parmA[1];
2567 ddphi += rbp*cosfac;
2570 dvdl_term += cosfac*rbpBA;
2572 rbpBA = parmB[2]-parmA[2];
2573 ddphi += c2*rbp*cosfac;
2576 dvdl_term += cosfac*rbpBA;
2578 rbpBA = parmB[3]-parmA[3];
2579 ddphi += c3*rbp*cosfac;
2582 dvdl_term += cosfac*rbpBA;
2584 rbpBA = parmB[4]-parmA[4];
2585 ddphi += c4*rbp*cosfac;
2588 dvdl_term += cosfac*rbpBA;
2590 rbpBA = parmB[5]-parmA[5];
2591 ddphi += c5*rbp*cosfac;
2594 dvdl_term += cosfac*rbpBA;
2596 ddphi = -ddphi*sin_phi; /* 11 */
2598 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
2599 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2602 *dvdlambda += dvdl_term;
2607 int cmap_setup_grid_index(int ip, int grid_spacing, int *ipm1, int *ipp1, int *ipp2)
2613 ip = ip + grid_spacing - 1;
2615 else if (ip > grid_spacing)
2617 ip = ip - grid_spacing - 1;
2626 im1 = grid_spacing - 1;
2628 else if (ip == grid_spacing-2)
2632 else if (ip == grid_spacing-1)
2646 real cmap_dihs(int nbonds,
2647 const t_iatom forceatoms[], const t_iparams forceparams[],
2648 const gmx_cmap_t *cmap_grid,
2649 const rvec x[], rvec f[], rvec fshift[],
2650 const t_pbc *pbc, const t_graph *g,
2651 real lambda, real *dvdlambda,
2652 const t_mdatoms *md, t_fcdata *fcd,
2653 int *global_atom_index)
2655 int i, j, k, n, idx;
2656 int ai, aj, ak, al, am;
2657 int a1i, a1j, a1k, a1l, a2i, a2j, a2k, a2l;
2659 int t11, t21, t31, t12, t22, t32;
2660 int iphi1, ip1m1, ip1p1, ip1p2;
2661 int iphi2, ip2m1, ip2p1, ip2p2;
2663 int pos1, pos2, pos3, pos4, tmp;
2665 real ty[4], ty1[4], ty2[4], ty12[4], tc[16], tx[16];
2666 real phi1, psi1, cos_phi1, sin_phi1, sign1, xphi1;
2667 real phi2, psi2, cos_phi2, sin_phi2, sign2, xphi2;
2668 real dx, xx, tt, tu, e, df1, df2, ddf1, ddf2, ddf12, vtot;
2669 real ra21, rb21, rg21, rg1, rgr1, ra2r1, rb2r1, rabr1;
2670 real ra22, rb22, rg22, rg2, rgr2, ra2r2, rb2r2, rabr2;
2671 real fg1, hg1, fga1, hgb1, gaa1, gbb1;
2672 real fg2, hg2, fga2, hgb2, gaa2, gbb2;
2675 rvec r1_ij, r1_kj, r1_kl, m1, n1;
2676 rvec r2_ij, r2_kj, r2_kl, m2, n2;
2677 rvec f1_i, f1_j, f1_k, f1_l;
2678 rvec f2_i, f2_j, f2_k, f2_l;
2679 rvec a1, b1, a2, b2;
2680 rvec f1, g1, h1, f2, g2, h2;
2681 rvec dtf1, dtg1, dth1, dtf2, dtg2, dth2;
2682 ivec jt1, dt1_ij, dt1_kj, dt1_lj;
2683 ivec jt2, dt2_ij, dt2_kj, dt2_lj;
2687 int loop_index[4][4] = {
2694 /* Total CMAP energy */
2697 for (n = 0; n < nbonds; )
2699 /* Five atoms are involved in the two torsions */
2700 type = forceatoms[n++];
2701 ai = forceatoms[n++];
2702 aj = forceatoms[n++];
2703 ak = forceatoms[n++];
2704 al = forceatoms[n++];
2705 am = forceatoms[n++];
2707 /* Which CMAP type is this */
2708 cmapA = forceparams[type].cmap.cmapA;
2709 cmapd = cmap_grid->cmapdata[cmapA].cmap;
2717 phi1 = dih_angle(x[a1i], x[a1j], x[a1k], x[a1l], pbc, r1_ij, r1_kj, r1_kl, m1, n1,
2718 &sign1, &t11, &t21, &t31); /* 84 */
2720 cos_phi1 = cos(phi1);
2722 a1[0] = r1_ij[1]*r1_kj[2]-r1_ij[2]*r1_kj[1];
2723 a1[1] = r1_ij[2]*r1_kj[0]-r1_ij[0]*r1_kj[2];
2724 a1[2] = r1_ij[0]*r1_kj[1]-r1_ij[1]*r1_kj[0]; /* 9 */
2726 b1[0] = r1_kl[1]*r1_kj[2]-r1_kl[2]*r1_kj[1];
2727 b1[1] = r1_kl[2]*r1_kj[0]-r1_kl[0]*r1_kj[2];
2728 b1[2] = r1_kl[0]*r1_kj[1]-r1_kl[1]*r1_kj[0]; /* 9 */
2730 tmp = pbc_rvec_sub(pbc, x[a1l], x[a1k], h1);
2732 ra21 = iprod(a1, a1); /* 5 */
2733 rb21 = iprod(b1, b1); /* 5 */
2734 rg21 = iprod(r1_kj, r1_kj); /* 5 */
2740 rabr1 = sqrt(ra2r1*rb2r1);
2742 sin_phi1 = rg1 * rabr1 * iprod(a1, h1) * (-1);
2744 if (cos_phi1 < -0.5 || cos_phi1 > 0.5)
2746 phi1 = asin(sin_phi1);
2756 phi1 = -M_PI - phi1;
2762 phi1 = acos(cos_phi1);
2770 xphi1 = phi1 + M_PI; /* 1 */
2772 /* Second torsion */
2778 phi2 = dih_angle(x[a2i], x[a2j], x[a2k], x[a2l], pbc, r2_ij, r2_kj, r2_kl, m2, n2,
2779 &sign2, &t12, &t22, &t32); /* 84 */
2781 cos_phi2 = cos(phi2);
2783 a2[0] = r2_ij[1]*r2_kj[2]-r2_ij[2]*r2_kj[1];
2784 a2[1] = r2_ij[2]*r2_kj[0]-r2_ij[0]*r2_kj[2];
2785 a2[2] = r2_ij[0]*r2_kj[1]-r2_ij[1]*r2_kj[0]; /* 9 */
2787 b2[0] = r2_kl[1]*r2_kj[2]-r2_kl[2]*r2_kj[1];
2788 b2[1] = r2_kl[2]*r2_kj[0]-r2_kl[0]*r2_kj[2];
2789 b2[2] = r2_kl[0]*r2_kj[1]-r2_kl[1]*r2_kj[0]; /* 9 */
2791 tmp = pbc_rvec_sub(pbc, x[a2l], x[a2k], h2);
2793 ra22 = iprod(a2, a2); /* 5 */
2794 rb22 = iprod(b2, b2); /* 5 */
2795 rg22 = iprod(r2_kj, r2_kj); /* 5 */
2801 rabr2 = sqrt(ra2r2*rb2r2);
2803 sin_phi2 = rg2 * rabr2 * iprod(a2, h2) * (-1);
2805 if (cos_phi2 < -0.5 || cos_phi2 > 0.5)
2807 phi2 = asin(sin_phi2);
2817 phi2 = -M_PI - phi2;
2823 phi2 = acos(cos_phi2);
2831 xphi2 = phi2 + M_PI; /* 1 */
2833 /* Range mangling */
2836 xphi1 = xphi1 + 2*M_PI;
2838 else if (xphi1 >= 2*M_PI)
2840 xphi1 = xphi1 - 2*M_PI;
2845 xphi2 = xphi2 + 2*M_PI;
2847 else if (xphi2 >= 2*M_PI)
2849 xphi2 = xphi2 - 2*M_PI;
2852 /* Number of grid points */
2853 dx = 2*M_PI / cmap_grid->grid_spacing;
2855 /* Where on the grid are we */
2856 iphi1 = (int)(xphi1/dx);
2857 iphi2 = (int)(xphi2/dx);
2859 iphi1 = cmap_setup_grid_index(iphi1, cmap_grid->grid_spacing, &ip1m1, &ip1p1, &ip1p2);
2860 iphi2 = cmap_setup_grid_index(iphi2, cmap_grid->grid_spacing, &ip2m1, &ip2p1, &ip2p2);
2862 pos1 = iphi1*cmap_grid->grid_spacing+iphi2;
2863 pos2 = ip1p1*cmap_grid->grid_spacing+iphi2;
2864 pos3 = ip1p1*cmap_grid->grid_spacing+ip2p1;
2865 pos4 = iphi1*cmap_grid->grid_spacing+ip2p1;
2867 ty[0] = cmapd[pos1*4];
2868 ty[1] = cmapd[pos2*4];
2869 ty[2] = cmapd[pos3*4];
2870 ty[3] = cmapd[pos4*4];
2872 ty1[0] = cmapd[pos1*4+1];
2873 ty1[1] = cmapd[pos2*4+1];
2874 ty1[2] = cmapd[pos3*4+1];
2875 ty1[3] = cmapd[pos4*4+1];
2877 ty2[0] = cmapd[pos1*4+2];
2878 ty2[1] = cmapd[pos2*4+2];
2879 ty2[2] = cmapd[pos3*4+2];
2880 ty2[3] = cmapd[pos4*4+2];
2882 ty12[0] = cmapd[pos1*4+3];
2883 ty12[1] = cmapd[pos2*4+3];
2884 ty12[2] = cmapd[pos3*4+3];
2885 ty12[3] = cmapd[pos4*4+3];
2887 /* Switch to degrees */
2888 dx = 360.0 / cmap_grid->grid_spacing;
2889 xphi1 = xphi1 * RAD2DEG;
2890 xphi2 = xphi2 * RAD2DEG;
2892 for (i = 0; i < 4; i++) /* 16 */
2895 tx[i+4] = ty1[i]*dx;
2896 tx[i+8] = ty2[i]*dx;
2897 tx[i+12] = ty12[i]*dx*dx;
2901 for (i = 0; i < 4; i++) /* 1056 */
2903 for (j = 0; j < 4; j++)
2906 for (k = 0; k < 16; k++)
2908 xx = xx + cmap_coeff_matrix[k*16+idx]*tx[k];
2916 tt = (xphi1-iphi1*dx)/dx;
2917 tu = (xphi2-iphi2*dx)/dx;
2926 for (i = 3; i >= 0; i--)
2928 l1 = loop_index[i][3];
2929 l2 = loop_index[i][2];
2930 l3 = loop_index[i][1];
2932 e = tt * e + ((tc[i*4+3]*tu+tc[i*4+2])*tu + tc[i*4+1])*tu+tc[i*4];
2933 df1 = tu * df1 + (3.0*tc[l1]*tt+2.0*tc[l2])*tt+tc[l3];
2934 df2 = tt * df2 + (3.0*tc[i*4+3]*tu+2.0*tc[i*4+2])*tu+tc[i*4+1];
2935 ddf1 = tu * ddf1 + 2.0*3.0*tc[l1]*tt+2.0*tc[l2];
2936 ddf2 = tt * ddf2 + 2.0*3.0*tc[4*i+3]*tu+2.0*tc[4*i+2];
2939 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) +
2940 3.0*tu*tu*(tc[7]+2.0*tc[11]*tt+3.0*tc[15]*tt*tt);
2945 ddf1 = ddf1 * fac * fac;
2946 ddf2 = ddf2 * fac * fac;
2947 ddf12 = ddf12 * fac * fac;
2952 /* Do forces - first torsion */
2953 fg1 = iprod(r1_ij, r1_kj);
2954 hg1 = iprod(r1_kl, r1_kj);
2955 fga1 = fg1*ra2r1*rgr1;
2956 hgb1 = hg1*rb2r1*rgr1;
2960 for (i = 0; i < DIM; i++)
2962 dtf1[i] = gaa1 * a1[i];
2963 dtg1[i] = fga1 * a1[i] - hgb1 * b1[i];
2964 dth1[i] = gbb1 * b1[i];
2966 f1[i] = df1 * dtf1[i];
2967 g1[i] = df1 * dtg1[i];
2968 h1[i] = df1 * dth1[i];
2971 f1_j[i] = -f1[i] - g1[i];
2972 f1_k[i] = h1[i] + g1[i];
2975 f[a1i][i] = f[a1i][i] + f1_i[i];
2976 f[a1j][i] = f[a1j][i] + f1_j[i]; /* - f1[i] - g1[i] */
2977 f[a1k][i] = f[a1k][i] + f1_k[i]; /* h1[i] + g1[i] */
2978 f[a1l][i] = f[a1l][i] + f1_l[i]; /* h1[i] */
2981 /* Do forces - second torsion */
2982 fg2 = iprod(r2_ij, r2_kj);
2983 hg2 = iprod(r2_kl, r2_kj);
2984 fga2 = fg2*ra2r2*rgr2;
2985 hgb2 = hg2*rb2r2*rgr2;
2989 for (i = 0; i < DIM; i++)
2991 dtf2[i] = gaa2 * a2[i];
2992 dtg2[i] = fga2 * a2[i] - hgb2 * b2[i];
2993 dth2[i] = gbb2 * b2[i];
2995 f2[i] = df2 * dtf2[i];
2996 g2[i] = df2 * dtg2[i];
2997 h2[i] = df2 * dth2[i];
3000 f2_j[i] = -f2[i] - g2[i];
3001 f2_k[i] = h2[i] + g2[i];
3004 f[a2i][i] = f[a2i][i] + f2_i[i]; /* f2[i] */
3005 f[a2j][i] = f[a2j][i] + f2_j[i]; /* - f2[i] - g2[i] */
3006 f[a2k][i] = f[a2k][i] + f2_k[i]; /* h2[i] + g2[i] */
3007 f[a2l][i] = f[a2l][i] + f2_l[i]; /* - h2[i] */
3013 copy_ivec(SHIFT_IVEC(g, a1j), jt1);
3014 ivec_sub(SHIFT_IVEC(g, a1i), jt1, dt1_ij);
3015 ivec_sub(SHIFT_IVEC(g, a1k), jt1, dt1_kj);
3016 ivec_sub(SHIFT_IVEC(g, a1l), jt1, dt1_lj);
3017 t11 = IVEC2IS(dt1_ij);
3018 t21 = IVEC2IS(dt1_kj);
3019 t31 = IVEC2IS(dt1_lj);
3021 copy_ivec(SHIFT_IVEC(g, a2j), jt2);
3022 ivec_sub(SHIFT_IVEC(g, a2i), jt2, dt2_ij);
3023 ivec_sub(SHIFT_IVEC(g, a2k), jt2, dt2_kj);
3024 ivec_sub(SHIFT_IVEC(g, a2l), jt2, dt2_lj);
3025 t12 = IVEC2IS(dt2_ij);
3026 t22 = IVEC2IS(dt2_kj);
3027 t32 = IVEC2IS(dt2_lj);
3031 t31 = pbc_rvec_sub(pbc, x[a1l], x[a1j], h1);
3032 t32 = pbc_rvec_sub(pbc, x[a2l], x[a2j], h2);
3040 rvec_inc(fshift[t11], f1_i);
3041 rvec_inc(fshift[CENTRAL], f1_j);
3042 rvec_inc(fshift[t21], f1_k);
3043 rvec_inc(fshift[t31], f1_l);
3045 rvec_inc(fshift[t21], f2_i);
3046 rvec_inc(fshift[CENTRAL], f2_j);
3047 rvec_inc(fshift[t22], f2_k);
3048 rvec_inc(fshift[t32], f2_l);
3055 /***********************************************************
3057 * G R O M O S 9 6 F U N C T I O N S
3059 ***********************************************************/
3060 real g96harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
3063 const real half = 0.5;
3064 real L1, kk, x0, dx, dx2;
3065 real v, f, dvdlambda;
3068 kk = L1*kA+lambda*kB;
3069 x0 = L1*xA+lambda*xB;
3076 dvdlambda = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
3083 /* That was 21 flops */
3086 real g96bonds(int nbonds,
3087 const t_iatom forceatoms[], const t_iparams forceparams[],
3088 const rvec x[], rvec f[], rvec fshift[],
3089 const t_pbc *pbc, const t_graph *g,
3090 real lambda, real *dvdlambda,
3091 const t_mdatoms *md, t_fcdata *fcd,
3092 int *global_atom_index)
3094 int i, m, ki, ai, aj, type;
3095 real dr2, fbond, vbond, fij, vtot;
3100 for (i = 0; (i < nbonds); )
3102 type = forceatoms[i++];
3103 ai = forceatoms[i++];
3104 aj = forceatoms[i++];
3106 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
3107 dr2 = iprod(dx, dx); /* 5 */
3109 *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3110 forceparams[type].harmonic.krB,
3111 forceparams[type].harmonic.rA,
3112 forceparams[type].harmonic.rB,
3113 dr2, lambda, &vbond, &fbond);
3115 vtot += 0.5*vbond; /* 1*/
3119 fprintf(debug, "G96-BONDS: dr = %10g vbond = %10g fbond = %10g\n",
3120 sqrt(dr2), vbond, fbond);
3126 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3129 for (m = 0; (m < DIM); m++) /* 15 */
3134 fshift[ki][m] += fij;
3135 fshift[CENTRAL][m] -= fij;
3141 real g96bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
3142 rvec r_ij, rvec r_kj,
3144 /* Return value is the angle between the bonds i-j and j-k */
3148 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
3149 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
3151 costh = cos_angle(r_ij, r_kj); /* 25 */
3156 real g96angles(int nbonds,
3157 const t_iatom forceatoms[], const t_iparams forceparams[],
3158 const rvec x[], rvec f[], rvec fshift[],
3159 const t_pbc *pbc, const t_graph *g,
3160 real lambda, real *dvdlambda,
3161 const t_mdatoms *md, t_fcdata *fcd,
3162 int *global_atom_index)
3164 int i, ai, aj, ak, type, m, t1, t2;
3166 real cos_theta, dVdt, va, vtot;
3167 real rij_1, rij_2, rkj_1, rkj_2, rijrkj_1;
3169 ivec jt, dt_ij, dt_kj;
3172 for (i = 0; (i < nbonds); )
3174 type = forceatoms[i++];
3175 ai = forceatoms[i++];
3176 aj = forceatoms[i++];
3177 ak = forceatoms[i++];
3179 cos_theta = g96bond_angle(x[ai], x[aj], x[ak], pbc, r_ij, r_kj, &t1, &t2);
3181 *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3182 forceparams[type].harmonic.krB,
3183 forceparams[type].harmonic.rA,
3184 forceparams[type].harmonic.rB,
3185 cos_theta, lambda, &va, &dVdt);
3188 rij_1 = gmx_invsqrt(iprod(r_ij, r_ij));
3189 rkj_1 = gmx_invsqrt(iprod(r_kj, r_kj));
3190 rij_2 = rij_1*rij_1;
3191 rkj_2 = rkj_1*rkj_1;
3192 rijrkj_1 = rij_1*rkj_1; /* 23 */
3197 fprintf(debug, "G96ANGLES: costheta = %10g vth = %10g dV/dct = %10g\n",
3198 cos_theta, va, dVdt);
3201 for (m = 0; (m < DIM); m++) /* 42 */
3203 f_i[m] = dVdt*(r_kj[m]*rijrkj_1 - r_ij[m]*rij_2*cos_theta);
3204 f_k[m] = dVdt*(r_ij[m]*rijrkj_1 - r_kj[m]*rkj_2*cos_theta);
3205 f_j[m] = -f_i[m]-f_k[m];
3213 copy_ivec(SHIFT_IVEC(g, aj), jt);
3215 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3216 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3217 t1 = IVEC2IS(dt_ij);
3218 t2 = IVEC2IS(dt_kj);
3220 rvec_inc(fshift[t1], f_i);
3221 rvec_inc(fshift[CENTRAL], f_j);
3222 rvec_inc(fshift[t2], f_k); /* 9 */
3228 real cross_bond_bond(int nbonds,
3229 const t_iatom forceatoms[], const t_iparams forceparams[],
3230 const rvec x[], rvec f[], rvec fshift[],
3231 const t_pbc *pbc, const t_graph *g,
3232 real lambda, real *dvdlambda,
3233 const t_mdatoms *md, t_fcdata *fcd,
3234 int *global_atom_index)
3236 /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3239 int i, ai, aj, ak, type, m, t1, t2;
3241 real vtot, vrr, s1, s2, r1, r2, r1e, r2e, krr;
3243 ivec jt, dt_ij, dt_kj;
3246 for (i = 0; (i < nbonds); )
3248 type = forceatoms[i++];
3249 ai = forceatoms[i++];
3250 aj = forceatoms[i++];
3251 ak = forceatoms[i++];
3252 r1e = forceparams[type].cross_bb.r1e;
3253 r2e = forceparams[type].cross_bb.r2e;
3254 krr = forceparams[type].cross_bb.krr;
3256 /* Compute distance vectors ... */
3257 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3258 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3260 /* ... and their lengths */
3264 /* Deviations from ideality */
3268 /* Energy (can be negative!) */
3273 svmul(-krr*s2/r1, r_ij, f_i);
3274 svmul(-krr*s1/r2, r_kj, f_k);
3276 for (m = 0; (m < DIM); m++) /* 12 */
3278 f_j[m] = -f_i[m] - f_k[m];
3287 copy_ivec(SHIFT_IVEC(g, aj), jt);
3289 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3290 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3291 t1 = IVEC2IS(dt_ij);
3292 t2 = IVEC2IS(dt_kj);
3294 rvec_inc(fshift[t1], f_i);
3295 rvec_inc(fshift[CENTRAL], f_j);
3296 rvec_inc(fshift[t2], f_k); /* 9 */
3302 real cross_bond_angle(int nbonds,
3303 const t_iatom forceatoms[], const t_iparams forceparams[],
3304 const rvec x[], rvec f[], rvec fshift[],
3305 const t_pbc *pbc, const t_graph *g,
3306 real lambda, real *dvdlambda,
3307 const t_mdatoms *md, t_fcdata *fcd,
3308 int *global_atom_index)
3310 /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3313 int i, ai, aj, ak, type, m, t1, t2, t3;
3314 rvec r_ij, r_kj, r_ik;
3315 real vtot, vrt, s1, s2, s3, r1, r2, r3, r1e, r2e, r3e, krt, k1, k2, k3;
3317 ivec jt, dt_ij, dt_kj;
3320 for (i = 0; (i < nbonds); )
3322 type = forceatoms[i++];
3323 ai = forceatoms[i++];
3324 aj = forceatoms[i++];
3325 ak = forceatoms[i++];
3326 r1e = forceparams[type].cross_ba.r1e;
3327 r2e = forceparams[type].cross_ba.r2e;
3328 r3e = forceparams[type].cross_ba.r3e;
3329 krt = forceparams[type].cross_ba.krt;
3331 /* Compute distance vectors ... */
3332 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3333 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3334 t3 = pbc_rvec_sub(pbc, x[ai], x[ak], r_ik);
3336 /* ... and their lengths */
3341 /* Deviations from ideality */
3346 /* Energy (can be negative!) */
3347 vrt = krt*s3*(s1+s2);
3353 k3 = -krt*(s1+s2)/r3;
3354 for (m = 0; (m < DIM); m++)
3356 f_i[m] = k1*r_ij[m] + k3*r_ik[m];
3357 f_k[m] = k2*r_kj[m] - k3*r_ik[m];
3358 f_j[m] = -f_i[m] - f_k[m];
3361 for (m = 0; (m < DIM); m++) /* 12 */
3371 copy_ivec(SHIFT_IVEC(g, aj), jt);
3373 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3374 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3375 t1 = IVEC2IS(dt_ij);
3376 t2 = IVEC2IS(dt_kj);
3378 rvec_inc(fshift[t1], f_i);
3379 rvec_inc(fshift[CENTRAL], f_j);
3380 rvec_inc(fshift[t2], f_k); /* 9 */
3386 static real bonded_tab(const char *type, int table_nr,
3387 const bondedtable_t *table, real kA, real kB, real r,
3388 real lambda, real *V, real *F)
3390 real k, tabscale, *VFtab, rt, eps, eps2, Yt, Ft, Geps, Heps2, Fp, VV, FF;
3392 real v, f, dvdlambda;
3394 k = (1.0 - lambda)*kA + lambda*kB;
3396 tabscale = table->scale;
3397 VFtab = table->data;
3403 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",
3404 type, table_nr, r, n0, n0+1, table->n);
3411 Geps = VFtab[nnn+2]*eps;
3412 Heps2 = VFtab[nnn+3]*eps2;
3413 Fp = Ft + Geps + Heps2;
3415 FF = Fp + Geps + 2.0*Heps2;
3417 *F = -k*FF*tabscale;
3419 dvdlambda = (kB - kA)*VV;
3423 /* That was 22 flops */
3426 real tab_bonds(int nbonds,
3427 const t_iatom forceatoms[], const t_iparams forceparams[],
3428 const rvec x[], rvec f[], rvec fshift[],
3429 const t_pbc *pbc, const t_graph *g,
3430 real lambda, real *dvdlambda,
3431 const t_mdatoms *md, t_fcdata *fcd,
3432 int *global_atom_index)
3434 int i, m, ki, ai, aj, type, table;
3435 real dr, dr2, fbond, vbond, fij, vtot;
3440 for (i = 0; (i < nbonds); )
3442 type = forceatoms[i++];
3443 ai = forceatoms[i++];
3444 aj = forceatoms[i++];
3446 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
3447 dr2 = iprod(dx, dx); /* 5 */
3448 dr = dr2*gmx_invsqrt(dr2); /* 10 */
3450 table = forceparams[type].tab.table;
3452 *dvdlambda += bonded_tab("bond", table,
3453 &fcd->bondtab[table],
3454 forceparams[type].tab.kA,
3455 forceparams[type].tab.kB,
3456 dr, lambda, &vbond, &fbond); /* 22 */
3464 vtot += vbond; /* 1*/
3465 fbond *= gmx_invsqrt(dr2); /* 6 */
3469 fprintf(debug, "TABBONDS: dr = %10g vbond = %10g fbond = %10g\n",
3475 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3478 for (m = 0; (m < DIM); m++) /* 15 */
3483 fshift[ki][m] += fij;
3484 fshift[CENTRAL][m] -= fij;
3490 real tab_angles(int nbonds,
3491 const t_iatom forceatoms[], const t_iparams forceparams[],
3492 const rvec x[], rvec f[], rvec fshift[],
3493 const t_pbc *pbc, const t_graph *g,
3494 real lambda, real *dvdlambda,
3495 const t_mdatoms *md, t_fcdata *fcd,
3496 int *global_atom_index)
3498 int i, ai, aj, ak, t1, t2, type, table;
3500 real cos_theta, cos_theta2, theta, dVdt, va, vtot;
3501 ivec jt, dt_ij, dt_kj;
3504 for (i = 0; (i < nbonds); )
3506 type = forceatoms[i++];
3507 ai = forceatoms[i++];
3508 aj = forceatoms[i++];
3509 ak = forceatoms[i++];
3511 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
3512 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
3514 table = forceparams[type].tab.table;
3516 *dvdlambda += bonded_tab("angle", table,
3517 &fcd->angletab[table],
3518 forceparams[type].tab.kA,
3519 forceparams[type].tab.kB,
3520 theta, lambda, &va, &dVdt); /* 22 */
3523 cos_theta2 = sqr(cos_theta); /* 1 */
3532 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
3533 sth = st*cos_theta; /* 1 */
3537 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
3538 theta*RAD2DEG, va, dVdt);
3541 nrkj2 = iprod(r_kj, r_kj); /* 5 */
3542 nrij2 = iprod(r_ij, r_ij);
3544 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
3545 cii = sth/nrij2; /* 10 */
3546 ckk = sth/nrkj2; /* 10 */
3548 for (m = 0; (m < DIM); m++) /* 39 */
3550 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
3551 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
3552 f_j[m] = -f_i[m]-f_k[m];
3559 copy_ivec(SHIFT_IVEC(g, aj), jt);
3561 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3562 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3563 t1 = IVEC2IS(dt_ij);
3564 t2 = IVEC2IS(dt_kj);
3566 rvec_inc(fshift[t1], f_i);
3567 rvec_inc(fshift[CENTRAL], f_j);
3568 rvec_inc(fshift[t2], f_k);
3574 real tab_dihs(int nbonds,
3575 const t_iatom forceatoms[], const t_iparams forceparams[],
3576 const rvec x[], rvec f[], rvec fshift[],
3577 const t_pbc *pbc, const t_graph *g,
3578 real lambda, real *dvdlambda,
3579 const t_mdatoms *md, t_fcdata *fcd,
3580 int *global_atom_index)
3582 int i, type, ai, aj, ak, al, table;
3584 rvec r_ij, r_kj, r_kl, m, n;
3585 real phi, sign, ddphi, vpd, vtot;
3588 for (i = 0; (i < nbonds); )
3590 type = forceatoms[i++];
3591 ai = forceatoms[i++];
3592 aj = forceatoms[i++];
3593 ak = forceatoms[i++];
3594 al = forceatoms[i++];
3596 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
3597 &sign, &t1, &t2, &t3); /* 84 */
3599 table = forceparams[type].tab.table;
3601 /* Hopefully phi+M_PI never results in values < 0 */
3602 *dvdlambda += bonded_tab("dihedral", table,
3603 &fcd->dihtab[table],
3604 forceparams[type].tab.kA,
3605 forceparams[type].tab.kB,
3606 phi+M_PI, lambda, &vpd, &ddphi);
3609 do_dih_fup(ai, aj, ak, al, -ddphi, r_ij, r_kj, r_kl, m, n,
3610 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
3613 fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
3614 ai, aj, ak, al, phi);
3622 calc_bonded_reduction_mask(const t_idef *idef,
3627 int ftype, nb, nat1, nb0, nb1, i, a;
3631 for (ftype = 0; ftype < F_NRE; ftype++)
3633 if (interaction_function[ftype].flags & IF_BOND &&
3634 !(ftype == F_CONNBONDS || ftype == F_POSRES) &&
3635 (ftype<F_GB12 || ftype>F_GB14))
3637 nb = idef->il[ftype].nr;
3640 nat1 = interaction_function[ftype].nratoms + 1;
3642 /* Divide this interaction equally over the threads.
3643 * This is not stored: should match division in calc_bonds.
3645 nb0 = (((nb/nat1)* t )/nt)*nat1;
3646 nb1 = (((nb/nat1)*(t+1))/nt)*nat1;
3648 for (i = nb0; i < nb1; i += nat1)
3650 for (a = 1; a < nat1; a++)
3652 mask |= (1U << (idef->il[ftype].iatoms[i+a]>>shift));
3662 void init_bonded_thread_force_reduction(t_forcerec *fr,
3665 #define MAX_BLOCK_BITS 32
3669 if (fr->nthreads <= 1)
3676 /* We divide the force array in a maximum of 32 blocks.
3677 * Minimum force block reduction size is 2^6=64.
3680 while (fr->natoms_force > (int)(MAX_BLOCK_BITS*(1U<<fr->red_ashift)))
3686 fprintf(debug, "bonded force buffer block atom shift %d bits\n",
3690 /* Determine to which blocks each thread's bonded force calculation
3691 * contributes. Store this is a mask for each thread.
3693 #pragma omp parallel for num_threads(fr->nthreads) schedule(static)
3694 for (t = 1; t < fr->nthreads; t++)
3696 fr->f_t[t].red_mask =
3697 calc_bonded_reduction_mask(idef, fr->red_ashift, t, fr->nthreads);
3700 /* Determine the maximum number of blocks we need to reduce over */
3703 for (t = 0; t < fr->nthreads; t++)
3706 for (b = 0; b < MAX_BLOCK_BITS; b++)
3708 if (fr->f_t[t].red_mask & (1U<<b))
3710 fr->red_nblock = max(fr->red_nblock, b+1);
3716 fprintf(debug, "thread %d flags %x count %d\n",
3717 t, fr->f_t[t].red_mask, c);
3723 fprintf(debug, "Number of blocks to reduce: %d of size %d\n",
3724 fr->red_nblock, 1<<fr->red_ashift);
3725 fprintf(debug, "Reduction density %.2f density/#thread %.2f\n",
3726 ctot*(1<<fr->red_ashift)/(double)fr->natoms_force,
3727 ctot*(1<<fr->red_ashift)/(double)(fr->natoms_force*fr->nthreads));
3731 static void zero_thread_forces(f_thread_t *f_t, int n,
3732 int nblock, int blocksize)
3734 int b, a0, a1, a, i, j;
3736 if (n > f_t->f_nalloc)
3738 f_t->f_nalloc = over_alloc_large(n);
3739 srenew(f_t->f, f_t->f_nalloc);
3742 if (f_t->red_mask != 0)
3744 for (b = 0; b < nblock; b++)
3746 if (f_t->red_mask && (1U<<b))
3749 a1 = min((b+1)*blocksize, n);
3750 for (a = a0; a < a1; a++)
3752 clear_rvec(f_t->f[a]);
3757 for (i = 0; i < SHIFTS; i++)
3759 clear_rvec(f_t->fshift[i]);
3761 for (i = 0; i < F_NRE; i++)
3765 for (i = 0; i < egNR; i++)
3767 for (j = 0; j < f_t->grpp.nener; j++)
3769 f_t->grpp.ener[i][j] = 0;
3772 for (i = 0; i < efptNR; i++)
3778 static void reduce_thread_force_buffer(int n, rvec *f,
3779 int nthreads, f_thread_t *f_t,
3780 int nblock, int block_size)
3782 /* The max thread number is arbitrary,
3783 * we used a fixed number to avoid memory management.
3784 * Using more than 16 threads is probably never useful performance wise.
3786 #define MAX_BONDED_THREADS 256
3789 if (nthreads > MAX_BONDED_THREADS)
3791 gmx_fatal(FARGS, "Can not reduce bonded forces on more than %d threads",
3792 MAX_BONDED_THREADS);
3795 /* This reduction can run on any number of threads,
3796 * independently of nthreads.
3798 #pragma omp parallel for num_threads(nthreads) schedule(static)
3799 for (b = 0; b < nblock; b++)
3801 rvec *fp[MAX_BONDED_THREADS];
3805 /* Determine which threads contribute to this block */
3807 for (ft = 1; ft < nthreads; ft++)
3809 if (f_t[ft].red_mask & (1U<<b))
3811 fp[nfb++] = f_t[ft].f;
3816 /* Reduce force buffers for threads that contribute */
3818 a1 = (b+1)*block_size;
3820 for (a = a0; a < a1; a++)
3822 for (fb = 0; fb < nfb; fb++)
3824 rvec_inc(f[a], fp[fb][a]);
3831 static void reduce_thread_forces(int n, rvec *f, rvec *fshift,
3832 real *ener, gmx_grppairener_t *grpp, real *dvdl,
3833 int nthreads, f_thread_t *f_t,
3834 int nblock, int block_size,
3835 gmx_bool bCalcEnerVir,
3840 /* Reduce the bonded force buffer */
3841 reduce_thread_force_buffer(n, f, nthreads, f_t, nblock, block_size);
3844 /* When necessary, reduce energy and virial using one thread only */
3849 for (i = 0; i < SHIFTS; i++)
3851 for (t = 1; t < nthreads; t++)
3853 rvec_inc(fshift[i], f_t[t].fshift[i]);
3856 for (i = 0; i < F_NRE; i++)
3858 for (t = 1; t < nthreads; t++)
3860 ener[i] += f_t[t].ener[i];
3863 for (i = 0; i < egNR; i++)
3865 for (j = 0; j < f_t[1].grpp.nener; j++)
3867 for (t = 1; t < nthreads; t++)
3870 grpp->ener[i][j] += f_t[t].grpp.ener[i][j];
3876 for (i = 0; i < efptNR; i++)
3879 for (t = 1; t < nthreads; t++)
3881 dvdl[i] += f_t[t].dvdl[i];
3888 static real calc_one_bond(FILE *fplog, int thread,
3889 int ftype, const t_idef *idef,
3890 rvec x[], rvec f[], rvec fshift[],
3892 const t_pbc *pbc, const t_graph *g,
3893 gmx_enerdata_t *enerd, gmx_grppairener_t *grpp,
3895 real *lambda, real *dvdl,
3896 const t_mdatoms *md, t_fcdata *fcd,
3897 gmx_bool bCalcEnerVir,
3898 int *global_atom_index, gmx_bool bPrintSepPot)
3900 int ind, nat1, nbonds, efptFTYPE;
3905 if (IS_RESTRAINT_TYPE(ftype))
3907 efptFTYPE = efptRESTRAINT;
3911 efptFTYPE = efptBONDED;
3914 if (interaction_function[ftype].flags & IF_BOND &&
3915 !(ftype == F_CONNBONDS || ftype == F_POSRES))
3917 ind = interaction_function[ftype].nrnb_ind;
3918 nat1 = interaction_function[ftype].nratoms + 1;
3919 nbonds = idef->il[ftype].nr/nat1;
3920 iatoms = idef->il[ftype].iatoms;
3922 nb0 = ((nbonds* thread )/(fr->nthreads))*nat1;
3923 nbn = ((nbonds*(thread+1))/(fr->nthreads))*nat1 - nb0;
3925 if (!IS_LISTED_LJ_C(ftype))
3927 if (ftype == F_CMAP)
3929 v = cmap_dihs(nbn, iatoms+nb0,
3930 idef->iparams, &idef->cmap_grid,
3931 (const rvec*)x, f, fshift,
3932 pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
3933 md, fcd, global_atom_index);
3936 else if (ftype == F_ANGLES &&
3937 !bCalcEnerVir && fr->efep == efepNO)
3939 /* No energies, shift forces, dvdl */
3940 angles_noener_simd(nbn, idef->il[ftype].iatoms+nb0,
3943 pbc, g, lambda[efptFTYPE], md, fcd,
3948 else if (ftype == F_PDIHS &&
3949 !bCalcEnerVir && fr->efep == efepNO)
3951 /* No energies, shift forces, dvdl */
3952 #ifndef SIMD_BONDEDS
3957 (nbn, idef->il[ftype].iatoms+nb0,
3960 pbc, g, lambda[efptFTYPE], md, fcd,
3966 v = interaction_function[ftype].ifunc(nbn, iatoms+nb0,
3968 (const rvec*)x, f, fshift,
3969 pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
3970 md, fcd, global_atom_index);
3974 fprintf(fplog, " %-23s #%4d V %12.5e dVdl %12.5e\n",
3975 interaction_function[ftype].longname,
3976 nbonds/nat1, v, lambda[efptFTYPE]);
3981 v = do_nonbonded_listed(ftype, nbn, iatoms+nb0, idef->iparams, (const rvec*)x, f, fshift,
3982 pbc, g, lambda, dvdl, md, fr, grpp, global_atom_index);
3986 fprintf(fplog, " %-5s + %-15s #%4d dVdl %12.5e\n",
3987 interaction_function[ftype].longname,
3988 interaction_function[F_LJ14].longname, nbonds/nat1, dvdl[efptVDW]);
3989 fprintf(fplog, " %-5s + %-15s #%4d dVdl %12.5e\n",
3990 interaction_function[ftype].longname,
3991 interaction_function[F_COUL14].longname, nbonds/nat1, dvdl[efptCOUL]);
3994 if (ind != -1 && thread == 0)
3996 inc_nrnb(nrnb, ind, nbonds);
4003 /* WARNING! THIS FUNCTION MUST EXACTLY TRACK THE calc
4004 function, or horrible things will happen when doing free energy
4005 calculations! In a good coding world, this would not be a
4006 different function, but for speed reasons, it needs to be made a
4007 separate function. TODO for 5.0 - figure out a way to reorganize
4008 to reduce duplication.
4011 static real calc_one_bond_foreign(FILE *fplog, int ftype, const t_idef *idef,
4012 rvec x[], rvec f[], t_forcerec *fr,
4013 const t_pbc *pbc, const t_graph *g,
4014 gmx_grppairener_t *grpp, t_nrnb *nrnb,
4015 real *lambda, real *dvdl,
4016 const t_mdatoms *md, t_fcdata *fcd,
4017 int *global_atom_index, gmx_bool bPrintSepPot)
4019 int ind, nat1, nbonds, efptFTYPE, nbonds_np;
4023 if (IS_RESTRAINT_TYPE(ftype))
4025 efptFTYPE = efptRESTRAINT;
4029 efptFTYPE = efptBONDED;
4032 if (ftype < F_GB12 || ftype > F_GB14)
4034 if (interaction_function[ftype].flags & IF_BOND &&
4035 !(ftype == F_CONNBONDS || ftype == F_POSRES))
4037 ind = interaction_function[ftype].nrnb_ind;
4038 nat1 = interaction_function[ftype].nratoms+1;
4039 nbonds_np = idef->il[ftype].nr_nonperturbed;
4040 nbonds = idef->il[ftype].nr - nbonds_np;
4041 iatoms = idef->il[ftype].iatoms + nbonds_np;
4044 if (!IS_LISTED_LJ_C(ftype))
4046 if (ftype == F_CMAP)
4048 v = cmap_dihs(nbonds, iatoms,
4049 idef->iparams, &idef->cmap_grid,
4050 (const rvec*)x, f, fr->fshift,
4051 pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]), md, fcd,
4056 v = interaction_function[ftype].ifunc(nbonds, iatoms,
4058 (const rvec*)x, f, fr->fshift,
4059 pbc, g, lambda[efptFTYPE], &dvdl[efptFTYPE],
4060 md, fcd, global_atom_index);
4065 v = do_nonbonded_listed(ftype, nbonds, iatoms,
4067 (const rvec*)x, f, fr->fshift,
4068 pbc, g, lambda, dvdl,
4069 md, fr, grpp, global_atom_index);
4073 inc_nrnb(nrnb, ind, nbonds/nat1);
4081 void calc_bonds(FILE *fplog, const gmx_multisim_t *ms,
4083 rvec x[], history_t *hist,
4084 rvec f[], t_forcerec *fr,
4085 const t_pbc *pbc, const t_graph *g,
4086 gmx_enerdata_t *enerd, t_nrnb *nrnb,
4088 const t_mdatoms *md,
4089 t_fcdata *fcd, int *global_atom_index,
4090 t_atomtypes *atype, gmx_genborn_t *born,
4092 gmx_bool bPrintSepPot, gmx_large_int_t step)
4094 gmx_bool bCalcEnerVir;
4096 real v, dvdl[efptNR], dvdl_dum[efptNR]; /* The dummy array is to have a place to store the dhdl at other values
4097 of lambda, which will be thrown away in the end*/
4098 const t_pbc *pbc_null;
4102 bCalcEnerVir = (force_flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY));
4104 for (i = 0; i < efptNR; i++)
4118 fprintf(fplog, "Step %s: bonded V and dVdl for this node\n",
4119 gmx_step_str(step, buf));
4125 p_graph(debug, "Bondage is fun", g);
4129 /* Do pre force calculation stuff which might require communication */
4130 if (idef->il[F_ORIRES].nr)
4132 enerd->term[F_ORIRESDEV] =
4133 calc_orires_dev(ms, idef->il[F_ORIRES].nr,
4134 idef->il[F_ORIRES].iatoms,
4135 idef->iparams, md, (const rvec*)x,
4136 pbc_null, fcd, hist);
4138 if (idef->il[F_DISRES].nr)
4140 calc_disres_R_6(ms, idef->il[F_DISRES].nr,
4141 idef->il[F_DISRES].iatoms,
4142 idef->iparams, (const rvec*)x, pbc_null,
4146 #pragma omp parallel for num_threads(fr->nthreads) schedule(static)
4147 for (thread = 0; thread < fr->nthreads; thread++)
4149 int ftype, nbonds, ind, nat1;
4154 gmx_grppairener_t *grpp;
4160 fshift = fr->fshift;
4162 grpp = &enerd->grpp;
4167 zero_thread_forces(&fr->f_t[thread], fr->natoms_force,
4168 fr->red_nblock, 1<<fr->red_ashift);
4170 ft = fr->f_t[thread].f;
4171 fshift = fr->f_t[thread].fshift;
4172 epot = fr->f_t[thread].ener;
4173 grpp = &fr->f_t[thread].grpp;
4174 dvdlt = fr->f_t[thread].dvdl;
4176 /* Loop over all bonded force types to calculate the bonded forces */
4177 for (ftype = 0; (ftype < F_NRE); ftype++)
4179 if (idef->il[ftype].nr > 0 &&
4180 (interaction_function[ftype].flags & IF_BOND) &&
4181 (ftype < F_GB12 || ftype > F_GB14) &&
4182 !(ftype == F_CONNBONDS || ftype == F_POSRES))
4184 v = calc_one_bond(fplog, thread, ftype, idef, x,
4185 ft, fshift, fr, pbc_null, g, enerd, grpp,
4186 nrnb, lambda, dvdlt,
4187 md, fcd, bCalcEnerVir,
4188 global_atom_index, bPrintSepPot);
4193 if (fr->nthreads > 1)
4195 reduce_thread_forces(fr->natoms_force, f, fr->fshift,
4196 enerd->term, &enerd->grpp, dvdl,
4197 fr->nthreads, fr->f_t,
4198 fr->red_nblock, 1<<fr->red_ashift,
4200 force_flags & GMX_FORCE_DHDL);
4202 if (force_flags & GMX_FORCE_DHDL)
4204 for (i = 0; i < efptNR; i++)
4206 enerd->dvdl_nonlin[i] += dvdl[i];
4210 /* Copy the sum of violations for the distance restraints from fcd */
4213 enerd->term[F_DISRESVIOL] = fcd->disres.sumviol;
4218 void calc_bonds_lambda(FILE *fplog,
4222 const t_pbc *pbc, const t_graph *g,
4223 gmx_grppairener_t *grpp, real *epot, t_nrnb *nrnb,
4225 const t_mdatoms *md,
4227 int *global_atom_index)
4229 int i, ftype, nbonds_np, nbonds, ind, nat;
4231 real dvdl_dum[efptNR];
4232 rvec *f, *fshift_orig;
4233 const t_pbc *pbc_null;
4245 snew(f, fr->natoms_force);
4246 /* We want to preserve the fshift array in forcerec */
4247 fshift_orig = fr->fshift;
4248 snew(fr->fshift, SHIFTS);
4250 /* Loop over all bonded force types to calculate the bonded forces */
4251 for (ftype = 0; (ftype < F_NRE); ftype++)
4253 v = calc_one_bond_foreign(fplog, ftype, idef, x,
4254 f, fr, pbc_null, g, grpp, nrnb, lambda, dvdl_dum,
4255 md, fcd, global_atom_index, FALSE);
4260 fr->fshift = fshift_orig;