Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / calc_verletbuf.c
1 /*  -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  *
4  *                This source code is part of
5  *
6  *                 G   R   O   M   A   C   S
7  *
8  *          GROningen MAchine for Chemical Simulations
9  *
10  *                        VERSION 3.2.03
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  *
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  *
31  * For more info, check our website at http://www.gromacs.org
32  *
33  * And Hey:
34  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <assert.h>
41
42 #include <sys/types.h>
43 #include <math.h>
44 #include "typedefs.h"
45 #include "physics.h"
46 #include "smalloc.h"
47 #include "gmx_fatal.h"
48 #include "macros.h"
49 #include "vec.h"
50 #include "coulomb.h"
51 #include "calc_verletbuf.h"
52 #include "../mdlib/nbnxn_consts.h"
53
54 /* Struct for unique atom type for calculating the energy drift.
55  * The atom displacement depends on mass and constraints.
56  * The energy jump for given distance depend on LJ type and q.
57  */
58 typedef struct
59 {
60     real     mass; /* mass */
61     int      type; /* type (used for LJ parameters) */
62     real     q;    /* charge */
63     int      con;  /* constrained: 0, else 1, if 1, use #DOF=2 iso 3 */
64     int      n;    /* total #atoms of this type in the system */
65 } verletbuf_atomtype_t;
66
67
68 void verletbuf_get_list_setup(gmx_bool bGPU,
69                               verletbuf_list_setup_t *list_setup)
70 {
71     list_setup->cluster_size_i     = NBNXN_CPU_CLUSTER_I_SIZE;
72
73     if (bGPU)
74     {
75         list_setup->cluster_size_j = NBNXN_GPU_CLUSTER_SIZE;
76     }
77     else
78     {
79 #ifndef GMX_NBNXN_SIMD
80         list_setup->cluster_size_j = NBNXN_CPU_CLUSTER_I_SIZE;
81 #else
82         list_setup->cluster_size_j = GMX_NBNXN_SIMD_BITWIDTH/(sizeof(real)*8);
83 #ifdef GMX_NBNXN_SIMD_2XNN
84         /* We assume the smallest cluster size to be on the safe side */
85         list_setup->cluster_size_j /= 2;
86 #endif
87 #endif
88     }
89 }
90
91 static void add_at(verletbuf_atomtype_t **att_p,int *natt_p,
92                    real mass,int type,real q,int con,int nmol)
93 {
94     verletbuf_atomtype_t *att;
95     int natt,i;
96
97     if (mass == 0)
98     {
99         /* Ignore massless particles */
100         return;
101     }
102
103     att  = *att_p;
104     natt = *natt_p;
105
106     i = 0;
107     while (i < natt &&
108            !(mass == att[i].mass &&
109              type == att[i].type &&
110              q    == att[i].q &&
111              con  == att[i].con))
112     {
113         i++;
114     }
115
116     if (i < natt)
117     {
118         att[i].n += nmol;
119     }
120     else
121     {
122         (*natt_p)++;
123         srenew(*att_p,*natt_p);
124         (*att_p)[i].mass = mass;
125         (*att_p)[i].type = type;
126         (*att_p)[i].q    = q;
127         (*att_p)[i].con  = con;
128         (*att_p)[i].n    = nmol;
129     }
130 }
131
132 static void get_verlet_buffer_atomtypes(const gmx_mtop_t *mtop,
133                                         verletbuf_atomtype_t **att_p,
134                                         int *natt_p,
135                                         int *n_nonlin_vsite)
136 {
137     verletbuf_atomtype_t *att;
138     int natt;
139     int mb,nmol,ft,i,j,a1,a2,a3,a;
140     const t_atoms *atoms;
141     const t_ilist *il;
142     const t_atom *at;
143     const t_iparams *ip;
144     real *con_m,*vsite_m,cam[5];
145
146     att  = NULL;
147     natt = 0;
148
149     if (n_nonlin_vsite != NULL)
150     {
151         *n_nonlin_vsite = 0;
152     }
153
154     for(mb=0; mb<mtop->nmolblock; mb++)
155     {
156         nmol = mtop->molblock[mb].nmol;
157
158         atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
159
160         /* Check for constraints, as they affect the kinetic energy */
161         snew(con_m,atoms->nr);
162         snew(vsite_m,atoms->nr);
163
164         for(ft=F_CONSTR; ft<=F_CONSTRNC; ft++)
165         {
166             il = &mtop->moltype[mtop->molblock[mb].type].ilist[ft];
167
168             for(i=0; i<il->nr; i+=1+NRAL(ft))
169             {
170                 a1 = il->iatoms[i+1];
171                 a2 = il->iatoms[i+2];
172                 con_m[a1] += atoms->atom[a2].m;
173                 con_m[a2] += atoms->atom[a1].m;
174             }
175         }
176
177         il = &mtop->moltype[mtop->molblock[mb].type].ilist[F_SETTLE];
178
179         for(i=0; i<il->nr; i+=1+NRAL(F_SETTLE))
180         {
181             a1 = il->iatoms[i+1];
182             a2 = il->iatoms[i+2];
183             a3 = il->iatoms[i+3];
184             con_m[a1] += atoms->atom[a2].m + atoms->atom[a3].m;
185             con_m[a2] += atoms->atom[a1].m + atoms->atom[a3].m;
186             con_m[a3] += atoms->atom[a1].m + atoms->atom[a2].m;
187         }
188
189         /* Check for virtual sites, determine mass from constructing atoms */
190         for(ft=0; ft<F_NRE; ft++)
191         {
192             if (IS_VSITE(ft))
193             {
194                 il = &mtop->moltype[mtop->molblock[mb].type].ilist[ft];
195
196                 for(i=0; i<il->nr; i+=1+NRAL(ft))
197                 {
198                     ip = &mtop->ffparams.iparams[il->iatoms[i]];
199
200                     a1 = il->iatoms[i+1];
201
202                     for(j=1; j<NRAL(ft); j++)
203                     {
204                         cam[j] = atoms->atom[il->iatoms[i+1+j]].m;
205                         if (cam[j] == 0)
206                         {
207                             cam[j] = vsite_m[il->iatoms[i+1+j]];
208                         }
209                         if (cam[j] == 0)
210                         {
211                             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.",
212                                       *mtop->moltype[mtop->molblock[mb].type].name,
213                                       interaction_function[ft].longname,
214                                       il->iatoms[i+1+j]+1);
215                         }
216                     }
217
218                     switch(ft)
219                     {
220                     case F_VSITE2:
221                         /* Exact except for ignoring constraints */
222                         vsite_m[a1] = (cam[2]*sqr(1-ip->vsite.a) + cam[1]*sqr(ip->vsite.a))/(cam[1]*cam[2]);
223                         break;
224                     case F_VSITE3:
225                         /* Exact except for ignoring constraints */
226                         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]);
227                         break;
228                     default:
229                         /* Use the mass of the lightest constructing atom.
230                          * This is an approximation.
231                          * If the distance of the virtual site to the
232                          * constructing atom is less than all distances
233                          * between constructing atoms, this is a safe
234                          * over-estimate of the displacement of the vsite.
235                          * This condition holds for all H mass replacement
236                          * replacement vsite constructions, except for SP2/3
237                          * groups. In SP3 groups one H will have a F_VSITE3
238                          * construction, so even there the total drift
239                          * estimation shouldn't be far off.
240                          */
241                         assert(j>=1);
242                         vsite_m[a1] = cam[1];
243                         for(j=2; j<NRAL(ft); j++)
244                         {
245                             vsite_m[a1] = min(vsite_m[a1],cam[j]);
246                         }
247                         if (n_nonlin_vsite != NULL)
248                         {
249                             *n_nonlin_vsite += nmol;
250                         }
251                         break;
252                     }
253                 }
254             }
255         }
256
257         for(a=0; a<atoms->nr; a++)
258         {
259             at = &atoms->atom[a];
260             /* We consider an atom constrained, #DOF=2, when it is
261              * connected with constraints to one or more atoms with
262              * total mass larger than 1.5 that of the atom itself.
263              */
264             add_at(&att,&natt,
265                    at->m,at->type,at->q,con_m[a] > 1.5*at->m,nmol);
266         }
267
268         sfree(vsite_m);
269         sfree(con_m);
270     }
271
272     if (gmx_debug_at)
273     {
274         for(a=0; a<natt; a++)
275         {
276             fprintf(debug,"type %d: m %5.2f t %d q %6.3f con %d n %d\n",
277                     a,att[a].mass,att[a].type,att[a].q,att[a].con,att[a].n);
278         }
279     }
280
281     *att_p  = att;
282     *natt_p = natt;
283 }
284
285 static void approx_2dof(real s2,real x,
286                         real *shift,real *scale)
287 {
288     /* A particle with 1 DOF constrained has 2 DOFs instead of 3.
289      * This code is also used for particles with multiple constraints,
290      * in which case we overestimate the displacement.
291      * The 2DOF distribution is sqrt(pi/2)*erfc(r/(sqrt(2)*s))/(2*s).
292      * We approximate this with scale*Gaussian(s,r+shift),
293      * by matching the distribution value and derivative at x.
294      * This is a tight overestimate for all r>=0 at any s and x.
295      */
296     real ex,er;
297
298     ex = exp(-x*x/(2*s2));
299     er = gmx_erfc(x/sqrt(2*s2));
300
301     *shift = -x + sqrt(2*s2/M_PI)*ex/er;
302     *scale = 0.5*M_PI*exp(ex*ex/(M_PI*er*er))*er;
303 }
304
305 static real ener_drift(const verletbuf_atomtype_t *att,int natt,
306                        const gmx_ffparams_t *ffp,
307                        real kT_fac,
308                        real md_ljd,real md_ljr,real md_el,real dd_el,
309                        real r_buffer,
310                        real rlist,real boxvol)
311 {
312     double drift_tot,pot1,pot2,pot;
313     int    i,j;
314     real   s2i,s2j,s2,s;
315     int    ti,tj;
316     real   md,dd;
317     real   sc_fac,rsh;
318     double c_exp,c_erfc;
319
320     drift_tot = 0;
321
322     /* Loop over the different atom type pairs */
323     for(i=0; i<natt; i++)
324     {
325         s2i = kT_fac/att[i].mass;
326         ti  = att[i].type;
327
328         for(j=i; j<natt; j++)
329         {
330             s2j = kT_fac/att[j].mass;
331             tj = att[j].type;
332
333             /* Note that attractive and repulsive potentials for individual
334              * pairs will partially cancel.
335              */
336             /* -dV/dr at the cut-off for LJ + Coulomb */
337             md =
338                 md_ljd*ffp->iparams[ti*ffp->atnr+tj].lj.c6 +
339                 md_ljr*ffp->iparams[ti*ffp->atnr+tj].lj.c12 +
340                 md_el*att[i].q*att[j].q;
341
342             /* d2V/dr2 at the cut-off for Coulomb, we neglect LJ */
343             dd = dd_el*att[i].q*att[j].q;
344
345             s2  = s2i + s2j;
346
347             rsh    = r_buffer;
348             sc_fac = 1.0;
349             /* For constraints: adapt r and scaling for the Gaussian */
350             if (att[i].con)
351             {
352                 real sh,sc;
353                 approx_2dof(s2i,r_buffer*s2i/s2,&sh,&sc);
354                 rsh    += sh;
355                 sc_fac *= sc;
356             }
357             if (att[j].con)
358             {
359                 real sh,sc;
360                 approx_2dof(s2j,r_buffer*s2j/s2,&sh,&sc);
361                 rsh    += sh;
362                 sc_fac *= sc;
363             }
364
365             /* Exact contribution of an atom pair with Gaussian displacement
366              * with sigma s to the energy drift for a potential with
367              * derivative -md and second derivative dd at the cut-off.
368              * The only catch is that for potentials that change sign
369              * near the cut-off there could be an unlucky compensation
370              * of positive and negative energy drift.
371              * Such potentials are extremely rare though.
372              *
373              * Note that pot has unit energy*length, as the linear
374              * atom density still needs to be put in.
375              */
376             c_exp  = exp(-rsh*rsh/(2*s2))/sqrt(2*M_PI);
377             c_erfc = 0.5*gmx_erfc(rsh/(sqrt(2*s2)));
378             s      = sqrt(s2);
379
380             pot1 = sc_fac*
381                 md/2*((rsh*rsh + s2)*c_erfc - rsh*s*c_exp);
382             pot2 = sc_fac*
383                 dd/6*(s*(rsh*rsh + 2*s2)*c_exp - rsh*(rsh*rsh + 3*s2)*c_erfc);
384             pot = pot1 + pot2;
385
386             if (gmx_debug_at)
387             {
388                 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",
389                         att[i].n,att[j].n,sqrt(s2i),sqrt(s2j),
390                         att[i].con+att[j].con,
391                         md,dd,pot1,pot2,pot);
392             }
393
394             /* Multiply by the number of atom pairs */
395             if (j == i)
396             {
397                 pot *= (double)att[i].n*(att[i].n - 1)/2;
398             }
399             else
400             {
401                 pot *= (double)att[i].n*att[j].n;
402             }
403             /* We need the line density to get the energy drift of the system.
404              * The effective average r^2 is close to (rlist+sigma)^2.
405              */
406             pot *= 4*M_PI*sqr(rlist + s)/boxvol;
407
408             /* Add the unsigned drift to avoid cancellation of errors */
409             drift_tot += fabs(pot);
410         }
411     }
412
413     return drift_tot;
414 }
415
416 static real surface_frac(int cluster_size,real particle_distance,real rlist)
417 {
418     real d,area_rel;
419
420     if (rlist < 0.5*particle_distance)
421     {
422         /* We have non overlapping spheres */
423         return 1.0;
424     }
425
426     /* Half the inter-particle distance relative to rlist */
427     d = 0.5*particle_distance/rlist;
428
429     /* Determine the area of the surface at distance rlist to the closest
430      * particle, relative to surface of a sphere of radius rlist.
431      * The formulas below assume close to cubic cells for the pair search grid,
432      * which the pair search code tries to achieve.
433      * Note that in practice particle distances will not be delta distributed,
434      * but have some spread, often involving shorter distances,
435      * as e.g. O-H bonds in a water molecule. Thus the estimates below will
436      * usually be slightly too high and thus conservative.
437      */
438     switch (cluster_size)
439     {
440     case 1:
441         /* One particle: trivial */
442         area_rel = 1.0;
443         break;
444     case 2:
445         /* Two particles: two spheres at fractional distance 2*a */
446         area_rel = 1.0 + d;
447         break;
448     case 4:
449         /* We assume a perfect, symmetric tetrahedron geometry.
450          * The surface around a tetrahedron is too complex for a full
451          * analytical solution, so we use a Taylor expansion.
452          */
453         area_rel = (1.0 + 1/M_PI*(6*acos(1/sqrt(3))*d +
454                                   sqrt(3)*d*d*(1.0 +
455                                                5.0/18.0*d*d +
456                                                7.0/45.0*d*d*d*d +
457                                                83.0/756.0*d*d*d*d*d*d)));
458         break;
459     default:
460         gmx_incons("surface_frac called with unsupported cluster_size");
461         area_rel = 1.0;
462     }
463         
464     return area_rel/cluster_size;
465 }
466
467 void calc_verlet_buffer_size(const gmx_mtop_t *mtop,real boxvol,
468                              const t_inputrec *ir,real drift_target,
469                              const verletbuf_list_setup_t *list_setup,
470                              int *n_nonlin_vsite,
471                              real *rlist)
472 {
473     double resolution;
474     char *env;
475
476     real particle_distance;
477     real nb_clust_frac_pairs_not_in_list_at_cutoff;
478
479     verletbuf_atomtype_t *att=NULL;
480     int  natt=-1,i;
481     double reppow;
482     real md_ljd,md_ljr,md_el,dd_el;
483     real elfac;
484     real kT_fac,mass_min;
485     int  ib0,ib1,ib;
486     real rb,rl;
487     real drift;
488
489     /* Resolution of the buffer size */
490     resolution = 0.001;
491
492     env = getenv("GMX_VERLET_BUFFER_RES");
493     if (env != NULL)
494     {
495         sscanf(env,"%lf",&resolution);
496     }
497
498     /* In an atom wise pair-list there would be no pairs in the list
499      * beyond the pair-list cut-off.
500      * However, we use a pair-list of groups vs groups of atoms.
501      * For groups of 4 atoms, the parallelism of SSE instructions, only
502      * 10% of the atoms pairs are not in the list just beyond the cut-off.
503      * As this percentage increases slowly compared to the decrease of the
504      * Gaussian displacement distribution over this range, we can simply
505      * reduce the drift by this fraction.
506      * For larger groups, e.g. of 8 atoms, this fraction will be lower,
507      * so then buffer size will be on the conservative (large) side.
508      *
509      * Note that the formulas used here do not take into account
510      * cancellation of errors which could occur by missing both
511      * attractive and repulsive interactions.
512      *
513      * The only major assumption is homogeneous particle distribution.
514      * For an inhomogeneous system, such as a liquid-vapor system,
515      * the buffer will be underestimated. The actual energy drift
516      * will be higher by the factor: local/homogeneous particle density.
517      *
518      * The results of this estimate have been checked againt simulations.
519      * In most cases the real drift differs by less than a factor 2.
520      */
521
522     /* Worst case assumption: HCP packing of particles gives largest distance */
523     particle_distance = pow(boxvol*sqrt(2)/mtop->natoms,1.0/3.0);
524
525     get_verlet_buffer_atomtypes(mtop,&att,&natt,n_nonlin_vsite);
526     assert(att != NULL && natt >= 0);
527
528     if (debug)
529     {
530         fprintf(debug,"particle distance assuming HCP packing: %f nm\n",
531                 particle_distance);
532         fprintf(debug,"energy drift atom types: %d\n",natt);
533     }
534
535     reppow = mtop->ffparams.reppow;
536     md_ljd = 0;
537     md_ljr = 0;
538     if (ir->vdwtype == evdwCUT)
539     {
540         /* -dV/dr of -r^-6 and r^-repporw */
541         md_ljd = -6*pow(ir->rvdw,-7.0);
542         md_ljr = reppow*pow(ir->rvdw,-(reppow+1));
543         /* The contribution of the second derivative is negligible */
544     }
545     else
546     {
547         gmx_fatal(FARGS,"Energy drift calculation is only implemented for plain cut-off Lennard-Jones interactions");
548     }
549
550     elfac = ONE_4PI_EPS0/ir->epsilon_r;
551
552     /* Determine md=-dV/dr and dd=d^2V/dr^2 */
553     md_el = 0;
554     dd_el = 0;
555     if (ir->coulombtype == eelCUT || EEL_RF(ir->coulombtype))
556     {
557         real eps_rf,k_rf;
558
559         if (ir->coulombtype == eelCUT)
560         {
561             eps_rf = 1;
562             k_rf = 0;
563         }
564         else
565         {
566             eps_rf = ir->epsilon_rf/ir->epsilon_r;
567             if (eps_rf != 0)
568             {
569                 k_rf = pow(ir->rcoulomb,-3.0)*(eps_rf - ir->epsilon_r)/(2*eps_rf + ir->epsilon_r);
570             }
571             else
572             {
573                 /* epsilon_rf = infinity */
574                 k_rf = 0.5*pow(ir->rcoulomb,-3.0);
575             }
576         }
577
578         if (eps_rf > 0)
579         {
580             md_el = elfac*(pow(ir->rcoulomb,-2.0) - 2*k_rf*ir->rcoulomb);
581         }
582         dd_el = elfac*(2*pow(ir->rcoulomb,-3.0) + 2*k_rf);
583     }
584     else if (EEL_PME(ir->coulombtype) || ir->coulombtype == eelEWALD)
585     {
586         real b,rc,br;
587
588         b  = calc_ewaldcoeff(ir->rcoulomb,ir->ewald_rtol);
589         rc = ir->rcoulomb;
590         br = b*rc;
591         md_el = elfac*(b*exp(-br*br)*M_2_SQRTPI/rc + gmx_erfc(br)/(rc*rc));
592         dd_el = elfac/(rc*rc)*(2*b*(1 + br*br)*exp(-br*br)*M_2_SQRTPI + 2*gmx_erfc(br)/rc);
593     }
594     else
595     {
596         gmx_fatal(FARGS,"Energy drift calculation is only implemented for Reaction-Field and Ewald electrostatics");
597     }
598
599     /* Determine the variance of the atomic displacement
600      * over nstlist-1 steps: kT_fac
601      * For inertial dynamics (not Brownian dynamics) the mass factor
602      * is not included in kT_fac, it is added later.
603      */
604     if (ir->eI == eiBD)
605     {
606         /* Get the displacement distribution from the random component only.
607          * With accurate integration the systematic (force) displacement
608          * should be negligible (unless nstlist is extremely large, which
609          * you wouldn't do anyhow).
610          */
611         kT_fac = 2*BOLTZ*ir->opts.ref_t[0]*(ir->nstlist-1)*ir->delta_t;
612         if (ir->bd_fric > 0)
613         {
614             /* This is directly sigma^2 of the displacement */
615             kT_fac /= ir->bd_fric;
616
617             /* Set the masses to 1 as kT_fac is the full sigma^2,
618              * but we divide by m in ener_drift().
619              */
620             for(i=0; i<natt; i++)
621             {
622                 att[i].mass = 1;
623             }
624         }
625         else
626         {
627             real tau_t;
628
629             /* Per group tau_t is not implemented yet, use the maximum */
630             tau_t = ir->opts.tau_t[0];
631             for(i=1; i<ir->opts.ngtc; i++)
632             {
633                 tau_t = max(tau_t,ir->opts.tau_t[i]);
634             }
635
636             kT_fac *= tau_t;
637             /* This kT_fac needs to be divided by the mass to get sigma^2 */
638         }
639     }
640     else
641     {
642         kT_fac = BOLTZ*ir->opts.ref_t[0]*sqr((ir->nstlist-1)*ir->delta_t);
643     }
644
645     mass_min = att[0].mass;
646     for(i=1; i<natt; i++)
647     {
648         mass_min = min(mass_min,att[i].mass);
649     }
650
651     if (debug)
652     {
653         fprintf(debug,"md_ljd %e md_ljr %e\n",md_ljd,md_ljr);
654         fprintf(debug,"md_el %e dd_el %e\n",md_el,dd_el);
655         fprintf(debug,"sqrt(kT_fac) %f\n",sqrt(kT_fac));
656         fprintf(debug,"mass_min %f\n",mass_min);
657     }
658
659     /* Search using bisection */
660     ib0 = -1;
661     /* The drift will be neglible at 5 times the max sigma */
662     ib1 = (int)(5*2*sqrt(kT_fac/mass_min)/resolution) + 1;
663     while (ib1 - ib0 > 1)
664     {
665         ib = (ib0 + ib1)/2;
666         rb = ib*resolution;
667         rl = max(ir->rvdw,ir->rcoulomb) + rb;
668
669         /* Calculate the average energy drift at the last step
670          * of the nstlist steps at which the pair-list is used.
671          */
672         drift = ener_drift(att,natt,&mtop->ffparams,
673                            kT_fac,
674                            md_ljd,md_ljr,md_el,dd_el,rb,
675                            rl,boxvol);
676
677         /* Correct for the fact that we are using a Ni x Nj particle pair list
678          * and not a 1 x 1 particle pair list. This reduces the drift.
679          */
680         /* We don't have a formula for 8 (yet), use 4 which is conservative */
681         nb_clust_frac_pairs_not_in_list_at_cutoff =
682             surface_frac(min(list_setup->cluster_size_i,4),
683                          particle_distance,rl)*
684             surface_frac(min(list_setup->cluster_size_j,4),
685                          particle_distance,rl);
686         drift *= nb_clust_frac_pairs_not_in_list_at_cutoff;
687
688         /* Convert the drift to drift per unit time per atom */
689         drift /= ir->nstlist*ir->delta_t*mtop->natoms;
690
691         if (debug)
692         {
693             fprintf(debug,"ib %3d %3d %3d rb %.3f %dx%d fac %.3f drift %f\n",
694                     ib0,ib,ib1,rb,
695                     list_setup->cluster_size_i,list_setup->cluster_size_j,
696                     nb_clust_frac_pairs_not_in_list_at_cutoff,
697                     drift);
698         }
699
700         if (fabs(drift) > drift_target)
701         {
702             ib0 = ib;
703         }
704         else
705         {
706             ib1 = ib;
707         }
708     }
709
710     sfree(att);
711
712     *rlist = max(ir->rvdw,ir->rcoulomb) + ib1*resolution;
713 }