Remove unnecessary config.h includes
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / calc_verletbuf.c
index f0cba0732e8869f8c2cbbaa51399e3c96d6dd2e8..8d1313a4083a8fc3d976f91dfab58e06cfdee47c 100644 (file)
-/*  -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
+/*
+ * This file is part of the GROMACS molecular simulation package.
  *
+ * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
  *
- *                This source code is part of
- *
- *                 G   R   O   M   A   C   S
- *
- *          GROningen MAchine for Chemical Simulations
- *
- *                        VERSION 3.2.03
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2004, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
  * of the License, or (at your option) any later version.
  *
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
  *
- * For more info, check our website at http://www.gromacs.org
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
  *
- * And Hey:
- * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
+ * 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
+#include "gmxpre.h"
 
 #include <assert.h>
+#include <math.h>
+#include <stdlib.h>
 
 #include <sys/types.h>
-#include <math.h>
-#include "typedefs.h"
-#include "physics.h"
-#include "smalloc.h"
-#include "gmx_fatal.h"
-#include "macros.h"
-#include "vec.h"
-#include "coulomb.h"
+
+#include "gromacs/legacyheaders/typedefs.h"
+#include "gromacs/math/units.h"
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/math/vec.h"
+#include "gromacs/legacyheaders/coulomb.h"
 #include "calc_verletbuf.h"
 #include "../mdlib/nbnxn_consts.h"
 
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/smalloc.h"
+
+#ifdef GMX_NBNXN_SIMD
+/* The include below sets the SIMD instruction type (precision+width)
+ * for all nbnxn SIMD search and non-bonded kernel code.
+ */
+#ifdef GMX_NBNXN_HALF_WIDTH_SIMD
+#define GMX_USE_HALF_WIDTH_SIMD_HERE
+#endif
+#include "gromacs/simd/simd.h"
+#endif
+
+
+/* The code in this file estimates a pairlist buffer length
+ * given a target energy drift per atom per picosecond.
+ * This is done by estimating the drift given a buffer length.
+ * Ideally we would like to have a tight overestimate of the drift,
+ * but that can be difficult to achieve.
+ *
+ * Significant approximations used:
+ *
+ * Uniform particle density. UNDERESTIMATES the drift by rho_global/rho_local.
+ *
+ * Interactions don't affect particle motion. OVERESTIMATES the drift on longer
+ * time scales. This approximation probably introduces the largest errors.
+ *
+ * Only take one constraint per particle into account: OVERESTIMATES the drift.
+ *
+ * For rotating constraints assume the same functional shape for time scales
+ * where the constraints rotate significantly as the exact expression for
+ * short time scales. OVERESTIMATES the drift on long time scales.
+ *
+ * For non-linear virtual sites use the mass of the lightest constructing atom
+ * to determine the displacement. OVER/UNDERESTIMATES the drift, depending on
+ * the geometry and masses of constructing atoms.
+ *
+ * Note that the formulas for normal atoms and linear virtual sites are exact,
+ * apart from the first two approximations.
+ *
+ * Note that apart from the effect of the above approximations, the actual
+ * drift of the total energy of a system can be order of magnitude smaller
+ * due to cancellation of positive and negative drift for different pairs.
+ */
+
+
 /* Struct for unique atom type for calculating the energy drift.
  * The atom displacement depends on mass and constraints.
  * The energy jump for given distance depend on LJ type and q.
  */
 typedef struct
 {
-    real     mass; /* mass */
-    int      type; /* type (used for LJ parameters) */
-    real     q;    /* charge */
-    int      con;  /* constrained: 0, else 1, if 1, use #DOF=2 iso 3 */
-    int      n;    /* total #atoms of this type in the system */
-} verletbuf_atomtype_t;
+    real     mass;     /* mass */
+    int      type;     /* type (used for LJ parameters) */
+    real     q;        /* charge */
+    gmx_bool bConstr;  /* constrained, if TRUE, use #DOF=2 iso 3 */
+    real     con_mass; /* mass of heaviest atom connected by constraints */
+    real     con_len;  /* constraint length to the heaviest atom */
+} atom_nonbonded_kinetic_prop_t;
 
+/* Struct for unique atom type for calculating the energy drift.
+ * The atom displacement depends on mass and constraints.
+ * The energy jump for given distance depend on LJ type and q.
+ */
+typedef struct
+{
+    atom_nonbonded_kinetic_prop_t prop; /* non-bonded and kinetic atom prop. */
+    int                           n;    /* #atoms of this type in the system */
+} verletbuf_atomtype_t;
 
 void verletbuf_get_list_setup(gmx_bool                bGPU,
                               verletbuf_list_setup_t *list_setup)
@@ -79,7 +132,7 @@ void verletbuf_get_list_setup(gmx_bool                bGPU,
 #ifndef GMX_NBNXN_SIMD
         list_setup->cluster_size_j = NBNXN_CPU_CLUSTER_I_SIZE;
 #else
-        list_setup->cluster_size_j = GMX_NBNXN_SIMD_BITWIDTH/(sizeof(real)*8);
+        list_setup->cluster_size_j = GMX_SIMD_REAL_WIDTH;
 #ifdef GMX_NBNXN_SIMD_2XNN
         /* We assume the smallest cluster size to be on the safe side */
         list_setup->cluster_size_j /= 2;
@@ -88,13 +141,26 @@ void verletbuf_get_list_setup(gmx_bool                bGPU,
     }
 }
 
