1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
31 * For more info, check our website at http://www.gromacs.org
34 * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
42 #include <sys/types.h>
47 #include "gmx_fatal.h"
51 #include "calc_verletbuf.h"
52 #include "../mdlib/nbnxn_consts.h"
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.
61 int type; /* type (used for LJ parameters) */
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;
68 void verletbuf_get_list_setup(gmx_bool bGPU,
69 verletbuf_list_setup_t *list_setup)
71 list_setup->cluster_size_i = NBNXN_CPU_CLUSTER_I_SIZE;
75 list_setup->cluster_size_j = NBNXN_GPU_CLUSTER_SIZE;
79 #ifndef GMX_NBNXN_SIMD
80 list_setup->cluster_size_j = NBNXN_CPU_CLUSTER_I_SIZE;
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;
91 static void add_at(verletbuf_atomtype_t **att_p, int *natt_p,
92 real mass, int type, real q, int con, int nmol)
94 verletbuf_atomtype_t *att;
99 /* Ignore massless particles */
108 !(mass == att[i].mass &&
109 type == att[i].type &&
123 srenew(*att_p, *natt_p);
124 (*att_p)[i].mass = mass;
125 (*att_p)[i].type = type;
127 (*att_p)[i].con = con;
128 (*att_p)[i].n = nmol;
132 static void get_verlet_buffer_atomtypes(const gmx_mtop_t *mtop,
133 verletbuf_atomtype_t **att_p,
137 verletbuf_atomtype_t *att;
139 int mb, nmol, ft, i, j, a1, a2, a3, a;
140 const t_atoms *atoms;
144 real *con_m, *vsite_m, cam[5];
149 if (n_nonlin_vsite != NULL)
154 for (mb = 0; mb < mtop->nmolblock; mb++)
156 nmol = mtop->molblock[mb].nmol;
158 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
160 /* Check for constraints, as they affect the kinetic energy */
161 snew(con_m, atoms->nr);
162 snew(vsite_m, atoms->nr);
164 for (ft = F_CONSTR; ft <= F_CONSTRNC; ft++)
166 il = &mtop->moltype[mtop->molblock[mb].type].ilist[ft];
168 for (i = 0; i < il->nr; i += 1+NRAL(ft))
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;
177 il = &mtop->moltype[mtop->molblock[mb].type].ilist[F_SETTLE];
179 for (i = 0; i < il->nr; i += 1+NRAL(F_SETTLE))
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;
189 /* Check for virtual sites, determine mass from constructing atoms */
190 for (ft = 0; ft < F_NRE; ft++)
194 il = &mtop->moltype[mtop->molblock[mb].type].ilist[ft];
196 for (i = 0; i < il->nr; i += 1+NRAL(ft))
198 ip = &mtop->ffparams.iparams[il->iatoms[i]];
200 a1 = il->iatoms[i+1];
202 for (j = 1; j < NRAL(ft); j++)
204 cam[j] = atoms->atom[il->iatoms[i+1+j]].m;
207 cam[j] = vsite_m[il->iatoms[i+1+j]];
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);
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]);
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]);
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.
242 vsite_m[a1] = cam[1];
243 for (j = 2; j < NRAL(ft); j++)
245 vsite_m[a1] = min(vsite_m[a1], cam[j]);
247 if (n_nonlin_vsite != NULL)
249 *n_nonlin_vsite += nmol;
257 for (a = 0; a < atoms->nr; a++)
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.
265 at->m, at->type, at->q, con_m[a] > 1.5*at->m, nmol);
274 for (a = 0; a < natt; a++)
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);
285 static void approx_2dof(real s2, real x,
286 real *shift, real *scale)
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.
298 ex = exp(-x*x/(2*s2));
299 er = gmx_erfc(x/sqrt(2*s2));
301 *shift = -x + sqrt(2*s2/M_PI)*ex/er;
302 *scale = 0.5*M_PI*exp(ex*ex/(M_PI*er*er))*er;
305 static real ener_drift(const verletbuf_atomtype_t *att, int natt,
306 const gmx_ffparams_t *ffp,
308 real md_ljd, real md_ljr, real md_el, real dd_el,
310 real rlist, real boxvol)
312 double drift_tot, pot1, pot2, pot;
314 real s2i, s2j, s2, s;
318 double c_exp, c_erfc;
322 /* Loop over the different atom type pairs */
323 for (i = 0; i < natt; i++)
325 s2i = kT_fac/att[i].mass;
328 for (j = i; j < natt; j++)
330 s2j = kT_fac/att[j].mass;
333 /* Note that attractive and repulsive potentials for individual
334 * pairs will partially cancel.
336 /* -dV/dr at the cut-off for LJ + Coulomb */
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;
342 /* d2V/dr2 at the cut-off for Coulomb, we neglect LJ */
343 dd = dd_el*att[i].q*att[j].q;
349 /* For constraints: adapt r and scaling for the Gaussian */
353 approx_2dof(s2i, r_buffer*s2i/s2, &sh, &sc);
360 approx_2dof(s2j, r_buffer*s2j/s2, &sh, &sc);
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.
373 * Note that pot has unit energy*length, as the linear
374 * atom density still needs to be put in.
376 c_exp = exp(-rsh*rsh/(2*s2))/sqrt(2*M_PI);
377 c_erfc = 0.5*gmx_erfc(rsh/(sqrt(2*s2)));
381 md/2*((rsh*rsh + s2)*c_erfc - rsh*s*c_exp);
383 dd/6*(s*(rsh*rsh + 2*s2)*c_exp - rsh*(rsh*rsh + 3*s2)*c_erfc);
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);
394 /* Multiply by the number of atom pairs */
397 pot *= (double)att[i].n*(att[i].n - 1)/2;
401 pot *= (double)att[i].n*att[j].n;
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.
406 pot *= 4*M_PI*sqr(rlist + s)/boxvol;
408 /* Add the unsigned drift to avoid cancellation of errors */
409 drift_tot += fabs(pot);
416 static real surface_frac(int cluster_size, real particle_distance, real rlist)
420 if (rlist < 0.5*particle_distance)
422 /* We have non overlapping spheres */
426 /* Half the inter-particle distance relative to rlist */
427 d = 0.5*particle_distance/rlist;
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.
438 switch (cluster_size)
441 /* One particle: trivial */
445 /* Two particles: two spheres at fractional distance 2*a */
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.
453 area_rel = (1.0 + 1/M_PI*(6*acos(1/sqrt(3))*d +
457 83.0/756.0*d*d*d*d*d*d)));
460 gmx_incons("surface_frac called with unsupported cluster_size");
464 return area_rel/cluster_size;
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,
476 real particle_distance;
477 real nb_clust_frac_pairs_not_in_list_at_cutoff;
479 verletbuf_atomtype_t *att = NULL;
482 real md_ljd, md_ljr, md_el, dd_el;
484 real kT_fac, mass_min;
489 /* Resolution of the buffer size */
492 env = getenv("GMX_VERLET_BUFFER_RES");
495 sscanf(env, "%lf", &resolution);
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.
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.
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.
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.
522 /* Worst case assumption: HCP packing of particles gives largest distance */
523 particle_distance = pow(boxvol*sqrt(2)/mtop->natoms, 1.0/3.0);
525 get_verlet_buffer_atomtypes(mtop, &att, &natt, n_nonlin_vsite);
526 assert(att != NULL && natt >= 0);
530 fprintf(debug, "particle distance assuming HCP packing: %f nm\n",
532 fprintf(debug, "energy drift atom types: %d\n", natt);
535 reppow = mtop->ffparams.reppow;
538 if (ir->vdwtype == evdwCUT)
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 */
547 gmx_fatal(FARGS, "Energy drift calculation is only implemented for plain cut-off Lennard-Jones interactions");
550 elfac = ONE_4PI_EPS0/ir->epsilon_r;
552 /* Determine md=-dV/dr and dd=d^2V/dr^2 */
555 if (ir->coulombtype == eelCUT || EEL_RF(ir->coulombtype))
559 if (ir->coulombtype == eelCUT)
566 eps_rf = ir->epsilon_rf/ir->epsilon_r;
569 k_rf = pow(ir->rcoulomb, -3.0)*(eps_rf - ir->epsilon_r)/(2*eps_rf + ir->epsilon_r);
573 /* epsilon_rf = infinity */
574 k_rf = 0.5*pow(ir->rcoulomb, -3.0);
580 md_el = elfac*(pow(ir->rcoulomb, -2.0) - 2*k_rf*ir->rcoulomb);
582 dd_el = elfac*(2*pow(ir->rcoulomb, -3.0) + 2*k_rf);
584 else if (EEL_PME(ir->coulombtype) || ir->coulombtype == eelEWALD)
588 b = calc_ewaldcoeff(ir->rcoulomb, ir->ewald_rtol);
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);
596 gmx_fatal(FARGS, "Energy drift calculation is only implemented for Reaction-Field and Ewald electrostatics");
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.
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).
611 kT_fac = 2*BOLTZ*ir->opts.ref_t[0]*(ir->nstlist-1)*ir->delta_t;
614 /* This is directly sigma^2 of the displacement */
615 kT_fac /= ir->bd_fric;
617 /* Set the masses to 1 as kT_fac is the full sigma^2,
618 * but we divide by m in ener_drift().
620 for (i = 0; i < natt; i++)
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++)
633 tau_t = max(tau_t, ir->opts.tau_t[i]);
637 /* This kT_fac needs to be divided by the mass to get sigma^2 */
642 kT_fac = BOLTZ*ir->opts.ref_t[0]*sqr((ir->nstlist-1)*ir->delta_t);
645 mass_min = att[0].mass;
646 for (i = 1; i < natt; i++)
648 mass_min = min(mass_min, att[i].mass);
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);
659 /* Search using bisection */
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)
667 rl = max(ir->rvdw, ir->rcoulomb) + rb;
669 /* Calculate the average energy drift at the last step
670 * of the nstlist steps at which the pair-list is used.
672 drift = ener_drift(att, natt, &mtop->ffparams,
674 md_ljd, md_ljr, md_el, dd_el, rb,
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.
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;
688 /* Convert the drift to drift per unit time per atom */
689 drift /= ir->nstlist*ir->delta_t*mtop->natoms;
693 fprintf(debug, "ib %3d %3d %3d rb %.3f %dx%d fac %.3f drift %f\n",
695 list_setup->cluster_size_i, list_setup->cluster_size_j,
696 nb_clust_frac_pairs_not_in_list_at_cutoff,
700 if (fabs(drift) > drift_target)
712 *rlist = max(ir->rvdw, ir->rcoulomb) + ib1*resolution;