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.
49 #include "gromacs/fileio/warninp.h"
50 #include "gromacs/gmxpreprocess/gpp_atomtype.h"
51 #include "gromacs/gmxpreprocess/gpp_bond_atomtype.h"
52 #include "gromacs/gmxpreprocess/notset.h"
53 #include "gromacs/gmxpreprocess/readir.h"
54 #include "gromacs/gmxpreprocess/topdirs.h"
55 #include "gromacs/gmxpreprocess/toputil.h"
56 #include "gromacs/math/functions.h"
57 #include "gromacs/mdtypes/md_enums.h"
58 #include "gromacs/topology/exclusionblocks.h"
59 #include "gromacs/topology/ifunc.h"
60 #include "gromacs/topology/symtab.h"
61 #include "gromacs/utility/arrayref.h"
62 #include "gromacs/utility/cstringutil.h"
63 #include "gromacs/utility/fatalerror.h"
64 #include "gromacs/utility/gmxassert.h"
65 #include "gromacs/utility/smalloc.h"
66 #include "gromacs/utility/stringutil.h"
68 void generate_nbparams(int comb, int ftype, t_params *plist, gpp_atomtype_t atype,
73 real c, bi, bj, ci, cj, ci0, ci1, ci2, cj0, cj1, cj2;
75 /* Lean mean shortcuts */
76 nr = get_atomtype_ntypes(atype);
78 snew(plist->param, nr*nr);
81 /* Fill the matrix with force parameters */
89 for (i = k = 0; (i < nr); i++)
91 for (j = 0; (j < nr); j++, k++)
93 for (nf = 0; (nf < nrfp); nf++)
95 ci = get_atomtype_nbparam(i, nf, atype);
96 cj = get_atomtype_nbparam(j, nf, atype);
97 c = std::sqrt(ci * cj);
98 plist->param[k].c[nf] = c;
104 case eCOMB_ARITHMETIC:
105 /* c0 and c1 are sigma and epsilon */
106 for (i = k = 0; (i < nr); i++)
108 for (j = 0; (j < nr); j++, k++)
110 ci0 = get_atomtype_nbparam(i, 0, atype);
111 cj0 = get_atomtype_nbparam(j, 0, atype);
112 ci1 = get_atomtype_nbparam(i, 1, atype);
113 cj1 = get_atomtype_nbparam(j, 1, atype);
114 plist->param[k].c[0] = (fabs(ci0) + fabs(cj0))*0.5;
115 /* Negative sigma signals that c6 should be set to zero later,
116 * so we need to propagate that through the combination rules.
118 if (ci0 < 0 || cj0 < 0)
120 plist->param[k].c[0] *= -1;
122 plist->param[k].c[1] = std::sqrt(ci1*cj1);
127 case eCOMB_GEOM_SIG_EPS:
128 /* c0 and c1 are sigma and epsilon */
129 for (i = k = 0; (i < nr); i++)
131 for (j = 0; (j < nr); j++, k++)
133 ci0 = get_atomtype_nbparam(i, 0, atype);
134 cj0 = get_atomtype_nbparam(j, 0, atype);
135 ci1 = get_atomtype_nbparam(i, 1, atype);
136 cj1 = get_atomtype_nbparam(j, 1, atype);
137 plist->param[k].c[0] = std::sqrt(std::fabs(ci0*cj0));
138 /* Negative sigma signals that c6 should be set to zero later,
139 * so we need to propagate that through the combination rules.
141 if (ci0 < 0 || cj0 < 0)
143 plist->param[k].c[0] *= -1;
145 plist->param[k].c[1] = std::sqrt(ci1*cj1);
151 auto message = gmx::formatString("No such combination rule %d", comb);
152 warning_error_and_exit(wi, message, FARGS);
156 gmx_incons("Topology processing, generate nb parameters");
161 /* Buckingham rules */
162 for (i = k = 0; (i < nr); i++)
164 for (j = 0; (j < nr); j++, k++)
166 ci0 = get_atomtype_nbparam(i, 0, atype);
167 cj0 = get_atomtype_nbparam(j, 0, atype);
168 ci2 = get_atomtype_nbparam(i, 2, atype);
169 cj2 = get_atomtype_nbparam(j, 2, atype);
170 bi = get_atomtype_nbparam(i, 1, atype);
171 bj = get_atomtype_nbparam(j, 1, atype);
172 plist->param[k].c[0] = std::sqrt(ci0 * cj0);
173 if ((bi == 0) || (bj == 0))
175 plist->param[k].c[1] = 0;
179 plist->param[k].c[1] = 2.0/(1/bi+1/bj);
181 plist->param[k].c[2] = std::sqrt(ci2 * cj2);
187 auto message = gmx::formatString("Invalid nonbonded type %s",
188 interaction_function[ftype].longname);
189 warning_error(wi, message);
193 static void realloc_nb_params(gpp_atomtype_t at,
194 t_nbparam ***nbparam, t_nbparam ***pair)
196 /* Add space in the non-bonded parameters matrix */
197 int atnr = get_atomtype_ntypes(at);
198 srenew(*nbparam, atnr);
199 snew((*nbparam)[atnr-1], atnr);
203 snew((*pair)[atnr-1], atnr);
207 static void copy_B_from_A(int ftype, double *c)
211 nrfpA = NRFPA(ftype);
212 nrfpB = NRFPB(ftype);
214 /* Copy the B parameters from the first nrfpB A parameters */
215 for (i = 0; (i < nrfpB); i++)
221 void push_at (t_symtab *symtab, gpp_atomtype_t at, t_bond_atomtype bat,
222 char *line, int nb_funct,
223 t_nbparam ***nbparam, t_nbparam ***pair,
230 t_xlate xl[eptNR] = {
238 int nr, i, nfields, j, pt, nfp0 = -1;
239 int batype_nr, nread;
240 char type[STRLEN], btype[STRLEN], ptype[STRLEN];
242 double c[MAXFORCEPARAM];
243 char tmpfield[12][100]; /* Max 12 fields of width 100 */
247 bool have_atomic_number;
248 bool have_bonded_type;
253 /* First assign input line to temporary array */
254 nfields = sscanf(line, "%s%s%s%s%s%s%s%s%s%s%s%s",
255 tmpfield[0], tmpfield[1], tmpfield[2], tmpfield[3], tmpfield[4], tmpfield[5],
256 tmpfield[6], tmpfield[7], tmpfield[8], tmpfield[9], tmpfield[10], tmpfield[11]);
258 /* Comments on optional fields in the atomtypes section:
260 * The force field format is getting a bit old. For OPLS-AA we needed
261 * to add a special bonded atomtype, and for Gerrit Groenhofs QM/MM stuff
262 * we also needed the atomic numbers.
263 * To avoid making all old or user-generated force fields unusable we
264 * have introduced both these quantities as optional columns, and do some
265 * acrobatics to check whether they are present or not.
266 * This will all look much nicer when we switch to XML... sigh.
268 * Field 0 (mandatory) is the nonbonded type name. (string)
269 * Field 1 (optional) is the bonded type (string)
270 * Field 2 (optional) is the atomic number (int)
271 * Field 3 (mandatory) is the mass (numerical)
272 * Field 4 (mandatory) is the charge (numerical)
273 * Field 5 (mandatory) is the particle type (single character)
274 * This is followed by a number of nonbonded parameters.
276 * The safest way to identify the format is the particle type field.
278 * So, here is what we do:
280 * A. Read in the first six fields as strings
281 * B. If field 3 (starting from 0) is a single char, we have neither
282 * bonded_type or atomic numbers.
283 * C. If field 5 is a single char we have both.
284 * D. If field 4 is a single char we check field 1. If this begins with
285 * an alphabetical character we have bonded types, otherwise atomic numbers.
294 if ( (strlen(tmpfield[5]) == 1) && isalpha(tmpfield[5][0]) )
296 have_bonded_type = TRUE;
297 have_atomic_number = TRUE;
299 else if ( (strlen(tmpfield[3]) == 1) && isalpha(tmpfield[3][0]) )
301 have_bonded_type = FALSE;
302 have_atomic_number = FALSE;
306 have_bonded_type = ( isalpha(tmpfield[1][0]) != 0 );
307 have_atomic_number = !have_bonded_type;
310 /* optional fields */
319 if (have_atomic_number)
321 if (have_bonded_type)
323 nread = sscanf(line, "%s%s%d%lf%lf%s%lf%lf",
324 type, btype, &atomnr, &m, &q, ptype, &c[0], &c[1]);
333 /* have_atomic_number && !have_bonded_type */
334 nread = sscanf(line, "%s%d%lf%lf%s%lf%lf",
335 type, &atomnr, &m, &q, ptype, &c[0], &c[1]);
345 if (have_bonded_type)
347 /* !have_atomic_number && have_bonded_type */
348 nread = sscanf(line, "%s%s%lf%lf%s%lf%lf",
349 type, btype, &m, &q, ptype, &c[0], &c[1]);
358 /* !have_atomic_number && !have_bonded_type */
359 nread = sscanf(line, "%s%lf%lf%s%lf%lf",
360 type, &m, &q, ptype, &c[0], &c[1]);
369 if (!have_bonded_type)
374 if (!have_atomic_number)
384 if (have_atomic_number)
386 if (have_bonded_type)
388 nread = sscanf(line, "%s%s%d%lf%lf%s%lf%lf%lf",
389 type, btype, &atomnr, &m, &q, ptype, &c[0], &c[1], &c[2]);
398 /* have_atomic_number && !have_bonded_type */
399 nread = sscanf(line, "%s%d%lf%lf%s%lf%lf%lf",
400 type, &atomnr, &m, &q, ptype, &c[0], &c[1], &c[2]);
410 if (have_bonded_type)
412 /* !have_atomic_number && have_bonded_type */
413 nread = sscanf(line, "%s%s%lf%lf%s%lf%lf%lf",
414 type, btype, &m, &q, ptype, &c[0], &c[1], &c[2]);
423 /* !have_atomic_number && !have_bonded_type */
424 nread = sscanf(line, "%s%lf%lf%s%lf%lf%lf",
425 type, &m, &q, ptype, &c[0], &c[1], &c[2]);
434 if (!have_bonded_type)
439 if (!have_atomic_number)
447 auto message = gmx::formatString("Invalid function type %d in push_at", nb_funct);
448 warning_error_and_exit(wi, message, FARGS);
450 for (j = nfp0; (j < MAXFORCEPARAM); j++)
455 if (strlen(type) == 1 && isdigit(type[0]))
457 warning_error_and_exit(wi, "Atom type names can't be single digits.", FARGS);
460 if (strlen(btype) == 1 && isdigit(btype[0]))
462 warning_error_and_exit(wi, "Bond atom type names can't be single digits.", FARGS);
465 /* Hack to read old topologies */
466 if (gmx_strcasecmp(ptype, "D") == 0)
470 for (j = 0; (j < eptNR); j++)
472 if (gmx_strcasecmp(ptype, xl[j].entry) == 0)
479 auto message = gmx::formatString("Invalid particle type %s", ptype);
480 warning_error_and_exit(wi, message, FARGS);
487 for (i = 0; (i < MAXFORCEPARAM); i++)
492 if ((batype_nr = get_bond_atomtype_type(btype, bat)) == NOTSET)
494 add_bond_atomtype(bat, symtab, btype);
496 batype_nr = get_bond_atomtype_type(btype, bat);
498 if ((nr = get_atomtype_type(type, at)) != NOTSET)
500 auto message = gmx::formatString
501 ("Atomtype %s was defined previously (e.g. in the forcefield files), "
502 "and has now been defined again. This could happen e.g. if you would "
503 "use a self-contained molecule .itp file that duplicates or replaces "
504 "the contents of the standard force-field files. You should check "
505 "the contents of your files and remove such repetition. If you know "
506 "you should override the previous definition, then you could choose "
507 "to suppress this warning with -maxwarn.", type);
508 warning(wi, message);
509 if ((nr = set_atomtype(nr, at, symtab, atom, type, param, batype_nr,
512 auto message = gmx::formatString("Replacing atomtype %s failed", type);
513 warning_error_and_exit(wi, message, FARGS);
516 else if ((add_atomtype(at, symtab, atom, type, param,
517 batype_nr, atomnr)) == NOTSET)
519 auto message = gmx::formatString("Adding atomtype %s failed", type);
520 warning_error_and_exit(wi, message, FARGS);
524 /* Add space in the non-bonded parameters matrix */
525 realloc_nb_params(at, nbparam, pair);
531 //! Return whether the contents of \c a and \c b are the same, considering also reversed order.
532 template <typename T>
533 static bool equalEitherForwardOrBackward(gmx::ArrayRef<const T> a, gmx::ArrayRef<const T> b)
535 return (std::equal(a.begin(), a.end(), b.begin()) ||
536 std::equal(a.begin(), a.end(), b.rbegin()));
539 static void push_bondtype(t_params * bt,
548 int nrfp = NRFP(ftype);
550 /* If bAllowRepeat is TRUE, we allow multiple entries as long as they
551 are on directly _adjacent_ lines.
554 /* First check if our atomtypes are _identical_ (not reversed) to the previous
555 entry. If they are not identical we search for earlier duplicates. If they are
556 we can skip it, since we already searched for the first line
560 bool isContinuationOfBlock = false;
561 if (bAllowRepeat && nr > 1)
563 isContinuationOfBlock = true;
564 for (int j = 0; j < nral; j++)
566 if (b->a[j] != bt->param[nr - 2].a[j])
568 isContinuationOfBlock = false;
573 /* Search for earlier duplicates if this entry was not a continuation
574 from the previous line.
576 bool addBondType = true;
577 bool haveWarned = false;
578 bool haveErrored = false;
579 for (int i = 0; (i < nr); i++)
581 gmx::ArrayRef<const int> bParams(b->a, b->a + nral);
582 gmx::ArrayRef<const int> testParams(bt->param[i].a, bt->param[i].a + nral);
583 if (equalEitherForwardOrBackward(bParams, testParams))
585 GMX_ASSERT(nrfp <= MAXFORCEPARAM, "This is ensured in other places, but we need this assert to keep the clang analyzer happy");
586 const bool identicalParameters = std::equal(bt->param[i].c, bt->param[i].c + nrfp, b->c);
588 if (!bAllowRepeat || identicalParameters)
593 if (!identicalParameters)
597 /* With dihedral type 9 we only allow for repeating
598 * of the same parameters with blocks with 1 entry.
599 * Allowing overriding is too complex to check.
601 if (!isContinuationOfBlock && !haveErrored)
603 warning_error(wi, "Encountered a second block of parameters for dihedral "
604 "type 9 for the same atoms, with either different parameters "
605 "and/or the first block has multiple lines. This is not supported.");
609 else if (!haveWarned)
611 auto message = gmx::formatString
612 ("Bondtype %s was defined previously (e.g. in the forcefield files), "
613 "and has now been defined again. This could happen e.g. if you would "
614 "use a self-contained molecule .itp file that duplicates or replaces "
615 "the contents of the standard force-field files. You should check "
616 "the contents of your files and remove such repetition. If you know "
617 "you should override the previous definition, then you could choose "
618 "to suppress this warning with -maxwarn.%s",
619 interaction_function[ftype].longname,
621 "\nUse dihedraltype 9 to allow several multiplicity terms. Only consecutive "
622 "lines are combined. Non-consective lines overwrite each other."
624 warning(wi, message);
626 fprintf(stderr, " old: ");
627 for (int j = 0; (j < nrfp); j++)
629 fprintf(stderr, " %g", bt->param[i].c[j]);
631 fprintf(stderr, " \n new: %s\n\n", line);
637 if (!identicalParameters && !bAllowRepeat)
639 /* Overwrite the parameters with the latest ones */
640 // TODO considering improving the following code by replacing with:
641 // std::copy(b->c, b->c + nrfp, bt->param[i].c);
642 for (int j = 0; (j < nrfp); j++)
644 bt->param[i].c[j] = b->c[j];
655 /* fill the arrays up and down */
656 memcpy(bt->param[bt->nr].c, b->c, sizeof(b->c));
657 memcpy(bt->param[bt->nr].a, b->a, sizeof(b->a));
658 memcpy(bt->param[bt->nr+1].c, b->c, sizeof(b->c));
660 /* The definitions of linear angles depend on the order of atoms,
661 * that means that for atoms i-j-k, with certain parameter a, the
662 * corresponding k-j-i angle will have parameter 1-a.
664 if (ftype == F_LINEAR_ANGLES)
666 bt->param[bt->nr+1].c[0] = 1-bt->param[bt->nr+1].c[0];
667 bt->param[bt->nr+1].c[2] = 1-bt->param[bt->nr+1].c[2];
670 for (int j = 0; (j < nral); j++)
672 bt->param[bt->nr+1].a[j] = b->a[nral-1-j];
679 void push_bt(directive d, t_params bt[], int nral,
681 t_bond_atomtype bat, char *line,
684 const char *formal[MAXATOMLIST+1] = {
693 const char *formnl[MAXATOMLIST+1] = {
699 "%*s%*s%*s%*s%*s%*s",
700 "%*s%*s%*s%*s%*s%*s%*s"
702 const char *formlf = "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf";
703 int i, ft, ftype, nn, nrfp, nrfpA;
705 char alc[MAXATOMLIST+1][20];
706 /* One force parameter more, so we can check if we read too many */
707 double c[MAXFORCEPARAM+1];
710 if ((bat && at) || (!bat && !at))
712 gmx_incons("You should pass either bat or at to push_bt");
715 /* Make format string (nral ints+functype) */
716 if ((nn = sscanf(line, formal[nral],
717 alc[0], alc[1], alc[2], alc[3], alc[4], alc[5])) != nral+1)
719 auto message = gmx::formatString("Not enough atomtypes (%d instead of %d)", nn-1, nral);
720 warning_error(wi, message);
724 ft = strtol(alc[nral], nullptr, 10);
725 ftype = ifunc_index(d, ft);
727 nrfpA = interaction_function[ftype].nrfpA;
728 strcpy(f1, formnl[nral]);
730 if ((nn = sscanf(line, f1, &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7], &c[8], &c[9], &c[10], &c[11], &c[12]))
735 /* Copy the B-state from the A-state */
736 copy_B_from_A(ftype, c);
742 warning_error(wi, "Not enough parameters");
744 else if (nn > nrfpA && nn < nrfp)
746 warning_error(wi, "Too many parameters or not enough parameters for topology B");
750 warning_error(wi, "Too many parameters");
752 for (i = nn; (i < nrfp); i++)
758 for (i = 0; (i < nral); i++)
760 if (at && ((p.a[i] = get_atomtype_type(alc[i], at)) == NOTSET))
762 auto message = gmx::formatString("Unknown atomtype %s\n", alc[i]);
763 warning_error_and_exit(wi, message, FARGS);
765 else if (bat && ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET))
767 auto message = gmx::formatString("Unknown bond_atomtype %s\n", alc[i]);
768 warning_error_and_exit(wi, message, FARGS);
771 for (i = 0; (i < nrfp); i++)
775 push_bondtype (&(bt[ftype]), &p, nral, ftype, FALSE, line, wi);
779 void push_dihedraltype(directive d, t_params bt[],
780 t_bond_atomtype bat, char *line,
783 const char *formal[MAXATOMLIST+1] = {
792 const char *formnl[MAXATOMLIST+1] = {
798 "%*s%*s%*s%*s%*s%*s",
799 "%*s%*s%*s%*s%*s%*s%*s"
801 const char *formlf[MAXFORCEPARAM] = {
807 "%lf%lf%lf%lf%lf%lf",
808 "%lf%lf%lf%lf%lf%lf%lf",
809 "%lf%lf%lf%lf%lf%lf%lf%lf",
810 "%lf%lf%lf%lf%lf%lf%lf%lf%lf",
811 "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
812 "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
813 "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
815 int i, ft, ftype, nn, nrfp, nrfpA, nral;
817 char alc[MAXATOMLIST+1][20];
818 double c[MAXFORCEPARAM];
822 /* This routine accepts dihedraltypes defined from either 2 or 4 atoms.
824 * We first check for 2 atoms with the 3th column being an integer
825 * defining the type. If this isn't the case, we try it with 4 atoms
826 * and the 5th column defining the dihedral type.
828 nn = sscanf(line, formal[4], alc[0], alc[1], alc[2], alc[3], alc[4]);
829 if (nn >= 3 && strlen(alc[2]) == 1 && isdigit(alc[2][0]))
832 ft = strtol(alc[nral], nullptr, 10);
833 /* Move atom types around a bit and use 'X' for wildcard atoms
834 * to create a 4-atom dihedral definition with arbitrary atoms in
837 if (alc[2][0] == '2')
839 /* improper - the two atomtypes are 1,4. Use wildcards for 2,3 */
840 strcpy(alc[3], alc[1]);
841 sprintf(alc[2], "X");
842 sprintf(alc[1], "X");
843 /* alc[0] stays put */
847 /* proper - the two atomtypes are 2,3. Use wildcards for 1,4 */
848 sprintf(alc[3], "X");
849 strcpy(alc[2], alc[1]);
850 strcpy(alc[1], alc[0]);
851 sprintf(alc[0], "X");
854 else if (nn == 5 && strlen(alc[4]) == 1 && isdigit(alc[4][0]))
857 ft = strtol(alc[nral], nullptr, 10);
861 auto message = gmx::formatString("Incorrect number of atomtypes for dihedral (%d instead of 2 or 4)", nn-1);
862 warning_error(wi, message);
868 /* Previously, we have always overwritten parameters if e.g. a torsion
869 with the same atomtypes occurs on multiple lines. However, CHARMM and
870 some other force fields specify multiple dihedrals over some bonds,
871 including cosines with multiplicity 6 and somethimes even higher.
872 Thus, they cannot be represented with Ryckaert-Bellemans terms.
873 To add support for these force fields, Dihedral type 9 is identical to
874 normal proper dihedrals, but repeated entries are allowed.
881 bAllowRepeat = FALSE;
885 ftype = ifunc_index(d, ft);
887 nrfpA = interaction_function[ftype].nrfpA;
889 strcpy(f1, formnl[nral]);
890 strcat(f1, formlf[nrfp-1]);
892 /* Check number of parameters given */
893 if ((nn = sscanf(line, f1, &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7], &c[8], &c[9], &c[10], &c[11]))
898 /* Copy the B-state from the A-state */
899 copy_B_from_A(ftype, c);
905 warning_error(wi, "Not enough parameters");
907 else if (nn > nrfpA && nn < nrfp)
909 warning_error(wi, "Too many parameters or not enough parameters for topology B");
913 warning_error(wi, "Too many parameters");
915 for (i = nn; (i < nrfp); i++)
922 for (i = 0; (i < 4); i++)
924 if (!strcmp(alc[i], "X"))
930 if ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET)
932 auto message = gmx::formatString("Unknown bond_atomtype %s", alc[i]);
933 warning_error_and_exit(wi, message, FARGS);
937 for (i = 0; (i < nrfp); i++)
941 /* Always use 4 atoms here, since we created two wildcard atoms
942 * if there wasn't of them 4 already.
944 push_bondtype (&(bt[ftype]), &p, 4, ftype, bAllowRepeat, line, wi);
948 void push_nbt(directive d, t_nbparam **nbt, gpp_atomtype_t atype,
949 char *pline, int nb_funct,
953 const char *form3 = "%*s%*s%*s%lf%lf%lf";
954 const char *form4 = "%*s%*s%*s%lf%lf%lf%lf";
955 const char *form5 = "%*s%*s%*s%lf%lf%lf%lf%lf";
957 int i, f, n, ftype, nrfp;
964 if (sscanf (pline, "%s%s%d", a0, a1, &f) != 3)
970 ftype = ifunc_index(d, f);
972 if (ftype != nb_funct)
974 auto message = gmx::formatString("Trying to add %s while the default nonbond type is %s",
975 interaction_function[ftype].longname,
976 interaction_function[nb_funct].longname);
977 warning_error(wi, message);
981 /* Get the force parameters */
985 n = sscanf(pline, form4, &c[0], &c[1], &c[2], &c[3]);
991 /* When the B topology parameters are not set,
992 * copy them from topology A
994 GMX_ASSERT(nrfp <= 4, "LJ-14 cannot have more than 4 parameters");
995 for (i = n; i < nrfp; i++)
1000 else if (ftype == F_LJC14_Q)
1002 n = sscanf(pline, form5, &c[0], &c[1], &c[2], &c[3], &dum);
1005 incorrect_n_param(wi);
1011 if (sscanf(pline, form3, &c[0], &c[1], &dum) != 2)
1013 incorrect_n_param(wi);
1019 if (sscanf(pline, form4, &c[0], &c[1], &c[2], &dum) != 3)
1021 incorrect_n_param(wi);
1027 auto message = gmx::formatString("Number of force parameters for nonbonded interactions is %d", nrfp);
1028 warning_error_and_exit(wi, message, FARGS);
1030 for (i = 0; (i < nrfp); i++)
1035 /* Put the parameters in the matrix */
1036 if ((ai = get_atomtype_type (a0, atype)) == NOTSET)
1038 auto message = gmx::formatString("Atomtype %s not found", a0);
1039 warning_error_and_exit(wi, message, FARGS);
1041 if ((aj = get_atomtype_type (a1, atype)) == NOTSET)
1043 auto message = gmx::formatString("Atomtype %s not found", a1);
1044 warning_error_and_exit(wi, message, FARGS);
1046 nbp = &(nbt[std::max(ai, aj)][std::min(ai, aj)]);
1051 for (i = 0; i < nrfp; i++)
1053 bId = bId && (nbp->c[i] == cr[i]);
1057 auto message = gmx::formatString
1058 ("Non-bonded parameters were defined previously (e.g. in the forcefield files), "
1059 "and have now been defined again. This could happen e.g. if you would "
1060 "use a self-contained molecule .itp file that duplicates or replaces "
1061 "the contents of the standard force-field files. You should check "
1062 "the contents of your files and remove such repetition. If you know "
1063 "you should override the previous definitions, then you could choose "
1064 "to suppress this warning with -maxwarn.");
1065 warning(wi, message);
1066 fprintf(stderr, " old:");
1067 for (i = 0; i < nrfp; i++)
1069 fprintf(stderr, " %g", nbp->c[i]);
1071 fprintf(stderr, " new\n%s\n", pline);
1075 for (i = 0; i < nrfp; i++)
1082 push_cmaptype(directive d, t_params bt[], int nral, gpp_atomtype_t at,
1083 t_bond_atomtype bat, char *line,
1086 const char *formal = "%s%s%s%s%s%s%s%s%n";
1088 int i, ft, ftype, nn, nrfp, nrfpA, nrfpB;
1089 int start, nchar_consumed;
1090 int nxcmap, nycmap, ncmap, read_cmap, sl, nct;
1091 char s[20], alc[MAXATOMLIST+2][20];
1094 /* Keep the compiler happy */
1098 GMX_ASSERT(nral == 5, "CMAP requires 5 atoms per interaction");
1100 /* Here we can only check for < 8 */
1101 if ((nn = sscanf(line, formal, alc[0], alc[1], alc[2], alc[3], alc[4], alc[5], alc[6], alc[7], &nchar_consumed)) < nral+3)
1103 auto message = gmx::formatString("Incorrect number of atomtypes for cmap (%d instead of 5)", nn-1);
1104 warning_error(wi, message);
1107 start += nchar_consumed;
1109 ft = strtol(alc[nral], nullptr, 10);
1110 nxcmap = strtol(alc[nral+1], nullptr, 10);
1111 nycmap = strtol(alc[nral+2], nullptr, 10);
1113 /* Check for equal grid spacing in x and y dims */
1114 if (nxcmap != nycmap)
1116 auto message = gmx::formatString("Not the same grid spacing in x and y for cmap grid: x=%d, y=%d", nxcmap, nycmap);
1117 warning_error(wi, message);
1120 ncmap = nxcmap*nycmap;
1121 ftype = ifunc_index(d, ft);
1122 nrfpA = strtol(alc[6], nullptr, 10)*strtol(alc[6], nullptr, 10);
1123 nrfpB = strtol(alc[7], nullptr, 10)*strtol(alc[7], nullptr, 10);
1126 /* Allocate memory for the CMAP grid */
1127 bt[F_CMAP].ncmap += nrfp;
1128 srenew(bt[F_CMAP].cmap, bt[F_CMAP].ncmap);
1130 /* Read in CMAP parameters */
1132 for (i = 0; i < ncmap; i++)
1134 while (isspace(*(line+start+sl)))
1138 nn = sscanf(line+start+sl, " %s ", s);
1140 bt[F_CMAP].cmap[i+(bt[F_CMAP].ncmap)-nrfp] = strtod(s, nullptr);
1148 auto message = gmx::formatString("Error in reading cmap parameter for angle %s %s %s %s %s", alc[0], alc[1], alc[2], alc[3], alc[4]);
1149 warning_error(wi, message);
1154 /* Check do that we got the number of parameters we expected */
1155 if (read_cmap == nrfpA)
1157 for (i = 0; i < ncmap; i++)
1159 bt[F_CMAP].cmap[i+ncmap] = bt[F_CMAP].cmap[i];
1164 if (read_cmap < nrfpA)
1166 warning_error(wi, "Not enough cmap parameters");
1168 else if (read_cmap > nrfpA && read_cmap < nrfp)
1170 warning_error(wi, "Too many cmap parameters or not enough parameters for topology B");
1172 else if (read_cmap > nrfp)
1174 warning_error(wi, "Too many cmap parameters");
1179 /* Set grid spacing and the number of grids (we assume these numbers to be the same for all grids
1180 * so we can safely assign them each time
1182 bt[F_CMAP].grid_spacing = nxcmap; /* Or nycmap, they need to be equal */
1183 bt[F_CMAP].nc = bt[F_CMAP].nc + 1; /* Since we are incrementing here, we need to subtract later, see (*****) */
1184 nct = (nral+1) * bt[F_CMAP].nc;
1186 /* Allocate memory for the cmap_types information */
1187 srenew(bt[F_CMAP].cmap_types, nct);
1189 for (i = 0; (i < nral); i++)
1191 if (at && ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET))
1193 auto message = gmx::formatString("Unknown atomtype %s\n", alc[i]);
1194 warning_error(wi, message);
1196 else if (bat && ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET))
1198 auto message = gmx::formatString("Unknown bond_atomtype %s\n", alc[i]);
1199 warning_error(wi, message);
1202 /* Assign a grid number to each cmap_type */
1203 bt[F_CMAP].cmap_types[bt[F_CMAP].nct++] = get_bond_atomtype_type(alc[i], bat);
1206 /* Assign a type number to this cmap */
1207 bt[F_CMAP].cmap_types[bt[F_CMAP].nct++] = bt[F_CMAP].nc-1; /* Since we inremented earlier, we need to subtrac here, to get the types right (****) */
1209 /* Check for the correct number of atoms (again) */
1210 if (bt[F_CMAP].nct != nct)
1212 auto message = gmx::formatString("Incorrect number of atom types (%d) in cmap type %d\n", nct, bt[F_CMAP].nc);
1213 warning_error(wi, message);
1216 /* Is this correct?? */
1217 for (i = 0; i < MAXFORCEPARAM; i++)
1222 /* Push the bond to the bondlist */
1223 push_bondtype (&(bt[ftype]), &p, nral, ftype, FALSE, line, wi);
1227 static void push_atom_now(t_symtab *symtab, t_atoms *at, int atomnr,
1229 int type, char *ctype, int ptype,
1231 char *resname, char *name, real m0, real q0,
1232 int typeB, char *ctypeB, real mB, real qB,
1235 int j, resind = 0, resnr;
1239 if (((nr == 0) && (atomnr != 1)) || (nr && (atomnr != at->nr+1)))
1241 auto message = gmx::formatString
1242 ("Atoms in the .top are not numbered consecutively from 1 (rather, "
1243 "atomnr = %d, while at->nr = %d)", atomnr, at->nr);
1244 warning_error_and_exit(wi, message, FARGS);
1247 j = strlen(resnumberic) - 1;
1248 if (isdigit(resnumberic[j]))
1254 ric = resnumberic[j];
1255 if (j == 0 || !isdigit(resnumberic[j-1]))
1257 auto message = gmx::formatString("Invalid residue number '%s' for atom %d",
1258 resnumberic, atomnr);
1259 warning_error_and_exit(wi, message, FARGS);
1262 resnr = strtol(resnumberic, nullptr, 10);
1266 resind = at->atom[nr-1].resind;
1268 if (nr == 0 || strcmp(resname, *at->resinfo[resind].name) != 0 ||
1269 resnr != at->resinfo[resind].nr ||
1270 ric != at->resinfo[resind].ic)
1280 at->nres = resind + 1;
1281 srenew(at->resinfo, at->nres);
1282 at->resinfo[resind].name = put_symtab(symtab, resname);
1283 at->resinfo[resind].nr = resnr;
1284 at->resinfo[resind].ic = ric;
1288 resind = at->atom[at->nr-1].resind;
1291 /* New atom instance
1292 * get new space for arrays
1294 srenew(at->atom, nr+1);
1295 srenew(at->atomname, nr+1);
1296 srenew(at->atomtype, nr+1);
1297 srenew(at->atomtypeB, nr+1);
1300 at->atom[nr].type = type;
1301 at->atom[nr].ptype = ptype;
1302 at->atom[nr].q = q0;
1303 at->atom[nr].m = m0;
1304 at->atom[nr].typeB = typeB;
1305 at->atom[nr].qB = qB;
1306 at->atom[nr].mB = mB;
1308 at->atom[nr].resind = resind;
1309 at->atom[nr].atomnumber = atomicnumber;
1310 at->atomname[nr] = put_symtab(symtab, name);
1311 at->atomtype[nr] = put_symtab(symtab, ctype);
1312 at->atomtypeB[nr] = put_symtab(symtab, ctypeB);
1316 static void push_cg(t_block *block, int *lastindex, int index, int a)
1318 if (((block->nr) && (*lastindex != index)) || (!block->nr))
1320 /* add a new block */
1322 srenew(block->index, block->nr+1);
1324 block->index[block->nr] = a + 1;
1328 void push_atom(t_symtab *symtab, t_block *cgs,
1329 t_atoms *at, gpp_atomtype_t atype, char *line, int *lastcg,
1333 int cgnumber, atomnr, type, typeB, nscan;
1334 char id[STRLEN], ctype[STRLEN], ctypeB[STRLEN],
1335 resnumberic[STRLEN], resname[STRLEN], name[STRLEN], check[STRLEN];
1336 double m, q, mb, qb;
1337 real m0, q0, mB, qB;
1339 /* Make a shortcut for writing in this molecule */
1342 /* Fixed parameters */
1343 if (sscanf(line, "%s%s%s%s%s%d",
1344 id, ctype, resnumberic, resname, name, &cgnumber) != 6)
1349 sscanf(id, "%d", &atomnr);
1350 if ((type = get_atomtype_type(ctype, atype)) == NOTSET)
1352 auto message = gmx::formatString("Atomtype %s not found", ctype);
1353 warning_error_and_exit(wi, message, FARGS);
1355 ptype = get_atomtype_ptype(type, atype);
1357 /* Set default from type */
1358 q0 = get_atomtype_qA(type, atype);
1359 m0 = get_atomtype_massA(type, atype);
1364 /* Optional parameters */
1365 nscan = sscanf(line, "%*s%*s%*s%*s%*s%*s%lf%lf%s%lf%lf%s",
1366 &q, &m, ctypeB, &qb, &mb, check);
1368 /* Nasty switch that falls thru all the way down! */
1377 if ((typeB = get_atomtype_type(ctypeB, atype)) == NOTSET)
1379 auto message = gmx::formatString("Atomtype %s not found", ctypeB);
1380 warning_error_and_exit(wi, message, FARGS);
1382 qB = get_atomtype_qA(typeB, atype);
1383 mB = get_atomtype_massA(typeB, atype);
1392 warning_error(wi, "Too many parameters");
1400 push_cg(cgs, lastcg, cgnumber, nr);
1402 push_atom_now(symtab, at, atomnr, get_atomtype_atomnumber(type, atype),
1403 type, ctype, ptype, resnumberic,
1404 resname, name, m0, q0, typeB,
1405 typeB == type ? ctype : ctypeB, mB, qB, wi);
1408 void push_molt(t_symtab *symtab, int *nmol, t_molinfo **mol, char *line,
1415 if ((sscanf(line, "%s%d", type, &nrexcl)) != 2)
1417 warning_error(wi, "Expected a molecule type name and nrexcl");
1420 /* Test if this moleculetype overwrites another */
1424 if (strcmp(*((*mol)[i].name), type) == 0)
1426 auto message = gmx::formatString("moleculetype %s is redefined", type);
1427 warning_error_and_exit(wi, message, FARGS);
1433 srenew(*mol, *nmol);
1434 newmol = &((*mol)[*nmol-1]);
1435 init_molinfo(newmol);
1437 /* Fill in the values */
1438 newmol->name = put_symtab(symtab, type);
1439 newmol->nrexcl = nrexcl;
1440 newmol->excl_set = FALSE;
1443 static bool default_nb_params(int ftype, t_params bt[], t_atoms *at,
1444 t_param *p, int c_start, bool bB, bool bGenPairs)
1446 int i, j, ti, tj, ntype;
1448 t_param *pi = nullptr;
1449 int nr = bt[ftype].nr;
1450 int nral = NRAL(ftype);
1451 int nrfp = interaction_function[ftype].nrfpA;
1452 int nrfpB = interaction_function[ftype].nrfpB;
1454 if ((!bB && nrfp == 0) || (bB && nrfpB == 0))
1462 /* First test the generated-pair position to save
1463 * time when we have 1000*1000 entries for e.g. OPLS...
1465 ntype = static_cast<int>(std::sqrt(static_cast<double>(nr)));
1466 GMX_ASSERT(ntype * ntype == nr, "Number of pairs of generated non-bonded parameters should be a perfect square");
1469 ti = at->atom[p->a[0]].typeB;
1470 tj = at->atom[p->a[1]].typeB;
1474 ti = at->atom[p->a[0]].type;
1475 tj = at->atom[p->a[1]].type;
1477 pi = &(bt[ftype].param[ntype*ti+tj]);
1478 bFound = ((ti == pi->a[0]) && (tj == pi->a[1]));
1481 /* Search explicitly if we didnt find it */
1484 for (i = 0; ((i < nr) && !bFound); i++)
1486 pi = &(bt[ftype].param[i]);
1489 for (j = 0; ((j < nral) &&
1490 (at->atom[p->a[j]].typeB == pi->a[j])); j++)
1497 for (j = 0; ((j < nral) &&
1498 (at->atom[p->a[j]].type == pi->a[j])); j++)
1503 bFound = (j == nral);
1511 if (nrfp+nrfpB > MAXFORCEPARAM)
1513 gmx_incons("Too many force parameters");
1515 for (j = c_start; (j < nrfpB); j++)
1517 p->c[nrfp+j] = pi->c[j];
1522 for (j = c_start; (j < nrfp); j++)
1530 for (j = c_start; (j < nrfp); j++)
1538 static bool default_cmap_params(t_params bondtype[],
1539 t_atoms *at, gpp_atomtype_t atype,
1540 t_param *p, bool bB,
1541 int *cmap_type, int *nparam_def,
1544 int i, nparam_found;
1546 bool bFound = FALSE;
1551 /* Match the current cmap angle against the list of cmap_types */
1552 for (i = 0; i < bondtype[F_CMAP].nct && !bFound; i += 6)
1561 (get_atomtype_batype(at->atom[p->a[0]].type, atype) == bondtype[F_CMAP].cmap_types[i]) &&
1562 (get_atomtype_batype(at->atom[p->a[1]].type, atype) == bondtype[F_CMAP].cmap_types[i+1]) &&
1563 (get_atomtype_batype(at->atom[p->a[2]].type, atype) == bondtype[F_CMAP].cmap_types[i+2]) &&
1564 (get_atomtype_batype(at->atom[p->a[3]].type, atype) == bondtype[F_CMAP].cmap_types[i+3]) &&
1565 (get_atomtype_batype(at->atom[p->a[4]].type, atype) == bondtype[F_CMAP].cmap_types[i+4]))
1567 /* Found cmap torsion */
1569 ct = bondtype[F_CMAP].cmap_types[i+5];
1575 /* If we did not find a matching type for this cmap torsion */
1578 auto message = gmx::formatString("Unknown cmap torsion between atoms %d %d %d %d %d",
1579 p->a[0]+1, p->a[1]+1, p->a[2]+1, p->a[3]+1, p->a[4]+1);
1580 warning_error_and_exit(wi, message, FARGS);
1583 *nparam_def = nparam_found;
1589 /* Returns the number of exact atom type matches, i.e. non wild-card matches,
1590 * returns -1 when there are no matches at all.
1592 static int natom_match(t_param *pi,
1593 int type_i, int type_j, int type_k, int type_l,
1594 const gpp_atomtype* atype)
1596 if ((pi->ai() == -1 || get_atomtype_batype(type_i, atype) == pi->ai()) &&
1597 (pi->aj() == -1 || get_atomtype_batype(type_j, atype) == pi->aj()) &&
1598 (pi->ak() == -1 || get_atomtype_batype(type_k, atype) == pi->ak()) &&
1599 (pi->al() == -1 || get_atomtype_batype(type_l, atype) == pi->al()))
1602 (pi->ai() == -1 ? 0 : 1) +
1603 (pi->aj() == -1 ? 0 : 1) +
1604 (pi->ak() == -1 ? 0 : 1) +
1605 (pi->al() == -1 ? 0 : 1);
1613 static bool default_params(int ftype, t_params bt[],
1614 t_atoms *at, gpp_atomtype_t atype,
1615 t_param *p, bool bB,
1616 t_param **param_def,
1621 t_param *pi = nullptr;
1622 t_param *pj = nullptr;
1623 int nr = bt[ftype].nr;
1624 int nral = NRAL(ftype);
1625 int nrfpA = interaction_function[ftype].nrfpA;
1626 int nrfpB = interaction_function[ftype].nrfpB;
1628 if ((!bB && nrfpA == 0) || (bB && nrfpB == 0))
1636 if (ftype == F_PDIHS || ftype == F_RBDIHS || ftype == F_IDIHS || ftype == F_PIDIHS)
1638 int nmatch_max = -1;
1642 /* For dihedrals we allow wildcards. We choose the first type
1643 * that has the most real matches, i.e. non-wildcard matches.
1645 for (t = 0; ((t < nr) && nmatch_max < 4); t++)
1650 pt = &(bt[ftype].param[t]);
1653 nmatch = natom_match(pt, at->atom[p->ai()].typeB, at->atom[p->aj()].typeB, at->atom[p->ak()].typeB, at->atom[p->al()].typeB, atype);
1657 nmatch = natom_match(pt, at->atom[p->ai()].type, at->atom[p->aj()].type, at->atom[p->ak()].type, at->atom[p->al()].type, atype);
1659 if (nmatch > nmatch_max)
1661 nmatch_max = nmatch;
1671 pi = &(bt[ftype].param[i]);
1674 /* Find additional matches for this dihedral - necessary
1676 * The rule in that case is that additional matches
1677 * HAVE to be on adjacent lines!
1680 /* Continue from current i value */
1681 for (j = i + 2; j < nr && bSame; j += 2)
1683 pj = &(bt[ftype].param[j]);
1684 bSame = (pi->ai() == pj->ai() && pi->aj() == pj->aj() && pi->ak() == pj->ak() && pi->al() == pj->al());
1689 /* nparam_found will be increased as long as the numbers match */
1693 else /* Not a dihedral */
1697 for (i = 0; ((i < nr) && !bFound); i++)
1699 pi = &(bt[ftype].param[i]);
1702 for (j = 0; ((j < nral) &&
1703 (get_atomtype_batype(at->atom[p->a[j]].typeB, atype) == pi->a[j])); j++)
1710 for (j = 0; ((j < nral) &&
1711 (get_atomtype_batype(at->atom[p->a[j]].type, atype) == pi->a[j])); j++)
1716 bFound = (j == nral);
1725 *nparam_def = nparam_found;
1732 void push_bond(directive d, t_params bondtype[], t_params bond[],
1733 t_atoms *at, gpp_atomtype_t atype, char *line,
1734 bool bBonded, bool bGenPairs, real fudgeQQ,
1735 bool bZero, bool *bWarn_copy_A_B,
1738 const char *aaformat[MAXATOMLIST] = {
1746 const char *asformat[MAXATOMLIST] = {
1751 "%*s%*s%*s%*s%*s%*s",
1752 "%*s%*s%*s%*s%*s%*s%*s"
1754 const char *ccformat = "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf";
1755 int nr, i, j, nral, nral_fmt, nread, ftype;
1756 char format[STRLEN];
1757 /* One force parameter more, so we can check if we read too many */
1758 double cc[MAXFORCEPARAM+1];
1759 int aa[MAXATOMLIST+1];
1760 t_param param, *param_defA, *param_defB;
1761 bool bFoundA = FALSE, bFoundB = FALSE, bDef, bPert, bSwapParity = FALSE;
1762 int nparam_defA, nparam_defB;
1764 nparam_defA = nparam_defB = 0;
1766 ftype = ifunc_index(d, 1);
1768 for (j = 0; j < MAXATOMLIST; j++)
1772 bDef = (NRFP(ftype) > 0);
1774 if (ftype == F_SETTLE)
1776 /* SETTLE acts on 3 atoms, but the topology format only specifies
1777 * the first atom (for historical reasons).
1786 nread = sscanf(line, aaformat[nral_fmt-1],
1787 &aa[0], &aa[1], &aa[2], &aa[3], &aa[4], &aa[5]);
1789 if (ftype == F_SETTLE)
1796 if (nread < nral_fmt)
1801 else if (nread > nral_fmt)
1803 /* this is a hack to allow for virtual sites with swapped parity */
1804 bSwapParity = (aa[nral] < 0);
1807 aa[nral] = -aa[nral];
1809 ftype = ifunc_index(d, aa[nral]);
1818 auto message = gmx::formatString("Negative function types only allowed for %s and %s",
1819 interaction_function[F_VSITE3FAD].longname,
1820 interaction_function[F_VSITE3OUT].longname);
1821 warning_error_and_exit(wi, message, FARGS);
1827 /* Check for double atoms and atoms out of bounds */
1828 for (i = 0; (i < nral); i++)
1830 if (aa[i] < 1 || aa[i] > at->nr)
1832 auto message = gmx::formatString
1833 ("Atom index (%d) in %s out of bounds (1-%d).\n"
1834 "This probably means that you have inserted topology section \"%s\"\n"
1835 "in a part belonging to a different molecule than you intended to.\n"
1836 "In that case move the \"%s\" section to the right molecule.",
1837 aa[i], dir2str(d), at->nr, dir2str(d), dir2str(d));
1838 warning_error_and_exit(wi, message, FARGS);
1840 for (j = i+1; (j < nral); j++)
1842 GMX_ASSERT(j < MAXATOMLIST + 1, "Values from nral=NRAL() will satisfy this, we assert to keep gcc 4 happy");
1845 auto message = gmx::formatString("Duplicate atom index (%d) in %s", aa[i], dir2str(d));
1846 if (ftype == F_ANGRES)
1848 /* Since the angle restraints uses 2 pairs of atoms to
1849 * defines an angle between vectors, it can be useful
1850 * to use one atom twice, so we only issue a note here.
1852 warning_note(wi, message);
1856 warning_error(wi, message);
1862 /* default force parameters */
1863 for (j = 0; (j < MAXATOMLIST); j++)
1865 param.a[j] = aa[j]-1;
1867 for (j = 0; (j < MAXFORCEPARAM); j++)
1872 /* Get force params for normal and free energy perturbation
1873 * studies, as determined by types!
1878 bFoundA = default_params(ftype, bondtype, at, atype, ¶m, FALSE, ¶m_defA, &nparam_defA);
1881 /* Copy the A-state and B-state default parameters. */
1882 GMX_ASSERT(NRFPA(ftype)+NRFPB(ftype) <= MAXFORCEPARAM, "Bonded interactions may have at most 12 parameters");
1883 for (j = 0; (j < NRFPA(ftype)+NRFPB(ftype)); j++)
1885 param.c[j] = param_defA->c[j];
1888 bFoundB = default_params(ftype, bondtype, at, atype, ¶m, TRUE, ¶m_defB, &nparam_defB);
1891 /* Copy only the B-state default parameters */
1892 for (j = NRFPA(ftype); (j < NRFP(ftype)); j++)
1894 param.c[j] = param_defB->c[j];
1898 else if (ftype == F_LJ14)
1900 bFoundA = default_nb_params(ftype, bondtype, at, ¶m, 0, FALSE, bGenPairs);
1901 bFoundB = default_nb_params(ftype, bondtype, at, ¶m, 0, TRUE, bGenPairs);
1903 else if (ftype == F_LJC14_Q)
1905 param.c[0] = fudgeQQ;
1906 /* Fill in the A-state charges as default parameters */
1907 param.c[1] = at->atom[param.a[0]].q;
1908 param.c[2] = at->atom[param.a[1]].q;
1909 /* The default LJ parameters are the standard 1-4 parameters */
1910 bFoundA = default_nb_params(F_LJ14, bondtype, at, ¶m, 3, FALSE, bGenPairs);
1913 else if (ftype == F_LJC_PAIRS_NB)
1915 /* Defaults are not supported here */
1921 gmx_incons("Unknown function type in push_bond");
1924 if (nread > nral_fmt)
1926 /* Manually specified parameters - in this case we discard multiple torsion info! */
1928 strcpy(format, asformat[nral_fmt-1]);
1929 strcat(format, ccformat);
1931 nread = sscanf(line, format, &cc[0], &cc[1], &cc[2], &cc[3], &cc[4], &cc[5],
1932 &cc[6], &cc[7], &cc[8], &cc[9], &cc[10], &cc[11], &cc[12]);
1934 if ((nread == NRFPA(ftype)) && (NRFPB(ftype) != 0))
1936 /* We only have to issue a warning if these atoms are perturbed! */
1938 for (j = 0; (j < nral); j++)
1940 bPert = bPert || PERTURBED(at->atom[param.a[j]]);
1943 if (bPert && *bWarn_copy_A_B)
1945 auto message = gmx::formatString("Some parameters for bonded interaction involving "
1946 "perturbed atoms are specified explicitly in "
1947 "state A, but not B - copying A to B");
1948 warning(wi, message);
1949 *bWarn_copy_A_B = FALSE;
1952 /* If only the A parameters were specified, copy them to the B state */
1953 /* The B-state parameters correspond to the first nrfpB
1954 * A-state parameters.
1956 for (j = 0; (j < NRFPB(ftype)); j++)
1958 cc[nread++] = cc[j];
1962 /* If nread was 0 or EOF, no parameters were read => use defaults.
1963 * If nread was nrfpA we copied above so nread=nrfp.
1964 * If nread was nrfp we are cool.
1965 * For F_LJC14_Q we allow supplying fudgeQQ only.
1966 * Anything else is an error!
1968 if ((nread != 0) && (nread != EOF) && (nread != NRFP(ftype)) &&
1969 !(ftype == F_LJC14_Q && nread == 1))
1971 auto message = gmx::formatString
1972 ("Incorrect number of parameters - found %d, expected %d "
1973 "or %d for %s (after the function type).",
1974 nread, NRFPA(ftype), NRFP(ftype), interaction_function[ftype].longname);
1975 warning_error_and_exit(wi, message, FARGS);
1978 for (j = 0; (j < nread); j++)
1983 /* Check whether we have to use the defaults */
1984 if (nread == NRFP(ftype))
1993 /* nread now holds the number of force parameters read! */
1998 /* When we have multiple terms it would be very dangerous to allow perturbations to a different atom type! */
1999 if (ftype == F_PDIHS)
2001 if ((nparam_defA != nparam_defB) || ((nparam_defA > 1 || nparam_defB > 1) && (param_defA != param_defB)))
2004 gmx::formatString("Cannot automatically perturb a torsion with multiple terms to different form.\n"
2005 "Please specify perturbed parameters manually for this torsion in your topology!");
2006 warning_error(wi, message);
2010 if (nread > 0 && nread < NRFPA(ftype))
2012 /* Issue an error, do not use defaults */
2013 auto message = gmx::formatString("Not enough parameters, there should be at least %d (or 0 for defaults)", NRFPA(ftype));
2014 warning_error(wi, message);
2017 if (nread == 0 || nread == EOF)
2021 if (interaction_function[ftype].flags & IF_VSITE)
2023 /* set them to NOTSET, will be calculated later */
2024 for (j = 0; (j < MAXFORCEPARAM); j++)
2026 param.c[j] = NOTSET;
2031 param.c1() = -1; /* flag to swap parity of vsite construction */
2038 fprintf(stderr, "NOTE: No default %s types, using zeroes\n",
2039 interaction_function[ftype].longname);
2043 auto message = gmx::formatString("No default %s types", interaction_function[ftype].longname);
2044 warning_error(wi, message);
2055 param.c0() = 360 - param.c0();
2058 param.c2() = -param.c2();
2065 /* We only have to issue a warning if these atoms are perturbed! */
2067 for (j = 0; (j < nral); j++)
2069 bPert = bPert || PERTURBED(at->atom[param.a[j]]);
2074 auto message = gmx::formatString("No default %s types for perturbed atoms, "
2075 "using normal values", interaction_function[ftype].longname);
2076 warning(wi, message);
2082 if ((ftype == F_PDIHS || ftype == F_ANGRES || ftype == F_ANGRESZ)
2083 && param.c[5] != param.c[2])
2085 auto message = gmx::formatString("%s multiplicity can not be perturbed %f!=%f",
2086 interaction_function[ftype].longname,
2087 param.c[2], param.c[5]);
2088 warning_error_and_exit(wi, message, FARGS);
2091 if (IS_TABULATED(ftype) && param.c[2] != param.c[0])
2093 auto message = gmx::formatString("%s table number can not be perturbed %d!=%d",
2094 interaction_function[ftype].longname,
2095 gmx::roundToInt(param.c[0]), gmx::roundToInt(param.c[2]));
2096 warning_error_and_exit(wi, message, FARGS);
2099 /* Dont add R-B dihedrals where all parameters are zero (no interaction) */
2100 if (ftype == F_RBDIHS)
2103 for (i = 0; i < NRFP(ftype); i++)
2105 if (param.c[i] != 0)
2116 /* Put the values in the appropriate arrays */
2117 add_param_to_list (&bond[ftype], ¶m);
2119 /* Push additional torsions from FF for ftype==9 if we have them.
2120 * We have already checked that the A/B states do not differ in this case,
2121 * so we do not have to double-check that again, or the vsite stuff.
2122 * In addition, those torsions cannot be automatically perturbed.
2124 if (bDef && ftype == F_PDIHS)
2126 for (i = 1; i < nparam_defA; i++)
2128 /* Advance pointer! */
2130 for (j = 0; (j < NRFPA(ftype)+NRFPB(ftype)); j++)
2132 param.c[j] = param_defA->c[j];
2134 /* And push the next term for this torsion */
2135 add_param_to_list (&bond[ftype], ¶m);
2140 void push_cmap(directive d, t_params bondtype[], t_params bond[],
2141 t_atoms *at, gpp_atomtype_t atype, char *line,
2144 const char *aaformat[MAXATOMLIST+1] =
2155 int i, j, ftype, nral, nread, ncmap_params;
2157 int aa[MAXATOMLIST+1];
2161 ftype = ifunc_index(d, 1);
2165 nread = sscanf(line, aaformat[nral-1],
2166 &aa[0], &aa[1], &aa[2], &aa[3], &aa[4], &aa[5]);
2173 else if (nread == nral)
2175 ftype = ifunc_index(d, 1);
2178 /* Check for double atoms and atoms out of bounds */
2179 for (i = 0; i < nral; i++)
2181 if (aa[i] < 1 || aa[i] > at->nr)
2183 auto message = gmx::formatString
2184 ("Atom index (%d) in %s out of bounds (1-%d).\n"
2185 "This probably means that you have inserted topology section \"%s\"\n"
2186 "in a part belonging to a different molecule than you intended to.\n"
2187 "In that case move the \"%s\" section to the right molecule.",
2188 aa[i], dir2str(d), at->nr, dir2str(d), dir2str(d));
2189 warning_error_and_exit(wi, message, FARGS);
2192 for (j = i+1; (j < nral); j++)
2196 auto message = gmx::formatString("Duplicate atom index (%d) in %s", aa[i], dir2str(d));
2197 warning_error(wi, message);
2202 /* default force parameters */
2203 for (j = 0; (j < MAXATOMLIST); j++)
2205 param.a[j] = aa[j]-1;
2207 for (j = 0; (j < MAXFORCEPARAM); j++)
2212 /* Get the cmap type for this cmap angle */
2213 bFound = default_cmap_params(bondtype, at, atype, ¶m, FALSE, &cmap_type, &ncmap_params, wi);
2215 /* We want exactly one parameter (the cmap type in state A (currently no state B) back */
2216 if (bFound && ncmap_params == 1)
2218 /* Put the values in the appropriate arrays */
2219 param.c[0] = cmap_type;
2220 add_param_to_list(&bond[ftype], ¶m);
2224 /* This is essentially the same check as in default_cmap_params() done one more time */
2225 auto message = gmx::formatString("Unable to assign a cmap type to torsion %d %d %d %d and %d\n",
2226 param.a[0]+1, param.a[1]+1, param.a[2]+1, param.a[3]+1, param.a[4]+1);
2227 warning_error_and_exit(wi, message, FARGS);
2233 void push_vsitesn(directive d, t_params bond[],
2234 t_atoms *at, char *line,
2238 int type, ftype, j, n, ret, nj, a;
2240 double *weight = nullptr, weight_tot;
2243 /* default force parameters */
2244 for (j = 0; (j < MAXATOMLIST); j++)
2246 param.a[j] = NOTSET;
2248 for (j = 0; (j < MAXFORCEPARAM); j++)
2254 ret = sscanf(ptr, "%d%n", &a, &n);
2258 auto message = gmx::formatString("Expected an atom index in section \"%s\"",
2260 warning_error_and_exit(wi, message, FARGS);
2265 sscanf(ptr, "%d%n", &type, &n);
2267 ftype = ifunc_index(d, type);
2273 ret = sscanf(ptr, "%d%n", &a, &n);
2280 srenew(weight, nj+20);
2289 /* Here we use the A-state mass as a parameter.
2290 * Note that the B-state mass has no influence.
2292 weight[nj] = at->atom[atc[nj]].m;
2296 ret = sscanf(ptr, "%lf%n", &(weight[nj]), &n);
2300 auto message = gmx::formatString
2301 ("No weight or negative weight found for vsiten "
2302 "constructing atom %d (atom index %d)",
2304 warning_error_and_exit(wi, message, FARGS);
2308 auto message = gmx::formatString("Unknown vsiten type %d", type);
2309 warning_error_and_exit(wi, message, FARGS);
2311 weight_tot += weight[nj];
2319 auto message = gmx::formatString("Expected more than one atom index in section \"%s\"",
2321 warning_error_and_exit(wi, message, FARGS);
2324 if (weight_tot == 0)
2326 warning_error_and_exit(wi, "The total mass of the construting atoms is zero", FARGS);
2329 for (j = 0; j < nj; j++)
2331 param.a[1] = atc[j];
2333 param.c[1] = weight[j]/weight_tot;
2334 /* Put the values in the appropriate arrays */
2335 add_param_to_list (&bond[ftype], ¶m);
2342 void push_mol(int nrmols, t_molinfo mols[], char *pline, int *whichmol,
2348 if (sscanf(pline, "%s%d", type, nrcopies) != 2)
2354 /* Search moleculename.
2355 * Here we originally only did case insensitive matching. But because
2356 * some PDB files can have many chains and use case to generate more
2357 * chain-identifiers, which in turn end up in our moleculetype name,
2358 * we added support for case-sensitivity.
2364 for (int i = 0; i < nrmols; i++)
2366 if (strcmp(type, *(mols[i].name)) == 0)
2371 if (gmx_strcasecmp(type, *(mols[i].name)) == 0)
2380 // select the case sensitive match
2381 *whichmol = matchcs;
2385 // avoid matching case-insensitive when we have multiple matches
2388 auto message = gmx::formatString
2389 ("For moleculetype '%s' in [ system ] %d case insensitive "
2390 "matches, but %d case sensitive matches were found. Check "
2391 "the case of the characters in the moleculetypes.",
2393 warning_error_and_exit(wi, message, FARGS);
2397 // select the unique case insensitive match
2398 *whichmol = matchci;
2402 auto message = gmx::formatString("No such moleculetype %s", type);
2403 warning_error_and_exit(wi, message, FARGS);
2408 void push_excl(char *line, gmx::ExclusionBlocks *b2, warninp_t wi)
2412 char base[STRLEN], format[STRLEN];
2414 if (sscanf(line, "%d", &i) == 0)
2419 if ((1 <= i) && (i <= b2->nr))
2427 strcpy(base, "%*d");
2430 strcpy(format, base);
2431 strcat(format, "%d");
2432 n = sscanf(line, format, &j);
2435 if ((1 <= j) && (j <= b2->nr))
2438 srenew(b2->a[i], ++(b2->nra[i]));
2439 b2->a[i][b2->nra[i]-1] = j;
2440 /* also add the reverse exclusion! */
2441 srenew(b2->a[j], ++(b2->nra[j]));
2442 b2->a[j][b2->nra[j]-1] = i;
2443 strcat(base, "%*d");
2447 auto message = gmx::formatString("Invalid Atomnr j: %d, b2->nr: %d\n", j, b2->nr);
2448 warning_error_and_exit(wi, message, FARGS);
2455 int add_atomtype_decoupled(t_symtab *symtab, gpp_atomtype_t at,
2456 t_nbparam ***nbparam, t_nbparam ***pair)
2462 /* Define an atom type with all parameters set to zero (no interactions) */
2465 /* Type for decoupled atoms could be anything,
2466 * this should be changed automatically later when required.
2468 atom.ptype = eptAtom;
2469 for (i = 0; (i < MAXFORCEPARAM); i++)
2474 nr = add_atomtype(at, symtab, &atom, "decoupled", ¶m, -1, 0);
2476 /* Add space in the non-bonded parameters matrix */
2477 realloc_nb_params(at, nbparam, pair);
2482 static void convert_pairs_to_pairsQ(t_params *plist,
2483 real fudgeQQ, t_atoms *atoms)
2485 t_param *paramp1, *paramp2, *paramnew;
2486 int i, j, p1nr, p2nr, p2newnr;
2488 /* Add the pair list to the pairQ list */
2489 p1nr = plist[F_LJ14].nr;
2490 p2nr = plist[F_LJC14_Q].nr;
2491 p2newnr = p1nr + p2nr;
2492 snew(paramnew, p2newnr);
2494 paramp1 = plist[F_LJ14].param;
2495 paramp2 = plist[F_LJC14_Q].param;
2497 /* Fill in the new F_LJC14_Q array with the old one. NOTE:
2498 it may be possible to just ADD the converted F_LJ14 array
2499 to the old F_LJC14_Q array, but since we have to create
2500 a new sized memory structure, better just to deep copy it all.
2503 for (i = 0; i < p2nr; i++)
2505 /* Copy over parameters */
2506 for (j = 0; j < 5; j++) /* entries are 0-4 for F_LJC14_Q */
2508 paramnew[i].c[j] = paramp2[i].c[j];
2511 /* copy over atoms */
2512 for (j = 0; j < 2; j++)
2514 paramnew[i].a[j] = paramp2[i].a[j];
2518 for (i = p2nr; i < p2newnr; i++)
2522 /* Copy over parameters */
2523 paramnew[i].c[0] = fudgeQQ;
2524 paramnew[i].c[1] = atoms->atom[paramp1[j].a[0]].q;
2525 paramnew[i].c[2] = atoms->atom[paramp1[j].a[1]].q;
2526 paramnew[i].c[3] = paramp1[j].c[0];
2527 paramnew[i].c[4] = paramp1[j].c[1];
2529 /* copy over atoms */
2530 paramnew[i].a[0] = paramp1[j].a[0];
2531 paramnew[i].a[1] = paramp1[j].a[1];
2534 /* free the old pairlists */
2535 sfree(plist[F_LJC14_Q].param);
2536 sfree(plist[F_LJ14].param);
2538 /* now assign the new data to the F_LJC14_Q structure */
2539 plist[F_LJC14_Q].param = paramnew;
2540 plist[F_LJC14_Q].nr = p2newnr;
2542 /* Empty the LJ14 pairlist */
2543 plist[F_LJ14].nr = 0;
2544 plist[F_LJ14].param = nullptr;
2547 static void generate_LJCpairsNB(t_molinfo *mol, int nb_funct, t_params *nbp, warninp_t wi)
2549 int n, ntype, i, j, k;
2556 atom = mol->atoms.atom;
2558 ntype = static_cast<int>(std::sqrt(static_cast<double>(nbp->nr)));
2559 GMX_ASSERT(ntype * ntype == nbp->nr, "Number of pairs of generated non-bonded parameters should be a perfect square");
2561 for (i = 0; i < MAXATOMLIST; i++)
2563 param.a[i] = NOTSET;
2565 for (i = 0; i < MAXFORCEPARAM; i++)
2567 param.c[i] = NOTSET;
2570 /* Add a pair interaction for all non-excluded atom pairs */
2572 for (i = 0; i < n; i++)
2574 for (j = i+1; j < n; j++)
2577 for (k = excl->index[i]; k < excl->index[i+1]; k++)
2579 if (excl->a[k] == j)
2586 if (nb_funct != F_LJ)
2588 auto message = gmx::formatString
2589 ("Can only generate non-bonded pair interactions "
2590 "for Van der Waals type Lennard-Jones");
2591 warning_error_and_exit(wi, message, FARGS);
2595 param.c[0] = atom[i].q;
2596 param.c[1] = atom[j].q;
2597 param.c[2] = nbp->param[ntype*atom[i].type+atom[j].type].c[0];
2598 param.c[3] = nbp->param[ntype*atom[i].type+atom[j].type].c[1];
2599 add_param_to_list(&mol->plist[F_LJC_PAIRS_NB], ¶m);
2605 static void set_excl_all(t_blocka *excl)
2609 /* Get rid of the current exclusions and exclude all atom pairs */
2611 excl->nra = nat*nat;
2612 srenew(excl->a, excl->nra);
2614 for (i = 0; i < nat; i++)
2617 for (j = 0; j < nat; j++)
2622 excl->index[nat] = k;
2625 static void decouple_atoms(t_atoms *atoms, int atomtype_decouple,
2626 int couple_lam0, int couple_lam1,
2627 const char *mol_name, warninp_t wi)
2631 for (i = 0; i < atoms->nr; i++)
2635 atom = &atoms->atom[i];
2637 if (atom->qB != atom->q || atom->typeB != atom->type)
2639 auto message = gmx::formatString
2640 ("Atom %d in molecule type '%s' has different A and B state "
2641 "charges and/or atom types set in the topology file as well "
2642 "as through the mdp option '%s'. You can not use both "
2643 "these methods simultaneously.",
2644 i + 1, mol_name, "couple-moltype");
2645 warning_error_and_exit(wi, message, FARGS);
2648 if (couple_lam0 == ecouplamNONE || couple_lam0 == ecouplamVDW)
2652 if (couple_lam0 == ecouplamNONE || couple_lam0 == ecouplamQ)
2654 atom->type = atomtype_decouple;
2656 if (couple_lam1 == ecouplamNONE || couple_lam1 == ecouplamVDW)
2660 if (couple_lam1 == ecouplamNONE || couple_lam1 == ecouplamQ)
2662 atom->typeB = atomtype_decouple;
2667 void convert_moltype_couple(t_molinfo *mol, int atomtype_decouple, real fudgeQQ,
2668 int couple_lam0, int couple_lam1,
2669 bool bCoupleIntra, int nb_funct, t_params *nbp,
2672 convert_pairs_to_pairsQ(mol->plist, fudgeQQ, &mol->atoms);
2675 generate_LJCpairsNB(mol, nb_funct, nbp, wi);
2676 set_excl_all(&mol->excls);
2678 decouple_atoms(&mol->atoms, atomtype_decouple, couple_lam0, couple_lam1,