Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / listed-forces / bonded.cpp
similarity index 92%
rename from src/gromacs/gmxlib/bondfree.c
rename to src/gromacs/listed-forces/bonded.cpp
index 0eb8d55791b983c0b5909786b7dffdb1c5ef462a..dfb846d9e700d1492bd6314d68537a9e21fb8860 100644 (file)
  * To help us fund GROMACS development, we humbly ask that you cite
  * the research papers on the package. Check out http://www.gromacs.org.
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+/*! \internal \file
+ *
+ * \brief This file defines functions necessary for mdrun and tools to
+ * compute energies and forces for bonded interactions.
+ *
+ * \author Mark Abraham <mark.j.abraham@gmail.com>
+ *
+ * \ingroup module_listed-forces
+ */
+#include "gmxpre.h"
+
+#include "bonded.h"
+
+#include "config.h"
 
-#include <math.h>
 #include <assert.h>
-#include "physics.h"
-#include "vec.h"
-#include "gromacs/math/utilities.h"
-#include "txtdump.h"
-#include "bondf.h"
-#include "gromacs/utility/smalloc.h"
-#include "pbc.h"
-#include "ns.h"
-#include "macros.h"
-#include "names.h"
-#include "gmx_fatal.h"
-#include "mshift.h"
-#include "main.h"
-#include "disre.h"
-#include "orires.h"
-#include "force.h"
-#include "nonbonded.h"
-#include "restcbt.h"
 
+#include <cmath>
+
+#include <algorithm>
+
+#include "gromacs/legacyheaders/disre.h"
+#include "gromacs/legacyheaders/force.h"
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/legacyheaders/names.h"
+#include "gromacs/legacyheaders/nonbonded.h"
+#include "gromacs/legacyheaders/ns.h"
+#include "gromacs/legacyheaders/orires.h"
+#include "gromacs/legacyheaders/txtdump.h"
+#include "gromacs/math/units.h"
+#include "gromacs/math/utilities.h"
+#include "gromacs/math/vec.h"
+#include "gromacs/pbcutil/ishift.h"
+#include "gromacs/pbcutil/mshift.h"
+#include "gromacs/pbcutil/pbc.h"
 #include "gromacs/simd/simd.h"
 #include "gromacs/simd/simd_math.h"
 #include "gromacs/simd/vector_operations.h"
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/smalloc.h"
 
