0ad3139599b1f8aa49333ea3e932247626d4e381
[alexxy/gromacs.git] / src / gromacs / bonded / bonded.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "bonded.h"
40
41 #include "config.h"
42
43 #include <assert.h>
44 #include <cmath>
45
46 #include <algorithm>
47
48 #include "gromacs/math/units.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/legacyheaders/txtdump.h"
52 #include "gromacs/legacyheaders/ns.h"
53 #include "gromacs/legacyheaders/macros.h"
54 #include "gromacs/legacyheaders/names.h"
55 #include "gromacs/legacyheaders/disre.h"
56 #include "gromacs/legacyheaders/orires.h"
57 #include "gromacs/legacyheaders/force.h"
58 #include "gromacs/legacyheaders/nonbonded.h"
59
60 #include "gromacs/pbcutil/ishift.h"
61 #include "gromacs/pbcutil/mshift.h"
62 #include "gromacs/pbcutil/pbc.h"
63 #include "gromacs/simd/simd.h"
64 #include "gromacs/simd/simd_math.h"
65 #include "gromacs/simd/vector_operations.h"
66 #include "gromacs/utility/fatalerror.h"
67 #include "gromacs/utility/smalloc.h"
68
69 #include "restcbt.h"
70
71 /* Find a better place for this? */
72 const int cmap_coeff_matrix[] = {
73     1, 0, -3,  2, 0, 0,  0,  0, -3,  0,  9, -6,  2,  0, -6,  4,
74     0, 0,  0,  0, 0, 0,  0,  0,  3,  0, -9,  6, -2,  0,  6, -4,
75     0, 0,  0,  0, 0, 0,  0,  0,  0,  0,  9, -6,  0,  0, -6,  4,
76     0, 0,  3, -2, 0, 0,  0,  0,  0,  0, -9,  6,  0,  0,  6, -4,
77     0, 0,  0,  0, 1, 0, -3,  2, -2,  0,  6, -4,  1,  0, -3,  2,
78     0, 0,  0,  0, 0, 0,  0,  0, -1,  0,  3, -2,  1,  0, -3,  2,
79     0, 0,  0,  0, 0, 0,  0,  0,  0,  0, -3,  2,  0,  0,  3, -2,
80     0, 0,  0,  0, 0, 0,  3, -2,  0,  0, -6,  4,  0,  0,  3, -2,
81     0, 1, -2,  1, 0, 0,  0,  0,  0, -3,  6, -3,  0,  2, -4,  2,
82     0, 0,  0,  0, 0, 0,  0,  0,  0,  3, -6,  3,  0, -2,  4, -2,
83     0, 0,  0,  0, 0, 0,  0,  0,  0,  0, -3,  3,  0,  0,  2, -2,
84     0, 0, -1,  1, 0, 0,  0,  0,  0,  0,  3, -3,  0,  0, -2,  2,
85     0, 0,  0,  0, 0, 1, -2,  1,  0, -2,  4, -2,  0,  1, -2,  1,
86     0, 0,  0,  0, 0, 0,  0,  0,  0, -1,  2, -1,  0,  1, -2,  1,
87     0, 0,  0,  0, 0, 0,  0,  0,  0,  0,  1, -1,  0,  0, -1,  1,
88     0, 0,  0,  0, 0, 0, -1,  1,  0,  0,  2, -2,  0,  0, -1,  1
89 };
90
91
92 /* TODO This function should go and live in nonbonded.c where it is
93    really needed. Here, it only supports giving a fatal error message
94    with FENE_bonds */
95 int glatnr(int *global_atom_index, int i)
96 {
97     int atnr;
98
99     if (global_atom_index == NULL)
100     {
101         atnr = i + 1;
102     }
103     else
104     {
105         atnr = global_atom_index[i] + 1;
106     }
107
108     return atnr;
109 }
110
111 /* TODO This kind of code appears in many places. Consolidate it */
112 static int pbc_rvec_sub(const t_pbc *pbc, const rvec xi, const rvec xj, rvec dx)
113 {
114     if (pbc)
115     {
116         return pbc_dx_aiuc(pbc, xi, xj, dx);
117     }
118     else
119     {
120         rvec_sub(xi, xj, dx);
121         return CENTRAL;
122     }
123 }
124
125 #ifdef GMX_SIMD_HAVE_REAL
126
127 /* SIMD PBC data structure, containing 1/boxdiag and the box vectors */
128 typedef struct {
129     gmx_simd_real_t inv_bzz;
130     gmx_simd_real_t inv_byy;
131     gmx_simd_real_t inv_bxx;
132     gmx_simd_real_t bzx;
133     gmx_simd_real_t bzy;
134     gmx_simd_real_t bzz;
135     gmx_simd_real_t byx;
136     gmx_simd_real_t byy;
137     gmx_simd_real_t bxx;
138 } pbc_simd_t;
139
140 /* Set the SIMD pbc data from a normal t_pbc struct */
141 static void set_pbc_simd(const t_pbc *pbc, pbc_simd_t *pbc_simd)
142 {
143     rvec inv_bdiag;
144     int  d;
145
146     /* Setting inv_bdiag to 0 effectively turns off PBC */
147     clear_rvec(inv_bdiag);
148     if (pbc != NULL)
149     {
150         for (d = 0; d < pbc->ndim_ePBC; d++)
151         {
152             inv_bdiag[d] = 1.0/pbc->box[d][d];
153         }
154     }
155
156     pbc_simd->inv_bzz = gmx_simd_set1_r(inv_bdiag[ZZ]);
157     pbc_simd->inv_byy = gmx_simd_set1_r(inv_bdiag[YY]);
158     pbc_simd->inv_bxx = gmx_simd_set1_r(inv_bdiag[XX]);
159
160     if (pbc != NULL)
161     {
162         pbc_simd->bzx = gmx_simd_set1_r(pbc->box[ZZ][XX]);
163         pbc_simd->bzy = gmx_simd_set1_r(pbc->box[ZZ][YY]);
164         pbc_simd->bzz = gmx_simd_set1_r(pbc->box[ZZ][ZZ]);
165         pbc_simd->byx = gmx_simd_set1_r(pbc->box[YY][XX]);
166         pbc_simd->byy = gmx_simd_set1_r(pbc->box[YY][YY]);
167         pbc_simd->bxx = gmx_simd_set1_r(pbc->box[XX][XX]);
168     }
169     else
170     {
171         pbc_simd->bzx = gmx_simd_setzero_r();
172         pbc_simd->bzy = gmx_simd_setzero_r();
173         pbc_simd->bzz = gmx_simd_setzero_r();
174         pbc_simd->byx = gmx_simd_setzero_r();
175         pbc_simd->byy = gmx_simd_setzero_r();
176         pbc_simd->bxx = gmx_simd_setzero_r();
177     }
178 }
179
180 /* Correct distance vector *dx,*dy,*dz for PBC using SIMD */
181 static gmx_inline void
182 pbc_dx_simd(gmx_simd_real_t *dx, gmx_simd_real_t *dy, gmx_simd_real_t *dz,
183             const pbc_simd_t *pbc)
184 {
185     gmx_simd_real_t sh;
186
187     sh  = gmx_simd_round_r(gmx_simd_mul_r(*dz, pbc->inv_bzz));
188     *dx = gmx_simd_fnmadd_r(sh, pbc->bzx, *dx);
189     *dy = gmx_simd_fnmadd_r(sh, pbc->bzy, *dy);
190     *dz = gmx_simd_fnmadd_r(sh, pbc->bzz, *dz);
191
192     sh  = gmx_simd_round_r(gmx_simd_mul_r(*dy, pbc->inv_byy));
193     *dx = gmx_simd_fnmadd_r(sh, pbc->byx, *dx);
194     *dy = gmx_simd_fnmadd_r(sh, pbc->byy, *dy);
195
196     sh  = gmx_simd_round_r(gmx_simd_mul_r(*dx, pbc->inv_bxx));
197     *dx = gmx_simd_fnmadd_r(sh, pbc->bxx, *dx);
198 }
199
200 #endif /* GMX_SIMD_HAVE_REAL */
201
202 /*
203  * Morse potential bond by Frank Everdij
204  *
205  * Three parameters needed:
206  *
207  * b0 = equilibrium distance in nm
208  * be = beta in nm^-1 (actually, it's nu_e*Sqrt(2*pi*pi*mu/D_e))
209  * cb = well depth in kJ/mol
210  *
211  * Note: the potential is referenced to be +cb at infinite separation
212  *       and zero at the equilibrium distance!
213  */
214
215 real morse_bonds(int nbonds,
216                  const t_iatom forceatoms[], const t_iparams forceparams[],
217                  const rvec x[], rvec f[], rvec fshift[],
218                  const t_pbc *pbc, const t_graph *g,
219                  real lambda, real *dvdlambda,
220                  const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
221                  int gmx_unused *global_atom_index)
222 {
223     const real one = 1.0;
224     const real two = 2.0;
225     real       dr, dr2, temp, omtemp, cbomtemp, fbond, vbond, fij, vtot;
226     real       b0, be, cb, b0A, beA, cbA, b0B, beB, cbB, L1;
227     rvec       dx;
228     int        i, m, ki, type, ai, aj;
229     ivec       dt;
230
231     vtot = 0.0;
232     for (i = 0; (i < nbonds); )
233     {
234         type = forceatoms[i++];
235         ai   = forceatoms[i++];
236         aj   = forceatoms[i++];
237
238         b0A   = forceparams[type].morse.b0A;
239         beA   = forceparams[type].morse.betaA;
240         cbA   = forceparams[type].morse.cbA;
241
242         b0B   = forceparams[type].morse.b0B;
243         beB   = forceparams[type].morse.betaB;
244         cbB   = forceparams[type].morse.cbB;
245
246         L1 = one-lambda;                            /* 1 */
247         b0 = L1*b0A + lambda*b0B;                   /* 3 */
248         be = L1*beA + lambda*beB;                   /* 3 */
249         cb = L1*cbA + lambda*cbB;                   /* 3 */
250
251         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /*   3          */
252         dr2  = iprod(dx, dx);                       /*   5          */
253         dr   = dr2*gmx_invsqrt(dr2);                /*  10          */
254         temp = exp(-be*(dr-b0));                    /*  12          */
255
256         if (temp == one)
257         {
258             /* bonds are constrainted. This may _not_ include bond constraints if they are lambda dependent */
259             *dvdlambda += cbB-cbA;
260             continue;
261         }
262
263         omtemp    = one-temp;                                                                                        /*   1          */
264         cbomtemp  = cb*omtemp;                                                                                       /*   1          */
265         vbond     = cbomtemp*omtemp;                                                                                 /*   1          */
266         fbond     = -two*be*temp*cbomtemp*gmx_invsqrt(dr2);                                                          /*   9          */
267         vtot     += vbond;                                                                                           /*   1          */
268
269         *dvdlambda += (cbB - cbA) * omtemp * omtemp - (2-2*omtemp)*omtemp * cb * ((b0B-b0A)*be - (beB-beA)*(dr-b0)); /* 15 */
270
271         if (g)
272         {
273             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
274             ki = IVEC2IS(dt);
275         }
276
277         for (m = 0; (m < DIM); m++)                    /*  15          */
278         {
279             fij                 = fbond*dx[m];
280             f[ai][m]           += fij;
281             f[aj][m]           -= fij;
282             fshift[ki][m]      += fij;
283             fshift[CENTRAL][m] -= fij;
284         }
285     }                                         /*  83 TOTAL    */
286     return vtot;
287 }
288
289 real cubic_bonds(int nbonds,
290                  const t_iatom forceatoms[], const t_iparams forceparams[],
291                  const rvec x[], rvec f[], rvec fshift[],
292                  const t_pbc *pbc, const t_graph *g,
293                  real gmx_unused lambda, real gmx_unused *dvdlambda,
294                  const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
295                  int gmx_unused *global_atom_index)
296 {
297     const real three = 3.0;
298     const real two   = 2.0;
299     real       kb, b0, kcub;
300     real       dr, dr2, dist, kdist, kdist2, fbond, vbond, fij, vtot;
301     rvec       dx;
302     int        i, m, ki, type, ai, aj;
303     ivec       dt;
304
305     vtot = 0.0;
306     for (i = 0; (i < nbonds); )
307     {
308         type = forceatoms[i++];
309         ai   = forceatoms[i++];
310         aj   = forceatoms[i++];
311
312         b0   = forceparams[type].cubic.b0;
313         kb   = forceparams[type].cubic.kb;
314         kcub = forceparams[type].cubic.kcub;
315
316         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx);     /*   3          */
317         dr2  = iprod(dx, dx);                           /*   5          */
318
319         if (dr2 == 0.0)
320         {
321             continue;
322         }
323
324         dr         = dr2*gmx_invsqrt(dr2);                  /*  10          */
325         dist       = dr-b0;
326         kdist      = kb*dist;
327         kdist2     = kdist*dist;
328
329         vbond      = kdist2 + kcub*kdist2*dist;
330         fbond      = -(two*kdist + three*kdist2*kcub)/dr;
331
332         vtot      += vbond;   /* 21 */
333
334         if (g)
335         {
336             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
337             ki = IVEC2IS(dt);
338         }
339         for (m = 0; (m < DIM); m++)                    /*  15          */
340         {
341             fij                 = fbond*dx[m];
342             f[ai][m]           += fij;
343             f[aj][m]           -= fij;
344             fshift[ki][m]      += fij;
345             fshift[CENTRAL][m] -= fij;
346         }
347     }                                         /*  54 TOTAL    */
348     return vtot;
349 }
350
351 real FENE_bonds(int nbonds,
352                 const t_iatom forceatoms[], const t_iparams forceparams[],
353                 const rvec x[], rvec f[], rvec fshift[],
354                 const t_pbc *pbc, const t_graph *g,
355                 real gmx_unused lambda, real gmx_unused *dvdlambda,
356                 const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
357                 int *global_atom_index)
358 {
359     const real half = 0.5;
360     const real one  = 1.0;
361     real       bm, kb;
362     real       dr2, bm2, omdr2obm2, fbond, vbond, fij, vtot;
363     rvec       dx;
364     int        i, m, ki, type, ai, aj;
365     ivec       dt;
366
367     vtot = 0.0;
368     for (i = 0; (i < nbonds); )
369     {
370         type = forceatoms[i++];
371         ai   = forceatoms[i++];
372         aj   = forceatoms[i++];
373
374         bm   = forceparams[type].fene.bm;
375         kb   = forceparams[type].fene.kb;
376
377         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx);     /*   3          */
378         dr2  = iprod(dx, dx);                           /*   5          */
379
380         if (dr2 == 0.0)
381         {
382             continue;
383         }
384
385         bm2 = bm*bm;
386
387         if (dr2 >= bm2)
388         {
389             gmx_fatal(FARGS,
390                       "r^2 (%f) >= bm^2 (%f) in FENE bond between atoms %d and %d",
391                       dr2, bm2,
392                       glatnr(global_atom_index, ai),
393                       glatnr(global_atom_index, aj));
394         }
395
396         omdr2obm2  = one - dr2/bm2;
397
398         vbond      = -half*kb*bm2*log(omdr2obm2);
399         fbond      = -kb/omdr2obm2;
400
401         vtot      += vbond;   /* 35 */
402
403         if (g)
404         {
405             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
406             ki = IVEC2IS(dt);
407         }
408         for (m = 0; (m < DIM); m++)                    /*  15          */
409         {
410             fij                 = fbond*dx[m];
411             f[ai][m]           += fij;
412             f[aj][m]           -= fij;
413             fshift[ki][m]      += fij;
414             fshift[CENTRAL][m] -= fij;
415         }
416     }                                         /*  58 TOTAL    */
417     return vtot;
418 }
419
420 real harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
421               real *V, real *F)
422 {
423     const real half = 0.5;
424     real       L1, kk, x0, dx, dx2;
425     real       v, f, dvdlambda;
426
427     L1    = 1.0-lambda;
428     kk    = L1*kA+lambda*kB;
429     x0    = L1*xA+lambda*xB;
430
431     dx    = x-x0;
432     dx2   = dx*dx;
433
434     f          = -kk*dx;
435     v          = half*kk*dx2;
436     dvdlambda  = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
437
438     *F    = f;
439     *V    = v;
440
441     return dvdlambda;
442
443     /* That was 19 flops */
444 }
445
446
447 real bonds(int nbonds,
448            const t_iatom forceatoms[], const t_iparams forceparams[],
449            const rvec x[], rvec f[], rvec fshift[],
450            const t_pbc *pbc, const t_graph *g,
451            real lambda, real *dvdlambda,
452            const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
453            int gmx_unused *global_atom_index)
454 {
455     int  i, m, ki, ai, aj, type;
456     real dr, dr2, fbond, vbond, fij, vtot;
457     rvec dx;
458     ivec dt;
459
460     vtot = 0.0;
461     for (i = 0; (i < nbonds); )
462     {
463         type = forceatoms[i++];
464         ai   = forceatoms[i++];
465         aj   = forceatoms[i++];
466
467         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /*   3      */
468         dr2  = iprod(dx, dx);                       /*   5              */
469         dr   = dr2*gmx_invsqrt(dr2);                /*  10              */
470
471         *dvdlambda += harmonic(forceparams[type].harmonic.krA,
472                                forceparams[type].harmonic.krB,
473                                forceparams[type].harmonic.rA,
474                                forceparams[type].harmonic.rB,
475                                dr, lambda, &vbond, &fbond); /*  19  */
476
477         if (dr2 == 0.0)
478         {
479             continue;
480         }
481
482
483         vtot  += vbond;            /* 1*/
484         fbond *= gmx_invsqrt(dr2); /*   6               */
485 #ifdef DEBUG
486         if (debug)
487         {
488             fprintf(debug, "BONDS: dr = %10g  vbond = %10g  fbond = %10g\n",
489                     dr, vbond, fbond);
490         }
491 #endif
492         if (g)
493         {
494             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
495             ki = IVEC2IS(dt);
496         }
497         for (m = 0; (m < DIM); m++)     /*  15          */
498         {
499             fij                 = fbond*dx[m];
500             f[ai][m]           += fij;
501             f[aj][m]           -= fij;
502             fshift[ki][m]      += fij;
503             fshift[CENTRAL][m] -= fij;
504         }
505     }               /* 59 TOTAL */
506     return vtot;
507 }
508
509 real restraint_bonds(int nbonds,
510                      const t_iatom forceatoms[], const t_iparams forceparams[],
511                      const rvec x[], rvec f[], rvec fshift[],
512                      const t_pbc *pbc, const t_graph *g,
513                      real lambda, real *dvdlambda,
514                      const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
515                      int gmx_unused *global_atom_index)
516 {
517     int  i, m, ki, ai, aj, type;
518     real dr, dr2, fbond, vbond, fij, vtot;
519     real L1;
520     real low, dlow, up1, dup1, up2, dup2, k, dk;
521     real drh, drh2;
522     rvec dx;
523     ivec dt;
524
525     L1   = 1.0 - lambda;
526
527     vtot = 0.0;
528     for (i = 0; (i < nbonds); )
529     {
530         type = forceatoms[i++];
531         ai   = forceatoms[i++];
532         aj   = forceatoms[i++];
533
534         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /*   3      */
535         dr2  = iprod(dx, dx);                       /*   5              */
536         dr   = dr2*gmx_invsqrt(dr2);                /*  10              */
537
538         low  = L1*forceparams[type].restraint.lowA + lambda*forceparams[type].restraint.lowB;
539         dlow =   -forceparams[type].restraint.lowA +        forceparams[type].restraint.lowB;
540         up1  = L1*forceparams[type].restraint.up1A + lambda*forceparams[type].restraint.up1B;
541         dup1 =   -forceparams[type].restraint.up1A +        forceparams[type].restraint.up1B;
542         up2  = L1*forceparams[type].restraint.up2A + lambda*forceparams[type].restraint.up2B;
543         dup2 =   -forceparams[type].restraint.up2A +        forceparams[type].restraint.up2B;
544         k    = L1*forceparams[type].restraint.kA   + lambda*forceparams[type].restraint.kB;
545         dk   =   -forceparams[type].restraint.kA   +        forceparams[type].restraint.kB;
546         /* 24 */
547
548         if (dr < low)
549         {
550             drh         = dr - low;
551             drh2        = drh*drh;
552             vbond       = 0.5*k*drh2;
553             fbond       = -k*drh;
554             *dvdlambda += 0.5*dk*drh2 - k*dlow*drh;
555         } /* 11 */
556         else if (dr <= up1)
557         {
558             vbond = 0;
559             fbond = 0;
560         }
561         else if (dr <= up2)
562         {
563             drh         = dr - up1;
564             drh2        = drh*drh;
565             vbond       = 0.5*k*drh2;
566             fbond       = -k*drh;
567             *dvdlambda += 0.5*dk*drh2 - k*dup1*drh;
568         } /* 11 */
569         else
570         {
571             drh         = dr - up2;
572             vbond       = k*(up2 - up1)*(0.5*(up2 - up1) + drh);
573             fbond       = -k*(up2 - up1);
574             *dvdlambda += dk*(up2 - up1)*(0.5*(up2 - up1) + drh)
575                 + k*(dup2 - dup1)*(up2 - up1 + drh)
576                 - k*(up2 - up1)*dup2;
577         }
578
579         if (dr2 == 0.0)
580         {
581             continue;
582         }
583
584         vtot  += vbond;            /* 1*/
585         fbond *= gmx_invsqrt(dr2); /*   6               */
586 #ifdef DEBUG
587         if (debug)
588         {
589             fprintf(debug, "BONDS: dr = %10g  vbond = %10g  fbond = %10g\n",
590                     dr, vbond, fbond);
591         }
592 #endif
593         if (g)
594         {
595             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
596             ki = IVEC2IS(dt);
597         }
598         for (m = 0; (m < DIM); m++)             /*  15          */
599         {
600             fij                 = fbond*dx[m];
601             f[ai][m]           += fij;
602             f[aj][m]           -= fij;
603             fshift[ki][m]      += fij;
604             fshift[CENTRAL][m] -= fij;
605         }
606     }                   /* 59 TOTAL     */
607
608     return vtot;
609 }
610
611 real polarize(int nbonds,
612               const t_iatom forceatoms[], const t_iparams forceparams[],
613               const rvec x[], rvec f[], rvec fshift[],
614               const t_pbc *pbc, const t_graph *g,
615               real lambda, real *dvdlambda,
616               const t_mdatoms *md, t_fcdata gmx_unused *fcd,
617               int gmx_unused *global_atom_index)
618 {
619     int  i, m, ki, ai, aj, type;
620     real dr, dr2, fbond, vbond, fij, vtot, ksh;
621     rvec dx;
622     ivec dt;
623
624     vtot = 0.0;
625     for (i = 0; (i < nbonds); )
626     {
627         type = forceatoms[i++];
628         ai   = forceatoms[i++];
629         aj   = forceatoms[i++];
630         ksh  = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].polarize.alpha;
631         if (debug)
632         {
633             fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
634         }
635
636         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx);                         /*   3      */
637         dr2  = iprod(dx, dx);                                               /*   5              */
638         dr   = dr2*gmx_invsqrt(dr2);                                        /*  10              */
639
640         *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /*  19  */
641
642         if (dr2 == 0.0)
643         {
644             continue;
645         }
646
647         vtot  += vbond;            /* 1*/
648         fbond *= gmx_invsqrt(dr2); /*   6               */
649
650         if (g)
651         {
652             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
653             ki = IVEC2IS(dt);
654         }
655         for (m = 0; (m < DIM); m++)     /*  15          */
656         {
657             fij                 = fbond*dx[m];
658             f[ai][m]           += fij;
659             f[aj][m]           -= fij;
660             fshift[ki][m]      += fij;
661             fshift[CENTRAL][m] -= fij;
662         }
663     }               /* 59 TOTAL */
664     return vtot;
665 }
666
667 real anharm_polarize(int nbonds,
668                      const t_iatom forceatoms[], const t_iparams forceparams[],
669                      const rvec x[], rvec f[], rvec fshift[],
670                      const t_pbc *pbc, const t_graph *g,
671                      real lambda, real *dvdlambda,
672                      const t_mdatoms *md, t_fcdata gmx_unused *fcd,
673                      int gmx_unused *global_atom_index)
674 {
675     int  i, m, ki, ai, aj, type;
676     real dr, dr2, fbond, vbond, fij, vtot, ksh, khyp, drcut, ddr, ddr3;
677     rvec dx;
678     ivec dt;
679
680     vtot = 0.0;
681     for (i = 0; (i < nbonds); )
682     {
683         type  = forceatoms[i++];
684         ai    = forceatoms[i++];
685         aj    = forceatoms[i++];
686         ksh   = sqr(md->chargeA[aj])*ONE_4PI_EPS0/forceparams[type].anharm_polarize.alpha; /* 7*/
687         khyp  = forceparams[type].anharm_polarize.khyp;
688         drcut = forceparams[type].anharm_polarize.drcut;
689         if (debug)
690         {
691             fprintf(debug, "POL: local ai = %d aj = %d ksh = %.3f\n", ai, aj, ksh);
692         }
693
694         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx);                         /*   3      */
695         dr2  = iprod(dx, dx);                                               /*   5              */
696         dr   = dr2*gmx_invsqrt(dr2);                                        /*  10              */
697
698         *dvdlambda += harmonic(ksh, ksh, 0, 0, dr, lambda, &vbond, &fbond); /*  19  */
699
700         if (dr2 == 0.0)
701         {
702             continue;
703         }
704
705         if (dr > drcut)
706         {
707             ddr    = dr-drcut;
708             ddr3   = ddr*ddr*ddr;
709             vbond += khyp*ddr*ddr3;
710             fbond -= 4*khyp*ddr3;
711         }
712         fbond *= gmx_invsqrt(dr2); /*   6               */
713         vtot  += vbond;            /* 1*/
714
715         if (g)
716         {
717             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
718             ki = IVEC2IS(dt);
719         }
720         for (m = 0; (m < DIM); m++)     /*  15          */
721         {
722             fij                 = fbond*dx[m];
723             f[ai][m]           += fij;
724             f[aj][m]           -= fij;
725             fshift[ki][m]      += fij;
726             fshift[CENTRAL][m] -= fij;
727         }
728     }               /* 72 TOTAL */
729     return vtot;
730 }
731
732 real water_pol(int nbonds,
733                const t_iatom forceatoms[], const t_iparams forceparams[],
734                const rvec x[], rvec f[], rvec gmx_unused fshift[],
735                const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
736                real gmx_unused lambda, real gmx_unused *dvdlambda,
737                const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
738                int gmx_unused *global_atom_index)
739 {
740     /* This routine implements anisotropic polarizibility for water, through
741      * a shell connected to a dummy with spring constant that differ in the
742      * three spatial dimensions in the molecular frame.
743      */
744     int  i, m, aO, aH1, aH2, aD, aS, type, type0, ki;
745     ivec dt;
746     rvec dOH1, dOH2, dHH, dOD, dDS, nW, kk, dx, kdx, proj;
747 #ifdef DEBUG
748     rvec df;
749 #endif
750     real vtot, fij, r_HH, r_OD, r_nW, tx, ty, tz, qS;
751
752     vtot = 0.0;
753     if (nbonds > 0)
754     {
755         type0  = forceatoms[0];
756         aS     = forceatoms[5];
757         qS     = md->chargeA[aS];
758         kk[XX] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_x;
759         kk[YY] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_y;
760         kk[ZZ] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_z;
761         r_HH   = 1.0/forceparams[type0].wpol.rHH;
762         if (debug)
763         {
764             fprintf(debug, "WPOL: qS  = %10.5f aS = %5d\n", qS, aS);
765             fprintf(debug, "WPOL: kk  = %10.3f        %10.3f        %10.3f\n",
766                     kk[XX], kk[YY], kk[ZZ]);
767             fprintf(debug, "WPOL: rOH = %10.3f  rHH = %10.3f  rOD = %10.3f\n",
768                     forceparams[type0].wpol.rOH,
769                     forceparams[type0].wpol.rHH,
770                     forceparams[type0].wpol.rOD);
771         }
772         for (i = 0; (i < nbonds); i += 6)
773         {
774             type = forceatoms[i];
775             if (type != type0)
776             {
777                 gmx_fatal(FARGS, "Sorry, type = %d, type0 = %d, file = %s, line = %d",
778                           type, type0, __FILE__, __LINE__);
779             }
780             aO   = forceatoms[i+1];
781             aH1  = forceatoms[i+2];
782             aH2  = forceatoms[i+3];
783             aD   = forceatoms[i+4];
784             aS   = forceatoms[i+5];
785
786             /* Compute vectors describing the water frame */
787             pbc_rvec_sub(pbc, x[aH1], x[aO], dOH1);
788             pbc_rvec_sub(pbc, x[aH2], x[aO], dOH2);
789             pbc_rvec_sub(pbc, x[aH2], x[aH1], dHH);
790             pbc_rvec_sub(pbc, x[aD], x[aO], dOD);
791             ki = pbc_rvec_sub(pbc, x[aS], x[aD], dDS);
792             cprod(dOH1, dOH2, nW);
793
794             /* Compute inverse length of normal vector
795              * (this one could be precomputed, but I'm too lazy now)
796              */
797             r_nW = gmx_invsqrt(iprod(nW, nW));
798             /* This is for precision, but does not make a big difference,
799              * it can go later.
800              */
801             r_OD = gmx_invsqrt(iprod(dOD, dOD));
802
803             /* Normalize the vectors in the water frame */
804             svmul(r_nW, nW, nW);
805             svmul(r_HH, dHH, dHH);
806             svmul(r_OD, dOD, dOD);
807
808             /* Compute displacement of shell along components of the vector */
809             dx[ZZ] = iprod(dDS, dOD);
810             /* Compute projection on the XY plane: dDS - dx[ZZ]*dOD */
811             for (m = 0; (m < DIM); m++)
812             {
813                 proj[m] = dDS[m]-dx[ZZ]*dOD[m];
814             }
815
816             /*dx[XX] = iprod(dDS,nW);
817                dx[YY] = iprod(dDS,dHH);*/
818             dx[XX] = iprod(proj, nW);
819             for (m = 0; (m < DIM); m++)
820             {
821                 proj[m] -= dx[XX]*nW[m];
822             }
823             dx[YY] = iprod(proj, dHH);
824             /*#define DEBUG*/
825 #ifdef DEBUG
826             if (debug)
827             {
828                 fprintf(debug, "WPOL: dx2=%10g  dy2=%10g  dz2=%10g  sum=%10g  dDS^2=%10g\n",
829                         sqr(dx[XX]), sqr(dx[YY]), sqr(dx[ZZ]), iprod(dx, dx), iprod(dDS, dDS));
830                 fprintf(debug, "WPOL: dHH=(%10g,%10g,%10g)\n", dHH[XX], dHH[YY], dHH[ZZ]);
831                 fprintf(debug, "WPOL: dOD=(%10g,%10g,%10g), 1/r_OD = %10g\n",
832                         dOD[XX], dOD[YY], dOD[ZZ], 1/r_OD);
833                 fprintf(debug, "WPOL: nW =(%10g,%10g,%10g), 1/r_nW = %10g\n",
834                         nW[XX], nW[YY], nW[ZZ], 1/r_nW);
835                 fprintf(debug, "WPOL: dx  =%10g, dy  =%10g, dz  =%10g\n",
836                         dx[XX], dx[YY], dx[ZZ]);
837                 fprintf(debug, "WPOL: dDSx=%10g, dDSy=%10g, dDSz=%10g\n",
838                         dDS[XX], dDS[YY], dDS[ZZ]);
839             }
840 #endif
841             /* Now compute the forces and energy */
842             kdx[XX] = kk[XX]*dx[XX];
843             kdx[YY] = kk[YY]*dx[YY];
844             kdx[ZZ] = kk[ZZ]*dx[ZZ];
845             vtot   += iprod(dx, kdx);
846
847             if (g)
848             {
849                 ivec_sub(SHIFT_IVEC(g, aS), SHIFT_IVEC(g, aD), dt);
850                 ki = IVEC2IS(dt);
851             }
852
853             for (m = 0; (m < DIM); m++)
854             {
855                 /* This is a tensor operation but written out for speed */
856                 tx        =  nW[m]*kdx[XX];
857                 ty        = dHH[m]*kdx[YY];
858                 tz        = dOD[m]*kdx[ZZ];
859                 fij       = -tx-ty-tz;
860 #ifdef DEBUG
861                 df[m] = fij;
862 #endif
863                 f[aS][m]           += fij;
864                 f[aD][m]           -= fij;
865                 fshift[ki][m]      += fij;
866                 fshift[CENTRAL][m] -= fij;
867             }
868 #ifdef DEBUG
869             if (debug)
870             {
871                 fprintf(debug, "WPOL: vwpol=%g\n", 0.5*iprod(dx, kdx));
872                 fprintf(debug, "WPOL: df = (%10g, %10g, %10g)\n", df[XX], df[YY], df[ZZ]);
873             }
874 #endif
875         }
876     }
877     return 0.5*vtot;
878 }
879
880 static real do_1_thole(const rvec xi, const rvec xj, rvec fi, rvec fj,
881                        const t_pbc *pbc, real qq,
882                        rvec fshift[], real afac)
883 {
884     rvec r12;
885     real r12sq, r12_1, r12bar, v0, v1, fscal, ebar, fff;
886     int  m, t;
887
888     t      = pbc_rvec_sub(pbc, xi, xj, r12);                      /*  3 */
889
890     r12sq  = iprod(r12, r12);                                     /*  5 */
891     r12_1  = gmx_invsqrt(r12sq);                                  /*  5 */
892     r12bar = afac/r12_1;                                          /*  5 */
893     v0     = qq*ONE_4PI_EPS0*r12_1;                               /*  2 */
894     ebar   = exp(-r12bar);                                        /*  5 */
895     v1     = (1-(1+0.5*r12bar)*ebar);                             /*  4 */
896     fscal  = ((v0*r12_1)*v1 - v0*0.5*afac*ebar*(r12bar+1))*r12_1; /* 9 */
897     if (debug)
898     {
899         fprintf(debug, "THOLE: v0 = %.3f v1 = %.3f r12= % .3f r12bar = %.3f fscal = %.3f  ebar = %.3f\n", v0, v1, 1/r12_1, r12bar, fscal, ebar);
900     }
901
902     for (m = 0; (m < DIM); m++)
903     {
904         fff                 = fscal*r12[m];
905         fi[m]              += fff;
906         fj[m]              -= fff;
907         fshift[t][m]       += fff;
908         fshift[CENTRAL][m] -= fff;
909     }             /* 15 */
910
911     return v0*v1; /* 1 */
912     /* 54 */
913 }
914
915 real thole_pol(int nbonds,
916                const t_iatom forceatoms[], const t_iparams forceparams[],
917                const rvec x[], rvec f[], rvec fshift[],
918                const t_pbc *pbc, const t_graph gmx_unused *g,
919                real gmx_unused lambda, real gmx_unused *dvdlambda,
920                const t_mdatoms *md, t_fcdata gmx_unused *fcd,
921                int gmx_unused *global_atom_index)
922 {
923     /* Interaction between two pairs of particles with opposite charge */
924     int        i, type, a1, da1, a2, da2;
925     real       q1, q2, qq, a, al1, al2, afac;
926     real       V             = 0;
927     const real minusOneOnSix = -1.0/6.0;
928
929     for (i = 0; (i < nbonds); )
930     {
931         type  = forceatoms[i++];
932         a1    = forceatoms[i++];
933         da1   = forceatoms[i++];
934         a2    = forceatoms[i++];
935         da2   = forceatoms[i++];
936         q1    = md->chargeA[da1];
937         q2    = md->chargeA[da2];
938         a     = forceparams[type].thole.a;
939         al1   = forceparams[type].thole.alpha1;
940         al2   = forceparams[type].thole.alpha2;
941         qq    = q1*q2;
942         afac  = a*pow(al1*al2, minusOneOnSix);
943         V    += do_1_thole(x[a1], x[a2], f[a1], f[a2], pbc, qq, fshift, afac);
944         V    += do_1_thole(x[da1], x[a2], f[da1], f[a2], pbc, -qq, fshift, afac);
945         V    += do_1_thole(x[a1], x[da2], f[a1], f[da2], pbc, -qq, fshift, afac);
946         V    += do_1_thole(x[da1], x[da2], f[da1], f[da2], pbc, qq, fshift, afac);
947     }
948     /* 290 flops */
949     return V;
950 }
951
952 real bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
953                 rvec r_ij, rvec r_kj, real *costh,
954                 int *t1, int *t2)
955 /* Return value is the angle between the bonds i-j and j-k */
956 {
957     /* 41 FLOPS */
958     real th;
959
960     *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /*  3                */
961     *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /*  3                */
962
963     *costh = cos_angle(r_ij, r_kj);        /* 25                */
964     th     = acos(*costh);                 /* 10                */
965     /* 41 TOTAL */
966     return th;
967 }
968
969 real angles(int nbonds,
970             const t_iatom forceatoms[], const t_iparams forceparams[],
971             const rvec x[], rvec f[], rvec fshift[],
972             const t_pbc *pbc, const t_graph *g,
973             real lambda, real *dvdlambda,
974             const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
975             int gmx_unused *global_atom_index)
976 {
977     int  i, ai, aj, ak, t1, t2, type;
978     rvec r_ij, r_kj;
979     real cos_theta, cos_theta2, theta, dVdt, va, vtot;
980     ivec jt, dt_ij, dt_kj;
981
982     vtot = 0.0;
983     for (i = 0; i < nbonds; )
984     {
985         type = forceatoms[i++];
986         ai   = forceatoms[i++];
987         aj   = forceatoms[i++];
988         ak   = forceatoms[i++];
989
990         theta  = bond_angle(x[ai], x[aj], x[ak], pbc,
991                             r_ij, r_kj, &cos_theta, &t1, &t2);  /*  41          */
992
993         *dvdlambda += harmonic(forceparams[type].harmonic.krA,
994                                forceparams[type].harmonic.krB,
995                                forceparams[type].harmonic.rA*DEG2RAD,
996                                forceparams[type].harmonic.rB*DEG2RAD,
997                                theta, lambda, &va, &dVdt);  /*  21  */
998         vtot += va;
999
1000         cos_theta2 = sqr(cos_theta);
1001         if (cos_theta2 < 1)
1002         {
1003             int  m;
1004             real st, sth;
1005             real cik, cii, ckk;
1006             real nrkj2, nrij2;
1007             real nrkj_1, nrij_1;
1008             rvec f_i, f_j, f_k;
1009
1010             st  = dVdt*gmx_invsqrt(1 - cos_theta2); /*  12              */
1011             sth = st*cos_theta;                     /*   1              */
1012 #ifdef DEBUG
1013             if (debug)
1014             {
1015                 fprintf(debug, "ANGLES: theta = %10g  vth = %10g  dV/dtheta = %10g\n",
1016                         theta*RAD2DEG, va, dVdt);
1017             }
1018 #endif
1019             nrij2 = iprod(r_ij, r_ij);      /*   5              */
1020             nrkj2 = iprod(r_kj, r_kj);      /*   5              */
1021
1022             nrij_1 = gmx_invsqrt(nrij2);    /*  10              */
1023             nrkj_1 = gmx_invsqrt(nrkj2);    /*  10              */
1024
1025             cik = st*nrij_1*nrkj_1;         /*   2              */
1026             cii = sth*nrij_1*nrij_1;        /*   2              */
1027             ckk = sth*nrkj_1*nrkj_1;        /*   2              */
1028
1029             for (m = 0; m < DIM; m++)
1030             {           /*  39          */
1031                 f_i[m]    = -(cik*r_kj[m] - cii*r_ij[m]);
1032                 f_k[m]    = -(cik*r_ij[m] - ckk*r_kj[m]);
1033                 f_j[m]    = -f_i[m] - f_k[m];
1034                 f[ai][m] += f_i[m];
1035                 f[aj][m] += f_j[m];
1036                 f[ak][m] += f_k[m];
1037             }
1038             if (g != NULL)
1039             {
1040                 copy_ivec(SHIFT_IVEC(g, aj), jt);
1041
1042                 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1043                 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1044                 t1 = IVEC2IS(dt_ij);
1045                 t2 = IVEC2IS(dt_kj);
1046             }
1047             rvec_inc(fshift[t1], f_i);
1048             rvec_inc(fshift[CENTRAL], f_j);
1049             rvec_inc(fshift[t2], f_k);
1050         }                                           /* 161 TOTAL        */
1051     }
1052
1053     return vtot;
1054 }
1055
1056 #ifdef GMX_SIMD_HAVE_REAL
1057
1058 /* As angles, but using SIMD to calculate many dihedrals at once.
1059  * This routines does not calculate energies and shift forces.
1060  */
1061 static gmx_inline void
1062 angles_noener_simd(int nbonds,
1063                    const t_iatom forceatoms[], const t_iparams forceparams[],
1064                    const rvec x[], rvec f[],
1065                    const t_pbc *pbc, const t_graph gmx_unused *g,
1066                    real gmx_unused lambda,
1067                    const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1068                    int gmx_unused *global_atom_index)
1069 {
1070     const int            nfa1 = 4;
1071     int                  i, iu, s, m;
1072     int                  type, ai[GMX_SIMD_REAL_WIDTH], aj[GMX_SIMD_REAL_WIDTH];
1073     int                  ak[GMX_SIMD_REAL_WIDTH];
1074     real                 coeff_array[2*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *coeff;
1075     real                 dr_array[2*DIM*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *dr;
1076     real                 f_buf_array[6*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *f_buf;
1077     gmx_simd_real_t      k_S, theta0_S;
1078     gmx_simd_real_t      rijx_S, rijy_S, rijz_S;
1079     gmx_simd_real_t      rkjx_S, rkjy_S, rkjz_S;
1080     gmx_simd_real_t      one_S;
1081     gmx_simd_real_t      min_one_plus_eps_S;
1082     gmx_simd_real_t      rij_rkj_S;
1083     gmx_simd_real_t      nrij2_S, nrij_1_S;
1084     gmx_simd_real_t      nrkj2_S, nrkj_1_S;
1085     gmx_simd_real_t      cos_S, invsin_S;
1086     gmx_simd_real_t      theta_S;
1087     gmx_simd_real_t      st_S, sth_S;
1088     gmx_simd_real_t      cik_S, cii_S, ckk_S;
1089     gmx_simd_real_t      f_ix_S, f_iy_S, f_iz_S;
1090     gmx_simd_real_t      f_kx_S, f_ky_S, f_kz_S;
1091     pbc_simd_t           pbc_simd;
1092
1093     /* Ensure register memory alignment */
1094     coeff = gmx_simd_align_r(coeff_array);
1095     dr    = gmx_simd_align_r(dr_array);
1096     f_buf = gmx_simd_align_r(f_buf_array);
1097
1098     set_pbc_simd(pbc, &pbc_simd);
1099
1100     one_S = gmx_simd_set1_r(1.0);
1101
1102     /* The smallest number > -1 */
1103     min_one_plus_eps_S = gmx_simd_set1_r(-1.0 + 2*GMX_REAL_EPS);
1104
1105     /* nbonds is the number of angles times nfa1, here we step GMX_SIMD_REAL_WIDTH angles */
1106     for (i = 0; (i < nbonds); i += GMX_SIMD_REAL_WIDTH*nfa1)
1107     {
1108         /* Collect atoms for GMX_SIMD_REAL_WIDTH angles.
1109          * iu indexes into forceatoms, we should not let iu go beyond nbonds.
1110          */
1111         iu = i;
1112         for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
1113         {
1114             type  = forceatoms[iu];
1115             ai[s] = forceatoms[iu+1];
1116             aj[s] = forceatoms[iu+2];
1117             ak[s] = forceatoms[iu+3];
1118
1119             coeff[s]                     = forceparams[type].harmonic.krA;
1120             coeff[GMX_SIMD_REAL_WIDTH+s] = forceparams[type].harmonic.rA*DEG2RAD;
1121
1122             /* If you can't use pbc_dx_simd below for PBC, e.g. because
1123              * you can't round in SIMD, use pbc_rvec_sub here.
1124              */
1125             /* Store the non PBC corrected distances packed and aligned */
1126             for (m = 0; m < DIM; m++)
1127             {
1128                 dr[s +      m *GMX_SIMD_REAL_WIDTH] = x[ai[s]][m] - x[aj[s]][m];
1129                 dr[s + (DIM+m)*GMX_SIMD_REAL_WIDTH] = x[ak[s]][m] - x[aj[s]][m];
1130             }
1131
1132             /* At the end fill the arrays with identical entries */
1133             if (iu + nfa1 < nbonds)
1134             {
1135                 iu += nfa1;
1136             }
1137         }
1138
1139         k_S       = gmx_simd_load_r(coeff);
1140         theta0_S  = gmx_simd_load_r(coeff+GMX_SIMD_REAL_WIDTH);
1141
1142         rijx_S    = gmx_simd_load_r(dr + 0*GMX_SIMD_REAL_WIDTH);
1143         rijy_S    = gmx_simd_load_r(dr + 1*GMX_SIMD_REAL_WIDTH);
1144         rijz_S    = gmx_simd_load_r(dr + 2*GMX_SIMD_REAL_WIDTH);
1145         rkjx_S    = gmx_simd_load_r(dr + 3*GMX_SIMD_REAL_WIDTH);
1146         rkjy_S    = gmx_simd_load_r(dr + 4*GMX_SIMD_REAL_WIDTH);
1147         rkjz_S    = gmx_simd_load_r(dr + 5*GMX_SIMD_REAL_WIDTH);
1148
1149         pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, &pbc_simd);
1150         pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, &pbc_simd);
1151
1152         rij_rkj_S = gmx_simd_iprod_r(rijx_S, rijy_S, rijz_S,
1153                                      rkjx_S, rkjy_S, rkjz_S);
1154
1155         nrij2_S   = gmx_simd_norm2_r(rijx_S, rijy_S, rijz_S);
1156         nrkj2_S   = gmx_simd_norm2_r(rkjx_S, rkjy_S, rkjz_S);
1157
1158         nrij_1_S  = gmx_simd_invsqrt_r(nrij2_S);
1159         nrkj_1_S  = gmx_simd_invsqrt_r(nrkj2_S);
1160
1161         cos_S     = gmx_simd_mul_r(rij_rkj_S, gmx_simd_mul_r(nrij_1_S, nrkj_1_S));
1162
1163         /* To allow for 180 degrees, we take the max of cos and -1 + 1bit,
1164          * so we can safely get the 1/sin from 1/sqrt(1 - cos^2).
1165          * This also ensures that rounding errors would cause the argument
1166          * of gmx_simd_acos_r to be < -1.
1167          * Note that we do not take precautions for cos(0)=1, so the outer
1168          * atoms in an angle should not be on top of each other.
1169          */
1170         cos_S     = gmx_simd_max_r(cos_S, min_one_plus_eps_S);
1171
1172         theta_S   = gmx_simd_acos_r(cos_S);
1173
1174         invsin_S  = gmx_simd_invsqrt_r(gmx_simd_sub_r(one_S, gmx_simd_mul_r(cos_S, cos_S)));
1175
1176         st_S      = gmx_simd_mul_r(gmx_simd_mul_r(k_S, gmx_simd_sub_r(theta0_S, theta_S)),
1177                                    invsin_S);
1178         sth_S     = gmx_simd_mul_r(st_S, cos_S);
1179
1180         cik_S     = gmx_simd_mul_r(st_S,  gmx_simd_mul_r(nrij_1_S, nrkj_1_S));
1181         cii_S     = gmx_simd_mul_r(sth_S, gmx_simd_mul_r(nrij_1_S, nrij_1_S));
1182         ckk_S     = gmx_simd_mul_r(sth_S, gmx_simd_mul_r(nrkj_1_S, nrkj_1_S));
1183
1184         f_ix_S    = gmx_simd_mul_r(cii_S, rijx_S);
1185         f_ix_S    = gmx_simd_fnmadd_r(cik_S, rkjx_S, f_ix_S);
1186         f_iy_S    = gmx_simd_mul_r(cii_S, rijy_S);
1187         f_iy_S    = gmx_simd_fnmadd_r(cik_S, rkjy_S, f_iy_S);
1188         f_iz_S    = gmx_simd_mul_r(cii_S, rijz_S);
1189         f_iz_S    = gmx_simd_fnmadd_r(cik_S, rkjz_S, f_iz_S);
1190         f_kx_S    = gmx_simd_mul_r(ckk_S, rkjx_S);
1191         f_kx_S    = gmx_simd_fnmadd_r(cik_S, rijx_S, f_kx_S);
1192         f_ky_S    = gmx_simd_mul_r(ckk_S, rkjy_S);
1193         f_ky_S    = gmx_simd_fnmadd_r(cik_S, rijy_S, f_ky_S);
1194         f_kz_S    = gmx_simd_mul_r(ckk_S, rkjz_S);
1195         f_kz_S    = gmx_simd_fnmadd_r(cik_S, rijz_S, f_kz_S);
1196
1197         gmx_simd_store_r(f_buf + 0*GMX_SIMD_REAL_WIDTH, f_ix_S);
1198         gmx_simd_store_r(f_buf + 1*GMX_SIMD_REAL_WIDTH, f_iy_S);
1199         gmx_simd_store_r(f_buf + 2*GMX_SIMD_REAL_WIDTH, f_iz_S);
1200         gmx_simd_store_r(f_buf + 3*GMX_SIMD_REAL_WIDTH, f_kx_S);
1201         gmx_simd_store_r(f_buf + 4*GMX_SIMD_REAL_WIDTH, f_ky_S);
1202         gmx_simd_store_r(f_buf + 5*GMX_SIMD_REAL_WIDTH, f_kz_S);
1203
1204         iu = i;
1205         s  = 0;
1206         do
1207         {
1208             for (m = 0; m < DIM; m++)
1209             {
1210                 f[ai[s]][m] += f_buf[s + m*GMX_SIMD_REAL_WIDTH];
1211                 f[aj[s]][m] -= f_buf[s + m*GMX_SIMD_REAL_WIDTH] + f_buf[s + (DIM+m)*GMX_SIMD_REAL_WIDTH];
1212                 f[ak[s]][m] += f_buf[s + (DIM+m)*GMX_SIMD_REAL_WIDTH];
1213             }
1214             s++;
1215             iu += nfa1;
1216         }
1217         while (s < GMX_SIMD_REAL_WIDTH && iu < nbonds);
1218     }
1219 }
1220
1221 #endif /* GMX_SIMD_HAVE_REAL */
1222
1223 real linear_angles(int nbonds,
1224                    const t_iatom forceatoms[], const t_iparams forceparams[],
1225                    const rvec x[], rvec f[], rvec fshift[],
1226                    const t_pbc *pbc, const t_graph *g,
1227                    real lambda, real *dvdlambda,
1228                    const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1229                    int gmx_unused *global_atom_index)
1230 {
1231     int  i, m, ai, aj, ak, t1, t2, type;
1232     rvec f_i, f_j, f_k;
1233     real L1, kA, kB, aA, aB, dr, dr2, va, vtot, a, b, klin;
1234     ivec jt, dt_ij, dt_kj;
1235     rvec r_ij, r_kj, r_ik, dx;
1236
1237     L1   = 1-lambda;
1238     vtot = 0.0;
1239     for (i = 0; (i < nbonds); )
1240     {
1241         type = forceatoms[i++];
1242         ai   = forceatoms[i++];
1243         aj   = forceatoms[i++];
1244         ak   = forceatoms[i++];
1245
1246         kA   = forceparams[type].linangle.klinA;
1247         kB   = forceparams[type].linangle.klinB;
1248         klin = L1*kA + lambda*kB;
1249
1250         aA   = forceparams[type].linangle.aA;
1251         aB   = forceparams[type].linangle.aB;
1252         a    = L1*aA+lambda*aB;
1253         b    = 1-a;
1254
1255         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
1256         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
1257         rvec_sub(r_ij, r_kj, r_ik);
1258
1259         dr2 = 0;
1260         for (m = 0; (m < DIM); m++)
1261         {
1262             dr        = -a * r_ij[m] - b * r_kj[m];
1263             dr2      += dr*dr;
1264             dx[m]     = dr;
1265             f_i[m]    = a*klin*dr;
1266             f_k[m]    = b*klin*dr;
1267             f_j[m]    = -(f_i[m]+f_k[m]);
1268             f[ai][m] += f_i[m];
1269             f[aj][m] += f_j[m];
1270             f[ak][m] += f_k[m];
1271         }
1272         va          = 0.5*klin*dr2;
1273         *dvdlambda += 0.5*(kB-kA)*dr2 + klin*(aB-aA)*iprod(dx, r_ik);
1274
1275         vtot += va;
1276
1277         if (g)
1278         {
1279             copy_ivec(SHIFT_IVEC(g, aj), jt);
1280
1281             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1282             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1283             t1 = IVEC2IS(dt_ij);
1284             t2 = IVEC2IS(dt_kj);
1285         }
1286         rvec_inc(fshift[t1], f_i);
1287         rvec_inc(fshift[CENTRAL], f_j);
1288         rvec_inc(fshift[t2], f_k);
1289     }                                         /* 57 TOTAL       */
1290     return vtot;
1291 }
1292
1293 real urey_bradley(int nbonds,
1294                   const t_iatom forceatoms[], const t_iparams forceparams[],
1295                   const rvec x[], rvec f[], rvec fshift[],
1296                   const t_pbc *pbc, const t_graph *g,
1297                   real lambda, real *dvdlambda,
1298                   const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1299                   int gmx_unused *global_atom_index)
1300 {
1301     int  i, m, ai, aj, ak, t1, t2, type, ki;
1302     rvec r_ij, r_kj, r_ik;
1303     real cos_theta, cos_theta2, theta;
1304     real dVdt, va, vtot, dr, dr2, vbond, fbond, fik;
1305     real kthA, th0A, kUBA, r13A, kthB, th0B, kUBB, r13B;
1306     ivec jt, dt_ij, dt_kj, dt_ik;
1307
1308     vtot = 0.0;
1309     for (i = 0; (i < nbonds); )
1310     {
1311         type  = forceatoms[i++];
1312         ai    = forceatoms[i++];
1313         aj    = forceatoms[i++];
1314         ak    = forceatoms[i++];
1315         th0A  = forceparams[type].u_b.thetaA*DEG2RAD;
1316         kthA  = forceparams[type].u_b.kthetaA;
1317         r13A  = forceparams[type].u_b.r13A;
1318         kUBA  = forceparams[type].u_b.kUBA;
1319         th0B  = forceparams[type].u_b.thetaB*DEG2RAD;
1320         kthB  = forceparams[type].u_b.kthetaB;
1321         r13B  = forceparams[type].u_b.r13B;
1322         kUBB  = forceparams[type].u_b.kUBB;
1323
1324         theta  = bond_angle(x[ai], x[aj], x[ak], pbc,
1325                             r_ij, r_kj, &cos_theta, &t1, &t2);                     /*  41               */
1326
1327         *dvdlambda += harmonic(kthA, kthB, th0A, th0B, theta, lambda, &va, &dVdt); /*  21  */
1328         vtot       += va;
1329
1330         ki   = pbc_rvec_sub(pbc, x[ai], x[ak], r_ik);                               /*   3      */
1331         dr2  = iprod(r_ik, r_ik);                                                   /*   5              */
1332         dr   = dr2*gmx_invsqrt(dr2);                                                /*  10              */
1333
1334         *dvdlambda += harmonic(kUBA, kUBB, r13A, r13B, dr, lambda, &vbond, &fbond); /*  19  */
1335
1336         cos_theta2 = sqr(cos_theta);                                                /*   1              */
1337         if (cos_theta2 < 1)
1338         {
1339             real st, sth;
1340             real cik, cii, ckk;
1341             real nrkj2, nrij2;
1342             rvec f_i, f_j, f_k;
1343
1344             st  = dVdt*gmx_invsqrt(1 - cos_theta2); /*  12              */
1345             sth = st*cos_theta;                     /*   1              */
1346 #ifdef DEBUG
1347             if (debug)
1348             {
1349                 fprintf(debug, "ANGLES: theta = %10g  vth = %10g  dV/dtheta = %10g\n",
1350                         theta*RAD2DEG, va, dVdt);
1351             }
1352 #endif
1353             nrkj2 = iprod(r_kj, r_kj);  /*   5          */
1354             nrij2 = iprod(r_ij, r_ij);
1355
1356             cik = st*gmx_invsqrt(nrkj2*nrij2); /*  12           */
1357             cii = sth/nrij2;                   /*  10           */
1358             ckk = sth/nrkj2;                   /*  10           */
1359
1360             for (m = 0; (m < DIM); m++)        /*  39           */
1361             {
1362                 f_i[m]    = -(cik*r_kj[m]-cii*r_ij[m]);
1363                 f_k[m]    = -(cik*r_ij[m]-ckk*r_kj[m]);
1364                 f_j[m]    = -f_i[m]-f_k[m];
1365                 f[ai][m] += f_i[m];
1366                 f[aj][m] += f_j[m];
1367                 f[ak][m] += f_k[m];
1368             }
1369             if (g)
1370             {
1371                 copy_ivec(SHIFT_IVEC(g, aj), jt);
1372
1373                 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1374                 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1375                 t1 = IVEC2IS(dt_ij);
1376                 t2 = IVEC2IS(dt_kj);
1377             }
1378             rvec_inc(fshift[t1], f_i);
1379             rvec_inc(fshift[CENTRAL], f_j);
1380             rvec_inc(fshift[t2], f_k);
1381         }                                       /* 161 TOTAL    */
1382         /* Time for the bond calculations */
1383         if (dr2 == 0.0)
1384         {
1385             continue;
1386         }
1387
1388         vtot  += vbond;            /* 1*/
1389         fbond *= gmx_invsqrt(dr2); /*   6               */
1390
1391         if (g)
1392         {
1393             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, ak), dt_ik);
1394             ki = IVEC2IS(dt_ik);
1395         }
1396         for (m = 0; (m < DIM); m++)     /*  15          */
1397         {
1398             fik                 = fbond*r_ik[m];
1399             f[ai][m]           += fik;
1400             f[ak][m]           -= fik;
1401             fshift[ki][m]      += fik;
1402             fshift[CENTRAL][m] -= fik;
1403         }
1404     }
1405     return vtot;
1406 }
1407
1408 real quartic_angles(int nbonds,
1409                     const t_iatom forceatoms[], const t_iparams forceparams[],
1410                     const rvec x[], rvec f[], rvec fshift[],
1411                     const t_pbc *pbc, const t_graph *g,
1412                     real gmx_unused lambda, real gmx_unused *dvdlambda,
1413                     const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1414                     int gmx_unused *global_atom_index)
1415 {
1416     int  i, j, ai, aj, ak, t1, t2, type;
1417     rvec r_ij, r_kj;
1418     real cos_theta, cos_theta2, theta, dt, dVdt, va, dtp, c, vtot;
1419     ivec jt, dt_ij, dt_kj;
1420
1421     vtot = 0.0;
1422     for (i = 0; (i < nbonds); )
1423     {
1424         type = forceatoms[i++];
1425         ai   = forceatoms[i++];
1426         aj   = forceatoms[i++];
1427         ak   = forceatoms[i++];
1428
1429         theta  = bond_angle(x[ai], x[aj], x[ak], pbc,
1430                             r_ij, r_kj, &cos_theta, &t1, &t2); /*  41           */
1431
1432         dt = theta - forceparams[type].qangle.theta*DEG2RAD;   /* 2          */
1433
1434         dVdt = 0;
1435         va   = forceparams[type].qangle.c[0];
1436         dtp  = 1.0;
1437         for (j = 1; j <= 4; j++)
1438         {
1439             c     = forceparams[type].qangle.c[j];
1440             dVdt -= j*c*dtp;
1441             dtp  *= dt;
1442             va   += c*dtp;
1443         }
1444         /* 20 */
1445
1446         vtot += va;
1447
1448         cos_theta2 = sqr(cos_theta);            /*   1          */
1449         if (cos_theta2 < 1)
1450         {
1451             int  m;
1452             real st, sth;
1453             real cik, cii, ckk;
1454             real nrkj2, nrij2;
1455             rvec f_i, f_j, f_k;
1456
1457             st  = dVdt*gmx_invsqrt(1 - cos_theta2); /*  12              */
1458             sth = st*cos_theta;                     /*   1              */
1459 #ifdef DEBUG
1460             if (debug)
1461             {
1462                 fprintf(debug, "ANGLES: theta = %10g  vth = %10g  dV/dtheta = %10g\n",
1463                         theta*RAD2DEG, va, dVdt);
1464             }
1465 #endif
1466             nrkj2 = iprod(r_kj, r_kj);  /*   5          */
1467             nrij2 = iprod(r_ij, r_ij);
1468
1469             cik = st*gmx_invsqrt(nrkj2*nrij2); /*  12           */
1470             cii = sth/nrij2;                   /*  10           */
1471             ckk = sth/nrkj2;                   /*  10           */
1472
1473             for (m = 0; (m < DIM); m++)        /*  39           */
1474             {
1475                 f_i[m]    = -(cik*r_kj[m]-cii*r_ij[m]);
1476                 f_k[m]    = -(cik*r_ij[m]-ckk*r_kj[m]);
1477                 f_j[m]    = -f_i[m]-f_k[m];
1478                 f[ai][m] += f_i[m];
1479                 f[aj][m] += f_j[m];
1480                 f[ak][m] += f_k[m];
1481             }
1482             if (g)
1483             {
1484                 copy_ivec(SHIFT_IVEC(g, aj), jt);
1485
1486                 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
1487                 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
1488                 t1 = IVEC2IS(dt_ij);
1489                 t2 = IVEC2IS(dt_kj);
1490             }
1491             rvec_inc(fshift[t1], f_i);
1492             rvec_inc(fshift[CENTRAL], f_j);
1493             rvec_inc(fshift[t2], f_k);
1494         }                                       /* 153 TOTAL    */
1495     }
1496     return vtot;
1497 }
1498
1499 real dih_angle(const rvec xi, const rvec xj, const rvec xk, const rvec xl,
1500                const t_pbc *pbc,
1501                rvec r_ij, rvec r_kj, rvec r_kl, rvec m, rvec n,
1502                real *sign, int *t1, int *t2, int *t3)
1503 {
1504     real ipr, phi;
1505
1506     *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /*  3        */
1507     *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /*  3                */
1508     *t3 = pbc_rvec_sub(pbc, xk, xl, r_kl); /*  3                */
1509
1510     cprod(r_ij, r_kj, m);                  /*  9        */
1511     cprod(r_kj, r_kl, n);                  /*  9                */
1512     phi     = gmx_angle(m, n);             /* 49 (assuming 25 for atan2) */
1513     ipr     = iprod(r_ij, n);              /*  5        */
1514     (*sign) = (ipr < 0.0) ? -1.0 : 1.0;
1515     phi     = (*sign)*phi;                 /*  1                */
1516     /* 82 TOTAL */
1517     return phi;
1518 }
1519
1520
1521 #ifdef GMX_SIMD_HAVE_REAL
1522
1523 /* As dih_angle above, but calculates 4 dihedral angles at once using SIMD,
1524  * also calculates the pre-factor required for the dihedral force update.
1525  * Note that bv and buf should be register aligned.
1526  */
1527 static gmx_inline void
1528 dih_angle_simd(const rvec *x,
1529                const int *ai, const int *aj, const int *ak, const int *al,
1530                const pbc_simd_t *pbc,
1531                real *dr,
1532                gmx_simd_real_t *phi_S,
1533                gmx_simd_real_t *mx_S, gmx_simd_real_t *my_S, gmx_simd_real_t *mz_S,
1534                gmx_simd_real_t *nx_S, gmx_simd_real_t *ny_S, gmx_simd_real_t *nz_S,
1535                gmx_simd_real_t *nrkj_m2_S,
1536                gmx_simd_real_t *nrkj_n2_S,
1537                real *p,
1538                real *q)
1539 {
1540     int             s, m;
1541     gmx_simd_real_t rijx_S, rijy_S, rijz_S;
1542     gmx_simd_real_t rkjx_S, rkjy_S, rkjz_S;
1543     gmx_simd_real_t rklx_S, rkly_S, rklz_S;
1544     gmx_simd_real_t cx_S, cy_S, cz_S;
1545     gmx_simd_real_t cn_S;
1546     gmx_simd_real_t s_S;
1547     gmx_simd_real_t ipr_S;
1548     gmx_simd_real_t iprm_S, iprn_S;
1549     gmx_simd_real_t nrkj2_S, nrkj_1_S, nrkj_2_S, nrkj_S;
1550     gmx_simd_real_t toler_S;
1551     gmx_simd_real_t p_S, q_S;
1552     gmx_simd_real_t nrkj2_min_S;
1553     gmx_simd_real_t real_eps_S;
1554
1555     /* Used to avoid division by zero.
1556      * We take into acount that we multiply the result by real_eps_S.
1557      */
1558     nrkj2_min_S = gmx_simd_set1_r(GMX_REAL_MIN/(2*GMX_REAL_EPS));
1559
1560     /* The value of the last significant bit (GMX_REAL_EPS is half of that) */
1561     real_eps_S  = gmx_simd_set1_r(2*GMX_REAL_EPS);
1562
1563     for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
1564     {
1565         /* If you can't use pbc_dx_simd below for PBC, e.g. because
1566          * you can't round in SIMD, use pbc_rvec_sub here.
1567          */
1568         for (m = 0; m < DIM; m++)
1569         {
1570             dr[s + (0*DIM + m)*GMX_SIMD_REAL_WIDTH] = x[ai[s]][m] - x[aj[s]][m];
1571             dr[s + (1*DIM + m)*GMX_SIMD_REAL_WIDTH] = x[ak[s]][m] - x[aj[s]][m];
1572             dr[s + (2*DIM + m)*GMX_SIMD_REAL_WIDTH] = x[ak[s]][m] - x[al[s]][m];
1573         }
1574     }
1575
1576     rijx_S = gmx_simd_load_r(dr + 0*GMX_SIMD_REAL_WIDTH);
1577     rijy_S = gmx_simd_load_r(dr + 1*GMX_SIMD_REAL_WIDTH);
1578     rijz_S = gmx_simd_load_r(dr + 2*GMX_SIMD_REAL_WIDTH);
1579     rkjx_S = gmx_simd_load_r(dr + 3*GMX_SIMD_REAL_WIDTH);
1580     rkjy_S = gmx_simd_load_r(dr + 4*GMX_SIMD_REAL_WIDTH);
1581     rkjz_S = gmx_simd_load_r(dr + 5*GMX_SIMD_REAL_WIDTH);
1582     rklx_S = gmx_simd_load_r(dr + 6*GMX_SIMD_REAL_WIDTH);
1583     rkly_S = gmx_simd_load_r(dr + 7*GMX_SIMD_REAL_WIDTH);
1584     rklz_S = gmx_simd_load_r(dr + 8*GMX_SIMD_REAL_WIDTH);
1585
1586     pbc_dx_simd(&rijx_S, &rijy_S, &rijz_S, pbc);
1587     pbc_dx_simd(&rkjx_S, &rkjy_S, &rkjz_S, pbc);
1588     pbc_dx_simd(&rklx_S, &rkly_S, &rklz_S, pbc);
1589
1590     gmx_simd_cprod_r(rijx_S, rijy_S, rijz_S,
1591                      rkjx_S, rkjy_S, rkjz_S,
1592                      mx_S, my_S, mz_S);
1593
1594     gmx_simd_cprod_r(rkjx_S, rkjy_S, rkjz_S,
1595                      rklx_S, rkly_S, rklz_S,
1596                      nx_S, ny_S, nz_S);
1597
1598     gmx_simd_cprod_r(*mx_S, *my_S, *mz_S,
1599                      *nx_S, *ny_S, *nz_S,
1600                      &cx_S, &cy_S, &cz_S);
1601
1602     cn_S       = gmx_simd_sqrt_r(gmx_simd_norm2_r(cx_S, cy_S, cz_S));
1603
1604     s_S        = gmx_simd_iprod_r(*mx_S, *my_S, *mz_S, *nx_S, *ny_S, *nz_S);
1605
1606     /* Determine the dihedral angle, the sign might need correction */
1607     *phi_S     = gmx_simd_atan2_r(cn_S, s_S);
1608
1609     ipr_S      = gmx_simd_iprod_r(rijx_S, rijy_S, rijz_S,
1610                                   *nx_S, *ny_S, *nz_S);
1611
1612     iprm_S     = gmx_simd_norm2_r(*mx_S, *my_S, *mz_S);
1613     iprn_S     = gmx_simd_norm2_r(*nx_S, *ny_S, *nz_S);
1614
1615     nrkj2_S    = gmx_simd_norm2_r(rkjx_S, rkjy_S, rkjz_S);
1616
1617     /* Avoid division by zero. When zero, the result is multiplied by 0
1618      * anyhow, so the 3 max below do not affect the final result.
1619      */
1620     nrkj2_S    = gmx_simd_max_r(nrkj2_S, nrkj2_min_S);
1621     nrkj_1_S   = gmx_simd_invsqrt_r(nrkj2_S);
1622     nrkj_2_S   = gmx_simd_mul_r(nrkj_1_S, nrkj_1_S);
1623     nrkj_S     = gmx_simd_mul_r(nrkj2_S, nrkj_1_S);
1624
1625     toler_S    = gmx_simd_mul_r(nrkj2_S, real_eps_S);
1626
1627     /* Here the plain-C code uses a conditional, but we can't do that in SIMD.
1628      * So we take a max with the tolerance instead. Since we multiply with
1629      * m or n later, the max does not affect the results.
1630      */
1631     iprm_S     = gmx_simd_max_r(iprm_S, toler_S);
1632     iprn_S     = gmx_simd_max_r(iprn_S, toler_S);
1633     *nrkj_m2_S = gmx_simd_mul_r(nrkj_S, gmx_simd_inv_r(iprm_S));
1634     *nrkj_n2_S = gmx_simd_mul_r(nrkj_S, gmx_simd_inv_r(iprn_S));
1635
1636     /* Set sign of phi_S with the sign of ipr_S; phi_S is currently positive */
1637     *phi_S     = gmx_simd_xor_sign_r(*phi_S, ipr_S);
1638     p_S        = gmx_simd_iprod_r(rijx_S, rijy_S, rijz_S,
1639                                   rkjx_S, rkjy_S, rkjz_S);
1640     p_S        = gmx_simd_mul_r(p_S, nrkj_2_S);
1641
1642     q_S        = gmx_simd_iprod_r(rklx_S, rkly_S, rklz_S,
1643                                   rkjx_S, rkjy_S, rkjz_S);
1644     q_S        = gmx_simd_mul_r(q_S, nrkj_2_S);
1645
1646     gmx_simd_store_r(p, p_S);
1647     gmx_simd_store_r(q, q_S);
1648 }
1649
1650 #endif /* GMX_SIMD_HAVE_REAL */
1651
1652
1653 void do_dih_fup(int i, int j, int k, int l, real ddphi,
1654                 rvec r_ij, rvec r_kj, rvec r_kl,
1655                 rvec m, rvec n, rvec f[], rvec fshift[],
1656                 const t_pbc *pbc, const t_graph *g,
1657                 const rvec x[], int t1, int t2, int t3)
1658 {
1659     /* 143 FLOPS */
1660     rvec f_i, f_j, f_k, f_l;
1661     rvec uvec, vvec, svec, dx_jl;
1662     real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1663     real a, b, p, q, toler;
1664     ivec jt, dt_ij, dt_kj, dt_lj;
1665
1666     iprm  = iprod(m, m);       /*  5    */
1667     iprn  = iprod(n, n);       /*  5    */
1668     nrkj2 = iprod(r_kj, r_kj); /*  5    */
1669     toler = nrkj2*GMX_REAL_EPS;
1670     if ((iprm > toler) && (iprn > toler))
1671     {
1672         nrkj_1 = gmx_invsqrt(nrkj2); /* 10      */
1673         nrkj_2 = nrkj_1*nrkj_1;      /*  1      */
1674         nrkj   = nrkj2*nrkj_1;       /*  1      */
1675         a      = -ddphi*nrkj/iprm;   /* 11      */
1676         svmul(a, m, f_i);            /*  3      */
1677         b     = ddphi*nrkj/iprn;     /* 11      */
1678         svmul(b, n, f_l);            /*  3  */
1679         p     = iprod(r_ij, r_kj);   /*  5      */
1680         p    *= nrkj_2;              /*  1      */
1681         q     = iprod(r_kl, r_kj);   /*  5      */
1682         q    *= nrkj_2;              /*  1      */
1683         svmul(p, f_i, uvec);         /*  3      */
1684         svmul(q, f_l, vvec);         /*  3      */
1685         rvec_sub(uvec, vvec, svec);  /*  3      */
1686         rvec_sub(f_i, svec, f_j);    /*  3      */
1687         rvec_add(f_l, svec, f_k);    /*  3      */
1688         rvec_inc(f[i], f_i);         /*  3      */
1689         rvec_dec(f[j], f_j);         /*  3      */
1690         rvec_dec(f[k], f_k);         /*  3      */
1691         rvec_inc(f[l], f_l);         /*  3      */
1692
1693         if (g)
1694         {
1695             copy_ivec(SHIFT_IVEC(g, j), jt);
1696             ivec_sub(SHIFT_IVEC(g, i), jt, dt_ij);
1697             ivec_sub(SHIFT_IVEC(g, k), jt, dt_kj);
1698             ivec_sub(SHIFT_IVEC(g, l), jt, dt_lj);
1699             t1 = IVEC2IS(dt_ij);
1700             t2 = IVEC2IS(dt_kj);
1701             t3 = IVEC2IS(dt_lj);
1702         }
1703         else if (pbc)
1704         {
1705             t3 = pbc_rvec_sub(pbc, x[l], x[j], dx_jl);
1706         }
1707         else
1708         {
1709             t3 = CENTRAL;
1710         }
1711
1712         rvec_inc(fshift[t1], f_i);
1713         rvec_dec(fshift[CENTRAL], f_j);
1714         rvec_dec(fshift[t2], f_k);
1715         rvec_inc(fshift[t3], f_l);
1716     }
1717     /* 112 TOTAL    */
1718 }
1719
1720 /* As do_dih_fup above, but without shift forces */
1721 static void
1722 do_dih_fup_noshiftf(int i, int j, int k, int l, real ddphi,
1723                     rvec r_ij, rvec r_kj, rvec r_kl,
1724                     rvec m, rvec n, rvec f[])
1725 {
1726     rvec f_i, f_j, f_k, f_l;
1727     rvec uvec, vvec, svec;
1728     real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
1729     real a, b, p, q, toler;
1730
1731     iprm  = iprod(m, m);       /*  5    */
1732     iprn  = iprod(n, n);       /*  5    */
1733     nrkj2 = iprod(r_kj, r_kj); /*  5    */
1734     toler = nrkj2*GMX_REAL_EPS;
1735     if ((iprm > toler) && (iprn > toler))
1736     {
1737         nrkj_1 = gmx_invsqrt(nrkj2); /* 10      */
1738         nrkj_2 = nrkj_1*nrkj_1;      /*  1      */
1739         nrkj   = nrkj2*nrkj_1;       /*  1      */
1740         a      = -ddphi*nrkj/iprm;   /* 11      */
1741         svmul(a, m, f_i);            /*  3      */
1742         b     = ddphi*nrkj/iprn;     /* 11      */
1743         svmul(b, n, f_l);            /*  3  */
1744         p     = iprod(r_ij, r_kj);   /*  5      */
1745         p    *= nrkj_2;              /*  1      */
1746         q     = iprod(r_kl, r_kj);   /*  5      */
1747         q    *= nrkj_2;              /*  1      */
1748         svmul(p, f_i, uvec);         /*  3      */
1749         svmul(q, f_l, vvec);         /*  3      */
1750         rvec_sub(uvec, vvec, svec);  /*  3      */
1751         rvec_sub(f_i, svec, f_j);    /*  3      */
1752         rvec_add(f_l, svec, f_k);    /*  3      */
1753         rvec_inc(f[i], f_i);         /*  3      */
1754         rvec_dec(f[j], f_j);         /*  3      */
1755         rvec_dec(f[k], f_k);         /*  3      */
1756         rvec_inc(f[l], f_l);         /*  3      */
1757     }
1758 }
1759
1760 /* As do_dih_fup_noshiftf above, but with pre-calculated pre-factors */
1761 static gmx_inline void
1762 do_dih_fup_noshiftf_precalc(int i, int j, int k, int l,
1763                             real p, real q,
1764                             real f_i_x, real f_i_y, real f_i_z,
1765                             real mf_l_x, real mf_l_y, real mf_l_z,
1766                             rvec f[])
1767 {
1768     rvec f_i, f_j, f_k, f_l;
1769     rvec uvec, vvec, svec;
1770
1771     f_i[XX] = f_i_x;
1772     f_i[YY] = f_i_y;
1773     f_i[ZZ] = f_i_z;
1774     f_l[XX] = -mf_l_x;
1775     f_l[YY] = -mf_l_y;
1776     f_l[ZZ] = -mf_l_z;
1777     svmul(p, f_i, uvec);
1778     svmul(q, f_l, vvec);
1779     rvec_sub(uvec, vvec, svec);
1780     rvec_sub(f_i, svec, f_j);
1781     rvec_add(f_l, svec, f_k);
1782     rvec_inc(f[i], f_i);
1783     rvec_dec(f[j], f_j);
1784     rvec_dec(f[k], f_k);
1785     rvec_inc(f[l], f_l);
1786 }
1787
1788
1789 real dopdihs(real cpA, real cpB, real phiA, real phiB, int mult,
1790              real phi, real lambda, real *V, real *F)
1791 {
1792     real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1793     real L1   = 1.0 - lambda;
1794     real ph0  = (L1*phiA + lambda*phiB)*DEG2RAD;
1795     real dph0 = (phiB - phiA)*DEG2RAD;
1796     real cp   = L1*cpA + lambda*cpB;
1797
1798     mdphi =  mult*phi - ph0;
1799     sdphi = sin(mdphi);
1800     ddphi = -cp*mult*sdphi;
1801     v1    = 1.0 + cos(mdphi);
1802     v     = cp*v1;
1803
1804     dvdlambda  = (cpB - cpA)*v1 + cp*dph0*sdphi;
1805
1806     *V = v;
1807     *F = ddphi;
1808
1809     return dvdlambda;
1810
1811     /* That was 40 flops */
1812 }
1813
1814 static void
1815 dopdihs_noener(real cpA, real cpB, real phiA, real phiB, int mult,
1816                real phi, real lambda, real *F)
1817 {
1818     real mdphi, sdphi, ddphi;
1819     real L1   = 1.0 - lambda;
1820     real ph0  = (L1*phiA + lambda*phiB)*DEG2RAD;
1821     real cp   = L1*cpA + lambda*cpB;
1822
1823     mdphi = mult*phi - ph0;
1824     sdphi = sin(mdphi);
1825     ddphi = -cp*mult*sdphi;
1826
1827     *F = ddphi;
1828
1829     /* That was 20 flops */
1830 }
1831
1832 static void
1833 dopdihs_mdphi(real cpA, real cpB, real phiA, real phiB, int mult,
1834               real phi, real lambda, real *cp, real *mdphi)
1835 {
1836     real L1   = 1.0 - lambda;
1837     real ph0  = (L1*phiA + lambda*phiB)*DEG2RAD;
1838
1839     *cp    = L1*cpA + lambda*cpB;
1840
1841     *mdphi = mult*phi - ph0;
1842 }
1843
1844 static real dopdihs_min(real cpA, real cpB, real phiA, real phiB, int mult,
1845                         real phi, real lambda, real *V, real *F)
1846 /* similar to dopdihs, except for a minus sign  *
1847  * and a different treatment of mult/phi0       */
1848 {
1849     real v, dvdlambda, mdphi, v1, sdphi, ddphi;
1850     real L1   = 1.0 - lambda;
1851     real ph0  = (L1*phiA + lambda*phiB)*DEG2RAD;
1852     real dph0 = (phiB - phiA)*DEG2RAD;
1853     real cp   = L1*cpA + lambda*cpB;
1854
1855     mdphi = mult*(phi-ph0);
1856     sdphi = sin(mdphi);
1857     ddphi = cp*mult*sdphi;
1858     v1    = 1.0-cos(mdphi);
1859     v     = cp*v1;
1860
1861     dvdlambda  = (cpB-cpA)*v1 + cp*dph0*sdphi;
1862
1863     *V = v;
1864     *F = ddphi;
1865
1866     return dvdlambda;
1867
1868     /* That was 40 flops */
1869 }
1870
1871 real pdihs(int nbonds,
1872            const t_iatom forceatoms[], const t_iparams forceparams[],
1873            const rvec x[], rvec f[], rvec fshift[],
1874            const t_pbc *pbc, const t_graph *g,
1875            real lambda, real *dvdlambda,
1876            const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1877            int gmx_unused *global_atom_index)
1878 {
1879     int  i, type, ai, aj, ak, al;
1880     int  t1, t2, t3;
1881     rvec r_ij, r_kj, r_kl, m, n;
1882     real phi, sign, ddphi, vpd, vtot;
1883
1884     vtot = 0.0;
1885
1886     for (i = 0; (i < nbonds); )
1887     {
1888         type = forceatoms[i++];
1889         ai   = forceatoms[i++];
1890         aj   = forceatoms[i++];
1891         ak   = forceatoms[i++];
1892         al   = forceatoms[i++];
1893
1894         phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1895                         &sign, &t1, &t2, &t3);  /*  84      */
1896         *dvdlambda += dopdihs(forceparams[type].pdihs.cpA,
1897                               forceparams[type].pdihs.cpB,
1898                               forceparams[type].pdihs.phiA,
1899                               forceparams[type].pdihs.phiB,
1900                               forceparams[type].pdihs.mult,
1901                               phi, lambda, &vpd, &ddphi);
1902
1903         vtot += vpd;
1904         do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
1905                    f, fshift, pbc, g, x, t1, t2, t3); /* 112            */
1906
1907 #ifdef DEBUG
1908         fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
1909                 ai, aj, ak, al, phi);
1910 #endif
1911     } /* 223 TOTAL  */
1912
1913     return vtot;
1914 }
1915
1916 void make_dp_periodic(real *dp)  /* 1 flop? */
1917 {
1918     /* dp cannot be outside (-pi,pi) */
1919     if (*dp >= M_PI)
1920     {
1921         *dp -= 2*M_PI;
1922     }
1923     else if (*dp < -M_PI)
1924     {
1925         *dp += 2*M_PI;
1926     }
1927     return;
1928 }
1929
1930 /* As pdihs above, but without calculating energies and shift forces */
1931 static void
1932 pdihs_noener(int nbonds,
1933              const t_iatom forceatoms[], const t_iparams forceparams[],
1934              const rvec x[], rvec f[],
1935              const t_pbc gmx_unused *pbc, const t_graph gmx_unused *g,
1936              real lambda,
1937              const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1938              int gmx_unused *global_atom_index)
1939 {
1940     int  i, type, ai, aj, ak, al;
1941     int  t1, t2, t3;
1942     rvec r_ij, r_kj, r_kl, m, n;
1943     real phi, sign, ddphi_tot, ddphi;
1944
1945     for (i = 0; (i < nbonds); )
1946     {
1947         ai   = forceatoms[i+1];
1948         aj   = forceatoms[i+2];
1949         ak   = forceatoms[i+3];
1950         al   = forceatoms[i+4];
1951
1952         phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
1953                         &sign, &t1, &t2, &t3);
1954
1955         ddphi_tot = 0;
1956
1957         /* Loop over dihedrals working on the same atoms,
1958          * so we avoid recalculating angles and force distributions.
1959          */
1960         do
1961         {
1962             type = forceatoms[i];
1963             dopdihs_noener(forceparams[type].pdihs.cpA,
1964                            forceparams[type].pdihs.cpB,
1965                            forceparams[type].pdihs.phiA,
1966                            forceparams[type].pdihs.phiB,
1967                            forceparams[type].pdihs.mult,
1968                            phi, lambda, &ddphi);
1969             ddphi_tot += ddphi;
1970
1971             i += 5;
1972         }
1973         while (i < nbonds &&
1974                forceatoms[i+1] == ai &&
1975                forceatoms[i+2] == aj &&
1976                forceatoms[i+3] == ak &&
1977                forceatoms[i+4] == al);
1978
1979         do_dih_fup_noshiftf(ai, aj, ak, al, ddphi_tot, r_ij, r_kj, r_kl, m, n, f);
1980     }
1981 }
1982
1983
1984 #ifdef GMX_SIMD_HAVE_REAL
1985
1986 /* As pdihs_noner above, but using SIMD to calculate many dihedrals at once */
1987 static void
1988 pdihs_noener_simd(int nbonds,
1989                   const t_iatom forceatoms[], const t_iparams forceparams[],
1990                   const rvec x[], rvec f[],
1991                   const t_pbc *pbc, const t_graph gmx_unused *g,
1992                   real gmx_unused lambda,
1993                   const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
1994                   int gmx_unused *global_atom_index)
1995 {
1996     const int             nfa1 = 5;
1997     int                   i, iu, s;
1998     int                   type, ai[GMX_SIMD_REAL_WIDTH], aj[GMX_SIMD_REAL_WIDTH], ak[GMX_SIMD_REAL_WIDTH], al[GMX_SIMD_REAL_WIDTH];
1999     real                  dr_array[3*DIM*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *dr;
2000     real                  buf_array[7*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *buf;
2001     real                 *cp, *phi0, *mult, *p, *q;
2002     gmx_simd_real_t       phi0_S, phi_S;
2003     gmx_simd_real_t       mx_S, my_S, mz_S;
2004     gmx_simd_real_t       nx_S, ny_S, nz_S;
2005     gmx_simd_real_t       nrkj_m2_S, nrkj_n2_S;
2006     gmx_simd_real_t       cp_S, mdphi_S, mult_S;
2007     gmx_simd_real_t       sin_S, cos_S;
2008     gmx_simd_real_t       mddphi_S;
2009     gmx_simd_real_t       sf_i_S, msf_l_S;
2010     pbc_simd_t            pbc_simd;
2011
2012     /* Ensure SIMD register alignment */
2013     dr  = gmx_simd_align_r(dr_array);
2014     buf = gmx_simd_align_r(buf_array);
2015
2016     /* Extract aligned pointer for parameters and variables */
2017     cp    = buf + 0*GMX_SIMD_REAL_WIDTH;
2018     phi0  = buf + 1*GMX_SIMD_REAL_WIDTH;
2019     mult  = buf + 2*GMX_SIMD_REAL_WIDTH;
2020     p     = buf + 3*GMX_SIMD_REAL_WIDTH;
2021     q     = buf + 4*GMX_SIMD_REAL_WIDTH;
2022
2023     set_pbc_simd(pbc, &pbc_simd);
2024
2025     /* nbonds is the number of dihedrals times nfa1, here we step GMX_SIMD_REAL_WIDTH dihs */
2026     for (i = 0; (i < nbonds); i += GMX_SIMD_REAL_WIDTH*nfa1)
2027     {
2028         /* Collect atoms quadruplets for GMX_SIMD_REAL_WIDTH dihedrals.
2029          * iu indexes into forceatoms, we should not let iu go beyond nbonds.
2030          */
2031         iu = i;
2032         for (s = 0; s < GMX_SIMD_REAL_WIDTH; s++)
2033         {
2034             type  = forceatoms[iu];
2035             ai[s] = forceatoms[iu+1];
2036             aj[s] = forceatoms[iu+2];
2037             ak[s] = forceatoms[iu+3];
2038             al[s] = forceatoms[iu+4];
2039
2040             cp[s]   = forceparams[type].pdihs.cpA;
2041             phi0[s] = forceparams[type].pdihs.phiA*DEG2RAD;
2042             mult[s] = forceparams[type].pdihs.mult;
2043
2044             /* At the end fill the arrays with identical entries */
2045             if (iu + nfa1 < nbonds)
2046             {
2047                 iu += nfa1;
2048             }
2049         }
2050
2051         /* Caclulate GMX_SIMD_REAL_WIDTH dihedral angles at once */
2052         dih_angle_simd(x, ai, aj, ak, al, &pbc_simd,
2053                        dr,
2054                        &phi_S,
2055                        &mx_S, &my_S, &mz_S,
2056                        &nx_S, &ny_S, &nz_S,
2057                        &nrkj_m2_S,
2058                        &nrkj_n2_S,
2059                        p, q);
2060
2061         cp_S     = gmx_simd_load_r(cp);
2062         phi0_S   = gmx_simd_load_r(phi0);
2063         mult_S   = gmx_simd_load_r(mult);
2064
2065         mdphi_S  = gmx_simd_sub_r(gmx_simd_mul_r(mult_S, phi_S), phi0_S);
2066
2067         /* Calculate GMX_SIMD_REAL_WIDTH sines at once */
2068         gmx_simd_sincos_r(mdphi_S, &sin_S, &cos_S);
2069         mddphi_S = gmx_simd_mul_r(gmx_simd_mul_r(cp_S, mult_S), sin_S);
2070         sf_i_S   = gmx_simd_mul_r(mddphi_S, nrkj_m2_S);
2071         msf_l_S  = gmx_simd_mul_r(mddphi_S, nrkj_n2_S);
2072
2073         /* After this m?_S will contain f[i] */
2074         mx_S     = gmx_simd_mul_r(sf_i_S, mx_S);
2075         my_S     = gmx_simd_mul_r(sf_i_S, my_S);
2076         mz_S     = gmx_simd_mul_r(sf_i_S, mz_S);
2077
2078         /* After this m?_S will contain -f[l] */
2079         nx_S     = gmx_simd_mul_r(msf_l_S, nx_S);
2080         ny_S     = gmx_simd_mul_r(msf_l_S, ny_S);
2081         nz_S     = gmx_simd_mul_r(msf_l_S, nz_S);
2082
2083         gmx_simd_store_r(dr + 0*GMX_SIMD_REAL_WIDTH, mx_S);
2084         gmx_simd_store_r(dr + 1*GMX_SIMD_REAL_WIDTH, my_S);
2085         gmx_simd_store_r(dr + 2*GMX_SIMD_REAL_WIDTH, mz_S);
2086         gmx_simd_store_r(dr + 3*GMX_SIMD_REAL_WIDTH, nx_S);
2087         gmx_simd_store_r(dr + 4*GMX_SIMD_REAL_WIDTH, ny_S);
2088         gmx_simd_store_r(dr + 5*GMX_SIMD_REAL_WIDTH, nz_S);
2089
2090         iu = i;
2091         s  = 0;
2092         do
2093         {
2094             do_dih_fup_noshiftf_precalc(ai[s], aj[s], ak[s], al[s],
2095                                         p[s], q[s],
2096                                         dr[     XX *GMX_SIMD_REAL_WIDTH+s],
2097                                         dr[     YY *GMX_SIMD_REAL_WIDTH+s],
2098                                         dr[     ZZ *GMX_SIMD_REAL_WIDTH+s],
2099                                         dr[(DIM+XX)*GMX_SIMD_REAL_WIDTH+s],
2100                                         dr[(DIM+YY)*GMX_SIMD_REAL_WIDTH+s],
2101                                         dr[(DIM+ZZ)*GMX_SIMD_REAL_WIDTH+s],
2102                                         f);
2103             s++;
2104             iu += nfa1;
2105         }
2106         while (s < GMX_SIMD_REAL_WIDTH && iu < nbonds);
2107     }
2108 }
2109
2110 #endif /* GMX_SIMD_HAVE_REAL */
2111
2112
2113 real idihs(int nbonds,
2114            const t_iatom forceatoms[], const t_iparams forceparams[],
2115            const rvec x[], rvec f[], rvec fshift[],
2116            const t_pbc *pbc, const t_graph *g,
2117            real lambda, real *dvdlambda,
2118            const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2119            int gmx_unused *global_atom_index)
2120 {
2121     int  i, type, ai, aj, ak, al;
2122     int  t1, t2, t3;
2123     real phi, phi0, dphi0, ddphi, sign, vtot;
2124     rvec r_ij, r_kj, r_kl, m, n;
2125     real L1, kk, dp, dp2, kA, kB, pA, pB, dvdl_term;
2126
2127     L1        = 1.0-lambda;
2128     dvdl_term = 0;
2129     vtot      = 0.0;
2130     for (i = 0; (i < nbonds); )
2131     {
2132         type = forceatoms[i++];
2133         ai   = forceatoms[i++];
2134         aj   = forceatoms[i++];
2135         ak   = forceatoms[i++];
2136         al   = forceatoms[i++];
2137
2138         phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2139                         &sign, &t1, &t2, &t3);  /*  84          */
2140
2141         /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2142          * force changes if we just apply a normal harmonic.
2143          * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2144          * This means we will never have the periodicity problem, unless
2145          * the dihedral is Pi away from phiO, which is very unlikely due to
2146          * the potential.
2147          */
2148         kA = forceparams[type].harmonic.krA;
2149         kB = forceparams[type].harmonic.krB;
2150         pA = forceparams[type].harmonic.rA;
2151         pB = forceparams[type].harmonic.rB;
2152
2153         kk    = L1*kA + lambda*kB;
2154         phi0  = (L1*pA + lambda*pB)*DEG2RAD;
2155         dphi0 = (pB - pA)*DEG2RAD;
2156
2157         dp = phi-phi0;
2158
2159         make_dp_periodic(&dp);
2160
2161         dp2 = dp*dp;
2162
2163         vtot += 0.5*kk*dp2;
2164         ddphi = -kk*dp;
2165
2166         dvdl_term += 0.5*(kB - kA)*dp2 - kk*dphi0*dp;
2167
2168         do_dih_fup(ai, aj, ak, al, -ddphi, r_ij, r_kj, r_kl, m, n,
2169                    f, fshift, pbc, g, x, t1, t2, t3); /* 112            */
2170         /* 218 TOTAL    */
2171 #ifdef DEBUG
2172         if (debug)
2173         {
2174             fprintf(debug, "idih: (%d,%d,%d,%d) phi=%g\n",
2175                     ai, aj, ak, al, phi);
2176         }
2177 #endif
2178     }
2179
2180     *dvdlambda += dvdl_term;
2181     return vtot;
2182 }
2183
2184
2185 /*! \brief returns dx, rdist, and dpdl for functions posres() and fbposres()
2186  */
2187 static void posres_dx(const rvec x, const rvec pos0A, const rvec pos0B,
2188                       const rvec comA_sc, const rvec comB_sc,
2189                       real lambda,
2190                       t_pbc *pbc, int refcoord_scaling, int npbcdim,
2191                       rvec dx, rvec rdist, rvec dpdl)
2192 {
2193     int  m, d;
2194     real posA, posB, L1, ref = 0.;
2195     rvec pos;
2196
2197     L1 = 1.0-lambda;
2198
2199     for (m = 0; m < DIM; m++)
2200     {
2201         posA = pos0A[m];
2202         posB = pos0B[m];
2203         if (m < npbcdim)
2204         {
2205             switch (refcoord_scaling)
2206             {
2207                 case erscNO:
2208                     ref      = 0;
2209                     rdist[m] = L1*posA + lambda*posB;
2210                     dpdl[m]  = posB - posA;
2211                     break;
2212                 case erscALL:
2213                     /* Box relative coordinates are stored for dimensions with pbc */
2214                     posA *= pbc->box[m][m];
2215                     posB *= pbc->box[m][m];
2216                     assert(npbcdim <= DIM);
2217                     for (d = m+1; d < npbcdim; d++)
2218                     {
2219                         posA += pos0A[d]*pbc->box[d][m];
2220                         posB += pos0B[d]*pbc->box[d][m];
2221                     }
2222                     ref      = L1*posA + lambda*posB;
2223                     rdist[m] = 0;
2224                     dpdl[m]  = posB - posA;
2225                     break;
2226                 case erscCOM:
2227                     ref      = L1*comA_sc[m] + lambda*comB_sc[m];
2228                     rdist[m] = L1*posA       + lambda*posB;
2229                     dpdl[m]  = comB_sc[m] - comA_sc[m] + posB - posA;
2230                     break;
2231                 default:
2232                     gmx_fatal(FARGS, "No such scaling method implemented");
2233             }
2234         }
2235         else
2236         {
2237             ref      = L1*posA + lambda*posB;
2238             rdist[m] = 0;
2239             dpdl[m]  = posB - posA;
2240         }
2241
2242         /* We do pbc_dx with ref+rdist,
2243          * since with only ref we can be up to half a box vector wrong.
2244          */
2245         pos[m] = ref + rdist[m];
2246     }
2247
2248     if (pbc)
2249     {
2250         pbc_dx(pbc, x, pos, dx);
2251     }
2252     else
2253     {
2254         rvec_sub(x, pos, dx);
2255     }
2256 }
2257
2258 /*! \brief Computes forces and potential for flat-bottom cylindrical restraints.
2259  *         Returns the flat-bottom potential. */
2260 static real do_fbposres_cylinder(int fbdim, rvec fm, rvec dx, real rfb, real kk, gmx_bool bInvert)
2261 {
2262     int     d;
2263     real    dr, dr2, invdr, v, rfb2;
2264
2265     dr2  = 0.0;
2266     rfb2 = sqr(rfb);
2267     v    = 0.0;
2268
2269     for (d = 0; d < DIM; d++)
2270     {
2271         if (d != fbdim)
2272         {
2273             dr2 += sqr(dx[d]);
2274         }
2275     }
2276
2277     if  (dr2 > 0.0 &&
2278          ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2279          )
2280     {
2281         dr     = sqrt(dr2);
2282         invdr  = 1./dr;
2283         v      = 0.5*kk*sqr(dr - rfb);
2284         for (d = 0; d < DIM; d++)
2285         {
2286             if (d != fbdim)
2287             {
2288                 fm[d] = -kk*(dr-rfb)*dx[d]*invdr; /* Force pointing to the center */
2289             }
2290         }
2291     }
2292
2293     return v;
2294 }
2295
2296 /*! \brief Adds forces of flat-bottomed positions restraints to f[]
2297  *         and fixes vir_diag. Returns the flat-bottomed potential. */
2298 real fbposres(int nbonds,
2299               const t_iatom forceatoms[], const t_iparams forceparams[],
2300               const rvec x[], rvec f[], rvec vir_diag,
2301               t_pbc *pbc,
2302               int refcoord_scaling, int ePBC, rvec com)
2303 /* compute flat-bottomed positions restraints */
2304 {
2305     int              i, ai, m, d, type, npbcdim = 0, fbdim;
2306     const t_iparams *pr;
2307     real             vtot, kk, v;
2308     real             dr, dr2, rfb, rfb2, fact;
2309     rvec             com_sc, rdist, dx, dpdl, fm;
2310     gmx_bool         bInvert;
2311
2312     npbcdim = ePBC2npbcdim(ePBC);
2313
2314     if (refcoord_scaling == erscCOM)
2315     {
2316         clear_rvec(com_sc);
2317         for (m = 0; m < npbcdim; m++)
2318         {
2319             assert(npbcdim <= DIM);
2320             for (d = m; d < npbcdim; d++)
2321             {
2322                 com_sc[m] += com[d]*pbc->box[d][m];
2323             }
2324         }
2325     }
2326
2327     vtot = 0.0;
2328     for (i = 0; (i < nbonds); )
2329     {
2330         type = forceatoms[i++];
2331         ai   = forceatoms[i++];
2332         pr   = &forceparams[type];
2333
2334         /* same calculation as for normal posres, but with identical A and B states, and lambda==0 */
2335         posres_dx(x[ai], forceparams[type].fbposres.pos0, forceparams[type].fbposres.pos0,
2336                   com_sc, com_sc, 0.0,
2337                   pbc, refcoord_scaling, npbcdim,
2338                   dx, rdist, dpdl);
2339
2340         clear_rvec(fm);
2341         v = 0.0;
2342
2343         kk   = pr->fbposres.k;
2344         rfb  = pr->fbposres.r;
2345         rfb2 = sqr(rfb);
2346
2347         /* with rfb<0, push particle out of the sphere/cylinder/layer */
2348         bInvert = FALSE;
2349         if (rfb < 0.)
2350         {
2351             bInvert = TRUE;
2352             rfb     = -rfb;
2353         }
2354
2355         switch (pr->fbposres.geom)
2356         {
2357             case efbposresSPHERE:
2358                 /* spherical flat-bottom posres */
2359                 dr2 = norm2(dx);
2360                 if (dr2 > 0.0 &&
2361                     ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2362                     )
2363                 {
2364                     dr   = sqrt(dr2);
2365                     v    = 0.5*kk*sqr(dr - rfb);
2366                     fact = -kk*(dr-rfb)/dr; /* Force pointing to the center pos0 */
2367                     svmul(fact, dx, fm);
2368                 }
2369                 break;
2370             case efbposresCYLINDERX:
2371                 /* cylindrical flat-bottom posres in y-z plane. fm[XX] = 0. */
2372                 fbdim = XX;
2373                 v     = do_fbposres_cylinder(fbdim, fm, dx, rfb, kk, bInvert);
2374                 break;
2375             case efbposresCYLINDERY:
2376                 /* cylindrical flat-bottom posres in x-z plane. fm[YY] = 0. */
2377                 fbdim = YY;
2378                 v     = do_fbposres_cylinder(fbdim, fm, dx, rfb, kk, bInvert);
2379                 break;
2380             case efbposresCYLINDER:
2381             /* equivalent to efbposresCYLINDERZ for backwards compatibility */
2382             case efbposresCYLINDERZ:
2383                 /* cylindrical flat-bottom posres in x-y plane. fm[ZZ] = 0. */
2384                 fbdim = ZZ;
2385                 v     = do_fbposres_cylinder(fbdim, fm, dx, rfb, kk, bInvert);
2386                 break;
2387             case efbposresX: /* fbdim=XX */
2388             case efbposresY: /* fbdim=YY */
2389             case efbposresZ: /* fbdim=ZZ */
2390                 /* 1D flat-bottom potential */
2391                 fbdim = pr->fbposres.geom - efbposresX;
2392                 dr    = dx[fbdim];
2393                 if ( ( dr > rfb && bInvert == FALSE ) || ( 0 < dr && dr < rfb && bInvert == TRUE )  )
2394                 {
2395                     v         = 0.5*kk*sqr(dr - rfb);
2396                     fm[fbdim] = -kk*(dr - rfb);
2397                 }
2398                 else if ( (dr < (-rfb) && bInvert == FALSE ) || ( (-rfb) < dr && dr < 0 && bInvert == TRUE ))
2399                 {
2400                     v         = 0.5*kk*sqr(dr + rfb);
2401                     fm[fbdim] = -kk*(dr + rfb);
2402                 }
2403                 break;
2404         }
2405
2406         vtot += v;
2407
2408         for (m = 0; (m < DIM); m++)
2409         {
2410             f[ai][m]   += fm[m];
2411             /* Here we correct for the pbc_dx which included rdist */
2412             vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm[m];
2413         }
2414     }
2415
2416     return vtot;
2417 }
2418
2419
2420 real posres(int nbonds,
2421             const t_iatom forceatoms[], const t_iparams forceparams[],
2422             const rvec x[], rvec f[], rvec vir_diag,
2423             t_pbc *pbc,
2424             real lambda, real *dvdlambda,
2425             int refcoord_scaling, int ePBC, rvec comA, rvec comB)
2426 {
2427     int              i, ai, m, d, type, npbcdim = 0;
2428     const t_iparams *pr;
2429     real             L1;
2430     real             vtot, kk, fm;
2431     rvec             comA_sc, comB_sc, rdist, dpdl, dx;
2432     gmx_bool         bForceValid = TRUE;
2433
2434     if ((f == NULL) || (vir_diag == NULL))    /* should both be null together! */
2435     {
2436         bForceValid = FALSE;
2437     }
2438
2439     npbcdim = ePBC2npbcdim(ePBC);
2440
2441     if (refcoord_scaling == erscCOM)
2442     {
2443         clear_rvec(comA_sc);
2444         clear_rvec(comB_sc);
2445         for (m = 0; m < npbcdim; m++)
2446         {
2447             assert(npbcdim <= DIM);
2448             for (d = m; d < npbcdim; d++)
2449             {
2450                 comA_sc[m] += comA[d]*pbc->box[d][m];
2451                 comB_sc[m] += comB[d]*pbc->box[d][m];
2452             }
2453         }
2454     }
2455
2456     L1 = 1.0 - lambda;
2457
2458     vtot = 0.0;
2459     for (i = 0; (i < nbonds); )
2460     {
2461         type = forceatoms[i++];
2462         ai   = forceatoms[i++];
2463         pr   = &forceparams[type];
2464
2465         /* return dx, rdist, and dpdl */
2466         posres_dx(x[ai], forceparams[type].posres.pos0A, forceparams[type].posres.pos0B,
2467                   comA_sc, comB_sc, lambda,
2468                   pbc, refcoord_scaling, npbcdim,
2469                   dx, rdist, dpdl);
2470
2471         for (m = 0; (m < DIM); m++)
2472         {
2473             kk          = L1*pr->posres.fcA[m] + lambda*pr->posres.fcB[m];
2474             fm          = -kk*dx[m];
2475             vtot       += 0.5*kk*dx[m]*dx[m];
2476             *dvdlambda +=
2477                 0.5*(pr->posres.fcB[m] - pr->posres.fcA[m])*dx[m]*dx[m]
2478                 + fm*dpdl[m];
2479
2480             /* Here we correct for the pbc_dx which included rdist */
2481             if (bForceValid)
2482             {
2483                 f[ai][m]    += fm;
2484                 vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm;
2485             }
2486         }
2487     }
2488
2489     return vtot;
2490 }
2491
2492 static real low_angres(int nbonds,
2493                        const t_iatom forceatoms[], const t_iparams forceparams[],
2494                        const rvec x[], rvec f[], rvec fshift[],
2495                        const t_pbc *pbc, const t_graph *g,
2496                        real lambda, real *dvdlambda,
2497                        gmx_bool bZAxis)
2498 {
2499     int  i, m, type, ai, aj, ak, al;
2500     int  t1, t2;
2501     real phi, cos_phi, cos_phi2, vid, vtot, dVdphi;
2502     rvec r_ij, r_kl, f_i, f_k = {0, 0, 0};
2503     real st, sth, nrij2, nrkl2, c, cij, ckl;
2504
2505     ivec dt;
2506     t2 = 0; /* avoid warning with gcc-3.3. It is never used uninitialized */
2507
2508     vtot = 0.0;
2509     ak   = al = 0; /* to avoid warnings */
2510     for (i = 0; i < nbonds; )
2511     {
2512         type = forceatoms[i++];
2513         ai   = forceatoms[i++];
2514         aj   = forceatoms[i++];
2515         t1   = pbc_rvec_sub(pbc, x[aj], x[ai], r_ij);       /*  3               */
2516         if (!bZAxis)
2517         {
2518             ak   = forceatoms[i++];
2519             al   = forceatoms[i++];
2520             t2   = pbc_rvec_sub(pbc, x[al], x[ak], r_kl);  /*  3                */
2521         }
2522         else
2523         {
2524             r_kl[XX] = 0;
2525             r_kl[YY] = 0;
2526             r_kl[ZZ] = 1;
2527         }
2528
2529         cos_phi = cos_angle(r_ij, r_kl); /* 25          */
2530         phi     = acos(cos_phi);         /* 10           */
2531
2532         *dvdlambda += dopdihs_min(forceparams[type].pdihs.cpA,
2533                                   forceparams[type].pdihs.cpB,
2534                                   forceparams[type].pdihs.phiA,
2535                                   forceparams[type].pdihs.phiB,
2536                                   forceparams[type].pdihs.mult,
2537                                   phi, lambda, &vid, &dVdphi); /*  40  */
2538
2539         vtot += vid;
2540
2541         cos_phi2 = sqr(cos_phi);                /*   1          */
2542         if (cos_phi2 < 1)
2543         {
2544             st    = -dVdphi*gmx_invsqrt(1 - cos_phi2); /*  12           */
2545             sth   = st*cos_phi;                        /*   1           */
2546             nrij2 = iprod(r_ij, r_ij);                 /*   5           */
2547             nrkl2 = iprod(r_kl, r_kl);                 /*   5          */
2548
2549             c   = st*gmx_invsqrt(nrij2*nrkl2);         /*  11           */
2550             cij = sth/nrij2;                           /*  10           */
2551             ckl = sth/nrkl2;                           /*  10           */
2552
2553             for (m = 0; m < DIM; m++)                  /*  18+18       */
2554             {
2555                 f_i[m]    = (c*r_kl[m]-cij*r_ij[m]);
2556                 f[ai][m] += f_i[m];
2557                 f[aj][m] -= f_i[m];
2558                 if (!bZAxis)
2559                 {
2560                     f_k[m]    = (c*r_ij[m]-ckl*r_kl[m]);
2561                     f[ak][m] += f_k[m];
2562                     f[al][m] -= f_k[m];
2563                 }
2564             }
2565
2566             if (g)
2567             {
2568                 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
2569                 t1 = IVEC2IS(dt);
2570             }
2571             rvec_inc(fshift[t1], f_i);
2572             rvec_dec(fshift[CENTRAL], f_i);
2573             if (!bZAxis)
2574             {
2575                 if (g)
2576                 {
2577                     ivec_sub(SHIFT_IVEC(g, ak), SHIFT_IVEC(g, al), dt);
2578                     t2 = IVEC2IS(dt);
2579                 }
2580                 rvec_inc(fshift[t2], f_k);
2581                 rvec_dec(fshift[CENTRAL], f_k);
2582             }
2583         }
2584     }
2585
2586     return vtot; /*  184 / 157 (bZAxis)  total  */
2587 }
2588
2589 real angres(int nbonds,
2590             const t_iatom forceatoms[], const t_iparams forceparams[],
2591             const rvec x[], rvec f[], rvec fshift[],
2592             const t_pbc *pbc, const t_graph *g,
2593             real lambda, real *dvdlambda,
2594             const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2595             int gmx_unused *global_atom_index)
2596 {
2597     return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2598                       lambda, dvdlambda, FALSE);
2599 }
2600
2601 real angresz(int nbonds,
2602              const t_iatom forceatoms[], const t_iparams forceparams[],
2603              const rvec x[], rvec f[], rvec fshift[],
2604              const t_pbc *pbc, const t_graph *g,
2605              real lambda, real *dvdlambda,
2606              const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2607              int gmx_unused *global_atom_index)
2608 {
2609     return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2610                       lambda, dvdlambda, TRUE);
2611 }
2612
2613 real dihres(int nbonds,
2614             const t_iatom forceatoms[], const t_iparams forceparams[],
2615             const rvec x[], rvec f[], rvec fshift[],
2616             const t_pbc *pbc, const t_graph *g,
2617             real lambda, real *dvdlambda,
2618             const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2619             int gmx_unused  *global_atom_index)
2620 {
2621     real vtot = 0;
2622     int  ai, aj, ak, al, i, k, type, t1, t2, t3;
2623     real phi0A, phi0B, dphiA, dphiB, kfacA, kfacB, phi0, dphi, kfac;
2624     real phi, ddphi, ddp, ddp2, dp, sign, d2r, L1;
2625     rvec r_ij, r_kj, r_kl, m, n;
2626
2627     L1 = 1.0-lambda;
2628
2629     d2r = DEG2RAD;
2630     k   = 0;
2631
2632     for (i = 0; (i < nbonds); )
2633     {
2634         type = forceatoms[i++];
2635         ai   = forceatoms[i++];
2636         aj   = forceatoms[i++];
2637         ak   = forceatoms[i++];
2638         al   = forceatoms[i++];
2639
2640         phi0A  = forceparams[type].dihres.phiA*d2r;
2641         dphiA  = forceparams[type].dihres.dphiA*d2r;
2642         kfacA  = forceparams[type].dihres.kfacA;
2643
2644         phi0B  = forceparams[type].dihres.phiB*d2r;
2645         dphiB  = forceparams[type].dihres.dphiB*d2r;
2646         kfacB  = forceparams[type].dihres.kfacB;
2647
2648         phi0  = L1*phi0A + lambda*phi0B;
2649         dphi  = L1*dphiA + lambda*dphiB;
2650         kfac  = L1*kfacA + lambda*kfacB;
2651
2652         phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2653                         &sign, &t1, &t2, &t3);
2654         /* 84 flops */
2655
2656         if (debug)
2657         {
2658             fprintf(debug, "dihres[%d]: %d %d %d %d : phi=%f, dphi=%f, kfac=%f\n",
2659                     k++, ai, aj, ak, al, phi0, dphi, kfac);
2660         }
2661         /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2662          * force changes if we just apply a normal harmonic.
2663          * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2664          * This means we will never have the periodicity problem, unless
2665          * the dihedral is Pi away from phiO, which is very unlikely due to
2666          * the potential.
2667          */
2668         dp = phi-phi0;
2669         make_dp_periodic(&dp);
2670
2671         if (dp > dphi)
2672         {
2673             ddp = dp-dphi;
2674         }
2675         else if (dp < -dphi)
2676         {
2677             ddp = dp+dphi;
2678         }
2679         else
2680         {
2681             ddp = 0;
2682         }
2683
2684         if (ddp != 0.0)
2685         {
2686             ddp2  = ddp*ddp;
2687             vtot += 0.5*kfac*ddp2;
2688             ddphi = kfac*ddp;
2689
2690             *dvdlambda += 0.5*(kfacB - kfacA)*ddp2;
2691             /* lambda dependence from changing restraint distances */
2692             if (ddp > 0)
2693             {
2694                 *dvdlambda -= kfac*ddp*((dphiB - dphiA)+(phi0B - phi0A));
2695             }
2696             else if (ddp < 0)
2697             {
2698                 *dvdlambda += kfac*ddp*((dphiB - dphiA)-(phi0B - phi0A));
2699             }
2700             do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
2701                        f, fshift, pbc, g, x, t1, t2, t3);      /* 112           */
2702         }
2703     }
2704     return vtot;
2705 }
2706
2707
2708 real unimplemented(int gmx_unused nbonds,
2709                    const t_iatom gmx_unused forceatoms[], const t_iparams gmx_unused forceparams[],
2710                    const rvec gmx_unused x[], rvec gmx_unused f[], rvec gmx_unused fshift[],
2711                    const t_pbc gmx_unused *pbc, const t_graph  gmx_unused *g,
2712                    real gmx_unused lambda, real gmx_unused *dvdlambda,
2713                    const t_mdatoms  gmx_unused *md, t_fcdata gmx_unused *fcd,
2714                    int gmx_unused *global_atom_index)
2715 {
2716     gmx_impl("*** you are using a not implemented function");
2717
2718     return 0.0; /* To make the compiler happy */
2719 }
2720
2721 real restrangles(int nbonds,
2722                  const t_iatom forceatoms[], const t_iparams forceparams[],
2723                  const rvec x[], rvec f[], rvec fshift[],
2724                  const t_pbc *pbc, const t_graph *g,
2725                  real gmx_unused lambda, real gmx_unused *dvdlambda,
2726                  const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2727                  int gmx_unused *global_atom_index)
2728 {
2729     int  i, d, ai, aj, ak, type, m;
2730     int  t1, t2;
2731     real v, vtot;
2732     ivec jt, dt_ij, dt_kj;
2733     rvec f_i, f_j, f_k;
2734     real prefactor, ratio_ante, ratio_post;
2735     rvec delta_ante, delta_post, vec_temp;
2736
2737     vtot = 0.0;
2738     for (i = 0; (i < nbonds); )
2739     {
2740         type = forceatoms[i++];
2741         ai   = forceatoms[i++];
2742         aj   = forceatoms[i++];
2743         ak   = forceatoms[i++];
2744
2745         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2746         pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2747         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], delta_post);
2748
2749
2750         /* This function computes factors needed for restricted angle potential.
2751          * The restricted angle potential is used in coarse-grained simulations to avoid singularities
2752          * when three particles align and the dihedral angle and dihedral potential
2753          * cannot be calculated. This potential is calculated using the formula:
2754            real restrangles(int nbonds,
2755             const t_iatom forceatoms[],const t_iparams forceparams[],
2756             const rvec x[],rvec f[],rvec fshift[],
2757             const t_pbc *pbc,const t_graph *g,
2758             real gmx_unused lambda,real gmx_unused *dvdlambda,
2759             const t_mdatoms gmx_unused *md,t_fcdata gmx_unused *fcd,
2760             int gmx_unused *global_atom_index)
2761            {
2762            int  i, d, ai, aj, ak, type, m;
2763            int t1, t2;
2764            rvec r_ij,r_kj;
2765            real v, vtot;
2766            ivec jt,dt_ij,dt_kj;
2767            rvec f_i, f_j, f_k;
2768            real prefactor, ratio_ante, ratio_post;
2769            rvec delta_ante, delta_post, vec_temp;
2770
2771            vtot = 0.0;
2772            for(i=0; (i<nbonds); )
2773            {
2774            type = forceatoms[i++];
2775            ai   = forceatoms[i++];
2776            aj   = forceatoms[i++];
2777            ak   = forceatoms[i++];
2778
2779          * \f[V_{\rm ReB}(\theta_i) = \frac{1}{2} k_{\theta} \frac{(\cos\theta_i - \cos\theta_0)^2}
2780          * {\sin^2\theta_i}\f] ({eq:ReB} and ref \cite{MonicaGoga2013} from the manual).
2781          * For more explanations see comments file "restcbt.h". */
2782
2783         compute_factors_restangles(type, forceparams,  delta_ante, delta_post,
2784                                    &prefactor, &ratio_ante, &ratio_post, &v);
2785
2786         /*   Forces are computed per component */
2787         for (d = 0; d < DIM; d++)
2788         {
2789             f_i[d] = prefactor * (ratio_ante * delta_ante[d] - delta_post[d]);
2790             f_j[d] = prefactor * ((ratio_post + 1.0) * delta_post[d] - (ratio_ante + 1.0) * delta_ante[d]);
2791             f_k[d] = prefactor * (delta_ante[d] - ratio_post * delta_post[d]);
2792         }
2793
2794         /*   Computation of potential energy   */
2795
2796         vtot += v;
2797
2798         /*   Update forces */
2799
2800         for (m = 0; (m < DIM); m++)
2801         {
2802             f[ai][m] += f_i[m];
2803             f[aj][m] += f_j[m];
2804             f[ak][m] += f_k[m];
2805         }
2806
2807         if (g)
2808         {
2809             copy_ivec(SHIFT_IVEC(g, aj), jt);
2810             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
2811             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
2812             t1 = IVEC2IS(dt_ij);
2813             t2 = IVEC2IS(dt_kj);
2814         }
2815
2816         rvec_inc(fshift[t1], f_i);
2817         rvec_inc(fshift[CENTRAL], f_j);
2818         rvec_inc(fshift[t2], f_k);
2819     }
2820     return vtot;
2821 }
2822
2823
2824 real restrdihs(int nbonds,
2825                const t_iatom forceatoms[], const t_iparams forceparams[],
2826                const rvec x[], rvec f[], rvec fshift[],
2827                const t_pbc *pbc, const t_graph *g,
2828                real gmx_unused lambda, real gmx_unused *dvlambda,
2829                const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2830                int gmx_unused *global_atom_index)
2831 {
2832     int  i, d, type, ai, aj, ak, al;
2833     rvec f_i, f_j, f_k, f_l;
2834     rvec dx_jl;
2835     ivec jt, dt_ij, dt_kj, dt_lj;
2836     int  t1, t2, t3;
2837     real v, vtot;
2838     rvec delta_ante,  delta_crnt, delta_post, vec_temp;
2839     real factor_phi_ai_ante, factor_phi_ai_crnt, factor_phi_ai_post;
2840     real factor_phi_aj_ante, factor_phi_aj_crnt, factor_phi_aj_post;
2841     real factor_phi_ak_ante, factor_phi_ak_crnt, factor_phi_ak_post;
2842     real factor_phi_al_ante, factor_phi_al_crnt, factor_phi_al_post;
2843     real prefactor_phi;
2844
2845
2846     vtot = 0.0;
2847     for (i = 0; (i < nbonds); )
2848     {
2849         type = forceatoms[i++];
2850         ai   = forceatoms[i++];
2851         aj   = forceatoms[i++];
2852         ak   = forceatoms[i++];
2853         al   = forceatoms[i++];
2854
2855         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2856         pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2857         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
2858         pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
2859         pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
2860
2861         /* This function computes factors needed for restricted angle potential.
2862          * The restricted angle potential is used in coarse-grained simulations to avoid singularities
2863          * when three particles align and the dihedral angle and dihedral potential cannot be calculated.
2864          * This potential is calculated using the formula:
2865          * \f[V_{\rm ReB}(\theta_i) = \frac{1}{2} k_{\theta}
2866          * \frac{(\cos\theta_i - \cos\theta_0)^2}{\sin^2\theta_i}\f]
2867          * ({eq:ReB} and ref \cite{MonicaGoga2013} from the manual).
2868          * For more explanations see comments file "restcbt.h" */
2869
2870         compute_factors_restrdihs(type, forceparams,
2871                                   delta_ante, delta_crnt, delta_post,
2872                                   &factor_phi_ai_ante, &factor_phi_ai_crnt, &factor_phi_ai_post,
2873                                   &factor_phi_aj_ante, &factor_phi_aj_crnt, &factor_phi_aj_post,
2874                                   &factor_phi_ak_ante, &factor_phi_ak_crnt, &factor_phi_ak_post,
2875                                   &factor_phi_al_ante, &factor_phi_al_crnt, &factor_phi_al_post,
2876                                   &prefactor_phi, &v);
2877
2878
2879         /*      Computation of forces per component */
2880         for (d = 0; d < DIM; d++)
2881         {
2882             f_i[d] = prefactor_phi * (factor_phi_ai_ante * delta_ante[d] + factor_phi_ai_crnt * delta_crnt[d] + factor_phi_ai_post * delta_post[d]);
2883             f_j[d] = prefactor_phi * (factor_phi_aj_ante * delta_ante[d] + factor_phi_aj_crnt * delta_crnt[d] + factor_phi_aj_post * delta_post[d]);
2884             f_k[d] = prefactor_phi * (factor_phi_ak_ante * delta_ante[d] + factor_phi_ak_crnt * delta_crnt[d] + factor_phi_ak_post * delta_post[d]);
2885             f_l[d] = prefactor_phi * (factor_phi_al_ante * delta_ante[d] + factor_phi_al_crnt * delta_crnt[d] + factor_phi_al_post * delta_post[d]);
2886         }
2887         /*      Computation of the energy */
2888
2889         vtot += v;
2890
2891
2892
2893         /*    Updating the forces */
2894
2895         rvec_inc(f[ai], f_i);
2896         rvec_inc(f[aj], f_j);
2897         rvec_inc(f[ak], f_k);
2898         rvec_inc(f[al], f_l);
2899
2900
2901         /* Updating the fshift forces for the pressure coupling */
2902         if (g)
2903         {
2904             copy_ivec(SHIFT_IVEC(g, aj), jt);
2905             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
2906             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
2907             ivec_sub(SHIFT_IVEC(g, al), jt, dt_lj);
2908             t1 = IVEC2IS(dt_ij);
2909             t2 = IVEC2IS(dt_kj);
2910             t3 = IVEC2IS(dt_lj);
2911         }
2912         else if (pbc)
2913         {
2914             t3 = pbc_rvec_sub(pbc, x[al], x[aj], dx_jl);
2915         }
2916         else
2917         {
2918             t3 = CENTRAL;
2919         }
2920
2921         rvec_inc(fshift[t1], f_i);
2922         rvec_inc(fshift[CENTRAL], f_j);
2923         rvec_inc(fshift[t2], f_k);
2924         rvec_inc(fshift[t3], f_l);
2925
2926     }
2927
2928     return vtot;
2929 }
2930
2931
2932 real cbtdihs(int nbonds,
2933              const t_iatom forceatoms[], const t_iparams forceparams[],
2934              const rvec x[], rvec f[], rvec fshift[],
2935              const t_pbc *pbc, const t_graph *g,
2936              real gmx_unused lambda, real gmx_unused *dvdlambda,
2937              const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2938              int gmx_unused *global_atom_index)
2939 {
2940     int  type, ai, aj, ak, al, i, d;
2941     int  t1, t2, t3;
2942     real v, vtot;
2943     rvec vec_temp;
2944     rvec f_i, f_j, f_k, f_l;
2945     ivec jt, dt_ij, dt_kj, dt_lj;
2946     rvec dx_jl;
2947     rvec delta_ante, delta_crnt, delta_post;
2948     rvec f_phi_ai, f_phi_aj, f_phi_ak, f_phi_al;
2949     rvec f_theta_ante_ai, f_theta_ante_aj, f_theta_ante_ak;
2950     rvec f_theta_post_aj, f_theta_post_ak, f_theta_post_al;
2951
2952
2953
2954
2955     vtot = 0.0;
2956     for (i = 0; (i < nbonds); )
2957     {
2958         type = forceatoms[i++];
2959         ai   = forceatoms[i++];
2960         aj   = forceatoms[i++];
2961         ak   = forceatoms[i++];
2962         al   = forceatoms[i++];
2963
2964
2965         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2966         pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2967         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], vec_temp);
2968         pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
2969         pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
2970         pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
2971
2972         /* \brief Compute factors for CBT potential
2973          * The combined bending-torsion potential goes to zero in a very smooth manner, eliminating the numerical
2974          * instabilities, when three coarse-grained particles align and the dihedral angle and standard
2975          * dihedral potentials cannot be calculated. The CBT potential is calculated using the formula:
2976          * \f[V_{\rm CBT}(\theta_{i-1}, \theta_i, \phi_i) = k_{\phi} \sin^3\theta_{i-1} \sin^3\theta_{i}
2977          * \sum_{n=0}^4 { a_n \cos^n\phi_i}\f] ({eq:CBT} and ref \cite{MonicaGoga2013} from the manual).
2978          * It contains in its expression not only the dihedral angle \f$\phi\f$
2979          * but also \f[\theta_{i-1}\f] (theta_ante bellow) and \f[\theta_{i}\f] (theta_post bellow)
2980          * --- the adjacent bending angles.
2981          * For more explanations see comments file "restcbt.h". */
2982
2983         compute_factors_cbtdihs(type, forceparams, delta_ante, delta_crnt, delta_post,
2984                                 f_phi_ai, f_phi_aj, f_phi_ak, f_phi_al,
2985                                 f_theta_ante_ai, f_theta_ante_aj, f_theta_ante_ak,
2986                                 f_theta_post_aj, f_theta_post_ak, f_theta_post_al,
2987                                 &v);
2988
2989
2990         /*      Acumulate the resuts per beads */
2991         for (d = 0; d < DIM; d++)
2992         {
2993             f_i[d] = f_phi_ai[d] + f_theta_ante_ai[d];
2994             f_j[d] = f_phi_aj[d] + f_theta_ante_aj[d] + f_theta_post_aj[d];
2995             f_k[d] = f_phi_ak[d] + f_theta_ante_ak[d] + f_theta_post_ak[d];
2996             f_l[d] = f_phi_al[d] + f_theta_post_al[d];
2997         }
2998
2999         /*      Compute the potential energy */
3000
3001         vtot += v;
3002
3003
3004         /*  Updating the forces */
3005         rvec_inc(f[ai], f_i);
3006         rvec_inc(f[aj], f_j);
3007         rvec_inc(f[ak], f_k);
3008         rvec_inc(f[al], f_l);
3009
3010
3011         /* Updating the fshift forces for the pressure coupling */
3012         if (g)
3013         {
3014             copy_ivec(SHIFT_IVEC(g, aj), jt);
3015             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3016             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3017             ivec_sub(SHIFT_IVEC(g, al), jt, dt_lj);
3018             t1 = IVEC2IS(dt_ij);
3019             t2 = IVEC2IS(dt_kj);
3020             t3 = IVEC2IS(dt_lj);
3021         }
3022         else if (pbc)
3023         {
3024             t3 = pbc_rvec_sub(pbc, x[al], x[aj], dx_jl);
3025         }
3026         else
3027         {
3028             t3 = CENTRAL;
3029         }
3030
3031         rvec_inc(fshift[t1], f_i);
3032         rvec_inc(fshift[CENTRAL], f_j);
3033         rvec_inc(fshift[t2], f_k);
3034         rvec_inc(fshift[t3], f_l);
3035     }
3036
3037     return vtot;
3038 }
3039
3040 real rbdihs(int nbonds,
3041             const t_iatom forceatoms[], const t_iparams forceparams[],
3042             const rvec x[], rvec f[], rvec fshift[],
3043             const t_pbc *pbc, const t_graph *g,
3044             real lambda, real *dvdlambda,
3045             const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3046             int gmx_unused *global_atom_index)
3047 {
3048     const real c0 = 0.0, c1 = 1.0, c2 = 2.0, c3 = 3.0, c4 = 4.0, c5 = 5.0;
3049     int        type, ai, aj, ak, al, i, j;
3050     int        t1, t2, t3;
3051     rvec       r_ij, r_kj, r_kl, m, n;
3052     real       parmA[NR_RBDIHS];
3053     real       parmB[NR_RBDIHS];
3054     real       parm[NR_RBDIHS];
3055     real       cos_phi, phi, rbp, rbpBA;
3056     real       v, sign, ddphi, sin_phi;
3057     real       cosfac, vtot;
3058     real       L1        = 1.0-lambda;
3059     real       dvdl_term = 0;
3060
3061     vtot = 0.0;
3062     for (i = 0; (i < nbonds); )
3063     {
3064         type = forceatoms[i++];
3065         ai   = forceatoms[i++];
3066         aj   = forceatoms[i++];
3067         ak   = forceatoms[i++];
3068         al   = forceatoms[i++];
3069
3070         phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
3071                         &sign, &t1, &t2, &t3);  /*  84          */
3072
3073         /* Change to polymer convention */
3074         if (phi < c0)
3075         {
3076             phi += M_PI;
3077         }
3078         else
3079         {
3080             phi -= M_PI;    /*   1              */
3081
3082         }
3083         cos_phi = cos(phi);
3084         /* Beware of accuracy loss, cannot use 1-sqrt(cos^2) ! */
3085         sin_phi = sin(phi);
3086
3087         for (j = 0; (j < NR_RBDIHS); j++)
3088         {
3089             parmA[j] = forceparams[type].rbdihs.rbcA[j];
3090             parmB[j] = forceparams[type].rbdihs.rbcB[j];
3091             parm[j]  = L1*parmA[j]+lambda*parmB[j];
3092         }
3093         /* Calculate cosine powers */
3094         /* Calculate the energy */
3095         /* Calculate the derivative */
3096
3097         v            = parm[0];
3098         dvdl_term   += (parmB[0]-parmA[0]);
3099         ddphi        = c0;
3100         cosfac       = c1;
3101
3102         rbp          = parm[1];
3103         rbpBA        = parmB[1]-parmA[1];
3104         ddphi       += rbp*cosfac;
3105         cosfac      *= cos_phi;
3106         v           += cosfac*rbp;
3107         dvdl_term   += cosfac*rbpBA;
3108         rbp          = parm[2];
3109         rbpBA        = parmB[2]-parmA[2];
3110         ddphi       += c2*rbp*cosfac;
3111         cosfac      *= cos_phi;
3112         v           += cosfac*rbp;
3113         dvdl_term   += cosfac*rbpBA;
3114         rbp          = parm[3];
3115         rbpBA        = parmB[3]-parmA[3];
3116         ddphi       += c3*rbp*cosfac;
3117         cosfac      *= cos_phi;
3118         v           += cosfac*rbp;
3119         dvdl_term   += cosfac*rbpBA;
3120         rbp          = parm[4];
3121         rbpBA        = parmB[4]-parmA[4];
3122         ddphi       += c4*rbp*cosfac;
3123         cosfac      *= cos_phi;
3124         v           += cosfac*rbp;
3125         dvdl_term   += cosfac*rbpBA;
3126         rbp          = parm[5];
3127         rbpBA        = parmB[5]-parmA[5];
3128         ddphi       += c5*rbp*cosfac;
3129         cosfac      *= cos_phi;
3130         v           += cosfac*rbp;
3131         dvdl_term   += cosfac*rbpBA;
3132
3133         ddphi = -ddphi*sin_phi;         /*  11          */
3134
3135         do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
3136                    f, fshift, pbc, g, x, t1, t2, t3); /* 112            */
3137         vtot += v;
3138     }
3139     *dvdlambda += dvdl_term;
3140
3141     return vtot;
3142 }
3143
3144 int cmap_setup_grid_index(int ip, int grid_spacing, int *ipm1, int *ipp1, int *ipp2)
3145 {
3146     int im1, ip1, ip2;
3147
3148     if (ip < 0)
3149     {
3150         ip = ip + grid_spacing - 1;
3151     }
3152     else if (ip > grid_spacing)
3153     {
3154         ip = ip - grid_spacing - 1;
3155     }
3156
3157     im1 = ip - 1;
3158     ip1 = ip + 1;
3159     ip2 = ip + 2;
3160
3161     if (ip == 0)
3162     {
3163         im1 = grid_spacing - 1;
3164     }
3165     else if (ip == grid_spacing-2)
3166     {
3167         ip2 = 0;
3168     }
3169     else if (ip == grid_spacing-1)
3170     {
3171         ip1 = 0;
3172         ip2 = 1;
3173     }
3174
3175     *ipm1 = im1;
3176     *ipp1 = ip1;
3177     *ipp2 = ip2;
3178
3179     return ip;
3180
3181 }
3182
3183 real cmap_dihs(int nbonds,
3184                const t_iatom forceatoms[], const t_iparams forceparams[],
3185                const gmx_cmap_t *cmap_grid,
3186                const rvec x[], rvec f[], rvec fshift[],
3187                const t_pbc *pbc, const t_graph *g,
3188                real gmx_unused lambda, real gmx_unused *dvdlambda,
3189                const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3190                int  gmx_unused *global_atom_index)
3191 {
3192     int         i, j, k, n, idx;
3193     int         ai, aj, ak, al, am;
3194     int         a1i, a1j, a1k, a1l, a2i, a2j, a2k, a2l;
3195     int         type, cmapA;
3196     int         t11, t21, t31, t12, t22, t32;
3197     int         iphi1, ip1m1, ip1p1, ip1p2;
3198     int         iphi2, ip2m1, ip2p1, ip2p2;
3199     int         l1, l2, l3;
3200     int         pos1, pos2, pos3, pos4;
3201
3202     real        ty[4], ty1[4], ty2[4], ty12[4], tc[16], tx[16];
3203     real        phi1, cos_phi1, sin_phi1, sign1, xphi1;
3204     real        phi2, cos_phi2, sin_phi2, sign2, xphi2;
3205     real        dx, xx, tt, tu, e, df1, df2, vtot;
3206     real        ra21, rb21, rg21, rg1, rgr1, ra2r1, rb2r1, rabr1;
3207     real        ra22, rb22, rg22, rg2, rgr2, ra2r2, rb2r2, rabr2;
3208     real        fg1, hg1, fga1, hgb1, gaa1, gbb1;
3209     real        fg2, hg2, fga2, hgb2, gaa2, gbb2;
3210     real        fac;
3211
3212     rvec        r1_ij, r1_kj, r1_kl, m1, n1;
3213     rvec        r2_ij, r2_kj, r2_kl, m2, n2;
3214     rvec        f1_i, f1_j, f1_k, f1_l;
3215     rvec        f2_i, f2_j, f2_k, f2_l;
3216     rvec        a1, b1, a2, b2;
3217     rvec        f1, g1, h1, f2, g2, h2;
3218     rvec        dtf1, dtg1, dth1, dtf2, dtg2, dth2;
3219     ivec        jt1, dt1_ij, dt1_kj, dt1_lj;
3220     ivec        jt2, dt2_ij, dt2_kj, dt2_lj;
3221
3222     const real *cmapd;
3223
3224     int         loop_index[4][4] = {
3225         {0, 4, 8, 12},
3226         {1, 5, 9, 13},
3227         {2, 6, 10, 14},
3228         {3, 7, 11, 15}
3229     };
3230
3231     /* Total CMAP energy */
3232     vtot = 0;
3233
3234     for (n = 0; n < nbonds; )
3235     {
3236         /* Five atoms are involved in the two torsions */
3237         type   = forceatoms[n++];
3238         ai     = forceatoms[n++];
3239         aj     = forceatoms[n++];
3240         ak     = forceatoms[n++];
3241         al     = forceatoms[n++];
3242         am     = forceatoms[n++];
3243
3244         /* Which CMAP type is this */
3245         cmapA = forceparams[type].cmap.cmapA;
3246         cmapd = cmap_grid->cmapdata[cmapA].cmap;
3247
3248         /* First torsion */
3249         a1i   = ai;
3250         a1j   = aj;
3251         a1k   = ak;
3252         a1l   = al;
3253
3254         phi1  = dih_angle(x[a1i], x[a1j], x[a1k], x[a1l], pbc, r1_ij, r1_kj, r1_kl, m1, n1,
3255                           &sign1, &t11, &t21, &t31);  /* 84 */
3256
3257         cos_phi1 = cos(phi1);
3258
3259         a1[0] = r1_ij[1]*r1_kj[2]-r1_ij[2]*r1_kj[1];
3260         a1[1] = r1_ij[2]*r1_kj[0]-r1_ij[0]*r1_kj[2];
3261         a1[2] = r1_ij[0]*r1_kj[1]-r1_ij[1]*r1_kj[0]; /* 9 */
3262
3263         b1[0] = r1_kl[1]*r1_kj[2]-r1_kl[2]*r1_kj[1];
3264         b1[1] = r1_kl[2]*r1_kj[0]-r1_kl[0]*r1_kj[2];
3265         b1[2] = r1_kl[0]*r1_kj[1]-r1_kl[1]*r1_kj[0]; /* 9 */
3266
3267         pbc_rvec_sub(pbc, x[a1l], x[a1k], h1);
3268
3269         ra21  = iprod(a1, a1);       /* 5 */
3270         rb21  = iprod(b1, b1);       /* 5 */
3271         rg21  = iprod(r1_kj, r1_kj); /* 5 */
3272         rg1   = sqrt(rg21);
3273
3274         rgr1  = 1.0/rg1;
3275         ra2r1 = 1.0/ra21;
3276         rb2r1 = 1.0/rb21;
3277         rabr1 = sqrt(ra2r1*rb2r1);
3278
3279         sin_phi1 = rg1 * rabr1 * iprod(a1, h1) * (-1);
3280
3281         if (cos_phi1 < -0.5 || cos_phi1 > 0.5)
3282         {
3283             phi1 = asin(sin_phi1);
3284
3285             if (cos_phi1 < 0)
3286             {
3287                 if (phi1 > 0)
3288                 {
3289                     phi1 = M_PI - phi1;
3290                 }
3291                 else
3292                 {
3293                     phi1 = -M_PI - phi1;
3294                 }
3295             }
3296         }
3297         else
3298         {
3299             phi1 = acos(cos_phi1);
3300
3301             if (sin_phi1 < 0)
3302             {
3303                 phi1 = -phi1;
3304             }
3305         }
3306
3307         xphi1 = phi1 + M_PI; /* 1 */
3308
3309         /* Second torsion */
3310         a2i   = aj;
3311         a2j   = ak;
3312         a2k   = al;
3313         a2l   = am;
3314
3315         phi2  = dih_angle(x[a2i], x[a2j], x[a2k], x[a2l], pbc, r2_ij, r2_kj, r2_kl, m2, n2,
3316                           &sign2, &t12, &t22, &t32); /* 84 */
3317
3318         cos_phi2 = cos(phi2);
3319
3320         a2[0] = r2_ij[1]*r2_kj[2]-r2_ij[2]*r2_kj[1];
3321         a2[1] = r2_ij[2]*r2_kj[0]-r2_ij[0]*r2_kj[2];
3322         a2[2] = r2_ij[0]*r2_kj[1]-r2_ij[1]*r2_kj[0]; /* 9 */
3323
3324         b2[0] = r2_kl[1]*r2_kj[2]-r2_kl[2]*r2_kj[1];
3325         b2[1] = r2_kl[2]*r2_kj[0]-r2_kl[0]*r2_kj[2];
3326         b2[2] = r2_kl[0]*r2_kj[1]-r2_kl[1]*r2_kj[0]; /* 9 */
3327
3328         pbc_rvec_sub(pbc, x[a2l], x[a2k], h2);
3329
3330         ra22  = iprod(a2, a2);         /* 5 */
3331         rb22  = iprod(b2, b2);         /* 5 */
3332         rg22  = iprod(r2_kj, r2_kj);   /* 5 */
3333         rg2   = sqrt(rg22);
3334
3335         rgr2  = 1.0/rg2;
3336         ra2r2 = 1.0/ra22;
3337         rb2r2 = 1.0/rb22;
3338         rabr2 = sqrt(ra2r2*rb2r2);
3339
3340         sin_phi2 = rg2 * rabr2 * iprod(a2, h2) * (-1);
3341
3342         if (cos_phi2 < -0.5 || cos_phi2 > 0.5)
3343         {
3344             phi2 = asin(sin_phi2);
3345
3346             if (cos_phi2 < 0)
3347             {
3348                 if (phi2 > 0)
3349                 {
3350                     phi2 = M_PI - phi2;
3351                 }
3352                 else
3353                 {
3354                     phi2 = -M_PI - phi2;
3355                 }
3356             }
3357         }
3358         else
3359         {
3360             phi2 = acos(cos_phi2);
3361
3362             if (sin_phi2 < 0)
3363             {
3364                 phi2 = -phi2;
3365             }
3366         }
3367
3368         xphi2 = phi2 + M_PI; /* 1 */
3369
3370         /* Range mangling */
3371         if (xphi1 < 0)
3372         {
3373             xphi1 = xphi1 + 2*M_PI;
3374         }
3375         else if (xphi1 >= 2*M_PI)
3376         {
3377             xphi1 = xphi1 - 2*M_PI;
3378         }
3379
3380         if (xphi2 < 0)
3381         {
3382             xphi2 = xphi2 + 2*M_PI;
3383         }
3384         else if (xphi2 >= 2*M_PI)
3385         {
3386             xphi2 = xphi2 - 2*M_PI;
3387         }
3388
3389         /* Number of grid points */
3390         dx = 2*M_PI / cmap_grid->grid_spacing;
3391
3392         /* Where on the grid are we */
3393         iphi1 = static_cast<int>(xphi1/dx);
3394         iphi2 = static_cast<int>(xphi2/dx);
3395
3396         iphi1 = cmap_setup_grid_index(iphi1, cmap_grid->grid_spacing, &ip1m1, &ip1p1, &ip1p2);
3397         iphi2 = cmap_setup_grid_index(iphi2, cmap_grid->grid_spacing, &ip2m1, &ip2p1, &ip2p2);
3398
3399         pos1    = iphi1*cmap_grid->grid_spacing+iphi2;
3400         pos2    = ip1p1*cmap_grid->grid_spacing+iphi2;
3401         pos3    = ip1p1*cmap_grid->grid_spacing+ip2p1;
3402         pos4    = iphi1*cmap_grid->grid_spacing+ip2p1;
3403
3404         ty[0]   = cmapd[pos1*4];
3405         ty[1]   = cmapd[pos2*4];
3406         ty[2]   = cmapd[pos3*4];
3407         ty[3]   = cmapd[pos4*4];
3408
3409         ty1[0]   = cmapd[pos1*4+1];
3410         ty1[1]   = cmapd[pos2*4+1];
3411         ty1[2]   = cmapd[pos3*4+1];
3412         ty1[3]   = cmapd[pos4*4+1];
3413
3414         ty2[0]   = cmapd[pos1*4+2];
3415         ty2[1]   = cmapd[pos2*4+2];
3416         ty2[2]   = cmapd[pos3*4+2];
3417         ty2[3]   = cmapd[pos4*4+2];
3418
3419         ty12[0]   = cmapd[pos1*4+3];
3420         ty12[1]   = cmapd[pos2*4+3];
3421         ty12[2]   = cmapd[pos3*4+3];
3422         ty12[3]   = cmapd[pos4*4+3];
3423
3424         /* Switch to degrees */
3425         dx    = 360.0 / cmap_grid->grid_spacing;
3426         xphi1 = xphi1 * RAD2DEG;
3427         xphi2 = xphi2 * RAD2DEG;
3428
3429         for (i = 0; i < 4; i++) /* 16 */
3430         {
3431             tx[i]    = ty[i];
3432             tx[i+4]  = ty1[i]*dx;
3433             tx[i+8]  = ty2[i]*dx;
3434             tx[i+12] = ty12[i]*dx*dx;
3435         }
3436
3437         idx = 0;
3438         for (i = 0; i < 4; i++) /* 1056 */
3439         {
3440             for (j = 0; j < 4; j++)
3441             {
3442                 xx = 0;
3443                 for (k = 0; k < 16; k++)
3444                 {
3445                     xx = xx + cmap_coeff_matrix[k*16+idx]*tx[k];
3446                 }
3447
3448                 idx++;
3449                 tc[i*4+j] = xx;
3450             }
3451         }
3452
3453         tt    = (xphi1-iphi1*dx)/dx;
3454         tu    = (xphi2-iphi2*dx)/dx;
3455
3456         e     = 0;
3457         df1   = 0;
3458         df2   = 0;
3459
3460         for (i = 3; i >= 0; i--)
3461         {
3462             l1 = loop_index[i][3];
3463             l2 = loop_index[i][2];
3464             l3 = loop_index[i][1];
3465
3466             e     = tt * e    + ((tc[i*4+3]*tu+tc[i*4+2])*tu + tc[i*4+1])*tu+tc[i*4];
3467             df1   = tu * df1  + (3.0*tc[l1]*tt+2.0*tc[l2])*tt+tc[l3];
3468             df2   = tt * df2  + (3.0*tc[i*4+3]*tu+2.0*tc[i*4+2])*tu+tc[i*4+1];
3469         }
3470
3471         fac     = RAD2DEG/dx;
3472         df1     = df1   * fac;
3473         df2     = df2   * fac;
3474
3475         /* CMAP energy */
3476         vtot += e;
3477
3478         /* Do forces - first torsion */
3479         fg1       = iprod(r1_ij, r1_kj);
3480         hg1       = iprod(r1_kl, r1_kj);
3481         fga1      = fg1*ra2r1*rgr1;
3482         hgb1      = hg1*rb2r1*rgr1;
3483         gaa1      = -ra2r1*rg1;
3484         gbb1      = rb2r1*rg1;
3485
3486         for (i = 0; i < DIM; i++)
3487         {
3488             dtf1[i]   = gaa1 * a1[i];
3489             dtg1[i]   = fga1 * a1[i] - hgb1 * b1[i];
3490             dth1[i]   = gbb1 * b1[i];
3491
3492             f1[i]     = df1  * dtf1[i];
3493             g1[i]     = df1  * dtg1[i];
3494             h1[i]     = df1  * dth1[i];
3495
3496             f1_i[i]   =  f1[i];
3497             f1_j[i]   = -f1[i] - g1[i];
3498             f1_k[i]   =  h1[i] + g1[i];
3499             f1_l[i]   = -h1[i];
3500
3501             f[a1i][i] = f[a1i][i] + f1_i[i];
3502             f[a1j][i] = f[a1j][i] + f1_j[i]; /* - f1[i] - g1[i] */
3503             f[a1k][i] = f[a1k][i] + f1_k[i]; /* h1[i] + g1[i] */
3504             f[a1l][i] = f[a1l][i] + f1_l[i]; /* h1[i] */
3505         }
3506
3507         /* Do forces - second torsion */
3508         fg2       = iprod(r2_ij, r2_kj);
3509         hg2       = iprod(r2_kl, r2_kj);
3510         fga2      = fg2*ra2r2*rgr2;
3511         hgb2      = hg2*rb2r2*rgr2;
3512         gaa2      = -ra2r2*rg2;
3513         gbb2      = rb2r2*rg2;
3514
3515         for (i = 0; i < DIM; i++)
3516         {
3517             dtf2[i]   = gaa2 * a2[i];
3518             dtg2[i]   = fga2 * a2[i] - hgb2 * b2[i];
3519             dth2[i]   = gbb2 * b2[i];
3520
3521             f2[i]     = df2  * dtf2[i];
3522             g2[i]     = df2  * dtg2[i];
3523             h2[i]     = df2  * dth2[i];
3524
3525             f2_i[i]   =  f2[i];
3526             f2_j[i]   = -f2[i] - g2[i];
3527             f2_k[i]   =  h2[i] + g2[i];
3528             f2_l[i]   = -h2[i];
3529
3530             f[a2i][i] = f[a2i][i] + f2_i[i]; /* f2[i] */
3531             f[a2j][i] = f[a2j][i] + f2_j[i]; /* - f2[i] - g2[i] */
3532             f[a2k][i] = f[a2k][i] + f2_k[i]; /* h2[i] + g2[i] */
3533             f[a2l][i] = f[a2l][i] + f2_l[i]; /* - h2[i] */
3534         }
3535
3536         /* Shift forces */
3537         if (g)
3538         {
3539             copy_ivec(SHIFT_IVEC(g, a1j), jt1);
3540             ivec_sub(SHIFT_IVEC(g, a1i),  jt1, dt1_ij);
3541             ivec_sub(SHIFT_IVEC(g, a1k),  jt1, dt1_kj);
3542             ivec_sub(SHIFT_IVEC(g, a1l),  jt1, dt1_lj);
3543             t11 = IVEC2IS(dt1_ij);
3544             t21 = IVEC2IS(dt1_kj);
3545             t31 = IVEC2IS(dt1_lj);
3546
3547             copy_ivec(SHIFT_IVEC(g, a2j), jt2);
3548             ivec_sub(SHIFT_IVEC(g, a2i),  jt2, dt2_ij);
3549             ivec_sub(SHIFT_IVEC(g, a2k),  jt2, dt2_kj);
3550             ivec_sub(SHIFT_IVEC(g, a2l),  jt2, dt2_lj);
3551             t12 = IVEC2IS(dt2_ij);
3552             t22 = IVEC2IS(dt2_kj);
3553             t32 = IVEC2IS(dt2_lj);
3554         }
3555         else if (pbc)
3556         {
3557             t31 = pbc_rvec_sub(pbc, x[a1l], x[a1j], h1);
3558             t32 = pbc_rvec_sub(pbc, x[a2l], x[a2j], h2);
3559         }
3560         else
3561         {
3562             t31 = CENTRAL;
3563             t32 = CENTRAL;
3564         }
3565
3566         rvec_inc(fshift[t11], f1_i);
3567         rvec_inc(fshift[CENTRAL], f1_j);
3568         rvec_inc(fshift[t21], f1_k);
3569         rvec_inc(fshift[t31], f1_l);
3570
3571         rvec_inc(fshift[t21], f2_i);
3572         rvec_inc(fshift[CENTRAL], f2_j);
3573         rvec_inc(fshift[t22], f2_k);
3574         rvec_inc(fshift[t32], f2_l);
3575     }
3576     return vtot;
3577 }
3578
3579
3580
3581 /***********************************************************
3582  *
3583  *   G R O M O S  9 6   F U N C T I O N S
3584  *
3585  ***********************************************************/
3586 real g96harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
3587                  real *V, real *F)
3588 {
3589     const real half = 0.5;
3590     real       L1, kk, x0, dx, dx2;
3591     real       v, f, dvdlambda;
3592
3593     L1    = 1.0-lambda;
3594     kk    = L1*kA+lambda*kB;
3595     x0    = L1*xA+lambda*xB;
3596
3597     dx    = x-x0;
3598     dx2   = dx*dx;
3599
3600     f          = -kk*dx;
3601     v          = half*kk*dx2;
3602     dvdlambda  = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
3603
3604     *F    = f;
3605     *V    = v;
3606
3607     return dvdlambda;
3608
3609     /* That was 21 flops */
3610 }
3611
3612 real g96bonds(int nbonds,
3613               const t_iatom forceatoms[], const t_iparams forceparams[],
3614               const rvec x[], rvec f[], rvec fshift[],
3615               const t_pbc *pbc, const t_graph *g,
3616               real lambda, real *dvdlambda,
3617               const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3618               int gmx_unused *global_atom_index)
3619 {
3620     int  i, m, ki, ai, aj, type;
3621     real dr2, fbond, vbond, fij, vtot;
3622     rvec dx;
3623     ivec dt;
3624
3625     vtot = 0.0;
3626     for (i = 0; (i < nbonds); )
3627     {
3628         type = forceatoms[i++];
3629         ai   = forceatoms[i++];
3630         aj   = forceatoms[i++];
3631
3632         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /*   3      */
3633         dr2  = iprod(dx, dx);                       /*   5              */
3634
3635         *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3636                                   forceparams[type].harmonic.krB,
3637                                   forceparams[type].harmonic.rA,
3638                                   forceparams[type].harmonic.rB,
3639                                   dr2, lambda, &vbond, &fbond);
3640
3641         vtot  += 0.5*vbond;                         /* 1*/
3642 #ifdef DEBUG
3643         if (debug)
3644         {
3645             fprintf(debug, "G96-BONDS: dr = %10g  vbond = %10g  fbond = %10g\n",
3646                     sqrt(dr2), vbond, fbond);
3647         }
3648 #endif
3649
3650         if (g)
3651         {
3652             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3653             ki = IVEC2IS(dt);
3654         }
3655         for (m = 0; (m < DIM); m++)     /*  15          */
3656         {
3657             fij                 = fbond*dx[m];
3658             f[ai][m]           += fij;
3659             f[aj][m]           -= fij;
3660             fshift[ki][m]      += fij;
3661             fshift[CENTRAL][m] -= fij;
3662         }
3663     }               /* 44 TOTAL */
3664     return vtot;
3665 }
3666
3667 real g96bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
3668                    rvec r_ij, rvec r_kj,
3669                    int *t1, int *t2)
3670 /* Return value is the angle between the bonds i-j and j-k */
3671 {
3672     real costh;
3673
3674     *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /*  3                */
3675     *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /*  3                */
3676
3677     costh = cos_angle(r_ij, r_kj);         /* 25                */
3678     /* 41 TOTAL */
3679     return costh;
3680 }
3681
3682 real g96angles(int nbonds,
3683                const t_iatom forceatoms[], const t_iparams forceparams[],
3684                const rvec x[], rvec f[], rvec fshift[],
3685                const t_pbc *pbc, const t_graph *g,
3686                real lambda, real *dvdlambda,
3687                const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3688                int gmx_unused *global_atom_index)
3689 {
3690     int  i, ai, aj, ak, type, m, t1, t2;
3691     rvec r_ij, r_kj;
3692     real cos_theta, dVdt, va, vtot;
3693     real rij_1, rij_2, rkj_1, rkj_2, rijrkj_1;
3694     rvec f_i, f_j, f_k;
3695     ivec jt, dt_ij, dt_kj;
3696
3697     vtot = 0.0;
3698     for (i = 0; (i < nbonds); )
3699     {
3700         type = forceatoms[i++];
3701         ai   = forceatoms[i++];
3702         aj   = forceatoms[i++];
3703         ak   = forceatoms[i++];
3704
3705         cos_theta  = g96bond_angle(x[ai], x[aj], x[ak], pbc, r_ij, r_kj, &t1, &t2);
3706
3707         *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3708                                   forceparams[type].harmonic.krB,
3709                                   forceparams[type].harmonic.rA,
3710                                   forceparams[type].harmonic.rB,
3711                                   cos_theta, lambda, &va, &dVdt);
3712         vtot    += va;
3713
3714         rij_1    = gmx_invsqrt(iprod(r_ij, r_ij));
3715         rkj_1    = gmx_invsqrt(iprod(r_kj, r_kj));
3716         rij_2    = rij_1*rij_1;
3717         rkj_2    = rkj_1*rkj_1;
3718         rijrkj_1 = rij_1*rkj_1;                 /* 23 */
3719
3720 #ifdef DEBUG
3721         if (debug)
3722         {
3723             fprintf(debug, "G96ANGLES: costheta = %10g  vth = %10g  dV/dct = %10g\n",
3724                     cos_theta, va, dVdt);
3725         }
3726 #endif
3727         for (m = 0; (m < DIM); m++)     /*  42  */
3728         {
3729             f_i[m]    = dVdt*(r_kj[m]*rijrkj_1 - r_ij[m]*rij_2*cos_theta);
3730             f_k[m]    = dVdt*(r_ij[m]*rijrkj_1 - r_kj[m]*rkj_2*cos_theta);
3731             f_j[m]    = -f_i[m]-f_k[m];
3732             f[ai][m] += f_i[m];
3733             f[aj][m] += f_j[m];
3734             f[ak][m] += f_k[m];
3735         }
3736
3737         if (g)
3738         {
3739             copy_ivec(SHIFT_IVEC(g, aj), jt);
3740
3741             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3742             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3743             t1 = IVEC2IS(dt_ij);
3744             t2 = IVEC2IS(dt_kj);
3745         }
3746         rvec_inc(fshift[t1], f_i);
3747         rvec_inc(fshift[CENTRAL], f_j);
3748         rvec_inc(fshift[t2], f_k);          /* 9 */
3749         /* 163 TOTAL    */
3750     }
3751     return vtot;
3752 }
3753
3754 real cross_bond_bond(int nbonds,
3755                      const t_iatom forceatoms[], const t_iparams forceparams[],
3756                      const rvec x[], rvec f[], rvec fshift[],
3757                      const t_pbc *pbc, const t_graph *g,
3758                      real gmx_unused lambda, real gmx_unused *dvdlambda,
3759                      const t_mdatoms gmx_unused *md, t_fcdata gmx_unused  *fcd,
3760                      int gmx_unused *global_atom_index)
3761 {
3762     /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3763      * pp. 842-847
3764      */
3765     int  i, ai, aj, ak, type, m, t1, t2;
3766     rvec r_ij, r_kj;
3767     real vtot, vrr, s1, s2, r1, r2, r1e, r2e, krr;
3768     rvec f_i, f_j, f_k;
3769     ivec jt, dt_ij, dt_kj;
3770
3771     vtot = 0.0;
3772     for (i = 0; (i < nbonds); )
3773     {
3774         type = forceatoms[i++];
3775         ai   = forceatoms[i++];
3776         aj   = forceatoms[i++];
3777         ak   = forceatoms[i++];
3778         r1e  = forceparams[type].cross_bb.r1e;
3779         r2e  = forceparams[type].cross_bb.r2e;
3780         krr  = forceparams[type].cross_bb.krr;
3781
3782         /* Compute distance vectors ... */
3783         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3784         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3785
3786         /* ... and their lengths */
3787         r1 = norm(r_ij);
3788         r2 = norm(r_kj);
3789
3790         /* Deviations from ideality */
3791         s1 = r1-r1e;
3792         s2 = r2-r2e;
3793
3794         /* Energy (can be negative!) */
3795         vrr   = krr*s1*s2;
3796         vtot += vrr;
3797
3798         /* Forces */
3799         svmul(-krr*s2/r1, r_ij, f_i);
3800         svmul(-krr*s1/r2, r_kj, f_k);
3801
3802         for (m = 0; (m < DIM); m++)     /*  12  */
3803         {
3804             f_j[m]    = -f_i[m] - f_k[m];
3805             f[ai][m] += f_i[m];
3806             f[aj][m] += f_j[m];
3807             f[ak][m] += f_k[m];
3808         }
3809
3810         /* Virial stuff */
3811         if (g)
3812         {
3813             copy_ivec(SHIFT_IVEC(g, aj), jt);
3814
3815             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3816             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3817             t1 = IVEC2IS(dt_ij);
3818             t2 = IVEC2IS(dt_kj);
3819         }
3820         rvec_inc(fshift[t1], f_i);
3821         rvec_inc(fshift[CENTRAL], f_j);
3822         rvec_inc(fshift[t2], f_k);          /* 9 */
3823         /* 163 TOTAL    */
3824     }
3825     return vtot;
3826 }
3827
3828 real cross_bond_angle(int nbonds,
3829                       const t_iatom forceatoms[], const t_iparams forceparams[],
3830                       const rvec x[], rvec f[], rvec fshift[],
3831                       const t_pbc *pbc, const t_graph *g,
3832                       real gmx_unused lambda, real gmx_unused *dvdlambda,
3833                       const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3834                       int gmx_unused *global_atom_index)
3835 {
3836     /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3837      * pp. 842-847
3838      */
3839     int  i, ai, aj, ak, type, m, t1, t2;
3840     rvec r_ij, r_kj, r_ik;
3841     real vtot, vrt, s1, s2, s3, r1, r2, r3, r1e, r2e, r3e, krt, k1, k2, k3;
3842     rvec f_i, f_j, f_k;
3843     ivec jt, dt_ij, dt_kj;
3844
3845     vtot = 0.0;
3846     for (i = 0; (i < nbonds); )
3847     {
3848         type = forceatoms[i++];
3849         ai   = forceatoms[i++];
3850         aj   = forceatoms[i++];
3851         ak   = forceatoms[i++];
3852         r1e  = forceparams[type].cross_ba.r1e;
3853         r2e  = forceparams[type].cross_ba.r2e;
3854         r3e  = forceparams[type].cross_ba.r3e;
3855         krt  = forceparams[type].cross_ba.krt;
3856
3857         /* Compute distance vectors ... */
3858         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3859         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3860         pbc_rvec_sub(pbc, x[ai], x[ak], r_ik);
3861
3862         /* ... and their lengths */
3863         r1 = norm(r_ij);
3864         r2 = norm(r_kj);
3865         r3 = norm(r_ik);
3866
3867         /* Deviations from ideality */
3868         s1 = r1-r1e;
3869         s2 = r2-r2e;
3870         s3 = r3-r3e;
3871
3872         /* Energy (can be negative!) */
3873         vrt   = krt*s3*(s1+s2);
3874         vtot += vrt;
3875
3876         /* Forces */
3877         k1 = -krt*(s3/r1);
3878         k2 = -krt*(s3/r2);
3879         k3 = -krt*(s1+s2)/r3;
3880         for (m = 0; (m < DIM); m++)
3881         {
3882             f_i[m] = k1*r_ij[m] + k3*r_ik[m];
3883             f_k[m] = k2*r_kj[m] - k3*r_ik[m];
3884             f_j[m] = -f_i[m] - f_k[m];
3885         }
3886
3887         for (m = 0; (m < DIM); m++)     /*  12  */
3888         {
3889             f[ai][m] += f_i[m];
3890             f[aj][m] += f_j[m];
3891             f[ak][m] += f_k[m];
3892         }
3893
3894         /* Virial stuff */
3895         if (g)
3896         {
3897             copy_ivec(SHIFT_IVEC(g, aj), jt);
3898
3899             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3900             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3901             t1 = IVEC2IS(dt_ij);
3902             t2 = IVEC2IS(dt_kj);
3903         }
3904         rvec_inc(fshift[t1], f_i);
3905         rvec_inc(fshift[CENTRAL], f_j);
3906         rvec_inc(fshift[t2], f_k);          /* 9 */
3907         /* 163 TOTAL    */
3908     }
3909     return vtot;
3910 }
3911
3912 static real bonded_tab(const char *type, int table_nr,
3913                        const bondedtable_t *table, real kA, real kB, real r,
3914                        real lambda, real *V, real *F)
3915 {
3916     real k, tabscale, *VFtab, rt, eps, eps2, Yt, Ft, Geps, Heps2, Fp, VV, FF;
3917     int  n0, nnn;
3918     real dvdlambda;
3919
3920     k = (1.0 - lambda)*kA + lambda*kB;
3921
3922     tabscale = table->scale;
3923     VFtab    = table->data;
3924
3925     rt    = r*tabscale;
3926     n0    = static_cast<int>(rt);
3927     if (n0 >= table->n)
3928     {
3929         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",
3930                   type, table_nr, r, n0, n0+1, table->n);
3931     }
3932     eps   = rt - n0;
3933     eps2  = eps*eps;
3934     nnn   = 4*n0;
3935     Yt    = VFtab[nnn];
3936     Ft    = VFtab[nnn+1];
3937     Geps  = VFtab[nnn+2]*eps;
3938     Heps2 = VFtab[nnn+3]*eps2;
3939     Fp    = Ft + Geps + Heps2;
3940     VV    = Yt + Fp*eps;
3941     FF    = Fp + Geps + 2.0*Heps2;
3942
3943     *F         = -k*FF*tabscale;
3944     *V         = k*VV;
3945     dvdlambda  = (kB - kA)*VV;
3946
3947     return dvdlambda;
3948
3949     /* That was 22 flops */
3950 }
3951
3952 real tab_bonds(int nbonds,
3953                const t_iatom forceatoms[], const t_iparams forceparams[],
3954                const rvec x[], rvec f[], rvec fshift[],
3955                const t_pbc *pbc, const t_graph *g,
3956                real lambda, real *dvdlambda,
3957                const t_mdatoms gmx_unused *md, t_fcdata *fcd,
3958                int gmx_unused  *global_atom_index)
3959 {
3960     int  i, m, ki, ai, aj, type, table;
3961     real dr, dr2, fbond, vbond, fij, vtot;
3962     rvec dx;
3963     ivec dt;
3964
3965     vtot = 0.0;
3966     for (i = 0; (i < nbonds); )
3967     {
3968         type = forceatoms[i++];
3969         ai   = forceatoms[i++];
3970         aj   = forceatoms[i++];
3971
3972         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /*   3      */
3973         dr2  = iprod(dx, dx);                       /*   5              */
3974         dr   = dr2*gmx_invsqrt(dr2);                /*  10              */
3975
3976         table = forceparams[type].tab.table;
3977
3978         *dvdlambda += bonded_tab("bond", table,
3979                                  &fcd->bondtab[table],
3980                                  forceparams[type].tab.kA,
3981                                  forceparams[type].tab.kB,
3982                                  dr, lambda, &vbond, &fbond); /*  22 */
3983
3984         if (dr2 == 0.0)
3985         {
3986             continue;
3987         }
3988
3989
3990         vtot  += vbond;            /* 1*/
3991         fbond *= gmx_invsqrt(dr2); /*   6               */
3992 #ifdef DEBUG
3993         if (debug)
3994         {
3995             fprintf(debug, "TABBONDS: dr = %10g  vbond = %10g  fbond = %10g\n",
3996                     dr, vbond, fbond);
3997         }
3998 #endif
3999         if (g)
4000         {
4001             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
4002             ki = IVEC2IS(dt);
4003         }
4004         for (m = 0; (m < DIM); m++)     /*  15          */
4005         {
4006             fij                 = fbond*dx[m];
4007             f[ai][m]           += fij;
4008             f[aj][m]           -= fij;
4009             fshift[ki][m]      += fij;
4010             fshift[CENTRAL][m] -= fij;
4011         }
4012     }               /* 62 TOTAL */
4013     return vtot;
4014 }
4015
4016 real tab_angles(int nbonds,
4017                 const t_iatom forceatoms[], const t_iparams forceparams[],
4018                 const rvec x[], rvec f[], rvec fshift[],
4019                 const t_pbc *pbc, const t_graph *g,
4020                 real lambda, real *dvdlambda,
4021                 const t_mdatoms gmx_unused  *md, t_fcdata *fcd,
4022                 int gmx_unused *global_atom_index)
4023 {
4024     int  i, ai, aj, ak, t1, t2, type, table;
4025     rvec r_ij, r_kj;
4026     real cos_theta, cos_theta2, theta, dVdt, va, vtot;
4027     ivec jt, dt_ij, dt_kj;
4028
4029     vtot = 0.0;
4030     for (i = 0; (i < nbonds); )
4031     {
4032         type = forceatoms[i++];
4033         ai   = forceatoms[i++];
4034         aj   = forceatoms[i++];
4035         ak   = forceatoms[i++];
4036
4037         theta  = bond_angle(x[ai], x[aj], x[ak], pbc,
4038                             r_ij, r_kj, &cos_theta, &t1, &t2); /*  41           */
4039
4040         table = forceparams[type].tab.table;
4041
4042         *dvdlambda += bonded_tab("angle", table,
4043                                  &fcd->angletab[table],
4044                                  forceparams[type].tab.kA,
4045                                  forceparams[type].tab.kB,
4046                                  theta, lambda, &va, &dVdt); /*  22  */
4047         vtot += va;
4048
4049         cos_theta2 = sqr(cos_theta);            /*   1          */
4050         if (cos_theta2 < 1)
4051         {
4052             int  m;
4053             real st, sth;
4054             real cik, cii, ckk;
4055             real nrkj2, nrij2;
4056             rvec f_i, f_j, f_k;
4057
4058             st  = dVdt*gmx_invsqrt(1 - cos_theta2); /*  12              */
4059             sth = st*cos_theta;                     /*   1              */
4060 #ifdef DEBUG
4061             if (debug)
4062             {
4063                 fprintf(debug, "ANGLES: theta = %10g  vth = %10g  dV/dtheta = %10g\n",
4064                         theta*RAD2DEG, va, dVdt);
4065             }
4066 #endif
4067             nrkj2 = iprod(r_kj, r_kj);  /*   5          */
4068             nrij2 = iprod(r_ij, r_ij);
4069
4070             cik = st*gmx_invsqrt(nrkj2*nrij2); /*  12           */
4071             cii = sth/nrij2;                   /*  10           */
4072             ckk = sth/nrkj2;                   /*  10           */
4073
4074             for (m = 0; (m < DIM); m++)        /*  39           */
4075             {
4076                 f_i[m]    = -(cik*r_kj[m]-cii*r_ij[m]);
4077                 f_k[m]    = -(cik*r_ij[m]-ckk*r_kj[m]);
4078                 f_j[m]    = -f_i[m]-f_k[m];
4079                 f[ai][m] += f_i[m];
4080                 f[aj][m] += f_j[m];
4081                 f[ak][m] += f_k[m];
4082             }
4083             if (g)
4084             {
4085                 copy_ivec(SHIFT_IVEC(g, aj), jt);
4086
4087                 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
4088                 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
4089                 t1 = IVEC2IS(dt_ij);
4090                 t2 = IVEC2IS(dt_kj);
4091             }
4092             rvec_inc(fshift[t1], f_i);
4093             rvec_inc(fshift[CENTRAL], f_j);
4094             rvec_inc(fshift[t2], f_k);
4095         }                                       /* 169 TOTAL    */
4096     }
4097     return vtot;
4098 }
4099
4100 real tab_dihs(int nbonds,
4101               const t_iatom forceatoms[], const t_iparams forceparams[],
4102               const rvec x[], rvec f[], rvec fshift[],
4103               const t_pbc *pbc, const t_graph *g,
4104               real lambda, real *dvdlambda,
4105               const t_mdatoms gmx_unused *md, t_fcdata *fcd,
4106               int gmx_unused *global_atom_index)
4107 {
4108     int  i, type, ai, aj, ak, al, table;
4109     int  t1, t2, t3;
4110     rvec r_ij, r_kj, r_kl, m, n;
4111     real phi, sign, ddphi, vpd, vtot;
4112
4113     vtot = 0.0;
4114     for (i = 0; (i < nbonds); )
4115     {
4116         type = forceatoms[i++];
4117         ai   = forceatoms[i++];
4118         aj   = forceatoms[i++];
4119         ak   = forceatoms[i++];
4120         al   = forceatoms[i++];
4121
4122         phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
4123                         &sign, &t1, &t2, &t3);  /*  84  */
4124
4125         table = forceparams[type].tab.table;
4126
4127         /* Hopefully phi+M_PI never results in values < 0 */
4128         *dvdlambda += bonded_tab("dihedral", table,
4129                                  &fcd->dihtab[table],
4130                                  forceparams[type].tab.kA,
4131                                  forceparams[type].tab.kB,
4132                                  phi+M_PI, lambda, &vpd, &ddphi);
4133
4134         vtot += vpd;
4135         do_dih_fup(ai, aj, ak, al, -ddphi, r_ij, r_kj, r_kl, m, n,
4136                    f, fshift, pbc, g, x, t1, t2, t3); /* 112    */
4137
4138 #ifdef DEBUG
4139         fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
4140                 ai, aj, ak, al, phi);
4141 #endif
4142     } /* 227 TOTAL  */
4143
4144     return vtot;
4145 }
4146
4147 /* TODO This function could go away when idef is not a big bucket of
4148    everything. */
4149 gmx_bool ftype_is_bonded_potential(int ftype)
4150 {
4151     return
4152         (interaction_function[ftype].flags & IF_BOND) &&
4153         !(ftype == F_CONNBONDS || ftype == F_POSRES || ftype == F_FBPOSRES) &&
4154         (ftype < F_GB12 || ftype > F_GB14);
4155 }
4156
4157 static void zero_thread_forces(f_thread_t *f_t, int n,
4158                                int nblock, int blocksize)
4159 {
4160     int b, a0, a1, a, i, j;
4161
4162     if (n > f_t->f_nalloc)
4163     {
4164         f_t->f_nalloc = over_alloc_large(n);
4165         srenew(f_t->f, f_t->f_nalloc);
4166     }
4167
4168     if (f_t->red_mask != 0)
4169     {
4170         for (b = 0; b < nblock; b++)
4171         {
4172             if (f_t->red_mask && (1U<<b))
4173             {
4174                 a0 = b*blocksize;
4175                 a1 = std::min((b+1)*blocksize, n);
4176                 for (a = a0; a < a1; a++)
4177                 {
4178                     clear_rvec(f_t->f[a]);
4179                 }
4180             }
4181         }
4182     }
4183     for (i = 0; i < SHIFTS; i++)
4184     {
4185         clear_rvec(f_t->fshift[i]);
4186     }
4187     for (i = 0; i < F_NRE; i++)
4188     {
4189         f_t->ener[i] = 0;
4190     }
4191     for (i = 0; i < egNR; i++)
4192     {
4193         for (j = 0; j < f_t->grpp.nener; j++)
4194         {
4195             f_t->grpp.ener[i][j] = 0;
4196         }
4197     }
4198     for (i = 0; i < efptNR; i++)
4199     {
4200         f_t->dvdl[i] = 0;
4201     }
4202 }
4203
4204 static void reduce_thread_force_buffer(int n, rvec *f,
4205                                        int nthreads, f_thread_t *f_t,
4206                                        int nblock, int block_size)
4207 {
4208     /* The max thread number is arbitrary,
4209      * we used a fixed number to avoid memory management.
4210      * Using more than 16 threads is probably never useful performance wise.
4211      */
4212 #define MAX_BONDED_THREADS 256
4213     int b;
4214
4215     if (nthreads > MAX_BONDED_THREADS)
4216     {
4217         gmx_fatal(FARGS, "Can not reduce bonded forces on more than %d threads",
4218                   MAX_BONDED_THREADS);
4219     }
4220
4221     /* This reduction can run on any number of threads,
4222      * independently of nthreads.
4223      */
4224 #pragma omp parallel for num_threads(nthreads) schedule(static)
4225     for (b = 0; b < nblock; b++)
4226     {
4227         rvec *fp[MAX_BONDED_THREADS];
4228         int   nfb, ft, fb;
4229         int   a0, a1, a;
4230
4231         /* Determine which threads contribute to this block */
4232         nfb = 0;
4233         for (ft = 1; ft < nthreads; ft++)
4234         {
4235             if (f_t[ft].red_mask & (1U<<b))
4236             {
4237                 fp[nfb++] = f_t[ft].f;
4238             }
4239         }
4240         if (nfb > 0)
4241         {
4242             /* Reduce force buffers for threads that contribute */
4243             a0 =  b   *block_size;
4244             a1 = (b+1)*block_size;
4245             a1 = std::min(a1, n);
4246             for (a = a0; a < a1; a++)
4247             {
4248                 for (fb = 0; fb < nfb; fb++)
4249                 {
4250                     rvec_inc(f[a], fp[fb][a]);
4251                 }
4252             }
4253         }
4254     }
4255 }
4256
4257 static void reduce_thread_forces(int n, rvec *f, rvec *fshift,
4258                                  real *ener, gmx_grppairener_t *grpp, real *dvdl,
4259                                  int nthreads, f_thread_t *f_t,
4260                                  int nblock, int block_size,
4261                                  gmx_bool bCalcEnerVir,
4262                                  gmx_bool bDHDL)
4263 {
4264     if (nblock > 0)
4265     {
4266         /* Reduce the bonded force buffer */
4267         reduce_thread_force_buffer(n, f, nthreads, f_t, nblock, block_size);
4268     }
4269
4270     /* When necessary, reduce energy and virial using one thread only */
4271     if (bCalcEnerVir)
4272     {
4273         int t, i, j;
4274
4275         for (i = 0; i < SHIFTS; i++)
4276         {
4277             for (t = 1; t < nthreads; t++)
4278             {
4279                 rvec_inc(fshift[i], f_t[t].fshift[i]);
4280             }
4281         }
4282         for (i = 0; i < F_NRE; i++)
4283         {
4284             for (t = 1; t < nthreads; t++)
4285             {
4286                 ener[i] += f_t[t].ener[i];
4287             }
4288         }
4289         for (i = 0; i < egNR; i++)
4290         {
4291             for (j = 0; j < f_t[1].grpp.nener; j++)
4292             {
4293                 for (t = 1; t < nthreads; t++)
4294                 {
4295
4296                     grpp->ener[i][j] += f_t[t].grpp.ener[i][j];
4297                 }
4298             }
4299         }
4300         if (bDHDL)
4301         {
4302             for (i = 0; i < efptNR; i++)
4303             {
4304
4305                 for (t = 1; t < nthreads; t++)
4306                 {
4307                     dvdl[i] += f_t[t].dvdl[i];
4308                 }
4309             }
4310         }
4311     }
4312 }
4313
4314 static real calc_one_bond(int thread,
4315                           int ftype, const t_idef *idef,
4316                           const rvec x[], rvec f[], rvec fshift[],
4317                           t_forcerec *fr,
4318                           const t_pbc *pbc, const t_graph *g,
4319                           gmx_grppairener_t *grpp,
4320                           t_nrnb *nrnb,
4321                           real *lambda, real *dvdl,
4322                           const t_mdatoms *md, t_fcdata *fcd,
4323                           gmx_bool bCalcEnerVir,
4324                           int *global_atom_index)
4325 {
4326     int      nat1, nbonds, efptFTYPE;
4327     real     v = 0;
4328     t_iatom *iatoms;
4329     int      nb0, nbn;
4330
4331     if (IS_RESTRAINT_TYPE(ftype))
4332     {
4333         efptFTYPE = efptRESTRAINT;
4334     }
4335     else
4336     {
4337         efptFTYPE = efptBONDED;
4338     }
4339
4340     nat1      = interaction_function[ftype].nratoms + 1;
4341     nbonds    = idef->il[ftype].nr/nat1;
4342     iatoms    = idef->il[ftype].iatoms;
4343
4344     nb0 = idef->il_thread_division[ftype*(idef->nthreads+1)+thread];
4345     nbn = idef->il_thread_division[ftype*(idef->nthreads+1)+thread+1] - nb0;
4346
4347     if (!IS_LISTED_LJ_C(ftype))
4348     {
4349         if (ftype == F_CMAP)
4350         {
4351             v = cmap_dihs(nbn, iatoms+nb0,
4352                           idef->iparams, &idef->cmap_grid,
4353                           x, f, fshift,
4354                           pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4355                           md, fcd, global_atom_index);
4356         }
4357 #ifdef GMX_SIMD_HAVE_REAL
4358         else if (ftype == F_ANGLES &&
4359                  !bCalcEnerVir && fr->efep == efepNO)
4360         {
4361             /* No energies, shift forces, dvdl */
4362             angles_noener_simd(nbn, idef->il[ftype].iatoms+nb0,
4363                                idef->iparams,
4364                                x, f,
4365                                pbc, g, lambda[efptFTYPE], md, fcd,
4366                                global_atom_index);
4367             v = 0;
4368         }
4369 #endif
4370         else if (ftype == F_PDIHS &&
4371                  !bCalcEnerVir && fr->efep == efepNO)
4372         {
4373             /* No energies, shift forces, dvdl */
4374 #ifdef GMX_SIMD_HAVE_REAL
4375             pdihs_noener_simd
4376 #else
4377             pdihs_noener
4378 #endif
4379                 (nbn, idef->il[ftype].iatoms+nb0,
4380                 idef->iparams,
4381                 x, f,
4382                 pbc, g, lambda[efptFTYPE], md, fcd,
4383                 global_atom_index);
4384             v = 0;
4385         }
4386         else
4387         {
4388             v = interaction_function[ftype].ifunc(nbn, iatoms+nb0,
4389                                                   idef->iparams,
4390                                                   x, f, fshift,
4391                                                   pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4392                                                   md, fcd, global_atom_index);
4393         }
4394     }
4395     else
4396     {
4397         v = do_nonbonded_listed(ftype, nbn, iatoms+nb0, idef->iparams, x, f, fshift,
4398                                 pbc, g, lambda, dvdl, md, fr, grpp, global_atom_index);
4399     }
4400
4401     if (thread == 0)
4402     {
4403         inc_nrnb(nrnb, interaction_function[ftype].nrnb_ind, nbonds);
4404     }
4405
4406     return v;
4407 }
4408
4409 void calc_bonds(const gmx_multisim_t *ms,
4410                 const t_idef *idef,
4411                 const rvec x[], history_t *hist,
4412                 rvec f[], t_forcerec *fr,
4413                 const t_pbc *pbc, const t_graph *g,
4414                 gmx_enerdata_t *enerd, t_nrnb *nrnb,
4415                 real *lambda,
4416                 const t_mdatoms *md,
4417                 t_fcdata *fcd, int *global_atom_index,
4418                 int force_flags)
4419 {
4420     gmx_bool      bCalcEnerVir;
4421     int           i;
4422     real          dvdl[efptNR]; /* The dummy array is to have a place to store the dhdl at other values
4423                                                         of lambda, which will be thrown away in the end*/
4424     const  t_pbc *pbc_null;
4425     int           thread;
4426
4427     assert(fr->nthreads == idef->nthreads);
4428
4429     bCalcEnerVir = (force_flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY));
4430
4431     for (i = 0; i < efptNR; i++)
4432     {
4433         dvdl[i] = 0.0;
4434     }
4435     if (fr->bMolPBC)
4436     {
4437         pbc_null = pbc;
4438     }
4439     else
4440     {
4441         pbc_null = NULL;
4442     }
4443
4444 #ifdef DEBUG
4445     if (g && debug)
4446     {
4447         p_graph(debug, "Bondage is fun", g);
4448     }
4449 #endif
4450
4451     /* Do pre force calculation stuff which might require communication */
4452     if (idef->il[F_ORIRES].nr)
4453     {
4454         enerd->term[F_ORIRESDEV] =
4455             calc_orires_dev(ms, idef->il[F_ORIRES].nr,
4456                             idef->il[F_ORIRES].iatoms,
4457                             idef->iparams, md, x,
4458                             pbc_null, fcd, hist);
4459     }
4460     if (idef->il[F_DISRES].nr)
4461     {
4462         calc_disres_R_6(idef->il[F_DISRES].nr,
4463                         idef->il[F_DISRES].iatoms,
4464                         idef->iparams, x, pbc_null,
4465                         fcd, hist);
4466 #ifdef GMX_MPI
4467         if (fcd->disres.nsystems > 1)
4468         {
4469             gmx_sum_sim(2*fcd->disres.nres, fcd->disres.Rt_6, ms);
4470         }
4471 #endif
4472     }
4473
4474 #pragma omp parallel for num_threads(fr->nthreads) schedule(static)
4475     for (thread = 0; thread < fr->nthreads; thread++)
4476     {
4477         int                ftype;
4478         real              *epot, v;
4479         /* thread stuff */
4480         rvec              *ft, *fshift;
4481         real              *dvdlt;
4482         gmx_grppairener_t *grpp;
4483
4484         if (thread == 0)
4485         {
4486             ft     = f;
4487             fshift = fr->fshift;
4488             epot   = enerd->term;
4489             grpp   = &enerd->grpp;
4490             dvdlt  = dvdl;
4491         }
4492         else
4493         {
4494             zero_thread_forces(&fr->f_t[thread], fr->natoms_force,
4495                                fr->red_nblock, 1<<fr->red_ashift);
4496
4497             ft     = fr->f_t[thread].f;
4498             fshift = fr->f_t[thread].fshift;
4499             epot   = fr->f_t[thread].ener;
4500             grpp   = &fr->f_t[thread].grpp;
4501             dvdlt  = fr->f_t[thread].dvdl;
4502         }
4503         /* Loop over all bonded force types to calculate the bonded forces */
4504         for (ftype = 0; (ftype < F_NRE); ftype++)
4505         {
4506             if (idef->il[ftype].nr > 0 && ftype_is_bonded_potential(ftype))
4507             {
4508                 v = calc_one_bond(thread, ftype, idef, x,
4509                                   ft, fshift, fr, pbc_null, g, grpp,
4510                                   nrnb, lambda, dvdlt,
4511                                   md, fcd, bCalcEnerVir,
4512                                   global_atom_index);
4513                 epot[ftype] += v;
4514             }
4515         }
4516     }
4517     if (fr->nthreads > 1)
4518     {
4519         reduce_thread_forces(fr->natoms_force, f, fr->fshift,
4520                              enerd->term, &enerd->grpp, dvdl,
4521                              fr->nthreads, fr->f_t,
4522                              fr->red_nblock, 1<<fr->red_ashift,
4523                              bCalcEnerVir,
4524                              force_flags & GMX_FORCE_DHDL);
4525     }
4526     if (force_flags & GMX_FORCE_DHDL)
4527     {
4528         for (i = 0; i < efptNR; i++)
4529         {
4530             enerd->dvdl_nonlin[i] += dvdl[i];
4531         }
4532     }
4533
4534     /* Copy the sum of violations for the distance restraints from fcd */
4535     if (fcd)
4536     {
4537         enerd->term[F_DISRESVIOL] = fcd->disres.sumviol;
4538
4539     }
4540 }
4541
4542 void calc_bonds_lambda(const t_idef *idef,
4543                        const rvec x[],
4544                        t_forcerec *fr,
4545                        const t_pbc *pbc, const t_graph *g,
4546                        gmx_grppairener_t *grpp, real *epot, t_nrnb *nrnb,
4547                        real *lambda,
4548                        const t_mdatoms *md,
4549                        t_fcdata *fcd,
4550                        int *global_atom_index)
4551 {
4552     int           ftype, nr_nonperturbed, nr;
4553     real          v;
4554     real          dvdl_dum[efptNR];
4555     rvec         *f, *fshift;
4556     const  t_pbc *pbc_null;
4557     t_idef        idef_fe;
4558
4559     if (fr->bMolPBC)
4560     {
4561         pbc_null = pbc;
4562     }
4563     else
4564     {
4565         pbc_null = NULL;
4566     }
4567
4568     /* Copy the whole idef, so we can modify the contents locally */
4569     idef_fe          = *idef;
4570     idef_fe.nthreads = 1;
4571     snew(idef_fe.il_thread_division, F_NRE*(idef_fe.nthreads+1));
4572
4573     /* We already have the forces, so we use temp buffers here */
4574     snew(f, fr->natoms_force);
4575     snew(fshift, SHIFTS);
4576
4577     /* Loop over all bonded force types to calculate the bonded energies */
4578     for (ftype = 0; (ftype < F_NRE); ftype++)
4579     {
4580         if (ftype_is_bonded_potential(ftype))
4581         {
4582             /* Set the work range of thread 0 to the perturbed bondeds only */
4583             nr_nonperturbed                       = idef->il[ftype].nr_nonperturbed;
4584             nr                                    = idef->il[ftype].nr;
4585             idef_fe.il_thread_division[ftype*2+0] = nr_nonperturbed;
4586             idef_fe.il_thread_division[ftype*2+1] = nr;
4587
4588             /* This is only to get the flop count correct */
4589             idef_fe.il[ftype].nr = nr - nr_nonperturbed;
4590
4591             if (nr - nr_nonperturbed > 0)
4592             {
4593                 v = calc_one_bond(0, ftype, &idef_fe,
4594                                   x, f, fshift, fr, pbc_null, g,
4595                                   grpp, nrnb, lambda, dvdl_dum,
4596                                   md, fcd, TRUE,
4597                                   global_atom_index);
4598                 epot[ftype] += v;
4599             }
4600         }
4601     }
4602
4603     sfree(fshift);
4604     sfree(f);
4605
4606     sfree(idef_fe.il_thread_division);
4607 }