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);
146 fprintf(debug, "Fudge factor for 1-4 interactions: %g\n", fudge);
147 fprintf(debug, "Holy Cow! there are %d types\n", ntp);
149 snew(pairs->param, pairs->nr);
150 for (i = 0; (i < ntp); i++)
153 pairs->param[i].a[0] = i / nnn;
154 pairs->param[i].a[1] = i % nnn;
155 /* Copy normal and FEP parameters and multiply by fudge factor */
159 for (j = 0; (j < nrfp); j++)
161 /* If we are using sigma/epsilon values, only the epsilon values
162 * should be scaled, but not sigma.
163 * The sigma values have even indices 0,2, etc.
165 if ((comb == eCOMB_ARITHMETIC || comb == eCOMB_GEOM_SIG_EPS) && (j%2 == 0))
174 pairs->param[i].c[j] = scaling*nbs->param[i].c[j];
175 /* NOTE: this should be cleat to the compiler, but some gcc 5.2 versions
176 * issue false positive warnings for the pairs->param.c[] indexing below.
178 assert(2*nrfp <= MAXFORCEPARAM);
179 pairs->param[i].c[nrfp+j] = scaling*nbs->param[i].c[j];
184 double check_mol(const gmx_mtop_t *mtop, warninp_t wi)
191 /* Check mass and charge */
194 for (const gmx_molblock_t &molb : mtop->molblock)
196 const t_atoms *atoms = &mtop->moltype[molb.type].atoms;
197 for (i = 0; (i < atoms->nr); i++)
199 q += molb.nmol*atoms->atom[i].q;
200 m = atoms->atom[i].m;
201 mB = atoms->atom[i].mB;
202 pt = atoms->atom[i].ptype;
203 /* If the particle is an atom or a nucleus it must have a mass,
204 * else, if it is a shell, a vsite or a bondshell it can have mass zero
206 if (((m <= 0.0) || (mB <= 0.0)) && ((pt == eptAtom) || (pt == eptNucleus)))
208 ri = atoms->atom[i].resind;
209 sprintf(buf, "atom %s (Res %s-%d) has mass %g (state A) / %g (state B)\n",
210 *(atoms->atomname[i]),
211 *(atoms->resinfo[ri].name),
212 atoms->resinfo[ri].nr,
214 warning_error(wi, buf);
217 if (((m != 0) || (mB != 0)) && (pt == eptVSite))
219 ri = atoms->atom[i].resind;
220 sprintf(buf, "virtual site %s (Res %s-%d) has non-zero mass %g (state A) / %g (state B)\n"
221 " Check your topology.\n",
222 *(atoms->atomname[i]),
223 *(atoms->resinfo[ri].name),
224 atoms->resinfo[ri].nr,
226 warning_error(wi, buf);
227 /* The following statements make LINCS break! */
228 /* atoms->atom[i].m=0; */
235 /*! \brief Returns the rounded charge of a molecule, when close to integer, otherwise returns the original charge.
237 * The results of this routine are only used for checking and for
238 * printing warning messages. Thus we can assume that charges of molecules
239 * should be integer. If the user wanted non-integer molecular charge,
240 * an undesired warning is printed and the user should use grompp -maxwarn 1.
242 * \param qMol The total, unrounded, charge of the molecule
243 * \param sumAbsQ The sum of absolute values of the charges, used for determining the tolerance for the rounding.
245 static double roundedMoleculeCharge(double qMol, double sumAbsQ)
247 /* We use a tolerance of 1e-6 for inaccuracies beyond the 6th decimal
248 * of the charges for ascii float truncation in the topology files.
249 * Although the summation here uses double precision, the charges
250 * are read and stored in single precision when real=float. This can
251 * lead to rounding errors of half the least significant bit.
252 * Note that, unfortunately, we can not assume addition of random
253 * rounding errors. It is not entirely unlikely that many charges
254 * have a near half-bit rounding error with the same sign.
256 double tolAbs = 1e-6;
257 double tol = std::max(tolAbs, 0.5*GMX_REAL_EPS*sumAbsQ);
258 double qRound = std::round(qMol);
259 if (std::abs(qMol - qRound) <= tol)
269 static void sum_q(const t_atoms *atoms, int numMols,
270 double *qTotA, double *qTotB)
277 for (int i = 0; i < atoms->nr; i++)
279 qmolA += atoms->atom[i].q;
280 qmolB += atoms->atom[i].qB;
281 sumAbsQA += std::abs(atoms->atom[i].q);
282 sumAbsQB += std::abs(atoms->atom[i].qB);
285 *qTotA += numMols*roundedMoleculeCharge(qmolA, sumAbsQA);
286 *qTotB += numMols*roundedMoleculeCharge(qmolB, sumAbsQB);
289 static void get_nbparm(char *nb_str, char *comb_str, int *nb, int *comb,
293 char warn_buf[STRLEN];
296 for (i = 1; (i < eNBF_NR); i++)
298 if (gmx_strcasecmp(nb_str, enbf_names[i]) == 0)
305 *nb = strtol(nb_str, nullptr, 10);
307 if ((*nb < 1) || (*nb >= eNBF_NR))
309 sprintf(warn_buf, "Invalid nonbond function selector '%s' using %s",
310 nb_str, enbf_names[1]);
311 warning_error(wi, warn_buf);
315 for (i = 1; (i < eCOMB_NR); i++)
317 if (gmx_strcasecmp(comb_str, ecomb_names[i]) == 0)
324 *comb = strtol(comb_str, nullptr, 10);
326 if ((*comb < 1) || (*comb >= eCOMB_NR))
328 sprintf(warn_buf, "Invalid combination rule selector '%s' using %s",
329 comb_str, ecomb_names[1]);
330 warning_error(wi, warn_buf);
335 static char ** cpp_opts(const char *define, const char *include,
340 const char *cppadds[2];
341 char **cppopts = nullptr;
342 const char *option[2] = { "-D", "-I" };
343 const char *nopt[2] = { "define", "include" };
347 char warn_buf[STRLEN];
350 cppadds[1] = include;
351 for (n = 0; (n < 2); n++)
358 while ((*ptr != '\0') && isspace(*ptr))
363 while ((*rptr != '\0') && !isspace(*rptr))
371 strncpy(buf, ptr, len);
372 if (strstr(ptr, option[n]) != ptr)
374 set_warning_line(wi, "mdp file", -1);
375 sprintf(warn_buf, "Malformed %s option %s", nopt[n], buf);
376 warning(wi, warn_buf);
380 srenew(cppopts, ++ncppopts);
381 cppopts[ncppopts-1] = gmx_strdup(buf);
389 srenew(cppopts, ++ncppopts);
390 cppopts[ncppopts-1] = nullptr;
396 static void make_atoms_sys(const std::vector<gmx_molblock_t> &molblock,
397 const t_molinfo *molinfo,
401 atoms->atom = nullptr;
403 for (const gmx_molblock_t &molb : molblock)
405 const t_atoms &mol_atoms = molinfo[molb.type].atoms;
407 srenew(atoms->atom, atoms->nr + molb.nmol*mol_atoms.nr);
409 for (int m = 0; m < molb.nmol; m++)
411 for (int a = 0; a < mol_atoms.nr; a++)
413 atoms->atom[atoms->nr++] = mol_atoms.atom[a];
420 static char **read_topol(const char *infile, const char *outfile,
421 const char *define, const char *include,
423 gpp_atomtype_t atype,
426 t_molinfo **intermolecular_interactions,
428 int *combination_rule,
432 std::vector<gmx_molblock_t> *molblock,
435 gmx_bool usingFullRangeElectrostatics,
440 char *pline = nullptr, **title = nullptr;
441 char line[STRLEN], errbuf[256], comb_str[256], nb_str[256];
443 char *dirstr, *dummy2;
444 int nrcopies, nmol, nscan, ncombs, ncopy;
445 double fLJ, fQQ, fPOW;
446 t_molinfo *mi0 = nullptr;
449 t_nbparam **nbparam, **pair;
451 real fudgeLJ = -1; /* Multiplication factor to generate 1-4 from LJ */
452 gmx_bool bReadDefaults, bReadMolType, bGenPairs, bWarn_copy_A_B;
453 double qt = 0, qBt = 0; /* total charge */
454 t_bond_atomtype batype;
456 int dcatt = -1, nmol_couple;
457 /* File handling variables */
460 char *tmp_line = nullptr;
461 char warn_buf[STRLEN];
462 const char *floating_point_arithmetic_tip =
463 "Total charge should normally be an integer. See\n"
464 "http://www.gromacs.org/Documentation/Floating_Point_Arithmetic\n"
465 "for discussion on how close it should be to an integer.\n";
466 /* We need to open the output file before opening the input file,
467 * because cpp_open_file can change the current working directory.
471 out = gmx_fio_fopen(outfile, "w");
478 /* open input file */
479 status = cpp_open_file(infile, &handle, cpp_opts(define, include, wi));
482 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
485 /* some local variables */
486 DS_Init(&DS); /* directive stack */
487 nmol = 0; /* no molecules yet... */
488 d = d_invalid; /* first thing should be a directive */
489 nbparam = nullptr; /* The temporary non-bonded matrix */
490 pair = nullptr; /* The temporary pair interaction matrix */
491 block2 = nullptr; /* the extra exclusions */
494 *reppow = 12.0; /* Default value for repulsion power */
496 *intermolecular_interactions = nullptr;
498 /* Init the number of CMAP torsion angles and grid spacing */
499 plist[F_CMAP].grid_spacing = 0;
500 plist[F_CMAP].nc = 0;
502 bWarn_copy_A_B = bFEP;
504 batype = init_bond_atomtype();
505 /* parse the actual file */
506 bReadDefaults = FALSE;
508 bReadMolType = FALSE;
513 status = cpp_read_line(&handle, STRLEN, line);
514 done = (status == eCPP_EOF);
517 if (status != eCPP_OK)
519 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
523 fprintf(out, "%s\n", line);
526 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
528 pline = gmx_strdup(line);
530 /* Strip trailing '\' from pline, if it exists */
532 if ((sl > 0) && (pline[sl-1] == CONTINUE))
537 /* build one long line from several fragments - necessary for CMAP */
538 while (continuing(line))
540 status = cpp_read_line(&handle, STRLEN, line);
541 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
543 /* Since we depend on the '\' being present to continue to read, we copy line
544 * to a tmp string, strip the '\' from that string, and cat it to pline
546 tmp_line = gmx_strdup(line);
548 sl = strlen(tmp_line);
549 if ((sl > 0) && (tmp_line[sl-1] == CONTINUE))
551 tmp_line[sl-1] = ' ';
554 done = (status == eCPP_EOF);
557 if (status != eCPP_OK)
559 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
563 fprintf(out, "%s\n", line);
567 srenew(pline, strlen(pline)+strlen(tmp_line)+1);
568 strcat(pline, tmp_line);
572 /* skip trailing and leading spaces and comment text */
573 strip_comment (pline);
576 /* if there is something left... */
577 if ((int)strlen(pline) > 0)
579 if (pline[0] == OPENDIR)
581 /* A directive on this line: copy the directive
582 * without the brackets into dirstr, then
583 * skip spaces and tabs on either side of directive
585 dirstr = gmx_strdup((pline+1));
586 if ((dummy2 = strchr (dirstr, CLOSEDIR)) != nullptr)
592 if ((newd = str2dir(dirstr)) == d_invalid)
594 sprintf(errbuf, "Invalid directive %s", dirstr);
595 warning_error(wi, errbuf);
599 /* Directive found */
602 fprintf(debug, "found directive '%s'\n", dir2str(newd));
604 if (DS_Check_Order (DS, newd))
611 /* we should print here which directives should have
612 been present, and which actually are */
613 gmx_fatal(FARGS, "%s\nInvalid order for directive %s",
614 cpp_error(&handle, eCPP_SYNTAX), dir2str(newd));
618 if (d == d_intermolecular_interactions)
620 if (*intermolecular_interactions == nullptr)
622 /* We (mis)use the moleculetype processing
623 * to process the intermolecular interactions
624 * by making a "molecule" of the size of the system.
626 snew(*intermolecular_interactions, 1);
627 init_molinfo(*intermolecular_interactions);
628 mi0 = *intermolecular_interactions;
629 make_atoms_sys(*molblock, *molinfo,
636 else if (d != d_invalid)
638 /* Not a directive, just a plain string
639 * use a gigantic switch to decode,
640 * if there is a valid directive!
647 gmx_fatal(FARGS, "%s\nFound a second defaults directive.\n",
648 cpp_error(&handle, eCPP_SYNTAX));
650 bReadDefaults = TRUE;
651 nscan = sscanf(pline, "%s%s%s%lf%lf%lf",
652 nb_str, comb_str, genpairs, &fLJ, &fQQ, &fPOW);
663 get_nbparm(nb_str, comb_str, &nb_funct, combination_rule, wi);
666 bGenPairs = (gmx_strncasecmp(genpairs, "Y", 1) == 0);
667 if (nb_funct != eNBF_LJ && bGenPairs)
669 gmx_fatal(FARGS, "Generating pair parameters is only supported with LJ non-bonded interactions");
685 nb_funct = ifunc_index(d_nonbond_params, nb_funct);
689 push_at(symtab, atype, batype, pline, nb_funct,
690 &nbparam, bGenPairs ? &pair : nullptr, wi);
694 push_bt(d, plist, 2, nullptr, batype, pline, wi);
696 case d_constrainttypes:
697 push_bt(d, plist, 2, nullptr, batype, pline, wi);
702 push_nbt(d, pair, atype, pline, F_LJ14, wi);
706 push_bt(d, plist, 2, atype, nullptr, pline, wi);
710 push_bt(d, plist, 3, nullptr, batype, pline, wi);
712 case d_dihedraltypes:
713 /* Special routine that can read both 2 and 4 atom dihedral definitions. */
714 push_dihedraltype(d, plist, batype, pline, wi);
717 case d_nonbond_params:
718 push_nbt(d, nbparam, atype, pline, nb_funct, wi);
723 srenew(block,nblock);
724 srenew(blockinfo,nblock);
725 blk0=&(block[nblock-1]);
726 bi0=&(blockinfo[nblock-1]);
729 push_molt(symtab,bi0,pline);
733 case d_implicit_genborn_params:
734 // Skip this line, so old topologies with
735 // GB parameters can be read.
738 case d_implicit_surface_params:
739 // Skip this line, so that any topologies
740 // with surface parameters can be read
741 // (even though these were never formally
746 push_cmaptype(d, plist, 5, atype, batype, pline, wi);
754 if (opts->couple_moltype != nullptr &&
755 (opts->couple_lam0 == ecouplamNONE ||
756 opts->couple_lam0 == ecouplamQ ||
757 opts->couple_lam1 == ecouplamNONE ||
758 opts->couple_lam1 == ecouplamQ))
760 dcatt = add_atomtype_decoupled(symtab, atype,
761 &nbparam, bGenPairs ? &pair : nullptr);
763 ntype = get_atomtype_ntypes(atype);
764 ncombs = (ntype*(ntype+1))/2;
765 generate_nbparams(*combination_rule, nb_funct, &(plist[nb_funct]), atype, wi);
766 ncopy = copy_nbparams(nbparam, nb_funct, &(plist[nb_funct]),
768 fprintf(stderr, "Generated %d of the %d non-bonded parameter combinations\n", ncombs-ncopy, ncombs);
769 free_nbparam(nbparam, ntype);
772 gen_pairs(&(plist[nb_funct]), &(plist[F_LJ14]), fudgeLJ, *combination_rule);
773 ncopy = copy_nbparams(pair, nb_funct, &(plist[F_LJ14]),
775 fprintf(stderr, "Generated %d of the %d 1-4 parameter combinations\n", ncombs-ncopy, ncombs);
776 free_nbparam(pair, ntype);
778 /* Copy GBSA parameters to atomtype array? */
783 push_molt(symtab, &nmol, molinfo, pline, wi);
784 srenew(block2, nmol);
785 block2[nmol-1].nr = 0;
786 mi0 = &((*molinfo)[nmol-1]);
787 mi0->atoms.haveMass = TRUE;
788 mi0->atoms.haveCharge = TRUE;
789 mi0->atoms.haveType = TRUE;
790 mi0->atoms.haveBState = TRUE;
791 mi0->atoms.havePdbInfo = FALSE;
795 push_atom(symtab, &(mi0->cgs), &(mi0->atoms), atype, pline, &lastcg, wi);
799 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, FALSE,
800 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
803 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, FALSE,
804 FALSE, 1.0, bZero, &bWarn_copy_A_B, wi);
814 case d_position_restraints:
815 case d_angle_restraints:
816 case d_angle_restraints_z:
817 case d_distance_restraints:
818 case d_orientation_restraints:
819 case d_dihedral_restraints:
822 case d_water_polarization:
823 case d_thole_polarization:
824 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, TRUE,
825 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
828 push_cmap(d, plist, mi0->plist, &(mi0->atoms), atype, pline, wi);
832 push_vsitesn(d, mi0->plist, &(mi0->atoms), pline, wi);
835 GMX_ASSERT(block2, "block2 must always be allocated so exclusions can be processed");
836 if (!block2[nmol-1].nr)
838 init_block2(&(block2[nmol-1]), mi0->atoms.nr);
840 push_excl(pline, &(block2[nmol-1]), wi);
844 title = put_symtab(symtab, pline);
851 push_mol(nmol, *molinfo, pline, &whichmol, &nrcopies, wi);
852 mi0 = &((*molinfo)[whichmol]);
853 molblock->resize(molblock->size() + 1);
854 molblock->back().type = whichmol;
855 molblock->back().nmol = nrcopies;
857 bCouple = (opts->couple_moltype != nullptr &&
858 (gmx_strcasecmp("system", opts->couple_moltype) == 0 ||
859 strcmp(*(mi0->name), opts->couple_moltype) == 0));
862 nmol_couple += nrcopies;
865 if (mi0->atoms.nr == 0)
867 gmx_fatal(FARGS, "Molecule type '%s' contains no atoms",
871 "Excluding %d bonded neighbours molecule type '%s'\n",
872 mi0->nrexcl, *mi0->name);
873 sum_q(&mi0->atoms, nrcopies, &qt, &qBt);
874 if (!mi0->bProcessed)
877 generate_excl(mi0->nrexcl,
882 merge_excl(&(mi0->excls), &(block2[whichmol]), wi);
883 done_block2(&(block2[whichmol]));
884 make_shake(mi0->plist, &mi0->atoms, opts->nshake);
892 convert_moltype_couple(mi0, dcatt, *fudgeQQ,
893 opts->couple_lam0, opts->couple_lam1,
895 nb_funct, &(plist[nb_funct]), wi);
897 stupid_fill_block(&mi0->mols, mi0->atoms.nr, TRUE);
898 mi0->bProcessed = TRUE;
903 fprintf (stderr, "case: %d\n", (int)d);
904 gmx_incons("unknown directive");
913 status = cpp_close_file(&handle);
914 if (status != eCPP_OK)
916 gmx_fatal(FARGS, "%s", cpp_error(&handle, status));
924 if (opts->couple_moltype)
926 if (nmol_couple == 0)
928 gmx_fatal(FARGS, "Did not find any molecules of type '%s' for coupling",
929 opts->couple_moltype);
931 fprintf(stderr, "Coupling %d copies of molecule type '%s'\n",
932 nmol_couple, opts->couple_moltype);
935 /* this is not very clean, but fixes core dump on empty system name */
938 title = put_symtab(symtab, "");
943 sprintf(warn_buf, "System has non-zero total charge: %.6f\n%s\n", qt, floating_point_arithmetic_tip);
944 warning_note(wi, warn_buf);
946 if (fabs(qBt) > 1e-4 && !gmx_within_tol(qBt, qt, 1e-6))
948 sprintf(warn_buf, "State B has non-zero total charge: %.6f\n%s\n", qBt, floating_point_arithmetic_tip);
949 warning_note(wi, warn_buf);
951 if (usingFullRangeElectrostatics && (fabs(qt) > 1e-4 || fabs(qBt) > 1e-4))
953 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.");
954 please_cite(stdout, "Hub2014a");
958 for (i = 0; i < nmol; i++)
960 done_block2(&(block2[i]));
964 done_bond_atomtype(&batype);
966 if (*intermolecular_interactions != nullptr)
968 sfree(mi0->atoms.atom);
976 char **do_top(gmx_bool bVerbose,
978 const char *topppfile,
983 int *combination_rule,
984 double *repulsion_power,
986 gpp_atomtype_t atype,
989 t_molinfo **intermolecular_interactions,
990 const t_inputrec *ir,
991 std::vector<gmx_molblock_t> *molblock,
994 /* Tmpfile might contain a long path */
1000 tmpfile = topppfile;
1009 printf("processing topology...\n");
1011 title = read_topol(topfile, tmpfile, opts->define, opts->include,
1013 nrmols, molinfo, intermolecular_interactions,
1014 plist, combination_rule, repulsion_power,
1015 opts, fudgeQQ, molblock,
1016 ir->efep != efepNO, bZero,
1017 EEL_FULL(ir->coulombtype), wi);
1019 if ((*combination_rule != eCOMB_GEOMETRIC) &&
1020 (ir->vdwtype == evdwUSER))
1022 warning(wi, "Using sigma/epsilon based combination rules with"
1023 " user supplied potential function may produce unwanted"
1031 static void generate_qmexcl_moltype(gmx_moltype_t *molt, const unsigned char *grpnr,
1032 t_inputrec *ir, warninp_t wi)
1034 /* This routine expects molt->ilist to be of size F_NRE and ordered. */
1036 /* generates the exclusions between the individual QM atoms, as
1037 * these interactions should be handled by the QM subroutines and
1038 * not by the gromacs routines
1040 int qm_max = 0, qm_nr = 0, link_nr = 0, link_max = 0;
1041 int *qm_arr = nullptr, *link_arr = nullptr;
1042 gmx_bool *bQMMM, *blink;
1044 /* First we search and select the QM atoms in an qm_arr array that
1045 * we use to create the exclusions.
1047 * we take the possibility into account that a user has defined more
1048 * than one QM group:
1050 * for that we also need to do this an ugly work-about just in case
1051 * the QM group contains the entire system...
1054 /* we first search for all the QM atoms and put them in an array
1056 for (int j = 0; j < ir->opts.ngQM; j++)
1058 for (int i = 0; i < molt->atoms.nr; i++)
1060 if (qm_nr >= qm_max)
1063 srenew(qm_arr, qm_max);
1065 if ((grpnr ? grpnr[i] : 0) == j)
1067 qm_arr[qm_nr++] = i;
1071 /* bQMMM[..] is an array containin TRUE/FALSE for atoms that are
1072 * QM/not QM. We first set all elements to false. Afterwards we use
1073 * the qm_arr to change the elements corresponding to the QM atoms
1076 snew(bQMMM, molt->atoms.nr);
1077 for (int i = 0; i < molt->atoms.nr; i++)
1081 for (int i = 0; i < qm_nr; i++)
1083 bQMMM[qm_arr[i]] = TRUE;
1086 /* We remove all bonded interactions (i.e. bonds,
1087 * angles, dihedrals, 1-4's), involving the QM atoms. The way they
1088 * are removed is as follows: if the interaction invloves 2 atoms,
1089 * it is removed if both atoms are QMatoms. If it involves 3 atoms,
1090 * it is removed if at least two of the atoms are QM atoms, if the
1091 * interaction involves 4 atoms, it is removed if there are at least
1092 * 2 QM atoms. Since this routine is called once before any forces
1093 * are computed, the top->idef.il[N].iatom[] array (see idef.h) can
1094 * be rewritten at this poitn without any problem. 25-9-2002 */
1096 /* first check whether we already have CONNBONDS.
1097 * Note that if we don't, we don't add a param entry and set ftype=0,
1098 * which is ok, since CONNBONDS does not use parameters.
1100 int ftype_connbond = 0;
1101 int ind_connbond = 0;
1102 if (molt->ilist[F_CONNBONDS].nr != 0)
1104 fprintf(stderr, "nr. of CONNBONDS present already: %d\n",
1105 molt->ilist[F_CONNBONDS].nr/3);
1106 ftype_connbond = molt->ilist[F_CONNBONDS].iatoms[0];
1107 ind_connbond = molt->ilist[F_CONNBONDS].nr;
1109 /* now we delete all bonded interactions, except the ones describing
1110 * a chemical bond. These are converted to CONNBONDS
1112 for (int ftype = 0; ftype < F_NRE; ftype++)
1114 if (!(interaction_function[ftype].flags & IF_BOND) ||
1115 ftype == F_CONNBONDS)
1119 int nratoms = interaction_function[ftype].nratoms;
1121 while (j < molt->ilist[ftype].nr)
1127 /* Remove an interaction between two atoms when both are
1128 * in the QM region. Note that we don't have to worry about
1129 * link atoms here, as they won't have 2-atom interactions.
1131 int a1 = molt->ilist[ftype].iatoms[1 + j + 0];
1132 int a2 = molt->ilist[ftype].iatoms[1 + j + 1];
1133 bexcl = (bQMMM[a1] && bQMMM[a2]);
1134 /* A chemical bond between two QM atoms will be copied to
1135 * the F_CONNBONDS list, for reasons mentioned above.
1137 if (bexcl && IS_CHEMBOND(ftype))
1139 srenew(molt->ilist[F_CONNBONDS].iatoms, ind_connbond + 3);
1140 molt->ilist[F_CONNBONDS].nr += 3;
1141 molt->ilist[F_CONNBONDS].iatoms[ind_connbond++] = ftype_connbond;
1142 molt->ilist[F_CONNBONDS].iatoms[ind_connbond++] = a1;
1143 molt->ilist[F_CONNBONDS].iatoms[ind_connbond++] = a2;
1148 /* MM interactions have to be excluded if they are included
1149 * in the QM already. Because we use a link atom (H atom)
1150 * when the QM/MM boundary runs through a chemical bond, this
1151 * means that as long as one atom is MM, we still exclude,
1152 * as the interaction is included in the QM via:
1153 * QMatom1-QMatom2-QMatom-3-Linkatom.
1156 for (int jj = j + 1; jj < j + 1 + nratoms; jj++)
1158 if (bQMMM[molt->ilist[ftype].iatoms[jj]])
1163 bexcl = (numQmAtoms >= nratoms - 1);
1165 if (bexcl && ftype == F_SETTLE)
1167 gmx_fatal(FARGS, "Can not apply QM to molecules with SETTLE, replace the moleculetype using QM and SETTLE by one without SETTLE");
1172 /* since the interaction involves QM atoms, these should be
1173 * removed from the MM ilist
1175 molt->ilist[ftype].nr -= (nratoms+1);
1176 for (int l = j; l < molt->ilist[ftype].nr; l++)
1178 molt->ilist[ftype].iatoms[l] = molt->ilist[ftype].iatoms[l+(nratoms+1)];
1183 j += nratoms+1; /* the +1 is for the functype */
1187 /* Now, we search for atoms bonded to a QM atom because we also want
1188 * to exclude their nonbonded interactions with the QM atoms. The
1189 * reason for this is that this interaction is accounted for in the
1190 * linkatoms interaction with the QMatoms and would be counted
1193 for (int i = 0; i < F_NRE; i++)
1198 while (j < molt->ilist[i].nr)
1200 int a1 = molt->ilist[i].iatoms[j+1];
1201 int a2 = molt->ilist[i].iatoms[j+2];
1202 if ((bQMMM[a1] && !bQMMM[a2]) || (!bQMMM[a1] && bQMMM[a2]))
1204 if (link_nr >= link_max)
1207 srenew(link_arr, link_max);
1211 link_arr[link_nr++] = a2;
1215 link_arr[link_nr++] = a1;
1222 snew(blink, molt->atoms.nr);
1223 for (int i = 0; i < molt->atoms.nr; i++)
1227 for (int i = 0; i < link_nr; i++)
1229 blink[link_arr[i]] = TRUE;
1231 /* creating the exclusion block for the QM atoms. Each QM atom has
1232 * as excluded elements all the other QMatoms (and itself).
1235 qmexcl.nr = molt->atoms.nr;
1236 qmexcl.nra = qm_nr*(qm_nr+link_nr)+link_nr*qm_nr;
1237 snew(qmexcl.index, qmexcl.nr+1);
1238 snew(qmexcl.a, qmexcl.nra);
1240 for (int i = 0; i < qmexcl.nr; i++)
1242 qmexcl.index[i] = j;
1245 for (int k = 0; k < qm_nr; k++)
1247 qmexcl.a[k+j] = qm_arr[k];
1249 for (int k = 0; k < link_nr; k++)
1251 qmexcl.a[qm_nr+k+j] = link_arr[k];
1253 j += (qm_nr+link_nr);
1257 for (int k = 0; k < qm_nr; k++)
1259 qmexcl.a[k+j] = qm_arr[k];
1264 qmexcl.index[qmexcl.nr] = j;
1266 /* and merging with the exclusions already present in sys.
1270 init_block2(&qmexcl2, molt->atoms.nr);
1271 b_to_b2(&qmexcl, &qmexcl2);
1272 merge_excl(&(molt->excls), &qmexcl2, wi);
1273 done_block2(&qmexcl2);
1275 /* Finally, we also need to get rid of the pair interactions of the
1276 * classical atom bonded to the boundary QM atoms with the QMatoms,
1277 * as this interaction is already accounted for by the QM, so also
1278 * here we run the risk of double counting! We proceed in a similar
1279 * way as we did above for the other bonded interactions: */
1280 for (int i = F_LJ14; i < F_COUL14; i++)
1282 int nratoms = interaction_function[i].nratoms;
1284 while (j < molt->ilist[i].nr)
1286 int a1 = molt->ilist[i].iatoms[j+1];
1287 int a2 = molt->ilist[i].iatoms[j+2];
1288 bool bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1289 (blink[a1] && bQMMM[a2]) ||
1290 (bQMMM[a1] && blink[a2]));
1293 /* since the interaction involves QM atoms, these should be
1294 * removed from the MM ilist
1296 molt->ilist[i].nr -= (nratoms+1);
1297 for (int k = j; k < molt->ilist[i].nr; k++)
1299 molt->ilist[i].iatoms[k] = molt->ilist[i].iatoms[k+(nratoms+1)];
1304 j += nratoms+1; /* the +1 is for the functype */
1313 } /* generate_qmexcl */
1315 void generate_qmexcl(gmx_mtop_t *sys, t_inputrec *ir, warninp_t wi)
1317 /* This routine expects molt->molt[m].ilist to be of size F_NRE and ordered.
1320 unsigned char *grpnr;
1321 int mol, nat_mol, nr_mol_with_qm_atoms = 0;
1322 gmx_molblock_t *molb;
1325 grpnr = sys->groups.grpnr[egcQMMM];
1327 for (size_t mb = 0; mb < sys->molblock.size(); mb++)
1329 molb = &sys->molblock[mb];
1330 nat_mol = sys->moltype[molb->type].atoms.nr;
1331 for (mol = 0; mol < molb->nmol; mol++)
1334 for (int i = 0; i < nat_mol; i++)
1336 if ((grpnr ? grpnr[i] : 0) < ir->opts.ngQM)
1343 nr_mol_with_qm_atoms++;
1346 /* We need to split this molblock */
1349 /* Split the molblock at this molecule */
1350 auto pos = sys->molblock.begin() + mb + 1;
1351 sys->molblock.insert(pos, sys->molblock[mb]);
1352 sys->molblock[mb ].nmol = mol;
1353 sys->molblock[mb+1].nmol -= mol;
1355 molb = &sys->molblock[mb];
1359 /* Split the molblock after this molecule */
1360 auto pos = sys->molblock.begin() + mb + 1;
1361 sys->molblock.insert(pos, sys->molblock[mb]);
1362 molb = &sys->molblock[mb];
1363 sys->molblock[mb ].nmol = 1;
1364 sys->molblock[mb+1].nmol -= 1;
1367 /* Add a moltype for the QMMM molecule */
1368 sys->moltype.push_back(sys->moltype[molb->type]);
1369 /* Copy the exclusions to a new array, since this is the only
1370 * thing that needs to be modified for QMMM.
1372 copy_blocka(&sys->moltype[molb->type ].excls,
1373 &sys->moltype.back().excls);
1374 /* Set the molecule type for the QMMM molblock */
1375 molb->type = sys->moltype.size() - 1;
1377 generate_qmexcl_moltype(&sys->moltype[molb->type], grpnr, ir, wi);
1385 if (nr_mol_with_qm_atoms > 1)
1387 /* generate a warning is there are QM atoms in different
1388 * topologies. In this case it is not possible at this stage to
1389 * mutualy exclude the non-bonded interactions via the
1390 * exclusions (AFAIK). Instead, the user is advised to use the
1391 * energy group exclusions in the mdp file
1394 "\nThe QM subsystem is divided over multiple topologies. "
1395 "The mutual non-bonded interactions cannot be excluded. "
1396 "There are two ways to achieve this:\n\n"
1397 "1) merge the topologies, such that the atoms of the QM "
1398 "subsystem are all present in one single topology file. "
1399 "In this case this warning will dissappear\n\n"
1400 "2) exclude the non-bonded interactions explicitly via the "
1401 "energygrp-excl option in the mdp file. if this is the case "
1402 "this warning may be ignored"