1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
52 #include "gmx_fatal.h"
58 #include "nonbonded.h"
60 /* Include the SIMD macro file and then check for support */
61 #include "gmx_simd_macros.h"
62 #if defined GMX_HAVE_SIMD_MACROS && defined GMX_SIMD_HAVE_TRIGONOMETRIC
64 #include "gmx_simd_vec.h"
67 /* Find a better place for this? */
68 const int cmap_coeff_matrix[] = {
69 1, 0, -3, 2, 0, 0, 0, 0, -3, 0, 9, -6, 2, 0, -6, 4,
70 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, -9, 6, -2, 0, 6, -4,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, -6, 0, 0, -6, 4,
72 0, 0, 3, -2, 0, 0, 0, 0, 0, 0, -9, 6, 0, 0, 6, -4,
73 0, 0, 0, 0, 1, 0, -3, 2, -2, 0, 6, -4, 1, 0, -3, 2,
74 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 3, -2, 1, 0, -3, 2,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 2, 0, 0, 3, -2,
76 0, 0, 0, 0, 0, 0, 3, -2, 0, 0, -6, 4, 0, 0, 3, -2,
77 0, 1, -2, 1, 0, 0, 0, 0, 0, -3, 6, -3, 0, 2, -4, 2,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, -6, 3, 0, -2, 4, -2,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -3, 3, 0, 0, 2, -2,
80 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 3, -3, 0, 0, -2, 2,
81 0, 0, 0, 0, 0, 1, -2, 1, 0, -2, 4, -2, 0, 1, -2, 1,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 2, -1, 0, 1, -2, 1,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, -1, 1,
84 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 2, -2, 0, 0, -1, 1
89 int glatnr(int *global_atom_index, int i)
93 if (global_atom_index == NULL)
99 atnr = global_atom_index[i] + 1;
105 static int pbc_rvec_sub(const t_pbc *pbc, const rvec xi, const rvec xj, rvec dx)
109 return pbc_dx_aiuc(pbc, xi, xj, dx);
113 rvec_sub(xi, xj, dx);
120 /* SIMD PBC data structure, containing 1/boxdiag and the box vectors */
133 /* Set the SIMD pbc data from a normal t_pbc struct */
134 static void set_pbc_simd(const t_pbc *pbc, pbc_simd_t *pbc_simd)
139 /* Setting inv_bdiag to 0 effectively turns off PBC */
140 clear_rvec(inv_bdiag);
143 for (d = 0; d < pbc->ndim_ePBC; d++)
145 inv_bdiag[d] = 1.0/pbc->box[d][d];
149 pbc_simd->inv_bzz = gmx_set1_pr(inv_bdiag[ZZ]);
150 pbc_simd->inv_byy = gmx_set1_pr(inv_bdiag[YY]);
151 pbc_simd->inv_bxx = gmx_set1_pr(inv_bdiag[XX]);
155 pbc_simd->bzx = gmx_set1_pr(pbc->box[ZZ][XX]);
156 pbc_simd->bzy = gmx_set1_pr(pbc->box[ZZ][YY]);
157 pbc_simd->bzz = gmx_set1_pr(pbc->box[ZZ][ZZ]);
158 pbc_simd->byx = gmx_set1_pr(pbc->box[YY][XX]);
159 pbc_simd->byy = gmx_set1_pr(pbc->box[YY][YY]);
160 pbc_simd->bxx = gmx_set1_pr(pbc->box[XX][XX]);
164 pbc_simd->bzx = gmx_setzero_pr();
165 pbc_simd->bzy = gmx_setzero_pr();
166 pbc_simd->bzz = gmx_setzero_pr();
167 pbc_simd->byx = gmx_setzero_pr();
168 pbc_simd->byy = gmx_setzero_pr();
169 pbc_simd->bxx = gmx_setzero_pr();
173 /* Correct distance vector *dx,*dy,*dz for PBC using SIMD */
174 static gmx_inline void
175 pbc_dx_simd(gmx_mm_pr *dx, gmx_mm_pr *dy, gmx_mm_pr *dz,
176 const pbc_simd_t *pbc)
180 sh = gmx_round_pr(gmx_mul_pr(*dz, pbc->inv_bzz));
181 *dx = gmx_nmsub_pr(sh, pbc->bzx, *dx);
182 *dy = gmx_nmsub_pr(sh, pbc->bzy, *dy);
183 *dz = gmx_nmsub_pr(sh, pbc->bzz, *dz);
185 sh = gmx_round_pr(gmx_mul_pr(*dy, pbc->inv_byy));
186 *dx = gmx_nmsub_pr(sh, pbc->byx, *dx);
187 *dy = gmx_nmsub_pr(sh, pbc->byy, *dy);
189 sh = gmx_round_pr(gmx_mul_pr(*dx, pbc->inv_bxx));
190 *dx = gmx_nmsub_pr(sh, pbc->bxx, *dx);
193 #endif /* SIMD_BONDEDS */
196 * Morse potential bond by Frank Everdij
198 * Three parameters needed:
200 * b0 = equilibrium distance in nm
201 * be = beta in nm^-1 (actually, it's nu_e*Sqrt(2*pi*pi*mu/D_e))
202 * cb = well depth in kJ/mol
204 * Note: the potential is referenced to be +cb at infinite separation
205 * and zero at the equilibrium distance!
208 real morse_bonds(int nbonds,
209 const t_iatom forceatoms[], const t_iparams forceparams[],
210 const rvec x[], rvec f[], rvec fshift[],
211 const t_pbc *pbc, const t_graph *g,
212 real lambda, real *dvdlambda,
213 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
214 int gmx_unused *global_atom_index)
216 const real one = 1.0;
217 const real two = 2.0;
218 real dr, dr2, temp, omtemp, cbomtemp, fbond, vbond, fij, vtot;
219 real b0, be, cb, b0A, beA, cbA, b0B, beB, cbB, L1;
221 int i, m, ki, type, ai, aj;
225 for (i = 0; (i < nbonds); )
227 type = forceatoms[i++];
228 ai = forceatoms[i++];
229 aj = forceatoms[i++];
231 b0A = forceparams[type].morse.b0A;
232 beA = forceparams[type].morse.betaA;
233 cbA = forceparams[type].morse.cbA;
235 b0B = forceparams[type].morse.b0B;
236 beB = forceparams[type].morse.betaB;
237 cbB = forceparams[type].morse.cbB;
239 L1 = one-lambda; /* 1 */
240 b0 = L1*b0A + lambda*b0B; /* 3 */
241 be = L1*beA + lambda*beB; /* 3 */
242 cb = L1*cbA + lambda*cbB; /* 3 */
244 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
245 dr2 = iprod(dx, dx); /* 5 */
246 dr = dr2*gmx_invsqrt(dr2); /* 10 */
247 temp = exp(-be*(dr-b0)); /* 12 */
251 /* bonds are constrainted. This may _not_ include bond constraints if they are lambda dependent */
252 *dvdlambda += cbB-cbA;
256 omtemp = one-temp; /* 1 */
257 cbomtemp = cb*omtemp; /* 1 */
258 vbond = cbomtemp*omtemp; /* 1 */
259 fbond = -two*be*temp*cbomtemp*gmx_invsqrt(dr2); /* 9 */
260 vtot += vbond; /* 1 */
262 *dvdlambda += (cbB - cbA) * omtemp * omtemp - (2-2*omtemp)*omtemp * cb * ((b0B-b0A)*be - (beB-beA)*(dr-b0)); /* 15 */
266 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
270 for (m = 0; (m < DIM); m++) /* 15 */
275 fshift[ki][m] += fij;
276 fshift[CENTRAL][m] -= fij;
282 real cubic_bonds(int nbonds,
283 const t_iatom forceatoms[], const t_iparams forceparams[],
284 const rvec x[], rvec f[], rvec fshift[],
285 const t_pbc *pbc, const t_graph *g,
286 real gmx_unused lambda, real gmx_unused *dvdlambda,
287 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
288 int gmx_unused *global_atom_index)
290 const real three = 3.0;
291 const real two = 2.0;
293 real dr, dr2, dist, kdist, kdist2, fbond, vbond, fij, vtot;
295 int i, m, ki, type, ai, aj;
299 for (i = 0; (i < nbonds); )
301 type = forceatoms[i++];
302 ai = forceatoms[i++];
303 aj = forceatoms[i++];
305 b0 = forceparams[type].cubic.b0;
306 kb = forceparams[type].cubic.kb;
307 kcub = forceparams[type].cubic.kcub;
309 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
310 dr2 = iprod(dx, dx); /* 5 */
317 dr = dr2*gmx_invsqrt(dr2); /* 10 */
322 vbond = kdist2 + kcub*kdist2*dist;
323 fbond = -(two*kdist + three*kdist2*kcub)/dr;
325 vtot += vbond; /* 21 */
329 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
332 for (m = 0; (m < DIM); m++) /* 15 */
337 fshift[ki][m] += fij;
338 fshift[CENTRAL][m] -= fij;
344 real FENE_bonds(int nbonds,
345 const t_iatom forceatoms[], const t_iparams forceparams[],
346 const rvec x[], rvec f[], rvec fshift[],
347 const t_pbc *pbc, const t_graph *g,
348 real gmx_unused lambda, real gmx_unused *dvdlambda,
349 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
350 int *global_atom_index)
352 const real half = 0.5;
353 const real one = 1.0;
355 real dr, dr2, bm2, omdr2obm2, fbond, vbond, fij, vtot;
357 int i, m, ki, type, ai, aj;
361 for (i = 0; (i < nbonds); )
363 type = forceatoms[i++];
364 ai = forceatoms[i++];
365 aj = forceatoms[i++];
367 bm = forceparams[type].fene.bm;
368 kb = forceparams[type].fene.kb;
370 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
371 dr2 = iprod(dx, dx); /* 5 */
383 "r^2 (%f) >= bm^2 (%f) in FENE bond between atoms %d and %d",
385 glatnr(global_atom_index, ai),
386 glatnr(global_atom_index, aj));
389 omdr2obm2 = one - dr2/bm2;
391 vbond = -half*kb*bm2*log(omdr2obm2);
392 fbond = -kb/omdr2obm2;
394 vtot += vbond; /* 35 */
398 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
401 for (m = 0; (m < DIM); m++) /* 15 */
406 fshift[ki][m] += fij;
407 fshift[CENTRAL][m] -= fij;
413 real harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
416 const real half = 0.5;
417 real L1, kk, x0, dx, dx2;
418 real v, f, dvdlambda;
421 kk = L1*kA+lambda*kB;
422 x0 = L1*xA+lambda*xB;
429 dvdlambda = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
436 /* That was 19 flops */
440 real bonds(int nbonds,
441 const t_iatom forceatoms[], const t_iparams forceparams[],
442 const rvec x[], rvec f[], rvec fshift[],
443 const t_pbc *pbc, const t_graph *g,
444 real lambda, real *dvdlambda,
445 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
446 int gmx_unused *global_atom_index)
448 int i, m, ki, ai, aj, type;
449 real dr, dr2, fbond, vbond, fij, vtot;
454 for (i = 0; (i < nbonds); )
456 type = forceatoms[i++];
457 ai = forceatoms[i++];
458 aj = forceatoms[i++];
460 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
461 dr2 = iprod(dx, dx); /* 5 */
462 dr = dr2*gmx_invsqrt(dr2); /* 10 */
464 *dvdlambda += harmonic(forceparams[type].harmonic.krA,
465 forceparams[type].harmonic.krB,
466 forceparams[type].harmonic.rA,
467 forceparams[type].harmonic.rB,
468 dr, lambda, &vbond, &fbond); /* 19 */
476 vtot += vbond; /* 1*/
477 fbond *= gmx_invsqrt(dr2); /* 6 */
481 fprintf(debug, "BONDS: dr = %10g vbond = %10g fbond = %10g\n",
487 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
490 for (m = 0; (m < DIM); m++) /* 15 */
495 fshift[ki][m] += fij;
496 fshift[CENTRAL][m] -= fij;
502 real restraint_bonds(int nbonds,
503 const t_iatom forceatoms[], const t_iparams forceparams[],
504 const rvec x[], rvec f[], rvec fshift[],
505 const t_pbc *pbc, const t_graph *g,
506 real lambda, real *dvdlambda,
507 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
508 int gmx_unused *global_atom_index)
510 int i, m, ki, ai, aj, type;
511 real dr, dr2, fbond, vbond, fij, vtot;
513 real low, dlow, up1, dup1, up2, dup2, k, dk;
521 for (i = 0; (i < nbonds); )
523 type = forceatoms[i++];
524 ai = forceatoms[i++];
525 aj = forceatoms[i++];
527 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
528 dr2 = iprod(dx, dx); /* 5 */
529 dr = dr2*gmx_invsqrt(dr2); /* 10 */
531 low = L1*forceparams[type].restraint.lowA + lambda*forceparams[type].restraint.lowB;
532 dlow = -forceparams[type].restraint.lowA + forceparams[type].restraint.lowB;
533 up1 = L1*forceparams[type].restraint.up1A + lambda*forceparams[type].restraint.up1B;
534 dup1 = -forceparams[type].restraint.up1A + forceparams[type].restraint.up1B;
535 up2 = L1*forceparams[type].restraint.up2A + lambda*forceparams[type].restraint.up2B;
536 dup2 = -forceparams[type].restraint.up2A + forceparams[type].restraint.up2B;
537 k = L1*forceparams[type].restraint.kA + lambda*forceparams[type].restraint.kB;
538 dk = -forceparams[type].restraint.kA + forceparams[type].restraint.kB;
547 *dvdlambda += 0.5*dk*drh2 - k*dlow*drh;
560 *dvdlambda += 0.5*dk*drh2 - k*dup1*drh;
565 vbond = k*(up2 - up1)*(0.5*(up2 - up1) + drh);
566 fbond = -k*(up2 - up1);
567 *dvdlambda += dk*(up2 - up1)*(0.5*(up2 - up1) + drh)
568 + k*(dup2 - dup1)*(up2 - up1 + drh)
569 - k*(up2 - up1)*dup2;
577 vtot += vbond; /* 1*/
578 fbond *= gmx_invsqrt(dr2); /* 6 */
582 fprintf(debug, "BONDS: dr = %10g vbond = %10g fbond = %10g\n",
588 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
591 for (m = 0; (m < DIM); m++) /* 15 */
596 fshift[ki][m] += fij;
597 fshift[CENTRAL][m] -= fij;
604 real polarize(int nbonds,
605 const t_iatom forceatoms[], const t_iparams forceparams[],
606 const rvec x[], rvec f[], rvec fshift[],
607 const t_pbc *pbc, const t_graph *g,
608 real lambda, real *dvdlambda,
609 const t_mdatoms *md, t_fcdata gmx_unused *fcd,
610 int gmx_unused *global_atom_index)
612 int i, m, ki, ai, aj, type;
613 real dr, dr2, fbond, vbond, fij, vtot, ksh;
618 for (i = 0; (i < nbonds); )
620 type = forceatoms[i++];
621 ai = forceatoms[i++];
622 aj = forceatoms[i++];
623 ksh = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].polarize.alpha;
626 fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
629 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
630 dr2 = iprod(dx, dx); /* 5 */
631 dr = dr2*gmx_invsqrt(dr2); /* 10 */
633 *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /* 19 */
640 vtot += vbond; /* 1*/
641 fbond *= gmx_invsqrt(dr2); /* 6 */
645 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
648 for (m = 0; (m < DIM); m++) /* 15 */
653 fshift[ki][m] += fij;
654 fshift[CENTRAL][m] -= fij;
660 real anharm_polarize(int nbonds,
661 const t_iatom forceatoms[], const t_iparams forceparams[],
662 const rvec x[], rvec f[], rvec fshift[],
663 const t_pbc *pbc, const t_graph *g,
664 real lambda, real *dvdlambda,
665 const t_mdatoms *md, t_fcdata gmx_unused *fcd,
666 int gmx_unused *global_atom_index)
668 int i, m, ki, ai, aj, type;
669 real dr, dr2, fbond, vbond, fij, vtot, ksh, khyp, drcut, ddr, ddr3;
674 for (i = 0; (i < nbonds); )
676 type = forceatoms[i++];
677 ai = forceatoms[i++];
678 aj = forceatoms[i++];
679 ksh = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].anharm_polarize.alpha; /* 7*/
680 khyp = forceparams[type].anharm_polarize.khyp;
681 drcut = forceparams[type].anharm_polarize.drcut;
684 fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
687 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
688 dr2 = iprod(dx, dx); /* 5 */
689 dr = dr2*gmx_invsqrt(dr2); /* 10 */
691 *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /* 19 */
702 vbond += khyp*ddr*ddr3;
703 fbond -= 4*khyp*ddr3;
705 fbond *= gmx_invsqrt(dr2); /* 6 */
706 vtot += vbond; /* 1*/
710 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
713 for (m = 0; (m < DIM); m++) /* 15 */
718 fshift[ki][m] += fij;
719 fshift[CENTRAL][m] -= fij;
725 real water_pol(int nbonds,
726 const t_iatom forceatoms[], const t_iparams forceparams[],
727 const rvec x[], rvec f[], rvec gmx_unused fshift[],
728 const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
729 real gmx_unused lambda, real gmx_unused *dvdlambda,
730 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
731 int gmx_unused *global_atom_index)
733 /* This routine implements anisotropic polarizibility for water, through
734 * a shell connected to a dummy with spring constant that differ in the
735 * three spatial dimensions in the molecular frame.
737 int i, m, aO, aH1, aH2, aD, aS, type, type0;
738 rvec dOH1, dOH2, dHH, dOD, dDS, nW, kk, dx, kdx, proj;
742 real vtot, fij, r_HH, r_OD, r_nW, tx, ty, tz, qS;
747 type0 = forceatoms[0];
749 qS = md->chargeA[aS];
750 kk[XX] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_x;
751 kk[YY] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_y;
752 kk[ZZ] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_z;
753 r_HH = 1.0/forceparams[type0].wpol.rHH;
754 r_OD = 1.0/forceparams[type0].wpol.rOD;
757 fprintf(debug, "WPOL: qS = %10.5f aS = %5d\n", qS, aS);
758 fprintf(debug, "WPOL: kk = %10.3f %10.3f %10.3f\n",
759 kk[XX], kk[YY], kk[ZZ]);
760 fprintf(debug, "WPOL: rOH = %10.3f rHH = %10.3f rOD = %10.3f\n",
761 forceparams[type0].wpol.rOH,
762 forceparams[type0].wpol.rHH,
763 forceparams[type0].wpol.rOD);
765 for (i = 0; (i < nbonds); i += 6)
767 type = forceatoms[i];
770 gmx_fatal(FARGS, "Sorry, type = %d, type0 = %d, file = %s, line = %d",
771 type, type0, __FILE__, __LINE__);
773 aO = forceatoms[i+1];
774 aH1 = forceatoms[i+2];
775 aH2 = forceatoms[i+3];
776 aD = forceatoms[i+4];
777 aS = forceatoms[i+5];
779 /* Compute vectors describing the water frame */
780 rvec_sub(x[aH1], x[aO], dOH1);
781 rvec_sub(x[aH2], x[aO], dOH2);
782 rvec_sub(x[aH2], x[aH1], dHH);
783 rvec_sub(x[aD], x[aO], dOD);
784 rvec_sub(x[aS], x[aD], dDS);
785 cprod(dOH1, dOH2, nW);
787 /* Compute inverse length of normal vector
788 * (this one could be precomputed, but I'm too lazy now)
790 r_nW = gmx_invsqrt(iprod(nW, nW));
791 /* This is for precision, but does not make a big difference,
794 r_OD = gmx_invsqrt(iprod(dOD, dOD));
796 /* Normalize the vectors in the water frame */
798 svmul(r_HH, dHH, dHH);
799 svmul(r_OD, dOD, dOD);
801 /* Compute displacement of shell along components of the vector */
802 dx[ZZ] = iprod(dDS, dOD);
803 /* Compute projection on the XY plane: dDS - dx[ZZ]*dOD */
804 for (m = 0; (m < DIM); m++)
806 proj[m] = dDS[m]-dx[ZZ]*dOD[m];
809 /*dx[XX] = iprod(dDS,nW);
810 dx[YY] = iprod(dDS,dHH);*/
811 dx[XX] = iprod(proj, nW);
812 for (m = 0; (m < DIM); m++)
814 proj[m] -= dx[XX]*nW[m];
816 dx[YY] = iprod(proj, dHH);
821 fprintf(debug, "WPOL: dx2=%10g dy2=%10g dz2=%10g sum=%10g dDS^2=%10g\n",
822 sqr(dx[XX]), sqr(dx[YY]), sqr(dx[ZZ]), iprod(dx, dx), iprod(dDS, dDS));
823 fprintf(debug, "WPOL: dHH=(%10g,%10g,%10g)\n", dHH[XX], dHH[YY], dHH[ZZ]);
824 fprintf(debug, "WPOL: dOD=(%10g,%10g,%10g), 1/r_OD = %10g\n",
825 dOD[XX], dOD[YY], dOD[ZZ], 1/r_OD);
826 fprintf(debug, "WPOL: nW =(%10g,%10g,%10g), 1/r_nW = %10g\n",
827 nW[XX], nW[YY], nW[ZZ], 1/r_nW);
828 fprintf(debug, "WPOL: dx =%10g, dy =%10g, dz =%10g\n",
829 dx[XX], dx[YY], dx[ZZ]);
830 fprintf(debug, "WPOL: dDSx=%10g, dDSy=%10g, dDSz=%10g\n",
831 dDS[XX], dDS[YY], dDS[ZZ]);
834 /* Now compute the forces and energy */
835 kdx[XX] = kk[XX]*dx[XX];
836 kdx[YY] = kk[YY]*dx[YY];
837 kdx[ZZ] = kk[ZZ]*dx[ZZ];
838 vtot += iprod(dx, kdx);
839 for (m = 0; (m < DIM); m++)
841 /* This is a tensor operation but written out for speed */
855 fprintf(debug, "WPOL: vwpol=%g\n", 0.5*iprod(dx, kdx));
856 fprintf(debug, "WPOL: df = (%10g, %10g, %10g)\n", df[XX], df[YY], df[ZZ]);
864 static real do_1_thole(const rvec xi, const rvec xj, rvec fi, rvec fj,
865 const t_pbc *pbc, real qq,
866 rvec fshift[], real afac)
869 real r12sq, r12_1, r12n, r12bar, v0, v1, fscal, ebar, fff;
872 t = pbc_rvec_sub(pbc, xi, xj, r12); /* 3 */
874 r12sq = iprod(r12, r12); /* 5 */
875 r12_1 = gmx_invsqrt(r12sq); /* 5 */
876 r12bar = afac/r12_1; /* 5 */
877 v0 = qq*ONE_4PI_EPS0*r12_1; /* 2 */
878 ebar = exp(-r12bar); /* 5 */
879 v1 = (1-(1+0.5*r12bar)*ebar); /* 4 */
880 fscal = ((v0*r12_1)*v1 - v0*0.5*afac*ebar*(r12bar+1))*r12_1; /* 9 */
883 fprintf(debug, "THOLE: v0 = %.3f v1 = %.3f r12= % .3f r12bar = %.3f fscal = %.3f ebar = %.3f\n", v0, v1, 1/r12_1, r12bar, fscal, ebar);
886 for (m = 0; (m < DIM); m++)
892 fshift[CENTRAL][m] -= fff;
895 return v0*v1; /* 1 */
899 real thole_pol(int nbonds,
900 const t_iatom forceatoms[], const t_iparams forceparams[],
901 const rvec x[], rvec f[], rvec fshift[],
902 const t_pbc *pbc, const t_graph gmx_unused *g,
903 real gmx_unused lambda, real gmx_unused *dvdlambda,
904 const t_mdatoms *md, t_fcdata gmx_unused *fcd,
905 int gmx_unused *global_atom_index)
907 /* Interaction between two pairs of particles with opposite charge */
908 int i, type, a1, da1, a2, da2;
909 real q1, q2, qq, a, al1, al2, afac;
912 for (i = 0; (i < nbonds); )
914 type = forceatoms[i++];
915 a1 = forceatoms[i++];
916 da1 = forceatoms[i++];
917 a2 = forceatoms[i++];
918 da2 = forceatoms[i++];
919 q1 = md->chargeA[da1];
920 q2 = md->chargeA[da2];
921 a = forceparams[type].thole.a;
922 al1 = forceparams[type].thole.alpha1;
923 al2 = forceparams[type].thole.alpha2;
925 afac = a*pow(al1*al2, -1.0/6.0);
926 V += do_1_thole(x[a1], x[a2], f[a1], f[a2], pbc, qq, fshift, afac);
927 V += do_1_thole(x[da1], x[a2], f[da1], f[a2], pbc, -qq, fshift, afac);
928 V += do_1_thole(x[a1], x[da2], f[a1], f[da2], pbc, -qq, fshift, afac);
929 V += do_1_thole(x[da1], x[da2], f[da1], f[da2], pbc, qq, fshift, afac);
935 real bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
936 rvec r_ij, rvec r_kj, real *costh,
938 /* Return value is the angle between the bonds i-j and j-k */
943 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
944 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
946 *costh = cos_angle(r_ij, r_kj); /* 25 */
947 th = acos(*costh); /* 10 */
952 real angles(int nbonds,
953 const t_iatom forceatoms[], const t_iparams forceparams[],
954 const rvec x[], rvec f[], rvec fshift[],
955 const t_pbc *pbc, const t_graph *g,
956 real lambda, real *dvdlambda,
957 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
958 int gmx_unused *global_atom_index)
960 int i, ai, aj, ak, t1, t2, type;
962 real cos_theta, cos_theta2, theta, dVdt, va, vtot;
963 ivec jt, dt_ij, dt_kj;
966 for (i = 0; i < nbonds; )
968 type = forceatoms[i++];
969 ai = forceatoms[i++];
970 aj = forceatoms[i++];
971 ak = forceatoms[i++];
973 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
974 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
976 *dvdlambda += harmonic(forceparams[type].harmonic.krA,
977 forceparams[type].harmonic.krB,
978 forceparams[type].harmonic.rA*DEG2RAD,
979 forceparams[type].harmonic.rB*DEG2RAD,
980 theta, lambda, &va, &dVdt); /* 21 */
983 cos_theta2 = sqr(cos_theta);
993 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
994 sth = st*cos_theta; /* 1 */
998 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
999 theta*RAD2DEG, va, dVdt);
1002 nrij2 = iprod(r_ij, r_ij); /* 5 */
1003 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1005 nrij_1 = gmx_invsqrt(nrij2); /* 10 */
1006 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1008 cik = st*nrij_1*nrkj_1; /* 2 */
1009 cii = sth*nrij_1*nrij_1; /* 2 */
1010 ckk = sth*nrkj_1*nrkj_1; /* 2 */
1012 for (m = 0; m < DIM; m++)
1014 f_i[m] = -(cik*r_kj[m] - cii*r_ij[m]);
1015 f_k[m] = -(cik*r_ij[m] - ckk*r_kj[m]);
1016 f_j[m] = -f_i[m] - f_k[m];
1023 copy_ivec(SHIFT_IVEC(g, aj), jt);
1025 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1026 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1027 t1 = IVEC2IS(dt_ij);
1028 t2 = IVEC2IS(dt_kj);
1030 rvec_inc(fshift[t1], f_i);
1031 rvec_inc(fshift[CENTRAL], f_j);
1032 rvec_inc(fshift[t2], f_k);
1041 /* As angles, but using SIMD to calculate many dihedrals at once.
1042 * This routines does not calculate energies and shift forces.
1044 static gmx_inline void
1045 angles_noener_simd(int nbonds,
1046 const t_iatom forceatoms[], const t_iparams forceparams[],
1047 const rvec x[], rvec f[],
1048 const t_pbc *pbc, const t_graph gmx_unused *g,
1049 real gmx_unused lambda,
1050 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1051 int gmx_unused *global_atom_index)
1053 #define UNROLL GMX_SIMD_WIDTH_HERE
1056 int type, ai[UNROLL], aj[UNROLL], ak[UNROLL];
1057 real coeff_array[2*UNROLL+UNROLL], *coeff;
1058 real dr_array[2*DIM*UNROLL+UNROLL], *dr;
1059 real f_buf_array[6*UNROLL+UNROLL], *f_buf;
1060 gmx_mm_pr k_S, theta0_S;
1061 gmx_mm_pr rijx_S, rijy_S, rijz_S;
1062 gmx_mm_pr rkjx_S, rkjy_S, rkjz_S;
1064 gmx_mm_pr rij_rkj_S;
1065 gmx_mm_pr nrij2_S, nrij_1_S;
1066 gmx_mm_pr nrkj2_S, nrkj_1_S;
1067 gmx_mm_pr cos_S, sin_S;
1069 gmx_mm_pr st_S, sth_S;
1070 gmx_mm_pr cik_S, cii_S, ckk_S;
1071 gmx_mm_pr f_ix_S, f_iy_S, f_iz_S;
1072 gmx_mm_pr f_kx_S, f_ky_S, f_kz_S;
1073 pbc_simd_t pbc_simd;
1075 /* Ensure register memory alignment */
1076 coeff = gmx_simd_align_real(coeff_array);
1077 dr = gmx_simd_align_real(dr_array);
1078 f_buf = gmx_simd_align_real(f_buf_array);
1080 set_pbc_simd(pbc, &pbc_simd);
1082 one_S = gmx_set1_pr(1.0);
1084 /* nbonds is the number of angles times nfa1, here we step UNROLL angles */
1085 for (i = 0; (i < nbonds); i += UNROLL*nfa1)
1087 /* Collect atoms for UNROLL angles.
1088 * iu indexes into forceatoms, we should not let iu go beyond nbonds.
1091 for (s = 0; s < UNROLL; s++)
1093 type = forceatoms[iu];
1094 ai[s] = forceatoms[iu+1];
1095 aj[s] = forceatoms[iu+2];
1096 ak[s] = forceatoms[iu+3];
1098 coeff[s] = forceparams[type].harmonic.krA;
1099 coeff[UNROLL+s] = forceparams[type].harmonic.rA*DEG2RAD;
1101 /* If you can't use pbc_dx_simd below for PBC, e.g. because
1102 * you can't round in SIMD, use pbc_rvec_sub here.
1104 /* Store the non PBC corrected distances packed and aligned */
1105 for (m = 0; m < DIM; m++)
1107 dr[s + m *UNROLL] = x[ai[s]][m] - x[aj[s]][m];
1108 dr[s + (DIM+m)*UNROLL] = x[ak[s]][m] - x[aj[s]][m];
1111 /* At the end fill the arrays with identical entries */
1112 if (iu + nfa1 < nbonds)
1118 k_S = gmx_load_pr(coeff);
1119 theta0_S = gmx_load_pr(coeff+UNROLL);
1121 rijx_S = gmx_load_pr(dr + 0*UNROLL);
1122 rijy_S = gmx_load_pr(dr + 1*UNROLL);
1123 rijz_S = gmx_load_pr(dr + 2*UNROLL);
1124 rkjx_S = gmx_load_pr(dr + 3*UNROLL);
1125 rkjy_S = gmx_load_pr(dr + 4*UNROLL);
1126 rkjz_S = gmx_load_pr(dr + 5*UNROLL);
1128 pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, &pbc_simd);
1129 pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, &pbc_simd);
1131 rij_rkj_S = gmx_iprod_pr(rijx_S, rijy_S, rijz_S,
1132 rkjx_S, rkjy_S, rkjz_S);
1134 nrij2_S = gmx_norm2_pr(rijx_S, rijy_S, rijz_S);
1135 nrkj2_S = gmx_norm2_pr(rkjx_S, rkjy_S, rkjz_S);
1137 nrij_1_S = gmx_invsqrt_pr(nrij2_S);
1138 nrkj_1_S = gmx_invsqrt_pr(nrkj2_S);
1140 cos_S = gmx_mul_pr(rij_rkj_S, gmx_mul_pr(nrij_1_S, nrkj_1_S));
1142 theta_S = gmx_acos_pr(cos_S);
1144 sin_S = gmx_invsqrt_pr(gmx_max_pr(gmx_sub_pr(one_S, gmx_mul_pr(cos_S, cos_S)),
1146 st_S = gmx_mul_pr(gmx_mul_pr(k_S, gmx_sub_pr(theta0_S, theta_S)),
1148 sth_S = gmx_mul_pr(st_S, cos_S);
1150 cik_S = gmx_mul_pr(st_S, gmx_mul_pr(nrij_1_S, nrkj_1_S));
1151 cii_S = gmx_mul_pr(sth_S, gmx_mul_pr(nrij_1_S, nrij_1_S));
1152 ckk_S = gmx_mul_pr(sth_S, gmx_mul_pr(nrkj_1_S, nrkj_1_S));
1154 f_ix_S = gmx_mul_pr(cii_S, rijx_S);
1155 f_ix_S = gmx_nmsub_pr(cik_S, rkjx_S, f_ix_S);
1156 f_iy_S = gmx_mul_pr(cii_S, rijy_S);
1157 f_iy_S = gmx_nmsub_pr(cik_S, rkjy_S, f_iy_S);
1158 f_iz_S = gmx_mul_pr(cii_S, rijz_S);
1159 f_iz_S = gmx_nmsub_pr(cik_S, rkjz_S, f_iz_S);
1160 f_kx_S = gmx_mul_pr(ckk_S, rkjx_S);
1161 f_kx_S = gmx_nmsub_pr(cik_S, rijx_S, f_kx_S);
1162 f_ky_S = gmx_mul_pr(ckk_S, rkjy_S);
1163 f_ky_S = gmx_nmsub_pr(cik_S, rijy_S, f_ky_S);
1164 f_kz_S = gmx_mul_pr(ckk_S, rkjz_S);
1165 f_kz_S = gmx_nmsub_pr(cik_S, rijz_S, f_kz_S);
1167 gmx_store_pr(f_buf + 0*UNROLL, f_ix_S);
1168 gmx_store_pr(f_buf + 1*UNROLL, f_iy_S);
1169 gmx_store_pr(f_buf + 2*UNROLL, f_iz_S);
1170 gmx_store_pr(f_buf + 3*UNROLL, f_kx_S);
1171 gmx_store_pr(f_buf + 4*UNROLL, f_ky_S);
1172 gmx_store_pr(f_buf + 5*UNROLL, f_kz_S);
1178 for (m = 0; m < DIM; m++)
1180 f[ai[s]][m] += f_buf[s + m*UNROLL];
1181 f[aj[s]][m] -= f_buf[s + m*UNROLL] + f_buf[s + (DIM+m)*UNROLL];
1182 f[ak[s]][m] += f_buf[s + (DIM+m)*UNROLL];
1187 while (s < UNROLL && iu < nbonds);
1192 #endif /* SIMD_BONDEDS */
1194 real linear_angles(int nbonds,
1195 const t_iatom forceatoms[], const t_iparams forceparams[],
1196 const rvec x[], rvec f[], rvec fshift[],
1197 const t_pbc *pbc, const t_graph *g,
1198 real lambda, real *dvdlambda,
1199 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1200 int gmx_unused *global_atom_index)
1202 int i, m, ai, aj, ak, t1, t2, type;
1204 real L1, kA, kB, aA, aB, dr, dr2, va, vtot, a, b, klin;
1205 ivec jt, dt_ij, dt_kj;
1206 rvec r_ij, r_kj, r_ik, dx;
1210 for (i = 0; (i < nbonds); )
1212 type = forceatoms[i++];
1213 ai = forceatoms[i++];
1214 aj = forceatoms[i++];
1215 ak = forceatoms[i++];
1217 kA = forceparams[type].linangle.klinA;
1218 kB = forceparams[type].linangle.klinB;
1219 klin = L1*kA + lambda*kB;
1221 aA = forceparams[type].linangle.aA;
1222 aB = forceparams[type].linangle.aB;
1223 a = L1*aA+lambda*aB;
1226 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
1227 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
1228 rvec_sub(r_ij, r_kj, r_ik);
1231 for (m = 0; (m < DIM); m++)
1233 dr = -a * r_ij[m] - b * r_kj[m];
1238 f_j[m] = -(f_i[m]+f_k[m]);
1244 *dvdlambda += 0.5*(kB-kA)*dr2 + klin*(aB-aA)*iprod(dx, r_ik);
1250 copy_ivec(SHIFT_IVEC(g, aj), jt);
1252 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1253 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1254 t1 = IVEC2IS(dt_ij);
1255 t2 = IVEC2IS(dt_kj);
1257 rvec_inc(fshift[t1], f_i);
1258 rvec_inc(fshift[CENTRAL], f_j);
1259 rvec_inc(fshift[t2], f_k);
1264 real urey_bradley(int nbonds,
1265 const t_iatom forceatoms[], const t_iparams forceparams[],
1266 const rvec x[], rvec f[], rvec fshift[],
1267 const t_pbc *pbc, const t_graph *g,
1268 real lambda, real *dvdlambda,
1269 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1270 int gmx_unused *global_atom_index)
1272 int i, m, ai, aj, ak, t1, t2, type, ki;
1273 rvec r_ij, r_kj, r_ik;
1274 real cos_theta, cos_theta2, theta;
1275 real dVdt, va, vtot, dr, dr2, vbond, fbond, fik;
1276 real kthA, th0A, kUBA, r13A, kthB, th0B, kUBB, r13B;
1277 ivec jt, dt_ij, dt_kj, dt_ik;
1280 for (i = 0; (i < nbonds); )
1282 type = forceatoms[i++];
1283 ai = forceatoms[i++];
1284 aj = forceatoms[i++];
1285 ak = forceatoms[i++];
1286 th0A = forceparams[type].u_b.thetaA*DEG2RAD;
1287 kthA = forceparams[type].u_b.kthetaA;
1288 r13A = forceparams[type].u_b.r13A;
1289 kUBA = forceparams[type].u_b.kUBA;
1290 th0B = forceparams[type].u_b.thetaB*DEG2RAD;
1291 kthB = forceparams[type].u_b.kthetaB;
1292 r13B = forceparams[type].u_b.r13B;
1293 kUBB = forceparams[type].u_b.kUBB;
1295 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
1296 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
1298 *dvdlambda += harmonic(kthA, kthB, th0A, th0B, theta, lambda, &va, &dVdt); /* 21 */
1301 ki = pbc_rvec_sub(pbc, x[ai], x[ak], r_ik); /* 3 */
1302 dr2 = iprod(r_ik, r_ik); /* 5 */
1303 dr = dr2*gmx_invsqrt(dr2); /* 10 */
1305 *dvdlambda += harmonic(kUBA, kUBB, r13A, r13B, dr, lambda, &vbond, &fbond); /* 19 */
1307 cos_theta2 = sqr(cos_theta); /* 1 */
1315 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1316 sth = st*cos_theta; /* 1 */
1320 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1321 theta*RAD2DEG, va, dVdt);
1324 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1325 nrij2 = iprod(r_ij, r_ij);
1327 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
1328 cii = sth/nrij2; /* 10 */
1329 ckk = sth/nrkj2; /* 10 */
1331 for (m = 0; (m < DIM); m++) /* 39 */
1333 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
1334 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
1335 f_j[m] = -f_i[m]-f_k[m];
1342 copy_ivec(SHIFT_IVEC(g, aj), jt);
1344 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1345 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1346 t1 = IVEC2IS(dt_ij);
1347 t2 = IVEC2IS(dt_kj);
1349 rvec_inc(fshift[t1], f_i);
1350 rvec_inc(fshift[CENTRAL], f_j);
1351 rvec_inc(fshift[t2], f_k);
1353 /* Time for the bond calculations */
1359 vtot += vbond; /* 1*/
1360 fbond *= gmx_invsqrt(dr2); /* 6 */
1364 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, ak), dt_ik);
1365 ki = IVEC2IS(dt_ik);
1367 for (m = 0; (m < DIM); m++) /* 15 */
1369 fik = fbond*r_ik[m];
1372 fshift[ki][m] += fik;
1373 fshift[CENTRAL][m] -= fik;
1379 real quartic_angles(int nbonds,
1380 const t_iatom forceatoms[], const t_iparams forceparams[],
1381 const rvec x[], rvec f[], rvec fshift[],
1382 const t_pbc *pbc, const t_graph *g,
1383 real gmx_unused lambda, real gmx_unused *dvdlambda,
1384 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1385 int gmx_unused *global_atom_index)
1387 int i, j, ai, aj, ak, t1, t2, type;
1389 real cos_theta, cos_theta2, theta, dt, dVdt, va, dtp, c, vtot;
1390 ivec jt, dt_ij, dt_kj;
1393 for (i = 0; (i < nbonds); )
1395 type = forceatoms[i++];
1396 ai = forceatoms[i++];
1397 aj = forceatoms[i++];
1398 ak = forceatoms[i++];
1400 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
1401 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
1403 dt = theta - forceparams[type].qangle.theta*DEG2RAD; /* 2 */
1406 va = forceparams[type].qangle.c[0];
1408 for (j = 1; j <= 4; j++)
1410 c = forceparams[type].qangle.c[j];
1419 cos_theta2 = sqr(cos_theta); /* 1 */
1428 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
1429 sth = st*cos_theta; /* 1 */
1433 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
1434 theta*RAD2DEG, va, dVdt);
1437 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1438 nrij2 = iprod(r_ij, r_ij);
1440 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
1441 cii = sth/nrij2; /* 10 */
1442 ckk = sth/nrkj2; /* 10 */
1444 for (m = 0; (m < DIM); m++) /* 39 */
1446 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
1447 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
1448 f_j[m] = -f_i[m]-f_k[m];
1455 copy_ivec(SHIFT_IVEC(g, aj), jt);
1457 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1458 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1459 t1 = IVEC2IS(dt_ij);
1460 t2 = IVEC2IS(dt_kj);
1462 rvec_inc(fshift[t1], f_i);
1463 rvec_inc(fshift[CENTRAL], f_j);
1464 rvec_inc(fshift[t2], f_k);
1470 real dih_angle(const rvec xi, const rvec xj, const rvec xk, const rvec xl,
1472 rvec r_ij, rvec r_kj, rvec r_kl, rvec m, rvec n,
1473 real *sign, int *t1, int *t2, int *t3)
1477 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
1478 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
1479 *t3 = pbc_rvec_sub(pbc, xk, xl, r_kl); /* 3 */
1481 cprod(r_ij, r_kj, m); /* 9 */
1482 cprod(r_kj, r_kl, n); /* 9 */
1483 phi = gmx_angle(m, n); /* 49 (assuming 25 for atan2) */
1484 ipr = iprod(r_ij, n); /* 5 */
1485 (*sign) = (ipr < 0.0) ? -1.0 : 1.0;
1486 phi = (*sign)*phi; /* 1 */
1494 /* As dih_angle above, but calculates 4 dihedral angles at once using SIMD,
1495 * also calculates the pre-factor required for the dihedral force update.
1496 * Note that bv and buf should be register aligned.
1498 static gmx_inline void
1499 dih_angle_simd(const rvec *x,
1500 const int *ai, const int *aj, const int *ak, const int *al,
1501 const pbc_simd_t *pbc,
1504 gmx_mm_pr *mx_S, gmx_mm_pr *my_S, gmx_mm_pr *mz_S,
1505 gmx_mm_pr *nx_S, gmx_mm_pr *ny_S, gmx_mm_pr *nz_S,
1506 gmx_mm_pr *nrkj_m2_S,
1507 gmx_mm_pr *nrkj_n2_S,
1511 #define UNROLL GMX_SIMD_WIDTH_HERE
1513 gmx_mm_pr rijx_S, rijy_S, rijz_S;
1514 gmx_mm_pr rkjx_S, rkjy_S, rkjz_S;
1515 gmx_mm_pr rklx_S, rkly_S, rklz_S;
1516 gmx_mm_pr cx_S, cy_S, cz_S;
1520 gmx_mm_pr iprm_S, iprn_S;
1521 gmx_mm_pr nrkj2_S, nrkj_1_S, nrkj_2_S, nrkj_S;
1523 gmx_mm_pr fmin_S = gmx_set1_pr(GMX_FLOAT_MIN);
1525 for (s = 0; s < UNROLL; s++)
1527 /* If you can't use pbc_dx_simd below for PBC, e.g. because
1528 * you can't round in SIMD, use pbc_rvec_sub here.
1530 for (m = 0; m < DIM; m++)
1532 dr[s + (0*DIM + m)*UNROLL] = x[ai[s]][m] - x[aj[s]][m];
1533 dr[s + (1*DIM + m)*UNROLL] = x[ak[s]][m] - x[aj[s]][m];
1534 dr[s + (2*DIM + m)*UNROLL] = x[ak[s]][m] - x[al[s]][m];
1538 rijx_S = gmx_load_pr(dr + 0*UNROLL);
1539 rijy_S = gmx_load_pr(dr + 1*UNROLL);
1540 rijz_S = gmx_load_pr(dr + 2*UNROLL);
1541 rkjx_S = gmx_load_pr(dr + 3*UNROLL);
1542 rkjy_S = gmx_load_pr(dr + 4*UNROLL);
1543 rkjz_S = gmx_load_pr(dr + 5*UNROLL);
1544 rklx_S = gmx_load_pr(dr + 6*UNROLL);
1545 rkly_S = gmx_load_pr(dr + 7*UNROLL);
1546 rklz_S = gmx_load_pr(dr + 8*UNROLL);
1548 pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, pbc);
1549 pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, pbc);
1550 pbc_dx_simd(&rklx_S, &rkly_S, &rklz_S, pbc);
1552 gmx_cprod_pr(rijx_S, rijy_S, rijz_S,
1553 rkjx_S, rkjy_S, rkjz_S,
1556 gmx_cprod_pr(rkjx_S, rkjy_S, rkjz_S,
1557 rklx_S, rkly_S, rklz_S,
1560 gmx_cprod_pr(*mx_S, *my_S, *mz_S,
1561 *nx_S, *ny_S, *nz_S,
1562 &cx_S, &cy_S, &cz_S);
1564 cn_S = gmx_sqrt_pr(gmx_norm2_pr(cx_S, cy_S, cz_S));
1566 s_S = gmx_iprod_pr(*mx_S, *my_S, *mz_S, *nx_S, *ny_S, *nz_S);
1568 /* Determine the dihedral angle, the sign might need correction */
1569 *phi_S = gmx_atan2_pr(cn_S, s_S);
1571 ipr_S = gmx_iprod_pr(rijx_S, rijy_S, rijz_S,
1572 *nx_S, *ny_S, *nz_S);
1574 iprm_S = gmx_norm2_pr(*mx_S, *my_S, *mz_S);
1575 iprn_S = gmx_norm2_pr(*nx_S, *ny_S, *nz_S);
1577 nrkj2_S = gmx_norm2_pr(rkjx_S, rkjy_S, rkjz_S);
1579 /* Avoid division by zero. When zero, the result is multiplied by 0
1580 * anyhow, so the 3 max below do not affect the final result.
1582 nrkj2_S = gmx_max_pr(nrkj2_S, fmin_S);
1583 nrkj_1_S = gmx_invsqrt_pr(nrkj2_S);
1584 nrkj_2_S = gmx_mul_pr(nrkj_1_S, nrkj_1_S);
1585 nrkj_S = gmx_mul_pr(nrkj2_S, nrkj_1_S);
1587 iprm_S = gmx_max_pr(iprm_S, fmin_S);
1588 iprn_S = gmx_max_pr(iprn_S, fmin_S);
1589 *nrkj_m2_S = gmx_mul_pr(nrkj_S, gmx_inv_pr(iprm_S));
1590 *nrkj_n2_S = gmx_mul_pr(nrkj_S, gmx_inv_pr(iprn_S));
1592 /* Set sign of phi_S with the sign of ipr_S; phi_S is currently positive */
1593 *phi_S = gmx_cpsgn_nonneg_pr(ipr_S, *phi_S);
1595 p_S = gmx_iprod_pr(rijx_S, rijy_S, rijz_S,
1596 rkjx_S, rkjy_S, rkjz_S);
1597 p_S = gmx_mul_pr(p_S, nrkj_2_S);
1599 q_S = gmx_iprod_pr(rklx_S, rkly_S, rklz_S,
1600 rkjx_S, rkjy_S, rkjz_S);
1601 q_S = gmx_mul_pr(q_S, nrkj_2_S);
1603 gmx_store_pr(p, p_S);
1604 gmx_store_pr(q, q_S);
1608 #endif /* SIMD_BONDEDS */
1611 void do_dih_fup(int i, int j, int k, int l, real ddphi,
1612 rvec r_ij, rvec r_kj, rvec r_kl,
1613 rvec m, rvec n, rvec f[], rvec fshift[],
1614 const t_pbc *pbc, const t_graph *g,
1615 const rvec x[], int t1, int t2, int t3)
1618 rvec f_i, f_j, f_k, f_l;
1619 rvec uvec, vvec, svec, dx_jl;
1620 real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1621 real a, b, p, q, toler;
1622 ivec jt, dt_ij, dt_kj, dt_lj;
1624 iprm = iprod(m, m); /* 5 */
1625 iprn = iprod(n, n); /* 5 */
1626 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1627 toler = nrkj2*GMX_REAL_EPS;
1628 if ((iprm > toler) && (iprn > toler))
1630 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1631 nrkj_2 = nrkj_1*nrkj_1; /* 1 */
1632 nrkj = nrkj2*nrkj_1; /* 1 */
1633 a = -ddphi*nrkj/iprm; /* 11 */
1634 svmul(a, m, f_i); /* 3 */
1635 b = ddphi*nrkj/iprn; /* 11 */
1636 svmul(b, n, f_l); /* 3 */
1637 p = iprod(r_ij, r_kj); /* 5 */
1638 p *= nrkj_2; /* 1 */
1639 q = iprod(r_kl, r_kj); /* 5 */
1640 q *= nrkj_2; /* 1 */
1641 svmul(p, f_i, uvec); /* 3 */
1642 svmul(q, f_l, vvec); /* 3 */
1643 rvec_sub(uvec, vvec, svec); /* 3 */
1644 rvec_sub(f_i, svec, f_j); /* 3 */
1645 rvec_add(f_l, svec, f_k); /* 3 */
1646 rvec_inc(f[i], f_i); /* 3 */
1647 rvec_dec(f[j], f_j); /* 3 */
1648 rvec_dec(f[k], f_k); /* 3 */
1649 rvec_inc(f[l], f_l); /* 3 */
1653 copy_ivec(SHIFT_IVEC(g, j), jt);
1654 ivec_sub(SHIFT_IVEC(g, i), jt, dt_ij);
1655 ivec_sub(SHIFT_IVEC(g, k), jt, dt_kj);
1656 ivec_sub(SHIFT_IVEC(g, l), jt, dt_lj);
1657 t1 = IVEC2IS(dt_ij);
1658 t2 = IVEC2IS(dt_kj);
1659 t3 = IVEC2IS(dt_lj);
1663 t3 = pbc_rvec_sub(pbc, x[l], x[j], dx_jl);
1670 rvec_inc(fshift[t1], f_i);
1671 rvec_dec(fshift[CENTRAL], f_j);
1672 rvec_dec(fshift[t2], f_k);
1673 rvec_inc(fshift[t3], f_l);
1678 /* As do_dih_fup above, but without shift forces */
1680 do_dih_fup_noshiftf(int i, int j, int k, int l, real ddphi,
1681 rvec r_ij, rvec r_kj, rvec r_kl,
1682 rvec m, rvec n, rvec f[])
1684 rvec f_i, f_j, f_k, f_l;
1685 rvec uvec, vvec, svec, dx_jl;
1686 real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1687 real a, b, p, q, toler;
1688 ivec jt, dt_ij, dt_kj, dt_lj;
1690 iprm = iprod(m, m); /* 5 */
1691 iprn = iprod(n, n); /* 5 */
1692 nrkj2 = iprod(r_kj, r_kj); /* 5 */
1693 toler = nrkj2*GMX_REAL_EPS;
1694 if ((iprm > toler) && (iprn > toler))
1696 nrkj_1 = gmx_invsqrt(nrkj2); /* 10 */
1697 nrkj_2 = nrkj_1*nrkj_1; /* 1 */
1698 nrkj = nrkj2*nrkj_1; /* 1 */
1699 a = -ddphi*nrkj/iprm; /* 11 */
1700 svmul(a, m, f_i); /* 3 */
1701 b = ddphi*nrkj/iprn; /* 11 */
1702 svmul(b, n, f_l); /* 3 */
1703 p = iprod(r_ij, r_kj); /* 5 */
1704 p *= nrkj_2; /* 1 */
1705 q = iprod(r_kl, r_kj); /* 5 */
1706 q *= nrkj_2; /* 1 */
1707 svmul(p, f_i, uvec); /* 3 */
1708 svmul(q, f_l, vvec); /* 3 */
1709 rvec_sub(uvec, vvec, svec); /* 3 */
1710 rvec_sub(f_i, svec, f_j); /* 3 */
1711 rvec_add(f_l, svec, f_k); /* 3 */
1712 rvec_inc(f[i], f_i); /* 3 */
1713 rvec_dec(f[j], f_j); /* 3 */
1714 rvec_dec(f[k], f_k); /* 3 */
1715 rvec_inc(f[l], f_l); /* 3 */
1719 /* As do_dih_fup_noshiftf above, but with pre-calculated pre-factors */
1720 static gmx_inline void
1721 do_dih_fup_noshiftf_precalc(int i, int j, int k, int l,
1723 real f_i_x, real f_i_y, real f_i_z,
1724 real mf_l_x, real mf_l_y, real mf_l_z,
1727 rvec f_i, f_j, f_k, f_l;
1728 rvec uvec, vvec, svec;
1736 svmul(p, f_i, uvec);
1737 svmul(q, f_l, vvec);
1738 rvec_sub(uvec, vvec, svec);
1739 rvec_sub(f_i, svec, f_j);
1740 rvec_add(f_l, svec, f_k);
1741 rvec_inc(f[i], f_i);
1742 rvec_dec(f[j], f_j);
1743 rvec_dec(f[k], f_k);
1744 rvec_inc(f[l], f_l);
1748 real dopdihs(real cpA, real cpB, real phiA, real phiB, int mult,
1749 real phi, real lambda, real *V, real *F)
1751 real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1752 real L1 = 1.0 - lambda;
1753 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1754 real dph0 = (phiB - phiA)*DEG2RAD;
1755 real cp = L1*cpA + lambda*cpB;
1757 mdphi = mult*phi - ph0;
1759 ddphi = -cp*mult*sdphi;
1760 v1 = 1.0 + cos(mdphi);
1763 dvdlambda = (cpB - cpA)*v1 + cp*dph0*sdphi;
1770 /* That was 40 flops */
1774 dopdihs_noener(real cpA, real cpB, real phiA, real phiB, int mult,
1775 real phi, real lambda, real *F)
1777 real mdphi, sdphi, ddphi;
1778 real L1 = 1.0 - lambda;
1779 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1780 real cp = L1*cpA + lambda*cpB;
1782 mdphi = mult*phi - ph0;
1784 ddphi = -cp*mult*sdphi;
1788 /* That was 20 flops */
1792 dopdihs_mdphi(real cpA, real cpB, real phiA, real phiB, int mult,
1793 real phi, real lambda, real *cp, real *mdphi)
1795 real L1 = 1.0 - lambda;
1796 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1798 *cp = L1*cpA + lambda*cpB;
1800 *mdphi = mult*phi - ph0;
1803 static real dopdihs_min(real cpA, real cpB, real phiA, real phiB, int mult,
1804 real phi, real lambda, real *V, real *F)
1805 /* similar to dopdihs, except for a minus sign *
1806 * and a different treatment of mult/phi0 */
1808 real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1809 real L1 = 1.0 - lambda;
1810 real ph0 = (L1*phiA + lambda*phiB)*DEG2RAD;
1811 real dph0 = (phiB - phiA)*DEG2RAD;
1812 real cp = L1*cpA + lambda*cpB;
1814 mdphi = mult*(phi-ph0);
1816 ddphi = cp*mult*sdphi;
1817 v1 = 1.0-cos(mdphi);
1820 dvdlambda = (cpB-cpA)*v1 + cp*dph0*sdphi;
1827 /* That was 40 flops */
1830 real pdihs(int nbonds,
1831 const t_iatom forceatoms[], const t_iparams forceparams[],
1832 const rvec x[], rvec f[], rvec fshift[],
1833 const t_pbc *pbc, const t_graph *g,
1834 real lambda, real *dvdlambda,
1835 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1836 int gmx_unused *global_atom_index)
1838 int i, type, ai, aj, ak, al;
1840 rvec r_ij, r_kj, r_kl, m, n;
1841 real phi, sign, ddphi, vpd, vtot;
1845 for (i = 0; (i < nbonds); )
1847 type = forceatoms[i++];
1848 ai = forceatoms[i++];
1849 aj = forceatoms[i++];
1850 ak = forceatoms[i++];
1851 al = forceatoms[i++];
1853 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1854 &sign, &t1, &t2, &t3); /* 84 */
1855 *dvdlambda += dopdihs(forceparams[type].pdihs.cpA,
1856 forceparams[type].pdihs.cpB,
1857 forceparams[type].pdihs.phiA,
1858 forceparams[type].pdihs.phiB,
1859 forceparams[type].pdihs.mult,
1860 phi, lambda, &vpd, &ddphi);
1863 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
1864 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
1867 fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
1868 ai, aj, ak, al, phi);
1875 void make_dp_periodic(real *dp) /* 1 flop? */
1877 /* dp cannot be outside (-pi,pi) */
1882 else if (*dp < -M_PI)
1889 /* As pdihs above, but without calculating energies and shift forces */
1891 pdihs_noener(int nbonds,
1892 const t_iatom forceatoms[], const t_iparams forceparams[],
1893 const rvec x[], rvec f[],
1894 const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
1896 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1897 int gmx_unused *global_atom_index)
1899 int i, type, ai, aj, ak, al;
1901 rvec r_ij, r_kj, r_kl, m, n;
1902 real phi, sign, ddphi_tot, ddphi;
1904 for (i = 0; (i < nbonds); )
1906 ai = forceatoms[i+1];
1907 aj = forceatoms[i+2];
1908 ak = forceatoms[i+3];
1909 al = forceatoms[i+4];
1911 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1912 &sign, &t1, &t2, &t3);
1916 /* Loop over dihedrals working on the same atoms,
1917 * so we avoid recalculating angles and force distributions.
1921 type = forceatoms[i];
1922 dopdihs_noener(forceparams[type].pdihs.cpA,
1923 forceparams[type].pdihs.cpB,
1924 forceparams[type].pdihs.phiA,
1925 forceparams[type].pdihs.phiB,
1926 forceparams[type].pdihs.mult,
1927 phi, lambda, &ddphi);
1932 while (i < nbonds &&
1933 forceatoms[i+1] == ai &&
1934 forceatoms[i+2] == aj &&
1935 forceatoms[i+3] == ak &&
1936 forceatoms[i+4] == al);
1938 do_dih_fup_noshiftf(ai, aj, ak, al, ddphi_tot, r_ij, r_kj, r_kl, m, n, f);
1945 /* As pdihs_noner above, but using SIMD to calculate many dihedrals at once */
1947 pdihs_noener_simd(int nbonds,
1948 const t_iatom forceatoms[], const t_iparams forceparams[],
1949 const rvec x[], rvec f[],
1950 const t_pbc *pbc, const t_graph gmx_unused *g,
1951 real gmx_unused lambda,
1952 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1953 int gmx_unused *global_atom_index)
1955 #define UNROLL GMX_SIMD_WIDTH_HERE
1958 int type, ai[UNROLL], aj[UNROLL], ak[UNROLL], al[UNROLL];
1959 int t1[UNROLL], t2[UNROLL], t3[UNROLL];
1961 real dr_array[3*DIM*UNROLL+UNROLL], *dr;
1962 real buf_array[7*UNROLL+UNROLL], *buf;
1963 real *cp, *phi0, *mult, *phi, *p, *q, *sf_i, *msf_l;
1964 gmx_mm_pr phi0_S, phi_S;
1965 gmx_mm_pr mx_S, my_S, mz_S;
1966 gmx_mm_pr nx_S, ny_S, nz_S;
1967 gmx_mm_pr nrkj_m2_S, nrkj_n2_S;
1968 gmx_mm_pr cp_S, mdphi_S, mult_S;
1969 gmx_mm_pr sin_S, cos_S;
1971 gmx_mm_pr sf_i_S, msf_l_S;
1972 pbc_simd_t pbc_simd;
1974 /* Ensure SIMD register alignment */
1975 dr = gmx_simd_align_real(dr_array);
1976 buf = gmx_simd_align_real(buf_array);
1978 /* Extract aligned pointer for parameters and variables */
1979 cp = buf + 0*UNROLL;
1980 phi0 = buf + 1*UNROLL;
1981 mult = buf + 2*UNROLL;
1984 sf_i = buf + 5*UNROLL;
1985 msf_l = buf + 6*UNROLL;
1987 set_pbc_simd(pbc, &pbc_simd);
1989 /* nbonds is the number of dihedrals times nfa1, here we step UNROLL dihs */
1990 for (i = 0; (i < nbonds); i += UNROLL*nfa1)
1992 /* Collect atoms quadruplets for UNROLL dihedrals.
1993 * iu indexes into forceatoms, we should not let iu go beyond nbonds.
1996 for (s = 0; s < UNROLL; s++)
1998 type = forceatoms[iu];
1999 ai[s] = forceatoms[iu+1];
2000 aj[s] = forceatoms[iu+2];
2001 ak[s] = forceatoms[iu+3];
2002 al[s] = forceatoms[iu+4];
2004 cp[s] = forceparams[type].pdihs.cpA;
2005 phi0[s] = forceparams[type].pdihs.phiA*DEG2RAD;
2006 mult[s] = forceparams[type].pdihs.mult;
2008 /* At the end fill the arrays with identical entries */
2009 if (iu + nfa1 < nbonds)
2015 /* Caclulate UNROLL dihedral angles at once */
2016 dih_angle_simd(x, ai, aj, ak, al, &pbc_simd,
2019 &mx_S, &my_S, &mz_S,
2020 &nx_S, &ny_S, &nz_S,
2025 cp_S = gmx_load_pr(cp);
2026 phi0_S = gmx_load_pr(phi0);
2027 mult_S = gmx_load_pr(mult);
2029 mdphi_S = gmx_sub_pr(gmx_mul_pr(mult_S, phi_S), phi0_S);
2031 /* Calculate UNROLL sines at once */
2032 gmx_sincos_pr(mdphi_S, &sin_S, &cos_S);
2033 mddphi_S = gmx_mul_pr(gmx_mul_pr(cp_S, mult_S), sin_S);
2034 sf_i_S = gmx_mul_pr(mddphi_S, nrkj_m2_S);
2035 msf_l_S = gmx_mul_pr(mddphi_S, nrkj_n2_S);
2037 /* After this m?_S will contain f[i] */
2038 mx_S = gmx_mul_pr(sf_i_S, mx_S);
2039 my_S = gmx_mul_pr(sf_i_S, my_S);
2040 mz_S = gmx_mul_pr(sf_i_S, mz_S);
2042 /* After this m?_S will contain -f[l] */
2043 nx_S = gmx_mul_pr(msf_l_S, nx_S);
2044 ny_S = gmx_mul_pr(msf_l_S, ny_S);
2045 nz_S = gmx_mul_pr(msf_l_S, nz_S);
2047 gmx_store_pr(dr + 0*UNROLL, mx_S);
2048 gmx_store_pr(dr + 1*UNROLL, my_S);
2049 gmx_store_pr(dr + 2*UNROLL, mz_S);
2050 gmx_store_pr(dr + 3*UNROLL, nx_S);
2051 gmx_store_pr(dr + 4*UNROLL, ny_S);
2052 gmx_store_pr(dr + 5*UNROLL, nz_S);
2058 do_dih_fup_noshiftf_precalc(ai[s], aj[s], ak[s], al[s],
2063 dr[(DIM+XX)*UNROLL+s],
2064 dr[(DIM+YY)*UNROLL+s],
2065 dr[(DIM+ZZ)*UNROLL+s],
2070 while (s < UNROLL && iu < nbonds);
2075 #endif /* SIMD_BONDEDS */
2078 real idihs(int nbonds,
2079 const t_iatom forceatoms[], const t_iparams forceparams[],
2080 const rvec x[], rvec f[], rvec fshift[],
2081 const t_pbc *pbc, const t_graph *g,
2082 real lambda, real *dvdlambda,
2083 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2084 int gmx_unused *global_atom_index)
2086 int i, type, ai, aj, ak, al;
2088 real phi, phi0, dphi0, ddphi, sign, vtot;
2089 rvec r_ij, r_kj, r_kl, m, n;
2090 real L1, kk, dp, dp2, kA, kB, pA, pB, dvdl_term;
2095 for (i = 0; (i < nbonds); )
2097 type = forceatoms[i++];
2098 ai = forceatoms[i++];
2099 aj = forceatoms[i++];
2100 ak = forceatoms[i++];
2101 al = forceatoms[i++];
2103 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2104 &sign, &t1, &t2, &t3); /* 84 */
2106 /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2107 * force changes if we just apply a normal harmonic.
2108 * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2109 * This means we will never have the periodicity problem, unless
2110 * the dihedral is Pi away from phiO, which is very unlikely due to
2113 kA = forceparams[type].harmonic.krA;
2114 kB = forceparams[type].harmonic.krB;
2115 pA = forceparams[type].harmonic.rA;
2116 pB = forceparams[type].harmonic.rB;
2118 kk = L1*kA + lambda*kB;
2119 phi0 = (L1*pA + lambda*pB)*DEG2RAD;
2120 dphi0 = (pB - pA)*DEG2RAD;
2124 make_dp_periodic(&dp);
2131 dvdl_term += 0.5*(kB - kA)*dp2 - kk*dphi0*dp;
2133 do_dih_fup(ai, aj, ak, al, (real)(-ddphi), r_ij, r_kj, r_kl, m, n,
2134 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2139 fprintf(debug, "idih: (%d,%d,%d,%d) phi=%g\n",
2140 ai, aj, ak, al, phi);
2145 *dvdlambda += dvdl_term;
2150 /*! \brief returns dx, rdist, and dpdl for functions posres() and fbposres()
2152 static void posres_dx(const rvec x, const rvec pos0A, const rvec pos0B,
2153 const rvec comA_sc, const rvec comB_sc,
2155 t_pbc *pbc, int refcoord_scaling, int npbcdim,
2156 rvec dx, rvec rdist, rvec dpdl)
2159 real posA, posB, L1, ref = 0.;
2164 for (m = 0; m < DIM; m++)
2170 switch (refcoord_scaling)
2174 rdist[m] = L1*posA + lambda*posB;
2175 dpdl[m] = posB - posA;
2178 /* Box relative coordinates are stored for dimensions with pbc */
2179 posA *= pbc->box[m][m];
2180 posB *= pbc->box[m][m];
2181 for (d = m+1; d < npbcdim; d++)
2183 posA += pos0A[d]*pbc->box[d][m];
2184 posB += pos0B[d]*pbc->box[d][m];
2186 ref = L1*posA + lambda*posB;
2188 dpdl[m] = posB - posA;
2191 ref = L1*comA_sc[m] + lambda*comB_sc[m];
2192 rdist[m] = L1*posA + lambda*posB;
2193 dpdl[m] = comB_sc[m] - comA_sc[m] + posB - posA;
2196 gmx_fatal(FARGS, "No such scaling method implemented");
2201 ref = L1*posA + lambda*posB;
2203 dpdl[m] = posB - posA;
2206 /* We do pbc_dx with ref+rdist,
2207 * since with only ref we can be up to half a box vector wrong.
2209 pos[m] = ref + rdist[m];
2214 pbc_dx(pbc, x, pos, dx);
2218 rvec_sub(x, pos, dx);
2222 /*! \brief Adds forces of flat-bottomed positions restraints to f[]
2223 * and fixes vir_diag. Returns the flat-bottomed potential. */
2224 real fbposres(int nbonds,
2225 const t_iatom forceatoms[], const t_iparams forceparams[],
2226 const rvec x[], rvec f[], rvec vir_diag,
2228 int refcoord_scaling, int ePBC, rvec com)
2229 /* compute flat-bottomed positions restraints */
2231 int i, ai, m, d, type, npbcdim = 0, fbdim;
2232 const t_iparams *pr;
2234 real ref = 0, dr, dr2, rpot, rfb, rfb2, fact, invdr;
2235 rvec com_sc, rdist, pos, dx, dpdl, fm;
2238 npbcdim = ePBC2npbcdim(ePBC);
2240 if (refcoord_scaling == erscCOM)
2243 for (m = 0; m < npbcdim; m++)
2245 for (d = m; d < npbcdim; d++)
2247 com_sc[m] += com[d]*pbc->box[d][m];
2253 for (i = 0; (i < nbonds); )
2255 type = forceatoms[i++];
2256 ai = forceatoms[i++];
2257 pr = &forceparams[type];
2259 /* same calculation as for normal posres, but with identical A and B states, and lambda==0 */
2260 posres_dx(x[ai], forceparams[type].fbposres.pos0, forceparams[type].fbposres.pos0,
2261 com_sc, com_sc, 0.0,
2262 pbc, refcoord_scaling, npbcdim,
2268 kk = pr->fbposres.k;
2269 rfb = pr->fbposres.r;
2272 /* with rfb<0, push particle out of the sphere/cylinder/layer */
2280 switch (pr->fbposres.geom)
2282 case efbposresSPHERE:
2283 /* spherical flat-bottom posres */
2286 ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2290 v = 0.5*kk*sqr(dr - rfb);
2291 fact = -kk*(dr-rfb)/dr; /* Force pointing to the center pos0 */
2292 svmul(fact, dx, fm);
2295 case efbposresCYLINDER:
2296 /* cylidrical flat-bottom posres in x-y plane. fm[ZZ] = 0. */
2297 dr2 = sqr(dx[XX])+sqr(dx[YY]);
2299 ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2304 v = 0.5*kk*sqr(dr - rfb);
2305 fm[XX] = -kk*(dr-rfb)*dx[XX]*invdr; /* Force pointing to the center */
2306 fm[YY] = -kk*(dr-rfb)*dx[YY]*invdr;
2309 case efbposresX: /* fbdim=XX */
2310 case efbposresY: /* fbdim=YY */
2311 case efbposresZ: /* fbdim=ZZ */
2312 /* 1D flat-bottom potential */
2313 fbdim = pr->fbposres.geom - efbposresX;
2315 if ( ( dr > rfb && bInvert == FALSE ) || ( 0 < dr && dr < rfb && bInvert == TRUE ) )
2317 v = 0.5*kk*sqr(dr - rfb);
2318 fm[fbdim] = -kk*(dr - rfb);
2320 else if ( (dr < (-rfb) && bInvert == FALSE ) || ( (-rfb) < dr && dr < 0 && bInvert == TRUE ))
2322 v = 0.5*kk*sqr(dr + rfb);
2323 fm[fbdim] = -kk*(dr + rfb);
2330 for (m = 0; (m < DIM); m++)
2333 /* Here we correct for the pbc_dx which included rdist */
2334 vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm[m];
2342 real posres(int nbonds,
2343 const t_iatom forceatoms[], const t_iparams forceparams[],
2344 const rvec x[], rvec f[], rvec vir_diag,
2346 real lambda, real *dvdlambda,
2347 int refcoord_scaling, int ePBC, rvec comA, rvec comB)
2349 int i, ai, m, d, type, ki, npbcdim = 0;
2350 const t_iparams *pr;
2353 real posA, posB, ref = 0;
2354 rvec comA_sc, comB_sc, rdist, dpdl, pos, dx;
2355 gmx_bool bForceValid = TRUE;
2357 if ((f == NULL) || (vir_diag == NULL)) /* should both be null together! */
2359 bForceValid = FALSE;
2362 npbcdim = ePBC2npbcdim(ePBC);
2364 if (refcoord_scaling == erscCOM)
2366 clear_rvec(comA_sc);
2367 clear_rvec(comB_sc);
2368 for (m = 0; m < npbcdim; m++)
2370 for (d = m; d < npbcdim; d++)
2372 comA_sc[m] += comA[d]*pbc->box[d][m];
2373 comB_sc[m] += comB[d]*pbc->box[d][m];
2381 for (i = 0; (i < nbonds); )
2383 type = forceatoms[i++];
2384 ai = forceatoms[i++];
2385 pr = &forceparams[type];
2387 /* return dx, rdist, and dpdl */
2388 posres_dx(x[ai], forceparams[type].posres.pos0A, forceparams[type].posres.pos0B,
2389 comA_sc, comB_sc, lambda,
2390 pbc, refcoord_scaling, npbcdim,
2393 for (m = 0; (m < DIM); m++)
2395 kk = L1*pr->posres.fcA[m] + lambda*pr->posres.fcB[m];
2397 vtot += 0.5*kk*dx[m]*dx[m];
2399 0.5*(pr->posres.fcB[m] - pr->posres.fcA[m])*dx[m]*dx[m]
2402 /* Here we correct for the pbc_dx which included rdist */
2406 vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm;
2414 static real low_angres(int nbonds,
2415 const t_iatom forceatoms[], const t_iparams forceparams[],
2416 const rvec x[], rvec f[], rvec fshift[],
2417 const t_pbc *pbc, const t_graph *g,
2418 real lambda, real *dvdlambda,
2421 int i, m, type, ai, aj, ak, al;
2423 real phi, cos_phi, cos_phi2, vid, vtot, dVdphi;
2424 rvec r_ij, r_kl, f_i, f_k = {0, 0, 0};
2425 real st, sth, nrij2, nrkl2, c, cij, ckl;
2428 t2 = 0; /* avoid warning with gcc-3.3. It is never used uninitialized */
2431 ak = al = 0; /* to avoid warnings */
2432 for (i = 0; i < nbonds; )
2434 type = forceatoms[i++];
2435 ai = forceatoms[i++];
2436 aj = forceatoms[i++];
2437 t1 = pbc_rvec_sub(pbc, x[aj], x[ai], r_ij); /* 3 */
2440 ak = forceatoms[i++];
2441 al = forceatoms[i++];
2442 t2 = pbc_rvec_sub(pbc, x[al], x[ak], r_kl); /* 3 */
2451 cos_phi = cos_angle(r_ij, r_kl); /* 25 */
2452 phi = acos(cos_phi); /* 10 */
2454 *dvdlambda += dopdihs_min(forceparams[type].pdihs.cpA,
2455 forceparams[type].pdihs.cpB,
2456 forceparams[type].pdihs.phiA,
2457 forceparams[type].pdihs.phiB,
2458 forceparams[type].pdihs.mult,
2459 phi, lambda, &vid, &dVdphi); /* 40 */
2463 cos_phi2 = sqr(cos_phi); /* 1 */
2466 st = -dVdphi*gmx_invsqrt(1 - cos_phi2); /* 12 */
2467 sth = st*cos_phi; /* 1 */
2468 nrij2 = iprod(r_ij, r_ij); /* 5 */
2469 nrkl2 = iprod(r_kl, r_kl); /* 5 */
2471 c = st*gmx_invsqrt(nrij2*nrkl2); /* 11 */
2472 cij = sth/nrij2; /* 10 */
2473 ckl = sth/nrkl2; /* 10 */
2475 for (m = 0; m < DIM; m++) /* 18+18 */
2477 f_i[m] = (c*r_kl[m]-cij*r_ij[m]);
2482 f_k[m] = (c*r_ij[m]-ckl*r_kl[m]);
2490 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
2493 rvec_inc(fshift[t1], f_i);
2494 rvec_dec(fshift[CENTRAL], f_i);
2499 ivec_sub(SHIFT_IVEC(g, ak), SHIFT_IVEC(g, al), dt);
2502 rvec_inc(fshift[t2], f_k);
2503 rvec_dec(fshift[CENTRAL], f_k);
2508 return vtot; /* 184 / 157 (bZAxis) total */
2511 real angres(int nbonds,
2512 const t_iatom forceatoms[], const t_iparams forceparams[],
2513 const rvec x[], rvec f[], rvec fshift[],
2514 const t_pbc *pbc, const t_graph *g,
2515 real lambda, real *dvdlambda,
2516 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2517 int gmx_unused *global_atom_index)
2519 return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2520 lambda, dvdlambda, FALSE);
2523 real angresz(int nbonds,
2524 const t_iatom forceatoms[], const t_iparams forceparams[],
2525 const rvec x[], rvec f[], rvec fshift[],
2526 const t_pbc *pbc, const t_graph *g,
2527 real lambda, real *dvdlambda,
2528 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2529 int gmx_unused *global_atom_index)
2531 return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2532 lambda, dvdlambda, TRUE);
2535 real dihres(int nbonds,
2536 const t_iatom forceatoms[], const t_iparams forceparams[],
2537 const rvec x[], rvec f[], rvec fshift[],
2538 const t_pbc *pbc, const t_graph *g,
2539 real lambda, real *dvdlambda,
2540 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2541 int gmx_unused *global_atom_index)
2544 int ai, aj, ak, al, i, k, type, t1, t2, t3;
2545 real phi0A, phi0B, dphiA, dphiB, kfacA, kfacB, phi0, dphi, kfac;
2546 real phi, ddphi, ddp, ddp2, dp, sign, d2r, fc, L1;
2547 rvec r_ij, r_kj, r_kl, m, n;
2554 for (i = 0; (i < nbonds); )
2556 type = forceatoms[i++];
2557 ai = forceatoms[i++];
2558 aj = forceatoms[i++];
2559 ak = forceatoms[i++];
2560 al = forceatoms[i++];
2562 phi0A = forceparams[type].dihres.phiA*d2r;
2563 dphiA = forceparams[type].dihres.dphiA*d2r;
2564 kfacA = forceparams[type].dihres.kfacA;
2566 phi0B = forceparams[type].dihres.phiB*d2r;
2567 dphiB = forceparams[type].dihres.dphiB*d2r;
2568 kfacB = forceparams[type].dihres.kfacB;
2570 phi0 = L1*phi0A + lambda*phi0B;
2571 dphi = L1*dphiA + lambda*dphiB;
2572 kfac = L1*kfacA + lambda*kfacB;
2574 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2575 &sign, &t1, &t2, &t3);
2580 fprintf(debug, "dihres[%d]: %d %d %d %d : phi=%f, dphi=%f, kfac=%f\n",
2581 k++, ai, aj, ak, al, phi0, dphi, kfac);
2583 /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2584 * force changes if we just apply a normal harmonic.
2585 * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2586 * This means we will never have the periodicity problem, unless
2587 * the dihedral is Pi away from phiO, which is very unlikely due to
2591 make_dp_periodic(&dp);
2597 else if (dp < -dphi)
2609 vtot += 0.5*kfac*ddp2;
2612 *dvdlambda += 0.5*(kfacB - kfacA)*ddp2;
2613 /* lambda dependence from changing restraint distances */
2616 *dvdlambda -= kfac*ddp*((dphiB - dphiA)+(phi0B - phi0A));
2620 *dvdlambda += kfac*ddp*((dphiB - dphiA)-(phi0B - phi0A));
2622 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
2623 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2630 real unimplemented(int gmx_unused nbonds,
2631 const t_iatom gmx_unused forceatoms[], const t_iparams gmx_unused forceparams[],
2632 const rvec gmx_unused x[], rvec gmx_unused f[], rvec gmx_unused fshift[],
2633 const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
2634 real gmx_unused lambda, real gmx_unused *dvdlambda,
2635 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2636 int gmx_unused *global_atom_index)
2638 gmx_impl("*** you are using a not implemented function");
2640 return 0.0; /* To make the compiler happy */
2643 real rbdihs(int nbonds,
2644 const t_iatom forceatoms[], const t_iparams forceparams[],
2645 const rvec x[], rvec f[], rvec fshift[],
2646 const t_pbc *pbc, const t_graph *g,
2647 real lambda, real *dvdlambda,
2648 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2649 int gmx_unused *global_atom_index)
2651 const real c0 = 0.0, c1 = 1.0, c2 = 2.0, c3 = 3.0, c4 = 4.0, c5 = 5.0;
2652 int type, ai, aj, ak, al, i, j;
2654 rvec r_ij, r_kj, r_kl, m, n;
2655 real parmA[NR_RBDIHS];
2656 real parmB[NR_RBDIHS];
2657 real parm[NR_RBDIHS];
2658 real cos_phi, phi, rbp, rbpBA;
2659 real v, sign, ddphi, sin_phi;
2661 real L1 = 1.0-lambda;
2665 for (i = 0; (i < nbonds); )
2667 type = forceatoms[i++];
2668 ai = forceatoms[i++];
2669 aj = forceatoms[i++];
2670 ak = forceatoms[i++];
2671 al = forceatoms[i++];
2673 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2674 &sign, &t1, &t2, &t3); /* 84 */
2676 /* Change to polymer convention */
2683 phi -= M_PI; /* 1 */
2687 /* Beware of accuracy loss, cannot use 1-sqrt(cos^2) ! */
2690 for (j = 0; (j < NR_RBDIHS); j++)
2692 parmA[j] = forceparams[type].rbdihs.rbcA[j];
2693 parmB[j] = forceparams[type].rbdihs.rbcB[j];
2694 parm[j] = L1*parmA[j]+lambda*parmB[j];
2696 /* Calculate cosine powers */
2697 /* Calculate the energy */
2698 /* Calculate the derivative */
2701 dvdl_term += (parmB[0]-parmA[0]);
2706 rbpBA = parmB[1]-parmA[1];
2707 ddphi += rbp*cosfac;
2710 dvdl_term += cosfac*rbpBA;
2712 rbpBA = parmB[2]-parmA[2];
2713 ddphi += c2*rbp*cosfac;
2716 dvdl_term += cosfac*rbpBA;
2718 rbpBA = parmB[3]-parmA[3];
2719 ddphi += c3*rbp*cosfac;
2722 dvdl_term += cosfac*rbpBA;
2724 rbpBA = parmB[4]-parmA[4];
2725 ddphi += c4*rbp*cosfac;
2728 dvdl_term += cosfac*rbpBA;
2730 rbpBA = parmB[5]-parmA[5];
2731 ddphi += c5*rbp*cosfac;
2734 dvdl_term += cosfac*rbpBA;
2736 ddphi = -ddphi*sin_phi; /* 11 */
2738 do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
2739 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
2742 *dvdlambda += dvdl_term;
2747 int cmap_setup_grid_index(int ip, int grid_spacing, int *ipm1, int *ipp1, int *ipp2)
2753 ip = ip + grid_spacing - 1;
2755 else if (ip > grid_spacing)
2757 ip = ip - grid_spacing - 1;
2766 im1 = grid_spacing - 1;
2768 else if (ip == grid_spacing-2)
2772 else if (ip == grid_spacing-1)
2786 real cmap_dihs(int nbonds,
2787 const t_iatom forceatoms[], const t_iparams forceparams[],
2788 const gmx_cmap_t *cmap_grid,
2789 const rvec x[], rvec f[], rvec fshift[],
2790 const t_pbc *pbc, const t_graph *g,
2791 real gmx_unused lambda, real gmx_unused *dvdlambda,
2792 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2793 int gmx_unused *global_atom_index)
2795 int i, j, k, n, idx;
2796 int ai, aj, ak, al, am;
2797 int a1i, a1j, a1k, a1l, a2i, a2j, a2k, a2l;
2799 int t11, t21, t31, t12, t22, t32;
2800 int iphi1, ip1m1, ip1p1, ip1p2;
2801 int iphi2, ip2m1, ip2p1, ip2p2;
2803 int pos1, pos2, pos3, pos4, tmp;
2805 real ty[4], ty1[4], ty2[4], ty12[4], tc[16], tx[16];
2806 real phi1, psi1, cos_phi1, sin_phi1, sign1, xphi1;
2807 real phi2, psi2, cos_phi2, sin_phi2, sign2, xphi2;
2808 real dx, xx, tt, tu, e, df1, df2, ddf1, ddf2, ddf12, vtot;
2809 real ra21, rb21, rg21, rg1, rgr1, ra2r1, rb2r1, rabr1;
2810 real ra22, rb22, rg22, rg2, rgr2, ra2r2, rb2r2, rabr2;
2811 real fg1, hg1, fga1, hgb1, gaa1, gbb1;
2812 real fg2, hg2, fga2, hgb2, gaa2, gbb2;
2815 rvec r1_ij, r1_kj, r1_kl, m1, n1;
2816 rvec r2_ij, r2_kj, r2_kl, m2, n2;
2817 rvec f1_i, f1_j, f1_k, f1_l;
2818 rvec f2_i, f2_j, f2_k, f2_l;
2819 rvec a1, b1, a2, b2;
2820 rvec f1, g1, h1, f2, g2, h2;
2821 rvec dtf1, dtg1, dth1, dtf2, dtg2, dth2;
2822 ivec jt1, dt1_ij, dt1_kj, dt1_lj;
2823 ivec jt2, dt2_ij, dt2_kj, dt2_lj;
2827 int loop_index[4][4] = {
2834 /* Total CMAP energy */
2837 for (n = 0; n < nbonds; )
2839 /* Five atoms are involved in the two torsions */
2840 type = forceatoms[n++];
2841 ai = forceatoms[n++];
2842 aj = forceatoms[n++];
2843 ak = forceatoms[n++];
2844 al = forceatoms[n++];
2845 am = forceatoms[n++];
2847 /* Which CMAP type is this */
2848 cmapA = forceparams[type].cmap.cmapA;
2849 cmapd = cmap_grid->cmapdata[cmapA].cmap;
2857 phi1 = dih_angle(x[a1i], x[a1j], x[a1k], x[a1l], pbc, r1_ij, r1_kj, r1_kl, m1, n1,
2858 &sign1, &t11, &t21, &t31); /* 84 */
2860 cos_phi1 = cos(phi1);
2862 a1[0] = r1_ij[1]*r1_kj[2]-r1_ij[2]*r1_kj[1];
2863 a1[1] = r1_ij[2]*r1_kj[0]-r1_ij[0]*r1_kj[2];
2864 a1[2] = r1_ij[0]*r1_kj[1]-r1_ij[1]*r1_kj[0]; /* 9 */
2866 b1[0] = r1_kl[1]*r1_kj[2]-r1_kl[2]*r1_kj[1];
2867 b1[1] = r1_kl[2]*r1_kj[0]-r1_kl[0]*r1_kj[2];
2868 b1[2] = r1_kl[0]*r1_kj[1]-r1_kl[1]*r1_kj[0]; /* 9 */
2870 tmp = pbc_rvec_sub(pbc, x[a1l], x[a1k], h1);
2872 ra21 = iprod(a1, a1); /* 5 */
2873 rb21 = iprod(b1, b1); /* 5 */
2874 rg21 = iprod(r1_kj, r1_kj); /* 5 */
2880 rabr1 = sqrt(ra2r1*rb2r1);
2882 sin_phi1 = rg1 * rabr1 * iprod(a1, h1) * (-1);
2884 if (cos_phi1 < -0.5 || cos_phi1 > 0.5)
2886 phi1 = asin(sin_phi1);
2896 phi1 = -M_PI - phi1;
2902 phi1 = acos(cos_phi1);
2910 xphi1 = phi1 + M_PI; /* 1 */
2912 /* Second torsion */
2918 phi2 = dih_angle(x[a2i], x[a2j], x[a2k], x[a2l], pbc, r2_ij, r2_kj, r2_kl, m2, n2,
2919 &sign2, &t12, &t22, &t32); /* 84 */
2921 cos_phi2 = cos(phi2);
2923 a2[0] = r2_ij[1]*r2_kj[2]-r2_ij[2]*r2_kj[1];
2924 a2[1] = r2_ij[2]*r2_kj[0]-r2_ij[0]*r2_kj[2];
2925 a2[2] = r2_ij[0]*r2_kj[1]-r2_ij[1]*r2_kj[0]; /* 9 */
2927 b2[0] = r2_kl[1]*r2_kj[2]-r2_kl[2]*r2_kj[1];
2928 b2[1] = r2_kl[2]*r2_kj[0]-r2_kl[0]*r2_kj[2];
2929 b2[2] = r2_kl[0]*r2_kj[1]-r2_kl[1]*r2_kj[0]; /* 9 */
2931 tmp = pbc_rvec_sub(pbc, x[a2l], x[a2k], h2);
2933 ra22 = iprod(a2, a2); /* 5 */
2934 rb22 = iprod(b2, b2); /* 5 */
2935 rg22 = iprod(r2_kj, r2_kj); /* 5 */
2941 rabr2 = sqrt(ra2r2*rb2r2);
2943 sin_phi2 = rg2 * rabr2 * iprod(a2, h2) * (-1);
2945 if (cos_phi2 < -0.5 || cos_phi2 > 0.5)
2947 phi2 = asin(sin_phi2);
2957 phi2 = -M_PI - phi2;
2963 phi2 = acos(cos_phi2);
2971 xphi2 = phi2 + M_PI; /* 1 */
2973 /* Range mangling */
2976 xphi1 = xphi1 + 2*M_PI;
2978 else if (xphi1 >= 2*M_PI)
2980 xphi1 = xphi1 - 2*M_PI;
2985 xphi2 = xphi2 + 2*M_PI;
2987 else if (xphi2 >= 2*M_PI)
2989 xphi2 = xphi2 - 2*M_PI;
2992 /* Number of grid points */
2993 dx = 2*M_PI / cmap_grid->grid_spacing;
2995 /* Where on the grid are we */
2996 iphi1 = (int)(xphi1/dx);
2997 iphi2 = (int)(xphi2/dx);
2999 iphi1 = cmap_setup_grid_index(iphi1, cmap_grid->grid_spacing, &ip1m1, &ip1p1, &ip1p2);
3000 iphi2 = cmap_setup_grid_index(iphi2, cmap_grid->grid_spacing, &ip2m1, &ip2p1, &ip2p2);
3002 pos1 = iphi1*cmap_grid->grid_spacing+iphi2;
3003 pos2 = ip1p1*cmap_grid->grid_spacing+iphi2;
3004 pos3 = ip1p1*cmap_grid->grid_spacing+ip2p1;
3005 pos4 = iphi1*cmap_grid->grid_spacing+ip2p1;
3007 ty[0] = cmapd[pos1*4];
3008 ty[1] = cmapd[pos2*4];
3009 ty[2] = cmapd[pos3*4];
3010 ty[3] = cmapd[pos4*4];
3012 ty1[0] = cmapd[pos1*4+1];
3013 ty1[1] = cmapd[pos2*4+1];
3014 ty1[2] = cmapd[pos3*4+1];
3015 ty1[3] = cmapd[pos4*4+1];
3017 ty2[0] = cmapd[pos1*4+2];
3018 ty2[1] = cmapd[pos2*4+2];
3019 ty2[2] = cmapd[pos3*4+2];
3020 ty2[3] = cmapd[pos4*4+2];
3022 ty12[0] = cmapd[pos1*4+3];
3023 ty12[1] = cmapd[pos2*4+3];
3024 ty12[2] = cmapd[pos3*4+3];
3025 ty12[3] = cmapd[pos4*4+3];
3027 /* Switch to degrees */
3028 dx = 360.0 / cmap_grid->grid_spacing;
3029 xphi1 = xphi1 * RAD2DEG;
3030 xphi2 = xphi2 * RAD2DEG;
3032 for (i = 0; i < 4; i++) /* 16 */
3035 tx[i+4] = ty1[i]*dx;
3036 tx[i+8] = ty2[i]*dx;
3037 tx[i+12] = ty12[i]*dx*dx;
3041 for (i = 0; i < 4; i++) /* 1056 */
3043 for (j = 0; j < 4; j++)
3046 for (k = 0; k < 16; k++)
3048 xx = xx + cmap_coeff_matrix[k*16+idx]*tx[k];
3056 tt = (xphi1-iphi1*dx)/dx;
3057 tu = (xphi2-iphi2*dx)/dx;
3066 for (i = 3; i >= 0; i--)
3068 l1 = loop_index[i][3];
3069 l2 = loop_index[i][2];
3070 l3 = loop_index[i][1];
3072 e = tt * e + ((tc[i*4+3]*tu+tc[i*4+2])*tu + tc[i*4+1])*tu+tc[i*4];
3073 df1 = tu * df1 + (3.0*tc[l1]*tt+2.0*tc[l2])*tt+tc[l3];
3074 df2 = tt * df2 + (3.0*tc[i*4+3]*tu+2.0*tc[i*4+2])*tu+tc[i*4+1];
3075 ddf1 = tu * ddf1 + 2.0*3.0*tc[l1]*tt+2.0*tc[l2];
3076 ddf2 = tt * ddf2 + 2.0*3.0*tc[4*i+3]*tu+2.0*tc[4*i+2];
3079 ddf12 = tc[5] + 2.0*tc[9]*tt + 3.0*tc[13]*tt*tt + 2.0*tu*(tc[6]+2.0*tc[10]*tt+3.0*tc[14]*tt*tt) +
3080 3.0*tu*tu*(tc[7]+2.0*tc[11]*tt+3.0*tc[15]*tt*tt);
3085 ddf1 = ddf1 * fac * fac;
3086 ddf2 = ddf2 * fac * fac;
3087 ddf12 = ddf12 * fac * fac;
3092 /* Do forces - first torsion */
3093 fg1 = iprod(r1_ij, r1_kj);
3094 hg1 = iprod(r1_kl, r1_kj);
3095 fga1 = fg1*ra2r1*rgr1;
3096 hgb1 = hg1*rb2r1*rgr1;
3100 for (i = 0; i < DIM; i++)
3102 dtf1[i] = gaa1 * a1[i];
3103 dtg1[i] = fga1 * a1[i] - hgb1 * b1[i];
3104 dth1[i] = gbb1 * b1[i];
3106 f1[i] = df1 * dtf1[i];
3107 g1[i] = df1 * dtg1[i];
3108 h1[i] = df1 * dth1[i];
3111 f1_j[i] = -f1[i] - g1[i];
3112 f1_k[i] = h1[i] + g1[i];
3115 f[a1i][i] = f[a1i][i] + f1_i[i];
3116 f[a1j][i] = f[a1j][i] + f1_j[i]; /* - f1[i] - g1[i] */
3117 f[a1k][i] = f[a1k][i] + f1_k[i]; /* h1[i] + g1[i] */
3118 f[a1l][i] = f[a1l][i] + f1_l[i]; /* h1[i] */
3121 /* Do forces - second torsion */
3122 fg2 = iprod(r2_ij, r2_kj);
3123 hg2 = iprod(r2_kl, r2_kj);
3124 fga2 = fg2*ra2r2*rgr2;
3125 hgb2 = hg2*rb2r2*rgr2;
3129 for (i = 0; i < DIM; i++)
3131 dtf2[i] = gaa2 * a2[i];
3132 dtg2[i] = fga2 * a2[i] - hgb2 * b2[i];
3133 dth2[i] = gbb2 * b2[i];
3135 f2[i] = df2 * dtf2[i];
3136 g2[i] = df2 * dtg2[i];
3137 h2[i] = df2 * dth2[i];
3140 f2_j[i] = -f2[i] - g2[i];
3141 f2_k[i] = h2[i] + g2[i];
3144 f[a2i][i] = f[a2i][i] + f2_i[i]; /* f2[i] */
3145 f[a2j][i] = f[a2j][i] + f2_j[i]; /* - f2[i] - g2[i] */
3146 f[a2k][i] = f[a2k][i] + f2_k[i]; /* h2[i] + g2[i] */
3147 f[a2l][i] = f[a2l][i] + f2_l[i]; /* - h2[i] */
3153 copy_ivec(SHIFT_IVEC(g, a1j), jt1);
3154 ivec_sub(SHIFT_IVEC(g, a1i), jt1, dt1_ij);
3155 ivec_sub(SHIFT_IVEC(g, a1k), jt1, dt1_kj);
3156 ivec_sub(SHIFT_IVEC(g, a1l), jt1, dt1_lj);
3157 t11 = IVEC2IS(dt1_ij);
3158 t21 = IVEC2IS(dt1_kj);
3159 t31 = IVEC2IS(dt1_lj);
3161 copy_ivec(SHIFT_IVEC(g, a2j), jt2);
3162 ivec_sub(SHIFT_IVEC(g, a2i), jt2, dt2_ij);
3163 ivec_sub(SHIFT_IVEC(g, a2k), jt2, dt2_kj);
3164 ivec_sub(SHIFT_IVEC(g, a2l), jt2, dt2_lj);
3165 t12 = IVEC2IS(dt2_ij);
3166 t22 = IVEC2IS(dt2_kj);
3167 t32 = IVEC2IS(dt2_lj);
3171 t31 = pbc_rvec_sub(pbc, x[a1l], x[a1j], h1);
3172 t32 = pbc_rvec_sub(pbc, x[a2l], x[a2j], h2);
3180 rvec_inc(fshift[t11], f1_i);
3181 rvec_inc(fshift[CENTRAL], f1_j);
3182 rvec_inc(fshift[t21], f1_k);
3183 rvec_inc(fshift[t31], f1_l);
3185 rvec_inc(fshift[t21], f2_i);
3186 rvec_inc(fshift[CENTRAL], f2_j);
3187 rvec_inc(fshift[t22], f2_k);
3188 rvec_inc(fshift[t32], f2_l);
3195 /***********************************************************
3197 * G R O M O S 9 6 F U N C T I O N S
3199 ***********************************************************/
3200 real g96harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
3203 const real half = 0.5;
3204 real L1, kk, x0, dx, dx2;
3205 real v, f, dvdlambda;
3208 kk = L1*kA+lambda*kB;
3209 x0 = L1*xA+lambda*xB;
3216 dvdlambda = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
3223 /* That was 21 flops */
3226 real g96bonds(int nbonds,
3227 const t_iatom forceatoms[], const t_iparams forceparams[],
3228 const rvec x[], rvec f[], rvec fshift[],
3229 const t_pbc *pbc, const t_graph *g,
3230 real lambda, real *dvdlambda,
3231 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3232 int gmx_unused *global_atom_index)
3234 int i, m, ki, ai, aj, type;
3235 real dr2, fbond, vbond, fij, vtot;
3240 for (i = 0; (i < nbonds); )
3242 type = forceatoms[i++];
3243 ai = forceatoms[i++];
3244 aj = forceatoms[i++];
3246 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
3247 dr2 = iprod(dx, dx); /* 5 */
3249 *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3250 forceparams[type].harmonic.krB,
3251 forceparams[type].harmonic.rA,
3252 forceparams[type].harmonic.rB,
3253 dr2, lambda, &vbond, &fbond);
3255 vtot += 0.5*vbond; /* 1*/
3259 fprintf(debug, "G96-BONDS: dr = %10g vbond = %10g fbond = %10g\n",
3260 sqrt(dr2), vbond, fbond);
3266 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3269 for (m = 0; (m < DIM); m++) /* 15 */
3274 fshift[ki][m] += fij;
3275 fshift[CENTRAL][m] -= fij;
3281 real g96bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
3282 rvec r_ij, rvec r_kj,
3284 /* Return value is the angle between the bonds i-j and j-k */
3288 *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /* 3 */
3289 *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /* 3 */
3291 costh = cos_angle(r_ij, r_kj); /* 25 */
3296 real g96angles(int nbonds,
3297 const t_iatom forceatoms[], const t_iparams forceparams[],
3298 const rvec x[], rvec f[], rvec fshift[],
3299 const t_pbc *pbc, const t_graph *g,
3300 real lambda, real *dvdlambda,
3301 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3302 int gmx_unused *global_atom_index)
3304 int i, ai, aj, ak, type, m, t1, t2;
3306 real cos_theta, dVdt, va, vtot;
3307 real rij_1, rij_2, rkj_1, rkj_2, rijrkj_1;
3309 ivec jt, dt_ij, dt_kj;
3312 for (i = 0; (i < nbonds); )
3314 type = forceatoms[i++];
3315 ai = forceatoms[i++];
3316 aj = forceatoms[i++];
3317 ak = forceatoms[i++];
3319 cos_theta = g96bond_angle(x[ai], x[aj], x[ak], pbc, r_ij, r_kj, &t1, &t2);
3321 *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3322 forceparams[type].harmonic.krB,
3323 forceparams[type].harmonic.rA,
3324 forceparams[type].harmonic.rB,
3325 cos_theta, lambda, &va, &dVdt);
3328 rij_1 = gmx_invsqrt(iprod(r_ij, r_ij));
3329 rkj_1 = gmx_invsqrt(iprod(r_kj, r_kj));
3330 rij_2 = rij_1*rij_1;
3331 rkj_2 = rkj_1*rkj_1;
3332 rijrkj_1 = rij_1*rkj_1; /* 23 */
3337 fprintf(debug, "G96ANGLES: costheta = %10g vth = %10g dV/dct = %10g\n",
3338 cos_theta, va, dVdt);
3341 for (m = 0; (m < DIM); m++) /* 42 */
3343 f_i[m] = dVdt*(r_kj[m]*rijrkj_1 - r_ij[m]*rij_2*cos_theta);
3344 f_k[m] = dVdt*(r_ij[m]*rijrkj_1 - r_kj[m]*rkj_2*cos_theta);
3345 f_j[m] = -f_i[m]-f_k[m];
3353 copy_ivec(SHIFT_IVEC(g, aj), jt);
3355 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3356 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3357 t1 = IVEC2IS(dt_ij);
3358 t2 = IVEC2IS(dt_kj);
3360 rvec_inc(fshift[t1], f_i);
3361 rvec_inc(fshift[CENTRAL], f_j);
3362 rvec_inc(fshift[t2], f_k); /* 9 */
3368 real cross_bond_bond(int nbonds,
3369 const t_iatom forceatoms[], const t_iparams forceparams[],
3370 const rvec x[], rvec f[], rvec fshift[],
3371 const t_pbc *pbc, const t_graph *g,
3372 real gmx_unused lambda, real gmx_unused *dvdlambda,
3373 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3374 int gmx_unused *global_atom_index)
3376 /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3379 int i, ai, aj, ak, type, m, t1, t2;
3381 real vtot, vrr, s1, s2, r1, r2, r1e, r2e, krr;
3383 ivec jt, dt_ij, dt_kj;
3386 for (i = 0; (i < nbonds); )
3388 type = forceatoms[i++];
3389 ai = forceatoms[i++];
3390 aj = forceatoms[i++];
3391 ak = forceatoms[i++];
3392 r1e = forceparams[type].cross_bb.r1e;
3393 r2e = forceparams[type].cross_bb.r2e;
3394 krr = forceparams[type].cross_bb.krr;
3396 /* Compute distance vectors ... */
3397 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3398 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3400 /* ... and their lengths */
3404 /* Deviations from ideality */
3408 /* Energy (can be negative!) */
3413 svmul(-krr*s2/r1, r_ij, f_i);
3414 svmul(-krr*s1/r2, r_kj, f_k);
3416 for (m = 0; (m < DIM); m++) /* 12 */
3418 f_j[m] = -f_i[m] - f_k[m];
3427 copy_ivec(SHIFT_IVEC(g, aj), jt);
3429 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3430 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3431 t1 = IVEC2IS(dt_ij);
3432 t2 = IVEC2IS(dt_kj);
3434 rvec_inc(fshift[t1], f_i);
3435 rvec_inc(fshift[CENTRAL], f_j);
3436 rvec_inc(fshift[t2], f_k); /* 9 */
3442 real cross_bond_angle(int nbonds,
3443 const t_iatom forceatoms[], const t_iparams forceparams[],
3444 const rvec x[], rvec f[], rvec fshift[],
3445 const t_pbc *pbc, const t_graph *g,
3446 real gmx_unused lambda, real gmx_unused *dvdlambda,
3447 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3448 int gmx_unused *global_atom_index)
3450 /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3453 int i, ai, aj, ak, type, m, t1, t2, t3;
3454 rvec r_ij, r_kj, r_ik;
3455 real vtot, vrt, s1, s2, s3, r1, r2, r3, r1e, r2e, r3e, krt, k1, k2, k3;
3457 ivec jt, dt_ij, dt_kj;
3460 for (i = 0; (i < nbonds); )
3462 type = forceatoms[i++];
3463 ai = forceatoms[i++];
3464 aj = forceatoms[i++];
3465 ak = forceatoms[i++];
3466 r1e = forceparams[type].cross_ba.r1e;
3467 r2e = forceparams[type].cross_ba.r2e;
3468 r3e = forceparams[type].cross_ba.r3e;
3469 krt = forceparams[type].cross_ba.krt;
3471 /* Compute distance vectors ... */
3472 t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3473 t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3474 t3 = pbc_rvec_sub(pbc, x[ai], x[ak], r_ik);
3476 /* ... and their lengths */
3481 /* Deviations from ideality */
3486 /* Energy (can be negative!) */
3487 vrt = krt*s3*(s1+s2);
3493 k3 = -krt*(s1+s2)/r3;
3494 for (m = 0; (m < DIM); m++)
3496 f_i[m] = k1*r_ij[m] + k3*r_ik[m];
3497 f_k[m] = k2*r_kj[m] - k3*r_ik[m];
3498 f_j[m] = -f_i[m] - f_k[m];
3501 for (m = 0; (m < DIM); m++) /* 12 */
3511 copy_ivec(SHIFT_IVEC(g, aj), jt);
3513 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3514 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3515 t1 = IVEC2IS(dt_ij);
3516 t2 = IVEC2IS(dt_kj);
3518 rvec_inc(fshift[t1], f_i);
3519 rvec_inc(fshift[CENTRAL], f_j);
3520 rvec_inc(fshift[t2], f_k); /* 9 */
3526 static real bonded_tab(const char *type, int table_nr,
3527 const bondedtable_t *table, real kA, real kB, real r,
3528 real lambda, real *V, real *F)
3530 real k, tabscale, *VFtab, rt, eps, eps2, Yt, Ft, Geps, Heps2, Fp, VV, FF;
3532 real v, f, dvdlambda;
3534 k = (1.0 - lambda)*kA + lambda*kB;
3536 tabscale = table->scale;
3537 VFtab = table->data;
3543 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",
3544 type, table_nr, r, n0, n0+1, table->n);
3551 Geps = VFtab[nnn+2]*eps;
3552 Heps2 = VFtab[nnn+3]*eps2;
3553 Fp = Ft + Geps + Heps2;
3555 FF = Fp + Geps + 2.0*Heps2;
3557 *F = -k*FF*tabscale;
3559 dvdlambda = (kB - kA)*VV;
3563 /* That was 22 flops */
3566 real tab_bonds(int nbonds,
3567 const t_iatom forceatoms[], const t_iparams forceparams[],
3568 const rvec x[], rvec f[], rvec fshift[],
3569 const t_pbc *pbc, const t_graph *g,
3570 real lambda, real *dvdlambda,
3571 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
3572 int gmx_unused *global_atom_index)
3574 int i, m, ki, ai, aj, type, table;
3575 real dr, dr2, fbond, vbond, fij, vtot;
3580 for (i = 0; (i < nbonds); )
3582 type = forceatoms[i++];
3583 ai = forceatoms[i++];
3584 aj = forceatoms[i++];
3586 ki = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /* 3 */
3587 dr2 = iprod(dx, dx); /* 5 */
3588 dr = dr2*gmx_invsqrt(dr2); /* 10 */
3590 table = forceparams[type].tab.table;
3592 *dvdlambda += bonded_tab("bond", table,
3593 &fcd->bondtab[table],
3594 forceparams[type].tab.kA,
3595 forceparams[type].tab.kB,
3596 dr, lambda, &vbond, &fbond); /* 22 */
3604 vtot += vbond; /* 1*/
3605 fbond *= gmx_invsqrt(dr2); /* 6 */
3609 fprintf(debug, "TABBONDS: dr = %10g vbond = %10g fbond = %10g\n",
3615 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3618 for (m = 0; (m < DIM); m++) /* 15 */
3623 fshift[ki][m] += fij;
3624 fshift[CENTRAL][m] -= fij;
3630 real tab_angles(int nbonds,
3631 const t_iatom forceatoms[], const t_iparams forceparams[],
3632 const rvec x[], rvec f[], rvec fshift[],
3633 const t_pbc *pbc, const t_graph *g,
3634 real lambda, real *dvdlambda,
3635 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
3636 int gmx_unused *global_atom_index)
3638 int i, ai, aj, ak, t1, t2, type, table;
3640 real cos_theta, cos_theta2, theta, dVdt, va, vtot;
3641 ivec jt, dt_ij, dt_kj;
3644 for (i = 0; (i < nbonds); )
3646 type = forceatoms[i++];
3647 ai = forceatoms[i++];
3648 aj = forceatoms[i++];
3649 ak = forceatoms[i++];
3651 theta = bond_angle(x[ai], x[aj], x[ak], pbc,
3652 r_ij, r_kj, &cos_theta, &t1, &t2); /* 41 */
3654 table = forceparams[type].tab.table;
3656 *dvdlambda += bonded_tab("angle", table,
3657 &fcd->angletab[table],
3658 forceparams[type].tab.kA,
3659 forceparams[type].tab.kB,
3660 theta, lambda, &va, &dVdt); /* 22 */
3663 cos_theta2 = sqr(cos_theta); /* 1 */
3672 st = dVdt*gmx_invsqrt(1 - cos_theta2); /* 12 */
3673 sth = st*cos_theta; /* 1 */
3677 fprintf(debug, "ANGLES: theta = %10g vth = %10g dV/dtheta = %10g\n",
3678 theta*RAD2DEG, va, dVdt);
3681 nrkj2 = iprod(r_kj, r_kj); /* 5 */
3682 nrij2 = iprod(r_ij, r_ij);
3684 cik = st*gmx_invsqrt(nrkj2*nrij2); /* 12 */
3685 cii = sth/nrij2; /* 10 */
3686 ckk = sth/nrkj2; /* 10 */
3688 for (m = 0; (m < DIM); m++) /* 39 */
3690 f_i[m] = -(cik*r_kj[m]-cii*r_ij[m]);
3691 f_k[m] = -(cik*r_ij[m]-ckk*r_kj[m]);
3692 f_j[m] = -f_i[m]-f_k[m];
3699 copy_ivec(SHIFT_IVEC(g, aj), jt);
3701 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3702 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3703 t1 = IVEC2IS(dt_ij);
3704 t2 = IVEC2IS(dt_kj);
3706 rvec_inc(fshift[t1], f_i);
3707 rvec_inc(fshift[CENTRAL], f_j);
3708 rvec_inc(fshift[t2], f_k);
3714 real tab_dihs(int nbonds,
3715 const t_iatom forceatoms[], const t_iparams forceparams[],
3716 const rvec x[], rvec f[], rvec fshift[],
3717 const t_pbc *pbc, const t_graph *g,
3718 real lambda, real *dvdlambda,
3719 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
3720 int gmx_unused *global_atom_index)
3722 int i, type, ai, aj, ak, al, table;
3724 rvec r_ij, r_kj, r_kl, m, n;
3725 real phi, sign, ddphi, vpd, vtot;
3728 for (i = 0; (i < nbonds); )
3730 type = forceatoms[i++];
3731 ai = forceatoms[i++];
3732 aj = forceatoms[i++];
3733 ak = forceatoms[i++];
3734 al = forceatoms[i++];
3736 phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
3737 &sign, &t1, &t2, &t3); /* 84 */
3739 table = forceparams[type].tab.table;
3741 /* Hopefully phi+M_PI never results in values < 0 */
3742 *dvdlambda += bonded_tab("dihedral", table,
3743 &fcd->dihtab[table],
3744 forceparams[type].tab.kA,
3745 forceparams[type].tab.kB,
3746 phi+M_PI, lambda, &vpd, &ddphi);
3749 do_dih_fup(ai, aj, ak, al, -ddphi, r_ij, r_kj, r_kl, m, n,
3750 f, fshift, pbc, g, x, t1, t2, t3); /* 112 */
3753 fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
3754 ai, aj, ak, al, phi);
3761 /* Return if this is a potential calculated in bondfree.c,
3762 * i.e. an interaction that actually calculates a potential and
3763 * works on multiple atoms (not e.g. a connection or a position restraint).
3765 static gmx_inline gmx_bool ftype_is_bonded_potential(int ftype)
3768 (interaction_function[ftype].flags & IF_BOND) &&
3769 !(ftype == F_CONNBONDS || ftype == F_POSRES) &&
3770 (ftype < F_GB12 || ftype > F_GB14);
3773 static void divide_bondeds_over_threads(t_idef *idef, int nthreads)
3780 idef->nthreads = nthreads;
3782 if (F_NRE*(nthreads+1) > idef->il_thread_division_nalloc)
3784 idef->il_thread_division_nalloc = F_NRE*(nthreads+1);
3785 snew(idef->il_thread_division, idef->il_thread_division_nalloc);
3788 for (ftype = 0; ftype < F_NRE; ftype++)
3790 if (ftype_is_bonded_potential(ftype))
3792 nat1 = interaction_function[ftype].nratoms + 1;
3794 for (t = 0; t <= nthreads; t++)
3796 /* Divide the interactions equally over the threads.
3797 * When the different types of bonded interactions
3798 * are distributed roughly equally over the threads,
3799 * this should lead to well localized output into
3800 * the force buffer on each thread.
3801 * If this is not the case, a more advanced scheme
3802 * (not implemented yet) will do better.
3804 il_nr_thread = (((idef->il[ftype].nr/nat1)*t)/nthreads)*nat1;
3806 /* Ensure that distance restraint pairs with the same label
3807 * end up on the same thread.
3808 * This is slighlty tricky code, since the next for iteration
3809 * may have an initial il_nr_thread lower than the final value
3810 * in the previous iteration, but this will anyhow be increased
3811 * to the approriate value again by this while loop.
3813 while (ftype == F_DISRES &&
3815 il_nr_thread < idef->il[ftype].nr &&
3816 idef->iparams[idef->il[ftype].iatoms[il_nr_thread]].disres.label ==
3817 idef->iparams[idef->il[ftype].iatoms[il_nr_thread-nat1]].disres.label)
3819 il_nr_thread += nat1;
3822 idef->il_thread_division[ftype*(nthreads+1)+t] = il_nr_thread;
3829 calc_bonded_reduction_mask(const t_idef *idef,
3834 int ftype, nb, nat1, nb0, nb1, i, a;
3838 for (ftype = 0; ftype < F_NRE; ftype++)
3840 if (ftype_is_bonded_potential(ftype))
3842 nb = idef->il[ftype].nr;
3845 nat1 = interaction_function[ftype].nratoms + 1;
3847 /* Divide this interaction equally over the threads.
3848 * This is not stored: should match division in calc_bonds.
3850 nb0 = idef->il_thread_division[ftype*(nt+1)+t];
3851 nb1 = idef->il_thread_division[ftype*(nt+1)+t+1];
3853 for (i = nb0; i < nb1; i += nat1)
3855 for (a = 1; a < nat1; a++)
3857 mask |= (1U << (idef->il[ftype].iatoms[i+a]>>shift));
3867 void setup_bonded_threading(t_forcerec *fr, t_idef *idef)
3869 #define MAX_BLOCK_BITS 32
3873 assert(fr->nthreads >= 1);
3875 /* Divide the bonded interaction over the threads */
3876 divide_bondeds_over_threads(idef, fr->nthreads);
3878 if (fr->nthreads == 1)
3885 /* We divide the force array in a maximum of 32 blocks.
3886 * Minimum force block reduction size is 2^6=64.
3889 while (fr->natoms_force > (int)(MAX_BLOCK_BITS*(1U<<fr->red_ashift)))
3895 fprintf(debug, "bonded force buffer block atom shift %d bits\n",
3899 /* Determine to which blocks each thread's bonded force calculation
3900 * contributes. Store this is a mask for each thread.
3902 #pragma omp parallel for num_threads(fr->nthreads) schedule(static)
3903 for (t = 1; t < fr->nthreads; t++)
3905 fr->f_t[t].red_mask =
3906 calc_bonded_reduction_mask(idef, fr->red_ashift, t, fr->nthreads);
3909 /* Determine the maximum number of blocks we need to reduce over */
3912 for (t = 0; t < fr->nthreads; t++)
3915 for (b = 0; b < MAX_BLOCK_BITS; b++)
3917 if (fr->f_t[t].red_mask & (1U<<b))
3919 fr->red_nblock = max(fr->red_nblock, b+1);
3925 fprintf(debug, "thread %d flags %x count %d\n",
3926 t, fr->f_t[t].red_mask, c);
3932 fprintf(debug, "Number of blocks to reduce: %d of size %d\n",
3933 fr->red_nblock, 1<<fr->red_ashift);
3934 fprintf(debug, "Reduction density %.2f density/#thread %.2f\n",
3935 ctot*(1<<fr->red_ashift)/(double)fr->natoms_force,
3936 ctot*(1<<fr->red_ashift)/(double)(fr->natoms_force*fr->nthreads));
3940 static void zero_thread_forces(f_thread_t *f_t, int n,
3941 int nblock, int blocksize)
3943 int b, a0, a1, a, i, j;
3945 if (n > f_t->f_nalloc)
3947 f_t->f_nalloc = over_alloc_large(n);
3948 srenew(f_t->f, f_t->f_nalloc);
3951 if (f_t->red_mask != 0)
3953 for (b = 0; b < nblock; b++)
3955 if (f_t->red_mask && (1U<<b))
3958 a1 = min((b+1)*blocksize, n);
3959 for (a = a0; a < a1; a++)
3961 clear_rvec(f_t->f[a]);
3966 for (i = 0; i < SHIFTS; i++)
3968 clear_rvec(f_t->fshift[i]);
3970 for (i = 0; i < F_NRE; i++)
3974 for (i = 0; i < egNR; i++)
3976 for (j = 0; j < f_t->grpp.nener; j++)
3978 f_t->grpp.ener[i][j] = 0;
3981 for (i = 0; i < efptNR; i++)
3987 static void reduce_thread_force_buffer(int n, rvec *f,
3988 int nthreads, f_thread_t *f_t,
3989 int nblock, int block_size)
3991 /* The max thread number is arbitrary,
3992 * we used a fixed number to avoid memory management.
3993 * Using more than 16 threads is probably never useful performance wise.
3995 #define MAX_BONDED_THREADS 256
3998 if (nthreads > MAX_BONDED_THREADS)
4000 gmx_fatal(FARGS, "Can not reduce bonded forces on more than %d threads",
4001 MAX_BONDED_THREADS);
4004 /* This reduction can run on any number of threads,
4005 * independently of nthreads.
4007 #pragma omp parallel for num_threads(nthreads) schedule(static)
4008 for (b = 0; b < nblock; b++)
4010 rvec *fp[MAX_BONDED_THREADS];
4014 /* Determine which threads contribute to this block */
4016 for (ft = 1; ft < nthreads; ft++)
4018 if (f_t[ft].red_mask & (1U<<b))
4020 fp[nfb++] = f_t[ft].f;
4025 /* Reduce force buffers for threads that contribute */
4027 a1 = (b+1)*block_size;
4029 for (a = a0; a < a1; a++)
4031 for (fb = 0; fb < nfb; fb++)
4033 rvec_inc(f[a], fp[fb][a]);
4040 static void reduce_thread_forces(int n, rvec *f, rvec *fshift,
4041 real *ener, gmx_grppairener_t *grpp, real *dvdl,
4042 int nthreads, f_thread_t *f_t,
4043 int nblock, int block_size,
4044 gmx_bool bCalcEnerVir,
4049 /* Reduce the bonded force buffer */
4050 reduce_thread_force_buffer(n, f, nthreads, f_t, nblock, block_size);
4053 /* When necessary, reduce energy and virial using one thread only */
4058 for (i = 0; i < SHIFTS; i++)
4060 for (t = 1; t < nthreads; t++)
4062 rvec_inc(fshift[i], f_t[t].fshift[i]);
4065 for (i = 0; i < F_NRE; i++)
4067 for (t = 1; t < nthreads; t++)
4069 ener[i] += f_t[t].ener[i];
4072 for (i = 0; i < egNR; i++)
4074 for (j = 0; j < f_t[1].grpp.nener; j++)
4076 for (t = 1; t < nthreads; t++)
4079 grpp->ener[i][j] += f_t[t].grpp.ener[i][j];
4085 for (i = 0; i < efptNR; i++)
4088 for (t = 1; t < nthreads; t++)
4090 dvdl[i] += f_t[t].dvdl[i];
4097 static real calc_one_bond(FILE *fplog, int thread,
4098 int ftype, const t_idef *idef,
4099 rvec x[], rvec f[], rvec fshift[],
4101 const t_pbc *pbc, const t_graph *g,
4102 gmx_grppairener_t *grpp,
4104 real *lambda, real *dvdl,
4105 const t_mdatoms *md, t_fcdata *fcd,
4106 gmx_bool bCalcEnerVir,
4107 int *global_atom_index, gmx_bool bPrintSepPot)
4109 int nat1, nbonds, efptFTYPE;
4114 if (IS_RESTRAINT_TYPE(ftype))
4116 efptFTYPE = efptRESTRAINT;
4120 efptFTYPE = efptBONDED;
4123 nat1 = interaction_function[ftype].nratoms + 1;
4124 nbonds = idef->il[ftype].nr/nat1;
4125 iatoms = idef->il[ftype].iatoms;
4127 nb0 = idef->il_thread_division[ftype*(idef->nthreads+1)+thread];
4128 nbn = idef->il_thread_division[ftype*(idef->nthreads+1)+thread+1] - nb0;
4130 if (!IS_LISTED_LJ_C(ftype))
4132 if (ftype == F_CMAP)
4134 v = cmap_dihs(nbn, iatoms+nb0,
4135 idef->iparams, &idef->cmap_grid,
4136 (const rvec*)x, f, fshift,
4137 pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4138 md, fcd, global_atom_index);
4141 else if (ftype == F_ANGLES &&
4142 !bCalcEnerVir && fr->efep == efepNO)
4144 /* No energies, shift forces, dvdl */
4145 angles_noener_simd(nbn, idef->il[ftype].iatoms+nb0,
4148 pbc, g, lambda[efptFTYPE], md, fcd,
4153 else if (ftype == F_PDIHS &&
4154 !bCalcEnerVir && fr->efep == efepNO)
4156 /* No energies, shift forces, dvdl */
4157 #ifndef SIMD_BONDEDS
4162 (nbn, idef->il[ftype].iatoms+nb0,
4165 pbc, g, lambda[efptFTYPE], md, fcd,
4171 v = interaction_function[ftype].ifunc(nbn, iatoms+nb0,
4173 (const rvec*)x, f, fshift,
4174 pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4175 md, fcd, global_atom_index);
4179 fprintf(fplog, " %-23s #%4d V %12.5e dVdl %12.5e\n",
4180 interaction_function[ftype].longname,
4181 nbonds, v, lambda[efptFTYPE]);
4186 v = do_nonbonded_listed(ftype, nbn, iatoms+nb0, idef->iparams, (const rvec*)x, f, fshift,
4187 pbc, g, lambda, dvdl, md, fr, grpp, global_atom_index);
4191 fprintf(fplog, " %-5s + %-15s #%4d dVdl %12.5e\n",
4192 interaction_function[ftype].longname,
4193 interaction_function[F_LJ14].longname, nbonds, dvdl[efptVDW]);
4194 fprintf(fplog, " %-5s + %-15s #%4d dVdl %12.5e\n",
4195 interaction_function[ftype].longname,
4196 interaction_function[F_COUL14].longname, nbonds, dvdl[efptCOUL]);
4202 inc_nrnb(nrnb, interaction_function[ftype].nrnb_ind, nbonds);
4208 void calc_bonds(FILE *fplog, const gmx_multisim_t *ms,
4210 rvec x[], history_t *hist,
4211 rvec f[], t_forcerec *fr,
4212 const t_pbc *pbc, const t_graph *g,
4213 gmx_enerdata_t *enerd, t_nrnb *nrnb,
4215 const t_mdatoms *md,
4216 t_fcdata *fcd, int *global_atom_index,
4217 t_atomtypes gmx_unused *atype, gmx_genborn_t gmx_unused *born,
4219 gmx_bool bPrintSepPot, gmx_large_int_t step)
4221 gmx_bool bCalcEnerVir;
4223 real v, dvdl[efptNR], dvdl_dum[efptNR]; /* The dummy array is to have a place to store the dhdl at other values
4224 of lambda, which will be thrown away in the end*/
4225 const t_pbc *pbc_null;
4229 assert(fr->nthreads == idef->nthreads);
4231 bCalcEnerVir = (force_flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY));
4233 for (i = 0; i < efptNR; i++)
4247 fprintf(fplog, "Step %s: bonded V and dVdl for this node\n",
4248 gmx_step_str(step, buf));
4254 p_graph(debug, "Bondage is fun", g);
4258 /* Do pre force calculation stuff which might require communication */
4259 if (idef->il[F_ORIRES].nr)
4261 enerd->term[F_ORIRESDEV] =
4262 calc_orires_dev(ms, idef->il[F_ORIRES].nr,
4263 idef->il[F_ORIRES].iatoms,
4264 idef->iparams, md, (const rvec*)x,
4265 pbc_null, fcd, hist);
4267 if (idef->il[F_DISRES].nr)
4269 calc_disres_R_6(ms, idef->il[F_DISRES].nr,
4270 idef->il[F_DISRES].iatoms,
4271 idef->iparams, (const rvec*)x, pbc_null,
4275 #pragma omp parallel for num_threads(fr->nthreads) schedule(static)
4276 for (thread = 0; thread < fr->nthreads; thread++)
4283 gmx_grppairener_t *grpp;
4288 fshift = fr->fshift;
4290 grpp = &enerd->grpp;
4295 zero_thread_forces(&fr->f_t[thread], fr->natoms_force,
4296 fr->red_nblock, 1<<fr->red_ashift);
4298 ft = fr->f_t[thread].f;
4299 fshift = fr->f_t[thread].fshift;
4300 epot = fr->f_t[thread].ener;
4301 grpp = &fr->f_t[thread].grpp;
4302 dvdlt = fr->f_t[thread].dvdl;
4304 /* Loop over all bonded force types to calculate the bonded forces */
4305 for (ftype = 0; (ftype < F_NRE); ftype++)
4307 if (idef->il[ftype].nr > 0 && ftype_is_bonded_potential(ftype))
4309 v = calc_one_bond(fplog, thread, ftype, idef, x,
4310 ft, fshift, fr, pbc_null, g, grpp,
4311 nrnb, lambda, dvdlt,
4312 md, fcd, bCalcEnerVir,
4313 global_atom_index, bPrintSepPot);
4318 if (fr->nthreads > 1)
4320 reduce_thread_forces(fr->natoms_force, f, fr->fshift,
4321 enerd->term, &enerd->grpp, dvdl,
4322 fr->nthreads, fr->f_t,
4323 fr->red_nblock, 1<<fr->red_ashift,
4325 force_flags & GMX_FORCE_DHDL);
4327 if (force_flags & GMX_FORCE_DHDL)
4329 for (i = 0; i < efptNR; i++)
4331 enerd->dvdl_nonlin[i] += dvdl[i];
4335 /* Copy the sum of violations for the distance restraints from fcd */
4338 enerd->term[F_DISRESVIOL] = fcd->disres.sumviol;
4343 void calc_bonds_lambda(FILE *fplog,
4347 const t_pbc *pbc, const t_graph *g,
4348 gmx_grppairener_t *grpp, real *epot, t_nrnb *nrnb,
4350 const t_mdatoms *md,
4352 int *global_atom_index)
4354 int i, ftype, nr_nonperturbed, nr;
4356 real dvdl_dum[efptNR];
4358 const t_pbc *pbc_null;
4370 /* Copy the whole idef, so we can modify the contents locally */
4372 idef_fe.nthreads = 1;
4373 snew(idef_fe.il_thread_division, F_NRE*(idef_fe.nthreads+1));
4375 /* We already have the forces, so we use temp buffers here */
4376 snew(f, fr->natoms_force);
4377 snew(fshift, SHIFTS);
4379 /* Loop over all bonded force types to calculate the bonded energies */
4380 for (ftype = 0; (ftype < F_NRE); ftype++)
4382 if (ftype_is_bonded_potential(ftype))
4384 /* Set the work range of thread 0 to the perturbed bondeds only */
4385 nr_nonperturbed = idef->il[ftype].nr_nonperturbed;
4386 nr = idef->il[ftype].nr;
4387 idef_fe.il_thread_division[ftype*2+0] = nr_nonperturbed;
4388 idef_fe.il_thread_division[ftype*2+1] = nr;
4390 /* This is only to get the flop count correct */
4391 idef_fe.il[ftype].nr = nr - nr_nonperturbed;
4393 if (nr - nr_nonperturbed > 0)
4395 v = calc_one_bond(fplog, 0, ftype, &idef_fe,
4396 x, f, fshift, fr, pbc_null, g,
4397 grpp, nrnb, lambda, dvdl_dum,
4399 global_atom_index, FALSE);
4408 sfree(idef_fe.il_thread_division);