a8757886d410cf35c224baf0894e719fa840b51f
[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 Adds forces of flat-bottomed positions restraints to f[]
2259  *         and fixes vir_diag. Returns the flat-bottomed potential. */
2260 real fbposres(int nbonds,
2261               const t_iatom forceatoms[], const t_iparams forceparams[],
2262               const rvec x[], rvec f[], rvec vir_diag,
2263               t_pbc *pbc,
2264               int refcoord_scaling, int ePBC, rvec com)
2265 /* compute flat-bottomed positions restraints */
2266 {
2267     int              i, ai, m, d, type, npbcdim = 0, fbdim;
2268     const t_iparams *pr;
2269     real             vtot, kk, v;
2270     real             dr, dr2, rfb, rfb2, fact, invdr;
2271     rvec             com_sc, rdist, dx, dpdl, fm;
2272     gmx_bool         bInvert;
2273
2274     npbcdim = ePBC2npbcdim(ePBC);
2275
2276     if (refcoord_scaling == erscCOM)
2277     {
2278         clear_rvec(com_sc);
2279         for (m = 0; m < npbcdim; m++)
2280         {
2281             assert(npbcdim <= DIM);
2282             for (d = m; d < npbcdim; d++)
2283             {
2284                 com_sc[m] += com[d]*pbc->box[d][m];
2285             }
2286         }
2287     }
2288
2289     vtot = 0.0;
2290     for (i = 0; (i < nbonds); )
2291     {
2292         type = forceatoms[i++];
2293         ai   = forceatoms[i++];
2294         pr   = &forceparams[type];
2295
2296         /* same calculation as for normal posres, but with identical A and B states, and lambda==0 */
2297         posres_dx(x[ai], forceparams[type].fbposres.pos0, forceparams[type].fbposres.pos0,
2298                   com_sc, com_sc, 0.0,
2299                   pbc, refcoord_scaling, npbcdim,
2300                   dx, rdist, dpdl);
2301
2302         clear_rvec(fm);
2303         v = 0.0;
2304
2305         kk   = pr->fbposres.k;
2306         rfb  = pr->fbposres.r;
2307         rfb2 = sqr(rfb);
2308
2309         /* with rfb<0, push particle out of the sphere/cylinder/layer */
2310         bInvert = FALSE;
2311         if (rfb < 0.)
2312         {
2313             bInvert = TRUE;
2314             rfb     = -rfb;
2315         }
2316
2317         switch (pr->fbposres.geom)
2318         {
2319             case efbposresSPHERE:
2320                 /* spherical flat-bottom posres */
2321                 dr2 = norm2(dx);
2322                 if (dr2 > 0.0 &&
2323                     ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2324                     )
2325                 {
2326                     dr   = sqrt(dr2);
2327                     v    = 0.5*kk*sqr(dr - rfb);
2328                     fact = -kk*(dr-rfb)/dr; /* Force pointing to the center pos0 */
2329                     svmul(fact, dx, fm);
2330                 }
2331                 break;
2332             case efbposresCYLINDER:
2333                 /* cylidrical flat-bottom posres in x-y plane. fm[ZZ] = 0. */
2334                 dr2 = sqr(dx[XX])+sqr(dx[YY]);
2335                 if  (dr2 > 0.0 &&
2336                      ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
2337                      )
2338                 {
2339                     dr     = sqrt(dr2);
2340                     invdr  = 1./dr;
2341                     v      = 0.5*kk*sqr(dr - rfb);
2342                     fm[XX] = -kk*(dr-rfb)*dx[XX]*invdr; /* Force pointing to the center */
2343                     fm[YY] = -kk*(dr-rfb)*dx[YY]*invdr;
2344                 }
2345                 break;
2346             case efbposresX: /* fbdim=XX */
2347             case efbposresY: /* fbdim=YY */
2348             case efbposresZ: /* fbdim=ZZ */
2349                 /* 1D flat-bottom potential */
2350                 fbdim = pr->fbposres.geom - efbposresX;
2351                 dr    = dx[fbdim];
2352                 if ( ( dr > rfb && bInvert == FALSE ) || ( 0 < dr && dr < rfb && bInvert == TRUE )  )
2353                 {
2354                     v         = 0.5*kk*sqr(dr - rfb);
2355                     fm[fbdim] = -kk*(dr - rfb);
2356                 }
2357                 else if ( (dr < (-rfb) && bInvert == FALSE ) || ( (-rfb) < dr && dr < 0 && bInvert == TRUE ))
2358                 {
2359                     v         = 0.5*kk*sqr(dr + rfb);
2360                     fm[fbdim] = -kk*(dr + rfb);
2361                 }
2362                 break;
2363         }
2364
2365         vtot += v;
2366
2367         for (m = 0; (m < DIM); m++)
2368         {
2369             f[ai][m]   += fm[m];
2370             /* Here we correct for the pbc_dx which included rdist */
2371             vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm[m];
2372         }
2373     }
2374
2375     return vtot;
2376 }
2377
2378
2379 real posres(int nbonds,
2380             const t_iatom forceatoms[], const t_iparams forceparams[],
2381             const rvec x[], rvec f[], rvec vir_diag,
2382             t_pbc *pbc,
2383             real lambda, real *dvdlambda,
2384             int refcoord_scaling, int ePBC, rvec comA, rvec comB)
2385 {
2386     int              i, ai, m, d, type, npbcdim = 0;
2387     const t_iparams *pr;
2388     real             L1;
2389     real             vtot, kk, fm;
2390     rvec             comA_sc, comB_sc, rdist, dpdl, dx;
2391     gmx_bool         bForceValid = TRUE;
2392
2393     if ((f == NULL) || (vir_diag == NULL))    /* should both be null together! */
2394     {
2395         bForceValid = FALSE;
2396     }
2397
2398     npbcdim = ePBC2npbcdim(ePBC);
2399
2400     if (refcoord_scaling == erscCOM)
2401     {
2402         clear_rvec(comA_sc);
2403         clear_rvec(comB_sc);
2404         for (m = 0; m < npbcdim; m++)
2405         {
2406             assert(npbcdim <= DIM);
2407             for (d = m; d < npbcdim; d++)
2408             {
2409                 comA_sc[m] += comA[d]*pbc->box[d][m];
2410                 comB_sc[m] += comB[d]*pbc->box[d][m];
2411             }
2412         }
2413     }
2414
2415     L1 = 1.0 - lambda;
2416
2417     vtot = 0.0;
2418     for (i = 0; (i < nbonds); )
2419     {
2420         type = forceatoms[i++];
2421         ai   = forceatoms[i++];
2422         pr   = &forceparams[type];
2423
2424         /* return dx, rdist, and dpdl */
2425         posres_dx(x[ai], forceparams[type].posres.pos0A, forceparams[type].posres.pos0B,
2426                   comA_sc, comB_sc, lambda,
2427                   pbc, refcoord_scaling, npbcdim,
2428                   dx, rdist, dpdl);
2429
2430         for (m = 0; (m < DIM); m++)
2431         {
2432             kk          = L1*pr->posres.fcA[m] + lambda*pr->posres.fcB[m];
2433             fm          = -kk*dx[m];
2434             vtot       += 0.5*kk*dx[m]*dx[m];
2435             *dvdlambda +=
2436                 0.5*(pr->posres.fcB[m] - pr->posres.fcA[m])*dx[m]*dx[m]
2437                 + fm*dpdl[m];
2438
2439             /* Here we correct for the pbc_dx which included rdist */
2440             if (bForceValid)
2441             {
2442                 f[ai][m]    += fm;
2443                 vir_diag[m] -= 0.5*(dx[m] + rdist[m])*fm;
2444             }
2445         }
2446     }
2447
2448     return vtot;
2449 }
2450
2451 static real low_angres(int nbonds,
2452                        const t_iatom forceatoms[], const t_iparams forceparams[],
2453                        const rvec x[], rvec f[], rvec fshift[],
2454                        const t_pbc *pbc, const t_graph *g,
2455                        real lambda, real *dvdlambda,
2456                        gmx_bool bZAxis)
2457 {
2458     int  i, m, type, ai, aj, ak, al;
2459     int  t1, t2;
2460     real phi, cos_phi, cos_phi2, vid, vtot, dVdphi;
2461     rvec r_ij, r_kl, f_i, f_k = {0, 0, 0};
2462     real st, sth, nrij2, nrkl2, c, cij, ckl;
2463
2464     ivec dt;
2465     t2 = 0; /* avoid warning with gcc-3.3. It is never used uninitialized */
2466
2467     vtot = 0.0;
2468     ak   = al = 0; /* to avoid warnings */
2469     for (i = 0; i < nbonds; )
2470     {
2471         type = forceatoms[i++];
2472         ai   = forceatoms[i++];
2473         aj   = forceatoms[i++];
2474         t1   = pbc_rvec_sub(pbc, x[aj], x[ai], r_ij);       /*  3               */
2475         if (!bZAxis)
2476         {
2477             ak   = forceatoms[i++];
2478             al   = forceatoms[i++];
2479             t2   = pbc_rvec_sub(pbc, x[al], x[ak], r_kl);  /*  3                */
2480         }
2481         else
2482         {
2483             r_kl[XX] = 0;
2484             r_kl[YY] = 0;
2485             r_kl[ZZ] = 1;
2486         }
2487
2488         cos_phi = cos_angle(r_ij, r_kl); /* 25          */
2489         phi     = acos(cos_phi);         /* 10           */
2490
2491         *dvdlambda += dopdihs_min(forceparams[type].pdihs.cpA,
2492                                   forceparams[type].pdihs.cpB,
2493                                   forceparams[type].pdihs.phiA,
2494                                   forceparams[type].pdihs.phiB,
2495                                   forceparams[type].pdihs.mult,
2496                                   phi, lambda, &vid, &dVdphi); /*  40  */
2497
2498         vtot += vid;
2499
2500         cos_phi2 = sqr(cos_phi);                /*   1          */
2501         if (cos_phi2 < 1)
2502         {
2503             st    = -dVdphi*gmx_invsqrt(1 - cos_phi2); /*  12           */
2504             sth   = st*cos_phi;                        /*   1           */
2505             nrij2 = iprod(r_ij, r_ij);                 /*   5           */
2506             nrkl2 = iprod(r_kl, r_kl);                 /*   5          */
2507
2508             c   = st*gmx_invsqrt(nrij2*nrkl2);         /*  11           */
2509             cij = sth/nrij2;                           /*  10           */
2510             ckl = sth/nrkl2;                           /*  10           */
2511
2512             for (m = 0; m < DIM; m++)                  /*  18+18       */
2513             {
2514                 f_i[m]    = (c*r_kl[m]-cij*r_ij[m]);
2515                 f[ai][m] += f_i[m];
2516                 f[aj][m] -= f_i[m];
2517                 if (!bZAxis)
2518                 {
2519                     f_k[m]    = (c*r_ij[m]-ckl*r_kl[m]);
2520                     f[ak][m] += f_k[m];
2521                     f[al][m] -= f_k[m];
2522                 }
2523             }
2524
2525             if (g)
2526             {
2527                 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
2528                 t1 = IVEC2IS(dt);
2529             }
2530             rvec_inc(fshift[t1], f_i);
2531             rvec_dec(fshift[CENTRAL], f_i);
2532             if (!bZAxis)
2533             {
2534                 if (g)
2535                 {
2536                     ivec_sub(SHIFT_IVEC(g, ak), SHIFT_IVEC(g, al), dt);
2537                     t2 = IVEC2IS(dt);
2538                 }
2539                 rvec_inc(fshift[t2], f_k);
2540                 rvec_dec(fshift[CENTRAL], f_k);
2541             }
2542         }
2543     }
2544
2545     return vtot; /*  184 / 157 (bZAxis)  total  */
2546 }
2547
2548 real angres(int nbonds,
2549             const t_iatom forceatoms[], const t_iparams forceparams[],
2550             const rvec x[], rvec f[], rvec fshift[],
2551             const t_pbc *pbc, const t_graph *g,
2552             real lambda, real *dvdlambda,
2553             const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2554             int gmx_unused *global_atom_index)
2555 {
2556     return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2557                       lambda, dvdlambda, FALSE);
2558 }
2559
2560 real angresz(int nbonds,
2561              const t_iatom forceatoms[], const t_iparams forceparams[],
2562              const rvec x[], rvec f[], rvec fshift[],
2563              const t_pbc *pbc, const t_graph *g,
2564              real lambda, real *dvdlambda,
2565              const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2566              int gmx_unused *global_atom_index)
2567 {
2568     return low_angres(nbonds, forceatoms, forceparams, x, f, fshift, pbc, g,
2569                       lambda, dvdlambda, TRUE);
2570 }
2571
2572 real dihres(int nbonds,
2573             const t_iatom forceatoms[], const t_iparams forceparams[],
2574             const rvec x[], rvec f[], rvec fshift[],
2575             const t_pbc *pbc, const t_graph *g,
2576             real lambda, real *dvdlambda,
2577             const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2578             int gmx_unused  *global_atom_index)
2579 {
2580     real vtot = 0;
2581     int  ai, aj, ak, al, i, k, type, t1, t2, t3;
2582     real phi0A, phi0B, dphiA, dphiB, kfacA, kfacB, phi0, dphi, kfac;
2583     real phi, ddphi, ddp, ddp2, dp, sign, d2r, L1;
2584     rvec r_ij, r_kj, r_kl, m, n;
2585
2586     L1 = 1.0-lambda;
2587
2588     d2r = DEG2RAD;
2589     k   = 0;
2590
2591     for (i = 0; (i < nbonds); )
2592     {
2593         type = forceatoms[i++];
2594         ai   = forceatoms[i++];
2595         aj   = forceatoms[i++];
2596         ak   = forceatoms[i++];
2597         al   = forceatoms[i++];
2598
2599         phi0A  = forceparams[type].dihres.phiA*d2r;
2600         dphiA  = forceparams[type].dihres.dphiA*d2r;
2601         kfacA  = forceparams[type].dihres.kfacA;
2602
2603         phi0B  = forceparams[type].dihres.phiB*d2r;
2604         dphiB  = forceparams[type].dihres.dphiB*d2r;
2605         kfacB  = forceparams[type].dihres.kfacB;
2606
2607         phi0  = L1*phi0A + lambda*phi0B;
2608         dphi  = L1*dphiA + lambda*dphiB;
2609         kfac  = L1*kfacA + lambda*kfacB;
2610
2611         phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
2612                         &sign, &t1, &t2, &t3);
2613         /* 84 flops */
2614
2615         if (debug)
2616         {
2617             fprintf(debug, "dihres[%d]: %d %d %d %d : phi=%f, dphi=%f, kfac=%f\n",
2618                     k++, ai, aj, ak, al, phi0, dphi, kfac);
2619         }
2620         /* phi can jump if phi0 is close to Pi/-Pi, which will cause huge
2621          * force changes if we just apply a normal harmonic.
2622          * Instead, we first calculate phi-phi0 and take it modulo (-Pi,Pi).
2623          * This means we will never have the periodicity problem, unless
2624          * the dihedral is Pi away from phiO, which is very unlikely due to
2625          * the potential.
2626          */
2627         dp = phi-phi0;
2628         make_dp_periodic(&dp);
2629
2630         if (dp > dphi)
2631         {
2632             ddp = dp-dphi;
2633         }
2634         else if (dp < -dphi)
2635         {
2636             ddp = dp+dphi;
2637         }
2638         else
2639         {
2640             ddp = 0;
2641         }
2642
2643         if (ddp != 0.0)
2644         {
2645             ddp2  = ddp*ddp;
2646             vtot += 0.5*kfac*ddp2;
2647             ddphi = kfac*ddp;
2648
2649             *dvdlambda += 0.5*(kfacB - kfacA)*ddp2;
2650             /* lambda dependence from changing restraint distances */
2651             if (ddp > 0)
2652             {
2653                 *dvdlambda -= kfac*ddp*((dphiB - dphiA)+(phi0B - phi0A));
2654             }
2655             else if (ddp < 0)
2656             {
2657                 *dvdlambda += kfac*ddp*((dphiB - dphiA)-(phi0B - phi0A));
2658             }
2659             do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
2660                        f, fshift, pbc, g, x, t1, t2, t3);      /* 112           */
2661         }
2662     }
2663     return vtot;
2664 }
2665
2666
2667 real unimplemented(int gmx_unused nbonds,
2668                    const t_iatom gmx_unused forceatoms[], const t_iparams gmx_unused forceparams[],
2669                    const rvec gmx_unused x[], rvec gmx_unused f[], rvec gmx_unused fshift[],
2670                    const t_pbc gmx_unused *pbc, const t_graph  gmx_unused *g,
2671                    real gmx_unused lambda, real gmx_unused *dvdlambda,
2672                    const t_mdatoms  gmx_unused *md, t_fcdata gmx_unused *fcd,
2673                    int gmx_unused *global_atom_index)
2674 {
2675     gmx_impl("*** you are using a not implemented function");
2676
2677     return 0.0; /* To make the compiler happy */
2678 }
2679
2680 real restrangles(int nbonds,
2681                  const t_iatom forceatoms[], const t_iparams forceparams[],
2682                  const rvec x[], rvec f[], rvec fshift[],
2683                  const t_pbc *pbc, const t_graph *g,
2684                  real gmx_unused lambda, real gmx_unused *dvdlambda,
2685                  const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2686                  int gmx_unused *global_atom_index)
2687 {
2688     int  i, d, ai, aj, ak, type, m;
2689     int  t1, t2;
2690     real v, vtot;
2691     ivec jt, dt_ij, dt_kj;
2692     rvec f_i, f_j, f_k;
2693     real prefactor, ratio_ante, ratio_post;
2694     rvec delta_ante, delta_post, vec_temp;
2695
2696     vtot = 0.0;
2697     for (i = 0; (i < nbonds); )
2698     {
2699         type = forceatoms[i++];
2700         ai   = forceatoms[i++];
2701         aj   = forceatoms[i++];
2702         ak   = forceatoms[i++];
2703
2704         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2705         pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2706         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], delta_post);
2707
2708
2709         /* This function computes factors needed for restricted angle potential.
2710          * The restricted angle potential is used in coarse-grained simulations to avoid singularities
2711          * when three particles align and the dihedral angle and dihedral potential
2712          * cannot be calculated. This potential is calculated using the formula:
2713            real restrangles(int nbonds,
2714             const t_iatom forceatoms[],const t_iparams forceparams[],
2715             const rvec x[],rvec f[],rvec fshift[],
2716             const t_pbc *pbc,const t_graph *g,
2717             real gmx_unused lambda,real gmx_unused *dvdlambda,
2718             const t_mdatoms gmx_unused *md,t_fcdata gmx_unused *fcd,
2719             int gmx_unused *global_atom_index)
2720            {
2721            int  i, d, ai, aj, ak, type, m;
2722            int t1, t2;
2723            rvec r_ij,r_kj;
2724            real v, vtot;
2725            ivec jt,dt_ij,dt_kj;
2726            rvec f_i, f_j, f_k;
2727            real prefactor, ratio_ante, ratio_post;
2728            rvec delta_ante, delta_post, vec_temp;
2729
2730            vtot = 0.0;
2731            for(i=0; (i<nbonds); )
2732            {
2733            type = forceatoms[i++];
2734            ai   = forceatoms[i++];
2735            aj   = forceatoms[i++];
2736            ak   = forceatoms[i++];
2737
2738          * \f[V_{\rm ReB}(\theta_i) = \frac{1}{2} k_{\theta} \frac{(\cos\theta_i - \cos\theta_0)^2}
2739          * {\sin^2\theta_i}\f] ({eq:ReB} and ref \cite{MonicaGoga2013} from the manual).
2740          * For more explanations see comments file "restcbt.h". */
2741
2742         compute_factors_restangles(type, forceparams,  delta_ante, delta_post,
2743                                    &prefactor, &ratio_ante, &ratio_post, &v);
2744
2745         /*   Forces are computed per component */
2746         for (d = 0; d < DIM; d++)
2747         {
2748             f_i[d] = prefactor * (ratio_ante * delta_ante[d] - delta_post[d]);
2749             f_j[d] = prefactor * ((ratio_post + 1.0) * delta_post[d] - (ratio_ante + 1.0) * delta_ante[d]);
2750             f_k[d] = prefactor * (delta_ante[d] - ratio_post * delta_post[d]);
2751         }
2752
2753         /*   Computation of potential energy   */
2754
2755         vtot += v;
2756
2757         /*   Update forces */
2758
2759         for (m = 0; (m < DIM); m++)
2760         {
2761             f[ai][m] += f_i[m];
2762             f[aj][m] += f_j[m];
2763             f[ak][m] += f_k[m];
2764         }
2765
2766         if (g)
2767         {
2768             copy_ivec(SHIFT_IVEC(g, aj), jt);
2769             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
2770             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
2771             t1 = IVEC2IS(dt_ij);
2772             t2 = IVEC2IS(dt_kj);
2773         }
2774
2775         rvec_inc(fshift[t1], f_i);
2776         rvec_inc(fshift[CENTRAL], f_j);
2777         rvec_inc(fshift[t2], f_k);
2778     }
2779     return vtot;
2780 }
2781
2782
2783 real restrdihs(int nbonds,
2784                const t_iatom forceatoms[], const t_iparams forceparams[],
2785                const rvec x[], rvec f[], rvec fshift[],
2786                const t_pbc *pbc, const t_graph *g,
2787                real gmx_unused lambda, real gmx_unused *dvlambda,
2788                const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2789                int gmx_unused *global_atom_index)
2790 {
2791     int  i, d, type, ai, aj, ak, al;
2792     rvec f_i, f_j, f_k, f_l;
2793     rvec dx_jl;
2794     ivec jt, dt_ij, dt_kj, dt_lj;
2795     int  t1, t2, t3;
2796     real v, vtot;
2797     rvec delta_ante,  delta_crnt, delta_post, vec_temp;
2798     real factor_phi_ai_ante, factor_phi_ai_crnt, factor_phi_ai_post;
2799     real factor_phi_aj_ante, factor_phi_aj_crnt, factor_phi_aj_post;
2800     real factor_phi_ak_ante, factor_phi_ak_crnt, factor_phi_ak_post;
2801     real factor_phi_al_ante, factor_phi_al_crnt, factor_phi_al_post;
2802     real prefactor_phi;
2803
2804
2805     vtot = 0.0;
2806     for (i = 0; (i < nbonds); )
2807     {
2808         type = forceatoms[i++];
2809         ai   = forceatoms[i++];
2810         aj   = forceatoms[i++];
2811         ak   = forceatoms[i++];
2812         al   = forceatoms[i++];
2813
2814         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2815         pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2816         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
2817         pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
2818         pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
2819
2820         /* This function computes factors needed for restricted angle potential.
2821          * The restricted angle potential is used in coarse-grained simulations to avoid singularities
2822          * when three particles align and the dihedral angle and dihedral potential cannot be calculated.
2823          * This potential is calculated using the formula:
2824          * \f[V_{\rm ReB}(\theta_i) = \frac{1}{2} k_{\theta}
2825          * \frac{(\cos\theta_i - \cos\theta_0)^2}{\sin^2\theta_i}\f]
2826          * ({eq:ReB} and ref \cite{MonicaGoga2013} from the manual).
2827          * For more explanations see comments file "restcbt.h" */
2828
2829         compute_factors_restrdihs(type, forceparams,
2830                                   delta_ante, delta_crnt, delta_post,
2831                                   &factor_phi_ai_ante, &factor_phi_ai_crnt, &factor_phi_ai_post,
2832                                   &factor_phi_aj_ante, &factor_phi_aj_crnt, &factor_phi_aj_post,
2833                                   &factor_phi_ak_ante, &factor_phi_ak_crnt, &factor_phi_ak_post,
2834                                   &factor_phi_al_ante, &factor_phi_al_crnt, &factor_phi_al_post,
2835                                   &prefactor_phi, &v);
2836
2837
2838         /*      Computation of forces per component */
2839         for (d = 0; d < DIM; d++)
2840         {
2841             f_i[d] = prefactor_phi * (factor_phi_ai_ante * delta_ante[d] + factor_phi_ai_crnt * delta_crnt[d] + factor_phi_ai_post * delta_post[d]);
2842             f_j[d] = prefactor_phi * (factor_phi_aj_ante * delta_ante[d] + factor_phi_aj_crnt * delta_crnt[d] + factor_phi_aj_post * delta_post[d]);
2843             f_k[d] = prefactor_phi * (factor_phi_ak_ante * delta_ante[d] + factor_phi_ak_crnt * delta_crnt[d] + factor_phi_ak_post * delta_post[d]);
2844             f_l[d] = prefactor_phi * (factor_phi_al_ante * delta_ante[d] + factor_phi_al_crnt * delta_crnt[d] + factor_phi_al_post * delta_post[d]);
2845         }
2846         /*      Computation of the energy */
2847
2848         vtot += v;
2849
2850
2851
2852         /*    Updating the forces */
2853
2854         rvec_inc(f[ai], f_i);
2855         rvec_inc(f[aj], f_j);
2856         rvec_inc(f[ak], f_k);
2857         rvec_inc(f[al], f_l);
2858
2859
2860         /* Updating the fshift forces for the pressure coupling */
2861         if (g)
2862         {
2863             copy_ivec(SHIFT_IVEC(g, aj), jt);
2864             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
2865             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
2866             ivec_sub(SHIFT_IVEC(g, al), jt, dt_lj);
2867             t1 = IVEC2IS(dt_ij);
2868             t2 = IVEC2IS(dt_kj);
2869             t3 = IVEC2IS(dt_lj);
2870         }
2871         else if (pbc)
2872         {
2873             t3 = pbc_rvec_sub(pbc, x[al], x[aj], dx_jl);
2874         }
2875         else
2876         {
2877             t3 = CENTRAL;
2878         }
2879
2880         rvec_inc(fshift[t1], f_i);
2881         rvec_inc(fshift[CENTRAL], f_j);
2882         rvec_inc(fshift[t2], f_k);
2883         rvec_inc(fshift[t3], f_l);
2884
2885     }
2886
2887     return vtot;
2888 }
2889
2890
2891 real cbtdihs(int nbonds,
2892              const t_iatom forceatoms[], const t_iparams forceparams[],
2893              const rvec x[], rvec f[], rvec fshift[],
2894              const t_pbc *pbc, const t_graph *g,
2895              real gmx_unused lambda, real gmx_unused *dvdlambda,
2896              const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
2897              int gmx_unused *global_atom_index)
2898 {
2899     int  type, ai, aj, ak, al, i, d;
2900     int  t1, t2, t3;
2901     real v, vtot;
2902     rvec vec_temp;
2903     rvec f_i, f_j, f_k, f_l;
2904     ivec jt, dt_ij, dt_kj, dt_lj;
2905     rvec dx_jl;
2906     rvec delta_ante, delta_crnt, delta_post;
2907     rvec f_phi_ai, f_phi_aj, f_phi_ak, f_phi_al;
2908     rvec f_theta_ante_ai, f_theta_ante_aj, f_theta_ante_ak;
2909     rvec f_theta_post_aj, f_theta_post_ak, f_theta_post_al;
2910
2911
2912
2913
2914     vtot = 0.0;
2915     for (i = 0; (i < nbonds); )
2916     {
2917         type = forceatoms[i++];
2918         ai   = forceatoms[i++];
2919         aj   = forceatoms[i++];
2920         ak   = forceatoms[i++];
2921         al   = forceatoms[i++];
2922
2923
2924         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
2925         pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
2926         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], vec_temp);
2927         pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
2928         pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
2929         pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
2930
2931         /* \brief Compute factors for CBT potential
2932          * The combined bending-torsion potential goes to zero in a very smooth manner, eliminating the numerical
2933          * instabilities, when three coarse-grained particles align and the dihedral angle and standard
2934          * dihedral potentials cannot be calculated. The CBT potential is calculated using the formula:
2935          * \f[V_{\rm CBT}(\theta_{i-1}, \theta_i, \phi_i) = k_{\phi} \sin^3\theta_{i-1} \sin^3\theta_{i}
2936          * \sum_{n=0}^4 { a_n \cos^n\phi_i}\f] ({eq:CBT} and ref \cite{MonicaGoga2013} from the manual).
2937          * It contains in its expression not only the dihedral angle \f$\phi\f$
2938          * but also \f[\theta_{i-1}\f] (theta_ante bellow) and \f[\theta_{i}\f] (theta_post bellow)
2939          * --- the adjacent bending angles.
2940          * For more explanations see comments file "restcbt.h". */
2941
2942         compute_factors_cbtdihs(type, forceparams, delta_ante, delta_crnt, delta_post,
2943                                 f_phi_ai, f_phi_aj, f_phi_ak, f_phi_al,
2944                                 f_theta_ante_ai, f_theta_ante_aj, f_theta_ante_ak,
2945                                 f_theta_post_aj, f_theta_post_ak, f_theta_post_al,
2946                                 &v);
2947
2948
2949         /*      Acumulate the resuts per beads */
2950         for (d = 0; d < DIM; d++)
2951         {
2952             f_i[d] = f_phi_ai[d] + f_theta_ante_ai[d];
2953             f_j[d] = f_phi_aj[d] + f_theta_ante_aj[d] + f_theta_post_aj[d];
2954             f_k[d] = f_phi_ak[d] + f_theta_ante_ak[d] + f_theta_post_ak[d];
2955             f_l[d] = f_phi_al[d] + f_theta_post_al[d];
2956         }
2957
2958         /*      Compute the potential energy */
2959
2960         vtot += v;
2961
2962
2963         /*  Updating the forces */
2964         rvec_inc(f[ai], f_i);
2965         rvec_inc(f[aj], f_j);
2966         rvec_inc(f[ak], f_k);
2967         rvec_inc(f[al], f_l);
2968
2969
2970         /* Updating the fshift forces for the pressure coupling */
2971         if (g)
2972         {
2973             copy_ivec(SHIFT_IVEC(g, aj), jt);
2974             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
2975             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
2976             ivec_sub(SHIFT_IVEC(g, al), jt, dt_lj);
2977             t1 = IVEC2IS(dt_ij);
2978             t2 = IVEC2IS(dt_kj);
2979             t3 = IVEC2IS(dt_lj);
2980         }
2981         else if (pbc)
2982         {
2983             t3 = pbc_rvec_sub(pbc, x[al], x[aj], dx_jl);
2984         }
2985         else
2986         {
2987             t3 = CENTRAL;
2988         }
2989
2990         rvec_inc(fshift[t1], f_i);
2991         rvec_inc(fshift[CENTRAL], f_j);
2992         rvec_inc(fshift[t2], f_k);
2993         rvec_inc(fshift[t3], f_l);
2994     }
2995
2996     return vtot;
2997 }
2998
2999 real rbdihs(int nbonds,
3000             const t_iatom forceatoms[], const t_iparams forceparams[],
3001             const rvec x[], rvec f[], rvec fshift[],
3002             const t_pbc *pbc, const t_graph *g,
3003             real lambda, real *dvdlambda,
3004             const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3005             int gmx_unused *global_atom_index)
3006 {
3007     const real c0 = 0.0, c1 = 1.0, c2 = 2.0, c3 = 3.0, c4 = 4.0, c5 = 5.0;
3008     int        type, ai, aj, ak, al, i, j;
3009     int        t1, t2, t3;
3010     rvec       r_ij, r_kj, r_kl, m, n;
3011     real       parmA[NR_RBDIHS];
3012     real       parmB[NR_RBDIHS];
3013     real       parm[NR_RBDIHS];
3014     real       cos_phi, phi, rbp, rbpBA;
3015     real       v, sign, ddphi, sin_phi;
3016     real       cosfac, vtot;
3017     real       L1        = 1.0-lambda;
3018     real       dvdl_term = 0;
3019
3020     vtot = 0.0;
3021     for (i = 0; (i < nbonds); )
3022     {
3023         type = forceatoms[i++];
3024         ai   = forceatoms[i++];
3025         aj   = forceatoms[i++];
3026         ak   = forceatoms[i++];
3027         al   = forceatoms[i++];
3028
3029         phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
3030                         &sign, &t1, &t2, &t3);  /*  84          */
3031
3032         /* Change to polymer convention */
3033         if (phi < c0)
3034         {
3035             phi += M_PI;
3036         }
3037         else
3038         {
3039             phi -= M_PI;    /*   1              */
3040
3041         }
3042         cos_phi = cos(phi);
3043         /* Beware of accuracy loss, cannot use 1-sqrt(cos^2) ! */
3044         sin_phi = sin(phi);
3045
3046         for (j = 0; (j < NR_RBDIHS); j++)
3047         {
3048             parmA[j] = forceparams[type].rbdihs.rbcA[j];
3049             parmB[j] = forceparams[type].rbdihs.rbcB[j];
3050             parm[j]  = L1*parmA[j]+lambda*parmB[j];
3051         }
3052         /* Calculate cosine powers */
3053         /* Calculate the energy */
3054         /* Calculate the derivative */
3055
3056         v            = parm[0];
3057         dvdl_term   += (parmB[0]-parmA[0]);
3058         ddphi        = c0;
3059         cosfac       = c1;
3060
3061         rbp          = parm[1];
3062         rbpBA        = parmB[1]-parmA[1];
3063         ddphi       += rbp*cosfac;
3064         cosfac      *= cos_phi;
3065         v           += cosfac*rbp;
3066         dvdl_term   += cosfac*rbpBA;
3067         rbp          = parm[2];
3068         rbpBA        = parmB[2]-parmA[2];
3069         ddphi       += c2*rbp*cosfac;
3070         cosfac      *= cos_phi;
3071         v           += cosfac*rbp;
3072         dvdl_term   += cosfac*rbpBA;
3073         rbp          = parm[3];
3074         rbpBA        = parmB[3]-parmA[3];
3075         ddphi       += c3*rbp*cosfac;
3076         cosfac      *= cos_phi;
3077         v           += cosfac*rbp;
3078         dvdl_term   += cosfac*rbpBA;
3079         rbp          = parm[4];
3080         rbpBA        = parmB[4]-parmA[4];
3081         ddphi       += c4*rbp*cosfac;
3082         cosfac      *= cos_phi;
3083         v           += cosfac*rbp;
3084         dvdl_term   += cosfac*rbpBA;
3085         rbp          = parm[5];
3086         rbpBA        = parmB[5]-parmA[5];
3087         ddphi       += c5*rbp*cosfac;
3088         cosfac      *= cos_phi;
3089         v           += cosfac*rbp;
3090         dvdl_term   += cosfac*rbpBA;
3091
3092         ddphi = -ddphi*sin_phi;         /*  11          */
3093
3094         do_dih_fup(ai, aj, ak, al, ddphi, r_ij, r_kj, r_kl, m, n,
3095                    f, fshift, pbc, g, x, t1, t2, t3); /* 112            */
3096         vtot += v;
3097     }
3098     *dvdlambda += dvdl_term;
3099
3100     return vtot;
3101 }
3102
3103 int cmap_setup_grid_index(int ip, int grid_spacing, int *ipm1, int *ipp1, int *ipp2)
3104 {
3105     int im1, ip1, ip2;
3106
3107     if (ip < 0)
3108     {
3109         ip = ip + grid_spacing - 1;
3110     }
3111     else if (ip > grid_spacing)
3112     {
3113         ip = ip - grid_spacing - 1;
3114     }
3115
3116     im1 = ip - 1;
3117     ip1 = ip + 1;
3118     ip2 = ip + 2;
3119
3120     if (ip == 0)
3121     {
3122         im1 = grid_spacing - 1;
3123     }
3124     else if (ip == grid_spacing-2)
3125     {
3126         ip2 = 0;
3127     }
3128     else if (ip == grid_spacing-1)
3129     {
3130         ip1 = 0;
3131         ip2 = 1;
3132     }
3133
3134     *ipm1 = im1;
3135     *ipp1 = ip1;
3136     *ipp2 = ip2;
3137
3138     return ip;
3139
3140 }
3141
3142 real cmap_dihs(int nbonds,
3143                const t_iatom forceatoms[], const t_iparams forceparams[],
3144                const gmx_cmap_t *cmap_grid,
3145                const rvec x[], rvec f[], rvec fshift[],
3146                const t_pbc *pbc, const t_graph *g,
3147                real gmx_unused lambda, real gmx_unused *dvdlambda,
3148                const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3149                int  gmx_unused *global_atom_index)
3150 {
3151     int         i, j, k, n, idx;
3152     int         ai, aj, ak, al, am;
3153     int         a1i, a1j, a1k, a1l, a2i, a2j, a2k, a2l;
3154     int         type, cmapA;
3155     int         t11, t21, t31, t12, t22, t32;
3156     int         iphi1, ip1m1, ip1p1, ip1p2;
3157     int         iphi2, ip2m1, ip2p1, ip2p2;
3158     int         l1, l2, l3;
3159     int         pos1, pos2, pos3, pos4;
3160
3161     real        ty[4], ty1[4], ty2[4], ty12[4], tc[16], tx[16];
3162     real        phi1, cos_phi1, sin_phi1, sign1, xphi1;
3163     real        phi2, cos_phi2, sin_phi2, sign2, xphi2;
3164     real        dx, xx, tt, tu, e, df1, df2, vtot;
3165     real        ra21, rb21, rg21, rg1, rgr1, ra2r1, rb2r1, rabr1;
3166     real        ra22, rb22, rg22, rg2, rgr2, ra2r2, rb2r2, rabr2;
3167     real        fg1, hg1, fga1, hgb1, gaa1, gbb1;
3168     real        fg2, hg2, fga2, hgb2, gaa2, gbb2;
3169     real        fac;
3170
3171     rvec        r1_ij, r1_kj, r1_kl, m1, n1;
3172     rvec        r2_ij, r2_kj, r2_kl, m2, n2;
3173     rvec        f1_i, f1_j, f1_k, f1_l;
3174     rvec        f2_i, f2_j, f2_k, f2_l;
3175     rvec        a1, b1, a2, b2;
3176     rvec        f1, g1, h1, f2, g2, h2;
3177     rvec        dtf1, dtg1, dth1, dtf2, dtg2, dth2;
3178     ivec        jt1, dt1_ij, dt1_kj, dt1_lj;
3179     ivec        jt2, dt2_ij, dt2_kj, dt2_lj;
3180
3181     const real *cmapd;
3182
3183     int         loop_index[4][4] = {
3184         {0, 4, 8, 12},
3185         {1, 5, 9, 13},
3186         {2, 6, 10, 14},
3187         {3, 7, 11, 15}
3188     };
3189
3190     /* Total CMAP energy */
3191     vtot = 0;
3192
3193     for (n = 0; n < nbonds; )
3194     {
3195         /* Five atoms are involved in the two torsions */
3196         type   = forceatoms[n++];
3197         ai     = forceatoms[n++];
3198         aj     = forceatoms[n++];
3199         ak     = forceatoms[n++];
3200         al     = forceatoms[n++];
3201         am     = forceatoms[n++];
3202
3203         /* Which CMAP type is this */
3204         cmapA = forceparams[type].cmap.cmapA;
3205         cmapd = cmap_grid->cmapdata[cmapA].cmap;
3206
3207         /* First torsion */
3208         a1i   = ai;
3209         a1j   = aj;
3210         a1k   = ak;
3211         a1l   = al;
3212
3213         phi1  = dih_angle(x[a1i], x[a1j], x[a1k], x[a1l], pbc, r1_ij, r1_kj, r1_kl, m1, n1,
3214                           &sign1, &t11, &t21, &t31);  /* 84 */
3215
3216         cos_phi1 = cos(phi1);
3217
3218         a1[0] = r1_ij[1]*r1_kj[2]-r1_ij[2]*r1_kj[1];
3219         a1[1] = r1_ij[2]*r1_kj[0]-r1_ij[0]*r1_kj[2];
3220         a1[2] = r1_ij[0]*r1_kj[1]-r1_ij[1]*r1_kj[0]; /* 9 */
3221
3222         b1[0] = r1_kl[1]*r1_kj[2]-r1_kl[2]*r1_kj[1];
3223         b1[1] = r1_kl[2]*r1_kj[0]-r1_kl[0]*r1_kj[2];
3224         b1[2] = r1_kl[0]*r1_kj[1]-r1_kl[1]*r1_kj[0]; /* 9 */
3225
3226         pbc_rvec_sub(pbc, x[a1l], x[a1k], h1);
3227
3228         ra21  = iprod(a1, a1);       /* 5 */
3229         rb21  = iprod(b1, b1);       /* 5 */
3230         rg21  = iprod(r1_kj, r1_kj); /* 5 */
3231         rg1   = sqrt(rg21);
3232
3233         rgr1  = 1.0/rg1;
3234         ra2r1 = 1.0/ra21;
3235         rb2r1 = 1.0/rb21;
3236         rabr1 = sqrt(ra2r1*rb2r1);
3237
3238         sin_phi1 = rg1 * rabr1 * iprod(a1, h1) * (-1);
3239
3240         if (cos_phi1 < -0.5 || cos_phi1 > 0.5)
3241         {
3242             phi1 = asin(sin_phi1);
3243
3244             if (cos_phi1 < 0)
3245             {
3246                 if (phi1 > 0)
3247                 {
3248                     phi1 = M_PI - phi1;
3249                 }
3250                 else
3251                 {
3252                     phi1 = -M_PI - phi1;
3253                 }
3254             }
3255         }
3256         else
3257         {
3258             phi1 = acos(cos_phi1);
3259
3260             if (sin_phi1 < 0)
3261             {
3262                 phi1 = -phi1;
3263             }
3264         }
3265
3266         xphi1 = phi1 + M_PI; /* 1 */
3267
3268         /* Second torsion */
3269         a2i   = aj;
3270         a2j   = ak;
3271         a2k   = al;
3272         a2l   = am;
3273
3274         phi2  = dih_angle(x[a2i], x[a2j], x[a2k], x[a2l], pbc, r2_ij, r2_kj, r2_kl, m2, n2,
3275                           &sign2, &t12, &t22, &t32); /* 84 */
3276
3277         cos_phi2 = cos(phi2);
3278
3279         a2[0] = r2_ij[1]*r2_kj[2]-r2_ij[2]*r2_kj[1];
3280         a2[1] = r2_ij[2]*r2_kj[0]-r2_ij[0]*r2_kj[2];
3281         a2[2] = r2_ij[0]*r2_kj[1]-r2_ij[1]*r2_kj[0]; /* 9 */
3282
3283         b2[0] = r2_kl[1]*r2_kj[2]-r2_kl[2]*r2_kj[1];
3284         b2[1] = r2_kl[2]*r2_kj[0]-r2_kl[0]*r2_kj[2];
3285         b2[2] = r2_kl[0]*r2_kj[1]-r2_kl[1]*r2_kj[0]; /* 9 */
3286
3287         pbc_rvec_sub(pbc, x[a2l], x[a2k], h2);
3288
3289         ra22  = iprod(a2, a2);         /* 5 */
3290         rb22  = iprod(b2, b2);         /* 5 */
3291         rg22  = iprod(r2_kj, r2_kj);   /* 5 */
3292         rg2   = sqrt(rg22);
3293
3294         rgr2  = 1.0/rg2;
3295         ra2r2 = 1.0/ra22;
3296         rb2r2 = 1.0/rb22;
3297         rabr2 = sqrt(ra2r2*rb2r2);
3298
3299         sin_phi2 = rg2 * rabr2 * iprod(a2, h2) * (-1);
3300
3301         if (cos_phi2 < -0.5 || cos_phi2 > 0.5)
3302         {
3303             phi2 = asin(sin_phi2);
3304
3305             if (cos_phi2 < 0)
3306             {
3307                 if (phi2 > 0)
3308                 {
3309                     phi2 = M_PI - phi2;
3310                 }
3311                 else
3312                 {
3313                     phi2 = -M_PI - phi2;
3314                 }
3315             }
3316         }
3317         else
3318         {
3319             phi2 = acos(cos_phi2);
3320
3321             if (sin_phi2 < 0)
3322             {
3323                 phi2 = -phi2;
3324             }
3325         }
3326
3327         xphi2 = phi2 + M_PI; /* 1 */
3328
3329         /* Range mangling */
3330         if (xphi1 < 0)
3331         {
3332             xphi1 = xphi1 + 2*M_PI;
3333         }
3334         else if (xphi1 >= 2*M_PI)
3335         {
3336             xphi1 = xphi1 - 2*M_PI;
3337         }
3338
3339         if (xphi2 < 0)
3340         {
3341             xphi2 = xphi2 + 2*M_PI;
3342         }
3343         else if (xphi2 >= 2*M_PI)
3344         {
3345             xphi2 = xphi2 - 2*M_PI;
3346         }
3347
3348         /* Number of grid points */
3349         dx = 2*M_PI / cmap_grid->grid_spacing;
3350
3351         /* Where on the grid are we */
3352         iphi1 = static_cast<int>(xphi1/dx);
3353         iphi2 = static_cast<int>(xphi2/dx);
3354
3355         iphi1 = cmap_setup_grid_index(iphi1, cmap_grid->grid_spacing, &ip1m1, &ip1p1, &ip1p2);
3356         iphi2 = cmap_setup_grid_index(iphi2, cmap_grid->grid_spacing, &ip2m1, &ip2p1, &ip2p2);
3357
3358         pos1    = iphi1*cmap_grid->grid_spacing+iphi2;
3359         pos2    = ip1p1*cmap_grid->grid_spacing+iphi2;
3360         pos3    = ip1p1*cmap_grid->grid_spacing+ip2p1;
3361         pos4    = iphi1*cmap_grid->grid_spacing+ip2p1;
3362
3363         ty[0]   = cmapd[pos1*4];
3364         ty[1]   = cmapd[pos2*4];
3365         ty[2]   = cmapd[pos3*4];
3366         ty[3]   = cmapd[pos4*4];
3367
3368         ty1[0]   = cmapd[pos1*4+1];
3369         ty1[1]   = cmapd[pos2*4+1];
3370         ty1[2]   = cmapd[pos3*4+1];
3371         ty1[3]   = cmapd[pos4*4+1];
3372
3373         ty2[0]   = cmapd[pos1*4+2];
3374         ty2[1]   = cmapd[pos2*4+2];
3375         ty2[2]   = cmapd[pos3*4+2];
3376         ty2[3]   = cmapd[pos4*4+2];
3377
3378         ty12[0]   = cmapd[pos1*4+3];
3379         ty12[1]   = cmapd[pos2*4+3];
3380         ty12[2]   = cmapd[pos3*4+3];
3381         ty12[3]   = cmapd[pos4*4+3];
3382
3383         /* Switch to degrees */
3384         dx    = 360.0 / cmap_grid->grid_spacing;
3385         xphi1 = xphi1 * RAD2DEG;
3386         xphi2 = xphi2 * RAD2DEG;
3387
3388         for (i = 0; i < 4; i++) /* 16 */
3389         {
3390             tx[i]    = ty[i];
3391             tx[i+4]  = ty1[i]*dx;
3392             tx[i+8]  = ty2[i]*dx;
3393             tx[i+12] = ty12[i]*dx*dx;
3394         }
3395
3396         idx = 0;
3397         for (i = 0; i < 4; i++) /* 1056 */
3398         {
3399             for (j = 0; j < 4; j++)
3400             {
3401                 xx = 0;
3402                 for (k = 0; k < 16; k++)
3403                 {
3404                     xx = xx + cmap_coeff_matrix[k*16+idx]*tx[k];
3405                 }
3406
3407                 idx++;
3408                 tc[i*4+j] = xx;
3409             }
3410         }
3411
3412         tt    = (xphi1-iphi1*dx)/dx;
3413         tu    = (xphi2-iphi2*dx)/dx;
3414
3415         e     = 0;
3416         df1   = 0;
3417         df2   = 0;
3418
3419         for (i = 3; i >= 0; i--)
3420         {
3421             l1 = loop_index[i][3];
3422             l2 = loop_index[i][2];
3423             l3 = loop_index[i][1];
3424
3425             e     = tt * e    + ((tc[i*4+3]*tu+tc[i*4+2])*tu + tc[i*4+1])*tu+tc[i*4];
3426             df1   = tu * df1  + (3.0*tc[l1]*tt+2.0*tc[l2])*tt+tc[l3];
3427             df2   = tt * df2  + (3.0*tc[i*4+3]*tu+2.0*tc[i*4+2])*tu+tc[i*4+1];
3428         }
3429
3430         fac     = RAD2DEG/dx;
3431         df1     = df1   * fac;
3432         df2     = df2   * fac;
3433
3434         /* CMAP energy */
3435         vtot += e;
3436
3437         /* Do forces - first torsion */
3438         fg1       = iprod(r1_ij, r1_kj);
3439         hg1       = iprod(r1_kl, r1_kj);
3440         fga1      = fg1*ra2r1*rgr1;
3441         hgb1      = hg1*rb2r1*rgr1;
3442         gaa1      = -ra2r1*rg1;
3443         gbb1      = rb2r1*rg1;
3444
3445         for (i = 0; i < DIM; i++)
3446         {
3447             dtf1[i]   = gaa1 * a1[i];
3448             dtg1[i]   = fga1 * a1[i] - hgb1 * b1[i];
3449             dth1[i]   = gbb1 * b1[i];
3450
3451             f1[i]     = df1  * dtf1[i];
3452             g1[i]     = df1  * dtg1[i];
3453             h1[i]     = df1  * dth1[i];
3454
3455             f1_i[i]   =  f1[i];
3456             f1_j[i]   = -f1[i] - g1[i];
3457             f1_k[i]   =  h1[i] + g1[i];
3458             f1_l[i]   = -h1[i];
3459
3460             f[a1i][i] = f[a1i][i] + f1_i[i];
3461             f[a1j][i] = f[a1j][i] + f1_j[i]; /* - f1[i] - g1[i] */
3462             f[a1k][i] = f[a1k][i] + f1_k[i]; /* h1[i] + g1[i] */
3463             f[a1l][i] = f[a1l][i] + f1_l[i]; /* h1[i] */
3464         }
3465
3466         /* Do forces - second torsion */
3467         fg2       = iprod(r2_ij, r2_kj);
3468         hg2       = iprod(r2_kl, r2_kj);
3469         fga2      = fg2*ra2r2*rgr2;
3470         hgb2      = hg2*rb2r2*rgr2;
3471         gaa2      = -ra2r2*rg2;
3472         gbb2      = rb2r2*rg2;
3473
3474         for (i = 0; i < DIM; i++)
3475         {
3476             dtf2[i]   = gaa2 * a2[i];
3477             dtg2[i]   = fga2 * a2[i] - hgb2 * b2[i];
3478             dth2[i]   = gbb2 * b2[i];
3479
3480             f2[i]     = df2  * dtf2[i];
3481             g2[i]     = df2  * dtg2[i];
3482             h2[i]     = df2  * dth2[i];
3483
3484             f2_i[i]   =  f2[i];
3485             f2_j[i]   = -f2[i] - g2[i];
3486             f2_k[i]   =  h2[i] + g2[i];
3487             f2_l[i]   = -h2[i];
3488
3489             f[a2i][i] = f[a2i][i] + f2_i[i]; /* f2[i] */
3490             f[a2j][i] = f[a2j][i] + f2_j[i]; /* - f2[i] - g2[i] */
3491             f[a2k][i] = f[a2k][i] + f2_k[i]; /* h2[i] + g2[i] */
3492             f[a2l][i] = f[a2l][i] + f2_l[i]; /* - h2[i] */
3493         }
3494
3495         /* Shift forces */
3496         if (g)
3497         {
3498             copy_ivec(SHIFT_IVEC(g, a1j), jt1);
3499             ivec_sub(SHIFT_IVEC(g, a1i),  jt1, dt1_ij);
3500             ivec_sub(SHIFT_IVEC(g, a1k),  jt1, dt1_kj);
3501             ivec_sub(SHIFT_IVEC(g, a1l),  jt1, dt1_lj);
3502             t11 = IVEC2IS(dt1_ij);
3503             t21 = IVEC2IS(dt1_kj);
3504             t31 = IVEC2IS(dt1_lj);
3505
3506             copy_ivec(SHIFT_IVEC(g, a2j), jt2);
3507             ivec_sub(SHIFT_IVEC(g, a2i),  jt2, dt2_ij);
3508             ivec_sub(SHIFT_IVEC(g, a2k),  jt2, dt2_kj);
3509             ivec_sub(SHIFT_IVEC(g, a2l),  jt2, dt2_lj);
3510             t12 = IVEC2IS(dt2_ij);
3511             t22 = IVEC2IS(dt2_kj);
3512             t32 = IVEC2IS(dt2_lj);
3513         }
3514         else if (pbc)
3515         {
3516             t31 = pbc_rvec_sub(pbc, x[a1l], x[a1j], h1);
3517             t32 = pbc_rvec_sub(pbc, x[a2l], x[a2j], h2);
3518         }
3519         else
3520         {
3521             t31 = CENTRAL;
3522             t32 = CENTRAL;
3523         }
3524
3525         rvec_inc(fshift[t11], f1_i);
3526         rvec_inc(fshift[CENTRAL], f1_j);
3527         rvec_inc(fshift[t21], f1_k);
3528         rvec_inc(fshift[t31], f1_l);
3529
3530         rvec_inc(fshift[t21], f2_i);
3531         rvec_inc(fshift[CENTRAL], f2_j);
3532         rvec_inc(fshift[t22], f2_k);
3533         rvec_inc(fshift[t32], f2_l);
3534     }
3535     return vtot;
3536 }
3537
3538
3539
3540 /***********************************************************
3541  *
3542  *   G R O M O S  9 6   F U N C T I O N S
3543  *
3544  ***********************************************************/
3545 real g96harmonic(real kA, real kB, real xA, real xB, real x, real lambda,
3546                  real *V, real *F)
3547 {
3548     const real half = 0.5;
3549     real       L1, kk, x0, dx, dx2;
3550     real       v, f, dvdlambda;
3551
3552     L1    = 1.0-lambda;
3553     kk    = L1*kA+lambda*kB;
3554     x0    = L1*xA+lambda*xB;
3555
3556     dx    = x-x0;
3557     dx2   = dx*dx;
3558
3559     f          = -kk*dx;
3560     v          = half*kk*dx2;
3561     dvdlambda  = half*(kB-kA)*dx2 + (xA-xB)*kk*dx;
3562
3563     *F    = f;
3564     *V    = v;
3565
3566     return dvdlambda;
3567
3568     /* That was 21 flops */
3569 }
3570
3571 real g96bonds(int nbonds,
3572               const t_iatom forceatoms[], const t_iparams forceparams[],
3573               const rvec x[], rvec f[], rvec fshift[],
3574               const t_pbc *pbc, const t_graph *g,
3575               real lambda, real *dvdlambda,
3576               const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3577               int gmx_unused *global_atom_index)
3578 {
3579     int  i, m, ki, ai, aj, type;
3580     real dr2, fbond, vbond, fij, vtot;
3581     rvec dx;
3582     ivec dt;
3583
3584     vtot = 0.0;
3585     for (i = 0; (i < nbonds); )
3586     {
3587         type = forceatoms[i++];
3588         ai   = forceatoms[i++];
3589         aj   = forceatoms[i++];
3590
3591         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /*   3      */
3592         dr2  = iprod(dx, dx);                       /*   5              */
3593
3594         *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3595                                   forceparams[type].harmonic.krB,
3596                                   forceparams[type].harmonic.rA,
3597                                   forceparams[type].harmonic.rB,
3598                                   dr2, lambda, &vbond, &fbond);
3599
3600         vtot  += 0.5*vbond;                         /* 1*/
3601 #ifdef DEBUG
3602         if (debug)
3603         {
3604             fprintf(debug, "G96-BONDS: dr = %10g  vbond = %10g  fbond = %10g\n",
3605                     sqrt(dr2), vbond, fbond);
3606         }
3607 #endif
3608
3609         if (g)
3610         {
3611             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3612             ki = IVEC2IS(dt);
3613         }
3614         for (m = 0; (m < DIM); m++)     /*  15          */
3615         {
3616             fij                 = fbond*dx[m];
3617             f[ai][m]           += fij;
3618             f[aj][m]           -= fij;
3619             fshift[ki][m]      += fij;
3620             fshift[CENTRAL][m] -= fij;
3621         }
3622     }               /* 44 TOTAL */
3623     return vtot;
3624 }
3625
3626 real g96bond_angle(const rvec xi, const rvec xj, const rvec xk, const t_pbc *pbc,
3627                    rvec r_ij, rvec r_kj,
3628                    int *t1, int *t2)
3629 /* Return value is the angle between the bonds i-j and j-k */
3630 {
3631     real costh;
3632
3633     *t1 = pbc_rvec_sub(pbc, xi, xj, r_ij); /*  3                */
3634     *t2 = pbc_rvec_sub(pbc, xk, xj, r_kj); /*  3                */
3635
3636     costh = cos_angle(r_ij, r_kj);         /* 25                */
3637     /* 41 TOTAL */
3638     return costh;
3639 }
3640
3641 real g96angles(int nbonds,
3642                const t_iatom forceatoms[], const t_iparams forceparams[],
3643                const rvec x[], rvec f[], rvec fshift[],
3644                const t_pbc *pbc, const t_graph *g,
3645                real lambda, real *dvdlambda,
3646                const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3647                int gmx_unused *global_atom_index)
3648 {
3649     int  i, ai, aj, ak, type, m, t1, t2;
3650     rvec r_ij, r_kj;
3651     real cos_theta, dVdt, va, vtot;
3652     real rij_1, rij_2, rkj_1, rkj_2, rijrkj_1;
3653     rvec f_i, f_j, f_k;
3654     ivec jt, dt_ij, dt_kj;
3655
3656     vtot = 0.0;
3657     for (i = 0; (i < nbonds); )
3658     {
3659         type = forceatoms[i++];
3660         ai   = forceatoms[i++];
3661         aj   = forceatoms[i++];
3662         ak   = forceatoms[i++];
3663
3664         cos_theta  = g96bond_angle(x[ai], x[aj], x[ak], pbc, r_ij, r_kj, &t1, &t2);
3665
3666         *dvdlambda += g96harmonic(forceparams[type].harmonic.krA,
3667                                   forceparams[type].harmonic.krB,
3668                                   forceparams[type].harmonic.rA,
3669                                   forceparams[type].harmonic.rB,
3670                                   cos_theta, lambda, &va, &dVdt);
3671         vtot    += va;
3672
3673         rij_1    = gmx_invsqrt(iprod(r_ij, r_ij));
3674         rkj_1    = gmx_invsqrt(iprod(r_kj, r_kj));
3675         rij_2    = rij_1*rij_1;
3676         rkj_2    = rkj_1*rkj_1;
3677         rijrkj_1 = rij_1*rkj_1;                 /* 23 */
3678
3679 #ifdef DEBUG
3680         if (debug)
3681         {
3682             fprintf(debug, "G96ANGLES: costheta = %10g  vth = %10g  dV/dct = %10g\n",
3683                     cos_theta, va, dVdt);
3684         }
3685 #endif
3686         for (m = 0; (m < DIM); m++)     /*  42  */
3687         {
3688             f_i[m]    = dVdt*(r_kj[m]*rijrkj_1 - r_ij[m]*rij_2*cos_theta);
3689             f_k[m]    = dVdt*(r_ij[m]*rijrkj_1 - r_kj[m]*rkj_2*cos_theta);
3690             f_j[m]    = -f_i[m]-f_k[m];
3691             f[ai][m] += f_i[m];
3692             f[aj][m] += f_j[m];
3693             f[ak][m] += f_k[m];
3694         }
3695
3696         if (g)
3697         {
3698             copy_ivec(SHIFT_IVEC(g, aj), jt);
3699
3700             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3701             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3702             t1 = IVEC2IS(dt_ij);
3703             t2 = IVEC2IS(dt_kj);
3704         }
3705         rvec_inc(fshift[t1], f_i);
3706         rvec_inc(fshift[CENTRAL], f_j);
3707         rvec_inc(fshift[t2], f_k);          /* 9 */
3708         /* 163 TOTAL    */
3709     }
3710     return vtot;
3711 }
3712
3713 real cross_bond_bond(int nbonds,
3714                      const t_iatom forceatoms[], const t_iparams forceparams[],
3715                      const rvec x[], rvec f[], rvec fshift[],
3716                      const t_pbc *pbc, const t_graph *g,
3717                      real gmx_unused lambda, real gmx_unused *dvdlambda,
3718                      const t_mdatoms gmx_unused *md, t_fcdata gmx_unused  *fcd,
3719                      int gmx_unused *global_atom_index)
3720 {
3721     /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3722      * pp. 842-847
3723      */
3724     int  i, ai, aj, ak, type, m, t1, t2;
3725     rvec r_ij, r_kj;
3726     real vtot, vrr, s1, s2, r1, r2, r1e, r2e, krr;
3727     rvec f_i, f_j, f_k;
3728     ivec jt, dt_ij, dt_kj;
3729
3730     vtot = 0.0;
3731     for (i = 0; (i < nbonds); )
3732     {
3733         type = forceatoms[i++];
3734         ai   = forceatoms[i++];
3735         aj   = forceatoms[i++];
3736         ak   = forceatoms[i++];
3737         r1e  = forceparams[type].cross_bb.r1e;
3738         r2e  = forceparams[type].cross_bb.r2e;
3739         krr  = forceparams[type].cross_bb.krr;
3740
3741         /* Compute distance vectors ... */
3742         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3743         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3744
3745         /* ... and their lengths */
3746         r1 = norm(r_ij);
3747         r2 = norm(r_kj);
3748
3749         /* Deviations from ideality */
3750         s1 = r1-r1e;
3751         s2 = r2-r2e;
3752
3753         /* Energy (can be negative!) */
3754         vrr   = krr*s1*s2;
3755         vtot += vrr;
3756
3757         /* Forces */
3758         svmul(-krr*s2/r1, r_ij, f_i);
3759         svmul(-krr*s1/r2, r_kj, f_k);
3760
3761         for (m = 0; (m < DIM); m++)     /*  12  */
3762         {
3763             f_j[m]    = -f_i[m] - f_k[m];
3764             f[ai][m] += f_i[m];
3765             f[aj][m] += f_j[m];
3766             f[ak][m] += f_k[m];
3767         }
3768
3769         /* Virial stuff */
3770         if (g)
3771         {
3772             copy_ivec(SHIFT_IVEC(g, aj), jt);
3773
3774             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3775             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3776             t1 = IVEC2IS(dt_ij);
3777             t2 = IVEC2IS(dt_kj);
3778         }
3779         rvec_inc(fshift[t1], f_i);
3780         rvec_inc(fshift[CENTRAL], f_j);
3781         rvec_inc(fshift[t2], f_k);          /* 9 */
3782         /* 163 TOTAL    */
3783     }
3784     return vtot;
3785 }
3786
3787 real cross_bond_angle(int nbonds,
3788                       const t_iatom forceatoms[], const t_iparams forceparams[],
3789                       const rvec x[], rvec f[], rvec fshift[],
3790                       const t_pbc *pbc, const t_graph *g,
3791                       real gmx_unused lambda, real gmx_unused *dvdlambda,
3792                       const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
3793                       int gmx_unused *global_atom_index)
3794 {
3795     /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
3796      * pp. 842-847
3797      */
3798     int  i, ai, aj, ak, type, m, t1, t2;
3799     rvec r_ij, r_kj, r_ik;
3800     real vtot, vrt, s1, s2, s3, r1, r2, r3, r1e, r2e, r3e, krt, k1, k2, k3;
3801     rvec f_i, f_j, f_k;
3802     ivec jt, dt_ij, dt_kj;
3803
3804     vtot = 0.0;
3805     for (i = 0; (i < nbonds); )
3806     {
3807         type = forceatoms[i++];
3808         ai   = forceatoms[i++];
3809         aj   = forceatoms[i++];
3810         ak   = forceatoms[i++];
3811         r1e  = forceparams[type].cross_ba.r1e;
3812         r2e  = forceparams[type].cross_ba.r2e;
3813         r3e  = forceparams[type].cross_ba.r3e;
3814         krt  = forceparams[type].cross_ba.krt;
3815
3816         /* Compute distance vectors ... */
3817         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
3818         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
3819         pbc_rvec_sub(pbc, x[ai], x[ak], r_ik);
3820
3821         /* ... and their lengths */
3822         r1 = norm(r_ij);
3823         r2 = norm(r_kj);
3824         r3 = norm(r_ik);
3825
3826         /* Deviations from ideality */
3827         s1 = r1-r1e;
3828         s2 = r2-r2e;
3829         s3 = r3-r3e;
3830
3831         /* Energy (can be negative!) */
3832         vrt   = krt*s3*(s1+s2);
3833         vtot += vrt;
3834
3835         /* Forces */
3836         k1 = -krt*(s3/r1);
3837         k2 = -krt*(s3/r2);
3838         k3 = -krt*(s1+s2)/r3;
3839         for (m = 0; (m < DIM); m++)
3840         {
3841             f_i[m] = k1*r_ij[m] + k3*r_ik[m];
3842             f_k[m] = k2*r_kj[m] - k3*r_ik[m];
3843             f_j[m] = -f_i[m] - f_k[m];
3844         }
3845
3846         for (m = 0; (m < DIM); m++)     /*  12  */
3847         {
3848             f[ai][m] += f_i[m];
3849             f[aj][m] += f_j[m];
3850             f[ak][m] += f_k[m];
3851         }
3852
3853         /* Virial stuff */
3854         if (g)
3855         {
3856             copy_ivec(SHIFT_IVEC(g, aj), jt);
3857
3858             ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
3859             ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
3860             t1 = IVEC2IS(dt_ij);
3861             t2 = IVEC2IS(dt_kj);
3862         }
3863         rvec_inc(fshift[t1], f_i);
3864         rvec_inc(fshift[CENTRAL], f_j);
3865         rvec_inc(fshift[t2], f_k);          /* 9 */
3866         /* 163 TOTAL    */
3867     }
3868     return vtot;
3869 }
3870
3871 static real bonded_tab(const char *type, int table_nr,
3872                        const bondedtable_t *table, real kA, real kB, real r,
3873                        real lambda, real *V, real *F)
3874 {
3875     real k, tabscale, *VFtab, rt, eps, eps2, Yt, Ft, Geps, Heps2, Fp, VV, FF;
3876     int  n0, nnn;
3877     real dvdlambda;
3878
3879     k = (1.0 - lambda)*kA + lambda*kB;
3880
3881     tabscale = table->scale;
3882     VFtab    = table->data;
3883
3884     rt    = r*tabscale;
3885     n0    = static_cast<int>(rt);
3886     if (n0 >= table->n)
3887     {
3888         gmx_fatal(FARGS, "A tabulated %s interaction table number %d is out of the table range: r %f, between table indices %d and %d, table length %d",
3889                   type, table_nr, r, n0, n0+1, table->n);
3890     }
3891     eps   = rt - n0;
3892     eps2  = eps*eps;
3893     nnn   = 4*n0;
3894     Yt    = VFtab[nnn];
3895     Ft    = VFtab[nnn+1];
3896     Geps  = VFtab[nnn+2]*eps;
3897     Heps2 = VFtab[nnn+3]*eps2;
3898     Fp    = Ft + Geps + Heps2;
3899     VV    = Yt + Fp*eps;
3900     FF    = Fp + Geps + 2.0*Heps2;
3901
3902     *F         = -k*FF*tabscale;
3903     *V         = k*VV;
3904     dvdlambda  = (kB - kA)*VV;
3905
3906     return dvdlambda;
3907
3908     /* That was 22 flops */
3909 }
3910
3911 real tab_bonds(int nbonds,
3912                const t_iatom forceatoms[], const t_iparams forceparams[],
3913                const rvec x[], rvec f[], rvec fshift[],
3914                const t_pbc *pbc, const t_graph *g,
3915                real lambda, real *dvdlambda,
3916                const t_mdatoms gmx_unused *md, t_fcdata *fcd,
3917                int gmx_unused  *global_atom_index)
3918 {
3919     int  i, m, ki, ai, aj, type, table;
3920     real dr, dr2, fbond, vbond, fij, vtot;
3921     rvec dx;
3922     ivec dt;
3923
3924     vtot = 0.0;
3925     for (i = 0; (i < nbonds); )
3926     {
3927         type = forceatoms[i++];
3928         ai   = forceatoms[i++];
3929         aj   = forceatoms[i++];
3930
3931         ki   = pbc_rvec_sub(pbc, x[ai], x[aj], dx); /*   3      */
3932         dr2  = iprod(dx, dx);                       /*   5              */
3933         dr   = dr2*gmx_invsqrt(dr2);                /*  10              */
3934
3935         table = forceparams[type].tab.table;
3936
3937         *dvdlambda += bonded_tab("bond", table,
3938                                  &fcd->bondtab[table],
3939                                  forceparams[type].tab.kA,
3940                                  forceparams[type].tab.kB,
3941                                  dr, lambda, &vbond, &fbond); /*  22 */
3942
3943         if (dr2 == 0.0)
3944         {
3945             continue;
3946         }
3947
3948
3949         vtot  += vbond;            /* 1*/
3950         fbond *= gmx_invsqrt(dr2); /*   6               */
3951 #ifdef DEBUG
3952         if (debug)
3953         {
3954             fprintf(debug, "TABBONDS: dr = %10g  vbond = %10g  fbond = %10g\n",
3955                     dr, vbond, fbond);
3956         }
3957 #endif
3958         if (g)
3959         {
3960             ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
3961             ki = IVEC2IS(dt);
3962         }
3963         for (m = 0; (m < DIM); m++)     /*  15          */
3964         {
3965             fij                 = fbond*dx[m];
3966             f[ai][m]           += fij;
3967             f[aj][m]           -= fij;
3968             fshift[ki][m]      += fij;
3969             fshift[CENTRAL][m] -= fij;
3970         }
3971     }               /* 62 TOTAL */
3972     return vtot;
3973 }
3974
3975 real tab_angles(int nbonds,
3976                 const t_iatom forceatoms[], const t_iparams forceparams[],
3977                 const rvec x[], rvec f[], rvec fshift[],
3978                 const t_pbc *pbc, const t_graph *g,
3979                 real lambda, real *dvdlambda,
3980                 const t_mdatoms gmx_unused  *md, t_fcdata *fcd,
3981                 int gmx_unused *global_atom_index)
3982 {
3983     int  i, ai, aj, ak, t1, t2, type, table;
3984     rvec r_ij, r_kj;
3985     real cos_theta, cos_theta2, theta, dVdt, va, vtot;
3986     ivec jt, dt_ij, dt_kj;
3987
3988     vtot = 0.0;
3989     for (i = 0; (i < nbonds); )
3990     {
3991         type = forceatoms[i++];
3992         ai   = forceatoms[i++];
3993         aj   = forceatoms[i++];
3994         ak   = forceatoms[i++];
3995
3996         theta  = bond_angle(x[ai], x[aj], x[ak], pbc,
3997                             r_ij, r_kj, &cos_theta, &t1, &t2); /*  41           */
3998
3999         table = forceparams[type].tab.table;
4000
4001         *dvdlambda += bonded_tab("angle", table,
4002                                  &fcd->angletab[table],
4003                                  forceparams[type].tab.kA,
4004                                  forceparams[type].tab.kB,
4005                                  theta, lambda, &va, &dVdt); /*  22  */
4006         vtot += va;
4007
4008         cos_theta2 = sqr(cos_theta);            /*   1          */
4009         if (cos_theta2 < 1)
4010         {
4011             int  m;
4012             real st, sth;
4013             real cik, cii, ckk;
4014             real nrkj2, nrij2;
4015             rvec f_i, f_j, f_k;
4016
4017             st  = dVdt*gmx_invsqrt(1 - cos_theta2); /*  12              */
4018             sth = st*cos_theta;                     /*   1              */
4019 #ifdef DEBUG
4020             if (debug)
4021             {
4022                 fprintf(debug, "ANGLES: theta = %10g  vth = %10g  dV/dtheta = %10g\n",
4023                         theta*RAD2DEG, va, dVdt);
4024             }
4025 #endif
4026             nrkj2 = iprod(r_kj, r_kj);  /*   5          */
4027             nrij2 = iprod(r_ij, r_ij);
4028
4029             cik = st*gmx_invsqrt(nrkj2*nrij2); /*  12           */
4030             cii = sth/nrij2;                   /*  10           */
4031             ckk = sth/nrkj2;                   /*  10           */
4032
4033             for (m = 0; (m < DIM); m++)        /*  39           */
4034             {
4035                 f_i[m]    = -(cik*r_kj[m]-cii*r_ij[m]);
4036                 f_k[m]    = -(cik*r_ij[m]-ckk*r_kj[m]);
4037                 f_j[m]    = -f_i[m]-f_k[m];
4038                 f[ai][m] += f_i[m];
4039                 f[aj][m] += f_j[m];
4040                 f[ak][m] += f_k[m];
4041             }
4042             if (g)
4043             {
4044                 copy_ivec(SHIFT_IVEC(g, aj), jt);
4045
4046                 ivec_sub(SHIFT_IVEC(g, ai), jt, dt_ij);
4047                 ivec_sub(SHIFT_IVEC(g, ak), jt, dt_kj);
4048                 t1 = IVEC2IS(dt_ij);
4049                 t2 = IVEC2IS(dt_kj);
4050             }
4051             rvec_inc(fshift[t1], f_i);
4052             rvec_inc(fshift[CENTRAL], f_j);
4053             rvec_inc(fshift[t2], f_k);
4054         }                                       /* 169 TOTAL    */
4055     }
4056     return vtot;
4057 }
4058
4059 real tab_dihs(int nbonds,
4060               const t_iatom forceatoms[], const t_iparams forceparams[],
4061               const rvec x[], rvec f[], rvec fshift[],
4062               const t_pbc *pbc, const t_graph *g,
4063               real lambda, real *dvdlambda,
4064               const t_mdatoms gmx_unused *md, t_fcdata *fcd,
4065               int gmx_unused *global_atom_index)
4066 {
4067     int  i, type, ai, aj, ak, al, table;
4068     int  t1, t2, t3;
4069     rvec r_ij, r_kj, r_kl, m, n;
4070     real phi, sign, ddphi, vpd, vtot;
4071
4072     vtot = 0.0;
4073     for (i = 0; (i < nbonds); )
4074     {
4075         type = forceatoms[i++];
4076         ai   = forceatoms[i++];
4077         aj   = forceatoms[i++];
4078         ak   = forceatoms[i++];
4079         al   = forceatoms[i++];
4080
4081         phi = dih_angle(x[ai], x[aj], x[ak], x[al], pbc, r_ij, r_kj, r_kl, m, n,
4082                         &sign, &t1, &t2, &t3);  /*  84  */
4083
4084         table = forceparams[type].tab.table;
4085
4086         /* Hopefully phi+M_PI never results in values < 0 */
4087         *dvdlambda += bonded_tab("dihedral", table,
4088                                  &fcd->dihtab[table],
4089                                  forceparams[type].tab.kA,
4090                                  forceparams[type].tab.kB,
4091                                  phi+M_PI, lambda, &vpd, &ddphi);
4092
4093         vtot += vpd;
4094         do_dih_fup(ai, aj, ak, al, -ddphi, r_ij, r_kj, r_kl, m, n,
4095                    f, fshift, pbc, g, x, t1, t2, t3); /* 112    */
4096
4097 #ifdef DEBUG
4098         fprintf(debug, "pdih: (%d,%d,%d,%d) phi=%g\n",
4099                 ai, aj, ak, al, phi);
4100 #endif
4101     } /* 227 TOTAL  */
4102
4103     return vtot;
4104 }
4105
4106 /* TODO This function could go away when idef is not a big bucket of
4107    everything. */
4108 gmx_bool ftype_is_bonded_potential(int ftype)
4109 {
4110     return
4111         (interaction_function[ftype].flags & IF_BOND) &&
4112         !(ftype == F_CONNBONDS || ftype == F_POSRES || ftype == F_FBPOSRES) &&
4113         (ftype < F_GB12 || ftype > F_GB14);
4114 }
4115
4116 static void zero_thread_forces(f_thread_t *f_t, int n,
4117                                int nblock, int blocksize)
4118 {
4119     int b, a0, a1, a, i, j;
4120
4121     if (n > f_t->f_nalloc)
4122     {
4123         f_t->f_nalloc = over_alloc_large(n);
4124         srenew(f_t->f, f_t->f_nalloc);
4125     }
4126
4127     if (f_t->red_mask != 0)
4128     {
4129         for (b = 0; b < nblock; b++)
4130         {
4131             if (f_t->red_mask && (1U<<b))
4132             {
4133                 a0 = b*blocksize;
4134                 a1 = std::min((b+1)*blocksize, n);
4135                 for (a = a0; a < a1; a++)
4136                 {
4137                     clear_rvec(f_t->f[a]);
4138                 }
4139             }
4140         }
4141     }
4142     for (i = 0; i < SHIFTS; i++)
4143     {
4144         clear_rvec(f_t->fshift[i]);
4145     }
4146     for (i = 0; i < F_NRE; i++)
4147     {
4148         f_t->ener[i] = 0;
4149     }
4150     for (i = 0; i < egNR; i++)
4151     {
4152         for (j = 0; j < f_t->grpp.nener; j++)
4153         {
4154             f_t->grpp.ener[i][j] = 0;
4155         }
4156     }
4157     for (i = 0; i < efptNR; i++)
4158     {
4159         f_t->dvdl[i] = 0;
4160     }
4161 }
4162
4163 static void reduce_thread_force_buffer(int n, rvec *f,
4164                                        int nthreads, f_thread_t *f_t,
4165                                        int nblock, int block_size)
4166 {
4167     /* The max thread number is arbitrary,
4168      * we used a fixed number to avoid memory management.
4169      * Using more than 16 threads is probably never useful performance wise.
4170      */
4171 #define MAX_BONDED_THREADS 256
4172     int b;
4173
4174     if (nthreads > MAX_BONDED_THREADS)
4175     {
4176         gmx_fatal(FARGS, "Can not reduce bonded forces on more than %d threads",
4177                   MAX_BONDED_THREADS);
4178     }
4179
4180     /* This reduction can run on any number of threads,
4181      * independently of nthreads.
4182      */
4183 #pragma omp parallel for num_threads(nthreads) schedule(static)
4184     for (b = 0; b < nblock; b++)
4185     {
4186         rvec *fp[MAX_BONDED_THREADS];
4187         int   nfb, ft, fb;
4188         int   a0, a1, a;
4189
4190         /* Determine which threads contribute to this block */
4191         nfb = 0;
4192         for (ft = 1; ft < nthreads; ft++)
4193         {
4194             if (f_t[ft].red_mask & (1U<<b))
4195             {
4196                 fp[nfb++] = f_t[ft].f;
4197             }
4198         }
4199         if (nfb > 0)
4200         {
4201             /* Reduce force buffers for threads that contribute */
4202             a0 =  b   *block_size;
4203             a1 = (b+1)*block_size;
4204             a1 = std::min(a1, n);
4205             for (a = a0; a < a1; a++)
4206             {
4207                 for (fb = 0; fb < nfb; fb++)
4208                 {
4209                     rvec_inc(f[a], fp[fb][a]);
4210                 }
4211             }
4212         }
4213     }
4214 }
4215
4216 static void reduce_thread_forces(int n, rvec *f, rvec *fshift,
4217                                  real *ener, gmx_grppairener_t *grpp, real *dvdl,
4218                                  int nthreads, f_thread_t *f_t,
4219                                  int nblock, int block_size,
4220                                  gmx_bool bCalcEnerVir,
4221                                  gmx_bool bDHDL)
4222 {
4223     if (nblock > 0)
4224     {
4225         /* Reduce the bonded force buffer */
4226         reduce_thread_force_buffer(n, f, nthreads, f_t, nblock, block_size);
4227     }
4228
4229     /* When necessary, reduce energy and virial using one thread only */
4230     if (bCalcEnerVir)
4231     {
4232         int t, i, j;
4233
4234         for (i = 0; i < SHIFTS; i++)
4235         {
4236             for (t = 1; t < nthreads; t++)
4237             {
4238                 rvec_inc(fshift[i], f_t[t].fshift[i]);
4239             }
4240         }
4241         for (i = 0; i < F_NRE; i++)
4242         {
4243             for (t = 1; t < nthreads; t++)
4244             {
4245                 ener[i] += f_t[t].ener[i];
4246             }
4247         }
4248         for (i = 0; i < egNR; i++)
4249         {
4250             for (j = 0; j < f_t[1].grpp.nener; j++)
4251             {
4252                 for (t = 1; t < nthreads; t++)
4253                 {
4254
4255                     grpp->ener[i][j] += f_t[t].grpp.ener[i][j];
4256                 }
4257             }
4258         }
4259         if (bDHDL)
4260         {
4261             for (i = 0; i < efptNR; i++)
4262             {
4263
4264                 for (t = 1; t < nthreads; t++)
4265                 {
4266                     dvdl[i] += f_t[t].dvdl[i];
4267                 }
4268             }
4269         }
4270     }
4271 }
4272
4273 static real calc_one_bond(int thread,
4274                           int ftype, const t_idef *idef,
4275                           const rvec x[], rvec f[], rvec fshift[],
4276                           t_forcerec *fr,
4277                           const t_pbc *pbc, const t_graph *g,
4278                           gmx_grppairener_t *grpp,
4279                           t_nrnb *nrnb,
4280                           real *lambda, real *dvdl,
4281                           const t_mdatoms *md, t_fcdata *fcd,
4282                           gmx_bool bCalcEnerVir,
4283                           int *global_atom_index)
4284 {
4285     int      nat1, nbonds, efptFTYPE;
4286     real     v = 0;
4287     t_iatom *iatoms;
4288     int      nb0, nbn;
4289
4290     if (IS_RESTRAINT_TYPE(ftype))
4291     {
4292         efptFTYPE = efptRESTRAINT;
4293     }
4294     else
4295     {
4296         efptFTYPE = efptBONDED;
4297     }
4298
4299     nat1      = interaction_function[ftype].nratoms + 1;
4300     nbonds    = idef->il[ftype].nr/nat1;
4301     iatoms    = idef->il[ftype].iatoms;
4302
4303     nb0 = idef->il_thread_division[ftype*(idef->nthreads+1)+thread];
4304     nbn = idef->il_thread_division[ftype*(idef->nthreads+1)+thread+1] - nb0;
4305
4306     if (!IS_LISTED_LJ_C(ftype))
4307     {
4308         if (ftype == F_CMAP)
4309         {
4310             v = cmap_dihs(nbn, iatoms+nb0,
4311                           idef->iparams, &idef->cmap_grid,
4312                           x, f, fshift,
4313                           pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4314                           md, fcd, global_atom_index);
4315         }
4316 #ifdef GMX_SIMD_HAVE_REAL
4317         else if (ftype == F_ANGLES &&
4318                  !bCalcEnerVir && fr->efep == efepNO)
4319         {
4320             /* No energies, shift forces, dvdl */
4321             angles_noener_simd(nbn, idef->il[ftype].iatoms+nb0,
4322                                idef->iparams,
4323                                x, f,
4324                                pbc, g, lambda[efptFTYPE], md, fcd,
4325                                global_atom_index);
4326             v = 0;
4327         }
4328 #endif
4329         else if (ftype == F_PDIHS &&
4330                  !bCalcEnerVir && fr->efep == efepNO)
4331         {
4332             /* No energies, shift forces, dvdl */
4333 #ifdef GMX_SIMD_HAVE_REAL
4334             pdihs_noener_simd
4335 #else
4336             pdihs_noener
4337 #endif
4338                 (nbn, idef->il[ftype].iatoms+nb0,
4339                 idef->iparams,
4340                 x, f,
4341                 pbc, g, lambda[efptFTYPE], md, fcd,
4342                 global_atom_index);
4343             v = 0;
4344         }
4345         else
4346         {
4347             v = interaction_function[ftype].ifunc(nbn, iatoms+nb0,
4348                                                   idef->iparams,
4349                                                   x, f, fshift,
4350                                                   pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
4351                                                   md, fcd, global_atom_index);
4352         }
4353     }
4354     else
4355     {
4356         v = do_nonbonded_listed(ftype, nbn, iatoms+nb0, idef->iparams, x, f, fshift,
4357                                 pbc, g, lambda, dvdl, md, fr, grpp, global_atom_index);
4358     }
4359
4360     if (thread == 0)
4361     {
4362         inc_nrnb(nrnb, interaction_function[ftype].nrnb_ind, nbonds);
4363     }
4364
4365     return v;
4366 }
4367
4368 void calc_bonds(const gmx_multisim_t *ms,
4369                 const t_idef *idef,
4370                 const rvec x[], history_t *hist,
4371                 rvec f[], t_forcerec *fr,
4372                 const t_pbc *pbc, const t_graph *g,
4373                 gmx_enerdata_t *enerd, t_nrnb *nrnb,
4374                 real *lambda,
4375                 const t_mdatoms *md,
4376                 t_fcdata *fcd, int *global_atom_index,
4377                 int force_flags)
4378 {
4379     gmx_bool      bCalcEnerVir;
4380     int           i;
4381     real          dvdl[efptNR]; /* The dummy array is to have a place to store the dhdl at other values
4382                                                         of lambda, which will be thrown away in the end*/
4383     const  t_pbc *pbc_null;
4384     int           thread;
4385
4386     assert(fr->nthreads == idef->nthreads);
4387
4388     bCalcEnerVir = (force_flags & (GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY));
4389
4390     for (i = 0; i < efptNR; i++)
4391     {
4392         dvdl[i] = 0.0;
4393     }
4394     if (fr->bMolPBC)
4395     {
4396         pbc_null = pbc;
4397     }
4398     else
4399     {
4400         pbc_null = NULL;
4401     }
4402
4403 #ifdef DEBUG
4404     if (g && debug)
4405     {
4406         p_graph(debug, "Bondage is fun", g);
4407     }
4408 #endif
4409
4410     /* Do pre force calculation stuff which might require communication */
4411     if (idef->il[F_ORIRES].nr)
4412     {
4413         enerd->term[F_ORIRESDEV] =
4414             calc_orires_dev(ms, idef->il[F_ORIRES].nr,
4415                             idef->il[F_ORIRES].iatoms,
4416                             idef->iparams, md, x,
4417                             pbc_null, fcd, hist);
4418     }
4419     if (idef->il[F_DISRES].nr)
4420     {
4421         calc_disres_R_6(idef->il[F_DISRES].nr,
4422                         idef->il[F_DISRES].iatoms,
4423                         idef->iparams, x, pbc_null,
4424                         fcd, hist);
4425 #ifdef GMX_MPI
4426         if (fcd->disres.nsystems > 1)
4427         {
4428             gmx_sum_sim(2*fcd->disres.nres, fcd->disres.Rt_6, ms);
4429         }
4430 #endif
4431     }
4432
4433 #pragma omp parallel for num_threads(fr->nthreads) schedule(static)
4434     for (thread = 0; thread < fr->nthreads; thread++)
4435     {
4436         int                ftype;
4437         real              *epot, v;
4438         /* thread stuff */
4439         rvec              *ft, *fshift;
4440         real              *dvdlt;
4441         gmx_grppairener_t *grpp;
4442
4443         if (thread == 0)
4444         {
4445             ft     = f;
4446             fshift = fr->fshift;
4447             epot   = enerd->term;
4448             grpp   = &enerd->grpp;
4449             dvdlt  = dvdl;
4450         }
4451         else
4452         {
4453             zero_thread_forces(&fr->f_t[thread], fr->natoms_force,
4454                                fr->red_nblock, 1<<fr->red_ashift);
4455
4456             ft     = fr->f_t[thread].f;
4457             fshift = fr->f_t[thread].fshift;
4458             epot   = fr->f_t[thread].ener;
4459             grpp   = &fr->f_t[thread].grpp;
4460             dvdlt  = fr->f_t[thread].dvdl;
4461         }
4462         /* Loop over all bonded force types to calculate the bonded forces */
4463         for (ftype = 0; (ftype < F_NRE); ftype++)
4464         {
4465             if (idef->il[ftype].nr > 0 && ftype_is_bonded_potential(ftype))
4466             {
4467                 v = calc_one_bond(thread, ftype, idef, x,
4468                                   ft, fshift, fr, pbc_null, g, grpp,
4469                                   nrnb, lambda, dvdlt,
4470                                   md, fcd, bCalcEnerVir,
4471                                   global_atom_index);
4472                 epot[ftype] += v;
4473             }
4474         }
4475     }
4476     if (fr->nthreads > 1)
4477     {
4478         reduce_thread_forces(fr->natoms_force, f, fr->fshift,
4479                              enerd->term, &enerd->grpp, dvdl,
4480                              fr->nthreads, fr->f_t,
4481                              fr->red_nblock, 1<<fr->red_ashift,
4482                              bCalcEnerVir,
4483                              force_flags & GMX_FORCE_DHDL);
4484     }
4485     if (force_flags & GMX_FORCE_DHDL)
4486     {
4487         for (i = 0; i < efptNR; i++)
4488         {
4489             enerd->dvdl_nonlin[i] += dvdl[i];
4490         }
4491     }
4492
4493     /* Copy the sum of violations for the distance restraints from fcd */
4494     if (fcd)
4495     {
4496         enerd->term[F_DISRESVIOL] = fcd->disres.sumviol;
4497
4498     }
4499 }
4500
4501 void calc_bonds_lambda(const t_idef *idef,
4502                        const rvec x[],
4503                        t_forcerec *fr,
4504                        const t_pbc *pbc, const t_graph *g,
4505                        gmx_grppairener_t *grpp, real *epot, t_nrnb *nrnb,
4506                        real *lambda,
4507                        const t_mdatoms *md,
4508                        t_fcdata *fcd,
4509                        int *global_atom_index)
4510 {
4511     int           ftype, nr_nonperturbed, nr;
4512     real          v;
4513     real          dvdl_dum[efptNR];
4514     rvec         *f, *fshift;
4515     const  t_pbc *pbc_null;
4516     t_idef        idef_fe;
4517
4518     if (fr->bMolPBC)
4519     {
4520         pbc_null = pbc;
4521     }
4522     else
4523     {
4524         pbc_null = NULL;
4525     }
4526
4527     /* Copy the whole idef, so we can modify the contents locally */
4528     idef_fe          = *idef;
4529     idef_fe.nthreads = 1;
4530     snew(idef_fe.il_thread_division, F_NRE*(idef_fe.nthreads+1));
4531
4532     /* We already have the forces, so we use temp buffers here */
4533     snew(f, fr->natoms_force);
4534     snew(fshift, SHIFTS);
4535
4536     /* Loop over all bonded force types to calculate the bonded energies */
4537     for (ftype = 0; (ftype < F_NRE); ftype++)
4538     {
4539         if (ftype_is_bonded_potential(ftype))
4540         {
4541             /* Set the work range of thread 0 to the perturbed bondeds only */
4542             nr_nonperturbed                       = idef->il[ftype].nr_nonperturbed;
4543             nr                                    = idef->il[ftype].nr;
4544             idef_fe.il_thread_division[ftype*2+0] = nr_nonperturbed;
4545             idef_fe.il_thread_division[ftype*2+1] = nr;
4546
4547             /* This is only to get the flop count correct */
4548             idef_fe.il[ftype].nr = nr - nr_nonperturbed;
4549
4550             if (nr - nr_nonperturbed > 0)
4551             {
4552                 v = calc_one_bond(0, ftype, &idef_fe,
4553                                   x, f, fshift, fr, pbc_null, g,
4554                                   grpp, nrnb, lambda, dvdl_dum,
4555                                   md, fcd, TRUE,
4556                                   global_atom_index);
4557                 epot[ftype] += v;
4558             }
4559         }
4560     }
4561
4562     sfree(fshift);
4563     sfree(f);
4564
4565     sfree(idef_fe.il_thread_division);
4566 }