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, 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.
52 #include <sys/types.h>
54 #include "gromacs/fileio/gmxfio.h"
55 #include "gromacs/fileio/warninp.h"
56 #include "gromacs/gmxpreprocess/gmxcpp.h"
57 #include "gromacs/gmxpreprocess/gpp_bond_atomtype.h"
58 #include "gromacs/gmxpreprocess/gpp_nextnb.h"
59 #include "gromacs/gmxpreprocess/grompp-impl.h"
60 #include "gromacs/gmxpreprocess/readir.h"
61 #include "gromacs/gmxpreprocess/topdirs.h"
62 #include "gromacs/gmxpreprocess/toppush.h"
63 #include "gromacs/gmxpreprocess/topshake.h"
64 #include "gromacs/gmxpreprocess/toputil.h"
65 #include "gromacs/gmxpreprocess/vsite_parm.h"
66 #include "gromacs/math/units.h"
67 #include "gromacs/math/utilities.h"
68 #include "gromacs/mdtypes/inputrec.h"
69 #include "gromacs/mdtypes/md_enums.h"
70 #include "gromacs/pbcutil/pbc.h"
71 #include "gromacs/topology/block.h"
72 #include "gromacs/topology/ifunc.h"
73 #include "gromacs/topology/symtab.h"
74 #include "gromacs/topology/topology.h"
75 #include "gromacs/utility/cstringutil.h"
76 #include "gromacs/utility/fatalerror.h"
77 #include "gromacs/utility/futil.h"
78 #include "gromacs/utility/gmxassert.h"
79 #include "gromacs/utility/pleasecite.h"
80 #include "gromacs/utility/smalloc.h"
82 #define OPENDIR '[' /* starting sign for directive */
83 #define CLOSEDIR ']' /* ending sign for directive */
85 static void free_nbparam(t_nbparam **param, int nr)
90 for (i = 0; i < nr; i++)
98 static int copy_nbparams(t_nbparam **param, int ftype, t_params *plist, int nr)
106 for (i = 0; i < nr; i++)
108 for (j = 0; j <= i; j++)
111 if (param[i][j].bSet)
113 for (f = 0; f < nrfp; f++)
115 plist->param[nr*i+j].c[f] = param[i][j].c[f];
116 plist->param[nr*j+i].c[f] = param[i][j].c[f];
126 static void gen_pairs(t_params *nbs, t_params *pairs, real fudge, int comb)
128 int i, j, ntp, nrfp, nrfpA, nrfpB, nnn;
131 nnn = static_cast<int>(std::sqrt(static_cast<double>(ntp)));
132 GMX_ASSERT(nnn * nnn == ntp, "Number of pairs of generated non-bonded parameters should be a perfect square");
134 nrfpA = interaction_function[F_LJ14].nrfpA;
135 nrfpB = interaction_function[F_LJ14].nrfpB;
138 if ((nrfp != nrfpA) || (nrfpA != nrfpB))
140 gmx_incons("Number of force parameters in gen_pairs wrong");
143 fprintf(stderr, "Generating 1-4 interactions: fudge = %g\n", fudge);
144 snew(pairs->param, pairs->nr);
145 for (i = 0; (i < ntp); i++)
148 pairs->param[i].a[0] = i / nnn;
149 pairs->param[i].a[1] = i % nnn;
150 /* Copy normal and FEP parameters and multiply by fudge factor */
154 for (j = 0; (j < nrfp); j++)
156 /* If we are using sigma/epsilon values, only the epsilon values
157 * should be scaled, but not sigma.
158 * The sigma values have even indices 0,2, etc.
160 if ((comb == eCOMB_ARITHMETIC || comb == eCOMB_GEOM_SIG_EPS) && (j%2 == 0))
169 pairs->param[i].c[j] = scaling*nbs->param[i].c[j];
170 /* NOTE: this should be cleat to the compiler, but some gcc 5.2 versions
171 * issue false positive warnings for the pairs->param.c[] indexing below.
173 assert(2*nrfp <= MAXFORCEPARAM);
174 pairs->param[i].c[nrfp+j] = scaling*nbs->param[i].c[j];
179 double check_mol(const gmx_mtop_t *mtop, warninp_t wi)
186 /* Check mass and charge */
189 for (const gmx_molblock_t &molb : mtop->molblock)
191 const t_atoms *atoms = &mtop->moltype[molb.type].atoms;
192 for (i = 0; (i < atoms->nr); i++)
194 q += molb.nmol*atoms->atom[i].q;
195 m = atoms->atom[i].m;
196 mB = atoms->atom[i].mB;
197 pt = atoms->atom[i].ptype;
198 /* If the particle is an atom or a nucleus it must have a mass,
199 * else, if it is a shell, a vsite or a bondshell it can have mass zero
201 if (((m <= 0.0) || (mB <= 0.0)) && ((pt == eptAtom) || (pt == eptNucleus)))
203 ri = atoms->atom[i].resind;
204 sprintf(buf, "atom %s (Res %s-%d) has mass %g (state A) / %g (state B)\n",
205 *(atoms->atomname[i]),
206 *(atoms->resinfo[ri].name),
207 atoms->resinfo[ri].nr,
209 warning_error(wi, buf);
212 if (((m != 0) || (mB != 0)) && (pt == eptVSite))
214 ri = atoms->atom[i].resind;
215 sprintf(buf, "virtual site %s (Res %s-%d) has non-zero mass %g (state A) / %g (state B)\n"
216 " Check your topology.\n",
217 *(atoms->atomname[i]),
218 *(atoms->resinfo[ri].name),
219 atoms->resinfo[ri].nr,
221 warning_error(wi, buf);
222 /* The following statements make LINCS break! */
223 /* atoms->atom[i].m=0; */
230 /*! \brief Returns the rounded charge of a molecule, when close to integer, otherwise returns the original charge.
232 * The results of this routine are only used for checking and for
233 * printing warning messages. Thus we can assume that charges of molecules
234 * should be integer. If the user wanted non-integer molecular charge,
235 * an undesired warning is printed and the user should use grompp -maxwarn 1.
237 * \param qMol The total, unrounded, charge of the molecule
238 * \param sumAbsQ The sum of absolute values of the charges, used for determining the tolerance for the rounding.
240 static double roundedMoleculeCharge(double qMol, double sumAbsQ)
242 /* We use a tolerance of 1e-6 for inaccuracies beyond the 6th decimal
243 * of the charges for ascii float truncation in the topology files.
244 * Although the summation here uses double precision, the charges
245 * are read and stored in single precision when real=float. This can
246 * lead to rounding errors of half the least significant bit.
247 * Note that, unfortunately, we can not assume addition of random
248 * rounding errors. It is not entirely unlikely that many charges
249 * have a near half-bit rounding error with the same sign.
251 double tolAbs = 1e-6;
252 double tol = std::max(tolAbs, 0.5*GMX_REAL_EPS*sumAbsQ);
253 double qRound = std::round(qMol);
254 if (std::abs(qMol - qRound) <= tol)
264 static void sum_q(const t_atoms *atoms, int numMols,
265 double *qTotA, double *qTotB)
272 for (int i = 0; i < atoms->nr; i++)
274 qmolA += atoms->atom[i].q;
275 qmolB += atoms->atom[i].qB;
276 sumAbsQA += std::abs(atoms->atom[i].q);
277 sumAbsQB += std::abs(atoms->atom[i].qB);
280 *qTotA += numMols*roundedMoleculeCharge(qmolA, sumAbsQA);
281 *qTotB += numMols*roundedMoleculeCharge(qmolB, sumAbsQB);
284 static void get_nbparm(char *nb_str, char *comb_str, int *nb, int *comb,
288 char warn_buf[STRLEN];
291 for (i = 1; (i < eNBF_NR); i++)
293 if (gmx_strcasecmp(nb_str, enbf_names[i]) == 0)
300 *nb = strtol(nb_str, nullptr, 10);
302 if ((*nb < 1) || (*nb >= eNBF_NR))
304 sprintf(warn_buf, "Invalid nonbond function selector '%s' using %s",
305 nb_str, enbf_names[1]);
306 warning_error(wi, warn_buf);
310 for (i = 1; (i < eCOMB_NR); i++)
312 if (gmx_strcasecmp(comb_str, ecomb_names[i]) == 0)
319 *comb = strtol(comb_str, nullptr, 10);
321 if ((*comb < 1) || (*comb >= eCOMB_NR))
323 sprintf(warn_buf, "Invalid combination rule selector '%s' using %s",
324 comb_str, ecomb_names[1]);
325 warning_error(wi, warn_buf);
330 static char ** cpp_opts(const char *define, const char *include,
335 const char *cppadds[2];
336 char **cppopts = nullptr;
337 const char *option[2] = { "-D", "-I" };
338 const char *nopt[2] = { "define", "include" };
342 char warn_buf[STRLEN];
345 cppadds[1] = include;
346 for (n = 0; (n < 2); n++)
353 while ((*ptr != '\0') && isspace(*ptr))
358 while ((*rptr != '\0') && !isspace(*rptr))
366 strncpy(buf, ptr, len);
367 if (strstr(ptr, option[n]) != ptr)
369 set_warning_line(wi, "mdp file", -1);
370 sprintf(warn_buf, "Malformed %s option %s", nopt[n], buf);
371 warning(wi, warn_buf);
375 srenew(cppopts, ++ncppopts);
376 cppopts[ncppopts-1] = gmx_strdup(buf);
384 srenew(cppopts, ++ncppopts);
385 cppopts[ncppopts-1] = nullptr;
391 static void make_atoms_sys(const std::vector<gmx_molblock_t> &molblock,
392 const t_molinfo *molinfo,
396 atoms->atom = nullptr;
398 for (const gmx_molblock_t &molb : molblock)
400 const t_atoms &mol_atoms = molinfo[molb.type].atoms;
402 srenew(atoms->atom, atoms->nr + molb.nmol*mol_atoms.nr);
404 for (int m = 0; m < molb.nmol; m++)
406 for (int a = 0; a < mol_atoms.nr; a++)
408 atoms->atom[atoms->nr++] = mol_atoms.atom[a];
415 static char **read_topol(const char *infile, const char *outfile,
416 const char *define, const char *include,
418 gpp_atomtype_t atype,
421 t_molinfo **intermolecular_interactions,
423 int *combination_rule,
427 std::vector<gmx_molblock_t> *molblock,
430 bool usingFullRangeElectrostatics,
435 char *pline = nullptr, **title = nullptr;
436 char line[STRLEN], errbuf[256], comb_str[256], nb_str[256];
438 char *dirstr, *dummy2;
439 int nrcopies, nmol, nscan, ncombs, ncopy;
440 double fLJ, fQQ, fPOW;
441 t_molinfo *mi0 = nullptr;
444 t_nbparam **nbparam, **pair;
446 real fudgeLJ = -1; /* Multiplication factor to generate 1-4 from LJ */
447 bool bReadDefaults, bReadMolType, bGenPairs, bWarn_copy_A_B;
448 double qt = 0, qBt = 0; /* total charge */
449 t_bond_atomtype batype;
451 int dcatt = -1, nmol_couple;
452 /* File handling variables */
455 char *tmp_line = nullptr;
456 char warn_buf[STRLEN];
457 const char *floating_point_arithmetic_tip =
458 "Total charge should normally be an integer. See\n"
459 "http://www.gromacs.org/Documentation/Floating_Point_Arithmetic\n"
460 "for discussion on how close it should be to an integer.\n";
461 /* We need to open the output file before opening the input file,
462 * because cpp_open_file can change the current working directory.
466 out = gmx_fio_fopen(outfile, "w");
473 /* open input file */
474 status = cpp_open_file(infile, &handle, cpp_opts(define, include, wi));
477 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
480 /* some local variables */
481 DS_Init(&DS); /* directive stack */
482 nmol = 0; /* no molecules yet... */
483 d = d_invalid; /* first thing should be a directive */
484 nbparam = nullptr; /* The temporary non-bonded matrix */
485 pair = nullptr; /* The temporary pair interaction matrix */
486 block2 = nullptr; /* the extra exclusions */
489 *reppow = 12.0; /* Default value for repulsion power */
491 *intermolecular_interactions = nullptr;
493 /* Init the number of CMAP torsion angles and grid spacing */
494 plist[F_CMAP].grid_spacing = 0;
495 plist[F_CMAP].nc = 0;
497 bWarn_copy_A_B = bFEP;
499 batype = init_bond_atomtype();
500 /* parse the actual file */
501 bReadDefaults = FALSE;
503 bReadMolType = FALSE;
508 status = cpp_read_line(&handle, STRLEN, line);
509 done = (status == eCPP_EOF);
512 if (status != eCPP_OK)
514 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
518 fprintf(out, "%s\n", line);
521 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
523 pline = gmx_strdup(line);
525 /* Strip trailing '\' from pline, if it exists */
527 if ((sl > 0) && (pline[sl-1] == CONTINUE))
532 /* build one long line from several fragments - necessary for CMAP */
533 while (continuing(line))
535 status = cpp_read_line(&handle, STRLEN, line);
536 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
538 /* Since we depend on the '\' being present to continue to read, we copy line
539 * to a tmp string, strip the '\' from that string, and cat it to pline
541 tmp_line = gmx_strdup(line);
543 sl = strlen(tmp_line);
544 if ((sl > 0) && (tmp_line[sl-1] == CONTINUE))
546 tmp_line[sl-1] = ' ';
549 done = (status == eCPP_EOF);
552 if (status != eCPP_OK)
554 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
558 fprintf(out, "%s\n", line);
562 srenew(pline, strlen(pline)+strlen(tmp_line)+1);
563 strcat(pline, tmp_line);
567 /* skip trailing and leading spaces and comment text */
568 strip_comment (pline);
571 /* if there is something left... */
572 if ((int)strlen(pline) > 0)
574 if (pline[0] == OPENDIR)
576 /* A directive on this line: copy the directive
577 * without the brackets into dirstr, then
578 * skip spaces and tabs on either side of directive
580 dirstr = gmx_strdup((pline+1));
581 if ((dummy2 = strchr (dirstr, CLOSEDIR)) != nullptr)
587 if ((newd = str2dir(dirstr)) == d_invalid)
589 sprintf(errbuf, "Invalid directive %s", dirstr);
590 warning_error(wi, errbuf);
594 /* Directive found */
595 if (DS_Check_Order (DS, newd))
602 /* we should print here which directives should have
603 been present, and which actually are */
604 gmx_fatal(FARGS, "%s\nInvalid order for directive %s",
605 cpp_error(&handle, eCPP_SYNTAX), dir2str(newd));
609 if (d == d_intermolecular_interactions)
611 if (*intermolecular_interactions == nullptr)
613 /* We (mis)use the moleculetype processing
614 * to process the intermolecular interactions
615 * by making a "molecule" of the size of the system.
617 snew(*intermolecular_interactions, 1);
618 init_molinfo(*intermolecular_interactions);
619 mi0 = *intermolecular_interactions;
620 make_atoms_sys(*molblock, *molinfo,
627 else if (d != d_invalid)
629 /* Not a directive, just a plain string
630 * use a gigantic switch to decode,
631 * if there is a valid directive!
638 gmx_fatal(FARGS, "%s\nFound a second defaults directive.\n",
639 cpp_error(&handle, eCPP_SYNTAX));
641 bReadDefaults = TRUE;
642 nscan = sscanf(pline, "%s%s%s%lf%lf%lf",
643 nb_str, comb_str, genpairs, &fLJ, &fQQ, &fPOW);
654 get_nbparm(nb_str, comb_str, &nb_funct, combination_rule, wi);
657 bGenPairs = (gmx_strncasecmp(genpairs, "Y", 1) == 0);
658 if (nb_funct != eNBF_LJ && bGenPairs)
660 gmx_fatal(FARGS, "Generating pair parameters is only supported with LJ non-bonded interactions");
676 nb_funct = ifunc_index(d_nonbond_params, nb_funct);
680 push_at(symtab, atype, batype, pline, nb_funct,
681 &nbparam, bGenPairs ? &pair : nullptr, wi);
685 push_bt(d, plist, 2, nullptr, batype, pline, wi);
687 case d_constrainttypes:
688 push_bt(d, plist, 2, nullptr, batype, pline, wi);
693 push_nbt(d, pair, atype, pline, F_LJ14, wi);
697 push_bt(d, plist, 2, atype, nullptr, pline, wi);
701 push_bt(d, plist, 3, nullptr, batype, pline, wi);
703 case d_dihedraltypes:
704 /* Special routine that can read both 2 and 4 atom dihedral definitions. */
705 push_dihedraltype(d, plist, batype, pline, wi);
708 case d_nonbond_params:
709 push_nbt(d, nbparam, atype, pline, nb_funct, wi);
714 srenew(block,nblock);
715 srenew(blockinfo,nblock);
716 blk0=&(block[nblock-1]);
717 bi0=&(blockinfo[nblock-1]);
720 push_molt(symtab,bi0,pline);
724 case d_implicit_genborn_params:
725 // Skip this line, so old topologies with
726 // GB parameters can be read.
729 case d_implicit_surface_params:
730 // Skip this line, so that any topologies
731 // with surface parameters can be read
732 // (even though these were never formally
737 push_cmaptype(d, plist, 5, atype, batype, pline, wi);
745 if (opts->couple_moltype != nullptr &&
746 (opts->couple_lam0 == ecouplamNONE ||
747 opts->couple_lam0 == ecouplamQ ||
748 opts->couple_lam1 == ecouplamNONE ||
749 opts->couple_lam1 == ecouplamQ))
751 dcatt = add_atomtype_decoupled(symtab, atype,
752 &nbparam, bGenPairs ? &pair : nullptr);
754 ntype = get_atomtype_ntypes(atype);
755 ncombs = (ntype*(ntype+1))/2;
756 generate_nbparams(*combination_rule, nb_funct, &(plist[nb_funct]), atype, wi);
757 ncopy = copy_nbparams(nbparam, nb_funct, &(plist[nb_funct]),
759 fprintf(stderr, "Generated %d of the %d non-bonded parameter combinations\n", ncombs-ncopy, ncombs);
760 free_nbparam(nbparam, ntype);
763 gen_pairs(&(plist[nb_funct]), &(plist[F_LJ14]), fudgeLJ, *combination_rule);
764 ncopy = copy_nbparams(pair, nb_funct, &(plist[F_LJ14]),
766 fprintf(stderr, "Generated %d of the %d 1-4 parameter combinations\n", ncombs-ncopy, ncombs);
767 free_nbparam(pair, ntype);
769 /* Copy GBSA parameters to atomtype array? */
774 push_molt(symtab, &nmol, molinfo, pline, wi);
775 srenew(block2, nmol);
776 block2[nmol-1].nr = 0;
777 mi0 = &((*molinfo)[nmol-1]);
778 mi0->atoms.haveMass = TRUE;
779 mi0->atoms.haveCharge = TRUE;
780 mi0->atoms.haveType = TRUE;
781 mi0->atoms.haveBState = TRUE;
782 mi0->atoms.havePdbInfo = FALSE;
786 push_atom(symtab, &(mi0->cgs), &(mi0->atoms), atype, pline, &lastcg, wi);
790 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, FALSE,
791 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
794 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, FALSE,
795 FALSE, 1.0, bZero, &bWarn_copy_A_B, wi);
805 case d_position_restraints:
806 case d_angle_restraints:
807 case d_angle_restraints_z:
808 case d_distance_restraints:
809 case d_orientation_restraints:
810 case d_dihedral_restraints:
813 case d_water_polarization:
814 case d_thole_polarization:
815 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, TRUE,
816 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
819 push_cmap(d, plist, mi0->plist, &(mi0->atoms), atype, pline, wi);
823 push_vsitesn(d, mi0->plist, &(mi0->atoms), pline, wi);
826 GMX_ASSERT(block2, "block2 must always be allocated so exclusions can be processed");
827 if (!block2[nmol-1].nr)
829 init_block2(&(block2[nmol-1]), mi0->atoms.nr);
831 push_excl(pline, &(block2[nmol-1]), wi);
835 title = put_symtab(symtab, pline);
842 push_mol(nmol, *molinfo, pline, &whichmol, &nrcopies, wi);
843 mi0 = &((*molinfo)[whichmol]);
844 molblock->resize(molblock->size() + 1);
845 molblock->back().type = whichmol;
846 molblock->back().nmol = nrcopies;
848 bCouple = (opts->couple_moltype != nullptr &&
849 (gmx_strcasecmp("system", opts->couple_moltype) == 0 ||
850 strcmp(*(mi0->name), opts->couple_moltype) == 0));
853 nmol_couple += nrcopies;
856 if (mi0->atoms.nr == 0)
858 gmx_fatal(FARGS, "Molecule type '%s' contains no atoms",
862 "Excluding %d bonded neighbours molecule type '%s'\n",
863 mi0->nrexcl, *mi0->name);
864 sum_q(&mi0->atoms, nrcopies, &qt, &qBt);
865 if (!mi0->bProcessed)
868 generate_excl(mi0->nrexcl,
873 merge_excl(&(mi0->excls), &(block2[whichmol]), wi);
874 done_block2(&(block2[whichmol]));
875 make_shake(mi0->plist, &mi0->atoms, opts->nshake);
883 convert_moltype_couple(mi0, dcatt, *fudgeQQ,
884 opts->couple_lam0, opts->couple_lam1,
886 nb_funct, &(plist[nb_funct]), wi);
888 stupid_fill_block(&mi0->mols, mi0->atoms.nr, TRUE);
889 mi0->bProcessed = TRUE;
894 fprintf (stderr, "case: %d\n", (int)d);
895 gmx_incons("unknown directive");
904 status = cpp_close_file(&handle);
905 if (status != eCPP_OK)
907 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
915 if (opts->couple_moltype)
917 if (nmol_couple == 0)
919 gmx_fatal(FARGS, "Did not find any molecules of type '%s' for coupling",
920 opts->couple_moltype);
922 fprintf(stderr, "Coupling %d copies of molecule type '%s'\n",
923 nmol_couple, opts->couple_moltype);
926 /* this is not very clean, but fixes core dump on empty system name */
929 title = put_symtab(symtab, "");
934 sprintf(warn_buf, "System has non-zero total charge: %.6f\n%s\n", qt, floating_point_arithmetic_tip);
935 warning_note(wi, warn_buf);
937 if (fabs(qBt) > 1e-4 && !gmx_within_tol(qBt, qt, 1e-6))
939 sprintf(warn_buf, "State B has non-zero total charge: %.6f\n%s\n", qBt, floating_point_arithmetic_tip);
940 warning_note(wi, warn_buf);
942 if (usingFullRangeElectrostatics && (fabs(qt) > 1e-4 || fabs(qBt) > 1e-4))
944 warning(wi, "You are using Ewald electrostatics in a system with net charge. This can lead to severe artifacts, such as ions moving into regions with low dielectric, due to the uniform background charge. We suggest to neutralize your system with counter ions, possibly in combination with a physiological salt concentration.");
945 please_cite(stdout, "Hub2014a");
949 for (i = 0; i < nmol; i++)
951 done_block2(&(block2[i]));
955 done_bond_atomtype(&batype);
957 if (*intermolecular_interactions != nullptr)
959 sfree(mi0->atoms.atom);
967 char **do_top(bool bVerbose,
969 const char *topppfile,
974 int *combination_rule,
975 double *repulsion_power,
977 gpp_atomtype_t atype,
980 t_molinfo **intermolecular_interactions,
981 const t_inputrec *ir,
982 std::vector<gmx_molblock_t> *molblock,
985 /* Tmpfile might contain a long path */
1000 printf("processing topology...\n");
1002 title = read_topol(topfile, tmpfile, opts->define, opts->include,
1004 nrmols, molinfo, intermolecular_interactions,
1005 plist, combination_rule, repulsion_power,
1006 opts, fudgeQQ, molblock,
1007 ir->efep != efepNO, bZero,
1008 EEL_FULL(ir->coulombtype), wi);
1010 if ((*combination_rule != eCOMB_GEOMETRIC) &&
1011 (ir->vdwtype == evdwUSER))
1013 warning(wi, "Using sigma/epsilon based combination rules with"
1014 " user supplied potential function may produce unwanted"
1022 static void generate_qmexcl_moltype(gmx_moltype_t *molt, const unsigned char *grpnr,
1023 t_inputrec *ir, warninp_t wi)
1025 /* This routine expects molt->ilist to be of size F_NRE and ordered. */
1027 /* generates the exclusions between the individual QM atoms, as
1028 * these interactions should be handled by the QM subroutines and
1029 * not by the gromacs routines
1031 int qm_max = 0, qm_nr = 0, link_nr = 0, link_max = 0;
1032 int *qm_arr = nullptr, *link_arr = nullptr;
1033 bool *bQMMM, *blink;
1035 /* First we search and select the QM atoms in an qm_arr array that
1036 * we use to create the exclusions.
1038 * we take the possibility into account that a user has defined more
1039 * than one QM group:
1041 * for that we also need to do this an ugly work-about just in case
1042 * the QM group contains the entire system...
1045 /* we first search for all the QM atoms and put them in an array
1047 for (int j = 0; j < ir->opts.ngQM; j++)
1049 for (int i = 0; i < molt->atoms.nr; i++)
1051 if (qm_nr >= qm_max)
1054 srenew(qm_arr, qm_max);
1056 if ((grpnr ? grpnr[i] : 0) == j)
1058 qm_arr[qm_nr++] = i;
1062 /* bQMMM[..] is an array containin TRUE/FALSE for atoms that are
1063 * QM/not QM. We first set all elements to false. Afterwards we use
1064 * the qm_arr to change the elements corresponding to the QM atoms
1067 snew(bQMMM, molt->atoms.nr);
1068 for (int i = 0; i < molt->atoms.nr; i++)
1072 for (int i = 0; i < qm_nr; i++)
1074 bQMMM[qm_arr[i]] = TRUE;
1077 /* We remove all bonded interactions (i.e. bonds,
1078 * angles, dihedrals, 1-4's), involving the QM atoms. The way they
1079 * are removed is as follows: if the interaction invloves 2 atoms,
1080 * it is removed if both atoms are QMatoms. If it involves 3 atoms,
1081 * it is removed if at least two of the atoms are QM atoms, if the
1082 * interaction involves 4 atoms, it is removed if there are at least
1083 * 2 QM atoms. Since this routine is called once before any forces
1084 * are computed, the top->idef.il[N].iatom[] array (see idef.h) can
1085 * be rewritten at this poitn without any problem. 25-9-2002 */
1087 /* first check whether we already have CONNBONDS.
1088 * Note that if we don't, we don't add a param entry and set ftype=0,
1089 * which is ok, since CONNBONDS does not use parameters.
1091 int ftype_connbond = 0;
1092 int ind_connbond = 0;
1093 if (molt->ilist[F_CONNBONDS].nr != 0)
1095 fprintf(stderr, "nr. of CONNBONDS present already: %d\n",
1096 molt->ilist[F_CONNBONDS].nr/3);
1097 ftype_connbond = molt->ilist[F_CONNBONDS].iatoms[0];
1098 ind_connbond = molt->ilist[F_CONNBONDS].nr;
1100 /* now we delete all bonded interactions, except the ones describing
1101 * a chemical bond. These are converted to CONNBONDS
1103 for (int ftype = 0; ftype < F_NRE; ftype++)
1105 if (!(interaction_function[ftype].flags & IF_BOND) ||
1106 ftype == F_CONNBONDS)
1110 int nratoms = interaction_function[ftype].nratoms;
1112 while (j < molt->ilist[ftype].nr)
1118 /* Remove an interaction between two atoms when both are
1119 * in the QM region. Note that we don't have to worry about
1120 * link atoms here, as they won't have 2-atom interactions.
1122 int a1 = molt->ilist[ftype].iatoms[1 + j + 0];
1123 int a2 = molt->ilist[ftype].iatoms[1 + j + 1];
1124 bexcl = (bQMMM[a1] && bQMMM[a2]);
1125 /* A chemical bond between two QM atoms will be copied to
1126 * the F_CONNBONDS list, for reasons mentioned above.
1128 if (bexcl && IS_CHEMBOND(ftype))
1130 srenew(molt->ilist[F_CONNBONDS].iatoms, ind_connbond + 3);
1131 molt->ilist[F_CONNBONDS].nr += 3;
1132 molt->ilist[F_CONNBONDS].iatoms[ind_connbond++] = ftype_connbond;
1133 molt->ilist[F_CONNBONDS].iatoms[ind_connbond++] = a1;
1134 molt->ilist[F_CONNBONDS].iatoms[ind_connbond++] = a2;
1139 /* MM interactions have to be excluded if they are included
1140 * in the QM already. Because we use a link atom (H atom)
1141 * when the QM/MM boundary runs through a chemical bond, this
1142 * means that as long as one atom is MM, we still exclude,
1143 * as the interaction is included in the QM via:
1144 * QMatom1-QMatom2-QMatom-3-Linkatom.
1147 for (int jj = j + 1; jj < j + 1 + nratoms; jj++)
1149 if (bQMMM[molt->ilist[ftype].iatoms[jj]])
1154 bexcl = (numQmAtoms >= nratoms - 1);
1156 if (bexcl && ftype == F_SETTLE)
1158 gmx_fatal(FARGS, "Can not apply QM to molecules with SETTLE, replace the moleculetype using QM and SETTLE by one without SETTLE");
1163 /* since the interaction involves QM atoms, these should be
1164 * removed from the MM ilist
1166 molt->ilist[ftype].nr -= (nratoms+1);
1167 for (int l = j; l < molt->ilist[ftype].nr; l++)
1169 molt->ilist[ftype].iatoms[l] = molt->ilist[ftype].iatoms[l+(nratoms+1)];
1174 j += nratoms+1; /* the +1 is for the functype */
1178 /* Now, we search for atoms bonded to a QM atom because we also want
1179 * to exclude their nonbonded interactions with the QM atoms. The
1180 * reason for this is that this interaction is accounted for in the
1181 * linkatoms interaction with the QMatoms and would be counted
1184 for (int i = 0; i < F_NRE; i++)
1189 while (j < molt->ilist[i].nr)
1191 int a1 = molt->ilist[i].iatoms[j+1];
1192 int a2 = molt->ilist[i].iatoms[j+2];
1193 if ((bQMMM[a1] && !bQMMM[a2]) || (!bQMMM[a1] && bQMMM[a2]))
1195 if (link_nr >= link_max)
1198 srenew(link_arr, link_max);
1202 link_arr[link_nr++] = a2;
1206 link_arr[link_nr++] = a1;
1213 snew(blink, molt->atoms.nr);
1214 for (int i = 0; i < molt->atoms.nr; i++)
1218 for (int i = 0; i < link_nr; i++)
1220 blink[link_arr[i]] = TRUE;
1222 /* creating the exclusion block for the QM atoms. Each QM atom has
1223 * as excluded elements all the other QMatoms (and itself).
1226 qmexcl.nr = molt->atoms.nr;
1227 qmexcl.nra = qm_nr*(qm_nr+link_nr)+link_nr*qm_nr;
1228 snew(qmexcl.index, qmexcl.nr+1);
1229 snew(qmexcl.a, qmexcl.nra);
1231 for (int i = 0; i < qmexcl.nr; i++)
1233 qmexcl.index[i] = j;
1236 for (int k = 0; k < qm_nr; k++)
1238 qmexcl.a[k+j] = qm_arr[k];
1240 for (int k = 0; k < link_nr; k++)
1242 qmexcl.a[qm_nr+k+j] = link_arr[k];
1244 j += (qm_nr+link_nr);
1248 for (int k = 0; k < qm_nr; k++)
1250 qmexcl.a[k+j] = qm_arr[k];
1255 qmexcl.index[qmexcl.nr] = j;
1257 /* and merging with the exclusions already present in sys.
1261 init_block2(&qmexcl2, molt->atoms.nr);
1262 b_to_b2(&qmexcl, &qmexcl2);
1263 merge_excl(&(molt->excls), &qmexcl2, wi);
1264 done_block2(&qmexcl2);
1266 /* Finally, we also need to get rid of the pair interactions of the
1267 * classical atom bonded to the boundary QM atoms with the QMatoms,
1268 * as this interaction is already accounted for by the QM, so also
1269 * here we run the risk of double counting! We proceed in a similar
1270 * way as we did above for the other bonded interactions: */
1271 for (int i = F_LJ14; i < F_COUL14; i++)
1273 int nratoms = interaction_function[i].nratoms;
1275 while (j < molt->ilist[i].nr)
1277 int a1 = molt->ilist[i].iatoms[j+1];
1278 int a2 = molt->ilist[i].iatoms[j+2];
1279 bool bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1280 (blink[a1] && bQMMM[a2]) ||
1281 (bQMMM[a1] && blink[a2]));
1284 /* since the interaction involves QM atoms, these should be
1285 * removed from the MM ilist
1287 molt->ilist[i].nr -= (nratoms+1);
1288 for (int k = j; k < molt->ilist[i].nr; k++)
1290 molt->ilist[i].iatoms[k] = molt->ilist[i].iatoms[k+(nratoms+1)];
1295 j += nratoms+1; /* the +1 is for the functype */
1304 } /* generate_qmexcl */
1306 void generate_qmexcl(gmx_mtop_t *sys, t_inputrec *ir, warninp_t wi)
1308 /* This routine expects molt->molt[m].ilist to be of size F_NRE and ordered.
1311 unsigned char *grpnr;
1312 int mol, nat_mol, nr_mol_with_qm_atoms = 0;
1313 gmx_molblock_t *molb;
1316 grpnr = sys->groups.grpnr[egcQMMM];
1318 for (size_t mb = 0; mb < sys->molblock.size(); mb++)
1320 molb = &sys->molblock[mb];
1321 nat_mol = sys->moltype[molb->type].atoms.nr;
1322 for (mol = 0; mol < molb->nmol; mol++)
1325 for (int i = 0; i < nat_mol; i++)
1327 if ((grpnr ? grpnr[i] : 0) < ir->opts.ngQM)
1334 nr_mol_with_qm_atoms++;
1337 /* We need to split this molblock */
1340 /* Split the molblock at this molecule */
1341 auto pos = sys->molblock.begin() + mb + 1;
1342 sys->molblock.insert(pos, sys->molblock[mb]);
1343 sys->molblock[mb ].nmol = mol;
1344 sys->molblock[mb+1].nmol -= mol;
1346 molb = &sys->molblock[mb];
1350 /* Split the molblock after this molecule */
1351 auto pos = sys->molblock.begin() + mb + 1;
1352 sys->molblock.insert(pos, sys->molblock[mb]);
1353 molb = &sys->molblock[mb];
1354 sys->molblock[mb ].nmol = 1;
1355 sys->molblock[mb+1].nmol -= 1;
1358 /* Add a moltype for the QMMM molecule */
1359 sys->moltype.push_back(sys->moltype[molb->type]);
1360 /* Copy the exclusions to a new array, since this is the only
1361 * thing that needs to be modified for QMMM.
1363 copy_blocka(&sys->moltype[molb->type ].excls,
1364 &sys->moltype.back().excls);
1365 /* Set the molecule type for the QMMM molblock */
1366 molb->type = sys->moltype.size() - 1;
1368 generate_qmexcl_moltype(&sys->moltype[molb->type], grpnr, ir, wi);
1376 if (nr_mol_with_qm_atoms > 1)
1378 /* generate a warning is there are QM atoms in different
1379 * topologies. In this case it is not possible at this stage to
1380 * mutualy exclude the non-bonded interactions via the
1381 * exclusions (AFAIK). Instead, the user is advised to use the
1382 * energy group exclusions in the mdp file
1385 "\nThe QM subsystem is divided over multiple topologies. "
1386 "The mutual non-bonded interactions cannot be excluded. "
1387 "There are two ways to achieve this:\n\n"
1388 "1) merge the topologies, such that the atoms of the QM "
1389 "subsystem are all present in one single topology file. "
1390 "In this case this warning will dissappear\n\n"
1391 "2) exclude the non-bonded interactions explicitly via the "
1392 "energygrp-excl option in the mdp file. if this is the case "
1393 "this warning may be ignored"