-/* Find a better place for this? */
+#include "restcbt.h"
+
+/*! \brief Mysterious CMAP coefficient matrix */
 const int cmap_coeff_matrix[] = {
     1, 0, -3,  2, 0, 0,  0,  0, -3,  0,  9, -6,  2,  0, -6,  4,
     0, 0,  0,  0, 0, 0,  0,  0,  3,  0, -9,  6, -2,  0,  6, -4,
@@ -84,7 +98,9 @@ const int cmap_coeff_matrix[] = {
 };
 
 
-
+/* TODO This function should go and live in nonbonded.c where it is
+   really needed. Here, it only supports giving a fatal error message
+   with FENE_bonds */
 int glatnr(int *global_atom_index, int i)
 {
     int atnr;
@@ -101,6 +117,9 @@ int glatnr(int *global_atom_index, int i)
     return atnr;
 }
 
+/*! \brief Compute dx = xi - xj, modulo PBC if non-NULL
+ *
+ * \todo This kind of code appears in many places. Consolidate it */
 static int pbc_rvec_sub(const t_pbc *pbc, const rvec xi, const rvec xj, rvec dx)
 {
     if (pbc)
@@ -129,7 +148,7 @@ typedef struct {
     gmx_simd_real_t bxx;
 } pbc_simd_t;
 
-/* Set the SIMD pbc data from a normal t_pbc struct */
+/*! \brief Set the SIMD pbc data from a normal t_pbc struct */
 static void set_pbc_simd(const t_pbc *pbc, pbc_simd_t *pbc_simd)
 {
     rvec inv_bdiag;
@@ -169,7 +188,7 @@ static void set_pbc_simd(const t_pbc *pbc, pbc_simd_t *pbc_simd)
     }
 }
 
-/* Correct distance vector *dx,*dy,*dz for PBC using SIMD */
+/*! \brief Correct distance vector *dx,*dy,*dz for PBC using SIMD */
 static gmx_inline void
 pbc_dx_simd(gmx_simd_real_t *dx, gmx_simd_real_t *dy, gmx_simd_real_t *dz,
             const pbc_simd_t *pbc)
@@ -191,10 +210,9 @@ pbc_dx_simd(gmx_simd_real_t *dx, gmx_simd_real_t *dy, gmx_simd_real_t *dz,
 
 #endif /* GMX_SIMD_HAVE_REAL */
 
-/*
- * Morse potential bond by Frank Everdij
+/*! \brief Morse potential bond
  *
- * Three parameters needed:
+ * By Frank Everdij. Three parameters needed:
  *
  * b0 = equilibrium distance in nm
  * be = beta in nm^-1 (actually, it's nu_e*Sqrt(2*pi*pi*mu/D_e))
@@ -203,7 +221,6 @@ pbc_dx_simd(gmx_simd_real_t *dx, gmx_simd_real_t *dy, gmx_simd_real_t *dz,
  * Note: the potential is referenced to be +cb at infinite separation
  *       and zero at the equilibrium distance!
  */
-
 real morse_bonds(int nbonds,
                  const t_iatom forceatoms[], const t_iparams forceparams[],
                  const rvec x[], rvec f[], rvec fshift[],
@@ -278,6 +295,7 @@ real morse_bonds(int nbonds,
     return vtot;
 }
 
+//! \cond
 real cubic_bonds(int nbonds,
                  const t_iatom forceatoms[], const t_iparams forceparams[],
                  const rvec x[], rvec f[], rvec fshift[],
@@ -351,7 +369,7 @@ real FENE_bonds(int nbonds,
     const real half = 0.5;
     const real one  = 1.0;
     real       bm, kb;
-    real       dr, dr2, bm2, omdr2obm2, fbond, vbond, fij, vtot;
+    real       dr2, bm2, omdr2obm2, fbond, vbond, fij, vtot;
     rvec       dx;
     int        i, m, ki, type, ai, aj;
     ivec       dt;
@@ -751,7 +769,6 @@ real water_pol(int nbonds,
         kk[YY] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_y;
         kk[ZZ] = sqr(qS)*ONE_4PI_EPS0/forceparams[type0].wpol.al_z;
         r_HH   = 1.0/forceparams[type0].wpol.rHH;
-        r_OD   = 1.0/forceparams[type0].wpol.rOD;
         if (debug)
         {
             fprintf(debug, "WPOL: qS  = %10.5f aS = %5d\n", qS, aS);
@@ -875,7 +892,7 @@ static real do_1_thole(const rvec xi, const rvec xj, rvec fi, rvec fj,
                        rvec fshift[], real afac)
 {
     rvec r12;
-    real r12sq, r12_1, r12n, r12bar, v0, v1, fscal, ebar, fff;
+    real r12sq, r12_1, r12bar, v0, v1, fscal, ebar, fff;
     int  m, t;
 
     t      = pbc_rvec_sub(pbc, xi, xj, r12);                      /*  3 */
@@ -914,9 +931,10 @@ real thole_pol(int nbonds,
                int gmx_unused *global_atom_index)
 {
     /* Interaction between two pairs of particles with opposite charge */
-    int  i, type, a1, da1, a2, da2;
-    real q1, q2, qq, a, al1, al2, afac;
-    real V = 0;
+    int        i, type, a1, da1, a2, da2;
+    real       q1, q2, qq, a, al1, al2, afac;
+    real       V             = 0;
+    const real minusOneOnSix = -1.0/6.0;
 
     for (i = 0; (i < nbonds); )
     {
@@ -931,7 +949,7 @@ real thole_pol(int nbonds,
         al1   = forceparams[type].thole.alpha1;
         al2   = forceparams[type].thole.alpha2;
         qq    = q1*q2;
-        afac  = a*pow(al1*al2, -1.0/6.0);
+        afac  = a*pow(al1*al2, minusOneOnSix);
         V    += do_1_thole(x[a1], x[a2], f[a1], f[a2], pbc, qq, fshift, afac);
         V    += do_1_thole(x[da1], x[a2], f[da1], f[a2], pbc, -qq, fshift, afac);
         V    += do_1_thole(x[a1], x[da2], f[a1], f[da2], pbc, -qq, fshift, afac);
@@ -1716,10 +1734,9 @@ do_dih_fup_noshiftf(int i, int j, int k, int l, real ddphi,
                     rvec m, rvec n, rvec f[])
 {
     rvec f_i, f_j, f_k, f_l;
-    rvec uvec, vvec, svec, dx_jl;
+    rvec uvec, vvec, svec;
     real iprm, iprn, nrkj, nrkj2, nrkj_1, nrkj_2;
     real a, b, p, q, toler;
-    ivec jt, dt_ij, dt_kj, dt_lj;
 
     iprm  = iprod(m, m);       /*  5    */
     iprn  = iprod(n, n);       /*  5   */
@@ -1989,10 +2006,9 @@ pdihs_noener_simd(int nbonds,
     const int             nfa1 = 5;
     int                   i, iu, s;
     int                   type, ai[GMX_SIMD_REAL_WIDTH], aj[GMX_SIMD_REAL_WIDTH], ak[GMX_SIMD_REAL_WIDTH], al[GMX_SIMD_REAL_WIDTH];
-    real                  ddphi;
     real                  dr_array[3*DIM*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *dr;
     real                  buf_array[7*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *buf;
-    real                 *cp, *phi0, *mult, *phi, *p, *q, *sf_i, *msf_l;
+    real                 *cp, *phi0, *mult, *p, *q;
     gmx_simd_real_t       phi0_S, phi_S;
     gmx_simd_real_t       mx_S, my_S, mz_S;
     gmx_simd_real_t       nx_S, ny_S, nz_S;
@@ -2013,8 +2029,6 @@ pdihs_noener_simd(int nbonds,
     mult  = buf + 2*GMX_SIMD_REAL_WIDTH;
     p     = buf + 3*GMX_SIMD_REAL_WIDTH;
     q     = buf + 4*GMX_SIMD_REAL_WIDTH;
-    sf_i  = buf + 5*GMX_SIMD_REAL_WIDTH;
-    msf_l = buf + 6*GMX_SIMD_REAL_WIDTH;
 
     set_pbc_simd(pbc, &pbc_simd);
 
@@ -2119,10 +2133,9 @@ rbdihs_noener_simd(int nbonds,
     const int             nfa1 = 5;
     int                   i, iu, s, j;
     int                   type, ai[GMX_SIMD_REAL_WIDTH], aj[GMX_SIMD_REAL_WIDTH], ak[GMX_SIMD_REAL_WIDTH], al[GMX_SIMD_REAL_WIDTH];
-    real                  ddphi;
     real                  dr_array[3*DIM*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *dr;
     real                  buf_array[(NR_RBDIHS + 4)*GMX_SIMD_REAL_WIDTH+GMX_SIMD_REAL_WIDTH], *buf;
-    real                 *parm, *phi, *p, *q, *sf_i, *msf_l;
+    real                 *parm, *p, *q;
 
     gmx_simd_real_t       phi_S;
     gmx_simd_real_t       ddphi_S, cosfac_S;
@@ -2145,8 +2158,6 @@ rbdihs_noener_simd(int nbonds,
     parm  = buf;
     p     = buf + (NR_RBDIHS + 0)*GMX_SIMD_REAL_WIDTH;
     q     = buf + (NR_RBDIHS + 1)*GMX_SIMD_REAL_WIDTH;
-    sf_i  = buf + (NR_RBDIHS + 2)*GMX_SIMD_REAL_WIDTH;
-    msf_l = buf + (NR_RBDIHS + 3)*GMX_SIMD_REAL_WIDTH;
 
     set_pbc_simd(pbc, &pbc_simd);
 
@@ -2310,7 +2321,7 @@ real idihs(int nbonds,
 
         dvdl_term += 0.5*(kB - kA)*dp2 - kk*dphi0*dp;
 
-        do_dih_fup(ai, aj, ak, al, (real)(-ddphi), r_ij, r_kj, r_kl, m, n,
+        do_dih_fup(ai, aj, ak, al, -ddphi, r_ij, r_kj, r_kl, m, n,
                    f, fshift, pbc, g, x, t1, t2, t3); /* 112           */
         /* 218 TOTAL   */
 #ifdef DEBUG
@@ -2400,6 +2411,44 @@ static void posres_dx(const rvec x, const rvec pos0A, const rvec pos0B,
     }
 }
 
+/*! \brief Computes forces and potential for flat-bottom cylindrical restraints.
+ *         Returns the flat-bottom potential. */
+static real do_fbposres_cylinder(int fbdim, rvec fm, rvec dx, real rfb, real kk, gmx_bool bInvert)
+{
+    int     d;
+    real    dr, dr2, invdr, v, rfb2;
+
+    dr2  = 0.0;
+    rfb2 = sqr(rfb);
+    v    = 0.0;
+
+    for (d = 0; d < DIM; d++)
+    {
+        if (d != fbdim)
+        {
+            dr2 += sqr(dx[d]);
+        }
+    }
+
+    if  (dr2 > 0.0 &&
+         ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
+         )
+    {
+        dr     = sqrt(dr2);
+        invdr  = 1./dr;
+        v      = 0.5*kk*sqr(dr - rfb);
+        for (d = 0; d < DIM; d++)
+        {
+            if (d != fbdim)
+            {
+                fm[d] = -kk*(dr-rfb)*dx[d]*invdr; /* Force pointing to the center */
+            }
+        }
+    }
+
+    return v;
+}
+
 /*! \brief Adds forces of flat-bottomed positions restraints to f[]
  *         and fixes vir_diag. Returns the flat-bottomed potential. */
 real fbposres(int nbonds,
@@ -2412,8 +2461,8 @@ real fbposres(int nbonds,
     int              i, ai, m, d, type, npbcdim = 0, fbdim;
     const t_iparams *pr;
     real             vtot, kk, v;
-    real             ref = 0, dr, dr2, rpot, rfb, rfb2, fact, invdr;
-    rvec             com_sc, rdist, pos, dx, dpdl, fm;
+    real             dr, dr2, rfb, rfb2, fact;
+    rvec             com_sc, rdist, dx, dpdl, fm;
     gmx_bool         bInvert;
 
     npbcdim = ePBC2npbcdim(ePBC);
@@ -2474,19 +2523,22 @@ real fbposres(int nbonds,
                     svmul(fact, dx, fm);
                 }
                 break;
+            case efbposresCYLINDERX:
+                /* cylindrical flat-bottom posres in y-z plane. fm[XX] = 0. */
+                fbdim = XX;
+                v     = do_fbposres_cylinder(fbdim, fm, dx, rfb, kk, bInvert);
+                break;
+            case efbposresCYLINDERY:
+                /* cylindrical flat-bottom posres in x-z plane. fm[YY] = 0. */
+                fbdim = YY;
+                v     = do_fbposres_cylinder(fbdim, fm, dx, rfb, kk, bInvert);
+                break;
             case efbposresCYLINDER:
-                /* cylidrical flat-bottom posres in x-y plane. fm[ZZ] = 0. */
-                dr2 = sqr(dx[XX])+sqr(dx[YY]);
-                if  (dr2 > 0.0 &&
-                     ( (dr2 > rfb2 && bInvert == FALSE ) || (dr2 < rfb2 && bInvert == TRUE ) )
-                     )
-                {
-                    dr     = sqrt(dr2);
-                    invdr  = 1./dr;
-                    v      = 0.5*kk*sqr(dr - rfb);
-                    fm[XX] = -kk*(dr-rfb)*dx[XX]*invdr; /* Force pointing to the center */
-                    fm[YY] = -kk*(dr-rfb)*dx[YY]*invdr;
-                }
+            /* equivalent to efbposresCYLINDERZ for backwards compatibility */
+            case efbposresCYLINDERZ:
+                /* cylindrical flat-bottom posres in x-y plane. fm[ZZ] = 0. */
+                fbdim = ZZ;
+                v     = do_fbposres_cylinder(fbdim, fm, dx, rfb, kk, bInvert);
                 break;
             case efbposresX: /* fbdim=XX */
             case efbposresY: /* fbdim=YY */
@@ -2528,12 +2580,11 @@ real posres(int nbonds,
             real lambda, real *dvdlambda,
             int refcoord_scaling, int ePBC, rvec comA, rvec comB)
 {
-    int              i, ai, m, d, type, ki, npbcdim = 0;
+    int              i, ai, m, d, type, npbcdim = 0;
     const t_iparams *pr;
     real             L1;
     real             vtot, kk, fm;
-    real             posA, posB, ref = 0;
-    rvec             comA_sc, comB_sc, rdist, dpdl, pos, dx;
+    rvec             comA_sc, comB_sc, rdist, dpdl, dx;
     gmx_bool         bForceValid = TRUE;
 
     if ((f == NULL) || (vir_diag == NULL))    /* should both be null together! */
@@ -2726,7 +2777,7 @@ real dihres(int nbonds,
     real vtot = 0;
     int  ai, aj, ak, al, i, k, type, t1, t2, t3;
     real phi0A, phi0B, dphiA, dphiB, kfacA, kfacB, phi0, dphi, kfac;
-    real phi, ddphi, ddp, ddp2, dp, sign, d2r, fc, L1;
+    real phi, ddphi, ddp, ddp2, dp, sign, d2r, L1;
     rvec r_ij, r_kj, r_kl, m, n;
 
     L1 = 1.0-lambda;
@@ -2833,7 +2884,6 @@ real restrangles(int nbonds,
 {
     int  i, d, ai, aj, ak, type, m;
     int  t1, t2;
-    rvec r_ij, r_kj;
     real v, vtot;
     ivec jt, dt_ij, dt_kj;
     rvec f_i, f_j, f_k;
@@ -2961,7 +3011,7 @@ real restrdihs(int nbonds,
         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], vec_temp);
         pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
-        t3 = pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
+        pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
         pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
 
         /* This function computes factors needed for restricted angle potential.
@@ -3072,7 +3122,7 @@ real cbtdihs(int nbonds,
         pbc_rvec_sub(pbc, x[aj], x[ai], delta_ante);
         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], vec_temp);
         pbc_rvec_sub(pbc, x[ak], x[aj], delta_crnt);
-        t3 = pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
+        pbc_rvec_sub(pbc, x[ak], x[al], vec_temp);
         pbc_rvec_sub(pbc, x[al], x[ak], delta_post);
 
         /* \brief Compute factors for CBT potential
@@ -3247,7 +3297,11 @@ real rbdihs(int nbonds,
     return vtot;
 }
 
-int cmap_setup_grid_index(int ip, int grid_spacing, int *ipm1, int *ipp1, int *ipp2)
+//! \endcond
+
+/*! \brief Mysterious undocumented function */
+static int
+cmap_setup_grid_index(int ip, int grid_spacing, int *ipm1, int *ipp1, int *ipp2)
 {
     int im1, ip1, ip2;
 
@@ -3286,14 +3340,16 @@ int cmap_setup_grid_index(int ip, int grid_spacing, int *ipm1, int *ipp1, int *i
 
 }
 
-real cmap_dihs(int nbonds,
-               const t_iatom forceatoms[], const t_iparams forceparams[],
-               const gmx_cmap_t *cmap_grid,
-               const rvec x[], rvec f[], rvec fshift[],
-               const t_pbc *pbc, const t_graph *g,
-               real gmx_unused lambda, real gmx_unused *dvdlambda,
-               const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
-               int  gmx_unused *global_atom_index)
+/*! \brief Compute CMAP dihedral energies and forces */
+static real
+cmap_dihs(int nbonds,
+          const t_iatom forceatoms[], const t_iparams forceparams[],
+          const gmx_cmap_t *cmap_grid,
+          const rvec x[], rvec f[], rvec fshift[],
+          const t_pbc *pbc, const t_graph *g,
+          real gmx_unused lambda, real gmx_unused *dvdlambda,
+          const t_mdatoms gmx_unused *md, t_fcdata gmx_unused *fcd,
+          int  gmx_unused *global_atom_index)
 {
     int         i, j, k, n, idx;
     int         ai, aj, ak, al, am;
@@ -3302,13 +3358,13 @@ real cmap_dihs(int nbonds,
     int         t11, t21, t31, t12, t22, t32;
     int         iphi1, ip1m1, ip1p1, ip1p2;
     int         iphi2, ip2m1, ip2p1, ip2p2;
-    int         l1, l2, l3, l4;
-    int         pos1, pos2, pos3, pos4, tmp;
+    int         l1, l2, l3;
+    int         pos1, pos2, pos3, pos4;
 
     real        ty[4], ty1[4], ty2[4], ty12[4], tc[16], tx[16];
-    real        phi1, psi1, cos_phi1, sin_phi1, sign1, xphi1;
-    real        phi2, psi2, cos_phi2, sin_phi2, sign2, xphi2;
-    real        dx, xx, tt, tu, e, df1, df2, ddf1, ddf2, ddf12, vtot;
+    real        phi1, cos_phi1, sin_phi1, sign1, xphi1;
+    real        phi2, cos_phi2, sin_phi2, sign2, xphi2;
+    real        dx, xx, tt, tu, e, df1, df2, vtot;
     real        ra21, rb21, rg21, rg1, rgr1, ra2r1, rb2r1, rabr1;
     real        ra22, rb22, rg22, rg2, rgr2, ra2r2, rb2r2, rabr2;
     real        fg1, hg1, fga1, hgb1, gaa1, gbb1;
@@ -3370,7 +3426,7 @@ real cmap_dihs(int nbonds,
         b1[1] = r1_kl[2]*r1_kj[0]-r1_kl[0]*r1_kj[2];
         b1[2] = r1_kl[0]*r1_kj[1]-r1_kl[1]*r1_kj[0]; /* 9 */
 
-        tmp = pbc_rvec_sub(pbc, x[a1l], x[a1k], h1);
+        pbc_rvec_sub(pbc, x[a1l], x[a1k], h1);
 
         ra21  = iprod(a1, a1);       /* 5 */
         rb21  = iprod(b1, b1);       /* 5 */
@@ -3431,7 +3487,7 @@ real cmap_dihs(int nbonds,
         b2[1] = r2_kl[2]*r2_kj[0]-r2_kl[0]*r2_kj[2];
         b2[2] = r2_kl[0]*r2_kj[1]-r2_kl[1]*r2_kj[0]; /* 9 */
 
-        tmp = pbc_rvec_sub(pbc, x[a2l], x[a2k], h2);
+        pbc_rvec_sub(pbc, x[a2l], x[a2k], h2);
 
         ra22  = iprod(a2, a2);         /* 5 */
         rb22  = iprod(b2, b2);         /* 5 */
@@ -3496,8 +3552,8 @@ real cmap_dihs(int nbonds,
         dx = 2*M_PI / cmap_grid->grid_spacing;
 
         /* Where on the grid are we */
-        iphi1 = (int)(xphi1/dx);
-        iphi2 = (int)(xphi2/dx);
+        iphi1 = static_cast<int>(xphi1/dx);
+        iphi2 = static_cast<int>(xphi2/dx);
 
         iphi1 = cmap_setup_grid_index(iphi1, cmap_grid->grid_spacing, &ip1m1, &ip1p1, &ip1p2);
         iphi2 = cmap_setup_grid_index(iphi2, cmap_grid->grid_spacing, &ip2m1, &ip2p1, &ip2p2);
@@ -3562,9 +3618,6 @@ real cmap_dihs(int nbonds,
         e     = 0;
         df1   = 0;
         df2   = 0;
-        ddf1  = 0;
-        ddf2  = 0;
-        ddf12 = 0;
 
         for (i = 3; i >= 0; i--)
         {
@@ -3575,19 +3628,11 @@ real cmap_dihs(int nbonds,
             e     = tt * e    + ((tc[i*4+3]*tu+tc[i*4+2])*tu + tc[i*4+1])*tu+tc[i*4];
             df1   = tu * df1  + (3.0*tc[l1]*tt+2.0*tc[l2])*tt+tc[l3];
             df2   = tt * df2  + (3.0*tc[i*4+3]*tu+2.0*tc[i*4+2])*tu+tc[i*4+1];
-            ddf1  = tu * ddf1 + 2.0*3.0*tc[l1]*tt+2.0*tc[l2];
-            ddf2  = tt * ddf2 + 2.0*3.0*tc[4*i+3]*tu+2.0*tc[4*i+2];
         }
 
-        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) +
-            3.0*tu*tu*(tc[7]+2.0*tc[11]*tt+3.0*tc[15]*tt*tt);
-
         fac     = RAD2DEG/dx;
         df1     = df1   * fac;
         df2     = df2   * fac;
-        ddf1    = ddf1  * fac * fac;
-        ddf2    = ddf2  * fac * fac;
-        ddf12   = ddf12 * fac * fac;
 
         /* CMAP energy */
         vtot += e;
@@ -3694,7 +3739,7 @@ real cmap_dihs(int nbonds,
 }
 
 
-
+//! \cond
 /***********************************************************
  *
  *   G R O M O S  9 6   F U N C T I O N S
@@ -3953,7 +3998,7 @@ real cross_bond_angle(int nbonds,
     /* Potential from Lawrence and Skimmer, Chem. Phys. Lett. 372 (2003)
      * pp. 842-847
      */
-    int  i, ai, aj, ak, type, m, t1, t2, t3;
+    int  i, ai, aj, ak, type, m, t1, t2;
     rvec r_ij, r_kj, r_ik;
     real vtot, vrt, s1, s2, s3, r1, r2, r3, r1e, r2e, r3e, krt, k1, k2, k3;
     rvec f_i, f_j, f_k;
@@ -3974,7 +4019,7 @@ real cross_bond_angle(int nbonds,
         /* Compute distance vectors ... */
         t1 = pbc_rvec_sub(pbc, x[ai], x[aj], r_ij);
         t2 = pbc_rvec_sub(pbc, x[ak], x[aj], r_kj);
-        t3 = pbc_rvec_sub(pbc, x[ai], x[ak], r_ik);
+        pbc_rvec_sub(pbc, x[ai], x[ak], r_ik);
 
         /* ... and their lengths */
         r1 = norm(r_ij);
@@ -4032,7 +4077,7 @@ static real bonded_tab(const char *type, int table_nr,
 {
     real k, tabscale, *VFtab, rt, eps, eps2, Yt, Ft, Geps, Heps2, Fp, VV, FF;
     int  n0, nnn;
-    real v, f, dvdlambda;
+    real dvdlambda;
 
     k = (1.0 - lambda)*kA + lambda*kB;
 
@@ -4040,7 +4085,7 @@ static real bonded_tab(const char *type, int table_nr,
     VFtab    = table->data;
 
     rt    = r*tabscale;
-    n0    = rt;
+    n0    = static_cast<int>(rt);
     if (n0 >= table->n)
     {
         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",
@@ -4167,7 +4212,7 @@ real tab_angles(int nbonds,
         if (cos_theta2 < 1)
         {
             int  m;
-            real snt, st, sth;
+            real st, sth;
             real cik, cii, ckk;
             real nrkj2, nrij2;
             rvec f_i, f_j, f_k;
@@ -4261,11 +4306,10 @@ real tab_dihs(int nbonds,
     return vtot;
 }
 
-/* Return if this is a potential calculated in bondfree.c,
- * i.e. an interaction that actually calculates a potential and
- * works on multiple atoms (not e.g. a connection or a position restraint).
- */
-static gmx_inline gmx_bool ftype_is_bonded_potential(int ftype)
+//! \endcond
+
+gmx_bool
+ftype_is_bonded_potential(int ftype)
 {
     return
         (interaction_function[ftype].flags & IF_BOND) &&
@@ -4273,173 +4317,7 @@ static gmx_inline gmx_bool ftype_is_bonded_potential(int ftype)
         (ftype < F_GB12 || ftype > F_GB14);
 }
 
-static void divide_bondeds_over_threads(t_idef *idef, int nthreads)
-{
-    int ftype;
-    int nat1;
-    int t;
-    int il_nr_thread;
-
-    idef->nthreads = nthreads;
-
-    if (F_NRE*(nthreads+1) > idef->il_thread_division_nalloc)
-    {
-        idef->il_thread_division_nalloc = F_NRE*(nthreads+1);
-        snew(idef->il_thread_division, idef->il_thread_division_nalloc);
-    }
-
-    for (ftype = 0; ftype < F_NRE; ftype++)
-    {
-        if (ftype_is_bonded_potential(ftype))
-        {
-            nat1 = interaction_function[ftype].nratoms + 1;
-
-            for (t = 0; t <= nthreads; t++)
-            {
-                /* Divide the interactions equally over the threads.
-                 * When the different types of bonded interactions
-                 * are distributed roughly equally over the threads,
-                 * this should lead to well localized output into
-                 * the force buffer on each thread.
-                 * If this is not the case, a more advanced scheme
-                 * (not implemented yet) will do better.
-                 */
-                il_nr_thread = (((idef->il[ftype].nr/nat1)*t)/nthreads)*nat1;
-
-                /* Ensure that distance restraint pairs with the same label
-                 * end up on the same thread.
-                 * This is slighlty tricky code, since the next for iteration
-                 * may have an initial il_nr_thread lower than the final value
-                 * in the previous iteration, but this will anyhow be increased
-                 * to the approriate value again by this while loop.
-                 */
-                while (ftype == F_DISRES &&
-                       il_nr_thread > 0 &&
-                       il_nr_thread < idef->il[ftype].nr &&
-                       idef->iparams[idef->il[ftype].iatoms[il_nr_thread]].disres.label ==
-                       idef->iparams[idef->il[ftype].iatoms[il_nr_thread-nat1]].disres.label)
-                {
-                    il_nr_thread += nat1;
-                }
-
-                idef->il_thread_division[ftype*(nthreads+1)+t] = il_nr_thread;
-            }
-        }
-    }
-}
-
-static unsigned
-calc_bonded_reduction_mask(const t_idef *idef,
-                           int shift,
-                           int t, int nt)
-{
-    unsigned mask;
-    int      ftype, nb, nat1, nb0, nb1, i, a;
-
-    mask = 0;
-
-    for (ftype = 0; ftype < F_NRE; ftype++)
-    {
-        if (ftype_is_bonded_potential(ftype))
-        {
-            nb = idef->il[ftype].nr;
-            if (nb > 0)
-            {
-                nat1 = interaction_function[ftype].nratoms + 1;
-
-                /* Divide this interaction equally over the threads.
-                 * This is not stored: should match division in calc_bonds.
-                 */
-                nb0 = idef->il_thread_division[ftype*(nt+1)+t];
-                nb1 = idef->il_thread_division[ftype*(nt+1)+t+1];
-
-                for (i = nb0; i < nb1; i += nat1)
-                {
-                    for (a = 1; a < nat1; a++)
-                    {
-                        mask |= (1U << (idef->il[ftype].iatoms[i+a]>>shift));
-                    }
-                }
-            }
-        }
-    }
-
-    return mask;
-}
-
-void setup_bonded_threading(t_forcerec   *fr, t_idef *idef)
-{
-#define MAX_BLOCK_BITS 32
-    int t;
-    int ctot, c, b;
-
-    assert(fr->nthreads >= 1);
-
-    /* Divide the bonded interaction over the threads */
-    divide_bondeds_over_threads(idef, fr->nthreads);
-
-    if (fr->nthreads == 1)
-    {
-        fr->red_nblock = 0;
-
-        return;
-    }
-
-    /* We divide the force array in a maximum of 32 blocks.
-     * Minimum force block reduction size is 2^6=64.
-     */
-    fr->red_ashift = 6;
-    while (fr->natoms_force > (int)(MAX_BLOCK_BITS*(1U<<fr->red_ashift)))
-    {
-        fr->red_ashift++;
-    }
-    if (debug)
-    {
-        fprintf(debug, "bonded force buffer block atom shift %d bits\n",
-                fr->red_ashift);
-    }
-
-    /* Determine to which blocks each thread's bonded force calculation
-     * contributes. Store this is a mask for each thread.
-     */
-#pragma omp parallel for num_threads(fr->nthreads) schedule(static)
-    for (t = 1; t < fr->nthreads; t++)
-    {
-        fr->f_t[t].red_mask =
-            calc_bonded_reduction_mask(idef, fr->red_ashift, t, fr->nthreads);
-    }
-
-    /* Determine the maximum number of blocks we need to reduce over */
-    fr->red_nblock = 0;
-    ctot           = 0;
-    for (t = 0; t < fr->nthreads; t++)
-    {
-        c = 0;
-        for (b = 0; b < MAX_BLOCK_BITS; b++)
-        {
-            if (fr->f_t[t].red_mask & (1U<<b))
-            {
-                fr->red_nblock = max(fr->red_nblock, b+1);
-                c++;
-            }
-        }
-        if (debug)
-        {
-            fprintf(debug, "thread %d flags %x count %d\n",
-                    t, fr->f_t[t].red_mask, c);
-        }
-        ctot += c;
-    }
-    if (debug)
-    {
-        fprintf(debug, "Number of blocks to reduce: %d of size %d\n",
-                fr->red_nblock, 1<<fr->red_ashift);
-        fprintf(debug, "Reduction density %.2f density/#thread %.2f\n",
-                ctot*(1<<fr->red_ashift)/(double)fr->natoms_force,
-                ctot*(1<<fr->red_ashift)/(double)(fr->natoms_force*fr->nthreads));
-    }
-}
-
+/*! \brief Zero thread-local force-output buffers */
 static void zero_thread_forces(f_thread_t *f_t, int n,
                                int nblock, int blocksize)
 {
@@ -4458,7 +4336,7 @@ static void zero_thread_forces(f_thread_t *f_t, int n,
             if (f_t->red_mask && (1U<<b))
             {
                 a0 = b*blocksize;
-                a1 = min((b+1)*blocksize, n);
+                a1 = std::min((b+1)*blocksize, n);
                 for (a = a0; a < a1; a++)
                 {
                     clear_rvec(f_t->f[a]);
@@ -4487,15 +4365,16 @@ static void zero_thread_forces(f_thread_t *f_t, int n,
     }
 }
 
+/*! \brief The max thread number is arbitrary, we used a fixed number
+ * to avoid memory management.  Using more than 16 threads is probably
+ * never useful performance wise. */
+#define MAX_BONDED_THREADS 256
+
+/*! \brief Reduce thread-local force buffers */
 static void reduce_thread_force_buffer(int n, rvec *f,
                                        int nthreads, f_thread_t *f_t,
                                        int nblock, int block_size)
 {
-    /* The max thread number is arbitrary,
-     * we used a fixed number to avoid memory management.
-     * Using more than 16 threads is probably never useful performance wise.
-     */
-#define MAX_BONDED_THREADS 256
     int b;
 
     if (nthreads > MAX_BONDED_THREADS)
@@ -4528,7 +4407,7 @@ static void reduce_thread_force_buffer(int n, rvec *f,
             /* Reduce force buffers for threads that contribute */
             a0 =  b   *block_size;
             a1 = (b+1)*block_size;
-            a1 = min(a1, n);
+            a1 = std::min(a1, n);
             for (a = a0; a < a1; a++)
             {
                 for (fb = 0; fb < nfb; fb++)
@@ -4540,6 +4419,7 @@ static void reduce_thread_force_buffer(int n, rvec *f,
     }
 }
 
+/*! \brief Reduce thread-local forces */
 static void reduce_thread_forces(int n, rvec *f, rvec *fshift,
                                  real *ener, gmx_grppairener_t *grpp, real *dvdl,
                                  int nthreads, f_thread_t *f_t,
@@ -4597,9 +4477,11 @@ static void reduce_thread_forces(int n, rvec *f, rvec *fshift,
     }
 }
 
-static real calc_one_bond(FILE *fplog, int thread,
+/*! \brief Calculate one element of the list of bonded interactions
+    for this thread */
+static real calc_one_bond(int thread,
                           int ftype, const t_idef *idef,
-                          rvec x[], rvec f[], rvec fshift[],
+                          const rvec x[], rvec f[], rvec fshift[],
                           t_forcerec *fr,
                           const t_pbc *pbc, const t_graph *g,
                           gmx_grppairener_t *grpp,
@@ -4607,7 +4489,7 @@ static real calc_one_bond(FILE *fplog, int thread,
                           real *lambda, real *dvdl,
                           const t_mdatoms *md, t_fcdata *fcd,
                           gmx_bool bCalcEnerVir,
-                          int *global_atom_index, gmx_bool bPrintSepPot)
+                          int *global_atom_index)
 {
     int      nat1, nbonds, efptFTYPE;
     real     v = 0;
@@ -4636,7 +4518,7 @@ static real calc_one_bond(FILE *fplog, int thread,
         {
             v = cmap_dihs(nbn, iatoms+nb0,
                           idef->iparams, &idef->cmap_grid,
-                          (const rvec*)x, f, fshift,
+                          x, f, fshift,
                           pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
                           md, fcd, global_atom_index);
         }
@@ -4647,7 +4529,7 @@ static real calc_one_bond(FILE *fplog, int thread,
             /* No energies, shift forces, dvdl */
             angles_noener_simd(nbn, idef->il[ftype].iatoms+nb0,
                                idef->iparams,
-                               (const rvec*)x, f,
+                               x, f,
                                pbc, g, lambda[efptFTYPE], md, fcd,
                                global_atom_index);
             v = 0;
@@ -4664,7 +4546,7 @@ static real calc_one_bond(FILE *fplog, int thread,
 #endif
                 (nbn, idef->il[ftype].iatoms+nb0,
                 idef->iparams,
-                (const rvec*)x, f,
+                x, f,
                 pbc, g, lambda[efptFTYPE], md, fcd,
                 global_atom_index);
             v = 0;
@@ -4686,31 +4568,15 @@ static real calc_one_bond(FILE *fplog, int thread,
         {
             v = interaction_function[ftype].ifunc(nbn, iatoms+nb0,
                                                   idef->iparams,
-                                                  (const rvec*)x, f, fshift,
+                                                  x, f, fshift,
                                                   pbc, g, lambda[efptFTYPE], &(dvdl[efptFTYPE]),
                                                   md, fcd, global_atom_index);
         }
-        if (bPrintSepPot)
-        {
-            fprintf(fplog, "  %-23s #%4d  V %12.5e  dVdl %12.5e\n",
-                    interaction_function[ftype].longname,
-                    nbonds, v, lambda[efptFTYPE]);
-        }
     }
     else
     {
-        v = do_nonbonded_listed(ftype, nbn, iatoms+nb0, idef->iparams, (const rvec*)x, f, fshift,
+        v = do_nonbonded_listed(ftype, nbn, iatoms+nb0, idef->iparams, x, f, fshift,
                                 pbc, g, lambda, dvdl, md, fr, grpp, global_atom_index);
-
-        if (bPrintSepPot)
-        {
-            fprintf(fplog, "  %-5s + %-15s #%4d                  dVdl %12.5e\n",
-                    interaction_function[ftype].longname,
-                    interaction_function[F_LJ14].longname, nbonds, dvdl[efptVDW]);
-            fprintf(fplog, "  %-5s + %-15s #%4d                  dVdl %12.5e\n",
-                    interaction_function[ftype].longname,
-                    interaction_function[F_COUL14].longname, nbonds, dvdl[efptCOUL]);
-        }
     }
 
     if (thread == 0)
@@ -4721,25 +4587,22 @@ static real calc_one_bond(FILE *fplog, int thread,
     return v;
 }
 
-void calc_bonds(FILE *fplog, const gmx_multisim_t *ms,
+void calc_bonds(const gmx_multisim_t *ms,
                 const t_idef *idef,
-                rvec x[], history_t *hist,
+                const rvec x[], history_t *hist,
                 rvec f[], t_forcerec *fr,
-                const t_pbc *pbc, const t_graph *g,
+                const struct t_pbc *pbc, const struct t_graph *g,
                 gmx_enerdata_t *enerd, t_nrnb *nrnb,
                 real *lambda,
                 const t_mdatoms *md,
                 t_fcdata *fcd, int *global_atom_index,
-                t_atomtypes gmx_unused *atype, gmx_genborn_t gmx_unused *born,
-                int force_flags,
-                gmx_bool bPrintSepPot, gmx_int64_t step)
+                int force_flags)
 {
     gmx_bool      bCalcEnerVir;
     int           i;
-    real          v, dvdl[efptNR], dvdl_dum[efptNR]; /* The dummy array is to have a place to store the dhdl at other values
+    real          dvdl[efptNR]; /* The dummy array is to have a place to store the dhdl at other values
                                                         of lambda, which will be thrown away in the end*/
     const  t_pbc *pbc_null;
-    char          buf[22];
     int           thread;
 
     assert(fr->nthreads == idef->nthreads);
@@ -4758,11 +4621,6 @@ void calc_bonds(FILE *fplog, const gmx_multisim_t *ms,
     {
         pbc_null = NULL;
     }
-    if (bPrintSepPot)
-    {
-        fprintf(fplog, "Step %s: bonded V and dVdl for this rank\n",
-                gmx_step_str(step, buf));
-    }
 
 #ifdef DEBUG
     if (g && debug)
@@ -4777,14 +4635,14 @@ void calc_bonds(FILE *fplog, const gmx_multisim_t *ms,
         enerd->term[F_ORIRESDEV] =
             calc_orires_dev(ms, idef->il[F_ORIRES].nr,
                             idef->il[F_ORIRES].iatoms,
-                            idef->iparams, md, (const rvec*)x,
+                            idef->iparams, md, x,
                             pbc_null, fcd, hist);
     }
     if (idef->il[F_DISRES].nr)
     {
         calc_disres_R_6(idef->il[F_DISRES].nr,
                         idef->il[F_DISRES].iatoms,
-                        idef->iparams, (const rvec*)x, pbc_null,
+                        idef->iparams, x, pbc_null,
                         fcd, hist);
 #ifdef GMX_MPI
         if (fcd->disres.nsystems > 1)
@@ -4828,11 +4686,11 @@ void calc_bonds(FILE *fplog, const gmx_multisim_t *ms,
         {
             if (idef->il[ftype].nr > 0 && ftype_is_bonded_potential(ftype))
             {
-                v = calc_one_bond(fplog, thread, ftype, idef, x,
+                v = calc_one_bond(thread, ftype, idef, x,
                                   ft, fshift, fr, pbc_null, g, grpp,
                                   nrnb, lambda, dvdlt,
                                   md, fcd, bCalcEnerVir,
-                                  global_atom_index, bPrintSepPot);
+                                  global_atom_index);
                 epot[ftype] += v;
             }
         }
@@ -4862,18 +4720,17 @@ void calc_bonds(FILE *fplog, const gmx_multisim_t *ms,
     }
 }
 
-void calc_bonds_lambda(FILE *fplog,
-                       const t_idef *idef,
-                       rvec x[],
+void calc_bonds_lambda(const t_idef *idef,
+                       const rvec x[],
                        t_forcerec *fr,
-                       const t_pbc *pbc, const t_graph *g,
+                       const struct t_pbc *pbc, const struct t_graph *g,
                        gmx_grppairener_t *grpp, real *epot, t_nrnb *nrnb,
                        real *lambda,
                        const t_mdatoms *md,
                        t_fcdata *fcd,
                        int *global_atom_index)
 {
-    int           i, ftype, nr_nonperturbed, nr;
+    int           ftype, nr_nonperturbed, nr;
     real          v;
     real          dvdl_dum[efptNR];
     rvec         *f, *fshift;
@@ -4914,11 +4771,11 @@ void calc_bonds_lambda(FILE *fplog,
 
             if (nr - nr_nonperturbed > 0)
             {
-                v = calc_one_bond(fplog, 0, ftype, &idef_fe,
+                v = calc_one_bond(0, ftype, &idef_fe,
                                   x, f, fshift, fr, pbc_null, g,
                                   grpp, nrnb, lambda, dvdl_dum,
                                   md, fcd, TRUE,
-                                  global_atom_index, FALSE);
+                                  global_atom_index);
                 epot[ftype] += v;
             }
         }