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.
48 #include "gromacs/fileio/warninp.h"
49 #include "gromacs/gmxpreprocess/gpp_atomtype.h"
50 #include "gromacs/gmxpreprocess/gpp_bond_atomtype.h"
51 #include "gromacs/gmxpreprocess/notset.h"
52 #include "gromacs/gmxpreprocess/readir.h"
53 #include "gromacs/gmxpreprocess/topdirs.h"
54 #include "gromacs/gmxpreprocess/toputil.h"
55 #include "gromacs/mdtypes/md_enums.h"
56 #include "gromacs/topology/ifunc.h"
57 #include "gromacs/topology/symtab.h"
58 #include "gromacs/utility/arrayref.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/gmxassert.h"
62 #include "gromacs/utility/smalloc.h"
64 void generate_nbparams(int comb, int ftype, t_params *plist, gpp_atomtype_t atype,
69 real c, bi, bj, ci, cj, ci0, ci1, ci2, cj0, cj1, cj2;
72 /* Lean mean shortcuts */
73 nr = get_atomtype_ntypes(atype);
75 snew(plist->param, nr*nr);
78 /* Fill the matrix with force parameters */
86 for (i = k = 0; (i < nr); i++)
88 for (j = 0; (j < nr); j++, k++)
90 for (nf = 0; (nf < nrfp); nf++)
92 ci = get_atomtype_nbparam(i, nf, atype);
93 cj = get_atomtype_nbparam(j, nf, atype);
94 c = std::sqrt(ci * cj);
95 plist->param[k].c[nf] = c;
101 case eCOMB_ARITHMETIC:
102 /* c0 and c1 are sigma and epsilon */
103 for (i = k = 0; (i < nr); i++)
105 for (j = 0; (j < nr); j++, k++)
107 ci0 = get_atomtype_nbparam(i, 0, atype);
108 cj0 = get_atomtype_nbparam(j, 0, atype);
109 ci1 = get_atomtype_nbparam(i, 1, atype);
110 cj1 = get_atomtype_nbparam(j, 1, atype);
111 plist->param[k].c[0] = (fabs(ci0) + fabs(cj0))*0.5;
112 /* Negative sigma signals that c6 should be set to zero later,
113 * so we need to propagate that through the combination rules.
115 if (ci0 < 0 || cj0 < 0)
117 plist->param[k].c[0] *= -1;
119 plist->param[k].c[1] = std::sqrt(ci1*cj1);
124 case eCOMB_GEOM_SIG_EPS:
125 /* c0 and c1 are sigma and epsilon */
126 for (i = k = 0; (i < nr); i++)
128 for (j = 0; (j < nr); j++, k++)
130 ci0 = get_atomtype_nbparam(i, 0, atype);
131 cj0 = get_atomtype_nbparam(j, 0, atype);
132 ci1 = get_atomtype_nbparam(i, 1, atype);
133 cj1 = get_atomtype_nbparam(j, 1, atype);
134 plist->param[k].c[0] = std::sqrt(std::fabs(ci0*cj0));
135 /* Negative sigma signals that c6 should be set to zero later,
136 * so we need to propagate that through the combination rules.
138 if (ci0 < 0 || cj0 < 0)
140 plist->param[k].c[0] *= -1;
142 plist->param[k].c[1] = std::sqrt(ci1*cj1);
148 sprintf(errbuf, "No such combination rule %d", comb);
149 warning_error_and_exit(wi, errbuf, FARGS);
153 gmx_incons("Topology processing, generate nb parameters");
158 /* Buckingham rules */
159 for (i = k = 0; (i < nr); i++)
161 for (j = 0; (j < nr); j++, k++)
163 ci0 = get_atomtype_nbparam(i, 0, atype);
164 cj0 = get_atomtype_nbparam(j, 0, atype);
165 ci2 = get_atomtype_nbparam(i, 2, atype);
166 cj2 = get_atomtype_nbparam(j, 2, atype);
167 bi = get_atomtype_nbparam(i, 1, atype);
168 bj = get_atomtype_nbparam(j, 1, atype);
169 plist->param[k].c[0] = std::sqrt(ci0 * cj0);
170 if ((bi == 0) || (bj == 0))
172 plist->param[k].c[1] = 0;
176 plist->param[k].c[1] = 2.0/(1/bi+1/bj);
178 plist->param[k].c[2] = std::sqrt(ci2 * cj2);
184 sprintf(errbuf, "Invalid nonbonded type %s",
185 interaction_function[ftype].longname);
186 warning_error(wi, errbuf);
190 static void realloc_nb_params(gpp_atomtype_t at,
191 t_nbparam ***nbparam, t_nbparam ***pair)
193 /* Add space in the non-bonded parameters matrix */
194 int atnr = get_atomtype_ntypes(at);
195 srenew(*nbparam, atnr);
196 snew((*nbparam)[atnr-1], atnr);
200 snew((*pair)[atnr-1], atnr);
204 static void copy_B_from_A(int ftype, double *c)
208 nrfpA = NRFPA(ftype);
209 nrfpB = NRFPB(ftype);
211 /* Copy the B parameters from the first nrfpB A parameters */
212 for (i = 0; (i < nrfpB); i++)
218 void push_at (t_symtab *symtab, gpp_atomtype_t at, t_bond_atomtype bat,
219 char *line, int nb_funct,
220 t_nbparam ***nbparam, t_nbparam ***pair,
227 t_xlate xl[eptNR] = {
235 int nr, i, nfields, j, pt, nfp0 = -1;
236 int batype_nr, nread;
237 char type[STRLEN], btype[STRLEN], ptype[STRLEN];
239 double c[MAXFORCEPARAM];
240 char tmpfield[12][100]; /* Max 12 fields of width 100 */
245 bool have_atomic_number;
246 bool have_bonded_type;
251 /* First assign input line to temporary array */
252 nfields = sscanf(line, "%s%s%s%s%s%s%s%s%s%s%s%s",
253 tmpfield[0], tmpfield[1], tmpfield[2], tmpfield[3], tmpfield[4], tmpfield[5],
254 tmpfield[6], tmpfield[7], tmpfield[8], tmpfield[9], tmpfield[10], tmpfield[11]);
256 /* Comments on optional fields in the atomtypes section:
258 * The force field format is getting a bit old. For OPLS-AA we needed
259 * to add a special bonded atomtype, and for Gerrit Groenhofs QM/MM stuff
260 * we also needed the atomic numbers.
261 * To avoid making all old or user-generated force fields unusable we
262 * have introduced both these quantities as optional columns, and do some
263 * acrobatics to check whether they are present or not.
264 * This will all look much nicer when we switch to XML... sigh.
266 * Field 0 (mandatory) is the nonbonded type name. (string)
267 * Field 1 (optional) is the bonded type (string)
268 * Field 2 (optional) is the atomic number (int)
269 * Field 3 (mandatory) is the mass (numerical)
270 * Field 4 (mandatory) is the charge (numerical)
271 * Field 5 (mandatory) is the particle type (single character)
272 * This is followed by a number of nonbonded parameters.
274 * The safest way to identify the format is the particle type field.
276 * So, here is what we do:
278 * A. Read in the first six fields as strings
279 * B. If field 3 (starting from 0) is a single char, we have neither
280 * bonded_type or atomic numbers.
281 * C. If field 5 is a single char we have both.
282 * D. If field 4 is a single char we check field 1. If this begins with
283 * an alphabetical character we have bonded types, otherwise atomic numbers.
292 if ( (strlen(tmpfield[5]) == 1) && isalpha(tmpfield[5][0]) )
294 have_bonded_type = TRUE;
295 have_atomic_number = TRUE;
297 else if ( (strlen(tmpfield[3]) == 1) && isalpha(tmpfield[3][0]) )
299 have_bonded_type = FALSE;
300 have_atomic_number = FALSE;
304 have_bonded_type = ( isalpha(tmpfield[1][0]) != 0 );
305 have_atomic_number = !have_bonded_type;
308 /* optional fields */
317 if (have_atomic_number)
319 if (have_bonded_type)
321 nread = sscanf(line, "%s%s%d%lf%lf%s%lf%lf",
322 type, btype, &atomnr, &m, &q, ptype, &c[0], &c[1]);
331 /* have_atomic_number && !have_bonded_type */
332 nread = sscanf(line, "%s%d%lf%lf%s%lf%lf",
333 type, &atomnr, &m, &q, ptype, &c[0], &c[1]);
343 if (have_bonded_type)
345 /* !have_atomic_number && have_bonded_type */
346 nread = sscanf(line, "%s%s%lf%lf%s%lf%lf",
347 type, btype, &m, &q, ptype, &c[0], &c[1]);
356 /* !have_atomic_number && !have_bonded_type */
357 nread = sscanf(line, "%s%lf%lf%s%lf%lf",
358 type, &m, &q, ptype, &c[0], &c[1]);
367 if (!have_bonded_type)
372 if (!have_atomic_number)
382 if (have_atomic_number)
384 if (have_bonded_type)
386 nread = sscanf(line, "%s%s%d%lf%lf%s%lf%lf%lf",
387 type, btype, &atomnr, &m, &q, ptype, &c[0], &c[1], &c[2]);
396 /* have_atomic_number && !have_bonded_type */
397 nread = sscanf(line, "%s%d%lf%lf%s%lf%lf%lf",
398 type, &atomnr, &m, &q, ptype, &c[0], &c[1], &c[2]);
408 if (have_bonded_type)
410 /* !have_atomic_number && have_bonded_type */
411 nread = sscanf(line, "%s%s%lf%lf%s%lf%lf%lf",
412 type, btype, &m, &q, ptype, &c[0], &c[1], &c[2]);
421 /* !have_atomic_number && !have_bonded_type */
422 nread = sscanf(line, "%s%lf%lf%s%lf%lf%lf",
423 type, &m, &q, ptype, &c[0], &c[1], &c[2]);
432 if (!have_bonded_type)
437 if (!have_atomic_number)
445 sprintf(errbuf, "Invalid function type %d in push_at", nb_funct);
446 warning_error_and_exit(wi, errbuf, FARGS);
448 for (j = nfp0; (j < MAXFORCEPARAM); j++)
453 if (strlen(type) == 1 && isdigit(type[0]))
455 warning_error_and_exit(wi, "Atom type names can't be single digits.", FARGS);
458 if (strlen(btype) == 1 && isdigit(btype[0]))
460 warning_error_and_exit(wi, "Bond atom type names can't be single digits.", FARGS);
463 /* Hack to read old topologies */
464 if (gmx_strcasecmp(ptype, "D") == 0)
468 for (j = 0; (j < eptNR); j++)
470 if (gmx_strcasecmp(ptype, xl[j].entry) == 0)
477 sprintf(errbuf, "Invalid particle type %s", ptype);
478 warning_error_and_exit(wi, errbuf, FARGS);
485 for (i = 0; (i < MAXFORCEPARAM); i++)
490 if ((batype_nr = get_bond_atomtype_type(btype, bat)) == NOTSET)
492 add_bond_atomtype(bat, symtab, btype);
494 batype_nr = get_bond_atomtype_type(btype, bat);
496 if ((nr = get_atomtype_type(type, at)) != NOTSET)
498 sprintf(errbuf, "Overriding atomtype %s", type);
500 if ((nr = set_atomtype(nr, at, symtab, atom, type, param, batype_nr,
503 sprintf(errbuf, "Replacing atomtype %s failed", type);
504 warning_error_and_exit(wi, errbuf, FARGS);
507 else if ((add_atomtype(at, symtab, atom, type, param,
508 batype_nr, atomnr)) == NOTSET)
510 sprintf(errbuf, "Adding atomtype %s failed", type);
511 warning_error_and_exit(wi, errbuf, FARGS);
515 /* Add space in the non-bonded parameters matrix */
516 realloc_nb_params(at, nbparam, pair);
522 //! Return whether the contents of \c a and \c b are the same, considering also reversed order.
523 template <typename T>
524 static bool equalEitherForwardOrBackward(gmx::ArrayRef<const T> a, gmx::ArrayRef<const T> b)
526 return (std::equal(a.begin(), a.end(), b.begin()) ||
527 std::equal(a.begin(), a.end(), b.rbegin()));
530 static void push_bondtype(t_params * bt,
539 int nrfp = NRFP(ftype);
542 /* If bAllowRepeat is TRUE, we allow multiple entries as long as they
543 are on directly _adjacent_ lines.
546 /* First check if our atomtypes are _identical_ (not reversed) to the previous
547 entry. If they are not identical we search for earlier duplicates. If they are
548 we can skip it, since we already searched for the first line
552 bool isContinuationOfBlock = false;
553 if (bAllowRepeat && nr > 1)
555 isContinuationOfBlock = true;
556 for (int j = 0; j < nral; j++)
558 if (b->a[j] != bt->param[nr - 2].a[j])
560 isContinuationOfBlock = false;
565 /* Search for earlier duplicates if this entry was not a continuation
566 from the previous line.
568 bool addBondType = true;
569 bool haveWarned = false;
570 bool haveErrored = false;
571 for (int i = 0; (i < nr); i++)
573 gmx::ArrayRef<const int> bParams(b->a, b->a + nral);
574 gmx::ArrayRef<const int> testParams(bt->param[i].a, bt->param[i].a + nral);
575 if (equalEitherForwardOrBackward(bParams, testParams))
577 GMX_ASSERT(nrfp <= MAXFORCEPARAM, "This is ensured in other places, but we need this assert to keep the clang analyzer happy");
578 const bool identicalParameters = std::equal(bt->param[i].c, bt->param[i].c + nrfp, b->c);
580 if (!bAllowRepeat || identicalParameters)
585 if (!identicalParameters)
589 /* With dihedral type 9 we only allow for repeating
590 * of the same parameters with blocks with 1 entry.
591 * Allowing overriding is too complex to check.
593 if (!isContinuationOfBlock && !haveErrored)
595 warning_error(wi, "Encountered a second block of parameters for dihedral type 9 for the same atoms, with either different parameters and/or the first block has multiple lines. This is not supported.");
599 else if (!haveWarned)
601 sprintf(errbuf, "Overriding %s parameters.%s",
602 interaction_function[ftype].longname,
604 "\nUse dihedraltype 9 to allow several multiplicity terms. Only consecutive lines are combined. Non-consective lines overwrite each other."
608 fprintf(stderr, " old: ");
609 for (int j = 0; (j < nrfp); j++)
611 fprintf(stderr, " %g", bt->param[i].c[j]);
613 fprintf(stderr, " \n new: %s\n\n", line);
619 if (!identicalParameters && !bAllowRepeat)
621 /* Overwrite the parameters with the latest ones */
622 // TODO considering improving the following code by replacing with:
623 // std::copy(b->c, b->c + nrfp, bt->param[i].c);
624 for (int j = 0; (j < nrfp); j++)
626 bt->param[i].c[j] = b->c[j];
637 /* fill the arrays up and down */
638 memcpy(bt->param[bt->nr].c, b->c, sizeof(b->c));
639 memcpy(bt->param[bt->nr].a, b->a, sizeof(b->a));
640 memcpy(bt->param[bt->nr+1].c, b->c, sizeof(b->c));
642 /* The definitions of linear angles depend on the order of atoms,
643 * that means that for atoms i-j-k, with certain parameter a, the
644 * corresponding k-j-i angle will have parameter 1-a.
646 if (ftype == F_LINEAR_ANGLES)
648 bt->param[bt->nr+1].c[0] = 1-bt->param[bt->nr+1].c[0];
649 bt->param[bt->nr+1].c[2] = 1-bt->param[bt->nr+1].c[2];
652 for (int j = 0; (j < nral); j++)
654 bt->param[bt->nr+1].a[j] = b->a[nral-1-j];
661 void push_bt(directive d, t_params bt[], int nral,
663 t_bond_atomtype bat, char *line,
666 const char *formal[MAXATOMLIST+1] = {
675 const char *formnl[MAXATOMLIST+1] = {
681 "%*s%*s%*s%*s%*s%*s",
682 "%*s%*s%*s%*s%*s%*s%*s"
684 const char *formlf = "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf";
685 int i, ft, ftype, nn, nrfp, nrfpA;
687 char alc[MAXATOMLIST+1][20];
688 /* One force parameter more, so we can check if we read too many */
689 double c[MAXFORCEPARAM+1];
693 if ((bat && at) || (!bat && !at))
695 gmx_incons("You should pass either bat or at to push_bt");
698 /* Make format string (nral ints+functype) */
699 if ((nn = sscanf(line, formal[nral],
700 alc[0], alc[1], alc[2], alc[3], alc[4], alc[5])) != nral+1)
702 sprintf(errbuf, "Not enough atomtypes (%d instead of %d)", nn-1, nral);
703 warning_error(wi, errbuf);
707 ft = strtol(alc[nral], nullptr, 10);
708 ftype = ifunc_index(d, ft);
710 nrfpA = interaction_function[ftype].nrfpA;
711 strcpy(f1, formnl[nral]);
713 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]))
718 /* Copy the B-state from the A-state */
719 copy_B_from_A(ftype, c);
725 warning_error(wi, "Not enough parameters");
727 else if (nn > nrfpA && nn < nrfp)
729 warning_error(wi, "Too many parameters or not enough parameters for topology B");
733 warning_error(wi, "Too many parameters");
735 for (i = nn; (i < nrfp); i++)
741 for (i = 0; (i < nral); i++)
743 if (at && ((p.a[i] = get_atomtype_type(alc[i], at)) == NOTSET))
745 sprintf(errbuf, "Unknown atomtype %s\n", alc[i]);
746 warning_error_and_exit(wi, errbuf, FARGS);
748 else if (bat && ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET))
750 sprintf(errbuf, "Unknown bond_atomtype %s\n", alc[i]);
751 warning_error_and_exit(wi, errbuf, FARGS);
754 for (i = 0; (i < nrfp); i++)
758 push_bondtype (&(bt[ftype]), &p, nral, ftype, FALSE, line, wi);
762 void push_dihedraltype(directive d, t_params bt[],
763 t_bond_atomtype bat, char *line,
766 const char *formal[MAXATOMLIST+1] = {
775 const char *formnl[MAXATOMLIST+1] = {
781 "%*s%*s%*s%*s%*s%*s",
782 "%*s%*s%*s%*s%*s%*s%*s"
784 const char *formlf[MAXFORCEPARAM] = {
790 "%lf%lf%lf%lf%lf%lf",
791 "%lf%lf%lf%lf%lf%lf%lf",
792 "%lf%lf%lf%lf%lf%lf%lf%lf",
793 "%lf%lf%lf%lf%lf%lf%lf%lf%lf",
794 "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
795 "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
796 "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
798 int i, ft, ftype, nn, nrfp, nrfpA, nral;
800 char alc[MAXATOMLIST+1][20];
801 double c[MAXFORCEPARAM];
806 /* This routine accepts dihedraltypes defined from either 2 or 4 atoms.
808 * We first check for 2 atoms with the 3th column being an integer
809 * defining the type. If this isn't the case, we try it with 4 atoms
810 * and the 5th column defining the dihedral type.
812 nn = sscanf(line, formal[4], alc[0], alc[1], alc[2], alc[3], alc[4]);
813 if (nn >= 3 && strlen(alc[2]) == 1 && isdigit(alc[2][0]))
816 ft = strtol(alc[nral], nullptr, 10);
817 /* Move atom types around a bit and use 'X' for wildcard atoms
818 * to create a 4-atom dihedral definition with arbitrary atoms in
821 if (alc[2][0] == '2')
823 /* improper - the two atomtypes are 1,4. Use wildcards for 2,3 */
824 strcpy(alc[3], alc[1]);
825 sprintf(alc[2], "X");
826 sprintf(alc[1], "X");
827 /* alc[0] stays put */
831 /* proper - the two atomtypes are 2,3. Use wildcards for 1,4 */
832 sprintf(alc[3], "X");
833 strcpy(alc[2], alc[1]);
834 strcpy(alc[1], alc[0]);
835 sprintf(alc[0], "X");
838 else if (nn == 5 && strlen(alc[4]) == 1 && isdigit(alc[4][0]))
841 ft = strtol(alc[nral], nullptr, 10);
845 sprintf(errbuf, "Incorrect number of atomtypes for dihedral (%d instead of 2 or 4)", nn-1);
846 warning_error(wi, errbuf);
852 /* Previously, we have always overwritten parameters if e.g. a torsion
853 with the same atomtypes occurs on multiple lines. However, CHARMM and
854 some other force fields specify multiple dihedrals over some bonds,
855 including cosines with multiplicity 6 and somethimes even higher.
856 Thus, they cannot be represented with Ryckaert-Bellemans terms.
857 To add support for these force fields, Dihedral type 9 is identical to
858 normal proper dihedrals, but repeated entries are allowed.
865 bAllowRepeat = FALSE;
869 ftype = ifunc_index(d, ft);
871 nrfpA = interaction_function[ftype].nrfpA;
873 strcpy(f1, formnl[nral]);
874 strcat(f1, formlf[nrfp-1]);
876 /* Check number of parameters given */
877 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]))
882 /* Copy the B-state from the A-state */
883 copy_B_from_A(ftype, c);
889 warning_error(wi, "Not enough parameters");
891 else if (nn > nrfpA && nn < nrfp)
893 warning_error(wi, "Too many parameters or not enough parameters for topology B");
897 warning_error(wi, "Too many parameters");
899 for (i = nn; (i < nrfp); i++)
906 for (i = 0; (i < 4); i++)
908 if (!strcmp(alc[i], "X"))
914 if ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET)
916 sprintf(errbuf, "Unknown bond_atomtype %s", alc[i]);
917 warning_error_and_exit(wi, errbuf, FARGS);
921 for (i = 0; (i < nrfp); i++)
925 /* Always use 4 atoms here, since we created two wildcard atoms
926 * if there wasn't of them 4 already.
928 push_bondtype (&(bt[ftype]), &p, 4, ftype, bAllowRepeat, line, wi);
932 void push_nbt(directive d, t_nbparam **nbt, gpp_atomtype_t atype,
933 char *pline, int nb_funct,
937 const char *form3 = "%*s%*s%*s%lf%lf%lf";
938 const char *form4 = "%*s%*s%*s%lf%lf%lf%lf";
939 const char *form5 = "%*s%*s%*s%lf%lf%lf%lf%lf";
941 int i, f, n, ftype, nrfp;
949 if (sscanf (pline, "%s%s%d", a0, a1, &f) != 3)
955 ftype = ifunc_index(d, f);
957 if (ftype != nb_funct)
959 sprintf(errbuf, "Trying to add %s while the default nonbond type is %s",
960 interaction_function[ftype].longname,
961 interaction_function[nb_funct].longname);
962 warning_error(wi, errbuf);
966 /* Get the force parameters */
970 n = sscanf(pline, form4, &c[0], &c[1], &c[2], &c[3]);
976 /* When the B topology parameters are not set,
977 * copy them from topology A
979 GMX_ASSERT(nrfp <= 4, "LJ-14 cannot have more than 4 parameters");
980 for (i = n; i < nrfp; i++)
985 else if (ftype == F_LJC14_Q)
987 n = sscanf(pline, form5, &c[0], &c[1], &c[2], &c[3], &dum);
990 incorrect_n_param(wi);
996 if (sscanf(pline, form3, &c[0], &c[1], &dum) != 2)
998 incorrect_n_param(wi);
1004 if (sscanf(pline, form4, &c[0], &c[1], &c[2], &dum) != 3)
1006 incorrect_n_param(wi);
1012 sprintf(errbuf, "Number of force parameters for nonbonded interactions is %d", nrfp);
1013 warning_error_and_exit(wi, errbuf, FARGS);
1015 for (i = 0; (i < nrfp); i++)
1020 /* Put the parameters in the matrix */
1021 if ((ai = get_atomtype_type (a0, atype)) == NOTSET)
1023 sprintf(errbuf, "Atomtype %s not found", a0);
1024 warning_error_and_exit(wi, errbuf, FARGS);
1026 if ((aj = get_atomtype_type (a1, atype)) == NOTSET)
1028 sprintf(errbuf, "Atomtype %s not found", a1);
1029 warning_error_and_exit(wi, errbuf, FARGS);
1031 nbp = &(nbt[std::max(ai, aj)][std::min(ai, aj)]);
1036 for (i = 0; i < nrfp; i++)
1038 bId = bId && (nbp->c[i] == cr[i]);
1042 sprintf(errbuf, "Overriding non-bonded parameters,");
1043 warning(wi, errbuf);
1044 fprintf(stderr, " old:");
1045 for (i = 0; i < nrfp; i++)
1047 fprintf(stderr, " %g", nbp->c[i]);
1049 fprintf(stderr, " new\n%s\n", pline);
1053 for (i = 0; i < nrfp; i++)
1060 push_cmaptype(directive d, t_params bt[], int nral, gpp_atomtype_t at,
1061 t_bond_atomtype bat, char *line,
1064 const char *formal = "%s%s%s%s%s%s%s%s%n";
1066 int i, ft, ftype, nn, nrfp, nrfpA, nrfpB;
1067 int start, nchar_consumed;
1068 int nxcmap, nycmap, ncmap, read_cmap, sl, nct;
1069 char s[20], alc[MAXATOMLIST+2][20];
1071 char errbuf[STRLEN];
1073 /* Keep the compiler happy */
1077 GMX_ASSERT(nral == 5, "CMAP requires 5 atoms per interaction");
1079 /* Here we can only check for < 8 */
1080 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)
1082 sprintf(errbuf, "Incorrect number of atomtypes for cmap (%d instead of 5)", nn-1);
1083 warning_error(wi, errbuf);
1086 start += nchar_consumed;
1088 ft = strtol(alc[nral], nullptr, 10);
1089 nxcmap = strtol(alc[nral+1], nullptr, 10);
1090 nycmap = strtol(alc[nral+2], nullptr, 10);
1092 /* Check for equal grid spacing in x and y dims */
1093 if (nxcmap != nycmap)
1095 sprintf(errbuf, "Not the same grid spacing in x and y for cmap grid: x=%d, y=%d", nxcmap, nycmap);
1096 warning_error(wi, errbuf);
1099 ncmap = nxcmap*nycmap;
1100 ftype = ifunc_index(d, ft);
1101 nrfpA = strtol(alc[6], nullptr, 10)*strtol(alc[6], nullptr, 10);
1102 nrfpB = strtol(alc[7], nullptr, 10)*strtol(alc[7], nullptr, 10);
1105 /* Allocate memory for the CMAP grid */
1106 bt[F_CMAP].ncmap += nrfp;
1107 srenew(bt[F_CMAP].cmap, bt[F_CMAP].ncmap);
1109 /* Read in CMAP parameters */
1111 for (i = 0; i < ncmap; i++)
1113 while (isspace(*(line+start+sl)))
1117 nn = sscanf(line+start+sl, " %s ", s);
1119 bt[F_CMAP].cmap[i+(bt[F_CMAP].ncmap)-nrfp] = strtod(s, nullptr);
1127 sprintf(errbuf, "Error in reading cmap parameter for angle %s %s %s %s %s", alc[0], alc[1], alc[2], alc[3], alc[4]);
1128 warning_error(wi, errbuf);
1133 /* Check do that we got the number of parameters we expected */
1134 if (read_cmap == nrfpA)
1136 for (i = 0; i < ncmap; i++)
1138 bt[F_CMAP].cmap[i+ncmap] = bt[F_CMAP].cmap[i];
1143 if (read_cmap < nrfpA)
1145 warning_error(wi, "Not enough cmap parameters");
1147 else if (read_cmap > nrfpA && read_cmap < nrfp)
1149 warning_error(wi, "Too many cmap parameters or not enough parameters for topology B");
1151 else if (read_cmap > nrfp)
1153 warning_error(wi, "Too many cmap parameters");
1158 /* Set grid spacing and the number of grids (we assume these numbers to be the same for all grids
1159 * so we can safely assign them each time
1161 bt[F_CMAP].grid_spacing = nxcmap; /* Or nycmap, they need to be equal */
1162 bt[F_CMAP].nc = bt[F_CMAP].nc + 1; /* Since we are incrementing here, we need to subtract later, see (*****) */
1163 nct = (nral+1) * bt[F_CMAP].nc;
1165 /* Allocate memory for the cmap_types information */
1166 srenew(bt[F_CMAP].cmap_types, nct);
1168 for (i = 0; (i < nral); i++)
1170 if (at && ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET))
1172 sprintf(errbuf, "Unknown atomtype %s\n", alc[i]);
1173 warning_error(wi, errbuf);
1175 else if (bat && ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET))
1177 sprintf(errbuf, "Unknown bond_atomtype %s\n", alc[i]);
1178 warning_error(wi, errbuf);
1181 /* Assign a grid number to each cmap_type */
1182 bt[F_CMAP].cmap_types[bt[F_CMAP].nct++] = get_bond_atomtype_type(alc[i], bat);
1185 /* Assign a type number to this cmap */
1186 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 (****) */
1188 /* Check for the correct number of atoms (again) */
1189 if (bt[F_CMAP].nct != nct)
1191 sprintf(errbuf, "Incorrect number of atom types (%d) in cmap type %d\n", nct, bt[F_CMAP].nc);
1192 warning_error(wi, errbuf);
1195 /* Is this correct?? */
1196 for (i = 0; i < MAXFORCEPARAM; i++)
1201 /* Push the bond to the bondlist */
1202 push_bondtype (&(bt[ftype]), &p, nral, ftype, FALSE, line, wi);
1206 static void push_atom_now(t_symtab *symtab, t_atoms *at, int atomnr,
1208 int type, char *ctype, int ptype,
1210 char *resname, char *name, real m0, real q0,
1211 int typeB, char *ctypeB, real mB, real qB,
1214 int j, resind = 0, resnr;
1217 char errbuf[STRLEN];
1219 if (((nr == 0) && (atomnr != 1)) || (nr && (atomnr != at->nr+1)))
1221 sprintf(errbuf, "Atoms in the .top are not numbered consecutively from 1 (rather, atomnr = %d, while at->nr = %d)", atomnr, at->nr);
1222 warning_error_and_exit(wi, errbuf, FARGS);
1225 j = strlen(resnumberic) - 1;
1226 if (isdigit(resnumberic[j]))
1232 ric = resnumberic[j];
1233 if (j == 0 || !isdigit(resnumberic[j-1]))
1235 sprintf(errbuf, "Invalid residue number '%s' for atom %d",
1236 resnumberic, atomnr);
1237 warning_error_and_exit(wi, errbuf, FARGS);
1240 resnr = strtol(resnumberic, nullptr, 10);
1244 resind = at->atom[nr-1].resind;
1246 if (nr == 0 || strcmp(resname, *at->resinfo[resind].name) != 0 ||
1247 resnr != at->resinfo[resind].nr ||
1248 ric != at->resinfo[resind].ic)
1258 at->nres = resind + 1;
1259 srenew(at->resinfo, at->nres);
1260 at->resinfo[resind].name = put_symtab(symtab, resname);
1261 at->resinfo[resind].nr = resnr;
1262 at->resinfo[resind].ic = ric;
1266 resind = at->atom[at->nr-1].resind;
1269 /* New atom instance
1270 * get new space for arrays
1272 srenew(at->atom, nr+1);
1273 srenew(at->atomname, nr+1);
1274 srenew(at->atomtype, nr+1);
1275 srenew(at->atomtypeB, nr+1);
1278 at->atom[nr].type = type;
1279 at->atom[nr].ptype = ptype;
1280 at->atom[nr].q = q0;
1281 at->atom[nr].m = m0;
1282 at->atom[nr].typeB = typeB;
1283 at->atom[nr].qB = qB;
1284 at->atom[nr].mB = mB;
1286 at->atom[nr].resind = resind;
1287 at->atom[nr].atomnumber = atomicnumber;
1288 at->atomname[nr] = put_symtab(symtab, name);
1289 at->atomtype[nr] = put_symtab(symtab, ctype);
1290 at->atomtypeB[nr] = put_symtab(symtab, ctypeB);
1294 static void push_cg(t_block *block, int *lastindex, int index, int a)
1296 if (((block->nr) && (*lastindex != index)) || (!block->nr))
1298 /* add a new block */
1300 srenew(block->index, block->nr+1);
1302 block->index[block->nr] = a + 1;
1306 void push_atom(t_symtab *symtab, t_block *cgs,
1307 t_atoms *at, gpp_atomtype_t atype, char *line, int *lastcg,
1311 int cgnumber, atomnr, type, typeB, nscan;
1312 char id[STRLEN], ctype[STRLEN], ctypeB[STRLEN],
1313 resnumberic[STRLEN], resname[STRLEN], name[STRLEN], check[STRLEN];
1314 double m, q, mb, qb;
1315 real m0, q0, mB, qB;
1316 char errbuf[STRLEN];
1318 /* Make a shortcut for writing in this molecule */
1321 /* Fixed parameters */
1322 if (sscanf(line, "%s%s%s%s%s%d",
1323 id, ctype, resnumberic, resname, name, &cgnumber) != 6)
1328 sscanf(id, "%d", &atomnr);
1329 if ((type = get_atomtype_type(ctype, atype)) == NOTSET)
1331 sprintf(errbuf, "Atomtype %s not found", ctype);
1332 warning_error_and_exit(wi, errbuf, FARGS);
1334 ptype = get_atomtype_ptype(type, atype);
1336 /* Set default from type */
1337 q0 = get_atomtype_qA(type, atype);
1338 m0 = get_atomtype_massA(type, atype);
1343 /* Optional parameters */
1344 nscan = sscanf(line, "%*s%*s%*s%*s%*s%*s%lf%lf%s%lf%lf%s",
1345 &q, &m, ctypeB, &qb, &mb, check);
1347 /* Nasty switch that falls thru all the way down! */
1356 if ((typeB = get_atomtype_type(ctypeB, atype)) == NOTSET)
1358 sprintf(errbuf, "Atomtype %s not found", ctypeB);
1359 warning_error_and_exit(wi, errbuf, FARGS);
1361 qB = get_atomtype_qA(typeB, atype);
1362 mB = get_atomtype_massA(typeB, atype);
1371 warning_error(wi, "Too many parameters");
1379 push_cg(cgs, lastcg, cgnumber, nr);
1381 push_atom_now(symtab, at, atomnr, get_atomtype_atomnumber(type, atype),
1382 type, ctype, ptype, resnumberic,
1383 resname, name, m0, q0, typeB,
1384 typeB == type ? ctype : ctypeB, mB, qB, wi);
1387 void push_molt(t_symtab *symtab, int *nmol, t_molinfo **mol, char *line,
1393 char errbuf[STRLEN];
1395 if ((sscanf(line, "%s%d", type, &nrexcl)) != 2)
1397 warning_error(wi, "Expected a molecule type name and nrexcl");
1400 /* Test if this moleculetype overwrites another */
1404 if (strcmp(*((*mol)[i].name), type) == 0)
1406 sprintf(errbuf, "moleculetype %s is redefined", type);
1407 warning_error_and_exit(wi, errbuf, FARGS);
1413 srenew(*mol, *nmol);
1414 newmol = &((*mol)[*nmol-1]);
1415 init_molinfo(newmol);
1417 /* Fill in the values */
1418 newmol->name = put_symtab(symtab, type);
1419 newmol->nrexcl = nrexcl;
1420 newmol->excl_set = FALSE;
1423 static bool default_nb_params(int ftype, t_params bt[], t_atoms *at,
1424 t_param *p, int c_start, bool bB, bool bGenPairs)
1426 int i, j, ti, tj, ntype;
1428 t_param *pi = nullptr;
1429 int nr = bt[ftype].nr;
1430 int nral = NRAL(ftype);
1431 int nrfp = interaction_function[ftype].nrfpA;
1432 int nrfpB = interaction_function[ftype].nrfpB;
1434 if ((!bB && nrfp == 0) || (bB && nrfpB == 0))
1442 /* First test the generated-pair position to save
1443 * time when we have 1000*1000 entries for e.g. OPLS...
1445 ntype = static_cast<int>(std::sqrt(static_cast<double>(nr)));
1446 GMX_ASSERT(ntype * ntype == nr, "Number of pairs of generated non-bonded parameters should be a perfect square");
1449 ti = at->atom[p->a[0]].typeB;
1450 tj = at->atom[p->a[1]].typeB;
1454 ti = at->atom[p->a[0]].type;
1455 tj = at->atom[p->a[1]].type;
1457 pi = &(bt[ftype].param[ntype*ti+tj]);
1458 bFound = ((ti == pi->a[0]) && (tj == pi->a[1]));
1461 /* Search explicitly if we didnt find it */
1464 for (i = 0; ((i < nr) && !bFound); i++)
1466 pi = &(bt[ftype].param[i]);
1469 for (j = 0; ((j < nral) &&
1470 (at->atom[p->a[j]].typeB == pi->a[j])); j++)
1477 for (j = 0; ((j < nral) &&
1478 (at->atom[p->a[j]].type == pi->a[j])); j++)
1483 bFound = (j == nral);
1491 if (nrfp+nrfpB > MAXFORCEPARAM)
1493 gmx_incons("Too many force parameters");
1495 for (j = c_start; (j < nrfpB); j++)
1497 p->c[nrfp+j] = pi->c[j];
1502 for (j = c_start; (j < nrfp); j++)
1510 for (j = c_start; (j < nrfp); j++)
1518 static bool default_cmap_params(t_params bondtype[],
1519 t_atoms *at, gpp_atomtype_t atype,
1520 t_param *p, bool bB,
1521 int *cmap_type, int *nparam_def,
1524 int i, nparam_found;
1526 bool bFound = FALSE;
1527 char errbuf[STRLEN];
1532 /* Match the current cmap angle against the list of cmap_types */
1533 for (i = 0; i < bondtype[F_CMAP].nct && !bFound; i += 6)
1542 (get_atomtype_batype(at->atom[p->a[0]].type, atype) == bondtype[F_CMAP].cmap_types[i]) &&
1543 (get_atomtype_batype(at->atom[p->a[1]].type, atype) == bondtype[F_CMAP].cmap_types[i+1]) &&
1544 (get_atomtype_batype(at->atom[p->a[2]].type, atype) == bondtype[F_CMAP].cmap_types[i+2]) &&
1545 (get_atomtype_batype(at->atom[p->a[3]].type, atype) == bondtype[F_CMAP].cmap_types[i+3]) &&
1546 (get_atomtype_batype(at->atom[p->a[4]].type, atype) == bondtype[F_CMAP].cmap_types[i+4]))
1548 /* Found cmap torsion */
1550 ct = bondtype[F_CMAP].cmap_types[i+5];
1556 /* If we did not find a matching type for this cmap torsion */
1559 sprintf(errbuf, "Unknown cmap torsion between atoms %d %d %d %d %d",
1560 p->a[0]+1, p->a[1]+1, p->a[2]+1, p->a[3]+1, p->a[4]+1);
1561 warning_error_and_exit(wi, errbuf, FARGS);
1564 *nparam_def = nparam_found;
1570 /* Returns the number of exact atom type matches, i.e. non wild-card matches,
1571 * returns -1 when there are no matches at all.
1573 static int natom_match(t_param *pi,
1574 int type_i, int type_j, int type_k, int type_l,
1575 const gpp_atomtype* atype)
1577 if ((pi->ai() == -1 || get_atomtype_batype(type_i, atype) == pi->ai()) &&
1578 (pi->aj() == -1 || get_atomtype_batype(type_j, atype) == pi->aj()) &&
1579 (pi->ak() == -1 || get_atomtype_batype(type_k, atype) == pi->ak()) &&
1580 (pi->al() == -1 || get_atomtype_batype(type_l, atype) == pi->al()))
1583 (pi->ai() == -1 ? 0 : 1) +
1584 (pi->aj() == -1 ? 0 : 1) +
1585 (pi->ak() == -1 ? 0 : 1) +
1586 (pi->al() == -1 ? 0 : 1);
1594 static bool default_params(int ftype, t_params bt[],
1595 t_atoms *at, gpp_atomtype_t atype,
1596 t_param *p, bool bB,
1597 t_param **param_def,
1602 t_param *pi = nullptr;
1603 t_param *pj = nullptr;
1604 int nr = bt[ftype].nr;
1605 int nral = NRAL(ftype);
1606 int nrfpA = interaction_function[ftype].nrfpA;
1607 int nrfpB = interaction_function[ftype].nrfpB;
1609 if ((!bB && nrfpA == 0) || (bB && nrfpB == 0))
1617 if (ftype == F_PDIHS || ftype == F_RBDIHS || ftype == F_IDIHS || ftype == F_PIDIHS)
1619 int nmatch_max = -1;
1623 /* For dihedrals we allow wildcards. We choose the first type
1624 * that has the most real matches, i.e. non-wildcard matches.
1626 for (t = 0; ((t < nr) && nmatch_max < 4); t++)
1631 pt = &(bt[ftype].param[t]);
1634 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);
1638 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);
1640 if (nmatch > nmatch_max)
1642 nmatch_max = nmatch;
1652 pi = &(bt[ftype].param[i]);
1655 /* Find additional matches for this dihedral - necessary
1657 * The rule in that case is that additional matches
1658 * HAVE to be on adjacent lines!
1661 /* Continue from current i value */
1662 for (j = i + 2; j < nr && bSame; j += 2)
1664 pj = &(bt[ftype].param[j]);
1665 bSame = (pi->ai() == pj->ai() && pi->aj() == pj->aj() && pi->ak() == pj->ak() && pi->al() == pj->al());
1670 /* nparam_found will be increased as long as the numbers match */
1674 else /* Not a dihedral */
1678 for (i = 0; ((i < nr) && !bFound); i++)
1680 pi = &(bt[ftype].param[i]);
1683 for (j = 0; ((j < nral) &&
1684 (get_atomtype_batype(at->atom[p->a[j]].typeB, atype) == pi->a[j])); j++)
1691 for (j = 0; ((j < nral) &&
1692 (get_atomtype_batype(at->atom[p->a[j]].type, atype) == pi->a[j])); j++)
1697 bFound = (j == nral);
1706 *nparam_def = nparam_found;
1713 void push_bond(directive d, t_params bondtype[], t_params bond[],
1714 t_atoms *at, gpp_atomtype_t atype, char *line,
1715 bool bBonded, bool bGenPairs, real fudgeQQ,
1716 bool bZero, bool *bWarn_copy_A_B,
1719 const char *aaformat[MAXATOMLIST] = {
1727 const char *asformat[MAXATOMLIST] = {
1732 "%*s%*s%*s%*s%*s%*s",
1733 "%*s%*s%*s%*s%*s%*s%*s"
1735 const char *ccformat = "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf";
1736 int nr, i, j, nral, nral_fmt, nread, ftype;
1737 char format[STRLEN];
1738 /* One force parameter more, so we can check if we read too many */
1739 double cc[MAXFORCEPARAM+1];
1740 int aa[MAXATOMLIST+1];
1741 t_param param, *param_defA, *param_defB;
1742 bool bFoundA = FALSE, bFoundB = FALSE, bDef, bPert, bSwapParity = FALSE;
1743 int nparam_defA, nparam_defB;
1744 char errbuf[STRLEN];
1746 nparam_defA = nparam_defB = 0;
1748 ftype = ifunc_index(d, 1);
1750 for (j = 0; j < MAXATOMLIST; j++)
1754 bDef = (NRFP(ftype) > 0);
1756 if (ftype == F_SETTLE)
1758 /* SETTLE acts on 3 atoms, but the topology format only specifies
1759 * the first atom (for historical reasons).
1768 nread = sscanf(line, aaformat[nral_fmt-1],
1769 &aa[0], &aa[1], &aa[2], &aa[3], &aa[4], &aa[5]);
1771 if (ftype == F_SETTLE)
1778 if (nread < nral_fmt)
1783 else if (nread > nral_fmt)
1785 /* this is a hack to allow for virtual sites with swapped parity */
1786 bSwapParity = (aa[nral] < 0);
1789 aa[nral] = -aa[nral];
1791 ftype = ifunc_index(d, aa[nral]);
1800 sprintf(errbuf, "Negative function types only allowed for %s and %s",
1801 interaction_function[F_VSITE3FAD].longname,
1802 interaction_function[F_VSITE3OUT].longname);
1803 warning_error_and_exit(wi, errbuf, FARGS);
1809 /* Check for double atoms and atoms out of bounds */
1810 for (i = 0; (i < nral); i++)
1812 if (aa[i] < 1 || aa[i] > at->nr)
1814 sprintf(errbuf, "Atom index (%d) in %s out of bounds (1-%d).\n"
1815 "This probably means that you have inserted topology section \"%s\"\n"
1816 "in a part belonging to a different molecule than you intended to.\n"
1817 "In that case move the \"%s\" section to the right molecule.",
1818 aa[i], dir2str(d), at->nr, dir2str(d), dir2str(d));
1819 warning_error_and_exit(wi, errbuf, FARGS);
1821 for (j = i+1; (j < nral); j++)
1823 GMX_ASSERT(j < MAXATOMLIST + 1, "Values from nral=NRAL() will satisfy this, we assert to keep gcc 4 happy");
1826 sprintf(errbuf, "Duplicate atom index (%d) in %s", aa[i], dir2str(d));
1827 if (ftype == F_ANGRES)
1829 /* Since the angle restraints uses 2 pairs of atoms to
1830 * defines an angle between vectors, it can be useful
1831 * to use one atom twice, so we only issue a note here.
1833 warning_note(wi, errbuf);
1837 warning_error(wi, errbuf);
1843 /* default force parameters */
1844 for (j = 0; (j < MAXATOMLIST); j++)
1846 param.a[j] = aa[j]-1;
1848 for (j = 0; (j < MAXFORCEPARAM); j++)
1853 /* Get force params for normal and free energy perturbation
1854 * studies, as determined by types!
1859 bFoundA = default_params(ftype, bondtype, at, atype, ¶m, FALSE, ¶m_defA, &nparam_defA);
1862 /* Copy the A-state and B-state default parameters. */
1863 GMX_ASSERT(NRFPA(ftype)+NRFPB(ftype) <= MAXFORCEPARAM, "Bonded interactions may have at most 12 parameters");
1864 for (j = 0; (j < NRFPA(ftype)+NRFPB(ftype)); j++)
1866 param.c[j] = param_defA->c[j];
1869 bFoundB = default_params(ftype, bondtype, at, atype, ¶m, TRUE, ¶m_defB, &nparam_defB);
1872 /* Copy only the B-state default parameters */
1873 for (j = NRFPA(ftype); (j < NRFP(ftype)); j++)
1875 param.c[j] = param_defB->c[j];
1879 else if (ftype == F_LJ14)
1881 bFoundA = default_nb_params(ftype, bondtype, at, ¶m, 0, FALSE, bGenPairs);
1882 bFoundB = default_nb_params(ftype, bondtype, at, ¶m, 0, TRUE, bGenPairs);
1884 else if (ftype == F_LJC14_Q)
1886 param.c[0] = fudgeQQ;
1887 /* Fill in the A-state charges as default parameters */
1888 param.c[1] = at->atom[param.a[0]].q;
1889 param.c[2] = at->atom[param.a[1]].q;
1890 /* The default LJ parameters are the standard 1-4 parameters */
1891 bFoundA = default_nb_params(F_LJ14, bondtype, at, ¶m, 3, FALSE, bGenPairs);
1894 else if (ftype == F_LJC_PAIRS_NB)
1896 /* Defaults are not supported here */
1902 gmx_incons("Unknown function type in push_bond");
1905 if (nread > nral_fmt)
1907 /* Manually specified parameters - in this case we discard multiple torsion info! */
1909 strcpy(format, asformat[nral_fmt-1]);
1910 strcat(format, ccformat);
1912 nread = sscanf(line, format, &cc[0], &cc[1], &cc[2], &cc[3], &cc[4], &cc[5],
1913 &cc[6], &cc[7], &cc[8], &cc[9], &cc[10], &cc[11], &cc[12]);
1915 if ((nread == NRFPA(ftype)) && (NRFPB(ftype) != 0))
1917 /* We only have to issue a warning if these atoms are perturbed! */
1919 for (j = 0; (j < nral); j++)
1921 bPert = bPert || PERTURBED(at->atom[param.a[j]]);
1924 if (bPert && *bWarn_copy_A_B)
1927 "Some parameters for bonded interaction involving perturbed atoms are specified explicitly in state A, but not B - copying A to B");
1928 warning(wi, errbuf);
1929 *bWarn_copy_A_B = FALSE;
1932 /* If only the A parameters were specified, copy them to the B state */
1933 /* The B-state parameters correspond to the first nrfpB
1934 * A-state parameters.
1936 for (j = 0; (j < NRFPB(ftype)); j++)
1938 cc[nread++] = cc[j];
1942 /* If nread was 0 or EOF, no parameters were read => use defaults.
1943 * If nread was nrfpA we copied above so nread=nrfp.
1944 * If nread was nrfp we are cool.
1945 * For F_LJC14_Q we allow supplying fudgeQQ only.
1946 * Anything else is an error!
1948 if ((nread != 0) && (nread != EOF) && (nread != NRFP(ftype)) &&
1949 !(ftype == F_LJC14_Q && nread == 1))
1951 sprintf(errbuf, "Incorrect number of parameters - found %d, expected %d or %d for %s (after the function type).",
1952 nread, NRFPA(ftype), NRFP(ftype), interaction_function[ftype].longname);
1953 warning_error_and_exit(wi, errbuf, FARGS);
1956 for (j = 0; (j < nread); j++)
1961 /* Check whether we have to use the defaults */
1962 if (nread == NRFP(ftype))
1971 /* nread now holds the number of force parameters read! */
1976 /* When we have multiple terms it would be very dangerous to allow perturbations to a different atom type! */
1977 if (ftype == F_PDIHS)
1979 if ((nparam_defA != nparam_defB) || ((nparam_defA > 1 || nparam_defB > 1) && (param_defA != param_defB)))
1982 "Cannot automatically perturb a torsion with multiple terms to different form.\n"
1983 "Please specify perturbed parameters manually for this torsion in your topology!");
1984 warning_error(wi, errbuf);
1988 if (nread > 0 && nread < NRFPA(ftype))
1990 /* Issue an error, do not use defaults */
1991 sprintf(errbuf, "Not enough parameters, there should be at least %d (or 0 for defaults)", NRFPA(ftype));
1992 warning_error(wi, errbuf);
1995 if (nread == 0 || nread == EOF)
1999 if (interaction_function[ftype].flags & IF_VSITE)
2001 /* set them to NOTSET, will be calculated later */
2002 for (j = 0; (j < MAXFORCEPARAM); j++)
2004 param.c[j] = NOTSET;
2009 param.c1() = -1; /* flag to swap parity of vsite construction */
2016 fprintf(stderr, "NOTE: No default %s types, using zeroes\n",
2017 interaction_function[ftype].longname);
2021 sprintf(errbuf, "No default %s types", interaction_function[ftype].longname);
2022 warning_error(wi, errbuf);
2033 param.c0() = 360 - param.c0();
2036 param.c2() = -param.c2();
2043 /* We only have to issue a warning if these atoms are perturbed! */
2045 for (j = 0; (j < nral); j++)
2047 bPert = bPert || PERTURBED(at->atom[param.a[j]]);
2052 sprintf(errbuf, "No default %s types for perturbed atoms, "
2053 "using normal values", interaction_function[ftype].longname);
2054 warning(wi, errbuf);
2060 if ((ftype == F_PDIHS || ftype == F_ANGRES || ftype == F_ANGRESZ)
2061 && param.c[5] != param.c[2])
2063 sprintf(errbuf, "%s multiplicity can not be perturbed %f!=%f",
2064 interaction_function[ftype].longname,
2065 param.c[2], param.c[5]);
2066 warning_error_and_exit(wi, errbuf, FARGS);
2069 if (IS_TABULATED(ftype) && param.c[2] != param.c[0])
2071 sprintf(errbuf, "%s table number can not be perturbed %d!=%d",
2072 interaction_function[ftype].longname,
2073 static_cast<int>(param.c[0]+0.5), static_cast<int>(param.c[2]+0.5));
2074 warning_error_and_exit(wi, errbuf, FARGS);
2077 /* Dont add R-B dihedrals where all parameters are zero (no interaction) */
2078 if (ftype == F_RBDIHS)
2081 for (i = 0; i < NRFP(ftype); i++)
2083 if (param.c[i] != 0)
2094 /* Put the values in the appropriate arrays */
2095 add_param_to_list (&bond[ftype], ¶m);
2097 /* Push additional torsions from FF for ftype==9 if we have them.
2098 * We have already checked that the A/B states do not differ in this case,
2099 * so we do not have to double-check that again, or the vsite stuff.
2100 * In addition, those torsions cannot be automatically perturbed.
2102 if (bDef && ftype == F_PDIHS)
2104 for (i = 1; i < nparam_defA; i++)
2106 /* Advance pointer! */
2108 for (j = 0; (j < NRFPA(ftype)+NRFPB(ftype)); j++)
2110 param.c[j] = param_defA->c[j];
2112 /* And push the next term for this torsion */
2113 add_param_to_list (&bond[ftype], ¶m);
2118 void push_cmap(directive d, t_params bondtype[], t_params bond[],
2119 t_atoms *at, gpp_atomtype_t atype, char *line,
2122 const char *aaformat[MAXATOMLIST+1] =
2133 int i, j, ftype, nral, nread, ncmap_params;
2135 int aa[MAXATOMLIST+1];
2136 char errbuf[STRLEN];
2140 ftype = ifunc_index(d, 1);
2144 nread = sscanf(line, aaformat[nral-1],
2145 &aa[0], &aa[1], &aa[2], &aa[3], &aa[4], &aa[5]);
2152 else if (nread == nral)
2154 ftype = ifunc_index(d, 1);
2157 /* Check for double atoms and atoms out of bounds */
2158 for (i = 0; i < nral; i++)
2160 if (aa[i] < 1 || aa[i] > at->nr)
2162 sprintf(errbuf, "Atom index (%d) in %s out of bounds (1-%d).\n"
2163 "This probably means that you have inserted topology section \"%s\"\n"
2164 "in a part belonging to a different molecule than you intended to.\n"
2165 "In that case move the \"%s\" section to the right molecule.",
2166 aa[i], dir2str(d), at->nr, dir2str(d), dir2str(d));
2167 warning_error_and_exit(wi, errbuf, FARGS);
2170 for (j = i+1; (j < nral); j++)
2174 sprintf(errbuf, "Duplicate atom index (%d) in %s", aa[i], dir2str(d));
2175 warning_error(wi, errbuf);
2180 /* default force parameters */
2181 for (j = 0; (j < MAXATOMLIST); j++)
2183 param.a[j] = aa[j]-1;
2185 for (j = 0; (j < MAXFORCEPARAM); j++)
2190 /* Get the cmap type for this cmap angle */
2191 bFound = default_cmap_params(bondtype, at, atype, ¶m, FALSE, &cmap_type, &ncmap_params, wi);
2193 /* We want exactly one parameter (the cmap type in state A (currently no state B) back */
2194 if (bFound && ncmap_params == 1)
2196 /* Put the values in the appropriate arrays */
2197 param.c[0] = cmap_type;
2198 add_param_to_list(&bond[ftype], ¶m);
2202 /* This is essentially the same check as in default_cmap_params() done one more time */
2203 sprintf(errbuf, "Unable to assign a cmap type to torsion %d %d %d %d and %d\n",
2204 param.a[0]+1, param.a[1]+1, param.a[2]+1, param.a[3]+1, param.a[4]+1);
2205 warning_error_and_exit(wi, errbuf, FARGS);
2211 void push_vsitesn(directive d, t_params bond[],
2212 t_atoms *at, char *line,
2216 int type, ftype, j, n, ret, nj, a;
2218 double *weight = nullptr, weight_tot;
2220 char errbuf[STRLEN];
2222 /* default force parameters */
2223 for (j = 0; (j < MAXATOMLIST); j++)
2225 param.a[j] = NOTSET;
2227 for (j = 0; (j < MAXFORCEPARAM); j++)
2233 ret = sscanf(ptr, "%d%n", &a, &n);
2237 sprintf(errbuf, "Expected an atom index in section \"%s\"",
2239 warning_error_and_exit(wi, errbuf, FARGS);
2244 sscanf(ptr, "%d%n", &type, &n);
2246 ftype = ifunc_index(d, type);
2252 ret = sscanf(ptr, "%d%n", &a, &n);
2259 srenew(weight, nj+20);
2268 /* Here we use the A-state mass as a parameter.
2269 * Note that the B-state mass has no influence.
2271 weight[nj] = at->atom[atc[nj]].m;
2275 ret = sscanf(ptr, "%lf%n", &(weight[nj]), &n);
2279 sprintf(errbuf, "No weight or negative weight found for vsiten constructing atom %d (atom index %d)",
2281 warning_error_and_exit(wi, errbuf, FARGS);
2285 sprintf(errbuf, "Unknown vsiten type %d", type);
2286 warning_error_and_exit(wi, errbuf, FARGS);
2288 weight_tot += weight[nj];
2296 sprintf(errbuf, "Expected more than one atom index in section \"%s\"",
2298 warning_error_and_exit(wi, errbuf, FARGS);
2301 if (weight_tot == 0)
2303 warning_error_and_exit(wi, "The total mass of the construting atoms is zero", FARGS);
2306 for (j = 0; j < nj; j++)
2308 param.a[1] = atc[j];
2310 param.c[1] = weight[j]/weight_tot;
2311 /* Put the values in the appropriate arrays */
2312 add_param_to_list (&bond[ftype], ¶m);
2319 void push_mol(int nrmols, t_molinfo mols[], char *pline, int *whichmol,
2324 char errbuf[STRLEN];
2326 if (sscanf(pline, "%s%d", type, nrcopies) != 2)
2332 /* Search moleculename.
2333 * Here we originally only did case insensitive matching. But because
2334 * some PDB files can have many chains and use case to generate more
2335 * chain-identifiers, which in turn end up in our moleculetype name,
2336 * we added support for case-sensitivity.
2342 for (int i = 0; i < nrmols; i++)
2344 if (strcmp(type, *(mols[i].name)) == 0)
2349 if (gmx_strcasecmp(type, *(mols[i].name)) == 0)
2358 // select the case sensitive match
2359 *whichmol = matchcs;
2363 // avoid matching case-insensitive when we have multiple matches
2366 sprintf(errbuf, "For moleculetype '%s' in [ system ] %d case insensitive matches, but %d case sensitive matches were found. Check the case of the characters in the moleculetypes.", type, nrci, nrcs);
2367 warning_error_and_exit(wi, errbuf, FARGS);
2371 // select the unique case insensitive match
2372 *whichmol = matchci;
2376 sprintf(errbuf, "No such moleculetype %s", type);
2377 warning_error_and_exit(wi, errbuf, FARGS);
2382 void init_block2(t_block2 *b2, int natom)
2387 snew(b2->nra, b2->nr);
2388 snew(b2->a, b2->nr);
2389 for (i = 0; (i < b2->nr); i++)
2395 void done_block2(t_block2 *b2)
2401 for (i = 0; (i < b2->nr); i++)
2411 void push_excl(char *line, t_block2 *b2, warninp_t wi)
2415 char base[STRLEN], format[STRLEN];
2416 char errbuf[STRLEN];
2418 if (sscanf(line, "%d", &i) == 0)
2423 if ((1 <= i) && (i <= b2->nr))
2431 strcpy(base, "%*d");
2434 strcpy(format, base);
2435 strcat(format, "%d");
2436 n = sscanf(line, format, &j);
2439 if ((1 <= j) && (j <= b2->nr))
2442 srenew(b2->a[i], ++(b2->nra[i]));
2443 b2->a[i][b2->nra[i]-1] = j;
2444 /* also add the reverse exclusion! */
2445 srenew(b2->a[j], ++(b2->nra[j]));
2446 b2->a[j][b2->nra[j]-1] = i;
2447 strcat(base, "%*d");
2451 sprintf(errbuf, "Invalid Atomnr j: %d, b2->nr: %d\n", j, b2->nr);
2452 warning_error_and_exit(wi, errbuf, FARGS);
2459 void b_to_b2(t_blocka *b, t_block2 *b2)
2464 for (i = 0; (i < b->nr); i++)
2466 for (j = b->index[i]; (j < b->index[i+1]); j++)
2469 srenew(b2->a[i], ++b2->nra[i]);
2470 b2->a[i][b2->nra[i]-1] = a;
2475 void b2_to_b(t_block2 *b2, t_blocka *b)
2481 for (i = 0; (i < b2->nr); i++)
2484 for (j = 0; (j < b2->nra[i]); j++)
2486 b->a[nra+j] = b2->a[i][j];
2490 /* terminate list */
2494 void merge_excl(t_blocka *excl, t_block2 *b2, warninp_t wi)
2499 char errbuf[STRLEN];
2505 else if (b2->nr != excl->nr)
2507 sprintf(errbuf, "DEATH HORROR: b2->nr = %d, while excl->nr = %d",
2509 warning_error_and_exit(wi, errbuf, FARGS);
2512 /* First copy all entries from excl to b2 */
2515 /* Count and sort the exclusions */
2517 for (i = 0; (i < b2->nr); i++)
2521 /* remove double entries */
2522 std::sort(b2->a[i], b2->a[i]+b2->nra[i]);
2524 for (j = 1; (j < b2->nra[i]); j++)
2526 if (b2->a[i][j] != b2->a[i][k-1])
2528 b2->a[i][k] = b2->a[i][j];
2537 srenew(excl->a, excl->nra);
2542 int add_atomtype_decoupled(t_symtab *symtab, gpp_atomtype_t at,
2543 t_nbparam ***nbparam, t_nbparam ***pair)
2549 /* Define an atom type with all parameters set to zero (no interactions) */
2552 /* Type for decoupled atoms could be anything,
2553 * this should be changed automatically later when required.
2555 atom.ptype = eptAtom;
2556 for (i = 0; (i < MAXFORCEPARAM); i++)
2561 nr = add_atomtype(at, symtab, &atom, "decoupled", ¶m, -1, 0);
2563 /* Add space in the non-bonded parameters matrix */
2564 realloc_nb_params(at, nbparam, pair);
2569 static void convert_pairs_to_pairsQ(t_params *plist,
2570 real fudgeQQ, t_atoms *atoms)
2572 t_param *paramp1, *paramp2, *paramnew;
2573 int i, j, p1nr, p2nr, p2newnr;
2575 /* Add the pair list to the pairQ list */
2576 p1nr = plist[F_LJ14].nr;
2577 p2nr = plist[F_LJC14_Q].nr;
2578 p2newnr = p1nr + p2nr;
2579 snew(paramnew, p2newnr);
2581 paramp1 = plist[F_LJ14].param;
2582 paramp2 = plist[F_LJC14_Q].param;
2584 /* Fill in the new F_LJC14_Q array with the old one. NOTE:
2585 it may be possible to just ADD the converted F_LJ14 array
2586 to the old F_LJC14_Q array, but since we have to create
2587 a new sized memory structure, better just to deep copy it all.
2590 for (i = 0; i < p2nr; i++)
2592 /* Copy over parameters */
2593 for (j = 0; j < 5; j++) /* entries are 0-4 for F_LJC14_Q */
2595 paramnew[i].c[j] = paramp2[i].c[j];
2598 /* copy over atoms */
2599 for (j = 0; j < 2; j++)
2601 paramnew[i].a[j] = paramp2[i].a[j];
2605 for (i = p2nr; i < p2newnr; i++)
2609 /* Copy over parameters */
2610 paramnew[i].c[0] = fudgeQQ;
2611 paramnew[i].c[1] = atoms->atom[paramp1[j].a[0]].q;
2612 paramnew[i].c[2] = atoms->atom[paramp1[j].a[1]].q;
2613 paramnew[i].c[3] = paramp1[j].c[0];
2614 paramnew[i].c[4] = paramp1[j].c[1];
2616 /* copy over atoms */
2617 paramnew[i].a[0] = paramp1[j].a[0];
2618 paramnew[i].a[1] = paramp1[j].a[1];
2621 /* free the old pairlists */
2622 sfree(plist[F_LJC14_Q].param);
2623 sfree(plist[F_LJ14].param);
2625 /* now assign the new data to the F_LJC14_Q structure */
2626 plist[F_LJC14_Q].param = paramnew;
2627 plist[F_LJC14_Q].nr = p2newnr;
2629 /* Empty the LJ14 pairlist */
2630 plist[F_LJ14].nr = 0;
2631 plist[F_LJ14].param = nullptr;
2634 static void generate_LJCpairsNB(t_molinfo *mol, int nb_funct, t_params *nbp, warninp_t wi)
2636 int n, ntype, i, j, k;
2641 char errbuf[STRLEN];
2644 atom = mol->atoms.atom;
2646 ntype = static_cast<int>(std::sqrt(static_cast<double>(nbp->nr)));
2647 GMX_ASSERT(ntype * ntype == nbp->nr, "Number of pairs of generated non-bonded parameters should be a perfect square");
2649 for (i = 0; i < MAXATOMLIST; i++)
2651 param.a[i] = NOTSET;
2653 for (i = 0; i < MAXFORCEPARAM; i++)
2655 param.c[i] = NOTSET;
2658 /* Add a pair interaction for all non-excluded atom pairs */
2660 for (i = 0; i < n; i++)
2662 for (j = i+1; j < n; j++)
2665 for (k = excl->index[i]; k < excl->index[i+1]; k++)
2667 if (excl->a[k] == j)
2674 if (nb_funct != F_LJ)
2676 sprintf(errbuf, "Can only generate non-bonded pair interactions for Van der Waals type Lennard-Jones");
2677 warning_error_and_exit(wi, errbuf, FARGS);
2681 param.c[0] = atom[i].q;
2682 param.c[1] = atom[j].q;
2683 param.c[2] = nbp->param[ntype*atom[i].type+atom[j].type].c[0];
2684 param.c[3] = nbp->param[ntype*atom[i].type+atom[j].type].c[1];
2685 add_param_to_list(&mol->plist[F_LJC_PAIRS_NB], ¶m);
2691 static void set_excl_all(t_blocka *excl)
2695 /* Get rid of the current exclusions and exclude all atom pairs */
2697 excl->nra = nat*nat;
2698 srenew(excl->a, excl->nra);
2700 for (i = 0; i < nat; i++)
2703 for (j = 0; j < nat; j++)
2708 excl->index[nat] = k;
2711 static void decouple_atoms(t_atoms *atoms, int atomtype_decouple,
2712 int couple_lam0, int couple_lam1,
2713 const char *mol_name, warninp_t wi)
2716 char errbuf[STRLEN];
2718 for (i = 0; i < atoms->nr; i++)
2722 atom = &atoms->atom[i];
2724 if (atom->qB != atom->q || atom->typeB != atom->type)
2726 sprintf(errbuf, "Atom %d in molecule type '%s' has different A and B state charges and/or atom types set in the topology file as well as through the mdp option '%s'. You can not use both these methods simultaneously.",
2727 i + 1, mol_name, "couple-moltype");
2728 warning_error_and_exit(wi, errbuf, FARGS);
2731 if (couple_lam0 == ecouplamNONE || couple_lam0 == ecouplamVDW)
2735 if (couple_lam0 == ecouplamNONE || couple_lam0 == ecouplamQ)
2737 atom->type = atomtype_decouple;
2739 if (couple_lam1 == ecouplamNONE || couple_lam1 == ecouplamVDW)
2743 if (couple_lam1 == ecouplamNONE || couple_lam1 == ecouplamQ)
2745 atom->typeB = atomtype_decouple;
2750 void convert_moltype_couple(t_molinfo *mol, int atomtype_decouple, real fudgeQQ,
2751 int couple_lam0, int couple_lam1,
2752 bool bCoupleIntra, int nb_funct, t_params *nbp,
2755 convert_pairs_to_pairsQ(mol->plist, fudgeQQ, &mol->atoms);
2758 generate_LJCpairsNB(mol, nb_funct, nbp, wi);
2759 set_excl_all(&mol->excls);
2761 decouple_atoms(&mol->atoms, atomtype_decouple, couple_lam0, couple_lam1,