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.
48 #include "gromacs/math/units.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/legacyheaders/txtdump.h"
52 #include "gromacs/legacyheaders/ns.h"
53 #include "gromacs/legacyheaders/macros.h"
54 #include "gromacs/legacyheaders/names.h"
55 #include "gromacs/legacyheaders/disre.h"
56 #include "gromacs/legacyheaders/orires.h"
57 #include "gromacs/legacyheaders/force.h"
58 #include "gromacs/legacyheaders/nonbonded.h"
60 #include "gromacs/pbcutil/ishift.h"
61 #include "gromacs/pbcutil/mshift.h"
62 #include "gromacs/pbcutil/pbc.h"
63 #include "gromacs/simd/simd.h"
64 #include "gromacs/simd/simd_math.h"
65 #include "gromacs/simd/vector_operations.h"
66 #include "gromacs/utility/fatalerror.h"
67 #include "gromacs/utility/smalloc.h"
71 /* Find a better place for this? */
72 const int cmap_coeff_matrix[] = {
73 1, 0, -3, 2, 0, 0, 0, 0, -3, 0, 9, -6, 2, 0, -6, 4,
74 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, -9, 6, -2, 0, 6, -4,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, -6, 0, 0, -6, 4,
76 0, 0, 3, -2, 0, 0, 0, 0, 0, 0, -9, 6, 0, 0, 6, -4,
77 0, 0, 0, 0, 1, 0, -3, 2, -2, 0, 6, -4, 1, 0, -3, 2,
78 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 3, -2, 1, 0, -3, 2,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 2, 0, 0, 3, -2,
80 0, 0, 0, 0, 0, 0, 3, -2, 0, 0, -6, 4, 0, 0, 3, -2,
81 0, 1, -2, 1, 0, 0, 0, 0, 0, -3, 6, -3, 0, 2, -4, 2,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, -6, 3, 0, -2, 4, -2,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 3, 0, 0, 2, -2,
84 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 3, -3, 0, 0, -2, 2,
85 0, 0, 0, 0, 0, 1, -2, 1, 0, -2, 4, -2, 0, 1, -2, 1,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 2, -1, 0, 1, -2, 1,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, -1, 1,
88 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 2, -2, 0, 0, -1, 1
92 /* TODO This function should go and live in nonbonded.c where it is
93 really needed. Here, it only supports giving a fatal error message
95 int glatnr(int *global_atom_index, int i)
99 if (global_atom_index == NULL)
105 atnr = global_atom_index[i] + 1;
111 /* TODO This kind of code appears in many places. Consolidate it */
112 static int pbc_rvec_sub(const t_pbc *pbc, const rvec xi, const rvec xj, rvec dx)
116 return pbc_dx_aiuc(pbc, xi, xj, dx);
120 rvec_sub(xi, xj, dx);
125 #ifdef GMX_SIMD_HAVE_REAL
127 /* SIMD PBC data structure, containing 1/boxdiag and the box vectors */
129 gmx_simd_real_t inv_bzz;
130 gmx_simd_real_t inv_byy;
131 gmx_simd_real_t inv_bxx;
140 /* Set the SIMD pbc data from a normal t_pbc struct */
141 static void set_pbc_simd(const t_pbc *pbc, pbc_simd_t *pbc_simd)
146 /* Setting inv_bdiag to 0 effectively turns off PBC */
147 clear_rvec(inv_bdiag);
150 for (d = 0; d < pbc->ndim_ePBC; d++)
152 inv_bdiag[d] = 1.0/pbc->box[d][d];
156 pbc_simd->inv_bzz = gmx_simd_set1_r(inv_bdiag[ZZ]);
157 pbc_simd->inv_byy = gmx_simd_set1_r(inv_bdiag[YY]);
158 pbc_simd->inv_bxx = gmx_simd_set1_r(inv_bdiag[XX]);
162 pbc_simd->bzx = gmx_simd_set1_r(pbc->box[ZZ][XX]);
163 pbc_simd->bzy = gmx_simd_set1_r(pbc->box[ZZ][YY]);
164 pbc_simd->bzz = gmx_simd_set1_r(pbc->box[ZZ][ZZ]);
165 pbc_simd->byx = gmx_simd_set1_r(pbc->box[YY][XX]);
166 pbc_simd->byy = gmx_simd_set1_r(pbc->box[YY][YY]);
167 pbc_simd->bxx = gmx_simd_set1_r(pbc->box[XX][XX]);
171 pbc_simd->bzx = gmx_simd_setzero_r();
172 pbc_simd->bzy = gmx_simd_setzero_r();
173 pbc_simd->bzz = gmx_simd_setzero_r();
174 pbc_simd->byx = gmx_simd_setzero_r();
175 pbc_simd->byy = gmx_simd_setzero_r();
176 pbc_simd->bxx = gmx_simd_setzero_r();
180 /* Correct distance vector *dx,*dy,*dz for PBC using SIMD */
181 static gmx_inline void
182 pbc_dx_simd(gmx_simd_real_t *dx, gmx_simd_real_t *dy, gmx_simd_real_t *dz,
183 const pbc_simd_t *pbc)
187 sh = gmx_simd_round_r(gmx_simd_mul_r(*dz, pbc->inv_bzz));
188 *dx = gmx_simd_fnmadd_r(sh, pbc->bzx, *dx);
189 *dy = gmx_simd_fnmadd_r(sh, pbc->bzy, *dy);
190 *dz = gmx_simd_fnmadd_r(sh, pbc->bzz, *dz);
192 sh = gmx_simd_round_r(gmx_simd_mul_r(*dy, pbc->inv_byy));
193 *dx = gmx_simd_fnmadd_r(sh, pbc->byx, *dx);
194 *dy = gmx_simd_fnmadd_r(sh, pbc->byy, *dy);
196 sh = gmx_simd_round_r(gmx_simd_mul_r(*dx, pbc->inv_bxx));
197 *dx = gmx_simd_fnmadd_r(sh, pbc->bxx, *dx);
200 #endif /* GMX_SIMD_HAVE_REAL */
203 * Morse potential bond by Frank Everdij
205 * Three parameters needed:
207 * b0 = equilibrium distance in nm
208 * be = beta in nm^-1 (actually, it's nu_e*Sqrt(2*pi*pi*mu/D_e))
209 * cb = well depth in kJ/mol
211 * Note: the potential is referenced to be +cb at infinite separation
212 * and zero at the equilibrium distance!
215 real morse_bonds(int nbonds,
216 const t_iatom forceatoms[], const t_iparams forceparams[],
217 const rvec x[], rvec f[], rvec fshift[],
218 const t_pbc *pbc, const t_graph *g,
219 real lambda, real *dvdlambda,
220 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
221 int gmx_unused *global_atom_index)
223 const real one = 1.0;
224 const real two = 2.0;
225 real dr, dr2, temp, omtemp, cbomtemp, fbond, vbond, fij, vtot;
226 real b0, be, cb, b0A, beA, cbA, b0B, beB, cbB, L1;
228 int i, m, ki, type, ai, aj;
232 for (i = 0; (i < nbonds); )
234 type = forceatoms[i++];
235 ai = forceatoms[i++];
236 aj = forceatoms[i++];
238 b0A = forceparams[type].morse.b0A;
239 beA = forceparams[type].morse.betaA;
240 cbA = forceparams[type].morse.cbA;
242 b0B = forceparams[type].morse.b0B;
243 beB = forceparams[type].morse.betaB;
244 cbB = forceparams[type].morse.cbB;
246 L1 = one-lambda; /* 1 */
247 b0 = L1*b0A + lambda*b0B; /* 3 */
248 be = L1*beA + lambda*beB; /* 3 */
249 cb = L1*cbA + lambda*cbB; /* 3 */
251 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
252 dr2 = iprod(dx, dx); /* 5 */
253 dr = dr2*gmx_invsqrt(dr2); /* 10 */
254 temp = exp(-be*(dr-b0)); /* 12 */
258 /* bonds are constrainted. This may _not_ include bond constraints if they are lambda dependent */
259 *dvdlambda += cbB-cbA;
263 omtemp = one-temp; /* 1 */
264 cbomtemp = cb*omtemp; /* 1 */
265 vbond = cbomtemp*omtemp; /* 1 */
266 fbond = -two*be*temp*cbomtemp*gmx_invsqrt(dr2); /* 9 */
267 vtot += vbond; /* 1 */
269 *dvdlambda += (cbB - cbA) * omtemp * omtemp - (2-2*omtemp)*omtemp * cb * ((b0B-b0A)*be - (beB-beA)*(dr-b0)); /* 15 */
273 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
277 for (m = 0; (m < DIM); m++) /* 15 */
282 fshift[ki][m] += fij;
283 fshift[CENTRAL][m] -= fij;
289 real cubic_bonds(int nbonds,
290 const t_iatom forceatoms[], const t_iparams forceparams[],
291 const rvec x[], rvec f[], rvec fshift[],
292 const t_pbc *pbc, const t_graph *g,
293 real gmx_unused lambda, real gmx_unused *dvdlambda,
294 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
295 int gmx_unused *global_atom_index)
297 const real three = 3.0;
298 const real two = 2.0;
300 real dr, dr2, dist, kdist, kdist2, fbond, vbond, fij, vtot;
302 int i, m, ki, type, ai, aj;
306 for (i = 0; (i < nbonds); )
308 type = forceatoms[i++];
309 ai = forceatoms[i++];
310 aj = forceatoms[i++];
312 b0 = forceparams[type].cubic.b0;
313 kb = forceparams[type].cubic.kb;
314 kcub = forceparams[type].cubic.kcub;
316 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
317 dr2 = iprod(dx, dx); /* 5 */
324 dr = dr2*gmx_invsqrt(dr2); /* 10 */
329 vbond = kdist2 + kcub*kdist2*dist;
330 fbond = -(two*kdist + three*kdist2*kcub)/dr;
332 vtot += vbond; /* 21 */
336 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
339 for (m = 0; (m < DIM); m++) /* 15 */
344 fshift[ki][m] += fij;
345 fshift[CENTRAL][m] -= fij;
351 real FENE_bonds(int nbonds,
352 const t_iatom forceatoms[], const t_iparams forceparams[],
353 const rvec x[], rvec f[], rvec fshift[],
354 const t_pbc *pbc, const t_graph *g,
355 real gmx_unused lambda, real gmx_unused *dvdlambda,
356 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
357 int *global_atom_index)
359 const real half = 0.5;
360 const real one = 1.0;
362 real dr2, bm2, omdr2obm2, fbond, vbond, fij, vtot;
364 int i, m, ki, type, ai, aj;
368 for (i = 0; (i < nbonds); )
370 type = forceatoms[i++];
371 ai = forceatoms[i++];
372 aj = forceatoms[i++];
374 bm = forceparams[type].fene.bm;
375 kb = forceparams[type].fene.kb;
377 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
378 dr2 = iprod(dx, dx); /* 5 */
390 "r^2 (%f) >= bm^2 (%f) in FENE bond between atoms %d and %d",
392 glatnr(global_atom_index, ai),
393 glatnr(global_atom_index, aj));
396 omdr2obm2 = one - dr2/bm2;
398 vbond = -half*kb*bm2*log(omdr2obm2);
399 fbond = -kb/omdr2obm2;
401 vtot += vbond; /* 35 */
405 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
408 for (m = 0; (m < DIM); m++) /* 15 */
413 fshift[ki][m] += fij;
414 fshift[CENTRAL][m] -= fij;
420 real harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
423 const real half = 0.5;
424 real L1, kk, x0, dx, dx2;
425 real v, f, dvdlambda;
428 kk = L1*kA+lambda*kB;
429 x0 = L1*xA+lambda*xB;
436 dvdlambda = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
443 /* That was 19 flops */
447 real bonds(int nbonds,
448 const t_iatom forceatoms[], const t_iparams forceparams[],
449 const rvec x[], rvec f[], rvec fshift[],
450 const t_pbc *pbc, const t_graph *g,
451 real lambda, real *dvdlambda,
452 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
453 int gmx_unused *global_atom_index)
455 int i, m, ki, ai, aj, type;
456 real dr, dr2, fbond, vbond, fij, vtot;
461 for (i = 0; (i < nbonds); )
463 type = forceatoms[i++];
464 ai = forceatoms[i++];
465 aj = forceatoms[i++];
467 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
468 dr2 = iprod(dx, dx); /* 5 */
469 dr = dr2*gmx_invsqrt(dr2); /* 10 */
471 *dvdlambda += harmonic(forceparams[type].harmonic.krA,
472 forceparams[type].harmonic.krB,
473 forceparams[type].harmonic.rA,
474 forceparams[type].harmonic.rB,
475 dr, lambda, &vbond, &fbond); /* 19 */
483 vtot += vbond; /* 1*/
484 fbond *= gmx_invsqrt(dr2); /* 6 */
488 fprintf(debug, "BONDS: dr = %10g vbond = %10g fbond = %10g\n",
494 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
497 for (m = 0; (m < DIM); m++) /* 15 */
502 fshift[ki][m] += fij;
503 fshift[CENTRAL][m] -= fij;
509 real restraint_bonds(int nbonds,
510 const t_iatom forceatoms[], const t_iparams forceparams[],
511 const rvec x[], rvec f[], rvec fshift[],
512 const t_pbc *pbc, const t_graph *g,
513 real lambda, real *dvdlambda,
514 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
515 int gmx_unused *global_atom_index)
517 int i, m, ki, ai, aj, type;
518 real dr, dr2, fbond, vbond, fij, vtot;
520 real low, dlow, up1, dup1, up2, dup2, k, dk;
528 for (i = 0; (i < nbonds); )
530 type = forceatoms[i++];
531 ai = forceatoms[i++];
532 aj = forceatoms[i++];
534 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
535 dr2 = iprod(dx, dx); /* 5 */
536 dr = dr2*gmx_invsqrt(dr2); /* 10 */
538 low = L1*forceparams[type].restraint.lowA + lambda*forceparams[type].restraint.lowB;
539 dlow = -forceparams[type].restraint.lowA + forceparams[type].restraint.lowB;
540 up1 = L1*forceparams[type].restraint.up1A + lambda*forceparams[type].restraint.up1B;
541 dup1 = -forceparams[type].restraint.up1A + forceparams[type].restraint.up1B;
542 up2 = L1*forceparams[type].restraint.up2A + lambda*forceparams[type].restraint.up2B;
543 dup2 = -forceparams[type].restraint.up2A + forceparams[type].restraint.up2B;
544 k = L1*forceparams[type].restraint.kA + lambda*forceparams[type].restraint.kB;
545 dk = -forceparams[type].restraint.kA + forceparams[type].restraint.kB;
554 *dvdlambda += 0.5*dk*drh2 - k*dlow*drh;
567 *dvdlambda += 0.5*dk*drh2 - k*dup1*drh;
572 vbond = k*(up2 - up1)*(0.5*(up2 - up1) + drh);
573 fbond = -k*(up2 - up1);
574 *dvdlambda += dk*(up2 - up1)*(0.5*(up2 - up1) + drh)
575 + k*(dup2 - dup1)*(up2 - up1 + drh)
576 - k*(up2 - up1)*dup2;
584 vtot += vbond; /* 1*/
585 fbond *= gmx_invsqrt(dr2); /* 6 */
589 fprintf(debug, "BONDS: dr = %10g vbond = %10g fbond = %10g\n",
595 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
598 for (m = 0; (m < DIM); m++) /* 15 */
603 fshift[ki][m] += fij;
604 fshift[CENTRAL][m] -= fij;
611 real polarize(int nbonds,
612 const t_iatom forceatoms[], const t_iparams forceparams[],
613 const rvec x[], rvec f[], rvec fshift[],
614 const t_pbc *pbc, const t_graph *g,
615 real lambda, real *dvdlambda,
616 const t_mdatoms *md, t_fcdata gmx_unused *fcd,
617 int gmx_unused *global_atom_index)
619 int i, m, ki, ai, aj, type;
620 real dr, dr2, fbond, vbond, fij, vtot, ksh;
625 for (i = 0; (i < nbonds); )
627 type = forceatoms[i++];
628 ai = forceatoms[i++];
629 aj = forceatoms[i++];
630 ksh = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].polarize.alpha;
633 fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
636 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
637 dr2 = iprod(dx, dx); /* 5 */
638 dr = dr2*gmx_invsqrt(dr2); /* 10 */
640 *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /* 19 */
647 vtot += vbond; /* 1*/
648 fbond *= gmx_invsqrt(dr2); /* 6 */
652 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
655 for (m = 0; (m < DIM); m++) /* 15 */
660 fshift[ki][m] += fij;
661 fshift[CENTRAL][m] -= fij;
667 real anharm_polarize(int nbonds,
668 const t_iatom forceatoms[], const t_iparams forceparams[],
669 const rvec x[], rvec f[], rvec fshift[],
670 const t_pbc *pbc, const t_graph *g,
671 real lambda, real *dvdlambda,
672 const t_mdatoms *md, t_fcdata gmx_unused *fcd,
673 int gmx_unused *global_atom_index)
675 int i, m, ki, ai, aj, type;
676 real dr, dr2, fbond, vbond, fij, vtot, ksh, khyp, drcut, ddr, ddr3;
681 for (i = 0; (i < nbonds); )
683 type = forceatoms[i++];
684 ai = forceatoms[i++];
685 aj = forceatoms[i++];
686 ksh = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].anharm_polarize.alpha; /* 7*/
687 khyp = forceparams[type].anharm_polarize.khyp;
688 drcut = forceparams[type].anharm_polarize.drcut;
691 fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
694 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
695 dr2 = iprod(dx, dx); /* 5 */
696 dr = dr2*gmx_invsqrt(dr2); /* 10 */
698 *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /* 19 */
709 vbond += khyp*ddr*ddr3;
710 fbond -= 4*khyp*ddr3;
712 fbond *= gmx_invsqrt(dr2); /* 6 */
713 vtot += vbond; /* 1*/
717 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
720 for (m = 0; (m < DIM); m++) /* 15 */
725 fshift[ki][m] += fij;
726 fshift[CENTRAL][m] -= fij;
732 real water_pol(int nbonds,
733 const t_iatom forceatoms[], const t_iparams forceparams[],
734 const rvec x[], rvec f[], rvec gmx_unused fshift[],
735 const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
736 real gmx_unused lambda, real gmx_unused *dvdlambda,
737 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
738 int gmx_unused *global_atom_index)
740 /* This routine implements anisotropic polarizibility for water, through
741 * a shell connected to a dummy with spring constant that differ in the
742 * three spatial dimensions in the molecular frame.
744 int i, m, aO, aH1, aH2, aD, aS, type, type0, ki;
746 rvec dOH1, dOH2, dHH, dOD, dDS, nW, kk, dx, kdx, proj;
750 real vtot, fij, r_HH, r_OD, r_nW, tx, ty, tz, qS;
755 type0 = forceatoms[0];
757 qS = md->chargeA[aS];
758 kk[XX] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_x;
759 kk[YY] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_y;
760 kk[ZZ] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_z;
761 r_HH = 1.0/forceparams[type0].wpol.rHH;
764 fprintf(debug, "WPOL: qS = %10.5f aS = %5d\n", qS, aS);
765 fprintf(debug, "WPOL: kk = %10.3f %10.3f %10.3f\n",
766 kk[XX], kk[YY], kk[ZZ]);
767 fprintf(debug, "WPOL: rOH = %10.3f rHH = %10.3f rOD = %10.3f\n",
768 forceparams[type0].wpol.rOH,
769 forceparams[type0].wpol.rHH,
770 forceparams[type0].wpol.rOD);
772 for (i = 0; (i < nbonds); i += 6)
774 type = forceatoms[i];
777 gmx_fatal(FARGS, "Sorry, type = %d, type0 = %d, file = %s, line = %d",
778 type, type0, __FILE__, __LINE__);
780 aO = forceatoms[i+1];
781 aH1 = forceatoms[i+2];
782 aH2 = forceatoms[i+3];
783 aD = forceatoms[i+4];
784 aS = forceatoms[i+5];
786 /* Compute vectors describing the water frame */
787 pbc_rvec_sub(pbc, x[aH1], x[aO], dOH1);
788 pbc_rvec_sub(pbc, x[aH2], x[aO], dOH2);
789 pbc_rvec_sub(pbc, x[aH2], x[aH1], dHH);
790 pbc_rvec_sub(pbc, x[aD], x[aO], dOD);
791 ki = pbc_rvec_sub(pbc, x[aS], x[aD], dDS);
792 cprod(dOH1, dOH2, nW);
794 /* Compute inverse length of normal vector
795 * (this one could be precomputed, but I'm too lazy now)
797 r_nW = gmx_invsqrt(iprod(nW, nW));
798 /* This is for precision, but does not make a big difference,
801 r_OD = gmx_invsqrt(iprod(dOD, dOD));
803 /* Normalize the vectors in the water frame */
805 svmul(r_HH, dHH, dHH);
806 svmul(r_OD, dOD, dOD);
808 /* Compute displacement of shell along components of the vector */
809 dx[ZZ] = iprod(dDS, dOD);
810 /* Compute projection on the XY plane: dDS - dx[ZZ]*dOD */
811 for (m = 0; (m < DIM); m++)
813 proj[m] = dDS[m]-dx[ZZ]*dOD[m];
816 /*dx[XX] = iprod(dDS,nW);
817 dx[YY] = iprod(dDS,dHH);*/
818 dx[XX] = iprod(proj, nW);
819 for (m = 0; (m < DIM); m++)
821 proj[m] -= dx[XX]*nW[m];
823 dx[YY] = iprod(proj, dHH);
828 fprintf(debug, "WPOL: dx2=%10g dy2=%10g dz2=%10g sum=%10g dDS^2=%10g\n",
829 sqr(dx[XX]), sqr(dx[YY]), sqr(dx[ZZ]), iprod(dx, dx), iprod(dDS, dDS));
830 fprintf(debug, "WPOL: dHH=(%10g,%10g,%10g)\n", dHH[XX], dHH[YY], dHH[ZZ]);
831 fprintf(debug, "WPOL: dOD=(%10g,%10g,%10g), 1/r_OD = %10g\n",
832 dOD[XX], dOD[YY], dOD[ZZ], 1/r_OD);
833 fprintf(debug, "WPOL: nW =(%10g,%10g,%10g), 1/r_nW = %10g\n",
834 nW[XX], nW[YY], nW[ZZ], 1/r_nW);
835 fprintf(debug, "WPOL: dx =%10g, dy =%10g, dz =%10g\n",
836 dx[XX], dx[YY], dx[ZZ]);
837 fprintf(debug, "WPOL: dDSx=%10g, dDSy=%10g, dDSz=%10g\n",
838 dDS[XX], dDS[YY], dDS[ZZ]);
841 /* Now compute the forces and energy */
842 kdx[XX] = kk[XX]*dx[XX];
843 kdx[YY] = kk[YY]*dx[YY];
844 kdx[ZZ] = kk[ZZ]*dx[ZZ];
845 vtot += iprod(dx, kdx);
849 ivec_sub(SHIFT_IVEC(g, aS), SHIFT_IVEC(g, aD), dt);
853 for (m = 0; (m < DIM); m++)
855 /* This is a tensor operation but written out for speed */
865 fshift[ki][m] += fij;
866 fshift[CENTRAL][m] -= fij;
871 fprintf(debug, "WPOL: vwpol=%g\n", 0.5*iprod(dx, kdx));
872 fprintf(debug, "WPOL: df = (%10g, %10g, %10g)\n", df[XX], df[YY], df[ZZ]);
880 static real do_1_thole(const rvec xi, const rvec xj, rvec fi, rvec fj,
881 const t_pbc *pbc, real qq,
882 rvec fshift[], real afac)
885 real r12sq, r12_1, r12bar, v0, v1, fscal, ebar, fff;
888 t = pbc_rvec_sub(pbc, xi, xj, r12); /* 3 */
890 r12sq = iprod(r12, r12); /* 5 */
891 r12_1 = gmx_invsqrt(r12sq); /* 5 */
892 r12bar = afac/r12_1; /* 5 */
893 v0 = qq*ONE_4PI_EPS0*r12_1; /* 2 */
894 ebar = exp(-r12bar); /* 5 */
895 v1 = (1-(1+0.5*r12bar)*ebar); /* 4 */
896 fscal = ((v0*r12_1)*v1 - v0*0.5*afac*ebar*(r12bar+1))*r12_1; /* 9 */
899 fprintf(debug, "THOLE: v0 = %.3f v1 = %.3f r12= % .3f r12bar = %.3f fscal = %.3f ebar = %.3f\n", v0, v1, 1/r12_1, r12bar, fscal, ebar);
902 for (m = 0; (m < DIM); m++)
908 fshift[CENTRAL][m] -= fff;
911 return v0*v1; /* 1 */
915 real thole_pol(int nbonds,
916 const t_iatom forceatoms[], const t_iparams forceparams[],
917 const rvec x[], rvec f[], rvec fshift[],
918 const t_pbc *pbc, const t_graph gmx_unused *g,
919 real gmx_unused lambda, real gmx_unused *dvdlambda,
920 const t_mdatoms *md, t_fcdata gmx_unused *fcd,
921 int gmx_unused *global_atom_index)
923 /* Interaction between two pairs of particles with opposite charge */
924 int i, type, a1, da1, a2, da2;
925 real q1, q2, qq, a, al1, al2, afac;
927 const real minusOneOnSix = -1.0/6.0;
929 for (i = 0; (i < nbonds); )
931 type = forceatoms[i++];
932 a1 = forceatoms[i++];
933 da1 = forceatoms[i++];
934 a2 = forceatoms[i++];
935 da2 = forceatoms[i++];
936 q1 = md->chargeA[da1];
937 q2 = md->chargeA[da2];
938 a = forceparams[type].thole.a;
939 al1 = forceparams[type].thole.alpha1;
940 al2 = forceparams[type].thole.alpha2;
942 afac = a*pow(al1*al2, minusOneOnSix);
943 V += do_1_thole(x[a1], x[a2], f[a1], f[a2], pbc, qq, fshift, afac);
944 V += do_1_thole(x[da1], x[a2], f[da1], f[a2], pbc, -qq, fshift, afac);
945 V += do_1_thole(x[a1], x[da2], f[a1], f[da2], pbc, -qq, fshift, afac);
946 V += do_1_thole(x[da1], x[da2], f[da1], f[da2], pbc, qq, fshift, afac);
952 real bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
953 rvec r_ij, rvec r_kj, real *costh,
955 /* Return value is the angle between the bonds i-j and j-k */
960 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
961 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
963 *costh = cos_angle(r_ij, r_kj); /* 25 */
964 th = acos(*costh); /* 10 */
969 real angles(int nbonds,
970 const t_iatom forceatoms[], const t_iparams forceparams[],
971 const rvec x[], rvec f[], rvec fshift[],
972 const t_pbc *pbc, const t_graph *g,
973 real lambda, real *dvdlambda,
974 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
975 int gmx_unused *global_atom_index)
977 int i, ai, aj, ak, t1, t2, type;
979 real cos_theta, cos_theta2, theta, dVdt, va, vtot;
980 ivec jt, dt_ij, dt_kj;
983 for (i = 0; i < nbonds; )
985 type = forceatoms[i++];
986 ai = forceatoms[i++];
987 aj = forceatoms[i++];
988 ak = forceatoms[i++];
990 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
991 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
993 *dvdlambda += harmonic(forceparams[type].harmonic.krA,
994 forceparams[type].harmonic.krB,
995 forceparams[type].harmonic.rA*DEG2RAD,
996 forceparams[type].harmonic.rB*DEG2RAD,
997 theta, lambda, &va, &dVdt); /* 21 */
1000 cos_theta2 = sqr(cos_theta);
1007 real nrkj_1, nrij_1;
1010 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1011 sth = st*cos_theta; /* 1 */
1015 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1016 theta*RAD2DEG, va, dVdt);
1019 nrij2 = iprod(r_ij, r_ij); /* 5 */
1020 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1022 nrij_1 = gmx_invsqrt(nrij2); /* 10 */
1023 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1025 cik = st*nrij_1*nrkj_1; /* 2 */
1026 cii = sth*nrij_1*nrij_1; /* 2 */
1027 ckk = sth*nrkj_1*nrkj_1; /* 2 */
1029 for (m = 0; m < DIM; m++)
1031 f_i[m] = -(cik*r_kj[m] - cii*r_ij[m]);
1032 f_k[m] = -(cik*r_ij[m] - ckk*r_kj[m]);
1033 f_j[m] = -f_i[m] - f_k[m];
1040 copy_ivec(SHIFT_IVEC(g, aj), jt);
1042 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1043 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1044 t1 = IVEC2IS(dt_ij);
1045 t2 = IVEC2IS(dt_kj);
1047 rvec_inc(fshift[t1], f_i);
1048 rvec_inc(fshift[CENTRAL], f_j);
1049 rvec_inc(fshift[t2], f_k);
1056 #ifdef GMX_SIMD_HAVE_REAL
1058 /* As angles, but using SIMD to calculate many dihedrals at once.
1059 * This routines does not calculate energies and shift forces.
1061 static gmx_inline void
1062 angles_noener_simd(int nbonds,
1063 const t_iatom forceatoms[], const t_iparams forceparams[],
1064 const rvec x[], rvec f[],
1065 const t_pbc *pbc, const t_graph gmx_unused *g,
1066 real gmx_unused lambda,
1067 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1068 int gmx_unused *global_atom_index)
1072 int type, ai[GMX_SIMD_REAL_WIDTH], aj[GMX_SIMD_REAL_WIDTH];
1073 int ak[GMX_SIMD_REAL_WIDTH];
1074 real coeff_array[2*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *coeff;
1075 real dr_array[2*DIM*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *dr;
1076 real f_buf_array[6*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *f_buf;
1077 gmx_simd_real_t k_S, theta0_S;
1078 gmx_simd_real_t rijx_S, rijy_S, rijz_S;
1079 gmx_simd_real_t rkjx_S, rkjy_S, rkjz_S;
1080 gmx_simd_real_t one_S;
1081 gmx_simd_real_t min_one_plus_eps_S;
1082 gmx_simd_real_t rij_rkj_S;
1083 gmx_simd_real_t nrij2_S, nrij_1_S;
1084 gmx_simd_real_t nrkj2_S, nrkj_1_S;
1085 gmx_simd_real_t cos_S, invsin_S;
1086 gmx_simd_real_t theta_S;
1087 gmx_simd_real_t st_S, sth_S;
1088 gmx_simd_real_t cik_S, cii_S, ckk_S;
1089 gmx_simd_real_t f_ix_S, f_iy_S, f_iz_S;
1090 gmx_simd_real_t f_kx_S, f_ky_S, f_kz_S;
1091 pbc_simd_t pbc_simd;
1093 /* Ensure register memory alignment */
1094 coeff = gmx_simd_align_r(coeff_array);
1095 dr = gmx_simd_align_r(dr_array);
1096 f_buf = gmx_simd_align_r(f_buf_array);
1098 set_pbc_simd(pbc, &pbc_simd);
1100 one_S = gmx_simd_set1_r(1.0);
1102 /* The smallest number > -1 */
1103 min_one_plus_eps_S = gmx_simd_set1_r(-1.0 + 2*GMX_REAL_EPS);
1105 /* nbonds is the number of angles times nfa1, here we step GMX_SIMD_REAL_WIDTH angles */
1106 for (i = 0; (i < nbonds); i += GMX_SIMD_REAL_WIDTH*nfa1)
1108 /* Collect atoms for GMX_SIMD_REAL_WIDTH angles.
1109 * iu indexes into forceatoms, we should not let iu go beyond nbonds.
1112 for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
1114 type = forceatoms[iu];
1115 ai[s] = forceatoms[iu+1];
1116 aj[s] = forceatoms[iu+2];
1117 ak[s] = forceatoms[iu+3];
1119 coeff[s] = forceparams[type].harmonic.krA;
1120 coeff[GMX_SIMD_REAL_WIDTH+s] = forceparams[type].harmonic.rA*DEG2RAD;
1122 /* If you can't use pbc_dx_simd below for PBC, e.g. because
1123 * you can't round in SIMD, use pbc_rvec_sub here.
1125 /* Store the non PBC corrected distances packed and aligned */
1126 for (m = 0; m < DIM; m++)
1128 dr[s + m *GMX_SIMD_REAL_WIDTH] = x[ai[s]][m] - x[aj[s]][m];
1129 dr[s + (DIM+m)*GMX_SIMD_REAL_WIDTH] = x[ak[s]][m] - x[aj[s]][m];
1132 /* At the end fill the arrays with identical entries */
1133 if (iu + nfa1 < nbonds)
1139 k_S = gmx_simd_load_r(coeff);
1140 theta0_S = gmx_simd_load_r(coeff+GMX_SIMD_REAL_WIDTH);
1142 rijx_S = gmx_simd_load_r(dr + 0*GMX_SIMD_REAL_WIDTH);
1143 rijy_S = gmx_simd_load_r(dr + 1*GMX_SIMD_REAL_WIDTH);
1144 rijz_S = gmx_simd_load_r(dr + 2*GMX_SIMD_REAL_WIDTH);
1145 rkjx_S = gmx_simd_load_r(dr + 3*GMX_SIMD_REAL_WIDTH);
1146 rkjy_S = gmx_simd_load_r(dr + 4*GMX_SIMD_REAL_WIDTH);
1147 rkjz_S = gmx_simd_load_r(dr + 5*GMX_SIMD_REAL_WIDTH);
1149 pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, &pbc_simd);
1150 pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, &pbc_simd);
1152 rij_rkj_S = gmx_simd_iprod_r(rijx_S, rijy_S, rijz_S,
1153 rkjx_S, rkjy_S, rkjz_S);
1155 nrij2_S = gmx_simd_norm2_r(rijx_S, rijy_S, rijz_S);
1156 nrkj2_S = gmx_simd_norm2_r(rkjx_S, rkjy_S, rkjz_S);
1158 nrij_1_S = gmx_simd_invsqrt_r(nrij2_S);
1159 nrkj_1_S = gmx_simd_invsqrt_r(nrkj2_S);
1161 cos_S = gmx_simd_mul_r(rij_rkj_S, gmx_simd_mul_r(nrij_1_S, nrkj_1_S));
1163 /* To allow for 180 degrees, we take the max of cos and -1 + 1bit,
1164 * so we can safely get the 1/sin from 1/sqrt(1 - cos^2).
1165 * This also ensures that rounding errors would cause the argument
1166 * of gmx_simd_acos_r to be < -1.
1167 * Note that we do not take precautions for cos(0)=1, so the outer
1168 * atoms in an angle should not be on top of each other.
1170 cos_S = gmx_simd_max_r(cos_S, min_one_plus_eps_S);
1172 theta_S = gmx_simd_acos_r(cos_S);
1174 invsin_S = gmx_simd_invsqrt_r(gmx_simd_sub_r(one_S, gmx_simd_mul_r(cos_S, cos_S)));
1176 st_S = gmx_simd_mul_r(gmx_simd_mul_r(k_S, gmx_simd_sub_r(theta0_S, theta_S)),
1178 sth_S = gmx_simd_mul_r(st_S, cos_S);
1180 cik_S = gmx_simd_mul_r(st_S, gmx_simd_mul_r(nrij_1_S, nrkj_1_S));
1181 cii_S = gmx_simd_mul_r(sth_S, gmx_simd_mul_r(nrij_1_S, nrij_1_S));
1182 ckk_S = gmx_simd_mul_r(sth_S, gmx_simd_mul_r(nrkj_1_S, nrkj_1_S));
1184 f_ix_S = gmx_simd_mul_r(cii_S, rijx_S);
1185 f_ix_S = gmx_simd_fnmadd_r(cik_S, rkjx_S, f_ix_S);
1186 f_iy_S = gmx_simd_mul_r(cii_S, rijy_S);
1187 f_iy_S = gmx_simd_fnmadd_r(cik_S, rkjy_S, f_iy_S);
1188 f_iz_S = gmx_simd_mul_r(cii_S, rijz_S);
1189 f_iz_S = gmx_simd_fnmadd_r(cik_S, rkjz_S, f_iz_S);
1190 f_kx_S = gmx_simd_mul_r(ckk_S, rkjx_S);
1191 f_kx_S = gmx_simd_fnmadd_r(cik_S, rijx_S, f_kx_S);
1192 f_ky_S = gmx_simd_mul_r(ckk_S, rkjy_S);
1193 f_ky_S = gmx_simd_fnmadd_r(cik_S, rijy_S, f_ky_S);
1194 f_kz_S = gmx_simd_mul_r(ckk_S, rkjz_S);
1195 f_kz_S = gmx_simd_fnmadd_r(cik_S, rijz_S, f_kz_S);
1197 gmx_simd_store_r(f_buf + 0*GMX_SIMD_REAL_WIDTH, f_ix_S);
1198 gmx_simd_store_r(f_buf + 1*GMX_SIMD_REAL_WIDTH, f_iy_S);
1199 gmx_simd_store_r(f_buf + 2*GMX_SIMD_REAL_WIDTH, f_iz_S);
1200 gmx_simd_store_r(f_buf + 3*GMX_SIMD_REAL_WIDTH, f_kx_S);
1201 gmx_simd_store_r(f_buf + 4*GMX_SIMD_REAL_WIDTH, f_ky_S);
1202 gmx_simd_store_r(f_buf + 5*GMX_SIMD_REAL_WIDTH, f_kz_S);
1208 for (m = 0; m < DIM; m++)
1210 f[ai[s]][m] += f_buf[s + m*GMX_SIMD_REAL_WIDTH];
1211 f[aj[s]][m] -= f_buf[s + m*GMX_SIMD_REAL_WIDTH] + f_buf[s + (DIM+m)*GMX_SIMD_REAL_WIDTH];
1212 f[ak[s]][m] += f_buf[s + (DIM+m)*GMX_SIMD_REAL_WIDTH];
1217 while (s < GMX_SIMD_REAL_WIDTH && iu < nbonds);
1221 #endif /* GMX_SIMD_HAVE_REAL */
1223 real linear_angles(int nbonds,
1224 const t_iatom forceatoms[], const t_iparams forceparams[],
1225 const rvec x[], rvec f[], rvec fshift[],
1226 const t_pbc *pbc, const t_graph *g,
1227 real lambda, real *dvdlambda,
1228 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1229 int gmx_unused *global_atom_index)
1231 int i, m, ai, aj, ak, t1, t2, type;
1233 real L1, kA, kB, aA, aB, dr, dr2, va, vtot, a, b, klin;
1234 ivec jt, dt_ij, dt_kj;
1235 rvec r_ij, r_kj, r_ik, dx;
1239 for (i = 0; (i < nbonds); )
1241 type = forceatoms[i++];
1242 ai = forceatoms[i++];
1243 aj = forceatoms[i++];
1244 ak = forceatoms[i++];
1246 kA = forceparams[type].linangle.klinA;
1247 kB = forceparams[type].linangle.klinB;
1248 klin = L1*kA + lambda*kB;
1250 aA = forceparams[type].linangle.aA;
1251 aB = forceparams[type].linangle.aB;
1252 a = L1*aA+lambda*aB;
1255 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
1256 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
1257 rvec_sub(r_ij, r_kj, r_ik);
1260 for (m = 0; (m < DIM); m++)
1262 dr = -a * r_ij[m] - b * r_kj[m];
1267 f_j[m] = -(f_i[m]+f_k[m]);
1273 *dvdlambda += 0.5*(kB-kA)*dr2 + klin*(aB-aA)*iprod(dx, r_ik);
1279 copy_ivec(SHIFT_IVEC(g, aj), jt);
1281 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1282 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1283 t1 = IVEC2IS(dt_ij);
1284 t2 = IVEC2IS(dt_kj);
1286 rvec_inc(fshift[t1], f_i);
1287 rvec_inc(fshift[CENTRAL], f_j);
1288 rvec_inc(fshift[t2], f_k);
1293 real urey_bradley(int nbonds,
1294 const t_iatom forceatoms[], const t_iparams forceparams[],
1295 const rvec x[], rvec f[], rvec fshift[],
1296 const t_pbc *pbc, const t_graph *g,
1297 real lambda, real *dvdlambda,
1298 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1299 int gmx_unused *global_atom_index)
1301 int i, m, ai, aj, ak, t1, t2, type, ki;
1302 rvec r_ij, r_kj, r_ik;
1303 real cos_theta, cos_theta2, theta;
1304 real dVdt, va, vtot, dr, dr2, vbond, fbond, fik;
1305 real kthA, th0A, kUBA, r13A, kthB, th0B, kUBB, r13B;
1306 ivec jt, dt_ij, dt_kj, dt_ik;
1309 for (i = 0; (i < nbonds); )
1311 type = forceatoms[i++];
1312 ai = forceatoms[i++];
1313 aj = forceatoms[i++];
1314 ak = forceatoms[i++];
1315 th0A = forceparams[type].u_b.thetaA*DEG2RAD;
1316 kthA = forceparams[type].u_b.kthetaA;
1317 r13A = forceparams[type].u_b.r13A;
1318 kUBA = forceparams[type].u_b.kUBA;
1319 th0B = forceparams[type].u_b.thetaB*DEG2RAD;
1320 kthB = forceparams[type].u_b.kthetaB;
1321 r13B = forceparams[type].u_b.r13B;
1322 kUBB = forceparams[type].u_b.kUBB;
1324 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
1325 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
1327 *dvdlambda += harmonic(kthA, kthB, th0A, th0B, theta, lambda, &va, &dVdt); /* 21 */
1330 ki = pbc_rvec_sub(pbc, x[ai], x[ak], r_ik); /* 3 */
1331 dr2 = iprod(r_ik, r_ik); /* 5 */
1332 dr = dr2*gmx_invsqrt(dr2); /* 10 */
1334 *dvdlambda += harmonic(kUBA, kUBB, r13A, r13B, dr, lambda, &vbond, &fbond); /* 19 */
1336 cos_theta2 = sqr(cos_theta); /* 1 */
1344 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1345 sth = st*cos_theta; /* 1 */
1349 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1350 theta*RAD2DEG, va, dVdt);
1353 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1354 nrij2 = iprod(r_ij, r_ij);
1356 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
1357 cii = sth/nrij2; /* 10 */
1358 ckk = sth/nrkj2; /* 10 */
1360 for (m = 0; (m < DIM); m++) /* 39 */
1362 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
1363 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
1364 f_j[m] = -f_i[m]-f_k[m];
1371 copy_ivec(SHIFT_IVEC(g, aj), jt);
1373 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1374 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1375 t1 = IVEC2IS(dt_ij);
1376 t2 = IVEC2IS(dt_kj);
1378 rvec_inc(fshift[t1], f_i);
1379 rvec_inc(fshift[CENTRAL], f_j);
1380 rvec_inc(fshift[t2], f_k);
1382 /* Time for the bond calculations */
1388 vtot += vbond; /* 1*/
1389 fbond *= gmx_invsqrt(dr2); /* 6 */
1393 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, ak), dt_ik);
1394 ki = IVEC2IS(dt_ik);
1396 for (m = 0; (m < DIM); m++) /* 15 */
1398 fik = fbond*r_ik[m];
1401 fshift[ki][m] += fik;
1402 fshift[CENTRAL][m] -= fik;
1408 real quartic_angles(int nbonds,
1409 const t_iatom forceatoms[], const t_iparams forceparams[],
1410 const rvec x[], rvec f[], rvec fshift[],
1411 const t_pbc *pbc, const t_graph *g,
1412 real gmx_unused lambda, real gmx_unused *dvdlambda,
1413 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1414 int gmx_unused *global_atom_index)
1416 int i, j, ai, aj, ak, t1, t2, type;
1418 real cos_theta, cos_theta2, theta, dt, dVdt, va, dtp, c, vtot;
1419 ivec jt, dt_ij, dt_kj;
1422 for (i = 0; (i < nbonds); )
1424 type = forceatoms[i++];
1425 ai = forceatoms[i++];
1426 aj = forceatoms[i++];
1427 ak = forceatoms[i++];
1429 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
1430 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
1432 dt = theta - forceparams[type].qangle.theta*DEG2RAD; /* 2 */
1435 va = forceparams[type].qangle.c[0];
1437 for (j = 1; j <= 4; j++)
1439 c = forceparams[type].qangle.c[j];
1448 cos_theta2 = sqr(cos_theta); /* 1 */
1457 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1458 sth = st*cos_theta; /* 1 */
1462 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1463 theta*RAD2DEG, va, dVdt);
1466 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1467 nrij2 = iprod(r_ij, r_ij);
1469 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
1470 cii = sth/nrij2; /* 10 */
1471 ckk = sth/nrkj2; /* 10 */
1473 for (m = 0; (m < DIM); m++) /* 39 */
1475 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
1476 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
1477 f_j[m] = -f_i[m]-f_k[m];
1484 copy_ivec(SHIFT_IVEC(g, aj), jt);
1486 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1487 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1488 t1 = IVEC2IS(dt_ij);
1489 t2 = IVEC2IS(dt_kj);
1491 rvec_inc(fshift[t1], f_i);
1492 rvec_inc(fshift[CENTRAL], f_j);
1493 rvec_inc(fshift[t2], f_k);
1499 real dih_angle(const rvec xi, const rvec xj, const rvec xk, const rvec xl,
1501 rvec r_ij, rvec r_kj, rvec r_kl, rvec m, rvec n,
1502 real *sign, int *t1, int *t2, int *t3)
1506 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
1507 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
1508 *t3 = pbc_rvec_sub(pbc, xk, xl, r_kl); /* 3 */
1510 cprod(r_ij, r_kj, m); /* 9 */
1511 cprod(r_kj, r_kl, n); /* 9 */
1512 phi = gmx_angle(m, n); /* 49 (assuming 25 for atan2) */
1513 ipr = iprod(r_ij, n); /* 5 */
1514 (*sign) = (ipr < 0.0) ? -1.0 : 1.0;
1515 phi = (*sign)*phi; /* 1 */
1521 #ifdef GMX_SIMD_HAVE_REAL
1523 /* As dih_angle above, but calculates 4 dihedral angles at once using SIMD,
1524 * also calculates the pre-factor required for the dihedral force update.
1525 * Note that bv and buf should be register aligned.
1527 static gmx_inline void
1528 dih_angle_simd(const rvec *x,
1529 const int *ai, const int *aj, const int *ak, const int *al,
1530 const pbc_simd_t *pbc,
1532 gmx_simd_real_t *phi_S,
1533 gmx_simd_real_t *mx_S, gmx_simd_real_t *my_S, gmx_simd_real_t *mz_S,
1534 gmx_simd_real_t *nx_S, gmx_simd_real_t *ny_S, gmx_simd_real_t *nz_S,
1535 gmx_simd_real_t *nrkj_m2_S,
1536 gmx_simd_real_t *nrkj_n2_S,
1541 gmx_simd_real_t rijx_S, rijy_S, rijz_S;
1542 gmx_simd_real_t rkjx_S, rkjy_S, rkjz_S;
1543 gmx_simd_real_t rklx_S, rkly_S, rklz_S;
1544 gmx_simd_real_t cx_S, cy_S, cz_S;
1545 gmx_simd_real_t cn_S;
1546 gmx_simd_real_t s_S;
1547 gmx_simd_real_t ipr_S;
1548 gmx_simd_real_t iprm_S, iprn_S;
1549 gmx_simd_real_t nrkj2_S, nrkj_1_S, nrkj_2_S, nrkj_S;
1550 gmx_simd_real_t toler_S;
1551 gmx_simd_real_t p_S, q_S;
1552 gmx_simd_real_t nrkj2_min_S;
1553 gmx_simd_real_t real_eps_S;
1555 /* Used to avoid division by zero.
1556 * We take into acount that we multiply the result by real_eps_S.
1558 nrkj2_min_S = gmx_simd_set1_r(GMX_REAL_MIN/(2*GMX_REAL_EPS));
1560 /* The value of the last significant bit (GMX_REAL_EPS is half of that) */
1561 real_eps_S = gmx_simd_set1_r(2*GMX_REAL_EPS);
1563 for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
1565 /* If you can't use pbc_dx_simd below for PBC, e.g. because
1566 * you can't round in SIMD, use pbc_rvec_sub here.
1568 for (m = 0; m < DIM; m++)
1570 dr[s + (0*DIM + m)*GMX_SIMD_REAL_WIDTH] = x[ai[s]][m] - x[aj[s]][m];
1571 dr[s + (1*DIM + m)*GMX_SIMD_REAL_WIDTH] = x[ak[s]][m] - x[aj[s]][m];
1572 dr[s + (2*DIM + m)*GMX_SIMD_REAL_WIDTH] = x[ak[s]][m] - x[al[s]][m];
1576 rijx_S = gmx_simd_load_r(dr + 0*GMX_SIMD_REAL_WIDTH);
1577 rijy_S = gmx_simd_load_r(dr + 1*GMX_SIMD_REAL_WIDTH);
1578 rijz_S = gmx_simd_load_r(dr + 2*GMX_SIMD_REAL_WIDTH);
1579 rkjx_S = gmx_simd_load_r(dr + 3*GMX_SIMD_REAL_WIDTH);
1580 rkjy_S = gmx_simd_load_r(dr + 4*GMX_SIMD_REAL_WIDTH);
1581 rkjz_S = gmx_simd_load_r(dr + 5*GMX_SIMD_REAL_WIDTH);
1582 rklx_S = gmx_simd_load_r(dr + 6*GMX_SIMD_REAL_WIDTH);
1583 rkly_S = gmx_simd_load_r(dr + 7*GMX_SIMD_REAL_WIDTH);
1584 rklz_S = gmx_simd_load_r(dr + 8*GMX_SIMD_REAL_WIDTH);
1586 pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, pbc);
1587 pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, pbc);
1588 pbc_dx_simd(&rklx_S, &rkly_S, &rklz_S, pbc);
1590 gmx_simd_cprod_r(rijx_S, rijy_S, rijz_S,
1591 rkjx_S, rkjy_S, rkjz_S,
1594 gmx_simd_cprod_r(rkjx_S, rkjy_S, rkjz_S,
1595 rklx_S, rkly_S, rklz_S,
1598 gmx_simd_cprod_r(*mx_S, *my_S, *mz_S,
1599 *nx_S, *ny_S, *nz_S,
1600 &cx_S, &cy_S, &cz_S);
1602 cn_S = gmx_simd_sqrt_r(gmx_simd_norm2_r(cx_S, cy_S, cz_S));
1604 s_S = gmx_simd_iprod_r(*mx_S, *my_S, *mz_S, *nx_S, *ny_S, *nz_S);
1606 /* Determine the dihedral angle, the sign might need correction */
1607 *phi_S = gmx_simd_atan2_r(cn_S, s_S);
1609 ipr_S = gmx_simd_iprod_r(rijx_S, rijy_S, rijz_S,
1610 *nx_S, *ny_S, *nz_S);
1612 iprm_S = gmx_simd_norm2_r(*mx_S, *my_S, *mz_S);
1613 iprn_S = gmx_simd_norm2_r(*nx_S, *ny_S, *nz_S);
1615 nrkj2_S = gmx_simd_norm2_r(rkjx_S, rkjy_S, rkjz_S);
1617 /* Avoid division by zero. When zero, the result is multiplied by 0
1618 * anyhow, so the 3 max below do not affect the final result.
1620 nrkj2_S = gmx_simd_max_r(nrkj2_S, nrkj2_min_S);
1621 nrkj_1_S = gmx_simd_invsqrt_r(nrkj2_S);
1622 nrkj_2_S = gmx_simd_mul_r(nrkj_1_S, nrkj_1_S);
1623 nrkj_S = gmx_simd_mul_r(nrkj2_S, nrkj_1_S);
1625 toler_S = gmx_simd_mul_r(nrkj2_S, real_eps_S);
1627 /* Here the plain-C code uses a conditional, but we can't do that in SIMD.
1628 * So we take a max with the tolerance instead. Since we multiply with
1629 * m or n later, the max does not affect the results.
1631 iprm_S = gmx_simd_max_r(iprm_S, toler_S);
1632 iprn_S = gmx_simd_max_r(iprn_S, toler_S);
1633 *nrkj_m2_S = gmx_simd_mul_r(nrkj_S, gmx_simd_inv_r(iprm_S));
1634 *nrkj_n2_S = gmx_simd_mul_r(nrkj_S, gmx_simd_inv_r(iprn_S));
1636 /* Set sign of phi_S with the sign of ipr_S; phi_S is currently positive */
1637 *phi_S = gmx_simd_xor_sign_r(*phi_S, ipr_S);
1638 p_S = gmx_simd_iprod_r(rijx_S, rijy_S, rijz_S,
1639 rkjx_S, rkjy_S, rkjz_S);
1640 p_S = gmx_simd_mul_r(p_S, nrkj_2_S);
1642 q_S = gmx_simd_iprod_r(rklx_S, rkly_S, rklz_S,
1643 rkjx_S, rkjy_S, rkjz_S);
1644 q_S = gmx_simd_mul_r(q_S, nrkj_2_S);
1646 gmx_simd_store_r(p, p_S);
1647 gmx_simd_store_r(q, q_S);
1650 #endif /* GMX_SIMD_HAVE_REAL */
1653 void do_dih_fup(int i, int j, int k, int l, real ddphi,
1654 rvec r_ij, rvec r_kj, rvec r_kl,
1655 rvec m, rvec n, rvec f[], rvec fshift[],
1656 const t_pbc *pbc, const t_graph *g,
1657 const rvec x[], int t1, int t2, int t3)
1660 rvec f_i, f_j, f_k, f_l;
1661 rvec uvec, vvec, svec, dx_jl;
1662 real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1663 real a, b, p, q, toler;
1664 ivec jt, dt_ij, dt_kj, dt_lj;
1666 iprm = iprod(m, m); /* 5 */
1667 iprn = iprod(n, n); /* 5 */
1668 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1669 toler = nrkj2*GMX_REAL_EPS;
1670 if ((iprm > toler) && (iprn > toler))
1672 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1673 nrkj_2 = nrkj_1*nrkj_1; /* 1 */
1674 nrkj = nrkj2*nrkj_1; /* 1 */
1675 a = -ddphi*nrkj/iprm; /* 11 */
1676 svmul(a, m, f_i); /* 3 */
1677 b = ddphi*nrkj/iprn; /* 11 */
1678 svmul(b, n, f_l); /* 3 */
1679 p = iprod(r_ij, r_kj); /* 5 */
1680 p *= nrkj_2; /* 1 */
1681 q = iprod(r_kl, r_kj); /* 5 */
1682 q *= nrkj_2; /* 1 */
1683 svmul(p, f_i, uvec); /* 3 */
1684 svmul(q, f_l, vvec); /* 3 */
1685 rvec_sub(uvec, vvec, svec); /* 3 */
1686 rvec_sub(f_i, svec, f_j); /* 3 */
1687 rvec_add(f_l, svec, f_k); /* 3 */
1688 rvec_inc(f[i], f_i); /* 3 */
1689 rvec_dec(f[j], f_j); /* 3 */
1690 rvec_dec(f[k], f_k); /* 3 */
1691 rvec_inc(f[l], f_l); /* 3 */
1695 copy_ivec(SHIFT_IVEC(g, j), jt);
1696 ivec_sub(SHIFT_IVEC(g, i), jt, dt_ij);
1697 ivec_sub(SHIFT_IVEC(g, k), jt, dt_kj);
1698 ivec_sub(SHIFT_IVEC(g, l), jt, dt_lj);
1699 t1 = IVEC2IS(dt_ij);
1700 t2 = IVEC2IS(dt_kj);
1701 t3 = IVEC2IS(dt_lj);
1705 t3 = pbc_rvec_sub(pbc, x[l], x[j], dx_jl);
1712 rvec_inc(fshift[t1], f_i);
1713 rvec_dec(fshift[CENTRAL], f_j);
1714 rvec_dec(fshift[t2], f_k);
1715 rvec_inc(fshift[t3], f_l);
1720 /* As do_dih_fup above, but without shift forces */
1722 do_dih_fup_noshiftf(int i, int j, int k, int l, real ddphi,
1723 rvec r_ij, rvec r_kj, rvec r_kl,
1724 rvec m, rvec n, rvec f[])
1726 rvec f_i, f_j, f_k, f_l;
1727 rvec uvec, vvec, svec;
1728 real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1729 real a, b, p, q, toler;
1731 iprm = iprod(m, m); /* 5 */
1732 iprn = iprod(n, n); /* 5 */
1733 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1734 toler = nrkj2*GMX_REAL_EPS;
1735 if ((iprm > toler) && (iprn > toler))
1737 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1738 nrkj_2 = nrkj_1*nrkj_1; /* 1 */
1739 nrkj = nrkj2*nrkj_1; /* 1 */
1740 a = -ddphi*nrkj/iprm; /* 11 */
1741 svmul(a, m, f_i); /* 3 */
1742 b = ddphi*nrkj/iprn; /* 11 */
1743 svmul(b, n, f_l); /* 3 */
1744 p = iprod(r_ij, r_kj); /* 5 */
1745 p *= nrkj_2; /* 1 */
1746 q = iprod(r_kl, r_kj); /* 5 */
1747 q *= nrkj_2; /* 1 */
1748 svmul(p, f_i, uvec); /* 3 */
1749 svmul(q, f_l, vvec); /* 3 */
1750 rvec_sub(uvec, vvec, svec); /* 3 */
1751 rvec_sub(f_i, svec, f_j); /* 3 */
1752 rvec_add(f_l, svec, f_k); /* 3 */
1753 rvec_inc(f[i], f_i); /* 3 */
1754 rvec_dec(f[j], f_j); /* 3 */
1755 rvec_dec(f[k], f_k); /* 3 */
1756 rvec_inc(f[l], f_l); /* 3 */
1760 /* As do_dih_fup_noshiftf above, but with pre-calculated pre-factors */
1761 static gmx_inline void
1762 do_dih_fup_noshiftf_precalc(int i, int j, int k, int l,
1764 real f_i_x, real f_i_y, real f_i_z,
1765 real mf_l_x, real mf_l_y, real mf_l_z,
1768 rvec f_i, f_j, f_k, f_l;
1769 rvec uvec, vvec, svec;
1777 svmul(p, f_i, uvec);
1778 svmul(q, f_l, vvec);
1779 rvec_sub(uvec, vvec, svec);
1780 rvec_sub(f_i, svec, f_j);
1781 rvec_add(f_l, svec, f_k);
1782 rvec_inc(f[i], f_i);
1783 rvec_dec(f[j], f_j);
1784 rvec_dec(f[k], f_k);
1785 rvec_inc(f[l], f_l);
1789 real dopdihs(real cpA, real cpB, real phiA, real phiB, int mult,
1790 real phi, real lambda, real *V, real *F)
1792 real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1793 real L1 = 1.0 - lambda;
1794 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1795 real dph0 = (phiB - phiA)*DEG2RAD;
1796 real cp = L1*cpA + lambda*cpB;
1798 mdphi = mult*phi - ph0;
1800 ddphi = -cp*mult*sdphi;
1801 v1 = 1.0 + cos(mdphi);
1804 dvdlambda = (cpB - cpA)*v1 + cp*dph0*sdphi;
1811 /* That was 40 flops */
1815 dopdihs_noener(real cpA, real cpB, real phiA, real phiB, int mult,
1816 real phi, real lambda, real *F)
1818 real mdphi, sdphi, ddphi;
1819 real L1 = 1.0 - lambda;
1820 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1821 real cp = L1*cpA + lambda*cpB;
1823 mdphi = mult*phi - ph0;
1825 ddphi = -cp*mult*sdphi;
1829 /* That was 20 flops */
1833 dopdihs_mdphi(real cpA, real cpB, real phiA, real phiB, int mult,
1834 real phi, real lambda, real *cp, real *mdphi)
1836 real L1 = 1.0 - lambda;
1837 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1839 *cp = L1*cpA + lambda*cpB;
1841 *mdphi = mult*phi - ph0;
1844 static real dopdihs_min(real cpA, real cpB, real phiA, real phiB, int mult,
1845 real phi, real lambda, real *V, real *F)
1846 /* similar to dopdihs, except for a minus sign *
1847 * and a different treatment of mult/phi0 */
1849 real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1850 real L1 = 1.0 - lambda;
1851 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1852 real dph0 = (phiB - phiA)*DEG2RAD;
1853 real cp = L1*cpA + lambda*cpB;
1855 mdphi = mult*(phi-ph0);
1857 ddphi = cp*mult*sdphi;
1858 v1 = 1.0-cos(mdphi);
1861 dvdlambda = (cpB-cpA)*v1 + cp*dph0*sdphi;
1868 /* That was 40 flops */
1871 real pdihs(int nbonds,
1872 const t_iatom forceatoms[], const t_iparams forceparams[],
1873 const rvec x[], rvec f[], rvec fshift[],
1874 const t_pbc *pbc, const t_graph *g,
1875 real lambda, real *dvdlambda,
1876 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1877 int gmx_unused *global_atom_index)
1879 int i, type, ai, aj, ak, al;
1881 rvec r_ij, r_kj, r_kl, m, n;
1882 real phi, sign, ddphi, vpd, vtot;
1886 for (i = 0; (i < nbonds); )
1888 type = forceatoms[i++];
1889 ai = forceatoms[i++];
1890 aj = forceatoms[i++];
1891 ak = forceatoms[i++];
1892 al = forceatoms[i++];
1894 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1895 &sign, &t1, &t2, &t3); /* 84 */
1896 *dvdlambda += dopdihs(forceparams[type].pdihs.cpA,
1897 forceparams[type].pdihs.cpB,
1898 forceparams[type].pdihs.phiA,
1899 forceparams[type].pdihs.phiB,
1900 forceparams[type].pdihs.mult,
1901 phi, lambda, &vpd, &ddphi);
1904 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
1905 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
1908 fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
1909 ai, aj, ak, al, phi);
1916 void make_dp_periodic(real *dp) /* 1 flop? */
1918 /* dp cannot be outside (-pi,pi) */
1923 else if (*dp < -M_PI)
1930 /* As pdihs above, but without calculating energies and shift forces */
1932 pdihs_noener(int nbonds,
1933 const t_iatom forceatoms[], const t_iparams forceparams[],
1934 const rvec x[], rvec f[],
1935 const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
1937 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1938 int gmx_unused *global_atom_index)
1940 int i, type, ai, aj, ak, al;
1942 rvec r_ij, r_kj, r_kl, m, n;
1943 real phi, sign, ddphi_tot, ddphi;
1945 for (i = 0; (i < nbonds); )
1947 ai = forceatoms[i+1];
1948 aj = forceatoms[i+2];
1949 ak = forceatoms[i+3];
1950 al = forceatoms[i+4];
1952 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1953 &sign, &t1, &t2, &t3);
1957 /* Loop over dihedrals working on the same atoms,
1958 * so we avoid recalculating angles and force distributions.
1962 type = forceatoms[i];
1963 dopdihs_noener(forceparams[type].pdihs.cpA,
1964 forceparams[type].pdihs.cpB,
1965 forceparams[type].pdihs.phiA,
1966 forceparams[type].pdihs.phiB,
1967 forceparams[type].pdihs.mult,
1968 phi, lambda, &ddphi);
1973 while (i < nbonds &&
1974 forceatoms[i+1] == ai &&
1975 forceatoms[i+2] == aj &&
1976 forceatoms[i+3] == ak &&
1977 forceatoms[i+4] == al);
1979 do_dih_fup_noshiftf(ai, aj, ak, al, ddphi_tot, r_ij, r_kj, r_kl, m, n, f);
1984 #ifdef GMX_SIMD_HAVE_REAL
1986 /* As pdihs_noner above, but using SIMD to calculate many dihedrals at once */
1988 pdihs_noener_simd(int nbonds,
1989 const t_iatom forceatoms[], const t_iparams forceparams[],
1990 const rvec x[], rvec f[],
1991 const t_pbc *pbc, const t_graph gmx_unused *g,
1992 real gmx_unused lambda,
1993 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1994 int gmx_unused *global_atom_index)
1998 int type, ai[GMX_SIMD_REAL_WIDTH], aj[GMX_SIMD_REAL_WIDTH], ak[GMX_SIMD_REAL_WIDTH], al[GMX_SIMD_REAL_WIDTH];
1999 real dr_array[3*DIM*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *dr;
2000 real buf_array[7*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *buf;
2001 real *cp, *phi0, *mult, *p, *q;
2002 gmx_simd_real_t phi0_S, phi_S;
2003 gmx_simd_real_t mx_S, my_S, mz_S;
2004 gmx_simd_real_t nx_S, ny_S, nz_S;
2005 gmx_simd_real_t nrkj_m2_S, nrkj_n2_S;
2006 gmx_simd_real_t cp_S, mdphi_S, mult_S;
2007 gmx_simd_real_t sin_S, cos_S;
2008 gmx_simd_real_t mddphi_S;
2009 gmx_simd_real_t sf_i_S, msf_l_S;
2010 pbc_simd_t pbc_simd;
2012 /* Ensure SIMD register alignment */
2013 dr = gmx_simd_align_r(dr_array);
2014 buf = gmx_simd_align_r(buf_array);
2016 /* Extract aligned pointer for parameters and variables */
2017 cp = buf + 0*GMX_SIMD_REAL_WIDTH;
2018 phi0 = buf + 1*GMX_SIMD_REAL_WIDTH;
2019 mult = buf + 2*GMX_SIMD_REAL_WIDTH;
2020 p = buf + 3*GMX_SIMD_REAL_WIDTH;
2021 q = buf + 4*GMX_SIMD_REAL_WIDTH;
2023 set_pbc_simd(pbc, &pbc_simd);
2025 /* nbonds is the number of dihedrals times nfa1, here we step GMX_SIMD_REAL_WIDTH dihs */
2026 for (i = 0; (i < nbonds); i += GMX_SIMD_REAL_WIDTH*nfa1)
2028 /* Collect atoms quadruplets for GMX_SIMD_REAL_WIDTH dihedrals.
2029 * iu indexes into forceatoms, we should not let iu go beyond nbonds.
2032 for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
2034 type = forceatoms[iu];
2035 ai[s] = forceatoms[iu+1];
2036 aj[s] = forceatoms[iu+2];
2037 ak[s] = forceatoms[iu+3];
2038 al[s] = forceatoms[iu+4];
2040 cp[s] = forceparams[type].pdihs.cpA;
2041 phi0[s] = forceparams[type].pdihs.phiA*DEG2RAD;
2042 mult[s] = forceparams[type].pdihs.mult;
2044 /* At the end fill the arrays with identical entries */
2045 if (iu + nfa1 < nbonds)
2051 /* Caclulate GMX_SIMD_REAL_WIDTH dihedral angles at once */
2052 dih_angle_simd(x, ai, aj, ak, al, &pbc_simd,
2055 &mx_S, &my_S, &mz_S,
2056 &nx_S, &ny_S, &nz_S,
2061 cp_S = gmx_simd_load_r(cp);
2062 phi0_S = gmx_simd_load_r(phi0);
2063 mult_S = gmx_simd_load_r(mult);
2065 mdphi_S = gmx_simd_sub_r(gmx_simd_mul_r(mult_S, phi_S), phi0_S);
2067 /* Calculate GMX_SIMD_REAL_WIDTH sines at once */
2068 gmx_simd_sincos_r(mdphi_S, &sin_S, &cos_S);
2069 mddphi_S = gmx_simd_mul_r(gmx_simd_mul_r(cp_S, mult_S), sin_S);
2070 sf_i_S = gmx_simd_mul_r(mddphi_S, nrkj_m2_S);
2071 msf_l_S = gmx_simd_mul_r(mddphi_S, nrkj_n2_S);
2073 /* After this m?_S will contain f[i] */
2074 mx_S = gmx_simd_mul_r(sf_i_S, mx_S);
2075 my_S = gmx_simd_mul_r(sf_i_S, my_S);
2076 mz_S = gmx_simd_mul_r(sf_i_S, mz_S);
2078 /* After this m?_S will contain -f[l] */
2079 nx_S = gmx_simd_mul_r(msf_l_S, nx_S);
2080 ny_S = gmx_simd_mul_r(msf_l_S, ny_S);
2081 nz_S = gmx_simd_mul_r(msf_l_S, nz_S);
2083 gmx_simd_store_r(dr + 0*GMX_SIMD_REAL_WIDTH, mx_S);
2084 gmx_simd_store_r(dr + 1*GMX_SIMD_REAL_WIDTH, my_S);
2085 gmx_simd_store_r(dr + 2*GMX_SIMD_REAL_WIDTH, mz_S);
2086 gmx_simd_store_r(dr + 3*GMX_SIMD_REAL_WIDTH, nx_S);
2087 gmx_simd_store_r(dr + 4*GMX_SIMD_REAL_WIDTH, ny_S);
2088 gmx_simd_store_r(dr + 5*GMX_SIMD_REAL_WIDTH, nz_S);
2094 do_dih_fup_noshiftf_precalc(ai[s], aj[s], ak[s], al[s],
2096 dr[ XX *GMX_SIMD_REAL_WIDTH+s],
2097 dr[ YY *GMX_SIMD_REAL_WIDTH+s],
2098 dr[ ZZ *GMX_SIMD_REAL_WIDTH+s],
2099 dr[(DIM+XX)*GMX_SIMD_REAL_WIDTH+s],
2100 dr[(DIM+YY)*GMX_SIMD_REAL_WIDTH+s],
2101 dr[(DIM+ZZ)*GMX_SIMD_REAL_WIDTH+s],
2106 while (s < GMX_SIMD_REAL_WIDTH && iu < nbonds);
2110 #endif /* GMX_SIMD_HAVE_REAL */
2113 real idihs(int nbonds,
2114 const t_iatom forceatoms[], const t_iparams forceparams[],
2115 const rvec x[], rvec f[], rvec fshift[],
2116 const t_pbc *pbc, const t_graph *g,
2117 real lambda, real *dvdlambda,
2118 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2119 int gmx_unused *global_atom_index)
2121 int i, type, ai, aj, ak, al;
2123 real phi, phi0, dphi0, ddphi, sign, vtot;
2124 rvec r_ij, r_kj, r_kl, m, n;
2125 real L1, kk, dp, dp2, kA, kB, pA, pB, dvdl_term;
2130 for (i = 0; (i < nbonds); )
2132 type = forceatoms[i++];
2133 ai = forceatoms[i++];
2134 aj = forceatoms[i++];
2135 ak = forceatoms[i++];
2136 al = forceatoms[i++];
2138 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2139 &sign, &t1, &t2, &t3); /* 84 */
2141 /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2142 * force changes if we just apply a normal harmonic.
2143 * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2144 * This means we will never have the periodicity problem, unless
2145 * the dihedral is Pi away from phiO, which is very unlikely due to
2148 kA = forceparams[type].harmonic.krA;
2149 kB = forceparams[type].harmonic.krB;
2150 pA = forceparams[type].harmonic.rA;
2151 pB = forceparams[type].harmonic.rB;
2153 kk = L1*kA + lambda*kB;
2154 phi0 = (L1*pA + lambda*pB)*DEG2RAD;
2155 dphi0 = (pB - pA)*DEG2RAD;
2159 make_dp_periodic(&dp);
2166 dvdl_term += 0.5*(kB - kA)*dp2 - kk*dphi0*dp;
2168 do_dih_fup(ai, aj, ak, al, -ddphi, r_ij, r_kj, r_kl, m, n,
2169 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2174 fprintf(debug, "idih: (%d,%d,%d,%d) phi=%g\n",
2175 ai, aj, ak, al, phi);
2180 *dvdlambda += dvdl_term;
2185 /*! \brief returns dx, rdist, and dpdl for functions posres() and fbposres()
2187 static void posres_dx(const rvec x, const rvec pos0A, const rvec pos0B,
2188 const rvec comA_sc, const rvec comB_sc,
2190 t_pbc *pbc, int refcoord_scaling, int npbcdim,
2191 rvec dx, rvec rdist, rvec dpdl)
2194 real posA, posB, L1, ref = 0.;
2199 for (m = 0; m < DIM; m++)
2205 switch (refcoord_scaling)
2209 rdist[m] = L1*posA + lambda*posB;
2210 dpdl[m] = posB - posA;
2213 /* Box relative coordinates are stored for dimensions with pbc */
2214 posA *= pbc->box[m][m];
2215 posB *= pbc->box[m][m];
2216 assert(npbcdim <= DIM);
2217 for (d = m+1; d < npbcdim; d++)
2219 posA += pos0A[d]*pbc->box[d][m];
2220 posB += pos0B[d]*pbc->box[d][m];
2222 ref = L1*posA + lambda*posB;
2224 dpdl[m] = posB - posA;
2227 ref = L1*comA_sc[m] + lambda*comB_sc[m];
2228 rdist[m] = L1*posA + lambda*posB;
2229 dpdl[m] = comB_sc[m] - comA_sc[m] + posB - posA;
2232 gmx_fatal(FARGS, "No such scaling method implemented");
2237 ref = L1*posA + lambda*posB;
2239 dpdl[m] = posB - posA;
2242 /* We do pbc_dx with ref+rdist,
2243 * since with only ref we can be up to half a box vector wrong.
2245 pos[m] = ref + rdist[m];
2250 pbc_dx(pbc, x, pos, dx);
2254 rvec_sub(x, pos, dx);
2258 /*! \brief Adds forces of flat-bottomed positions restraints to f[]
2259 * and fixes vir_diag. Returns the flat-bottomed potential. */
2260 real fbposres(int nbonds,
2261 const t_iatom forceatoms[], const t_iparams forceparams[],
2262 const rvec x[], rvec f[], rvec vir_diag,
2264 int refcoord_scaling, int ePBC, rvec com)
2265 /* compute flat-bottomed positions restraints */
2267 int i, ai, m, d, type, npbcdim = 0, fbdim;
2268 const t_iparams *pr;
2270 real dr, dr2, rfb, rfb2, fact, invdr;
2271 rvec com_sc, rdist, dx, dpdl, fm;
2274 npbcdim = ePBC2npbcdim(ePBC);
2276 if (refcoord_scaling == erscCOM)
2279 for (m = 0; m < npbcdim; m++)
2281 assert(npbcdim <= DIM);
2282 for (d = m; d < npbcdim; d++)
2284 com_sc[m] += com[d]*pbc->box[d][m];
2290 for (i = 0; (i < nbonds); )
2292 type = forceatoms[i++];
2293 ai = forceatoms[i++];
2294 pr = &forceparams[type];
2296 /* same calculation as for normal posres, but with identical A and B states, and lambda==0 */
2297 posres_dx(x[ai], forceparams[type].fbposres.pos0, forceparams[type].fbposres.pos0,
2298 com_sc, com_sc, 0.0,
2299 pbc, refcoord_scaling, npbcdim,
2305 kk = pr->fbposres.k;
2306 rfb = pr->fbposres.r;
2309 /* with rfb<0, push particle out of the sphere/cylinder/layer */
2317 switch (pr->fbposres.geom)
2319 case efbposresSPHERE:
2320 /* spherical flat-bottom posres */
2323 ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2327 v = 0.5*kk*sqr(dr - rfb);
2328 fact = -kk*(dr-rfb)/dr; /* Force pointing to the center pos0 */
2329 svmul(fact, dx, fm);
2332 case efbposresCYLINDER:
2333 /* cylidrical flat-bottom posres in x-y plane. fm[ZZ] = 0. */
2334 dr2 = sqr(dx[XX])+sqr(dx[YY]);
2336 ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2341 v = 0.5*kk*sqr(dr - rfb);
2342 fm[XX] = -kk*(dr-rfb)*dx[XX]*invdr; /* Force pointing to the center */
2343 fm[YY] = -kk*(dr-rfb)*dx[YY]*invdr;
2346 case efbposresX: /* fbdim=XX */
2347 case efbposresY: /* fbdim=YY */
2348 case efbposresZ: /* fbdim=ZZ */
2349 /* 1D flat-bottom potential */
2350 fbdim = pr->fbposres.geom - efbposresX;
2352 if ( ( dr > rfb && bInvert == FALSE ) || ( 0 < dr && dr < rfb && bInvert == TRUE ) )
2354 v = 0.5*kk*sqr(dr - rfb);
2355 fm[fbdim] = -kk*(dr - rfb);
2357 else if ( (dr < (-rfb) && bInvert == FALSE ) || ( (-rfb) < dr && dr < 0 && bInvert == TRUE ))
2359 v = 0.5*kk*sqr(dr + rfb);
2360 fm[fbdim] = -kk*(dr + rfb);
2367 for (m = 0; (m < DIM); m++)
2370 /* Here we correct for the pbc_dx which included rdist */
2371 vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm[m];
2379 real posres(int nbonds,
2380 const t_iatom forceatoms[], const t_iparams forceparams[],
2381 const rvec x[], rvec f[], rvec vir_diag,
2383 real lambda, real *dvdlambda,
2384 int refcoord_scaling, int ePBC, rvec comA, rvec comB)
2386 int i, ai, m, d, type, npbcdim = 0;
2387 const t_iparams *pr;
2390 rvec comA_sc, comB_sc, rdist, dpdl, dx;
2391 gmx_bool bForceValid = TRUE;
2393 if ((f == NULL) || (vir_diag == NULL)) /* should both be null together! */
2395 bForceValid = FALSE;
2398 npbcdim = ePBC2npbcdim(ePBC);
2400 if (refcoord_scaling == erscCOM)
2402 clear_rvec(comA_sc);
2403 clear_rvec(comB_sc);
2404 for (m = 0; m < npbcdim; m++)
2406 assert(npbcdim <= DIM);
2407 for (d = m; d < npbcdim; d++)
2409 comA_sc[m] += comA[d]*pbc->box[d][m];
2410 comB_sc[m] += comB[d]*pbc->box[d][m];
2418 for (i = 0; (i < nbonds); )
2420 type = forceatoms[i++];
2421 ai = forceatoms[i++];
2422 pr = &forceparams[type];
2424 /* return dx, rdist, and dpdl */
2425 posres_dx(x[ai], forceparams[type].posres.pos0A, forceparams[type].posres.pos0B,
2426 comA_sc, comB_sc, lambda,
2427 pbc, refcoord_scaling, npbcdim,
2430 for (m = 0; (m < DIM); m++)
2432 kk = L1*pr->posres.fcA[m] + lambda*pr->posres.fcB[m];
2434 vtot += 0.5*kk*dx[m]*dx[m];
2436 0.5*(pr->posres.fcB[m] - pr->posres.fcA[m])*dx[m]*dx[m]
2439 /* Here we correct for the pbc_dx which included rdist */
2443 vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm;
2451 static real low_angres(int nbonds,
2452 const t_iatom forceatoms[], const t_iparams forceparams[],
2453 const rvec x[], rvec f[], rvec fshift[],
2454 const t_pbc *pbc, const t_graph *g,
2455 real lambda, real *dvdlambda,
2458 int i, m, type, ai, aj, ak, al;
2460 real phi, cos_phi, cos_phi2, vid, vtot, dVdphi;
2461 rvec r_ij, r_kl, f_i, f_k = {0, 0, 0};
2462 real st, sth, nrij2, nrkl2, c, cij, ckl;
2465 t2 = 0; /* avoid warning with gcc-3.3. It is never used uninitialized */
2468 ak = al = 0; /* to avoid warnings */
2469 for (i = 0; i < nbonds; )
2471 type = forceatoms[i++];
2472 ai = forceatoms[i++];
2473 aj = forceatoms[i++];
2474 t1 = pbc_rvec_sub(pbc, x[aj], x[ai], r_ij); /* 3 */
2477 ak = forceatoms[i++];
2478 al = forceatoms[i++];
2479 t2 = pbc_rvec_sub(pbc, x[al], x[ak], r_kl); /* 3 */
2488 cos_phi = cos_angle(r_ij, r_kl); /* 25 */
2489 phi = acos(cos_phi); /* 10 */
2491 *dvdlambda += dopdihs_min(forceparams[type].pdihs.cpA,
2492 forceparams[type].pdihs.cpB,
2493 forceparams[type].pdihs.phiA,
2494 forceparams[type].pdihs.phiB,
2495 forceparams[type].pdihs.mult,
2496 phi, lambda, &vid, &dVdphi); /* 40 */
2500 cos_phi2 = sqr(cos_phi); /* 1 */
2503 st = -dVdphi*gmx_invsqrt(1 - cos_phi2); /* 12 */
2504 sth = st*cos_phi; /* 1 */
2505 nrij2 = iprod(r_ij, r_ij); /* 5 */
2506 nrkl2 = iprod(r_kl, r_kl); /* 5 */
2508 c = st*gmx_invsqrt(nrij2*nrkl2); /* 11 */
2509 cij = sth/nrij2; /* 10 */
2510 ckl = sth/nrkl2; /* 10 */
2512 for (m = 0; m < DIM; m++) /* 18+18 */
2514 f_i[m] = (c*r_kl[m]-cij*r_ij[m]);
2519 f_k[m] = (c*r_ij[m]-ckl*r_kl[m]);
2527 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
2530 rvec_inc(fshift[t1], f_i);
2531 rvec_dec(fshift[CENTRAL], f_i);
2536 ivec_sub(SHIFT_IVEC(g, ak), SHIFT_IVEC(g, al), dt);
2539 rvec_inc(fshift[t2], f_k);
2540 rvec_dec(fshift[CENTRAL], f_k);
2545 return vtot; /* 184 / 157 (bZAxis) total */
2548 real angres(int nbonds,
2549 const t_iatom forceatoms[], const t_iparams forceparams[],
2550 const rvec x[], rvec f[], rvec fshift[],
2551 const t_pbc *pbc, const t_graph *g,
2552 real lambda, real *dvdlambda,
2553 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2554 int gmx_unused *global_atom_index)
2556 return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2557 lambda, dvdlambda, FALSE);
2560 real angresz(int nbonds,
2561 const t_iatom forceatoms[], const t_iparams forceparams[],
2562 const rvec x[], rvec f[], rvec fshift[],
2563 const t_pbc *pbc, const t_graph *g,
2564 real lambda, real *dvdlambda,
2565 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2566 int gmx_unused *global_atom_index)
2568 return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2569 lambda, dvdlambda, TRUE);
2572 real dihres(int nbonds,
2573 const t_iatom forceatoms[], const t_iparams forceparams[],
2574 const rvec x[], rvec f[], rvec fshift[],
2575 const t_pbc *pbc, const t_graph *g,
2576 real lambda, real *dvdlambda,
2577 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2578 int gmx_unused *global_atom_index)
2581 int ai, aj, ak, al, i, k, type, t1, t2, t3;
2582 real phi0A, phi0B, dphiA, dphiB, kfacA, kfacB, phi0, dphi, kfac;
2583 real phi, ddphi, ddp, ddp2, dp, sign, d2r, L1;
2584 rvec r_ij, r_kj, r_kl, m, n;
2591 for (i = 0; (i < nbonds); )
2593 type = forceatoms[i++];
2594 ai = forceatoms[i++];
2595 aj = forceatoms[i++];
2596 ak = forceatoms[i++];
2597 al = forceatoms[i++];
2599 phi0A = forceparams[type].dihres.phiA*d2r;
2600 dphiA = forceparams[type].dihres.dphiA*d2r;
2601 kfacA = forceparams[type].dihres.kfacA;
2603 phi0B = forceparams[type].dihres.phiB*d2r;
2604 dphiB = forceparams[type].dihres.dphiB*d2r;
2605 kfacB = forceparams[type].dihres.kfacB;
2607 phi0 = L1*phi0A + lambda*phi0B;
2608 dphi = L1*dphiA + lambda*dphiB;
2609 kfac = L1*kfacA + lambda*kfacB;
2611 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2612 &sign, &t1, &t2, &t3);
2617 fprintf(debug, "dihres[%d]: %d %d %d %d : phi=%f, dphi=%f, kfac=%f\n",
2618 k++, ai, aj, ak, al, phi0, dphi, kfac);
2620 /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2621 * force changes if we just apply a normal harmonic.
2622 * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2623 * This means we will never have the periodicity problem, unless
2624 * the dihedral is Pi away from phiO, which is very unlikely due to
2628 make_dp_periodic(&dp);
2634 else if (dp < -dphi)
2646 vtot += 0.5*kfac*ddp2;
2649 *dvdlambda += 0.5*(kfacB - kfacA)*ddp2;
2650 /* lambda dependence from changing restraint distances */
2653 *dvdlambda -= kfac*ddp*((dphiB - dphiA)+(phi0B - phi0A));
2657 *dvdlambda += kfac*ddp*((dphiB - dphiA)-(phi0B - phi0A));
2659 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
2660 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2667 real unimplemented(int gmx_unused nbonds,
2668 const t_iatom gmx_unused forceatoms[], const t_iparams gmx_unused forceparams[],
2669 const rvec gmx_unused x[], rvec gmx_unused f[], rvec gmx_unused fshift[],
2670 const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
2671 real gmx_unused lambda, real gmx_unused *dvdlambda,
2672 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2673 int gmx_unused *global_atom_index)
2675 gmx_impl("*** you are using a not implemented function");
2677 return 0.0; /* To make the compiler happy */
2680 real restrangles(int nbonds,
2681 const t_iatom forceatoms[], const t_iparams forceparams[],
2682 const rvec x[], rvec f[], rvec fshift[],
2683 const t_pbc *pbc, const t_graph *g,
2684 real gmx_unused lambda, real gmx_unused *dvdlambda,
2685 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2686 int gmx_unused *global_atom_index)
2688 int i, d, ai, aj, ak, type, m;
2691 ivec jt, dt_ij, dt_kj;
2693 real prefactor, ratio_ante, ratio_post;
2694 rvec delta_ante, delta_post, vec_temp;
2697 for (i = 0; (i < nbonds); )
2699 type = forceatoms[i++];
2700 ai = forceatoms[i++];
2701 aj = forceatoms[i++];
2702 ak = forceatoms[i++];
2704 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2705 pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2706 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], delta_post);
2709 /* This function computes factors needed for restricted angle potential.
2710 * The restricted angle potential is used in coarse-grained simulations to avoid singularities
2711 * when three particles align and the dihedral angle and dihedral potential
2712 * cannot be calculated. This potential is calculated using the formula:
2713 real restrangles(int nbonds,
2714 const t_iatom forceatoms[],const t_iparams forceparams[],
2715 const rvec x[],rvec f[],rvec fshift[],
2716 const t_pbc *pbc,const t_graph *g,
2717 real gmx_unused lambda,real gmx_unused *dvdlambda,
2718 const t_mdatoms gmx_unused *md,t_fcdata gmx_unused *fcd,
2719 int gmx_unused *global_atom_index)
2721 int i, d, ai, aj, ak, type, m;
2725 ivec jt,dt_ij,dt_kj;
2727 real prefactor, ratio_ante, ratio_post;
2728 rvec delta_ante, delta_post, vec_temp;
2731 for(i=0; (i<nbonds); )
2733 type = forceatoms[i++];
2734 ai = forceatoms[i++];
2735 aj = forceatoms[i++];
2736 ak = forceatoms[i++];
2738 * \f[V_{\rm ReB}(\theta_i) = \frac{1}{2} k_{\theta} \frac{(\cos\theta_i - \cos\theta_0)^2}
2739 * {\sin^2\theta_i}\f] ({eq:ReB} and ref \cite{MonicaGoga2013} from the manual).
2740 * For more explanations see comments file "restcbt.h". */
2742 compute_factors_restangles(type, forceparams, delta_ante, delta_post,
2743 &prefactor, &ratio_ante, &ratio_post, &v);
2745 /* Forces are computed per component */
2746 for (d = 0; d < DIM; d++)
2748 f_i[d] = prefactor * (ratio_ante * delta_ante[d] - delta_post[d]);
2749 f_j[d] = prefactor * ((ratio_post + 1.0) * delta_post[d] - (ratio_ante + 1.0) * delta_ante[d]);
2750 f_k[d] = prefactor * (delta_ante[d] - ratio_post * delta_post[d]);
2753 /* Computation of potential energy */
2759 for (m = 0; (m < DIM); m++)
2768 copy_ivec(SHIFT_IVEC(g, aj), jt);
2769 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
2770 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
2771 t1 = IVEC2IS(dt_ij);
2772 t2 = IVEC2IS(dt_kj);
2775 rvec_inc(fshift[t1], f_i);
2776 rvec_inc(fshift[CENTRAL], f_j);
2777 rvec_inc(fshift[t2], f_k);
2783 real restrdihs(int nbonds,
2784 const t_iatom forceatoms[], const t_iparams forceparams[],
2785 const rvec x[], rvec f[], rvec fshift[],
2786 const t_pbc *pbc, const t_graph *g,
2787 real gmx_unused lambda, real gmx_unused *dvlambda,
2788 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2789 int gmx_unused *global_atom_index)
2791 int i, d, type, ai, aj, ak, al;
2792 rvec f_i, f_j, f_k, f_l;
2794 ivec jt, dt_ij, dt_kj, dt_lj;
2797 rvec delta_ante, delta_crnt, delta_post, vec_temp;
2798 real factor_phi_ai_ante, factor_phi_ai_crnt, factor_phi_ai_post;
2799 real factor_phi_aj_ante, factor_phi_aj_crnt, factor_phi_aj_post;
2800 real factor_phi_ak_ante, factor_phi_ak_crnt, factor_phi_ak_post;
2801 real factor_phi_al_ante, factor_phi_al_crnt, factor_phi_al_post;
2806 for (i = 0; (i < nbonds); )
2808 type = forceatoms[i++];
2809 ai = forceatoms[i++];
2810 aj = forceatoms[i++];
2811 ak = forceatoms[i++];
2812 al = forceatoms[i++];
2814 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2815 pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2816 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
2817 pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
2818 pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
2820 /* This function computes factors needed for restricted angle potential.
2821 * The restricted angle potential is used in coarse-grained simulations to avoid singularities
2822 * when three particles align and the dihedral angle and dihedral potential cannot be calculated.
2823 * This potential is calculated using the formula:
2824 * \f[V_{\rm ReB}(\theta_i) = \frac{1}{2} k_{\theta}
2825 * \frac{(\cos\theta_i - \cos\theta_0)^2}{\sin^2\theta_i}\f]
2826 * ({eq:ReB} and ref \cite{MonicaGoga2013} from the manual).
2827 * For more explanations see comments file "restcbt.h" */
2829 compute_factors_restrdihs(type, forceparams,
2830 delta_ante, delta_crnt, delta_post,
2831 &factor_phi_ai_ante, &factor_phi_ai_crnt, &factor_phi_ai_post,
2832 &factor_phi_aj_ante, &factor_phi_aj_crnt, &factor_phi_aj_post,
2833 &factor_phi_ak_ante, &factor_phi_ak_crnt, &factor_phi_ak_post,
2834 &factor_phi_al_ante, &factor_phi_al_crnt, &factor_phi_al_post,
2835 &prefactor_phi, &v);
2838 /* Computation of forces per component */
2839 for (d = 0; d < DIM; d++)
2841 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]);
2842 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]);
2843 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]);
2844 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]);
2846 /* Computation of the energy */
2852 /* Updating the forces */
2854 rvec_inc(f[ai], f_i);
2855 rvec_inc(f[aj], f_j);
2856 rvec_inc(f[ak], f_k);
2857 rvec_inc(f[al], f_l);
2860 /* Updating the fshift forces for the pressure coupling */
2863 copy_ivec(SHIFT_IVEC(g, aj), jt);
2864 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
2865 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
2866 ivec_sub(SHIFT_IVEC(g, al), jt, dt_lj);
2867 t1 = IVEC2IS(dt_ij);
2868 t2 = IVEC2IS(dt_kj);
2869 t3 = IVEC2IS(dt_lj);
2873 t3 = pbc_rvec_sub(pbc, x[al], x[aj], dx_jl);
2880 rvec_inc(fshift[t1], f_i);
2881 rvec_inc(fshift[CENTRAL], f_j);
2882 rvec_inc(fshift[t2], f_k);
2883 rvec_inc(fshift[t3], f_l);
2891 real cbtdihs(int nbonds,
2892 const t_iatom forceatoms[], const t_iparams forceparams[],
2893 const rvec x[], rvec f[], rvec fshift[],
2894 const t_pbc *pbc, const t_graph *g,
2895 real gmx_unused lambda, real gmx_unused *dvdlambda,
2896 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2897 int gmx_unused *global_atom_index)
2899 int type, ai, aj, ak, al, i, d;
2903 rvec f_i, f_j, f_k, f_l;
2904 ivec jt, dt_ij, dt_kj, dt_lj;
2906 rvec delta_ante, delta_crnt, delta_post;
2907 rvec f_phi_ai, f_phi_aj, f_phi_ak, f_phi_al;
2908 rvec f_theta_ante_ai, f_theta_ante_aj, f_theta_ante_ak;
2909 rvec f_theta_post_aj, f_theta_post_ak, f_theta_post_al;
2915 for (i = 0; (i < nbonds); )
2917 type = forceatoms[i++];
2918 ai = forceatoms[i++];
2919 aj = forceatoms[i++];
2920 ak = forceatoms[i++];
2921 al = forceatoms[i++];
2924 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2925 pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2926 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], vec_temp);
2927 pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
2928 pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
2929 pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
2931 /* \brief Compute factors for CBT potential
2932 * The combined bending-torsion potential goes to zero in a very smooth manner, eliminating the numerical
2933 * instabilities, when three coarse-grained particles align and the dihedral angle and standard
2934 * dihedral potentials cannot be calculated. The CBT potential is calculated using the formula:
2935 * \f[V_{\rm CBT}(\theta_{i-1}, \theta_i, \phi_i) = k_{\phi} \sin^3\theta_{i-1} \sin^3\theta_{i}
2936 * \sum_{n=0}^4 { a_n \cos^n\phi_i}\f] ({eq:CBT} and ref \cite{MonicaGoga2013} from the manual).
2937 * It contains in its expression not only the dihedral angle \f$\phi\f$
2938 * but also \f[\theta_{i-1}\f] (theta_ante bellow) and \f[\theta_{i}\f] (theta_post bellow)
2939 * --- the adjacent bending angles.
2940 * For more explanations see comments file "restcbt.h". */
2942 compute_factors_cbtdihs(type, forceparams, delta_ante, delta_crnt, delta_post,
2943 f_phi_ai, f_phi_aj, f_phi_ak, f_phi_al,
2944 f_theta_ante_ai, f_theta_ante_aj, f_theta_ante_ak,
2945 f_theta_post_aj, f_theta_post_ak, f_theta_post_al,
2949 /* Acumulate the resuts per beads */
2950 for (d = 0; d < DIM; d++)
2952 f_i[d] = f_phi_ai[d] + f_theta_ante_ai[d];
2953 f_j[d] = f_phi_aj[d] + f_theta_ante_aj[d] + f_theta_post_aj[d];
2954 f_k[d] = f_phi_ak[d] + f_theta_ante_ak[d] + f_theta_post_ak[d];
2955 f_l[d] = f_phi_al[d] + f_theta_post_al[d];
2958 /* Compute the potential energy */
2963 /* Updating the forces */
2964 rvec_inc(f[ai], f_i);
2965 rvec_inc(f[aj], f_j);
2966 rvec_inc(f[ak], f_k);
2967 rvec_inc(f[al], f_l);
2970 /* Updating the fshift forces for the pressure coupling */
2973 copy_ivec(SHIFT_IVEC(g, aj), jt);
2974 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
2975 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
2976 ivec_sub(SHIFT_IVEC(g, al), jt, dt_lj);
2977 t1 = IVEC2IS(dt_ij);
2978 t2 = IVEC2IS(dt_kj);
2979 t3 = IVEC2IS(dt_lj);
2983 t3 = pbc_rvec_sub(pbc, x[al], x[aj], dx_jl);
2990 rvec_inc(fshift[t1], f_i);
2991 rvec_inc(fshift[CENTRAL], f_j);
2992 rvec_inc(fshift[t2], f_k);
2993 rvec_inc(fshift[t3], f_l);
2999 real rbdihs(int nbonds,
3000 const t_iatom forceatoms[], const t_iparams forceparams[],
3001 const rvec x[], rvec f[], rvec fshift[],
3002 const t_pbc *pbc, const t_graph *g,
3003 real lambda, real *dvdlambda,
3004 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3005 int gmx_unused *global_atom_index)
3007 const real c0 = 0.0, c1 = 1.0, c2 = 2.0, c3 = 3.0, c4 = 4.0, c5 = 5.0;
3008 int type, ai, aj, ak, al, i, j;
3010 rvec r_ij, r_kj, r_kl, m, n;
3011 real parmA[NR_RBDIHS];
3012 real parmB[NR_RBDIHS];
3013 real parm[NR_RBDIHS];
3014 real cos_phi, phi, rbp, rbpBA;
3015 real v, sign, ddphi, sin_phi;
3017 real L1 = 1.0-lambda;
3021 for (i = 0; (i < nbonds); )
3023 type = forceatoms[i++];
3024 ai = forceatoms[i++];
3025 aj = forceatoms[i++];
3026 ak = forceatoms[i++];
3027 al = forceatoms[i++];
3029 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
3030 &sign, &t1, &t2, &t3); /* 84 */
3032 /* Change to polymer convention */
3039 phi -= M_PI; /* 1 */
3043 /* Beware of accuracy loss, cannot use 1-sqrt(cos^2) ! */
3046 for (j = 0; (j < NR_RBDIHS); j++)
3048 parmA[j] = forceparams[type].rbdihs.rbcA[j];
3049 parmB[j] = forceparams[type].rbdihs.rbcB[j];
3050 parm[j] = L1*parmA[j]+lambda*parmB[j];
3052 /* Calculate cosine powers */
3053 /* Calculate the energy */
3054 /* Calculate the derivative */
3057 dvdl_term += (parmB[0]-parmA[0]);
3062 rbpBA = parmB[1]-parmA[1];
3063 ddphi += rbp*cosfac;
3066 dvdl_term += cosfac*rbpBA;
3068 rbpBA = parmB[2]-parmA[2];
3069 ddphi += c2*rbp*cosfac;
3072 dvdl_term += cosfac*rbpBA;
3074 rbpBA = parmB[3]-parmA[3];
3075 ddphi += c3*rbp*cosfac;
3078 dvdl_term += cosfac*rbpBA;
3080 rbpBA = parmB[4]-parmA[4];
3081 ddphi += c4*rbp*cosfac;
3084 dvdl_term += cosfac*rbpBA;
3086 rbpBA = parmB[5]-parmA[5];
3087 ddphi += c5*rbp*cosfac;
3090 dvdl_term += cosfac*rbpBA;
3092 ddphi = -ddphi*sin_phi; /* 11 */
3094 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
3095 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
3098 *dvdlambda += dvdl_term;
3103 int cmap_setup_grid_index(int ip, int grid_spacing, int *ipm1, int *ipp1, int *ipp2)
3109 ip = ip + grid_spacing - 1;
3111 else if (ip > grid_spacing)
3113 ip = ip - grid_spacing - 1;
3122 im1 = grid_spacing - 1;
3124 else if (ip == grid_spacing-2)
3128 else if (ip == grid_spacing-1)
3142 real cmap_dihs(int nbonds,
3143 const t_iatom forceatoms[], const t_iparams forceparams[],
3144 const gmx_cmap_t *cmap_grid,
3145 const rvec x[], rvec f[], rvec fshift[],
3146 const t_pbc *pbc, const t_graph *g,
3147 real gmx_unused lambda, real gmx_unused *dvdlambda,
3148 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3149 int gmx_unused *global_atom_index)
3151 int i, j, k, n, idx;
3152 int ai, aj, ak, al, am;
3153 int a1i, a1j, a1k, a1l, a2i, a2j, a2k, a2l;
3155 int t11, t21, t31, t12, t22, t32;
3156 int iphi1, ip1m1, ip1p1, ip1p2;
3157 int iphi2, ip2m1, ip2p1, ip2p2;
3159 int pos1, pos2, pos3, pos4;
3161 real ty[4], ty1[4], ty2[4], ty12[4], tc[16], tx[16];
3162 real phi1, cos_phi1, sin_phi1, sign1, xphi1;
3163 real phi2, cos_phi2, sin_phi2, sign2, xphi2;
3164 real dx, xx, tt, tu, e, df1, df2, vtot;
3165 real ra21, rb21, rg21, rg1, rgr1, ra2r1, rb2r1, rabr1;
3166 real ra22, rb22, rg22, rg2, rgr2, ra2r2, rb2r2, rabr2;
3167 real fg1, hg1, fga1, hgb1, gaa1, gbb1;
3168 real fg2, hg2, fga2, hgb2, gaa2, gbb2;
3171 rvec r1_ij, r1_kj, r1_kl, m1, n1;
3172 rvec r2_ij, r2_kj, r2_kl, m2, n2;
3173 rvec f1_i, f1_j, f1_k, f1_l;
3174 rvec f2_i, f2_j, f2_k, f2_l;
3175 rvec a1, b1, a2, b2;
3176 rvec f1, g1, h1, f2, g2, h2;
3177 rvec dtf1, dtg1, dth1, dtf2, dtg2, dth2;
3178 ivec jt1, dt1_ij, dt1_kj, dt1_lj;
3179 ivec jt2, dt2_ij, dt2_kj, dt2_lj;
3183 int loop_index[4][4] = {
3190 /* Total CMAP energy */
3193 for (n = 0; n < nbonds; )
3195 /* Five atoms are involved in the two torsions */
3196 type = forceatoms[n++];
3197 ai = forceatoms[n++];
3198 aj = forceatoms[n++];
3199 ak = forceatoms[n++];
3200 al = forceatoms[n++];
3201 am = forceatoms[n++];
3203 /* Which CMAP type is this */
3204 cmapA = forceparams[type].cmap.cmapA;
3205 cmapd = cmap_grid->cmapdata[cmapA].cmap;
3213 phi1 = dih_angle(x[a1i], x[a1j], x[a1k], x[a1l], pbc, r1_ij, r1_kj, r1_kl, m1, n1,
3214 &sign1, &t11, &t21, &t31); /* 84 */
3216 cos_phi1 = cos(phi1);
3218 a1[0] = r1_ij[1]*r1_kj[2]-r1_ij[2]*r1_kj[1];
3219 a1[1] = r1_ij[2]*r1_kj[0]-r1_ij[0]*r1_kj[2];
3220 a1[2] = r1_ij[0]*r1_kj[1]-r1_ij[1]*r1_kj[0]; /* 9 */
3222 b1[0] = r1_kl[1]*r1_kj[2]-r1_kl[2]*r1_kj[1];
3223 b1[1] = r1_kl[2]*r1_kj[0]-r1_kl[0]*r1_kj[2];
3224 b1[2] = r1_kl[0]*r1_kj[1]-r1_kl[1]*r1_kj[0]; /* 9 */
3226 pbc_rvec_sub(pbc, x[a1l], x[a1k], h1);
3228 ra21 = iprod(a1, a1); /* 5 */
3229 rb21 = iprod(b1, b1); /* 5 */
3230 rg21 = iprod(r1_kj, r1_kj); /* 5 */
3236 rabr1 = sqrt(ra2r1*rb2r1);
3238 sin_phi1 = rg1 * rabr1 * iprod(a1, h1) * (-1);
3240 if (cos_phi1 < -0.5 || cos_phi1 > 0.5)
3242 phi1 = asin(sin_phi1);
3252 phi1 = -M_PI - phi1;
3258 phi1 = acos(cos_phi1);
3266 xphi1 = phi1 + M_PI; /* 1 */
3268 /* Second torsion */
3274 phi2 = dih_angle(x[a2i], x[a2j], x[a2k], x[a2l], pbc, r2_ij, r2_kj, r2_kl, m2, n2,
3275 &sign2, &t12, &t22, &t32); /* 84 */
3277 cos_phi2 = cos(phi2);
3279 a2[0] = r2_ij[1]*r2_kj[2]-r2_ij[2]*r2_kj[1];
3280 a2[1] = r2_ij[2]*r2_kj[0]-r2_ij[0]*r2_kj[2];
3281 a2[2] = r2_ij[0]*r2_kj[1]-r2_ij[1]*r2_kj[0]; /* 9 */
3283 b2[0] = r2_kl[1]*r2_kj[2]-r2_kl[2]*r2_kj[1];
3284 b2[1] = r2_kl[2]*r2_kj[0]-r2_kl[0]*r2_kj[2];
3285 b2[2] = r2_kl[0]*r2_kj[1]-r2_kl[1]*r2_kj[0]; /* 9 */
3287 pbc_rvec_sub(pbc, x[a2l], x[a2k], h2);
3289 ra22 = iprod(a2, a2); /* 5 */
3290 rb22 = iprod(b2, b2); /* 5 */
3291 rg22 = iprod(r2_kj, r2_kj); /* 5 */
3297 rabr2 = sqrt(ra2r2*rb2r2);
3299 sin_phi2 = rg2 * rabr2 * iprod(a2, h2) * (-1);
3301 if (cos_phi2 < -0.5 || cos_phi2 > 0.5)
3303 phi2 = asin(sin_phi2);
3313 phi2 = -M_PI - phi2;
3319 phi2 = acos(cos_phi2);
3327 xphi2 = phi2 + M_PI; /* 1 */
3329 /* Range mangling */
3332 xphi1 = xphi1 + 2*M_PI;
3334 else if (xphi1 >= 2*M_PI)
3336 xphi1 = xphi1 - 2*M_PI;
3341 xphi2 = xphi2 + 2*M_PI;
3343 else if (xphi2 >= 2*M_PI)
3345 xphi2 = xphi2 - 2*M_PI;
3348 /* Number of grid points */
3349 dx = 2*M_PI / cmap_grid->grid_spacing;
3351 /* Where on the grid are we */
3352 iphi1 = static_cast<int>(xphi1/dx);
3353 iphi2 = static_cast<int>(xphi2/dx);
3355 iphi1 = cmap_setup_grid_index(iphi1, cmap_grid->grid_spacing, &ip1m1, &ip1p1, &ip1p2);
3356 iphi2 = cmap_setup_grid_index(iphi2, cmap_grid->grid_spacing, &ip2m1, &ip2p1, &ip2p2);
3358 pos1 = iphi1*cmap_grid->grid_spacing+iphi2;
3359 pos2 = ip1p1*cmap_grid->grid_spacing+iphi2;
3360 pos3 = ip1p1*cmap_grid->grid_spacing+ip2p1;
3361 pos4 = iphi1*cmap_grid->grid_spacing+ip2p1;
3363 ty[0] = cmapd[pos1*4];
3364 ty[1] = cmapd[pos2*4];
3365 ty[2] = cmapd[pos3*4];
3366 ty[3] = cmapd[pos4*4];
3368 ty1[0] = cmapd[pos1*4+1];
3369 ty1[1] = cmapd[pos2*4+1];
3370 ty1[2] = cmapd[pos3*4+1];
3371 ty1[3] = cmapd[pos4*4+1];
3373 ty2[0] = cmapd[pos1*4+2];
3374 ty2[1] = cmapd[pos2*4+2];
3375 ty2[2] = cmapd[pos3*4+2];
3376 ty2[3] = cmapd[pos4*4+2];
3378 ty12[0] = cmapd[pos1*4+3];
3379 ty12[1] = cmapd[pos2*4+3];
3380 ty12[2] = cmapd[pos3*4+3];
3381 ty12[3] = cmapd[pos4*4+3];
3383 /* Switch to degrees */
3384 dx = 360.0 / cmap_grid->grid_spacing;
3385 xphi1 = xphi1 * RAD2DEG;
3386 xphi2 = xphi2 * RAD2DEG;
3388 for (i = 0; i < 4; i++) /* 16 */
3391 tx[i+4] = ty1[i]*dx;
3392 tx[i+8] = ty2[i]*dx;
3393 tx[i+12] = ty12[i]*dx*dx;
3397 for (i = 0; i < 4; i++) /* 1056 */
3399 for (j = 0; j < 4; j++)
3402 for (k = 0; k < 16; k++)
3404 xx = xx + cmap_coeff_matrix[k*16+idx]*tx[k];
3412 tt = (xphi1-iphi1*dx)/dx;
3413 tu = (xphi2-iphi2*dx)/dx;
3419 for (i = 3; i >= 0; i--)
3421 l1 = loop_index[i][3];
3422 l2 = loop_index[i][2];
3423 l3 = loop_index[i][1];
3425 e = tt * e + ((tc[i*4+3]*tu+tc[i*4+2])*tu + tc[i*4+1])*tu+tc[i*4];
3426 df1 = tu * df1 + (3.0*tc[l1]*tt+2.0*tc[l2])*tt+tc[l3];
3427 df2 = tt * df2 + (3.0*tc[i*4+3]*tu+2.0*tc[i*4+2])*tu+tc[i*4+1];
3437 /* Do forces - first torsion */
3438 fg1 = iprod(r1_ij, r1_kj);
3439 hg1 = iprod(r1_kl, r1_kj);
3440 fga1 = fg1*ra2r1*rgr1;
3441 hgb1 = hg1*rb2r1*rgr1;
3445 for (i = 0; i < DIM; i++)
3447 dtf1[i] = gaa1 * a1[i];
3448 dtg1[i] = fga1 * a1[i] - hgb1 * b1[i];
3449 dth1[i] = gbb1 * b1[i];
3451 f1[i] = df1 * dtf1[i];
3452 g1[i] = df1 * dtg1[i];
3453 h1[i] = df1 * dth1[i];
3456 f1_j[i] = -f1[i] - g1[i];
3457 f1_k[i] = h1[i] + g1[i];
3460 f[a1i][i] = f[a1i][i] + f1_i[i];
3461 f[a1j][i] = f[a1j][i] + f1_j[i]; /* - f1[i] - g1[i] */
3462 f[a1k][i] = f[a1k][i] + f1_k[i]; /* h1[i] + g1[i] */
3463 f[a1l][i] = f[a1l][i] + f1_l[i]; /* h1[i] */
3466 /* Do forces - second torsion */
3467 fg2 = iprod(r2_ij, r2_kj);
3468 hg2 = iprod(r2_kl, r2_kj);
3469 fga2 = fg2*ra2r2*rgr2;
3470 hgb2 = hg2*rb2r2*rgr2;
3474 for (i = 0; i < DIM; i++)
3476 dtf2[i] = gaa2 * a2[i];
3477 dtg2[i] = fga2 * a2[i] - hgb2 * b2[i];
3478 dth2[i] = gbb2 * b2[i];
3480 f2[i] = df2 * dtf2[i];
3481 g2[i] = df2 * dtg2[i];
3482 h2[i] = df2 * dth2[i];
3485 f2_j[i] = -f2[i] - g2[i];
3486 f2_k[i] = h2[i] + g2[i];
3489 f[a2i][i] = f[a2i][i] + f2_i[i]; /* f2[i] */
3490 f[a2j][i] = f[a2j][i] + f2_j[i]; /* - f2[i] - g2[i] */
3491 f[a2k][i] = f[a2k][i] + f2_k[i]; /* h2[i] + g2[i] */
3492 f[a2l][i] = f[a2l][i] + f2_l[i]; /* - h2[i] */
3498 copy_ivec(SHIFT_IVEC(g, a1j), jt1);
3499 ivec_sub(SHIFT_IVEC(g, a1i), jt1, dt1_ij);
3500 ivec_sub(SHIFT_IVEC(g, a1k), jt1, dt1_kj);
3501 ivec_sub(SHIFT_IVEC(g, a1l), jt1, dt1_lj);
3502 t11 = IVEC2IS(dt1_ij);
3503 t21 = IVEC2IS(dt1_kj);
3504 t31 = IVEC2IS(dt1_lj);
3506 copy_ivec(SHIFT_IVEC(g, a2j), jt2);
3507 ivec_sub(SHIFT_IVEC(g, a2i), jt2, dt2_ij);
3508 ivec_sub(SHIFT_IVEC(g, a2k), jt2, dt2_kj);
3509 ivec_sub(SHIFT_IVEC(g, a2l), jt2, dt2_lj);
3510 t12 = IVEC2IS(dt2_ij);
3511 t22 = IVEC2IS(dt2_kj);
3512 t32 = IVEC2IS(dt2_lj);
3516 t31 = pbc_rvec_sub(pbc, x[a1l], x[a1j], h1);
3517 t32 = pbc_rvec_sub(pbc, x[a2l], x[a2j], h2);
3525 rvec_inc(fshift[t11], f1_i);
3526 rvec_inc(fshift[CENTRAL], f1_j);
3527 rvec_inc(fshift[t21], f1_k);
3528 rvec_inc(fshift[t31], f1_l);
3530 rvec_inc(fshift[t21], f2_i);
3531 rvec_inc(fshift[CENTRAL], f2_j);
3532 rvec_inc(fshift[t22], f2_k);
3533 rvec_inc(fshift[t32], f2_l);
3540 /***********************************************************
3542 * G R O M O S 9 6 F U N C T I O N S
3544 ***********************************************************/
3545 real g96harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
3548 const real half = 0.5;
3549 real L1, kk, x0, dx, dx2;
3550 real v, f, dvdlambda;
3553 kk = L1*kA+lambda*kB;
3554 x0 = L1*xA+lambda*xB;
3561 dvdlambda = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
3568 /* That was 21 flops */
3571 real g96bonds(int nbonds,
3572 const t_iatom forceatoms[], const t_iparams forceparams[],
3573 const rvec x[], rvec f[], rvec fshift[],
3574 const t_pbc *pbc, const t_graph *g,
3575 real lambda, real *dvdlambda,
3576 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3577 int gmx_unused *global_atom_index)
3579 int i, m, ki, ai, aj, type;
3580 real dr2, fbond, vbond, fij, vtot;
3585 for (i = 0; (i < nbonds); )
3587 type = forceatoms[i++];
3588 ai = forceatoms[i++];
3589 aj = forceatoms[i++];
3591 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
3592 dr2 = iprod(dx, dx); /* 5 */
3594 *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3595 forceparams[type].harmonic.krB,
3596 forceparams[type].harmonic.rA,
3597 forceparams[type].harmonic.rB,
3598 dr2, lambda, &vbond, &fbond);
3600 vtot += 0.5*vbond; /* 1*/
3604 fprintf(debug, "G96-BONDS: dr = %10g vbond = %10g fbond = %10g\n",
3605 sqrt(dr2), vbond, fbond);
3611 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3614 for (m = 0; (m < DIM); m++) /* 15 */
3619 fshift[ki][m] += fij;
3620 fshift[CENTRAL][m] -= fij;
3626 real g96bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
3627 rvec r_ij, rvec r_kj,
3629 /* Return value is the angle between the bonds i-j and j-k */
3633 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
3634 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
3636 costh = cos_angle(r_ij, r_kj); /* 25 */
3641 real g96angles(int nbonds,
3642 const t_iatom forceatoms[], const t_iparams forceparams[],
3643 const rvec x[], rvec f[], rvec fshift[],
3644 const t_pbc *pbc, const t_graph *g,
3645 real lambda, real *dvdlambda,
3646 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3647 int gmx_unused *global_atom_index)
3649 int i, ai, aj, ak, type, m, t1, t2;
3651 real cos_theta, dVdt, va, vtot;
3652 real rij_1, rij_2, rkj_1, rkj_2, rijrkj_1;
3654 ivec jt, dt_ij, dt_kj;
3657 for (i = 0; (i < nbonds); )
3659 type = forceatoms[i++];
3660 ai = forceatoms[i++];
3661 aj = forceatoms[i++];
3662 ak = forceatoms[i++];
3664 cos_theta = g96bond_angle(x[ai], x[aj], x[ak], pbc, r_ij, r_kj, &t1, &t2);
3666 *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3667 forceparams[type].harmonic.krB,
3668 forceparams[type].harmonic.rA,
3669 forceparams[type].harmonic.rB,
3670 cos_theta, lambda, &va, &dVdt);
3673 rij_1 = gmx_invsqrt(iprod(r_ij, r_ij));
3674 rkj_1 = gmx_invsqrt(iprod(r_kj, r_kj));
3675 rij_2 = rij_1*rij_1;
3676 rkj_2 = rkj_1*rkj_1;
3677 rijrkj_1 = rij_1*rkj_1; /* 23 */
3682 fprintf(debug, "G96ANGLES: costheta = %10g vth = %10g dV/dct = %10g\n",
3683 cos_theta, va, dVdt);
3686 for (m = 0; (m < DIM); m++) /* 42 */
3688 f_i[m] = dVdt*(r_kj[m]*rijrkj_1 - r_ij[m]*rij_2*cos_theta);
3689 f_k[m] = dVdt*(r_ij[m]*rijrkj_1 - r_kj[m]*rkj_2*cos_theta);
3690 f_j[m] = -f_i[m]-f_k[m];
3698 copy_ivec(SHIFT_IVEC(g, aj), jt);
3700 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3701 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3702 t1 = IVEC2IS(dt_ij);
3703 t2 = IVEC2IS(dt_kj);
3705 rvec_inc(fshift[t1], f_i);
3706 rvec_inc(fshift[CENTRAL], f_j);
3707 rvec_inc(fshift[t2], f_k); /* 9 */
3713 real cross_bond_bond(int nbonds,
3714 const t_iatom forceatoms[], const t_iparams forceparams[],
3715 const rvec x[], rvec f[], rvec fshift[],
3716 const t_pbc *pbc, const t_graph *g,
3717 real gmx_unused lambda, real gmx_unused *dvdlambda,
3718 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3719 int gmx_unused *global_atom_index)
3721 /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3724 int i, ai, aj, ak, type, m, t1, t2;
3726 real vtot, vrr, s1, s2, r1, r2, r1e, r2e, krr;
3728 ivec jt, dt_ij, dt_kj;
3731 for (i = 0; (i < nbonds); )
3733 type = forceatoms[i++];
3734 ai = forceatoms[i++];
3735 aj = forceatoms[i++];
3736 ak = forceatoms[i++];
3737 r1e = forceparams[type].cross_bb.r1e;
3738 r2e = forceparams[type].cross_bb.r2e;
3739 krr = forceparams[type].cross_bb.krr;
3741 /* Compute distance vectors ... */
3742 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3743 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3745 /* ... and their lengths */
3749 /* Deviations from ideality */
3753 /* Energy (can be negative!) */
3758 svmul(-krr*s2/r1, r_ij, f_i);
3759 svmul(-krr*s1/r2, r_kj, f_k);
3761 for (m = 0; (m < DIM); m++) /* 12 */
3763 f_j[m] = -f_i[m] - f_k[m];
3772 copy_ivec(SHIFT_IVEC(g, aj), jt);
3774 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3775 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3776 t1 = IVEC2IS(dt_ij);
3777 t2 = IVEC2IS(dt_kj);
3779 rvec_inc(fshift[t1], f_i);
3780 rvec_inc(fshift[CENTRAL], f_j);
3781 rvec_inc(fshift[t2], f_k); /* 9 */
3787 real cross_bond_angle(int nbonds,
3788 const t_iatom forceatoms[], const t_iparams forceparams[],
3789 const rvec x[], rvec f[], rvec fshift[],
3790 const t_pbc *pbc, const t_graph *g,
3791 real gmx_unused lambda, real gmx_unused *dvdlambda,
3792 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3793 int gmx_unused *global_atom_index)
3795 /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3798 int i, ai, aj, ak, type, m, t1, t2;
3799 rvec r_ij, r_kj, r_ik;
3800 real vtot, vrt, s1, s2, s3, r1, r2, r3, r1e, r2e, r3e, krt, k1, k2, k3;
3802 ivec jt, dt_ij, dt_kj;
3805 for (i = 0; (i < nbonds); )
3807 type = forceatoms[i++];
3808 ai = forceatoms[i++];
3809 aj = forceatoms[i++];
3810 ak = forceatoms[i++];
3811 r1e = forceparams[type].cross_ba.r1e;
3812 r2e = forceparams[type].cross_ba.r2e;
3813 r3e = forceparams[type].cross_ba.r3e;
3814 krt = forceparams[type].cross_ba.krt;
3816 /* Compute distance vectors ... */
3817 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3818 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3819 pbc_rvec_sub(pbc, x[ai], x[ak], r_ik);
3821 /* ... and their lengths */
3826 /* Deviations from ideality */
3831 /* Energy (can be negative!) */
3832 vrt = krt*s3*(s1+s2);
3838 k3 = -krt*(s1+s2)/r3;
3839 for (m = 0; (m < DIM); m++)
3841 f_i[m] = k1*r_ij[m] + k3*r_ik[m];
3842 f_k[m] = k2*r_kj[m] - k3*r_ik[m];
3843 f_j[m] = -f_i[m] - f_k[m];
3846 for (m = 0; (m < DIM); m++) /* 12 */
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 static real bonded_tab(const char *type, int table_nr,
3872 const bondedtable_t *table, real kA, real kB, real r,
3873 real lambda, real *V, real *F)
3875 real k, tabscale, *VFtab, rt, eps, eps2, Yt, Ft, Geps, Heps2, Fp, VV, FF;
3879 k = (1.0 - lambda)*kA + lambda*kB;
3881 tabscale = table->scale;
3882 VFtab = table->data;
3885 n0 = static_cast<int>(rt);
3888 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",
3889 type, table_nr, r, n0, n0+1, table->n);
3896 Geps = VFtab[nnn+2]*eps;
3897 Heps2 = VFtab[nnn+3]*eps2;
3898 Fp = Ft + Geps + Heps2;
3900 FF = Fp + Geps + 2.0*Heps2;
3902 *F = -k*FF*tabscale;
3904 dvdlambda = (kB - kA)*VV;
3908 /* That was 22 flops */
3911 real tab_bonds(int nbonds,
3912 const t_iatom forceatoms[], const t_iparams forceparams[],
3913 const rvec x[], rvec f[], rvec fshift[],
3914 const t_pbc *pbc, const t_graph *g,
3915 real lambda, real *dvdlambda,
3916 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
3917 int gmx_unused *global_atom_index)
3919 int i, m, ki, ai, aj, type, table;
3920 real dr, dr2, fbond, vbond, fij, vtot;
3925 for (i = 0; (i < nbonds); )
3927 type = forceatoms[i++];
3928 ai = forceatoms[i++];
3929 aj = forceatoms[i++];
3931 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
3932 dr2 = iprod(dx, dx); /* 5 */
3933 dr = dr2*gmx_invsqrt(dr2); /* 10 */
3935 table = forceparams[type].tab.table;
3937 *dvdlambda += bonded_tab("bond", table,
3938 &fcd->bondtab[table],
3939 forceparams[type].tab.kA,
3940 forceparams[type].tab.kB,
3941 dr, lambda, &vbond, &fbond); /* 22 */
3949 vtot += vbond; /* 1*/
3950 fbond *= gmx_invsqrt(dr2); /* 6 */
3954 fprintf(debug, "TABBONDS: dr = %10g vbond = %10g fbond = %10g\n",
3960 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3963 for (m = 0; (m < DIM); m++) /* 15 */
3968 fshift[ki][m] += fij;
3969 fshift[CENTRAL][m] -= fij;
3975 real tab_angles(int nbonds,
3976 const t_iatom forceatoms[], const t_iparams forceparams[],
3977 const rvec x[], rvec f[], rvec fshift[],
3978 const t_pbc *pbc, const t_graph *g,
3979 real lambda, real *dvdlambda,
3980 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
3981 int gmx_unused *global_atom_index)
3983 int i, ai, aj, ak, t1, t2, type, table;
3985 real cos_theta, cos_theta2, theta, dVdt, va, vtot;
3986 ivec jt, dt_ij, dt_kj;
3989 for (i = 0; (i < nbonds); )
3991 type = forceatoms[i++];
3992 ai = forceatoms[i++];
3993 aj = forceatoms[i++];
3994 ak = forceatoms[i++];
3996 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
3997 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
3999 table = forceparams[type].tab.table;
4001 *dvdlambda += bonded_tab("angle", table,
4002 &fcd->angletab[table],
4003 forceparams[type].tab.kA,
4004 forceparams[type].tab.kB,
4005 theta, lambda, &va, &dVdt); /* 22 */
4008 cos_theta2 = sqr(cos_theta); /* 1 */
4017 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
4018 sth = st*cos_theta; /* 1 */
4022 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
4023 theta*RAD2DEG, va, dVdt);
4026 nrkj2 = iprod(r_kj, r_kj); /* 5 */
4027 nrij2 = iprod(r_ij, r_ij);
4029 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
4030 cii = sth/nrij2; /* 10 */
4031 ckk = sth/nrkj2; /* 10 */
4033 for (m = 0; (m < DIM); m++) /* 39 */
4035 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
4036 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
4037 f_j[m] = -f_i[m]-f_k[m];
4044 copy_ivec(SHIFT_IVEC(g, aj), jt);
4046 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
4047 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
4048 t1 = IVEC2IS(dt_ij);
4049 t2 = IVEC2IS(dt_kj);
4051 rvec_inc(fshift[t1], f_i);
4052 rvec_inc(fshift[CENTRAL], f_j);
4053 rvec_inc(fshift[t2], f_k);
4059 real tab_dihs(int nbonds,
4060 const t_iatom forceatoms[], const t_iparams forceparams[],
4061 const rvec x[], rvec f[], rvec fshift[],
4062 const t_pbc *pbc, const t_graph *g,
4063 real lambda, real *dvdlambda,
4064 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
4065 int gmx_unused *global_atom_index)
4067 int i, type, ai, aj, ak, al, table;
4069 rvec r_ij, r_kj, r_kl, m, n;
4070 real phi, sign, ddphi, vpd, vtot;
4073 for (i = 0; (i < nbonds); )
4075 type = forceatoms[i++];
4076 ai = forceatoms[i++];
4077 aj = forceatoms[i++];
4078 ak = forceatoms[i++];
4079 al = forceatoms[i++];
4081 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
4082 &sign, &t1, &t2, &t3); /* 84 */
4084 table = forceparams[type].tab.table;
4086 /* Hopefully phi+M_PI never results in values < 0 */
4087 *dvdlambda += bonded_tab("dihedral", table,
4088 &fcd->dihtab[table],
4089 forceparams[type].tab.kA,
4090 forceparams[type].tab.kB,
4091 phi+M_PI, lambda, &vpd, &ddphi);
4094 do_dih_fup(ai, aj, ak, al, -ddphi, r_ij, r_kj, r_kl, m, n,
4095 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
4098 fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
4099 ai, aj, ak, al, phi);
4106 /* TODO This function could go away when idef is not a big bucket of
4108 gmx_bool ftype_is_bonded_potential(int ftype)
4111 (interaction_function[ftype].flags & IF_BOND) &&
4112 !(ftype == F_CONNBONDS || ftype == F_POSRES || ftype == F_FBPOSRES) &&
4113 (ftype < F_GB12 || ftype > F_GB14);
4116 static void zero_thread_forces(f_thread_t *f_t, int n,
4117 int nblock, int blocksize)
4119 int b, a0, a1, a, i, j;
4121 if (n > f_t->f_nalloc)
4123 f_t->f_nalloc = over_alloc_large(n);
4124 srenew(f_t->f, f_t->f_nalloc);
4127 if (f_t->red_mask != 0)
4129 for (b = 0; b < nblock; b++)
4131 if (f_t->red_mask && (1U<<b))
4134 a1 = std::min((b+1)*blocksize, n);
4135 for (a = a0; a < a1; a++)
4137 clear_rvec(f_t->f[a]);
4142 for (i = 0; i < SHIFTS; i++)
4144 clear_rvec(f_t->fshift[i]);
4146 for (i = 0; i < F_NRE; i++)
4150 for (i = 0; i < egNR; i++)
4152 for (j = 0; j < f_t->grpp.nener; j++)
4154 f_t->grpp.ener[i][j] = 0;
4157 for (i = 0; i < efptNR; i++)
4163 static void reduce_thread_force_buffer(int n, rvec *f,
4164 int nthreads, f_thread_t *f_t,
4165 int nblock, int block_size)
4167 /* The max thread number is arbitrary,
4168 * we used a fixed number to avoid memory management.
4169 * Using more than 16 threads is probably never useful performance wise.
4171 #define MAX_BONDED_THREADS 256
4174 if (nthreads > MAX_BONDED_THREADS)
4176 gmx_fatal(FARGS, "Can not reduce bonded forces on more than %d threads",
4177 MAX_BONDED_THREADS);
4180 /* This reduction can run on any number of threads,
4181 * independently of nthreads.
4183 #pragma omp parallel for num_threads(nthreads) schedule(static)
4184 for (b = 0; b < nblock; b++)
4186 rvec *fp[MAX_BONDED_THREADS];
4190 /* Determine which threads contribute to this block */
4192 for (ft = 1; ft < nthreads; ft++)
4194 if (f_t[ft].red_mask & (1U<<b))
4196 fp[nfb++] = f_t[ft].f;
4201 /* Reduce force buffers for threads that contribute */
4203 a1 = (b+1)*block_size;
4204 a1 = std::min(a1, n);
4205 for (a = a0; a < a1; a++)
4207 for (fb = 0; fb < nfb; fb++)
4209 rvec_inc(f[a], fp[fb][a]);
4216 static void reduce_thread_forces(int n, rvec *f, rvec *fshift,
4217 real *ener, gmx_grppairener_t *grpp, real *dvdl,
4218 int nthreads, f_thread_t *f_t,
4219 int nblock, int block_size,
4220 gmx_bool bCalcEnerVir,
4225 /* Reduce the bonded force buffer */
4226 reduce_thread_force_buffer(n, f, nthreads, f_t, nblock, block_size);
4229 /* When necessary, reduce energy and virial using one thread only */
4234 for (i = 0; i < SHIFTS; i++)
4236 for (t = 1; t < nthreads; t++)
4238 rvec_inc(fshift[i], f_t[t].fshift[i]);
4241 for (i = 0; i < F_NRE; i++)
4243 for (t = 1; t < nthreads; t++)
4245 ener[i] += f_t[t].ener[i];
4248 for (i = 0; i < egNR; i++)
4250 for (j = 0; j < f_t[1].grpp.nener; j++)
4252 for (t = 1; t < nthreads; t++)
4255 grpp->ener[i][j] += f_t[t].grpp.ener[i][j];
4261 for (i = 0; i < efptNR; i++)
4264 for (t = 1; t < nthreads; t++)
4266 dvdl[i] += f_t[t].dvdl[i];
4273 static real calc_one_bond(int thread,
4274 int ftype, const t_idef *idef,
4275 const rvec x[], rvec f[], rvec fshift[],
4277 const t_pbc *pbc, const t_graph *g,
4278 gmx_grppairener_t *grpp,
4280 real *lambda, real *dvdl,
4281 const t_mdatoms *md, t_fcdata *fcd,
4282 gmx_bool bCalcEnerVir,
4283 int *global_atom_index)
4285 int nat1, nbonds, efptFTYPE;
4290 if (IS_RESTRAINT_TYPE(ftype))
4292 efptFTYPE = efptRESTRAINT;
4296 efptFTYPE = efptBONDED;
4299 nat1 = interaction_function[ftype].nratoms + 1;
4300 nbonds = idef->il[ftype].nr/nat1;
4301 iatoms = idef->il[ftype].iatoms;
4303 nb0 = idef->il_thread_division[ftype*(idef->nthreads+1)+thread];
4304 nbn = idef->il_thread_division[ftype*(idef->nthreads+1)+thread+1] - nb0;
4306 if (!IS_LISTED_LJ_C(ftype))
4308 if (ftype == F_CMAP)
4310 v = cmap_dihs(nbn, iatoms+nb0,
4311 idef->iparams, &idef->cmap_grid,
4313 pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4314 md, fcd, global_atom_index);
4316 #ifdef GMX_SIMD_HAVE_REAL
4317 else if (ftype == F_ANGLES &&
4318 !bCalcEnerVir && fr->efep == efepNO)
4320 /* No energies, shift forces, dvdl */
4321 angles_noener_simd(nbn, idef->il[ftype].iatoms+nb0,
4324 pbc, g, lambda[efptFTYPE], md, fcd,
4329 else if (ftype == F_PDIHS &&
4330 !bCalcEnerVir && fr->efep == efepNO)
4332 /* No energies, shift forces, dvdl */
4333 #ifdef GMX_SIMD_HAVE_REAL
4338 (nbn, idef->il[ftype].iatoms+nb0,
4341 pbc, g, lambda[efptFTYPE], md, fcd,
4347 v = interaction_function[ftype].ifunc(nbn, iatoms+nb0,
4350 pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4351 md, fcd, global_atom_index);
4356 v = do_nonbonded_listed(ftype, nbn, iatoms+nb0, idef->iparams, x, f, fshift,
4357 pbc, g, lambda, dvdl, md, fr, grpp, global_atom_index);
4362 inc_nrnb(nrnb, interaction_function[ftype].nrnb_ind, nbonds);
4368 void calc_bonds(const gmx_multisim_t *ms,
4370 const rvec x[], history_t *hist,
4371 rvec f[], t_forcerec *fr,
4372 const t_pbc *pbc, const t_graph *g,
4373 gmx_enerdata_t *enerd, t_nrnb *nrnb,
4375 const t_mdatoms *md,
4376 t_fcdata *fcd, int *global_atom_index,
4379 gmx_bool bCalcEnerVir;
4381 real dvdl[efptNR]; /* The dummy array is to have a place to store the dhdl at other values
4382 of lambda, which will be thrown away in the end*/
4383 const t_pbc *pbc_null;
4386 assert(fr->nthreads == idef->nthreads);
4388 bCalcEnerVir = (force_flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY));
4390 for (i = 0; i < efptNR; i++)
4406 p_graph(debug, "Bondage is fun", g);
4410 /* Do pre force calculation stuff which might require communication */
4411 if (idef->il[F_ORIRES].nr)
4413 enerd->term[F_ORIRESDEV] =
4414 calc_orires_dev(ms, idef->il[F_ORIRES].nr,
4415 idef->il[F_ORIRES].iatoms,
4416 idef->iparams, md, x,
4417 pbc_null, fcd, hist);
4419 if (idef->il[F_DISRES].nr)
4421 calc_disres_R_6(idef->il[F_DISRES].nr,
4422 idef->il[F_DISRES].iatoms,
4423 idef->iparams, x, pbc_null,
4426 if (fcd->disres.nsystems > 1)
4428 gmx_sum_sim(2*fcd->disres.nres, fcd->disres.Rt_6, ms);
4433 #pragma omp parallel for num_threads(fr->nthreads) schedule(static)
4434 for (thread = 0; thread < fr->nthreads; thread++)
4441 gmx_grppairener_t *grpp;
4446 fshift = fr->fshift;
4448 grpp = &enerd->grpp;
4453 zero_thread_forces(&fr->f_t[thread], fr->natoms_force,
4454 fr->red_nblock, 1<<fr->red_ashift);
4456 ft = fr->f_t[thread].f;
4457 fshift = fr->f_t[thread].fshift;
4458 epot = fr->f_t[thread].ener;
4459 grpp = &fr->f_t[thread].grpp;
4460 dvdlt = fr->f_t[thread].dvdl;
4462 /* Loop over all bonded force types to calculate the bonded forces */
4463 for (ftype = 0; (ftype < F_NRE); ftype++)
4465 if (idef->il[ftype].nr > 0 && ftype_is_bonded_potential(ftype))
4467 v = calc_one_bond(thread, ftype, idef, x,
4468 ft, fshift, fr, pbc_null, g, grpp,
4469 nrnb, lambda, dvdlt,
4470 md, fcd, bCalcEnerVir,
4476 if (fr->nthreads > 1)
4478 reduce_thread_forces(fr->natoms_force, f, fr->fshift,
4479 enerd->term, &enerd->grpp, dvdl,
4480 fr->nthreads, fr->f_t,
4481 fr->red_nblock, 1<<fr->red_ashift,
4483 force_flags & GMX_FORCE_DHDL);
4485 if (force_flags & GMX_FORCE_DHDL)
4487 for (i = 0; i < efptNR; i++)
4489 enerd->dvdl_nonlin[i] += dvdl[i];
4493 /* Copy the sum of violations for the distance restraints from fcd */
4496 enerd->term[F_DISRESVIOL] = fcd->disres.sumviol;
4501 void calc_bonds_lambda(const t_idef *idef,
4504 const t_pbc *pbc, const t_graph *g,
4505 gmx_grppairener_t *grpp, real *epot, t_nrnb *nrnb,
4507 const t_mdatoms *md,
4509 int *global_atom_index)
4511 int ftype, nr_nonperturbed, nr;
4513 real dvdl_dum[efptNR];
4515 const t_pbc *pbc_null;
4527 /* Copy the whole idef, so we can modify the contents locally */
4529 idef_fe.nthreads = 1;
4530 snew(idef_fe.il_thread_division, F_NRE*(idef_fe.nthreads+1));
4532 /* We already have the forces, so we use temp buffers here */
4533 snew(f, fr->natoms_force);
4534 snew(fshift, SHIFTS);
4536 /* Loop over all bonded force types to calculate the bonded energies */
4537 for (ftype = 0; (ftype < F_NRE); ftype++)
4539 if (ftype_is_bonded_potential(ftype))
4541 /* Set the work range of thread 0 to the perturbed bondeds only */
4542 nr_nonperturbed = idef->il[ftype].nr_nonperturbed;
4543 nr = idef->il[ftype].nr;
4544 idef_fe.il_thread_division[ftype*2+0] = nr_nonperturbed;
4545 idef_fe.il_thread_division[ftype*2+1] = nr;
4547 /* This is only to get the flop count correct */
4548 idef_fe.il[ftype].nr = nr - nr_nonperturbed;
4550 if (nr - nr_nonperturbed > 0)
4552 v = calc_one_bond(0, ftype, &idef_fe,
4553 x, f, fshift, fr, pbc_null, g,
4554 grpp, nrnb, lambda, dvdl_dum,
4565 sfree(idef_fe.il_thread_division);