+static gmx_bool
+atom_nonbonded_kinetic_prop_equal(const atom_nonbonded_kinetic_prop_t *prop1,
+                                  const atom_nonbonded_kinetic_prop_t *prop2)
+{
+    return (prop1->mass     == prop2->mass &&
+            prop1->type     == prop2->type &&
+            prop1->q        == prop2->q &&
+            prop1->bConstr  == prop2->bConstr &&
+            prop1->con_mass == prop2->con_mass &&
+            prop1->con_len  == prop2->con_len);
+}
+
 static void add_at(verletbuf_atomtype_t **att_p, int *natt_p,
-                   real mass, int type, real q, int con, int nmol)
+                   const atom_nonbonded_kinetic_prop_t *prop,
+                   int nmol)
 {
-    verletbuf_atomtype_t *att;
-    int                   natt, i;
+    verletbuf_atomtype_t   *att;
+    int                     natt, i;
 
-    if (mass == 0)
+    if (prop->mass == 0)
     {
         /* Ignore massless particles */
         return;
@@ -104,11 +170,7 @@ static void add_at(verletbuf_atomtype_t **att_p, int *natt_p,
     natt = *natt_p;
 
     i = 0;
-    while (i < natt &&
-           !(mass == att[i].mass &&
-             type == att[i].type &&
-             q    == att[i].q &&
-             con  == att[i].con))
+    while (i < natt && !atom_nonbonded_kinetic_prop_equal(prop, &att[i].prop))
     {
         i++;
     }
@@ -121,27 +183,138 @@ static void add_at(verletbuf_atomtype_t **att_p, int *natt_p,
     {
         (*natt_p)++;
         srenew(*att_p, *natt_p);
-        (*att_p)[i].mass = mass;
-        (*att_p)[i].type = type;
-        (*att_p)[i].q    = q;
-        (*att_p)[i].con  = con;
+        (*att_p)[i].prop = *prop;
         (*att_p)[i].n    = nmol;
     }
 }
 
+static void get_vsite_masses(const gmx_moltype_t  *moltype,
+                             const gmx_ffparams_t *ffparams,
+                             real                 *vsite_m,
+                             int                  *n_nonlin_vsite)
+{
+    int            ft, i;
+    const t_ilist *il;
+
+    *n_nonlin_vsite = 0;
+
+    /* Check for virtual sites, determine mass from constructing atoms */
+    for (ft = 0; ft < F_NRE; ft++)
+    {
+        if (IS_VSITE(ft))
+        {
+            il = &moltype->ilist[ft];
+
+            for (i = 0; i < il->nr; i += 1+NRAL(ft))
+            {
+                const t_iparams *ip;
+                real             cam[5] = {0}, inv_mass, m_aj;
+                int              a1, j, aj, coeff;
+
+                ip = &ffparams->iparams[il->iatoms[i]];
+
+                a1 = il->iatoms[i+1];
+
+                if (ft != F_VSITEN)
+                {
+                    for (j = 1; j < NRAL(ft); j++)
+                    {
+                        cam[j] = moltype->atoms.atom[il->iatoms[i+1+j]].m;
+                        if (cam[j] == 0)
+                        {
+                            cam[j] = vsite_m[il->iatoms[i+1+j]];
+                        }
+                        if (cam[j] == 0)
+                        {
+                            gmx_fatal(FARGS, "In molecule type '%s' %s construction involves atom %d, which is a virtual site of equal or high complexity. This is not supported.",
+                                      *moltype->name,
+                                      interaction_function[ft].longname,
+                                      il->iatoms[i+1+j]+1);
+                        }
+                    }
+                }
+
+                switch (ft)
+                {
+                    case F_VSITE2:
+                        /* Exact */
+                        vsite_m[a1] = (cam[1]*cam[2])/(cam[2]*sqr(1-ip->vsite.a) + cam[1]*sqr(ip->vsite.a));
+                        break;
+                    case F_VSITE3:
+                        /* Exact */
+                        vsite_m[a1] = (cam[1]*cam[2]*cam[3])/(cam[2]*cam[3]*sqr(1-ip->vsite.a-ip->vsite.b) + cam[1]*cam[3]*sqr(ip->vsite.a) + cam[1]*cam[2]*sqr(ip->vsite.b));
+                        break;
+                    case F_VSITEN:
+                        /* Exact */
+                        inv_mass = 0;
+                        for (j = 0; j < 3*ip->vsiten.n; j += 3)
+                        {
+                            aj    = il->iatoms[i+j+2];
+                            coeff = ip[il->iatoms[i+j]].vsiten.a;
+                            if (moltype->atoms.atom[aj].ptype == eptVSite)
+                            {
+                                m_aj = vsite_m[aj];
+                            }
+                            else
+                            {
+                                m_aj = moltype->atoms.atom[aj].m;
+                            }
+                            if (m_aj <= 0)
+                            {
+                                gmx_incons("The mass of a vsiten constructing atom is <= 0");
+                            }
+                            inv_mass += coeff*coeff/m_aj;
+                        }
+                        vsite_m[a1] = 1/inv_mass;
+                        /* Correct for loop increment of i */
+                        i += j - 1 - NRAL(ft);
+                        break;
+                    default:
+                        /* Use the mass of the lightest constructing atom.
+                         * This is an approximation.
+                         * If the distance of the virtual site to the
+                         * constructing atom is less than all distances
+                         * between constructing atoms, this is a safe
+                         * over-estimate of the displacement of the vsite.
+                         * This condition holds for all H mass replacement
+                         * vsite constructions, except for SP2/3 groups.
+                         * In SP3 groups one H will have a F_VSITE3
+                         * construction, so even there the total drift
+                         * estimate shouldn't be far off.
+                         */
+                        assert(j >= 1);
+                        vsite_m[a1] = cam[1];
+                        for (j = 2; j < NRAL(ft); j++)
+                        {
+                            vsite_m[a1] = min(vsite_m[a1], cam[j]);
+                        }
+                        (*n_nonlin_vsite)++;
+                        break;
+                }
+                if (gmx_debug_at)
+                {
+                    fprintf(debug, "atom %4d %-20s mass %6.3f\n",
+                            a1, interaction_function[ft].longname, vsite_m[a1]);
+                }
+            }
+        }
+    }
+}
+
 static void get_verlet_buffer_atomtypes(const gmx_mtop_t      *mtop,
                                         verletbuf_atomtype_t **att_p,
                                         int                   *natt_p,
                                         int                   *n_nonlin_vsite)
 {
-    verletbuf_atomtype_t *att;
-    int                   natt;
-    int                   mb, nmol, ft, i, j, a1, a2, a3, a;
-    const t_atoms        *atoms;
-    const t_ilist        *il;
-    const t_atom         *at;
-    const t_iparams      *ip;
-    real                 *con_m, *vsite_m, cam[5];
+    verletbuf_atomtype_t          *att;
+    int                            natt;
+    int                            mb, nmol, ft, i, a1, a2, a3, a;
+    const t_atoms                 *atoms;
+    const t_ilist                 *il;
+    const t_iparams               *ip;
+    atom_nonbonded_kinetic_prop_t *prop;
+    real                          *vsite_m;
+    int                            n_nonlin_vsite_mol;
 
     att  = NULL;
     natt = 0;
@@ -157,8 +330,11 @@ static void get_verlet_buffer_atomtypes(const gmx_mtop_t      *mtop,
 
         atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
 
-        /* Check for constraints, as they affect the kinetic energy */
-        snew(con_m, atoms->nr);
+        /* Check for constraints, as they affect the kinetic energy.
+         * For virtual sites we need the masses and geometry of
+         * the constructing atoms to determine their velocity distribution.
+         */
+        snew(prop, atoms->nr);
         snew(vsite_m, atoms->nr);
 
         for (ft = F_CONSTR; ft <= F_CONSTRNC; ft++)
@@ -167,10 +343,19 @@ static void get_verlet_buffer_atomtypes(const gmx_mtop_t      *mtop,
 
             for (i = 0; i < il->nr; i += 1+NRAL(ft))
             {
+                ip         = &mtop->ffparams.iparams[il->iatoms[i]];
                 a1         = il->iatoms[i+1];
                 a2         = il->iatoms[i+2];
-                con_m[a1] += atoms->atom[a2].m;
-                con_m[a2] += atoms->atom[a1].m;
+                if (atoms->atom[a2].m > prop[a1].con_mass)
+                {
+                    prop[a1].con_mass = atoms->atom[a2].m;
+                    prop[a1].con_len  = ip->constr.dA;
+                }
+                if (atoms->atom[a1].m > prop[a2].con_mass)
+                {
+                    prop[a2].con_mass = atoms->atom[a1].m;
+                    prop[a2].con_len  = ip->constr.dA;
+                }
             }
         }
 
@@ -178,103 +363,70 @@ static void get_verlet_buffer_atomtypes(const gmx_mtop_t      *mtop,
 
         for (i = 0; i < il->nr; i += 1+NRAL(F_SETTLE))
         {
+            ip         = &mtop->ffparams.iparams[il->iatoms[i]];
             a1         = il->iatoms[i+1];
             a2         = il->iatoms[i+2];
             a3         = il->iatoms[i+3];
-            con_m[a1] += atoms->atom[a2].m + atoms->atom[a3].m;
-            con_m[a2] += atoms->atom[a1].m + atoms->atom[a3].m;
-            con_m[a3] += atoms->atom[a1].m + atoms->atom[a2].m;
-        }
-
-        /* Check for virtual sites, determine mass from constructing atoms */
-        for (ft = 0; ft < F_NRE; ft++)
-        {
-            if (IS_VSITE(ft))
-            {
-                il = &mtop->moltype[mtop->molblock[mb].type].ilist[ft];
-
-                for (i = 0; i < il->nr; i += 1+NRAL(ft))
-                {
-                    ip = &mtop->ffparams.iparams[il->iatoms[i]];
+            /* Usually the mass of a1 (usually oxygen) is larger than a2/a3.
+             * If this is not the case, we overestimate the displacement,
+             * which leads to a larger buffer (ok since this is an exotic case).
+             */
+            prop[a1].con_mass = atoms->atom[a2].m;
+            prop[a1].con_len  = ip->settle.doh;
 
-                    a1 = il->iatoms[i+1];
+            prop[a2].con_mass = atoms->atom[a1].m;
+            prop[a2].con_len  = ip->settle.doh;
 
-                    for (j = 1; j < NRAL(ft); j++)
-                    {
-                        cam[j] = atoms->atom[il->iatoms[i+1+j]].m;
-                        if (cam[j] == 0)
-                        {
-                            cam[j] = vsite_m[il->iatoms[i+1+j]];
-                        }
-                        if (cam[j] == 0)
-                        {
-                            gmx_fatal(FARGS, "In molecule type '%s' %s construction involves atom %d, which is a virtual site of equal or high complexity. This is not supported.",
-                                      *mtop->moltype[mtop->molblock[mb].type].name,
-                                      interaction_function[ft].longname,
-                                      il->iatoms[i+1+j]+1);
-                        }
-                    }
+            prop[a3].con_mass = atoms->atom[a1].m;
+            prop[a3].con_len  = ip->settle.doh;
+        }
 
-                    switch (ft)
-                    {
-                        case F_VSITE2:
-                            /* Exact except for ignoring constraints */
-                            vsite_m[a1] = (cam[2]*sqr(1-ip->vsite.a) + cam[1]*sqr(ip->vsite.a))/(cam[1]*cam[2]);
-                            break;
-                        case F_VSITE3:
-                            /* Exact except for ignoring constraints */
-                            vsite_m[a1] = (cam[2]*cam[3]*sqr(1-ip->vsite.a-ip->vsite.b) + cam[1]*cam[3]*sqr(ip->vsite.a) + cam[1]*cam[2]*sqr(ip->vsite.b))/(cam[1]*cam[2]*cam[3]);
-                            break;
-                        default:
-                            /* Use the mass of the lightest constructing atom.
-                             * This is an approximation.
-                             * If the distance of the virtual site to the
-                             * constructing atom is less than all distances
-                             * between constructing atoms, this is a safe
-                             * over-estimate of the displacement of the vsite.
-                             * This condition holds for all H mass replacement
-                             * replacement vsite constructions, except for SP2/3
-                             * groups. In SP3 groups one H will have a F_VSITE3
-                             * construction, so even there the total drift
-                             * estimation shouldn't be far off.
-                             */
-                            assert(j >= 1);
-                            vsite_m[a1] = cam[1];
-                            for (j = 2; j < NRAL(ft); j++)
-                            {
-                                vsite_m[a1] = min(vsite_m[a1], cam[j]);
-                            }
-                            if (n_nonlin_vsite != NULL)
-                            {
-                                *n_nonlin_vsite += nmol;
-                            }
-                            break;
-                    }
-                }
-            }
+        get_vsite_masses(&mtop->moltype[mtop->molblock[mb].type],
+                         &mtop->ffparams,
+                         vsite_m,
+                         &n_nonlin_vsite_mol);
+        if (n_nonlin_vsite != NULL)
+        {
+            *n_nonlin_vsite += nmol*n_nonlin_vsite_mol;
         }
 
         for (a = 0; a < atoms->nr; a++)
         {
-            at = &atoms->atom[a];
+            if (atoms->atom[a].ptype == eptVSite)
+            {
+                prop[a].mass = vsite_m[a];
+            }
+            else
+            {
+                prop[a].mass = atoms->atom[a].m;
+            }
+            prop[a].type     = atoms->atom[a].type;
+            prop[a].q        = atoms->atom[a].q;
             /* We consider an atom constrained, #DOF=2, when it is
-             * connected with constraints to one or more atoms with
-             * total mass larger than 1.5 that of the atom itself.
+             * connected with constraints to (at least one) atom with
+             * a mass of more than 0.4x its own mass. This is not a critical
+             * parameter, since with roughly equal masses the unconstrained
+             * and constrained displacement will not differ much (and both
+             * overestimate the displacement).
              */
-            add_at(&att, &natt,
-                   at->m, at->type, at->q, con_m[a] > 1.5*at->m, nmol);
+            prop[a].bConstr = (prop[a].con_mass > 0.4*prop[a].mass);
+
+            add_at(&att, &natt, &prop[a], nmol);
         }
 
+        /* cppcheck-suppress uninitvar Fixed in cppcheck 1.65 */
         sfree(vsite_m);
-        sfree(con_m);
+        sfree(prop);
     }
 
     if (gmx_debug_at)
     {
         for (a = 0; a < natt; a++)
         {
-            fprintf(debug, "type %d: m %5.2f t %d q %6.3f con %d n %d\n",
-                    a, att[a].mass, att[a].type, att[a].q, att[a].con, att[a].n);
+            fprintf(debug, "type %d: m %5.2f t %d q %6.3f con %d con_m %5.3f con_l %5.3f n %d\n",
+                    a, att[a].prop.mass, att[a].prop.type, att[a].prop.q,
+                    att[a].prop.bConstr, att[a].prop.con_mass, att[a].prop.con_len,
+                    att[a].n);
         }
     }
 
@@ -282,8 +434,101 @@ static void get_verlet_buffer_atomtypes(const gmx_mtop_t      *mtop,
     *natt_p = natt;
 }
 
-static void approx_2dof(real s2, real x,
-                        real *shift, real *scale)
+/* This function computes two components of the estimate of the variance
+ * in the displacement of one atom in a system of two constrained atoms.
+ * Returns in sigma2_2d the variance due to rotation of the constrained
+ * atom around the atom to which it constrained.
+ * Returns in sigma2_3d the variance due to displacement of the COM
+ * of the whole system of the two constrained atoms.
+ *
+ * Note that we only take a single constraint (the one to the heaviest atom)
+ * into account. If an atom has multiple constraints, this will result in
+ * an overestimate of the displacement, which gives a larger drift and buffer.
+ */
+static void constrained_atom_sigma2(real                                 kT_fac,
+                                    const atom_nonbonded_kinetic_prop_t *prop,
+                                    real                                *sigma2_2d,
+                                    real                                *sigma2_3d)
+{
+    real sigma2_rot;
+    real com_dist;
+    real sigma2_rel;
+    real scale;
+
+    /* Here we decompose the motion of a constrained atom into two
+     * components: rotation around the COM and translation of the COM.
+     */
+
+    /* Determine the variance for the displacement of the rotational mode */
+    sigma2_rot = kT_fac/(prop->mass*(prop->mass + prop->con_mass)/prop->con_mass);
+
+    /* The distance from the atom to the COM, i.e. the rotational arm */
+    com_dist = prop->con_len*prop->con_mass/(prop->mass + prop->con_mass);
+
+    /* The variance relative to the arm */
+    sigma2_rel = sigma2_rot/(com_dist*com_dist);
+    /* At 6 the scaling formula has slope 0,
+     * so we keep sigma2_2d constant after that.
+     */
+    if (sigma2_rel < 6)
+    {
+        /* A constrained atom rotates around the atom it is constrained to.
+         * This results in a smaller linear displacement than for a free atom.
+         * For a perfectly circular displacement, this lowers the displacement
+         * by: 1/arcsin(arc_length)
+         * and arcsin(x) = 1 + x^2/6 + ...
+         * For sigma2_rel<<1 the displacement distribution is erfc
+         * (exact formula is provided below). For larger sigma, it is clear
+         * that the displacement can't be larger than 2*com_dist.
+         * It turns out that the distribution becomes nearly uniform.
+         * For intermediate sigma2_rel, scaling down sigma with the third
+         * order expansion of arcsin with argument sigma_rel turns out
+         * to give a very good approximation of the distribution and variance.
+         * Even for larger values, the variance is only slightly overestimated.
+         * Note that the most relevant displacements are in the long tail.
+         * This rotation approximation always overestimates the tail (which
+         * runs to infinity, whereas it should be <= 2*com_dist).
+         * Thus we always overestimate the drift and the buffer size.
+         */
+        scale      = 1/(1 + sigma2_rel/6);
+        *sigma2_2d = sigma2_rot*scale*scale;
+    }
+    else
+    {
+        /* sigma_2d is set to the maximum given by the scaling above.
+         * For large sigma2 the real displacement distribution is close
+         * to uniform over -2*con_len to 2*com_dist.
+         * Our erfc with sigma_2d=sqrt(1.5)*com_dist (which means the sigma
+         * of the erfc output distribution is con_dist) overestimates
+         * the variance and additionally has a long tail. This means
+         * we have a (safe) overestimation of the drift.
+         */
+        *sigma2_2d = 1.5*com_dist*com_dist;
+    }
+
+    /* The constrained atom also moves (in 3D) with the COM of both atoms */
+    *sigma2_3d = kT_fac/(prop->mass + prop->con_mass);
+}
+
+static void get_atom_sigma2(real                                 kT_fac,
+                            const atom_nonbonded_kinetic_prop_t *prop,
+                            real                                *sigma2_2d,
+                            real                                *sigma2_3d)
+{
+    if (prop->bConstr)
+    {
+        /* Complicated constraint calculation in a separate function */
+        constrained_atom_sigma2(kT_fac, prop, sigma2_2d, sigma2_3d);
+    }
+    else
+    {
+        /* Unconstrained atom: trivial */
+        *sigma2_2d = 0;
+        *sigma2_3d = kT_fac/prop->mass;
+    }
+}
+
+static void approx_2dof(real s2, real x, real *shift, real *scale)
 {
     /* A particle with 1 DOF constrained has 2 DOFs instead of 3.
      * This code is also used for particles with multiple constraints,
@@ -305,16 +550,18 @@ static void approx_2dof(real s2, real x,
 static real ener_drift(const verletbuf_atomtype_t *att, int natt,
                        const gmx_ffparams_t *ffp,
                        real kT_fac,
-                       real md_ljd, real md_ljr, real md_el, real dd_el,
+                       real md1_ljd, real d2_ljd, real md3_ljd,
+                       real md1_ljr, real d2_ljr, real md3_ljr,
+                       real md1_el,  real d2_el,
                        real r_buffer,
                        real rlist, real boxvol)
 {
-    double drift_tot, pot1, pot2, pot;
+    double drift_tot, pot1, pot2, pot3, pot;
     int    i, j;
-    real   s2i, s2j, s2, s;
+    real   s2i_2d, s2i_3d, s2j_2d, s2j_3d, s2, s;
     int    ti, tj;
-    real   md, dd;
-    real   sc_fac, rsh;
+    real   md1, d2, md3;
+    real   sc_fac, rsh, rsh2;
     double c_exp, c_erfc;
 
     drift_tot = 0;
@@ -322,42 +569,53 @@ static real ener_drift(const verletbuf_atomtype_t *att, int natt,
     /* Loop over the different atom type pairs */
     for (i = 0; i < natt; i++)
     {
-        s2i = kT_fac/att[i].mass;
-        ti  = att[i].type;
+        get_atom_sigma2(kT_fac, &att[i].prop, &s2i_2d, &s2i_3d);
+        ti = att[i].prop.type;
 
         for (j = i; j < natt; j++)
         {
-            s2j = kT_fac/att[j].mass;
-            tj  = att[j].type;
+            get_atom_sigma2(kT_fac, &att[j].prop, &s2j_2d, &s2j_3d);
+            tj = att[j].prop.type;
+
+            /* Add up the up to four independent variances */
+            s2 = s2i_2d + s2i_3d + s2j_2d + s2j_3d;
 
             /* Note that attractive and repulsive potentials for individual
              * pairs will partially cancel.
              */
             /* -dV/dr at the cut-off for LJ + Coulomb */
-            md =
-                md_ljd*ffp->iparams[ti*ffp->atnr+tj].lj.c6 +
-                md_ljr*ffp->iparams[ti*ffp->atnr+tj].lj.c12 +
-                md_el*att[i].q*att[j].q;
-
-            /* d2V/dr2 at the cut-off for Coulomb, we neglect LJ */
-            dd = dd_el*att[i].q*att[j].q;
-
-            s2  = s2i + s2j;
+            md1 =
+                md1_ljd*ffp->iparams[ti*ffp->atnr+tj].lj.c6 +
+                md1_ljr*ffp->iparams[ti*ffp->atnr+tj].lj.c12 +
+                md1_el*att[i].prop.q*att[j].prop.q;
+
+            /* d2V/dr2 at the cut-off for LJ + Coulomb */
+            d2 =
+                d2_ljd*ffp->iparams[ti*ffp->atnr+tj].lj.c6 +
+                d2_ljr*ffp->iparams[ti*ffp->atnr+tj].lj.c12 +
+                d2_el*att[i].prop.q*att[j].prop.q;
+
+            /* -d3V/dr3 at the cut-off for LJ, we neglect Coulomb */
+            md3 =
+                md3_ljd*ffp->iparams[ti*ffp->atnr+tj].lj.c6 +
+                md3_ljr*ffp->iparams[ti*ffp->atnr+tj].lj.c12;
 
             rsh    = r_buffer;
             sc_fac = 1.0;
             /* For constraints: adapt r and scaling for the Gaussian */
-            if (att[i].con)
+            if (att[i].prop.bConstr)
             {
                 real sh, sc;
-                approx_2dof(s2i, r_buffer*s2i/s2, &sh, &sc);
+
+                approx_2dof(s2i_2d, r_buffer*s2i_2d/s2, &sh, &sc);
                 rsh    += sh;
                 sc_fac *= sc;
             }
-            if (att[j].con)
+            if (att[j].prop.bConstr)
             {
                 real sh, sc;
-                approx_2dof(s2j, r_buffer*s2j/s2, &sh, &sc);
+
+                approx_2dof(s2j_2d, r_buffer*s2j_2d/s2, &sh, &sc);
                 rsh    += sh;
                 sc_fac *= sc;
             }
@@ -376,19 +634,25 @@ static real ener_drift(const verletbuf_atomtype_t *att, int natt,
             c_exp  = exp(-rsh*rsh/(2*s2))/sqrt(2*M_PI);
             c_erfc = 0.5*gmx_erfc(rsh/(sqrt(2*s2)));
             s      = sqrt(s2);
+            rsh2   = rsh*rsh;
 
             pot1 = sc_fac*
-                md/2*((rsh*rsh + s2)*c_erfc - rsh*s*c_exp);
+                md1/2*((rsh2 + s2)*c_erfc - rsh*s*c_exp);
             pot2 = sc_fac*
-                dd/6*(s*(rsh*rsh + 2*s2)*c_exp - rsh*(rsh*rsh + 3*s2)*c_erfc);
-            pot = pot1 + pot2;
+                d2/6*(s*(rsh2 + 2*s2)*c_exp - rsh*(rsh2 + 3*s2)*c_erfc);
+            pot3 =
+                md3/24*((rsh2*rsh2 + 6*rsh2*s2 + 3*s2*s2)*c_erfc - rsh*s*(rsh2 + 5*s2)*c_exp);
+            pot = pot1 + pot2 + pot3;
 
             if (gmx_debug_at)
             {
-                fprintf(debug, "n %d %d d s %.3f %.3f con %d md %8.1e dd %8.1e pot1 %8.1e pot2 %8.1e pot %8.1e\n",
-                        att[i].n, att[j].n, sqrt(s2i), sqrt(s2j),
-                        att[i].con+att[j].con,
-                        md, dd, pot1, pot2, pot);
+                fprintf(debug, "n %d %d d s %.3f %.3f %.3f %.3f con %d -d1 %8.1e d2 %8.1e -d3 %8.1e pot1 %8.1e pot2 %8.1e pot3 %8.1e pot %8.1e\n",
+                        att[i].n, att[j].n,
+                        sqrt(s2i_2d), sqrt(s2i_3d),
+                        sqrt(s2j_2d), sqrt(s2j_3d),
+                        att[i].prop.bConstr+att[j].prop.bConstr,
+                        md1, d2, md3,
+                        pot1, pot2, pot3, pot);
             }
 
             /* Multiply by the number of atom pairs */
@@ -464,8 +728,29 @@ static real surface_frac(int cluster_size, real particle_distance, real rlist)
     return area_rel/cluster_size;
 }
 
+/* Returns the negative of the third derivative of a potential r^-p
+ * with a force-switch function, evaluated at the cut-off rc.
+ */
+static real md3_force_switch(real p, real rswitch, real rc)
+{
+    /* The switched force function is:
+     * p*r^-(p+1) + a*(r - rswitch)^2 + b*(r - rswitch)^3
+     */
+    real a, b;
+    real md3_pot, md3_sw;
+
+    a = -((p + 4)*rc - (p + 1)*rswitch)/(pow(rc, p+2)*pow(rc-rswitch, 2));
+    b =  ((p + 3)*rc - (p + 1)*rswitch)/(pow(rc, p+2)*pow(rc-rswitch, 3));
+
+    md3_pot = (p + 2)*(p + 1)*p*pow(rc, p+3);
+    md3_sw  = 2*a + 6*b*(rc - rswitch);
+
+    return md3_pot + md3_sw;
+}
+
 void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
-                             const t_inputrec *ir, real drift_target,
+                             const t_inputrec *ir,
+                             real reference_temperature,
                              const verletbuf_list_setup_t *list_setup,
                              int *n_nonlin_vsite,
                              real *rlist)
@@ -479,13 +764,38 @@ void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
     verletbuf_atomtype_t *att  = NULL;
     int                   natt = -1, i;
     double                reppow;
-    real                  md_ljd, md_ljr, md_el, dd_el;
+    real                  md1_ljd, d2_ljd, md3_ljd;
+    real                  md1_ljr, d2_ljr, md3_ljr;
+    real                  md1_el,  d2_el;
     real                  elfac;
     real                  kT_fac, mass_min;
     int                   ib0, ib1, ib;
     real                  rb, rl;
     real                  drift;
 
+    if (reference_temperature < 0)
+    {
+        if (EI_MD(ir->eI) && ir->etc == etcNO)
+        {
+            /* This case should be handled outside calc_verlet_buffer_size */
+            gmx_incons("calc_verlet_buffer_size called with an NVE ensemble and reference_temperature < 0");
+        }
+
+        /* We use the maximum temperature with multiple T-coupl groups.
+         * We could use a per particle temperature, but since particles
+         * interact, this might underestimate the buffer size.
+         */
+        reference_temperature = 0;
+        for (i = 0; i < ir->opts.ngtc; i++)
+        {
+            if (ir->opts.tau_t[i] >= 0)
+            {
+                reference_temperature = max(reference_temperature,
+                                            ir->opts.ref_t[i]);
+            }
+        }
+    }
+
     /* Resolution of the buffer size */
     resolution = 0.001;
 
@@ -532,15 +842,59 @@ void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
         fprintf(debug, "energy drift atom types: %d\n", natt);
     }
 
-    reppow = mtop->ffparams.reppow;
-    md_ljd = 0;
-    md_ljr = 0;
+    reppow   = mtop->ffparams.reppow;
+    md1_ljd  = 0;
+    d2_ljd   = 0;
+    md3_ljd  = 0;
+    md1_ljr  = 0;
+    d2_ljr   = 0;
+    md3_ljr  = 0;
     if (ir->vdwtype == evdwCUT)
     {
-        /* -dV/dr of -r^-6 and r^-repporw */
-        md_ljd = -6*pow(ir->rvdw, -7.0);
-        md_ljr = reppow*pow(ir->rvdw, -(reppow+1));
-        /* The contribution of the second derivative is negligible */
+        real sw_range, md3_pswf;
+
+        switch (ir->vdw_modifier)
+        {
+            case eintmodNONE:
+            case eintmodPOTSHIFT:
+                /* -dV/dr of -r^-6 and r^-reppow */
+                md1_ljd =     -6*pow(ir->rvdw, -7.0);
+                md1_ljr = reppow*pow(ir->rvdw, -(reppow+1));
+                /* The contribution of the higher derivatives is negligible */
+                break;
+            case eintmodFORCESWITCH:
+                /* At the cut-off: V=V'=V''=0, so we use only V''' */
+                md3_ljd  = -md3_force_switch(6.0,    ir->rvdw_switch, ir->rvdw);
+                md3_ljr  =  md3_force_switch(reppow, ir->rvdw_switch, ir->rvdw);
+                break;
+            case eintmodPOTSWITCH:
+                /* At the cut-off: V=V'=V''=0.
+                 * V''' is given by the original potential times
+                 * the third derivative of the switch function.
+                 */
+                sw_range  = ir->rvdw - ir->rvdw_switch;
+                md3_pswf  = 60.0*pow(sw_range, -3.0);
+
+                md3_ljd   = -pow(ir->rvdw, -6.0   )*md3_pswf;
+                md3_ljr   =  pow(ir->rvdw, -reppow)*md3_pswf;
+                break;
+            default:
+                gmx_incons("Unimplemented VdW modifier");
+        }
+    }
+    else if (EVDW_PME(ir->vdwtype))
+    {
+        real b, r, br, br2, br4, br6;
+        b        = calc_ewaldcoeff_lj(ir->rvdw, ir->ewald_rtol_lj);
+        r        = ir->rvdw;
+        br       = b*r;
+        br2      = br*br;
+        br4      = br2*br2;
+        br6      = br4*br2;
+        /* -dV/dr of g(br)*r^-6 [where g(x) = exp(-x^2)(1+x^2+x^4/2), see LJ-PME equations in manual] and r^-reppow */
+        md1_ljd  = -exp(-br2)*(br6 + 3.0*br4 + 6.0*br2 + 6.0)*pow(r, -7.0);
+        md1_ljr  = reppow*pow(r, -(reppow+1));
+        /* The contribution of the higher derivatives is negligible */
     }
     else
     {
@@ -550,8 +904,8 @@ void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
     elfac = ONE_4PI_EPS0/ir->epsilon_r;
 
     /* Determine md=-dV/dr and dd=d^2V/dr^2 */
-    md_el = 0;
-    dd_el = 0;
+    md1_el = 0;
+    d2_el  = 0;
     if (ir->coulombtype == eelCUT || EEL_RF(ir->coulombtype))
     {
         real eps_rf, k_rf;
@@ -577,19 +931,19 @@ void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
 
         if (eps_rf > 0)
         {
-            md_el = elfac*(pow(ir->rcoulomb, -2.0) - 2*k_rf*ir->rcoulomb);
+            md1_el = elfac*(pow(ir->rcoulomb, -2.0) - 2*k_rf*ir->rcoulomb);
         }
-        dd_el = elfac*(2*pow(ir->rcoulomb, -3.0) + 2*k_rf);
+        d2_el      = elfac*(2*pow(ir->rcoulomb, -3.0) + 2*k_rf);
     }
     else if (EEL_PME(ir->coulombtype) || ir->coulombtype == eelEWALD)
     {
         real b, rc, br;
 
-        b     = calc_ewaldcoeff(ir->rcoulomb, ir->ewald_rtol);
-        rc    = ir->rcoulomb;
-        br    = b*rc;
-        md_el = elfac*(b*exp(-br*br)*M_2_SQRTPI/rc + gmx_erfc(br)/(rc*rc));
-        dd_el = elfac/(rc*rc)*(2*b*(1 + br*br)*exp(-br*br)*M_2_SQRTPI + 2*gmx_erfc(br)/rc);
+        b      = calc_ewaldcoeff_q(ir->rcoulomb, ir->ewald_rtol);
+        rc     = ir->rcoulomb;
+        br     = b*rc;
+        md1_el = elfac*(b*exp(-br*br)*M_2_SQRTPI/rc + gmx_erfc(br)/(rc*rc));
+        d2_el  = elfac/(rc*rc)*(2*b*(1 + br*br)*exp(-br*br)*M_2_SQRTPI + 2*gmx_erfc(br)/rc);
     }
     else
     {
@@ -608,7 +962,7 @@ void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
          * should be negligible (unless nstlist is extremely large, which
          * you wouldn't do anyhow).
          */
-        kT_fac = 2*BOLTZ*ir->opts.ref_t[0]*(ir->nstlist-1)*ir->delta_t;
+        kT_fac = 2*BOLTZ*reference_temperature*(ir->nstlist-1)*ir->delta_t;
         if (ir->bd_fric > 0)
         {
             /* This is directly sigma^2 of the displacement */
@@ -619,7 +973,7 @@ void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
              */
             for (i = 0; i < natt; i++)
             {
-                att[i].mass = 1;
+                att[i].prop.mass = 1;
             }
         }
         else
@@ -639,19 +993,20 @@ void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
     }
     else
     {
-        kT_fac = BOLTZ*ir->opts.ref_t[0]*sqr((ir->nstlist-1)*ir->delta_t);
+        kT_fac = BOLTZ*reference_temperature*sqr((ir->nstlist-1)*ir->delta_t);
     }
 
-    mass_min = att[0].mass;
+    mass_min = att[0].prop.mass;
     for (i = 1; i < natt; i++)
     {
-        mass_min = min(mass_min, att[i].mass);
+        mass_min = min(mass_min, att[i].prop.mass);
     }
 
     if (debug)
     {
-        fprintf(debug, "md_ljd %e md_ljr %e\n", md_ljd, md_ljr);
-        fprintf(debug, "md_el %e dd_el %e\n", md_el, dd_el);
+        fprintf(debug, "md1_ljd %9.2e d2_ljd %9.2e md3_ljd %9.2e\n", md1_ljd, d2_ljd, md3_ljd);
+        fprintf(debug, "md1_ljr %9.2e d2_ljr %9.2e md3_ljr %9.2e\n", md1_ljr, d2_ljr, md3_ljr);
+        fprintf(debug, "md1_el  %9.2e d2_el  %9.2e\n", md1_el, d2_el);
         fprintf(debug, "sqrt(kT_fac) %f\n", sqrt(kT_fac));
         fprintf(debug, "mass_min %f\n", mass_min);
     }
@@ -671,7 +1026,10 @@ void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
          */
         drift = ener_drift(att, natt, &mtop->ffparams,
                            kT_fac,
-                           md_ljd, md_ljr, md_el, dd_el, rb,
+                           md1_ljd, d2_ljd, md3_ljd,
+                           md1_ljr, d2_ljr, md3_ljr,
+                           md1_el,  d2_el,
+                           rb,
                            rl, boxvol);
 
         /* Correct for the fact that we are using a Ni x Nj particle pair list
@@ -697,7 +1055,7 @@ void calc_verlet_buffer_size(const gmx_mtop_t *mtop, real boxvol,
                     drift);
         }
 
-        if (fabs(drift) > drift_target)
+        if (fabs(drift) > ir->verletbuf_tol)
         {
             ib0 = ib;
         }