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