2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
51 #include "gromacs/commandline/filenm.h"
52 #include "gromacs/domdec/domdec.h"
53 #include "gromacs/domdec/domdec_struct.h"
54 #include "gromacs/ewald/ewald.h"
55 #include "gromacs/ewald/ewald_utils.h"
56 #include "gromacs/fileio/filetypes.h"
57 #include "gromacs/gmxlib/network.h"
58 #include "gromacs/gmxlib/nonbonded/nonbonded.h"
59 #include "gromacs/gpu_utils/gpu_utils.h"
60 #include "gromacs/hardware/hw_info.h"
61 #include "gromacs/listed_forces/gpubonded.h"
62 #include "gromacs/listed_forces/manage_threading.h"
63 #include "gromacs/listed_forces/pairs.h"
64 #include "gromacs/math/functions.h"
65 #include "gromacs/math/units.h"
66 #include "gromacs/math/utilities.h"
67 #include "gromacs/math/vec.h"
68 #include "gromacs/mdlib/force.h"
69 #include "gromacs/mdlib/forcerec_threading.h"
70 #include "gromacs/mdlib/gmx_omp_nthreads.h"
71 #include "gromacs/mdlib/md_support.h"
72 #include "gromacs/mdlib/ns.h"
73 #include "gromacs/mdlib/qmmm.h"
74 #include "gromacs/mdlib/rf_util.h"
75 #include "gromacs/mdlib/sim_util.h"
76 #include "gromacs/mdlib/wall.h"
77 #include "gromacs/mdtypes/commrec.h"
78 #include "gromacs/mdtypes/fcdata.h"
79 #include "gromacs/mdtypes/group.h"
80 #include "gromacs/mdtypes/iforceprovider.h"
81 #include "gromacs/mdtypes/inputrec.h"
82 #include "gromacs/mdtypes/md_enums.h"
83 #include "gromacs/nbnxm/gpu_data_mgmt.h"
84 #include "gromacs/nbnxm/nbnxm.h"
85 #include "gromacs/nbnxm/nbnxm_geometry.h"
86 #include "gromacs/pbcutil/ishift.h"
87 #include "gromacs/pbcutil/pbc.h"
88 #include "gromacs/tables/forcetable.h"
89 #include "gromacs/topology/mtop_util.h"
90 #include "gromacs/trajectory/trajectoryframe.h"
91 #include "gromacs/utility/cstringutil.h"
92 #include "gromacs/utility/exceptions.h"
93 #include "gromacs/utility/fatalerror.h"
94 #include "gromacs/utility/gmxassert.h"
95 #include "gromacs/utility/logger.h"
96 #include "gromacs/utility/physicalnodecommunicator.h"
97 #include "gromacs/utility/pleasecite.h"
98 #include "gromacs/utility/smalloc.h"
99 #include "gromacs/utility/strconvert.h"
101 t_forcerec *mk_forcerec()
110 static real *mk_nbfp(const gmx_ffparams_t *idef, gmx_bool bBHAM)
118 snew(nbfp, 3*atnr*atnr);
119 for (i = k = 0; (i < atnr); i++)
121 for (j = 0; (j < atnr); j++, k++)
123 BHAMA(nbfp, atnr, i, j) = idef->iparams[k].bham.a;
124 BHAMB(nbfp, atnr, i, j) = idef->iparams[k].bham.b;
125 /* nbfp now includes the 6.0 derivative prefactor */
126 BHAMC(nbfp, atnr, i, j) = idef->iparams[k].bham.c*6.0;
132 snew(nbfp, 2*atnr*atnr);
133 for (i = k = 0; (i < atnr); i++)
135 for (j = 0; (j < atnr); j++, k++)
137 /* nbfp now includes the 6.0/12.0 derivative prefactors */
138 C6(nbfp, atnr, i, j) = idef->iparams[k].lj.c6*6.0;
139 C12(nbfp, atnr, i, j) = idef->iparams[k].lj.c12*12.0;
147 static real *make_ljpme_c6grid(const gmx_ffparams_t *idef, t_forcerec *fr)
150 real c6, c6i, c6j, c12i, c12j, epsi, epsj, sigmai, sigmaj;
153 /* For LJ-PME simulations, we correct the energies with the reciprocal space
154 * inside of the cut-off. To do this the non-bonded kernels needs to have
155 * access to the C6-values used on the reciprocal grid in pme.c
159 snew(grid, 2*atnr*atnr);
160 for (i = k = 0; (i < atnr); i++)
162 for (j = 0; (j < atnr); j++, k++)
164 c6i = idef->iparams[i*(atnr+1)].lj.c6;
165 c12i = idef->iparams[i*(atnr+1)].lj.c12;
166 c6j = idef->iparams[j*(atnr+1)].lj.c6;
167 c12j = idef->iparams[j*(atnr+1)].lj.c12;
168 c6 = std::sqrt(c6i * c6j);
169 if (fr->ljpme_combination_rule == eljpmeLB
170 && !gmx_numzero(c6) && !gmx_numzero(c12i) && !gmx_numzero(c12j))
172 sigmai = gmx::sixthroot(c12i / c6i);
173 sigmaj = gmx::sixthroot(c12j / c6j);
174 epsi = c6i * c6i / c12i;
175 epsj = c6j * c6j / c12j;
176 c6 = std::sqrt(epsi * epsj) * gmx::power6(0.5*(sigmai+sigmaj));
178 /* Store the elements at the same relative positions as C6 in nbfp in order
179 * to simplify access in the kernels
181 grid[2*(atnr*i+j)] = c6*6.0;
187 static real *mk_nbfp_combination_rule(const gmx_ffparams_t *idef, int comb_rule)
191 real c6i, c6j, c12i, c12j, epsi, epsj, sigmai, sigmaj;
195 snew(nbfp, 2*atnr*atnr);
196 for (i = 0; i < atnr; ++i)
198 for (j = 0; j < atnr; ++j)
200 c6i = idef->iparams[i*(atnr+1)].lj.c6;
201 c12i = idef->iparams[i*(atnr+1)].lj.c12;
202 c6j = idef->iparams[j*(atnr+1)].lj.c6;
203 c12j = idef->iparams[j*(atnr+1)].lj.c12;
204 c6 = std::sqrt(c6i * c6j);
205 c12 = std::sqrt(c12i * c12j);
206 if (comb_rule == eCOMB_ARITHMETIC
207 && !gmx_numzero(c6) && !gmx_numzero(c12))
209 sigmai = gmx::sixthroot(c12i / c6i);
210 sigmaj = gmx::sixthroot(c12j / c6j);
211 epsi = c6i * c6i / c12i;
212 epsj = c6j * c6j / c12j;
213 c6 = std::sqrt(epsi * epsj) * gmx::power6(0.5*(sigmai+sigmaj));
214 c12 = std::sqrt(epsi * epsj) * gmx::power12(0.5*(sigmai+sigmaj));
216 C6(nbfp, atnr, i, j) = c6*6.0;
217 C12(nbfp, atnr, i, j) = c12*12.0;
223 /* This routine sets fr->solvent_opt to the most common solvent in the
224 * system, e.g. esolSPC or esolTIP4P. It will also mark each charge group in
225 * the fr->solvent_type array with the correct type (or esolNO).
227 * Charge groups that fulfill the conditions but are not identical to the
228 * most common one will be marked as esolNO in the solvent_type array.
230 * TIP3p is identical to SPC for these purposes, so we call it
231 * SPC in the arrays (Apologies to Bill Jorgensen ;-)
233 * NOTE: QM particle should not
234 * become an optimized solvent. Not even if there is only one charge
244 } solvent_parameters_t;
247 check_solvent_cg(const gmx_moltype_t *molt,
250 const unsigned char *qm_grpnr,
251 const t_grps *qm_grps,
253 int *n_solvent_parameters,
254 solvent_parameters_t **solvent_parameters_p,
264 real tmp_charge[4] = { 0.0 }; /* init to zero to make gcc 7 happy */
265 int tmp_vdwtype[4] = { 0 }; /* init to zero to make gcc 7 happy */
268 solvent_parameters_t *solvent_parameters;
270 /* We use a list with parameters for each solvent type.
271 * Every time we discover a new molecule that fulfills the basic
272 * conditions for a solvent we compare with the previous entries
273 * in these lists. If the parameters are the same we just increment
274 * the counter for that type, and otherwise we create a new type
275 * based on the current molecule.
277 * Once we've finished going through all molecules we check which
278 * solvent is most common, and mark all those molecules while we
279 * clear the flag on all others.
282 solvent_parameters = *solvent_parameters_p;
284 /* Mark the cg first as non optimized */
287 /* Check if this cg has no exclusions with atoms in other charge groups
288 * and all atoms inside the charge group excluded.
289 * We only have 3 or 4 atom solvent loops.
291 if (GET_CGINFO_EXCL_INTER(cginfo) ||
292 !GET_CGINFO_EXCL_INTRA(cginfo))
297 /* Get the indices of the first atom in this charge group */
298 j0 = molt->cgs.index[cg0];
299 j1 = molt->cgs.index[cg0+1];
301 /* Number of atoms in our molecule */
307 "Moltype '%s': there are %d atoms in this charge group\n",
311 /* Check if it could be an SPC (3 atoms) or TIP4p (4) water,
314 if (nj < 3 || nj > 4)
319 /* Check if we are doing QM on this group */
321 if (qm_grpnr != nullptr)
323 for (j = j0; j < j1 && !qm; j++)
325 qm = (qm_grpnr[j] < qm_grps->nr - 1);
328 /* Cannot use solvent optimization with QM */
334 atom = molt->atoms.atom;
336 /* Still looks like a solvent, time to check parameters */
338 /* If it is perturbed (free energy) we can't use the solvent loops,
339 * so then we just skip to the next molecule.
343 for (j = j0; j < j1 && !perturbed; j++)
345 perturbed = PERTURBED(atom[j]);
353 /* Now it's only a question if the VdW and charge parameters
354 * are OK. Before doing the check we compare and see if they are
355 * identical to a possible previous solvent type.
356 * First we assign the current types and charges.
358 for (j = 0; j < nj; j++)
360 tmp_vdwtype[j] = atom[j0+j].type;
361 tmp_charge[j] = atom[j0+j].q;
364 /* Does it match any previous solvent type? */
365 for (k = 0; k < *n_solvent_parameters; k++)
370 /* We can only match SPC with 3 atoms and TIP4p with 4 atoms */
371 if ( (solvent_parameters[k].model == esolSPC && nj != 3) ||
372 (solvent_parameters[k].model == esolTIP4P && nj != 4) )
377 /* Check that types & charges match for all atoms in molecule */
378 for (j = 0; j < nj && match; j++)
380 if (tmp_vdwtype[j] != solvent_parameters[k].vdwtype[j])
384 if (tmp_charge[j] != solvent_parameters[k].charge[j])
391 /* Congratulations! We have a matched solvent.
392 * Flag it with this type for later processing.
395 solvent_parameters[k].count += nmol;
397 /* We are done with this charge group */
402 /* If we get here, we have a tentative new solvent type.
403 * Before we add it we must check that it fulfills the requirements
404 * of the solvent optimized loops. First determine which atoms have
407 for (j = 0; j < nj; j++)
410 tjA = tmp_vdwtype[j];
412 /* Go through all other tpes and see if any have non-zero
413 * VdW parameters when combined with this one.
415 for (k = 0; k < fr->ntype && (!has_vdw[j]); k++)
417 /* We already checked that the atoms weren't perturbed,
418 * so we only need to check state A now.
422 has_vdw[j] = (has_vdw[j] ||
423 (BHAMA(fr->nbfp, fr->ntype, tjA, k) != 0.0) ||
424 (BHAMB(fr->nbfp, fr->ntype, tjA, k) != 0.0) ||
425 (BHAMC(fr->nbfp, fr->ntype, tjA, k) != 0.0));
430 has_vdw[j] = (has_vdw[j] ||
431 (C6(fr->nbfp, fr->ntype, tjA, k) != 0.0) ||
432 (C12(fr->nbfp, fr->ntype, tjA, k) != 0.0));
437 /* Now we know all we need to make the final check and assignment. */
441 * For this we require thatn all atoms have charge,
442 * the charges on atom 2 & 3 should be the same, and only
443 * atom 1 might have VdW.
447 tmp_charge[0] != 0 &&
448 tmp_charge[1] != 0 &&
449 tmp_charge[2] == tmp_charge[1])
451 srenew(solvent_parameters, *n_solvent_parameters+1);
452 solvent_parameters[*n_solvent_parameters].model = esolSPC;
453 solvent_parameters[*n_solvent_parameters].count = nmol;
454 for (k = 0; k < 3; k++)
456 solvent_parameters[*n_solvent_parameters].vdwtype[k] = tmp_vdwtype[k];
457 solvent_parameters[*n_solvent_parameters].charge[k] = tmp_charge[k];
460 *cg_sp = *n_solvent_parameters;
461 (*n_solvent_parameters)++;
466 /* Or could it be a TIP4P?
467 * For this we require thatn atoms 2,3,4 have charge, but not atom 1.
468 * Only atom 1 mght have VdW.
473 tmp_charge[0] == 0 &&
474 tmp_charge[1] != 0 &&
475 tmp_charge[2] == tmp_charge[1] &&
478 srenew(solvent_parameters, *n_solvent_parameters+1);
479 solvent_parameters[*n_solvent_parameters].model = esolTIP4P;
480 solvent_parameters[*n_solvent_parameters].count = nmol;
481 for (k = 0; k < 4; k++)
483 solvent_parameters[*n_solvent_parameters].vdwtype[k] = tmp_vdwtype[k];
484 solvent_parameters[*n_solvent_parameters].charge[k] = tmp_charge[k];
487 *cg_sp = *n_solvent_parameters;
488 (*n_solvent_parameters)++;
492 *solvent_parameters_p = solvent_parameters;
496 check_solvent(FILE * fp,
497 const gmx_mtop_t * mtop,
499 cginfo_mb_t *cginfo_mb)
502 const gmx_moltype_t *molt;
503 int mol, cg_mol, at_offset, am, cgm, i, nmol_ch, nmol;
504 int n_solvent_parameters;
505 solvent_parameters_t *solvent_parameters;
511 fprintf(debug, "Going to determine what solvent types we have.\n");
514 n_solvent_parameters = 0;
515 solvent_parameters = nullptr;
516 /* Allocate temporary array for solvent type */
517 snew(cg_sp, mtop->molblock.size());
520 for (size_t mb = 0; mb < mtop->molblock.size(); mb++)
522 molt = &mtop->moltype[mtop->molblock[mb].type];
524 /* Here we have to loop over all individual molecules
525 * because we need to check for QMMM particles.
527 snew(cg_sp[mb], cginfo_mb[mb].cg_mod);
528 nmol_ch = cginfo_mb[mb].cg_mod/cgs->nr;
529 nmol = mtop->molblock[mb].nmol/nmol_ch;
530 for (mol = 0; mol < nmol_ch; mol++)
533 am = mol*cgs->index[cgs->nr];
534 for (cg_mol = 0; cg_mol < cgs->nr; cg_mol++)
536 check_solvent_cg(molt, cg_mol, nmol,
537 mtop->groups.grpnr[egcQMMM] ?
538 mtop->groups.grpnr[egcQMMM]+at_offset+am : nullptr,
539 &mtop->groups.grps[egcQMMM],
541 &n_solvent_parameters, &solvent_parameters,
542 cginfo_mb[mb].cginfo[cgm+cg_mol],
543 &cg_sp[mb][cgm+cg_mol]);
546 at_offset += cgs->index[cgs->nr];
549 /* Puh! We finished going through all charge groups.
550 * Now find the most common solvent model.
553 /* Most common solvent this far */
555 for (i = 0; i < n_solvent_parameters; i++)
558 solvent_parameters[i].count > solvent_parameters[bestsp].count)
566 bestsol = solvent_parameters[bestsp].model;
574 for (size_t mb = 0; mb < mtop->molblock.size(); mb++)
576 cgs = &mtop->moltype[mtop->molblock[mb].type].cgs;
577 nmol = (mtop->molblock[mb].nmol*cgs->nr)/cginfo_mb[mb].cg_mod;
578 for (i = 0; i < cginfo_mb[mb].cg_mod; i++)
580 if (cg_sp[mb][i] == bestsp)
582 SET_CGINFO_SOLOPT(cginfo_mb[mb].cginfo[i], bestsol);
587 SET_CGINFO_SOLOPT(cginfo_mb[mb].cginfo[i], esolNO);
594 if (bestsol != esolNO && fp != nullptr)
596 fprintf(fp, "\nEnabling %s-like water optimization for %d molecules.\n\n",
598 solvent_parameters[bestsp].count);
601 sfree(solvent_parameters);
602 fr->solvent_opt = bestsol;
606 acNONE = 0, acCONSTRAINT, acSETTLE
609 static cginfo_mb_t *init_cginfo_mb(FILE *fplog, const gmx_mtop_t *mtop,
610 t_forcerec *fr, gmx_bool bNoSolvOpt,
611 gmx_bool *bFEP_NonBonded,
612 gmx_bool *bExcl_IntraCGAll_InterCGNone)
615 const t_blocka *excl;
616 const gmx_moltype_t *molt;
617 const gmx_molblock_t *molb;
618 cginfo_mb_t *cginfo_mb;
621 int cg_offset, a_offset;
622 int m, cg, a0, a1, gid, ai, j, aj, excl_nalloc;
626 gmx_bool bId, *bExcl, bExclIntraAll, bExclInter, bHaveVDW, bHaveQ, bHavePerturbedAtoms;
628 snew(cginfo_mb, mtop->molblock.size());
630 snew(type_VDW, fr->ntype);
631 for (ai = 0; ai < fr->ntype; ai++)
633 type_VDW[ai] = FALSE;
634 for (j = 0; j < fr->ntype; j++)
636 type_VDW[ai] = type_VDW[ai] ||
638 C6(fr->nbfp, fr->ntype, ai, j) != 0 ||
639 C12(fr->nbfp, fr->ntype, ai, j) != 0;
643 *bFEP_NonBonded = FALSE;
644 *bExcl_IntraCGAll_InterCGNone = TRUE;
647 snew(bExcl, excl_nalloc);
650 for (size_t mb = 0; mb < mtop->molblock.size(); mb++)
652 molb = &mtop->molblock[mb];
653 molt = &mtop->moltype[molb->type];
657 /* Check if the cginfo is identical for all molecules in this block.
658 * If so, we only need an array of the size of one molecule.
659 * Otherwise we make an array of #mol times #cgs per molecule.
662 for (m = 0; m < molb->nmol; m++)
664 int am = m*cgs->index[cgs->nr];
665 for (cg = 0; cg < cgs->nr; cg++)
668 a1 = cgs->index[cg+1];
669 if (getGroupType(mtop->groups, egcENER, a_offset+am+a0) !=
670 getGroupType(mtop->groups, egcENER, a_offset +a0))
674 if (mtop->groups.grpnr[egcQMMM] != nullptr)
676 for (ai = a0; ai < a1; ai++)
678 if (mtop->groups.grpnr[egcQMMM][a_offset+am+ai] !=
679 mtop->groups.grpnr[egcQMMM][a_offset +ai])
688 cginfo_mb[mb].cg_start = cg_offset;
689 cginfo_mb[mb].cg_end = cg_offset + molb->nmol*cgs->nr;
690 cginfo_mb[mb].cg_mod = (bId ? 1 : molb->nmol)*cgs->nr;
691 snew(cginfo_mb[mb].cginfo, cginfo_mb[mb].cg_mod);
692 cginfo = cginfo_mb[mb].cginfo;
694 /* Set constraints flags for constrained atoms */
695 snew(a_con, molt->atoms.nr);
696 for (ftype = 0; ftype < F_NRE; ftype++)
698 if (interaction_function[ftype].flags & IF_CONSTRAINT)
703 for (ia = 0; ia < molt->ilist[ftype].size(); ia += 1+nral)
707 for (a = 0; a < nral; a++)
709 a_con[molt->ilist[ftype].iatoms[ia+1+a]] =
710 (ftype == F_SETTLE ? acSETTLE : acCONSTRAINT);
716 for (m = 0; m < (bId ? 1 : molb->nmol); m++)
719 int am = m*cgs->index[cgs->nr];
720 for (cg = 0; cg < cgs->nr; cg++)
723 a1 = cgs->index[cg+1];
725 /* Store the energy group in cginfo */
726 gid = getGroupType(mtop->groups, egcENER, a_offset+am+a0);
727 SET_CGINFO_GID(cginfo[cgm+cg], gid);
729 /* Check the intra/inter charge group exclusions */
730 if (a1-a0 > excl_nalloc)
732 excl_nalloc = a1 - a0;
733 srenew(bExcl, excl_nalloc);
735 /* bExclIntraAll: all intra cg interactions excluded
736 * bExclInter: any inter cg interactions excluded
738 bExclIntraAll = TRUE;
742 bHavePerturbedAtoms = FALSE;
743 for (ai = a0; ai < a1; ai++)
745 /* Check VDW and electrostatic interactions */
746 bHaveVDW = bHaveVDW || (type_VDW[molt->atoms.atom[ai].type] ||
747 type_VDW[molt->atoms.atom[ai].typeB]);
748 bHaveQ = bHaveQ || (molt->atoms.atom[ai].q != 0 ||
749 molt->atoms.atom[ai].qB != 0);
751 bHavePerturbedAtoms = bHavePerturbedAtoms || (PERTURBED(molt->atoms.atom[ai]) != 0);
753 /* Clear the exclusion list for atom ai */
754 for (aj = a0; aj < a1; aj++)
756 bExcl[aj-a0] = FALSE;
758 /* Loop over all the exclusions of atom ai */
759 for (j = excl->index[ai]; j < excl->index[ai+1]; j++)
762 if (aj < a0 || aj >= a1)
771 /* Check if ai excludes a0 to a1 */
772 for (aj = a0; aj < a1; aj++)
776 bExclIntraAll = FALSE;
783 SET_CGINFO_CONSTR(cginfo[cgm+cg]);
786 SET_CGINFO_SETTLE(cginfo[cgm+cg]);
794 SET_CGINFO_EXCL_INTRA(cginfo[cgm+cg]);
798 SET_CGINFO_EXCL_INTER(cginfo[cgm+cg]);
800 if (a1 - a0 > MAX_CHARGEGROUP_SIZE)
802 /* The size in cginfo is currently only read with DD */
803 gmx_fatal(FARGS, "A charge group has size %d which is larger than the limit of %d atoms", a1-a0, MAX_CHARGEGROUP_SIZE);
807 SET_CGINFO_HAS_VDW(cginfo[cgm+cg]);
811 SET_CGINFO_HAS_Q(cginfo[cgm+cg]);
813 if (bHavePerturbedAtoms && fr->efep != efepNO)
815 SET_CGINFO_FEP(cginfo[cgm+cg]);
816 *bFEP_NonBonded = TRUE;
818 /* Store the charge group size */
819 SET_CGINFO_NATOMS(cginfo[cgm+cg], a1-a0);
821 if (!bExclIntraAll || bExclInter)
823 *bExcl_IntraCGAll_InterCGNone = FALSE;
830 cg_offset += molb->nmol*cgs->nr;
831 a_offset += molb->nmol*cgs->index[cgs->nr];
836 /* the solvent optimizer is called after the QM is initialized,
837 * because we don't want to have the QM subsystemto become an
841 check_solvent(fplog, mtop, fr, cginfo_mb);
843 if (getenv("GMX_NO_SOLV_OPT"))
847 fprintf(fplog, "Found environment variable GMX_NO_SOLV_OPT.\n"
848 "Disabling all solvent optimization\n");
850 fr->solvent_opt = esolNO;
854 fr->solvent_opt = esolNO;
856 if (!fr->solvent_opt)
858 for (size_t mb = 0; mb < mtop->molblock.size(); mb++)
860 for (cg = 0; cg < cginfo_mb[mb].cg_mod; cg++)
862 SET_CGINFO_SOLOPT(cginfo_mb[mb].cginfo[cg], esolNO);
870 static int *cginfo_expand(int nmb, cginfo_mb_t *cgi_mb)
875 ncg = cgi_mb[nmb-1].cg_end;
878 for (cg = 0; cg < ncg; cg++)
880 while (cg >= cgi_mb[mb].cg_end)
885 cgi_mb[mb].cginfo[(cg - cgi_mb[mb].cg_start) % cgi_mb[mb].cg_mod];
891 static void done_cginfo_mb(cginfo_mb_t *cginfo_mb, int numMolBlocks)
893 if (cginfo_mb == nullptr)
897 for (int mb = 0; mb < numMolBlocks; ++mb)
899 sfree(cginfo_mb[mb].cginfo);
904 /* Sets the sum of charges (squared) and C6 in the system in fr.
905 * Returns whether the system has a net charge.
907 static bool set_chargesum(FILE *log, t_forcerec *fr, const gmx_mtop_t *mtop)
909 /*This now calculates sum for q and c6*/
910 double qsum, q2sum, q, c6sum, c6;
915 for (const gmx_molblock_t &molb : mtop->molblock)
917 int nmol = molb.nmol;
918 const t_atoms *atoms = &mtop->moltype[molb.type].atoms;
919 for (int i = 0; i < atoms->nr; i++)
921 q = atoms->atom[i].q;
924 c6 = mtop->ffparams.iparams[atoms->atom[i].type*(mtop->ffparams.atnr+1)].lj.c6;
929 fr->q2sum[0] = q2sum;
930 fr->c6sum[0] = c6sum;
932 if (fr->efep != efepNO)
937 for (const gmx_molblock_t &molb : mtop->molblock)
939 int nmol = molb.nmol;
940 const t_atoms *atoms = &mtop->moltype[molb.type].atoms;
941 for (int i = 0; i < atoms->nr; i++)
943 q = atoms->atom[i].qB;
946 c6 = mtop->ffparams.iparams[atoms->atom[i].typeB*(mtop->ffparams.atnr+1)].lj.c6;
950 fr->q2sum[1] = q2sum;
951 fr->c6sum[1] = c6sum;
956 fr->qsum[1] = fr->qsum[0];
957 fr->q2sum[1] = fr->q2sum[0];
958 fr->c6sum[1] = fr->c6sum[0];
962 if (fr->efep == efepNO)
964 fprintf(log, "System total charge: %.3f\n", fr->qsum[0]);
968 fprintf(log, "System total charge, top. A: %.3f top. B: %.3f\n",
969 fr->qsum[0], fr->qsum[1]);
973 /* A cut-off of 1e-4 is used to catch rounding errors due to ascii input */
974 return (std::abs(fr->qsum[0]) > 1e-4 ||
975 std::abs(fr->qsum[1]) > 1e-4);
978 void update_forcerec(t_forcerec *fr, matrix box)
980 if (fr->ic->eeltype == eelGRF)
982 calc_rffac(nullptr, fr->ic->eeltype, fr->ic->epsilon_r, fr->ic->epsilon_rf,
983 fr->ic->rcoulomb, fr->temp, fr->zsquare, box,
984 &fr->ic->k_rf, &fr->ic->c_rf);
988 void set_avcsixtwelve(FILE *fplog, t_forcerec *fr, const gmx_mtop_t *mtop)
990 const t_atoms *atoms, *atoms_tpi;
991 const t_blocka *excl;
992 int nmolc, i, j, tpi, tpj, j1, j2, k, nexcl, q;
993 int64_t npair, npair_ij, tmpi, tmpj;
994 double csix, ctwelve;
998 real *nbfp_comb = nullptr;
1004 /* For LJ-PME, we want to correct for the difference between the
1005 * actual C6 values and the C6 values used by the LJ-PME based on
1006 * combination rules. */
1008 if (EVDW_PME(fr->ic->vdwtype))
1010 nbfp_comb = mk_nbfp_combination_rule(&mtop->ffparams,
1011 (fr->ljpme_combination_rule == eljpmeLB) ? eCOMB_ARITHMETIC : eCOMB_GEOMETRIC);
1012 for (tpi = 0; tpi < ntp; ++tpi)
1014 for (tpj = 0; tpj < ntp; ++tpj)
1016 C6(nbfp_comb, ntp, tpi, tpj) =
1017 C6(nbfp, ntp, tpi, tpj) - C6(nbfp_comb, ntp, tpi, tpj);
1018 C12(nbfp_comb, ntp, tpi, tpj) = C12(nbfp, ntp, tpi, tpj);
1023 for (q = 0; q < (fr->efep == efepNO ? 1 : 2); q++)
1031 /* Count the types so we avoid natoms^2 operations */
1032 snew(typecount, ntp);
1033 gmx_mtop_count_atomtypes(mtop, q, typecount);
1035 for (tpi = 0; tpi < ntp; tpi++)
1037 for (tpj = tpi; tpj < ntp; tpj++)
1039 tmpi = typecount[tpi];
1040 tmpj = typecount[tpj];
1043 npair_ij = tmpi*tmpj;
1047 npair_ij = tmpi*(tmpi - 1)/2;
1051 /* nbfp now includes the 6.0 derivative prefactor */
1052 csix += npair_ij*BHAMC(nbfp, ntp, tpi, tpj)/6.0;
1056 /* nbfp now includes the 6.0/12.0 derivative prefactors */
1057 csix += npair_ij* C6(nbfp, ntp, tpi, tpj)/6.0;
1058 ctwelve += npair_ij* C12(nbfp, ntp, tpi, tpj)/12.0;
1064 /* Subtract the excluded pairs.
1065 * The main reason for substracting exclusions is that in some cases
1066 * some combinations might never occur and the parameters could have
1067 * any value. These unused values should not influence the dispersion
1070 for (const gmx_molblock_t &molb : mtop->molblock)
1072 int nmol = molb.nmol;
1073 atoms = &mtop->moltype[molb.type].atoms;
1074 excl = &mtop->moltype[molb.type].excls;
1075 for (int i = 0; (i < atoms->nr); i++)
1079 tpi = atoms->atom[i].type;
1083 tpi = atoms->atom[i].typeB;
1085 j1 = excl->index[i];
1086 j2 = excl->index[i+1];
1087 for (j = j1; j < j2; j++)
1094 tpj = atoms->atom[k].type;
1098 tpj = atoms->atom[k].typeB;
1102 /* nbfp now includes the 6.0 derivative prefactor */
1103 csix -= nmol*BHAMC(nbfp, ntp, tpi, tpj)/6.0;
1107 /* nbfp now includes the 6.0/12.0 derivative prefactors */
1108 csix -= nmol*C6 (nbfp, ntp, tpi, tpj)/6.0;
1109 ctwelve -= nmol*C12(nbfp, ntp, tpi, tpj)/12.0;
1119 /* Only correct for the interaction of the test particle
1120 * with the rest of the system.
1123 &mtop->moltype[mtop->molblock.back().type].atoms;
1126 for (size_t mb = 0; mb < mtop->molblock.size(); mb++)
1128 const gmx_molblock_t &molb = mtop->molblock[mb];
1129 atoms = &mtop->moltype[molb.type].atoms;
1130 for (j = 0; j < atoms->nr; j++)
1133 /* Remove the interaction of the test charge group
1136 if (mb == mtop->molblock.size() - 1)
1140 if (mb == 0 && molb.nmol == 1)
1142 gmx_fatal(FARGS, "Old format tpr with TPI, please generate a new tpr file");
1147 tpj = atoms->atom[j].type;
1151 tpj = atoms->atom[j].typeB;
1153 for (i = 0; i < fr->n_tpi; i++)
1157 tpi = atoms_tpi->atom[i].type;
1161 tpi = atoms_tpi->atom[i].typeB;
1165 /* nbfp now includes the 6.0 derivative prefactor */
1166 csix += nmolc*BHAMC(nbfp, ntp, tpi, tpj)/6.0;
1170 /* nbfp now includes the 6.0/12.0 derivative prefactors */
1171 csix += nmolc*C6 (nbfp, ntp, tpi, tpj)/6.0;
1172 ctwelve += nmolc*C12(nbfp, ntp, tpi, tpj)/12.0;
1179 if (npair - nexcl <= 0 && fplog)
1181 fprintf(fplog, "\nWARNING: There are no atom pairs for dispersion correction\n\n");
1187 csix /= npair - nexcl;
1188 ctwelve /= npair - nexcl;
1192 fprintf(debug, "Counted %d exclusions\n", nexcl);
1193 fprintf(debug, "Average C6 parameter is: %10g\n", csix);
1194 fprintf(debug, "Average C12 parameter is: %10g\n", ctwelve);
1196 fr->avcsix[q] = csix;
1197 fr->avctwelve[q] = ctwelve;
1200 if (EVDW_PME(fr->ic->vdwtype))
1205 if (fplog != nullptr)
1207 if (fr->eDispCorr == edispcAllEner ||
1208 fr->eDispCorr == edispcAllEnerPres)
1210 fprintf(fplog, "Long Range LJ corr.: <C6> %10.4e, <C12> %10.4e\n",
1211 fr->avcsix[0], fr->avctwelve[0]);
1215 fprintf(fplog, "Long Range LJ corr.: <C6> %10.4e\n", fr->avcsix[0]);
1221 static real calcBuckinghamBMax(FILE *fplog, const gmx_mtop_t *mtop)
1223 const t_atoms *at1, *at2;
1224 int i, j, tpi, tpj, ntypes;
1229 fprintf(fplog, "Determining largest Buckingham b parameter for table\n");
1231 ntypes = mtop->ffparams.atnr;
1234 real bham_b_max = 0;
1235 for (size_t mt1 = 0; mt1 < mtop->moltype.size(); mt1++)
1237 at1 = &mtop->moltype[mt1].atoms;
1238 for (i = 0; (i < at1->nr); i++)
1240 tpi = at1->atom[i].type;
1243 gmx_fatal(FARGS, "Atomtype[%d] = %d, maximum = %d", i, tpi, ntypes);
1246 for (size_t mt2 = mt1; mt2 < mtop->moltype.size(); mt2++)
1248 at2 = &mtop->moltype[mt2].atoms;
1249 for (j = 0; (j < at2->nr); j++)
1251 tpj = at2->atom[j].type;
1254 gmx_fatal(FARGS, "Atomtype[%d] = %d, maximum = %d", j, tpj, ntypes);
1256 b = mtop->ffparams.iparams[tpi*ntypes + tpj].bham.b;
1261 if ((b < bmin) || (bmin == -1))
1271 fprintf(fplog, "Buckingham b parameters, min: %g, max: %g\n",
1278 static void make_nbf_tables(FILE *fp,
1279 const interaction_const_t *ic, real rtab,
1280 const char *tabfn, char *eg1, char *eg2,
1286 if (tabfn == nullptr)
1290 fprintf(debug, "No table file name passed, can not read table, can not do non-bonded interactions\n");
1295 sprintf(buf, "%s", tabfn);
1298 /* Append the two energy group names */
1299 sprintf(buf + strlen(tabfn) - strlen(ftp2ext(efXVG)) - 1, "_%s_%s.%s",
1300 eg1, eg2, ftp2ext(efXVG));
1302 nbl->table_elec_vdw = make_tables(fp, ic, buf, rtab, 0);
1303 /* Copy the contents of the table to separate coulomb and LJ tables too,
1304 * to improve cache performance.
1306 /* For performance reasons we want
1307 * the table data to be aligned to 16-byte. The pointers could be freed
1308 * but currently aren't.
1310 snew(nbl->table_elec, 1);
1311 nbl->table_elec->interaction = GMX_TABLE_INTERACTION_ELEC;
1312 nbl->table_elec->format = nbl->table_elec_vdw->format;
1313 nbl->table_elec->r = nbl->table_elec_vdw->r;
1314 nbl->table_elec->n = nbl->table_elec_vdw->n;
1315 nbl->table_elec->scale = nbl->table_elec_vdw->scale;
1316 nbl->table_elec->formatsize = nbl->table_elec_vdw->formatsize;
1317 nbl->table_elec->ninteractions = 1;
1318 nbl->table_elec->stride = nbl->table_elec->formatsize * nbl->table_elec->ninteractions;
1319 snew_aligned(nbl->table_elec->data, nbl->table_elec->stride*(nbl->table_elec->n+1), 32);
1321 snew(nbl->table_vdw, 1);
1322 nbl->table_vdw->interaction = GMX_TABLE_INTERACTION_VDWREP_VDWDISP;
1323 nbl->table_vdw->format = nbl->table_elec_vdw->format;
1324 nbl->table_vdw->r = nbl->table_elec_vdw->r;
1325 nbl->table_vdw->n = nbl->table_elec_vdw->n;
1326 nbl->table_vdw->scale = nbl->table_elec_vdw->scale;
1327 nbl->table_vdw->formatsize = nbl->table_elec_vdw->formatsize;
1328 nbl->table_vdw->ninteractions = 2;
1329 nbl->table_vdw->stride = nbl->table_vdw->formatsize * nbl->table_vdw->ninteractions;
1330 snew_aligned(nbl->table_vdw->data, nbl->table_vdw->stride*(nbl->table_vdw->n+1), 32);
1332 /* NOTE: Using a single i-loop here leads to mix-up of data in table_vdw
1333 * with (at least) gcc 6.2, 6.3 and 6.4 when compiled with -O3 and AVX
1335 for (i = 0; i <= nbl->table_elec_vdw->n; i++)
1337 for (j = 0; j < 4; j++)
1339 nbl->table_elec->data[4*i+j] = nbl->table_elec_vdw->data[12*i+j];
1342 for (i = 0; i <= nbl->table_elec_vdw->n; i++)
1344 for (j = 0; j < 8; j++)
1346 nbl->table_vdw->data[8*i+j] = nbl->table_elec_vdw->data[12*i+4+j];
1351 /*!\brief If there's bonded interactions of type \c ftype1 or \c
1352 * ftype2 present in the topology, build an array of the number of
1353 * interactions present for each bonded interaction index found in the
1356 * \c ftype1 or \c ftype2 may be set to -1 to disable seeking for a
1357 * valid type with that parameter.
1359 * \c count will be reallocated as necessary to fit the largest bonded
1360 * interaction index found, and its current size will be returned in
1361 * \c ncount. It will contain zero for every bonded interaction index
1362 * for which no interactions are present in the topology.
1364 static void count_tables(int ftype1, int ftype2, const gmx_mtop_t *mtop,
1365 int *ncount, int **count)
1367 int ftype, i, j, tabnr;
1369 // Loop over all moleculetypes
1370 for (const gmx_moltype_t &molt : mtop->moltype)
1372 // Loop over all interaction types
1373 for (ftype = 0; ftype < F_NRE; ftype++)
1375 // If the current interaction type is one of the types whose tables we're trying to count...
1376 if (ftype == ftype1 || ftype == ftype2)
1378 const InteractionList &il = molt.ilist[ftype];
1379 const int stride = 1 + NRAL(ftype);
1380 // ... and there are actually some interactions for this type
1381 for (i = 0; i < il.size(); i += stride)
1383 // Find out which table index the user wanted
1384 tabnr = mtop->ffparams.iparams[il.iatoms[i]].tab.table;
1387 gmx_fatal(FARGS, "A bonded table number is smaller than 0: %d\n", tabnr);
1389 // Make room for this index in the data structure
1390 if (tabnr >= *ncount)
1392 srenew(*count, tabnr+1);
1393 for (j = *ncount; j < tabnr+1; j++)
1399 // Record that this table index is used and must have a valid file
1407 /*!\brief If there's bonded interactions of flavour \c tabext and type
1408 * \c ftype1 or \c ftype2 present in the topology, seek them in the
1409 * list of filenames passed to mdrun, and make bonded tables from
1412 * \c ftype1 or \c ftype2 may be set to -1 to disable seeking for a
1413 * valid type with that parameter.
1415 * A fatal error occurs if no matching filename is found.
1417 static bondedtable_t *make_bonded_tables(FILE *fplog,
1418 int ftype1, int ftype2,
1419 const gmx_mtop_t *mtop,
1420 gmx::ArrayRef<const std::string> tabbfnm,
1430 count_tables(ftype1, ftype2, mtop, &ncount, &count);
1432 // Are there any relevant tabulated bond interactions?
1436 for (int i = 0; i < ncount; i++)
1438 // Do any interactions exist that requires this table?
1441 // This pattern enforces the current requirement that
1442 // table filenames end in a characteristic sequence
1443 // before the file type extension, and avoids table 13
1444 // being recognized and used for table 1.
1445 std::string patternToFind = gmx::formatString("_%s%d.%s", tabext, i, ftp2ext(efXVG));
1446 bool madeTable = false;
1447 for (gmx::index j = 0; j < tabbfnm.ssize() && !madeTable; ++j)
1449 if (gmx::endsWith(tabbfnm[j], patternToFind))
1451 // Finally read the table from the file found
1452 tab[i] = make_bonded_table(fplog, tabbfnm[j].c_str(), NRAL(ftype1)-2);
1458 bool isPlural = (ftype2 != -1);
1459 gmx_fatal(FARGS, "Tabulated interaction of type '%s%s%s' with index %d cannot be used because no table file whose name matched '%s' was passed via the gmx mdrun -tableb command-line option.",
1460 interaction_function[ftype1].longname,
1461 isPlural ? "' or '" : "",
1462 isPlural ? interaction_function[ftype2].longname : "",
1464 patternToFind.c_str());
1474 void forcerec_set_ranges(t_forcerec *fr,
1475 int ncg_home, int ncg_force,
1477 int natoms_force_constr, int natoms_f_novirsum)
1482 /* fr->ncg_force is unused in the standard code,
1483 * but it can be useful for modified code dealing with charge groups.
1485 fr->ncg_force = ncg_force;
1486 fr->natoms_force = natoms_force;
1487 fr->natoms_force_constr = natoms_force_constr;
1489 if (fr->natoms_force_constr > fr->nalloc_force)
1491 fr->nalloc_force = over_alloc_dd(fr->natoms_force_constr);
1494 if (fr->haveDirectVirialContributions)
1496 fr->forceBufferForDirectVirialContributions->resize(natoms_f_novirsum);
1500 static real cutoff_inf(real cutoff)
1504 cutoff = GMX_CUTOFF_INF;
1510 /*! \brief Print Coulomb Ewald citations and set ewald coefficients */
1511 static void initCoulombEwaldParameters(FILE *fp, const t_inputrec *ir,
1512 bool systemHasNetCharge,
1513 interaction_const_t *ic)
1515 if (!EEL_PME_EWALD(ir->coulombtype))
1522 fprintf(fp, "Will do PME sum in reciprocal space for electrostatic interactions.\n");
1524 if (ir->coulombtype == eelP3M_AD)
1526 please_cite(fp, "Hockney1988");
1527 please_cite(fp, "Ballenegger2012");
1531 please_cite(fp, "Essmann95a");
1534 if (ir->ewald_geometry == eewg3DC)
1538 fprintf(fp, "Using the Ewald3DC correction for systems with a slab geometry%s.\n",
1539 systemHasNetCharge ? " and net charge" : "");
1541 please_cite(fp, "In-Chul99a");
1542 if (systemHasNetCharge)
1544 please_cite(fp, "Ballenegger2009");
1549 ic->ewaldcoeff_q = calc_ewaldcoeff_q(ir->rcoulomb, ir->ewald_rtol);
1552 fprintf(fp, "Using a Gaussian width (1/beta) of %g nm for Ewald\n",
1553 1/ic->ewaldcoeff_q);
1556 if (ic->coulomb_modifier == eintmodPOTSHIFT)
1558 GMX_RELEASE_ASSERT(ic->rcoulomb != 0, "Cutoff radius cannot be zero");
1559 ic->sh_ewald = std::erfc(ic->ewaldcoeff_q*ic->rcoulomb) / ic->rcoulomb;
1567 /*! \brief Print Van der Waals Ewald citations and set ewald coefficients */
1568 static void initVdwEwaldParameters(FILE *fp, const t_inputrec *ir,
1569 interaction_const_t *ic)
1571 if (!EVDW_PME(ir->vdwtype))
1578 fprintf(fp, "Will do PME sum in reciprocal space for LJ dispersion interactions.\n");
1579 please_cite(fp, "Essmann95a");
1581 ic->ewaldcoeff_lj = calc_ewaldcoeff_lj(ir->rvdw, ir->ewald_rtol_lj);
1584 fprintf(fp, "Using a Gaussian width (1/beta) of %g nm for LJ Ewald\n",
1585 1/ic->ewaldcoeff_lj);
1588 if (ic->vdw_modifier == eintmodPOTSHIFT)
1590 real crc2 = gmx::square(ic->ewaldcoeff_lj*ic->rvdw);
1591 ic->sh_lj_ewald = (std::exp(-crc2)*(1 + crc2 + 0.5*crc2*crc2) - 1)/gmx::power6(ic->rvdw);
1595 ic->sh_lj_ewald = 0;
1599 gmx_bool uses_simple_tables(int cutoff_scheme,
1600 const nonbonded_verlet_t *nbv)
1602 gmx_bool bUsesSimpleTables = TRUE;
1604 switch (cutoff_scheme)
1607 bUsesSimpleTables = TRUE;
1610 GMX_RELEASE_ASSERT(nullptr != nbv, "A non-bonded verlet object is required with the Verlet cutoff-scheme");
1611 bUsesSimpleTables = nbv->pairlistIsSimple();
1614 gmx_incons("unimplemented");
1616 return bUsesSimpleTables;
1619 static void init_ewald_f_table(interaction_const_t *ic,
1624 /* Get the Ewald table spacing based on Coulomb and/or LJ
1625 * Ewald coefficients and rtol.
1627 ic->tabq_scale = ewald_spline3_table_scale(ic);
1629 if (ic->cutoff_scheme == ecutsVERLET)
1631 maxr = ic->rcoulomb;
1635 maxr = std::max(ic->rcoulomb, rtab);
1637 ic->tabq_size = static_cast<int>(maxr*ic->tabq_scale) + 2;
1639 sfree_aligned(ic->tabq_coul_FDV0);
1640 sfree_aligned(ic->tabq_coul_F);
1641 sfree_aligned(ic->tabq_coul_V);
1643 sfree_aligned(ic->tabq_vdw_FDV0);
1644 sfree_aligned(ic->tabq_vdw_F);
1645 sfree_aligned(ic->tabq_vdw_V);
1647 if (EEL_PME_EWALD(ic->eeltype))
1649 /* Create the original table data in FDV0 */
1650 snew_aligned(ic->tabq_coul_FDV0, ic->tabq_size*4, 32);
1651 snew_aligned(ic->tabq_coul_F, ic->tabq_size, 32);
1652 snew_aligned(ic->tabq_coul_V, ic->tabq_size, 32);
1653 table_spline3_fill_ewald_lr(ic->tabq_coul_F, ic->tabq_coul_V, ic->tabq_coul_FDV0,
1654 ic->tabq_size, 1/ic->tabq_scale, ic->ewaldcoeff_q, v_q_ewald_lr);
1657 if (EVDW_PME(ic->vdwtype))
1659 snew_aligned(ic->tabq_vdw_FDV0, ic->tabq_size*4, 32);
1660 snew_aligned(ic->tabq_vdw_F, ic->tabq_size, 32);
1661 snew_aligned(ic->tabq_vdw_V, ic->tabq_size, 32);
1662 table_spline3_fill_ewald_lr(ic->tabq_vdw_F, ic->tabq_vdw_V, ic->tabq_vdw_FDV0,
1663 ic->tabq_size, 1/ic->tabq_scale, ic->ewaldcoeff_lj, v_lj_ewald_lr);
1667 void init_interaction_const_tables(FILE *fp,
1668 interaction_const_t *ic,
1671 if (EEL_PME_EWALD(ic->eeltype) || EVDW_PME(ic->vdwtype))
1673 init_ewald_f_table(ic, rtab);
1677 fprintf(fp, "Initialized non-bonded Ewald correction tables, spacing: %.2e size: %d\n\n",
1678 1/ic->tabq_scale, ic->tabq_size);
1683 static void clear_force_switch_constants(shift_consts_t *sc)
1690 static void force_switch_constants(real p,
1694 /* Here we determine the coefficient for shifting the force to zero
1695 * between distance rsw and the cut-off rc.
1696 * For a potential of r^-p, we have force p*r^-(p+1).
1697 * But to save flops we absorb p in the coefficient.
1699 * force/p = r^-(p+1) + c2*r^2 + c3*r^3
1700 * potential = r^-p + c2/3*r^3 + c3/4*r^4 + cpot
1702 sc->c2 = ((p + 1)*rsw - (p + 4)*rc)/(pow(rc, p + 2)*gmx::square(rc - rsw));
1703 sc->c3 = -((p + 1)*rsw - (p + 3)*rc)/(pow(rc, p + 2)*gmx::power3(rc - rsw));
1704 sc->cpot = -pow(rc, -p) + p*sc->c2/3*gmx::power3(rc - rsw) + p*sc->c3/4*gmx::power4(rc - rsw);
1707 static void potential_switch_constants(real rsw, real rc,
1708 switch_consts_t *sc)
1710 /* The switch function is 1 at rsw and 0 at rc.
1711 * The derivative and second derivate are zero at both ends.
1712 * rsw = max(r - r_switch, 0)
1713 * sw = 1 + c3*rsw^3 + c4*rsw^4 + c5*rsw^5
1714 * dsw = 3*c3*rsw^2 + 4*c4*rsw^3 + 5*c5*rsw^4
1715 * force = force*dsw - potential*sw
1718 sc->c3 = -10/gmx::power3(rc - rsw);
1719 sc->c4 = 15/gmx::power4(rc - rsw);
1720 sc->c5 = -6/gmx::power5(rc - rsw);
1723 /*! \brief Construct interaction constants
1725 * This data is used (particularly) by search and force code for
1726 * short-range interactions. Many of these are constant for the whole
1727 * simulation; some are constant only after PME tuning completes.
1730 init_interaction_const(FILE *fp,
1731 interaction_const_t **interaction_const,
1732 const t_inputrec *ir,
1733 const gmx_mtop_t *mtop,
1734 bool systemHasNetCharge)
1736 interaction_const_t *ic;
1740 ic->cutoff_scheme = ir->cutoff_scheme;
1742 /* Just allocate something so we can free it */
1743 snew_aligned(ic->tabq_coul_FDV0, 16, 32);
1744 snew_aligned(ic->tabq_coul_F, 16, 32);
1745 snew_aligned(ic->tabq_coul_V, 16, 32);
1748 ic->vdwtype = ir->vdwtype;
1749 ic->vdw_modifier = ir->vdw_modifier;
1750 ic->reppow = mtop->ffparams.reppow;
1751 ic->rvdw = cutoff_inf(ir->rvdw);
1752 ic->rvdw_switch = ir->rvdw_switch;
1753 ic->ljpme_comb_rule = ir->ljpme_combination_rule;
1754 ic->useBuckingham = (mtop->ffparams.functype[0] == F_BHAM);
1755 if (ic->useBuckingham)
1757 ic->buckinghamBMax = calcBuckinghamBMax(fp, mtop);
1760 initVdwEwaldParameters(fp, ir, ic);
1762 clear_force_switch_constants(&ic->dispersion_shift);
1763 clear_force_switch_constants(&ic->repulsion_shift);
1765 switch (ic->vdw_modifier)
1767 case eintmodPOTSHIFT:
1768 /* Only shift the potential, don't touch the force */
1769 ic->dispersion_shift.cpot = -1.0/gmx::power6(ic->rvdw);
1770 ic->repulsion_shift.cpot = -1.0/gmx::power12(ic->rvdw);
1772 case eintmodFORCESWITCH:
1773 /* Switch the force, switch and shift the potential */
1774 force_switch_constants(6.0, ic->rvdw_switch, ic->rvdw,
1775 &ic->dispersion_shift);
1776 force_switch_constants(12.0, ic->rvdw_switch, ic->rvdw,
1777 &ic->repulsion_shift);
1779 case eintmodPOTSWITCH:
1780 /* Switch the potential and force */
1781 potential_switch_constants(ic->rvdw_switch, ic->rvdw,
1785 case eintmodEXACTCUTOFF:
1786 /* Nothing to do here */
1789 gmx_incons("unimplemented potential modifier");
1792 ic->sh_invrc6 = -ic->dispersion_shift.cpot;
1794 /* Electrostatics */
1795 ic->eeltype = ir->coulombtype;
1796 ic->coulomb_modifier = ir->coulomb_modifier;
1797 ic->rcoulomb = cutoff_inf(ir->rcoulomb);
1798 ic->rcoulomb_switch = ir->rcoulomb_switch;
1799 ic->epsilon_r = ir->epsilon_r;
1801 /* Set the Coulomb energy conversion factor */
1802 if (ic->epsilon_r != 0)
1804 ic->epsfac = ONE_4PI_EPS0/ic->epsilon_r;
1808 /* eps = 0 is infinite dieletric: no Coulomb interactions */
1812 /* Reaction-field */
1813 if (EEL_RF(ic->eeltype))
1815 ic->epsilon_rf = ir->epsilon_rf;
1816 /* Generalized reaction field parameters are updated every step */
1817 if (ic->eeltype != eelGRF)
1819 calc_rffac(fp, ic->eeltype, ic->epsilon_r, ic->epsilon_rf,
1820 ic->rcoulomb, 0, 0, nullptr,
1821 &ic->k_rf, &ic->c_rf);
1824 if (ir->cutoff_scheme == ecutsGROUP && ic->eeltype == eelRF_ZERO)
1826 /* grompp should have done this, but this scheme is obsolete */
1827 ic->coulomb_modifier = eintmodEXACTCUTOFF;
1832 /* For plain cut-off we might use the reaction-field kernels */
1833 ic->epsilon_rf = ic->epsilon_r;
1835 if (ir->coulomb_modifier == eintmodPOTSHIFT)
1837 ic->c_rf = 1/ic->rcoulomb;
1845 initCoulombEwaldParameters(fp, ir, systemHasNetCharge, ic);
1849 real dispersion_shift;
1851 dispersion_shift = ic->dispersion_shift.cpot;
1852 if (EVDW_PME(ic->vdwtype))
1854 dispersion_shift -= ic->sh_lj_ewald;
1856 fprintf(fp, "Potential shift: LJ r^-12: %.3e r^-6: %.3e",
1857 ic->repulsion_shift.cpot, dispersion_shift);
1859 if (ic->eeltype == eelCUT)
1861 fprintf(fp, ", Coulomb %.e", -ic->c_rf);
1863 else if (EEL_PME(ic->eeltype))
1865 fprintf(fp, ", Ewald %.3e", -ic->sh_ewald);
1870 *interaction_const = ic;
1874 done_interaction_const(interaction_const_t *interaction_const)
1876 sfree_aligned(interaction_const->tabq_coul_FDV0);
1877 sfree_aligned(interaction_const->tabq_coul_F);
1878 sfree_aligned(interaction_const->tabq_coul_V);
1879 sfree(interaction_const);
1882 void init_forcerec(FILE *fp,
1883 const gmx::MDLogger &mdlog,
1886 const t_inputrec *ir,
1887 const gmx_mtop_t *mtop,
1888 const t_commrec *cr,
1892 gmx::ArrayRef<const std::string> tabbfnm,
1893 const gmx_hw_info_t &hardwareInfo,
1894 const gmx_device_info_t *deviceInfo,
1895 const bool useGpuForBonded,
1896 gmx_bool bNoSolvOpt,
1899 int m, negp_pp, negptable, egi, egj;
1904 gmx_bool bGenericKernelOnly;
1905 gmx_bool needGroupSchemeTables, bSomeNormalNbListsAreInUse;
1906 gmx_bool bFEP_NonBonded;
1907 int *nm_ind, egp_flags;
1909 /* By default we turn SIMD kernels on, but it might be turned off further down... */
1910 fr->use_simd_kernels = TRUE;
1912 if (check_box(ir->ePBC, box))
1914 gmx_fatal(FARGS, "%s", check_box(ir->ePBC, box));
1917 /* Test particle insertion ? */
1920 /* Set to the size of the molecule to be inserted (the last one) */
1921 /* Because of old style topologies, we have to use the last cg
1922 * instead of the last molecule type.
1924 cgs = &mtop->moltype[mtop->molblock.back().type].cgs;
1925 fr->n_tpi = cgs->index[cgs->nr] - cgs->index[cgs->nr-1];
1926 gmx::RangePartitioning molecules = gmx_mtop_molecules(*mtop);
1927 if (fr->n_tpi != molecules.block(molecules.numBlocks() - 1).size())
1929 gmx_fatal(FARGS, "The molecule to insert can not consist of multiple charge groups.\nMake it a single charge group.");
1937 if (ir->coulombtype == eelRF_NEC_UNSUPPORTED)
1939 gmx_fatal(FARGS, "%s electrostatics is no longer supported",
1940 eel_names[ir->coulombtype]);
1945 gmx_fatal(FARGS, "AdResS simulations are no longer supported");
1947 if (ir->useTwinRange)
1949 gmx_fatal(FARGS, "Twin-range simulations are no longer supported");
1951 /* Copy the user determined parameters */
1952 fr->userint1 = ir->userint1;
1953 fr->userint2 = ir->userint2;
1954 fr->userint3 = ir->userint3;
1955 fr->userint4 = ir->userint4;
1956 fr->userreal1 = ir->userreal1;
1957 fr->userreal2 = ir->userreal2;
1958 fr->userreal3 = ir->userreal3;
1959 fr->userreal4 = ir->userreal4;
1962 fr->fc_stepsize = ir->fc_stepsize;
1965 fr->efep = ir->efep;
1966 fr->sc_alphavdw = ir->fepvals->sc_alpha;
1967 if (ir->fepvals->bScCoul)
1969 fr->sc_alphacoul = ir->fepvals->sc_alpha;
1970 fr->sc_sigma6_min = gmx::power6(ir->fepvals->sc_sigma_min);
1974 fr->sc_alphacoul = 0;
1975 fr->sc_sigma6_min = 0; /* only needed when bScCoul is on */
1977 fr->sc_power = ir->fepvals->sc_power;
1978 fr->sc_r_power = ir->fepvals->sc_r_power;
1979 fr->sc_sigma6_def = gmx::power6(ir->fepvals->sc_sigma);
1981 env = getenv("GMX_SCSIGMA_MIN");
1985 sscanf(env, "%20lf", &dbl);
1986 fr->sc_sigma6_min = gmx::power6(dbl);
1989 fprintf(fp, "Setting the minimum soft core sigma to %g nm\n", dbl);
1993 fr->bNonbonded = TRUE;
1994 if (getenv("GMX_NO_NONBONDED") != nullptr)
1996 /* turn off non-bonded calculations */
1997 fr->bNonbonded = FALSE;
1998 GMX_LOG(mdlog.warning).asParagraph().appendText(
1999 "Found environment variable GMX_NO_NONBONDED.\n"
2000 "Disabling nonbonded calculations.");
2003 bGenericKernelOnly = FALSE;
2005 /* We now check in the NS code whether a particular combination of interactions
2006 * can be used with water optimization, and disable it if that is not the case.
2009 if (getenv("GMX_NB_GENERIC") != nullptr)
2014 "Found environment variable GMX_NB_GENERIC.\n"
2015 "Disabling all interaction-specific nonbonded kernels, will only\n"
2016 "use the slow generic ones in src/gmxlib/nonbonded/nb_generic.c\n\n");
2018 bGenericKernelOnly = TRUE;
2021 if (bGenericKernelOnly)
2026 if ( (getenv("GMX_DISABLE_SIMD_KERNELS") != nullptr) || (getenv("GMX_NOOPTIMIZEDKERNELS") != nullptr) )
2028 fr->use_simd_kernels = FALSE;
2032 "\nFound environment variable GMX_DISABLE_SIMD_KERNELS.\n"
2033 "Disabling the usage of any SIMD-specific non-bonded & bonded kernel routines\n"
2034 "(e.g. SSE2/SSE4.1/AVX).\n\n");
2038 fr->bBHAM = (mtop->ffparams.functype[0] == F_BHAM);
2040 /* Neighbour searching stuff */
2041 fr->cutoff_scheme = ir->cutoff_scheme;
2042 fr->bGrid = (ir->ns_type == ensGRID);
2043 fr->ePBC = ir->ePBC;
2045 if (fr->cutoff_scheme == ecutsGROUP)
2047 const char *note = "NOTE: This file uses the deprecated 'group' cutoff_scheme. This will be\n"
2048 "removed in a future release when 'verlet' supports all interaction forms.\n";
2052 fprintf(stderr, "\n%s\n", note);
2056 fprintf(fp, "\n%s\n", note);
2060 /* Determine if we will do PBC for distances in bonded interactions */
2061 if (fr->ePBC == epbcNONE)
2063 fr->bMolPBC = FALSE;
2067 if (!DOMAINDECOMP(cr))
2071 bSHAKE = (ir->eConstrAlg == econtSHAKE &&
2072 (gmx_mtop_ftype_count(mtop, F_CONSTR) > 0 ||
2073 gmx_mtop_ftype_count(mtop, F_CONSTRNC) > 0));
2075 /* The group cut-off scheme and SHAKE assume charge groups
2076 * are whole, but not using molpbc is faster in most cases.
2077 * With intermolecular interactions we need PBC for calculating
2078 * distances between atoms in different molecules.
2080 if ((fr->cutoff_scheme == ecutsGROUP || bSHAKE) &&
2081 !mtop->bIntermolecularInteractions)
2083 fr->bMolPBC = ir->bPeriodicMols;
2085 if (bSHAKE && fr->bMolPBC)
2087 gmx_fatal(FARGS, "SHAKE is not supported with periodic molecules");
2092 /* Not making molecules whole is faster in most cases,
2093 * but With orientation restraints we need whole molecules.
2095 fr->bMolPBC = (fcd->orires.nr == 0);
2097 if (getenv("GMX_USE_GRAPH") != nullptr)
2099 fr->bMolPBC = FALSE;
2102 GMX_LOG(mdlog.warning).asParagraph().appendText("GMX_USE_GRAPH is set, using the graph for bonded interactions");
2105 if (mtop->bIntermolecularInteractions)
2107 GMX_LOG(mdlog.warning).asParagraph().appendText("WARNING: Molecules linked by intermolecular interactions have to reside in the same periodic image, otherwise artifacts will occur!");
2111 GMX_RELEASE_ASSERT(fr->bMolPBC || !mtop->bIntermolecularInteractions, "We need to use PBC within molecules with inter-molecular interactions");
2113 if (bSHAKE && fr->bMolPBC)
2115 gmx_fatal(FARGS, "SHAKE is not properly supported with intermolecular interactions. For short simulations where linked molecules remain in the same periodic image, the environment variable GMX_USE_GRAPH can be used to override this check.\n");
2121 fr->bMolPBC = dd_bonded_molpbc(cr->dd, fr->ePBC);
2125 fr->rc_scaling = ir->refcoord_scaling;
2126 copy_rvec(ir->posres_com, fr->posres_com);
2127 copy_rvec(ir->posres_comB, fr->posres_comB);
2128 fr->rlist = cutoff_inf(ir->rlist);
2129 fr->ljpme_combination_rule = ir->ljpme_combination_rule;
2131 /* This now calculates sum for q and c6*/
2132 bool systemHasNetCharge = set_chargesum(fp, fr, mtop);
2134 /* fr->ic is used both by verlet and group kernels (to some extent) now */
2135 init_interaction_const(fp, &fr->ic, ir, mtop, systemHasNetCharge);
2136 init_interaction_const_tables(fp, fr->ic, ir->rlist + ir->tabext);
2138 const interaction_const_t *ic = fr->ic;
2140 /* TODO: Replace this Ewald table or move it into interaction_const_t */
2141 if (ir->coulombtype == eelEWALD)
2143 init_ewald_tab(&(fr->ewald_table), ir, fp);
2146 /* Electrostatics: Translate from interaction-setting-in-mdp-file to kernel interaction format */
2147 switch (ic->eeltype)
2150 fr->nbkernel_elec_interaction = GMX_NBKERNEL_ELEC_COULOMB;
2155 fr->nbkernel_elec_interaction = GMX_NBKERNEL_ELEC_REACTIONFIELD;
2159 fr->nbkernel_elec_interaction = GMX_NBKERNEL_ELEC_REACTIONFIELD;
2160 GMX_RELEASE_ASSERT(ic->coulomb_modifier == eintmodEXACTCUTOFF, "With the group scheme RF-zero needs the exact cut-off modifier");
2169 case eelPMEUSERSWITCH:
2170 fr->nbkernel_elec_interaction = GMX_NBKERNEL_ELEC_CUBICSPLINETABLE;
2176 fr->nbkernel_elec_interaction = GMX_NBKERNEL_ELEC_EWALD;
2180 gmx_fatal(FARGS, "Unsupported electrostatic interaction: %s", eel_names[ic->eeltype]);
2182 fr->nbkernel_elec_modifier = ic->coulomb_modifier;
2184 /* Vdw: Translate from mdp settings to kernel format */
2185 switch (ic->vdwtype)
2190 fr->nbkernel_vdw_interaction = GMX_NBKERNEL_VDW_BUCKINGHAM;
2194 fr->nbkernel_vdw_interaction = GMX_NBKERNEL_VDW_LENNARDJONES;
2198 fr->nbkernel_vdw_interaction = GMX_NBKERNEL_VDW_LJEWALD;
2204 case evdwENCADSHIFT:
2205 fr->nbkernel_vdw_interaction = GMX_NBKERNEL_VDW_CUBICSPLINETABLE;
2209 gmx_fatal(FARGS, "Unsupported vdw interaction: %s", evdw_names[ic->vdwtype]);
2211 fr->nbkernel_vdw_modifier = ic->vdw_modifier;
2213 if (ir->cutoff_scheme == ecutsGROUP)
2215 fr->bvdwtab = ((ic->vdwtype != evdwCUT || !gmx_within_tol(ic->reppow, 12.0, 10*GMX_DOUBLE_EPS))
2216 && !EVDW_PME(ic->vdwtype));
2217 /* We have special kernels for standard Ewald and PME, but the pme-switch ones are tabulated above */
2218 fr->bcoultab = !(ic->eeltype == eelCUT ||
2219 ic->eeltype == eelEWALD ||
2220 ic->eeltype == eelPME ||
2221 ic->eeltype == eelP3M_AD ||
2222 ic->eeltype == eelRF ||
2223 ic->eeltype == eelRF_ZERO);
2225 /* If the user absolutely wants different switch/shift settings for coul/vdw, it is likely
2226 * going to be faster to tabulate the interaction than calling the generic kernel.
2227 * However, if generic kernels have been requested we keep things analytically.
2229 if (fr->nbkernel_elec_modifier == eintmodPOTSWITCH &&
2230 fr->nbkernel_vdw_modifier == eintmodPOTSWITCH &&
2231 !bGenericKernelOnly)
2233 if ((ic->rcoulomb_switch != ic->rvdw_switch) || (ic->rcoulomb != ic->rvdw))
2235 fr->bcoultab = TRUE;
2236 /* Once we tabulate electrostatics, we can use the switch function for LJ,
2237 * which would otherwise need two tables.
2241 else if ((fr->nbkernel_elec_modifier == eintmodPOTSHIFT && fr->nbkernel_vdw_modifier == eintmodPOTSHIFT) ||
2242 ((fr->nbkernel_elec_interaction == GMX_NBKERNEL_ELEC_REACTIONFIELD &&
2243 fr->nbkernel_elec_modifier == eintmodEXACTCUTOFF &&
2244 (fr->nbkernel_vdw_modifier == eintmodPOTSWITCH || fr->nbkernel_vdw_modifier == eintmodPOTSHIFT))))
2246 if ((ic->rcoulomb != ic->rvdw) && (!bGenericKernelOnly))
2248 fr->bcoultab = TRUE;
2252 if (fr->nbkernel_elec_modifier == eintmodFORCESWITCH)
2254 fr->bcoultab = TRUE;
2256 if (fr->nbkernel_vdw_modifier == eintmodFORCESWITCH)
2261 if (getenv("GMX_REQUIRE_TABLES"))
2264 fr->bcoultab = TRUE;
2269 fprintf(fp, "Table routines are used for coulomb: %s\n",
2270 gmx::boolToString(fr->bcoultab));
2271 fprintf(fp, "Table routines are used for vdw: %s\n",
2272 gmx::boolToString(fr->bvdwtab));
2277 fr->nbkernel_vdw_interaction = GMX_NBKERNEL_VDW_CUBICSPLINETABLE;
2278 fr->nbkernel_vdw_modifier = eintmodNONE;
2282 fr->nbkernel_elec_interaction = GMX_NBKERNEL_ELEC_CUBICSPLINETABLE;
2283 fr->nbkernel_elec_modifier = eintmodNONE;
2287 if (ir->cutoff_scheme == ecutsVERLET)
2289 if (!gmx_within_tol(ic->reppow, 12.0, 10*GMX_DOUBLE_EPS))
2291 gmx_fatal(FARGS, "Cut-off scheme %s only supports LJ repulsion power 12", ecutscheme_names[ir->cutoff_scheme]);
2293 /* Older tpr files can contain Coulomb user tables with the Verlet cutoff-scheme,
2294 * while mdrun does not (and never did) support this.
2296 if (EEL_USER(fr->ic->eeltype))
2298 gmx_fatal(FARGS, "Combination of %s and cutoff scheme %s is not supported",
2299 eel_names[ir->coulombtype], ecutscheme_names[ir->cutoff_scheme]);
2302 fr->bvdwtab = FALSE;
2303 fr->bcoultab = FALSE;
2306 /* 1-4 interaction electrostatics */
2307 fr->fudgeQQ = mtop->ffparams.fudgeQQ;
2309 /* Parameters for generalized RF */
2313 if (ic->eeltype == eelGRF)
2315 init_generalized_rf(fp, mtop, ir, fr);
2318 fr->haveDirectVirialContributions =
2319 (EEL_FULL(ic->eeltype) || EVDW_PME(ic->vdwtype) ||
2320 fr->forceProviders->hasForceProvider() ||
2321 gmx_mtop_ftype_count(mtop, F_POSRES) > 0 ||
2322 gmx_mtop_ftype_count(mtop, F_FBPOSRES) > 0 ||
2328 if (fr->haveDirectVirialContributions)
2330 fr->forceBufferForDirectVirialContributions = new std::vector<gmx::RVec>;
2333 if (fr->cutoff_scheme == ecutsGROUP &&
2334 ncg_mtop(mtop) > fr->cg_nalloc && !DOMAINDECOMP(cr))
2336 /* Count the total number of charge groups */
2337 fr->cg_nalloc = ncg_mtop(mtop);
2338 srenew(fr->cg_cm, fr->cg_nalloc);
2340 if (fr->shift_vec == nullptr)
2342 snew(fr->shift_vec, SHIFTS);
2345 if (fr->fshift == nullptr)
2347 snew(fr->fshift, SHIFTS);
2350 if (fr->nbfp == nullptr)
2352 fr->ntype = mtop->ffparams.atnr;
2353 fr->nbfp = mk_nbfp(&mtop->ffparams, fr->bBHAM);
2354 if (EVDW_PME(ic->vdwtype))
2356 fr->ljpme_c6grid = make_ljpme_c6grid(&mtop->ffparams, fr);
2360 /* Copy the energy group exclusions */
2361 fr->egp_flags = ir->opts.egp_flags;
2363 /* Van der Waals stuff */
2364 if ((ic->vdwtype != evdwCUT) && (ic->vdwtype != evdwUSER) && !fr->bBHAM)
2366 if (ic->rvdw_switch >= ic->rvdw)
2368 gmx_fatal(FARGS, "rvdw_switch (%f) must be < rvdw (%f)",
2369 ic->rvdw_switch, ic->rvdw);
2373 fprintf(fp, "Using %s Lennard-Jones, switch between %g and %g nm\n",
2374 (ic->eeltype == eelSWITCH) ? "switched" : "shifted",
2375 ic->rvdw_switch, ic->rvdw);
2379 if (fr->bBHAM && EVDW_PME(ic->vdwtype))
2381 gmx_fatal(FARGS, "LJ PME not supported with Buckingham");
2384 if (fr->bBHAM && (ic->vdwtype == evdwSHIFT || ic->vdwtype == evdwSWITCH))
2386 gmx_fatal(FARGS, "Switch/shift interaction not supported with Buckingham");
2389 if (fr->bBHAM && fr->cutoff_scheme == ecutsVERLET)
2391 gmx_fatal(FARGS, "Verlet cutoff-scheme is not supported with Buckingham");
2394 if (fp && fr->cutoff_scheme == ecutsGROUP)
2396 fprintf(fp, "Cut-off's: NS: %g Coulomb: %g %s: %g\n",
2397 fr->rlist, ic->rcoulomb, fr->bBHAM ? "BHAM" : "LJ", ic->rvdw);
2400 fr->eDispCorr = ir->eDispCorr;
2401 fr->numAtomsForDispersionCorrection = mtop->natoms;
2402 if (ir->eDispCorr != edispcNO)
2404 set_avcsixtwelve(fp, fr, mtop);
2407 if (ir->implicit_solvent)
2409 gmx_fatal(FARGS, "Implict solvation is no longer supported.");
2412 /* Construct tables for the group scheme. A little unnecessary to
2413 * make both vdw and coul tables sometimes, but what the
2414 * heck. Note that both cutoff schemes construct Ewald tables in
2415 * init_interaction_const_tables. */
2416 needGroupSchemeTables = (ir->cutoff_scheme == ecutsGROUP &&
2417 (fr->bcoultab || fr->bvdwtab));
2419 negp_pp = ir->opts.ngener - ir->nwall;
2421 if (!needGroupSchemeTables)
2423 bSomeNormalNbListsAreInUse = TRUE;
2428 bSomeNormalNbListsAreInUse = FALSE;
2429 for (egi = 0; egi < negp_pp; egi++)
2431 for (egj = egi; egj < negp_pp; egj++)
2433 egp_flags = ir->opts.egp_flags[GID(egi, egj, ir->opts.ngener)];
2434 if (!(egp_flags & EGP_EXCL))
2436 if (egp_flags & EGP_TABLE)
2442 bSomeNormalNbListsAreInUse = TRUE;
2447 if (bSomeNormalNbListsAreInUse)
2449 fr->nnblists = negptable + 1;
2453 fr->nnblists = negptable;
2455 if (fr->nnblists > 1)
2457 snew(fr->gid2nblists, ir->opts.ngener*ir->opts.ngener);
2461 snew(fr->nblists, fr->nnblists);
2463 /* This code automatically gives table length tabext without cut-off's,
2464 * in that case grompp should already have checked that we do not need
2465 * normal tables and we only generate tables for 1-4 interactions.
2467 rtab = ir->rlist + ir->tabext;
2469 if (needGroupSchemeTables)
2471 /* make tables for ordinary interactions */
2472 if (bSomeNormalNbListsAreInUse)
2474 make_nbf_tables(fp, ic, rtab, tabfn, nullptr, nullptr, &fr->nblists[0]);
2483 /* Read the special tables for certain energy group pairs */
2484 nm_ind = mtop->groups.grps[egcENER].nm_ind;
2485 for (egi = 0; egi < negp_pp; egi++)
2487 for (egj = egi; egj < negp_pp; egj++)
2489 egp_flags = ir->opts.egp_flags[GID(egi, egj, ir->opts.ngener)];
2490 if ((egp_flags & EGP_TABLE) && !(egp_flags & EGP_EXCL))
2492 if (fr->nnblists > 1)
2494 fr->gid2nblists[GID(egi, egj, ir->opts.ngener)] = m;
2496 /* Read the table file with the two energy groups names appended */
2497 make_nbf_tables(fp, ic, rtab, tabfn,
2498 *mtop->groups.grpname[nm_ind[egi]],
2499 *mtop->groups.grpname[nm_ind[egj]],
2503 else if (fr->nnblists > 1)
2505 fr->gid2nblists[GID(egi, egj, ir->opts.ngener)] = 0;
2512 /* Tables might not be used for the potential modifier
2513 * interactions per se, but we still need them to evaluate
2514 * switch/shift dispersion corrections in this case. */
2515 if (fr->eDispCorr != edispcNO)
2517 fr->dispersionCorrectionTable = makeDispersionCorrectionTable(fp, ic, rtab, tabfn);
2520 /* We want to use unmodified tables for 1-4 coulombic
2521 * interactions, so we must in general have an extra set of
2523 if (gmx_mtop_ftype_count(mtop, F_LJ14) > 0 ||
2524 gmx_mtop_ftype_count(mtop, F_LJC14_Q) > 0 ||
2525 gmx_mtop_ftype_count(mtop, F_LJC_PAIRS_NB) > 0)
2527 fr->pairsTable = make_tables(fp, ic, tabpfn, rtab,
2528 GMX_MAKETABLES_14ONLY);
2532 fr->nwall = ir->nwall;
2533 if (ir->nwall && ir->wall_type == ewtTABLE)
2535 make_wall_tables(fp, ir, tabfn, &mtop->groups, fr);
2538 if (fcd && !tabbfnm.empty())
2540 // Need to catch std::bad_alloc
2541 // TODO Don't need to catch this here, when merging with master branch
2544 fcd->bondtab = make_bonded_tables(fp,
2545 F_TABBONDS, F_TABBONDSNC,
2546 mtop, tabbfnm, "b");
2547 fcd->angletab = make_bonded_tables(fp,
2549 mtop, tabbfnm, "a");
2550 fcd->dihtab = make_bonded_tables(fp,
2552 mtop, tabbfnm, "d");
2554 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
2560 fprintf(debug, "No fcdata or table file name passed, can not read table, can not do bonded interactions\n");
2564 // QM/MM initialization if requested
2565 fr->bQMMM = ir->bQMMM;
2568 // Initialize QM/MM if supported
2571 GMX_LOG(mdlog.info).asParagraph().
2572 appendText("Large parts of the QM/MM support is deprecated, and may be removed in a future "
2573 "version. Please get in touch with the developers if you find the support useful, "
2574 "as help is needed if the functionality is to continue to be available.");
2575 fr->qr = mk_QMMMrec();
2576 init_QMMMrec(cr, mtop, ir, fr);
2580 gmx_incons("QM/MM was requested, but is only available when GROMACS "
2581 "is configured with QM/MM support");
2585 /* Set all the static charge group info */
2586 fr->cginfo_mb = init_cginfo_mb(fp, mtop, fr, bNoSolvOpt,
2588 &fr->bExcl_IntraCGAll_InterCGNone);
2589 if (DOMAINDECOMP(cr))
2591 fr->cginfo = nullptr;
2595 fr->cginfo = cginfo_expand(mtop->molblock.size(), fr->cginfo_mb);
2598 if (!DOMAINDECOMP(cr))
2600 forcerec_set_ranges(fr, ncg_mtop(mtop), ncg_mtop(mtop),
2601 mtop->natoms, mtop->natoms, mtop->natoms);
2604 fr->print_force = print_force;
2607 /* coarse load balancing vars */
2612 /* Initialize neighbor search */
2614 init_ns(fp, cr, fr->ns, fr, mtop);
2616 /* Initialize the thread working data for bonded interactions */
2617 init_bonded_threading(fp, mtop->groups.grps[egcENER].nr,
2618 &fr->bondedThreading);
2620 fr->nthread_ewc = gmx_omp_nthreads_get(emntBonded);
2621 snew(fr->ewc_t, fr->nthread_ewc);
2623 if (fr->cutoff_scheme == ecutsVERLET)
2625 // We checked the cut-offs in grompp, but double-check here.
2626 // We have PME+LJcutoff kernels for rcoulomb>rvdw.
2627 if (EEL_PME_EWALD(ir->coulombtype) && ir->vdwtype == eelCUT)
2629 GMX_RELEASE_ASSERT(ir->rcoulomb >= ir->rvdw, "With Verlet lists and PME we should have rcoulomb>=rvdw");
2633 GMX_RELEASE_ASSERT(ir->rcoulomb == ir->rvdw, "With Verlet lists and no PME rcoulomb and rvdw should be identical");
2636 Nbnxm::init_nb_verlet(mdlog, &fr->nbv, bFEP_NonBonded, ir, fr,
2637 cr, hardwareInfo, deviceInfo,
2640 if (useGpuForBonded)
2642 auto stream = DOMAINDECOMP(cr) ?
2643 Nbnxm::gpu_get_command_stream(fr->nbv->gpu_nbv, Nbnxm::InteractionLocality::NonLocal) :
2644 Nbnxm::gpu_get_command_stream(fr->nbv->gpu_nbv, Nbnxm::InteractionLocality::Local);
2645 // TODO the heap allocation is only needed while
2646 // t_forcerec lacks a constructor.
2647 fr->gpuBonded = new gmx::GpuBonded(mtop->ffparams,
2654 /* Here we switch from using mdlog, which prints the newline before
2655 * the paragraph, to our old fprintf logging, which prints the newline
2656 * after the paragraph, so we should add a newline here.
2661 if (ir->eDispCorr != edispcNO)
2663 calc_enervirdiff(fp, ir->eDispCorr, fr);
2667 /* Frees GPU memory and sets a tMPI node barrier.
2669 * Note that this function needs to be called even if GPUs are not used
2670 * in this run because the PME ranks have no knowledge of whether GPUs
2671 * are used or not, but all ranks need to enter the barrier below.
2672 * \todo Remove physical node barrier from this function after making sure
2673 * that it's not needed anymore (with a shared GPU run).
2675 void free_gpu_resources(t_forcerec *fr,
2676 const gmx::PhysicalNodeCommunicator &physicalNodeCommunicator)
2678 bool isPPrankUsingGPU = (fr != nullptr) && (fr->nbv != nullptr) && fr->nbv->useGpu();
2680 /* stop the GPU profiler (only CUDA) */
2683 if (isPPrankUsingGPU)
2685 /* free nbnxn data in GPU memory */
2686 Nbnxm::gpu_free(fr->nbv->gpu_nbv);
2687 delete fr->gpuBonded;
2688 fr->gpuBonded = nullptr;
2691 /* With tMPI we need to wait for all ranks to finish deallocation before
2692 * destroying the CUDA context in free_gpu() as some tMPI ranks may be sharing
2695 * This is not a concern in OpenCL where we use one context per rank which
2696 * is freed in nbnxn_gpu_free().
2698 * Note: it is safe to not call the barrier on the ranks which do not use GPU,
2699 * but it is easier and more futureproof to call it on the whole node.
2703 physicalNodeCommunicator.barrier();
2707 void done_forcerec(t_forcerec *fr, int numMolBlocks, int numEnergyGroups)
2711 // PME-only ranks don't have a forcerec
2714 // cginfo is dynamically allocated if no domain decomposition
2715 if (fr->cginfo != nullptr)
2719 done_cginfo_mb(fr->cginfo_mb, numMolBlocks);
2721 done_interaction_const(fr->ic);
2722 sfree(fr->shift_vec);
2725 done_ns(fr->ns, numEnergyGroups);
2727 tear_down_bonded_threading(fr->bondedThreading);
2728 GMX_RELEASE_ASSERT(fr->gpuBonded == nullptr, "Should have been deleted earlier, when used");
2729 fr->bondedThreading = nullptr;