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.
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/mdtypes/md_enums.h"
57 #include "gromacs/topology/ifunc.h"
58 #include "gromacs/topology/symtab.h"
59 #include "gromacs/utility/arrayref.h"
60 #include "gromacs/utility/cstringutil.h"
61 #include "gromacs/utility/fatalerror.h"
62 #include "gromacs/utility/gmxassert.h"
63 #include "gromacs/utility/smalloc.h"
65 void generate_nbparams(int comb, int ftype, t_params *plist, gpp_atomtype_t atype,
70 real c, bi, bj, ci, cj, ci0, ci1, ci2, cj0, cj1, cj2;
73 /* Lean mean shortcuts */
74 nr = get_atomtype_ntypes(atype);
76 snew(plist->param, nr*nr);
79 /* Fill the matrix with force parameters */
87 for (i = k = 0; (i < nr); i++)
89 for (j = 0; (j < nr); j++, k++)
91 for (nf = 0; (nf < nrfp); nf++)
93 ci = get_atomtype_nbparam(i, nf, atype);
94 cj = get_atomtype_nbparam(j, nf, atype);
95 c = std::sqrt(ci * cj);
96 plist->param[k].c[nf] = c;
102 case eCOMB_ARITHMETIC:
103 /* c0 and c1 are sigma and epsilon */
104 for (i = k = 0; (i < nr); i++)
106 for (j = 0; (j < nr); j++, k++)
108 ci0 = get_atomtype_nbparam(i, 0, atype);
109 cj0 = get_atomtype_nbparam(j, 0, atype);
110 ci1 = get_atomtype_nbparam(i, 1, atype);
111 cj1 = get_atomtype_nbparam(j, 1, atype);
112 plist->param[k].c[0] = (fabs(ci0) + fabs(cj0))*0.5;
113 /* Negative sigma signals that c6 should be set to zero later,
114 * so we need to propagate that through the combination rules.
116 if (ci0 < 0 || cj0 < 0)
118 plist->param[k].c[0] *= -1;
120 plist->param[k].c[1] = std::sqrt(ci1*cj1);
125 case eCOMB_GEOM_SIG_EPS:
126 /* c0 and c1 are sigma and epsilon */
127 for (i = k = 0; (i < nr); i++)
129 for (j = 0; (j < nr); j++, k++)
131 ci0 = get_atomtype_nbparam(i, 0, atype);
132 cj0 = get_atomtype_nbparam(j, 0, atype);
133 ci1 = get_atomtype_nbparam(i, 1, atype);
134 cj1 = get_atomtype_nbparam(j, 1, atype);
135 plist->param[k].c[0] = std::sqrt(std::fabs(ci0*cj0));
136 /* Negative sigma signals that c6 should be set to zero later,
137 * so we need to propagate that through the combination rules.
139 if (ci0 < 0 || cj0 < 0)
141 plist->param[k].c[0] *= -1;
143 plist->param[k].c[1] = std::sqrt(ci1*cj1);
149 sprintf(errbuf, "No such combination rule %d", comb);
150 warning_error_and_exit(wi, errbuf, FARGS);
154 gmx_incons("Topology processing, generate nb parameters");
159 /* Buckingham rules */
160 for (i = k = 0; (i < nr); i++)
162 for (j = 0; (j < nr); j++, k++)
164 ci0 = get_atomtype_nbparam(i, 0, atype);
165 cj0 = get_atomtype_nbparam(j, 0, atype);
166 ci2 = get_atomtype_nbparam(i, 2, atype);
167 cj2 = get_atomtype_nbparam(j, 2, atype);
168 bi = get_atomtype_nbparam(i, 1, atype);
169 bj = get_atomtype_nbparam(j, 1, atype);
170 plist->param[k].c[0] = std::sqrt(ci0 * cj0);
171 if ((bi == 0) || (bj == 0))
173 plist->param[k].c[1] = 0;
177 plist->param[k].c[1] = 2.0/(1/bi+1/bj);
179 plist->param[k].c[2] = std::sqrt(ci2 * cj2);
185 sprintf(errbuf, "Invalid nonbonded type %s",
186 interaction_function[ftype].longname);
187 warning_error(wi, errbuf);
191 static void realloc_nb_params(gpp_atomtype_t at,
192 t_nbparam ***nbparam, t_nbparam ***pair)
194 /* Add space in the non-bonded parameters matrix */
195 int atnr = get_atomtype_ntypes(at);
196 srenew(*nbparam, atnr);
197 snew((*nbparam)[atnr-1], atnr);
201 snew((*pair)[atnr-1], atnr);
205 static void copy_B_from_A(int ftype, double *c)
209 nrfpA = NRFPA(ftype);
210 nrfpB = NRFPB(ftype);
212 /* Copy the B parameters from the first nrfpB A parameters */
213 for (i = 0; (i < nrfpB); i++)
219 void push_at (t_symtab *symtab, gpp_atomtype_t at, t_bond_atomtype bat,
220 char *line, int nb_funct,
221 t_nbparam ***nbparam, t_nbparam ***pair,
228 t_xlate xl[eptNR] = {
236 int nr, i, nfields, j, pt, nfp0 = -1;
237 int batype_nr, nread;
238 char type[STRLEN], btype[STRLEN], ptype[STRLEN];
240 double c[MAXFORCEPARAM];
241 char tmpfield[12][100]; /* Max 12 fields of width 100 */
246 gmx_bool have_atomic_number;
247 gmx_bool have_bonded_type;
252 /* First assign input line to temporary array */
253 nfields = sscanf(line, "%s%s%s%s%s%s%s%s%s%s%s%s",
254 tmpfield[0], tmpfield[1], tmpfield[2], tmpfield[3], tmpfield[4], tmpfield[5],
255 tmpfield[6], tmpfield[7], tmpfield[8], tmpfield[9], tmpfield[10], tmpfield[11]);
257 /* Comments on optional fields in the atomtypes section:
259 * The force field format is getting a bit old. For OPLS-AA we needed
260 * to add a special bonded atomtype, and for Gerrit Groenhofs QM/MM stuff
261 * we also needed the atomic numbers.
262 * To avoid making all old or user-generated force fields unusable we
263 * have introduced both these quantities as optional columns, and do some
264 * acrobatics to check whether they are present or not.
265 * This will all look much nicer when we switch to XML... sigh.
267 * Field 0 (mandatory) is the nonbonded type name. (string)
268 * Field 1 (optional) is the bonded type (string)
269 * Field 2 (optional) is the atomic number (int)
270 * Field 3 (mandatory) is the mass (numerical)
271 * Field 4 (mandatory) is the charge (numerical)
272 * Field 5 (mandatory) is the particle type (single character)
273 * This is followed by a number of nonbonded parameters.
275 * The safest way to identify the format is the particle type field.
277 * So, here is what we do:
279 * A. Read in the first six fields as strings
280 * B. If field 3 (starting from 0) is a single char, we have neither
281 * bonded_type or atomic numbers.
282 * C. If field 5 is a single char we have both.
283 * D. If field 4 is a single char we check field 1. If this begins with
284 * an alphabetical character we have bonded types, otherwise atomic numbers.
293 if ( (strlen(tmpfield[5]) == 1) && isalpha(tmpfield[5][0]) )
295 have_bonded_type = TRUE;
296 have_atomic_number = TRUE;
298 else if ( (strlen(tmpfield[3]) == 1) && isalpha(tmpfield[3][0]) )
300 have_bonded_type = FALSE;
301 have_atomic_number = FALSE;
305 have_bonded_type = ( isalpha(tmpfield[1][0]) != 0 );
306 have_atomic_number = !have_bonded_type;
309 /* optional fields */
318 if (have_atomic_number)
320 if (have_bonded_type)
322 nread = sscanf(line, "%s%s%d%lf%lf%s%lf%lf",
323 type, btype, &atomnr, &m, &q, ptype, &c[0], &c[1]);
332 /* have_atomic_number && !have_bonded_type */
333 nread = sscanf(line, "%s%d%lf%lf%s%lf%lf",
334 type, &atomnr, &m, &q, ptype, &c[0], &c[1]);
344 if (have_bonded_type)
346 /* !have_atomic_number && have_bonded_type */
347 nread = sscanf(line, "%s%s%lf%lf%s%lf%lf",
348 type, btype, &m, &q, ptype, &c[0], &c[1]);
357 /* !have_atomic_number && !have_bonded_type */
358 nread = sscanf(line, "%s%lf%lf%s%lf%lf",
359 type, &m, &q, ptype, &c[0], &c[1]);
368 if (!have_bonded_type)
373 if (!have_atomic_number)
383 if (have_atomic_number)
385 if (have_bonded_type)
387 nread = sscanf(line, "%s%s%d%lf%lf%s%lf%lf%lf",
388 type, btype, &atomnr, &m, &q, ptype, &c[0], &c[1], &c[2]);
397 /* have_atomic_number && !have_bonded_type */
398 nread = sscanf(line, "%s%d%lf%lf%s%lf%lf%lf",
399 type, &atomnr, &m, &q, ptype, &c[0], &c[1], &c[2]);
409 if (have_bonded_type)
411 /* !have_atomic_number && have_bonded_type */
412 nread = sscanf(line, "%s%s%lf%lf%s%lf%lf%lf",
413 type, btype, &m, &q, ptype, &c[0], &c[1], &c[2]);
422 /* !have_atomic_number && !have_bonded_type */
423 nread = sscanf(line, "%s%lf%lf%s%lf%lf%lf",
424 type, &m, &q, ptype, &c[0], &c[1], &c[2]);
433 if (!have_bonded_type)
438 if (!have_atomic_number)
446 sprintf(errbuf, "Invalid function type %d in push_at", nb_funct);
447 warning_error_and_exit(wi, errbuf, FARGS);
449 for (j = nfp0; (j < MAXFORCEPARAM); j++)
454 if (strlen(type) == 1 && isdigit(type[0]))
456 warning_error_and_exit(wi, "Atom type names can't be single digits.", FARGS);
459 if (strlen(btype) == 1 && isdigit(btype[0]))
461 warning_error_and_exit(wi, "Bond atom type names can't be single digits.", FARGS);
464 /* Hack to read old topologies */
465 if (gmx_strcasecmp(ptype, "D") == 0)
469 for (j = 0; (j < eptNR); j++)
471 if (gmx_strcasecmp(ptype, xl[j].entry) == 0)
478 sprintf(errbuf, "Invalid particle type %s", ptype);
479 warning_error_and_exit(wi, errbuf, FARGS);
484 fprintf(debug, "ptype: %s\n", ptype_str[pt]);
490 for (i = 0; (i < MAXFORCEPARAM); i++)
495 if ((batype_nr = get_bond_atomtype_type(btype, bat)) == NOTSET)
497 add_bond_atomtype(bat, symtab, btype);
499 batype_nr = get_bond_atomtype_type(btype, bat);
501 if ((nr = get_atomtype_type(type, at)) != NOTSET)
503 sprintf(errbuf, "Overriding atomtype %s", type);
505 if ((nr = set_atomtype(nr, at, symtab, atom, type, param, batype_nr,
508 sprintf(errbuf, "Replacing atomtype %s failed", type);
509 warning_error_and_exit(wi, errbuf, FARGS);
512 else if ((add_atomtype(at, symtab, atom, type, param,
513 batype_nr, atomnr)) == NOTSET)
515 sprintf(errbuf, "Adding atomtype %s failed", type);
516 warning_error_and_exit(wi, errbuf, FARGS);
520 /* Add space in the non-bonded parameters matrix */
521 realloc_nb_params(at, nbparam, pair);
527 //! Return whether the contents of \c a and \c b are the same, considering also reversed order.
528 template <typename T>
529 static bool equalEitherForwardOrBackward(gmx::ArrayRef<const T> a, gmx::ArrayRef<const T> b)
531 return (std::equal(a.begin(), a.end(), b.begin()) ||
532 std::equal(a.begin(), a.end(), b.rbegin()));
535 static void push_bondtype(t_params * bt,
539 gmx_bool bAllowRepeat,
544 int nrfp = NRFP(ftype);
547 /* If bAllowRepeat is TRUE, we allow multiple entries as long as they
548 are on directly _adjacent_ lines.
551 /* First check if our atomtypes are _identical_ (not reversed) to the previous
552 entry. If they are not identical we search for earlier duplicates. If they are
553 we can skip it, since we already searched for the first line
557 bool isContinuationOfBlock = false;
558 if (bAllowRepeat && nr > 1)
560 isContinuationOfBlock = true;
561 for (int j = 0; j < nral; j++)
563 if (b->a[j] != bt->param[nr - 2].a[j])
565 isContinuationOfBlock = false;
570 /* Search for earlier duplicates if this entry was not a continuation
571 from the previous line.
573 bool addBondType = true;
574 bool haveWarned = false;
575 bool haveErrored = false;
576 for (int i = 0; (i < nr); i++)
578 gmx::ArrayRef<const int> bParams(b->a, b->a + nral);
579 gmx::ArrayRef<const int> testParams(bt->param[i].a, bt->param[i].a + nral);
580 if (equalEitherForwardOrBackward(bParams, testParams))
582 GMX_ASSERT(nrfp <= MAXFORCEPARAM, "This is ensured in other places, but we need this assert to keep the clang analyzer happy");
583 const bool identicalParameters = std::equal(bt->param[i].c, bt->param[i].c + nrfp, b->c);
585 if (!bAllowRepeat || identicalParameters)
590 if (!identicalParameters)
594 /* With dihedral type 9 we only allow for repeating
595 * of the same parameters with blocks with 1 entry.
596 * Allowing overriding is too complex to check.
598 if (!isContinuationOfBlock && !haveErrored)
600 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.");
604 else if (!haveWarned)
606 sprintf(errbuf, "Overriding %s parameters.%s",
607 interaction_function[ftype].longname,
609 "\nUse dihedraltype 9 to allow several multiplicity terms. Only consecutive lines are combined. Non-consective lines overwrite each other."
613 fprintf(stderr, " old: ");
614 for (int j = 0; (j < nrfp); j++)
616 fprintf(stderr, " %g", bt->param[i].c[j]);
618 fprintf(stderr, " \n new: %s\n\n", line);
624 if (!identicalParameters && !bAllowRepeat)
626 /* Overwrite the parameters with the latest ones */
627 // TODO considering improving the following code by replacing with:
628 // std::copy(b->c, b->c + nrfp, bt->param[i].c);
629 for (int j = 0; (j < nrfp); j++)
631 bt->param[i].c[j] = b->c[j];
642 /* fill the arrays up and down */
643 memcpy(bt->param[bt->nr].c, b->c, sizeof(b->c));
644 memcpy(bt->param[bt->nr].a, b->a, sizeof(b->a));
645 memcpy(bt->param[bt->nr+1].c, b->c, sizeof(b->c));
647 /* The definitions of linear angles depend on the order of atoms,
648 * that means that for atoms i-j-k, with certain parameter a, the
649 * corresponding k-j-i angle will have parameter 1-a.
651 if (ftype == F_LINEAR_ANGLES)
653 bt->param[bt->nr+1].c[0] = 1-bt->param[bt->nr+1].c[0];
654 bt->param[bt->nr+1].c[2] = 1-bt->param[bt->nr+1].c[2];
657 for (int j = 0; (j < nral); j++)
659 bt->param[bt->nr+1].a[j] = b->a[nral-1-j];
666 void push_bt(directive d, t_params bt[], int nral,
668 t_bond_atomtype bat, char *line,
671 const char *formal[MAXATOMLIST+1] = {
680 const char *formnl[MAXATOMLIST+1] = {
686 "%*s%*s%*s%*s%*s%*s",
687 "%*s%*s%*s%*s%*s%*s%*s"
689 const char *formlf = "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf";
690 int i, ft, ftype, nn, nrfp, nrfpA;
692 char alc[MAXATOMLIST+1][20];
693 /* One force parameter more, so we can check if we read too many */
694 double c[MAXFORCEPARAM+1];
698 if ((bat && at) || (!bat && !at))
700 gmx_incons("You should pass either bat or at to push_bt");
703 /* Make format string (nral ints+functype) */
704 if ((nn = sscanf(line, formal[nral],
705 alc[0], alc[1], alc[2], alc[3], alc[4], alc[5])) != nral+1)
707 sprintf(errbuf, "Not enough atomtypes (%d instead of %d)", nn-1, nral);
708 warning_error(wi, errbuf);
712 ft = strtol(alc[nral], nullptr, 10);
713 ftype = ifunc_index(d, ft);
715 nrfpA = interaction_function[ftype].nrfpA;
716 strcpy(f1, formnl[nral]);
718 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]))
723 /* Copy the B-state from the A-state */
724 copy_B_from_A(ftype, c);
730 warning_error(wi, "Not enough parameters");
732 else if (nn > nrfpA && nn < nrfp)
734 warning_error(wi, "Too many parameters or not enough parameters for topology B");
738 warning_error(wi, "Too many parameters");
740 for (i = nn; (i < nrfp); i++)
746 for (i = 0; (i < nral); i++)
748 if (at && ((p.a[i] = get_atomtype_type(alc[i], at)) == NOTSET))
750 sprintf(errbuf, "Unknown atomtype %s\n", alc[i]);
751 warning_error_and_exit(wi, errbuf, FARGS);
753 else if (bat && ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET))
755 sprintf(errbuf, "Unknown bond_atomtype %s\n", alc[i]);
756 warning_error_and_exit(wi, errbuf, FARGS);
759 for (i = 0; (i < nrfp); i++)
763 push_bondtype (&(bt[ftype]), &p, nral, ftype, FALSE, line, wi);
767 void push_dihedraltype(directive d, t_params bt[],
768 t_bond_atomtype bat, char *line,
771 const char *formal[MAXATOMLIST+1] = {
780 const char *formnl[MAXATOMLIST+1] = {
786 "%*s%*s%*s%*s%*s%*s",
787 "%*s%*s%*s%*s%*s%*s%*s"
789 const char *formlf[MAXFORCEPARAM] = {
795 "%lf%lf%lf%lf%lf%lf",
796 "%lf%lf%lf%lf%lf%lf%lf",
797 "%lf%lf%lf%lf%lf%lf%lf%lf",
798 "%lf%lf%lf%lf%lf%lf%lf%lf%lf",
799 "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
800 "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
801 "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
803 int i, ft, ftype, nn, nrfp, nrfpA, nral;
805 char alc[MAXATOMLIST+1][20];
806 double c[MAXFORCEPARAM];
808 gmx_bool bAllowRepeat;
811 /* This routine accepts dihedraltypes defined from either 2 or 4 atoms.
813 * We first check for 2 atoms with the 3th column being an integer
814 * defining the type. If this isn't the case, we try it with 4 atoms
815 * and the 5th column defining the dihedral type.
817 nn = sscanf(line, formal[4], alc[0], alc[1], alc[2], alc[3], alc[4]);
818 if (nn >= 3 && strlen(alc[2]) == 1 && isdigit(alc[2][0]))
821 ft = strtol(alc[nral], nullptr, 10);
822 /* Move atom types around a bit and use 'X' for wildcard atoms
823 * to create a 4-atom dihedral definition with arbitrary atoms in
826 if (alc[2][0] == '2')
828 /* improper - the two atomtypes are 1,4. Use wildcards for 2,3 */
829 strcpy(alc[3], alc[1]);
830 sprintf(alc[2], "X");
831 sprintf(alc[1], "X");
832 /* alc[0] stays put */
836 /* proper - the two atomtypes are 2,3. Use wildcards for 1,4 */
837 sprintf(alc[3], "X");
838 strcpy(alc[2], alc[1]);
839 strcpy(alc[1], alc[0]);
840 sprintf(alc[0], "X");
843 else if (nn == 5 && strlen(alc[4]) == 1 && isdigit(alc[4][0]))
846 ft = strtol(alc[nral], nullptr, 10);
850 sprintf(errbuf, "Incorrect number of atomtypes for dihedral (%d instead of 2 or 4)", nn-1);
851 warning_error(wi, errbuf);
857 /* Previously, we have always overwritten parameters if e.g. a torsion
858 with the same atomtypes occurs on multiple lines. However, CHARMM and
859 some other force fields specify multiple dihedrals over some bonds,
860 including cosines with multiplicity 6 and somethimes even higher.
861 Thus, they cannot be represented with Ryckaert-Bellemans terms.
862 To add support for these force fields, Dihedral type 9 is identical to
863 normal proper dihedrals, but repeated entries are allowed.
870 bAllowRepeat = FALSE;
874 ftype = ifunc_index(d, ft);
876 nrfpA = interaction_function[ftype].nrfpA;
878 strcpy(f1, formnl[nral]);
879 strcat(f1, formlf[nrfp-1]);
881 /* Check number of parameters given */
882 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]))
887 /* Copy the B-state from the A-state */
888 copy_B_from_A(ftype, c);
894 warning_error(wi, "Not enough parameters");
896 else if (nn > nrfpA && nn < nrfp)
898 warning_error(wi, "Too many parameters or not enough parameters for topology B");
902 warning_error(wi, "Too many parameters");
904 for (i = nn; (i < nrfp); i++)
911 for (i = 0; (i < 4); i++)
913 if (!strcmp(alc[i], "X"))
919 if ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET)
921 sprintf(errbuf, "Unknown bond_atomtype %s", alc[i]);
922 warning_error_and_exit(wi, errbuf, FARGS);
926 for (i = 0; (i < nrfp); i++)
930 /* Always use 4 atoms here, since we created two wildcard atoms
931 * if there wasn't of them 4 already.
933 push_bondtype (&(bt[ftype]), &p, 4, ftype, bAllowRepeat, line, wi);
937 void push_nbt(directive d, t_nbparam **nbt, gpp_atomtype_t atype,
938 char *pline, int nb_funct,
942 const char *form3 = "%*s%*s%*s%lf%lf%lf";
943 const char *form4 = "%*s%*s%*s%lf%lf%lf%lf";
944 const char *form5 = "%*s%*s%*s%lf%lf%lf%lf%lf";
946 int i, f, n, ftype, nrfp;
954 if (sscanf (pline, "%s%s%d", a0, a1, &f) != 3)
960 ftype = ifunc_index(d, f);
962 if (ftype != nb_funct)
964 sprintf(errbuf, "Trying to add %s while the default nonbond type is %s",
965 interaction_function[ftype].longname,
966 interaction_function[nb_funct].longname);
967 warning_error(wi, errbuf);
971 /* Get the force parameters */
975 n = sscanf(pline, form4, &c[0], &c[1], &c[2], &c[3]);
981 /* When the B topology parameters are not set,
982 * copy them from topology A
984 GMX_ASSERT(nrfp <= 4, "LJ-14 cannot have more than 4 parameters");
985 for (i = n; i < nrfp; i++)
990 else if (ftype == F_LJC14_Q)
992 n = sscanf(pline, form5, &c[0], &c[1], &c[2], &c[3], &dum);
995 incorrect_n_param(wi);
1001 if (sscanf(pline, form3, &c[0], &c[1], &dum) != 2)
1003 incorrect_n_param(wi);
1009 if (sscanf(pline, form4, &c[0], &c[1], &c[2], &dum) != 3)
1011 incorrect_n_param(wi);
1017 sprintf(errbuf, "Number of force parameters for nonbonded interactions is %d", nrfp);
1018 warning_error_and_exit(wi, errbuf, FARGS);
1020 for (i = 0; (i < nrfp); i++)
1025 /* Put the parameters in the matrix */
1026 if ((ai = get_atomtype_type (a0, atype)) == NOTSET)
1028 sprintf(errbuf, "Atomtype %s not found", a0);
1029 warning_error_and_exit(wi, errbuf, FARGS);
1031 if ((aj = get_atomtype_type (a1, atype)) == NOTSET)
1033 sprintf(errbuf, "Atomtype %s not found", a1);
1034 warning_error_and_exit(wi, errbuf, FARGS);
1036 nbp = &(nbt[std::max(ai, aj)][std::min(ai, aj)]);
1041 for (i = 0; i < nrfp; i++)
1043 bId = bId && (nbp->c[i] == cr[i]);
1047 sprintf(errbuf, "Overriding non-bonded parameters,");
1048 warning(wi, errbuf);
1049 fprintf(stderr, " old:");
1050 for (i = 0; i < nrfp; i++)
1052 fprintf(stderr, " %g", nbp->c[i]);
1054 fprintf(stderr, " new\n%s\n", pline);
1058 for (i = 0; i < nrfp; i++)
1065 push_cmaptype(directive d, t_params bt[], int nral, gpp_atomtype_t at,
1066 t_bond_atomtype bat, char *line,
1069 const char *formal = "%s%s%s%s%s%s%s%s%n";
1071 int i, ft, ftype, nn, nrfp, nrfpA, nrfpB;
1072 int start, nchar_consumed;
1073 int nxcmap, nycmap, ncmap, read_cmap, sl, nct;
1074 char s[20], alc[MAXATOMLIST+2][20];
1076 char errbuf[STRLEN];
1078 /* Keep the compiler happy */
1082 GMX_ASSERT(nral == 5, "CMAP requires 5 atoms per interaction");
1084 /* Here we can only check for < 8 */
1085 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)
1087 sprintf(errbuf, "Incorrect number of atomtypes for cmap (%d instead of 5)", nn-1);
1088 warning_error(wi, errbuf);
1091 start += nchar_consumed;
1093 ft = strtol(alc[nral], nullptr, 10);
1094 nxcmap = strtol(alc[nral+1], nullptr, 10);
1095 nycmap = strtol(alc[nral+2], nullptr, 10);
1097 /* Check for equal grid spacing in x and y dims */
1098 if (nxcmap != nycmap)
1100 sprintf(errbuf, "Not the same grid spacing in x and y for cmap grid: x=%d, y=%d", nxcmap, nycmap);
1101 warning_error(wi, errbuf);
1104 ncmap = nxcmap*nycmap;
1105 ftype = ifunc_index(d, ft);
1106 nrfpA = strtol(alc[6], nullptr, 10)*strtol(alc[6], nullptr, 10);
1107 nrfpB = strtol(alc[7], nullptr, 10)*strtol(alc[7], nullptr, 10);
1110 /* Allocate memory for the CMAP grid */
1111 bt[F_CMAP].ncmap += nrfp;
1112 srenew(bt[F_CMAP].cmap, bt[F_CMAP].ncmap);
1114 /* Read in CMAP parameters */
1116 for (i = 0; i < ncmap; i++)
1118 while (isspace(*(line+start+sl)))
1122 nn = sscanf(line+start+sl, " %s ", s);
1124 bt[F_CMAP].cmap[i+(bt[F_CMAP].ncmap)-nrfp] = strtod(s, nullptr);
1132 sprintf(errbuf, "Error in reading cmap parameter for angle %s %s %s %s %s", alc[0], alc[1], alc[2], alc[3], alc[4]);
1133 warning_error(wi, errbuf);
1138 /* Check do that we got the number of parameters we expected */
1139 if (read_cmap == nrfpA)
1141 for (i = 0; i < ncmap; i++)
1143 bt[F_CMAP].cmap[i+ncmap] = bt[F_CMAP].cmap[i];
1148 if (read_cmap < nrfpA)
1150 warning_error(wi, "Not enough cmap parameters");
1152 else if (read_cmap > nrfpA && read_cmap < nrfp)
1154 warning_error(wi, "Too many cmap parameters or not enough parameters for topology B");
1156 else if (read_cmap > nrfp)
1158 warning_error(wi, "Too many cmap parameters");
1163 /* Set grid spacing and the number of grids (we assume these numbers to be the same for all grids
1164 * so we can safely assign them each time
1166 bt[F_CMAP].grid_spacing = nxcmap; /* Or nycmap, they need to be equal */
1167 bt[F_CMAP].nc = bt[F_CMAP].nc + 1; /* Since we are incrementing here, we need to subtract later, see (*****) */
1168 nct = (nral+1) * bt[F_CMAP].nc;
1170 /* Allocate memory for the cmap_types information */
1171 srenew(bt[F_CMAP].cmap_types, nct);
1173 for (i = 0; (i < nral); i++)
1175 if (at && ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET))
1177 sprintf(errbuf, "Unknown atomtype %s\n", alc[i]);
1178 warning_error(wi, errbuf);
1180 else if (bat && ((p.a[i] = get_bond_atomtype_type(alc[i], bat)) == NOTSET))
1182 sprintf(errbuf, "Unknown bond_atomtype %s\n", alc[i]);
1183 warning_error(wi, errbuf);
1186 /* Assign a grid number to each cmap_type */
1187 bt[F_CMAP].cmap_types[bt[F_CMAP].nct++] = get_bond_atomtype_type(alc[i], bat);
1190 /* Assign a type number to this cmap */
1191 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 (****) */
1193 /* Check for the correct number of atoms (again) */
1194 if (bt[F_CMAP].nct != nct)
1196 sprintf(errbuf, "Incorrect number of atom types (%d) in cmap type %d\n", nct, bt[F_CMAP].nc);
1197 warning_error(wi, errbuf);
1200 /* Is this correct?? */
1201 for (i = 0; i < MAXFORCEPARAM; i++)
1206 /* Push the bond to the bondlist */
1207 push_bondtype (&(bt[ftype]), &p, nral, ftype, FALSE, line, wi);
1211 static void push_atom_now(t_symtab *symtab, t_atoms *at, int atomnr,
1213 int type, char *ctype, int ptype,
1215 char *resname, char *name, real m0, real q0,
1216 int typeB, char *ctypeB, real mB, real qB,
1219 int j, resind = 0, resnr;
1222 char errbuf[STRLEN];
1224 if (((nr == 0) && (atomnr != 1)) || (nr && (atomnr != at->nr+1)))
1226 sprintf(errbuf, "Atoms in the .top are not numbered consecutively from 1 (rather, atomnr = %d, while at->nr = %d)", atomnr, at->nr);
1227 warning_error_and_exit(wi, errbuf, FARGS);
1230 j = strlen(resnumberic) - 1;
1231 if (isdigit(resnumberic[j]))
1237 ric = resnumberic[j];
1238 if (j == 0 || !isdigit(resnumberic[j-1]))
1240 sprintf(errbuf, "Invalid residue number '%s' for atom %d",
1241 resnumberic, atomnr);
1242 warning_error_and_exit(wi, errbuf, FARGS);
1245 resnr = strtol(resnumberic, nullptr, 10);
1249 resind = at->atom[nr-1].resind;
1251 if (nr == 0 || strcmp(resname, *at->resinfo[resind].name) != 0 ||
1252 resnr != at->resinfo[resind].nr ||
1253 ric != at->resinfo[resind].ic)
1263 at->nres = resind + 1;
1264 srenew(at->resinfo, at->nres);
1265 at->resinfo[resind].name = put_symtab(symtab, resname);
1266 at->resinfo[resind].nr = resnr;
1267 at->resinfo[resind].ic = ric;
1271 resind = at->atom[at->nr-1].resind;
1274 /* New atom instance
1275 * get new space for arrays
1277 srenew(at->atom, nr+1);
1278 srenew(at->atomname, nr+1);
1279 srenew(at->atomtype, nr+1);
1280 srenew(at->atomtypeB, nr+1);
1283 at->atom[nr].type = type;
1284 at->atom[nr].ptype = ptype;
1285 at->atom[nr].q = q0;
1286 at->atom[nr].m = m0;
1287 at->atom[nr].typeB = typeB;
1288 at->atom[nr].qB = qB;
1289 at->atom[nr].mB = mB;
1291 at->atom[nr].resind = resind;
1292 at->atom[nr].atomnumber = atomicnumber;
1293 at->atomname[nr] = put_symtab(symtab, name);
1294 at->atomtype[nr] = put_symtab(symtab, ctype);
1295 at->atomtypeB[nr] = put_symtab(symtab, ctypeB);
1299 static void push_cg(t_block *block, int *lastindex, int index, int a)
1303 fprintf (debug, "Index %d, Atom %d\n", index, a);
1306 if (((block->nr) && (*lastindex != index)) || (!block->nr))
1308 /* add a new block */
1310 srenew(block->index, block->nr+1);
1312 block->index[block->nr] = a + 1;
1316 void push_atom(t_symtab *symtab, t_block *cgs,
1317 t_atoms *at, gpp_atomtype_t atype, char *line, int *lastcg,
1321 int cgnumber, atomnr, type, typeB, nscan;
1322 char id[STRLEN], ctype[STRLEN], ctypeB[STRLEN],
1323 resnumberic[STRLEN], resname[STRLEN], name[STRLEN], check[STRLEN];
1324 double m, q, mb, qb;
1325 real m0, q0, mB, qB;
1326 char errbuf[STRLEN];
1328 /* Make a shortcut for writing in this molecule */
1331 /* Fixed parameters */
1332 if (sscanf(line, "%s%s%s%s%s%d",
1333 id, ctype, resnumberic, resname, name, &cgnumber) != 6)
1338 sscanf(id, "%d", &atomnr);
1339 if ((type = get_atomtype_type(ctype, atype)) == NOTSET)
1341 sprintf(errbuf, "Atomtype %s not found", ctype);
1342 warning_error_and_exit(wi, errbuf, FARGS);
1344 ptype = get_atomtype_ptype(type, atype);
1346 /* Set default from type */
1347 q0 = get_atomtype_qA(type, atype);
1348 m0 = get_atomtype_massA(type, atype);
1353 /* Optional parameters */
1354 nscan = sscanf(line, "%*s%*s%*s%*s%*s%*s%lf%lf%s%lf%lf%s",
1355 &q, &m, ctypeB, &qb, &mb, check);
1357 /* Nasty switch that falls thru all the way down! */
1366 if ((typeB = get_atomtype_type(ctypeB, atype)) == NOTSET)
1368 sprintf(errbuf, "Atomtype %s not found", ctypeB);
1369 warning_error_and_exit(wi, errbuf, FARGS);
1371 qB = get_atomtype_qA(typeB, atype);
1372 mB = get_atomtype_massA(typeB, atype);
1381 warning_error(wi, "Too many parameters");
1390 fprintf(debug, "mB=%g, qB=%g, typeB=%d\n", mB, qB, typeB);
1393 push_cg(cgs, lastcg, cgnumber, nr);
1395 push_atom_now(symtab, at, atomnr, get_atomtype_atomnumber(type, atype),
1396 type, ctype, ptype, resnumberic,
1397 resname, name, m0, q0, typeB,
1398 typeB == type ? ctype : ctypeB, mB, qB, wi);
1401 void push_molt(t_symtab *symtab, int *nmol, t_molinfo **mol, char *line,
1407 char errbuf[STRLEN];
1409 if ((sscanf(line, "%s%d", type, &nrexcl)) != 2)
1411 warning_error(wi, "Expected a molecule type name and nrexcl");
1414 /* Test if this moleculetype overwrites another */
1418 if (strcmp(*((*mol)[i].name), type) == 0)
1420 sprintf(errbuf, "moleculetype %s is redefined", type);
1421 warning_error_and_exit(wi, errbuf, FARGS);
1427 srenew(*mol, *nmol);
1428 newmol = &((*mol)[*nmol-1]);
1429 init_molinfo(newmol);
1431 /* Fill in the values */
1432 newmol->name = put_symtab(symtab, type);
1433 newmol->nrexcl = nrexcl;
1434 newmol->excl_set = FALSE;
1437 static gmx_bool default_nb_params(int ftype, t_params bt[], t_atoms *at,
1438 t_param *p, int c_start, gmx_bool bB, gmx_bool bGenPairs)
1440 int i, j, ti, tj, ntype;
1442 t_param *pi = nullptr;
1443 int nr = bt[ftype].nr;
1444 int nral = NRAL(ftype);
1445 int nrfp = interaction_function[ftype].nrfpA;
1446 int nrfpB = interaction_function[ftype].nrfpB;
1448 if ((!bB && nrfp == 0) || (bB && nrfpB == 0))
1456 /* First test the generated-pair position to save
1457 * time when we have 1000*1000 entries for e.g. OPLS...
1459 ntype = static_cast<int>(std::sqrt(static_cast<double>(nr)));
1460 GMX_ASSERT(ntype * ntype == nr, "Number of pairs of generated non-bonded parameters should be a perfect square");
1463 ti = at->atom[p->a[0]].typeB;
1464 tj = at->atom[p->a[1]].typeB;
1468 ti = at->atom[p->a[0]].type;
1469 tj = at->atom[p->a[1]].type;
1471 pi = &(bt[ftype].param[ntype*ti+tj]);
1472 bFound = ((ti == pi->a[0]) && (tj == pi->a[1]));
1475 /* Search explicitly if we didnt find it */
1478 for (i = 0; ((i < nr) && !bFound); i++)
1480 pi = &(bt[ftype].param[i]);
1483 for (j = 0; ((j < nral) &&
1484 (at->atom[p->a[j]].typeB == pi->a[j])); j++)
1491 for (j = 0; ((j < nral) &&
1492 (at->atom[p->a[j]].type == pi->a[j])); j++)
1497 bFound = (j == nral);
1505 if (nrfp+nrfpB > MAXFORCEPARAM)
1507 gmx_incons("Too many force parameters");
1509 for (j = c_start; (j < nrfpB); j++)
1511 p->c[nrfp+j] = pi->c[j];
1516 for (j = c_start; (j < nrfp); j++)
1524 for (j = c_start; (j < nrfp); j++)
1532 static gmx_bool default_cmap_params(t_params bondtype[],
1533 t_atoms *at, gpp_atomtype_t atype,
1534 t_param *p, gmx_bool bB,
1535 int *cmap_type, int *nparam_def,
1538 int i, nparam_found;
1540 gmx_bool bFound = FALSE;
1541 char errbuf[STRLEN];
1546 /* Match the current cmap angle against the list of cmap_types */
1547 for (i = 0; i < bondtype[F_CMAP].nct && !bFound; i += 6)
1556 (get_atomtype_batype(at->atom[p->a[0]].type, atype) == bondtype[F_CMAP].cmap_types[i]) &&
1557 (get_atomtype_batype(at->atom[p->a[1]].type, atype) == bondtype[F_CMAP].cmap_types[i+1]) &&
1558 (get_atomtype_batype(at->atom[p->a[2]].type, atype) == bondtype[F_CMAP].cmap_types[i+2]) &&
1559 (get_atomtype_batype(at->atom[p->a[3]].type, atype) == bondtype[F_CMAP].cmap_types[i+3]) &&
1560 (get_atomtype_batype(at->atom[p->a[4]].type, atype) == bondtype[F_CMAP].cmap_types[i+4]))
1562 /* Found cmap torsion */
1564 ct = bondtype[F_CMAP].cmap_types[i+5];
1570 /* If we did not find a matching type for this cmap torsion */
1573 sprintf(errbuf, "Unknown cmap torsion between atoms %d %d %d %d %d",
1574 p->a[0]+1, p->a[1]+1, p->a[2]+1, p->a[3]+1, p->a[4]+1);
1575 warning_error_and_exit(wi, errbuf, FARGS);
1578 *nparam_def = nparam_found;
1584 /* Returns the number of exact atom type matches, i.e. non wild-card matches,
1585 * returns -1 when there are no matches at all.
1587 static int natom_match(t_param *pi,
1588 int type_i, int type_j, int type_k, int type_l,
1589 const gpp_atomtype* atype)
1591 if ((pi->ai() == -1 || get_atomtype_batype(type_i, atype) == pi->ai()) &&
1592 (pi->aj() == -1 || get_atomtype_batype(type_j, atype) == pi->aj()) &&
1593 (pi->ak() == -1 || get_atomtype_batype(type_k, atype) == pi->ak()) &&
1594 (pi->al() == -1 || get_atomtype_batype(type_l, atype) == pi->al()))
1597 (pi->ai() == -1 ? 0 : 1) +
1598 (pi->aj() == -1 ? 0 : 1) +
1599 (pi->ak() == -1 ? 0 : 1) +
1600 (pi->al() == -1 ? 0 : 1);
1608 static gmx_bool default_params(int ftype, t_params bt[],
1609 t_atoms *at, gpp_atomtype_t atype,
1610 t_param *p, gmx_bool bB,
1611 t_param **param_def,
1615 gmx_bool bFound, bSame;
1616 t_param *pi = nullptr;
1617 t_param *pj = nullptr;
1618 int nr = bt[ftype].nr;
1619 int nral = NRAL(ftype);
1620 int nrfpA = interaction_function[ftype].nrfpA;
1621 int nrfpB = interaction_function[ftype].nrfpB;
1623 if ((!bB && nrfpA == 0) || (bB && nrfpB == 0))
1631 if (ftype == F_PDIHS || ftype == F_RBDIHS || ftype == F_IDIHS || ftype == F_PIDIHS)
1633 int nmatch_max = -1;
1637 /* For dihedrals we allow wildcards. We choose the first type
1638 * that has the most real matches, i.e. non-wildcard matches.
1640 for (t = 0; ((t < nr) && nmatch_max < 4); t++)
1645 pt = &(bt[ftype].param[t]);
1648 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);
1652 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);
1654 if (nmatch > nmatch_max)
1656 nmatch_max = nmatch;
1666 pi = &(bt[ftype].param[i]);
1669 /* Find additional matches for this dihedral - necessary
1671 * The rule in that case is that additional matches
1672 * HAVE to be on adjacent lines!
1675 /* Continue from current i value */
1676 for (j = i + 2; j < nr && bSame; j += 2)
1678 pj = &(bt[ftype].param[j]);
1679 bSame = (pi->ai() == pj->ai() && pi->aj() == pj->aj() && pi->ak() == pj->ak() && pi->al() == pj->al());
1684 /* nparam_found will be increased as long as the numbers match */
1688 else /* Not a dihedral */
1692 for (i = 0; ((i < nr) && !bFound); i++)
1694 pi = &(bt[ftype].param[i]);
1697 for (j = 0; ((j < nral) &&
1698 (get_atomtype_batype(at->atom[p->a[j]].typeB, atype) == pi->a[j])); j++)
1705 for (j = 0; ((j < nral) &&
1706 (get_atomtype_batype(at->atom[p->a[j]].type, atype) == pi->a[j])); j++)
1711 bFound = (j == nral);
1720 *nparam_def = nparam_found;
1727 void push_bond(directive d, t_params bondtype[], t_params bond[],
1728 t_atoms *at, gpp_atomtype_t atype, char *line,
1729 gmx_bool bBonded, gmx_bool bGenPairs, real fudgeQQ,
1730 gmx_bool bZero, gmx_bool *bWarn_copy_A_B,
1733 const char *aaformat[MAXATOMLIST] = {
1741 const char *asformat[MAXATOMLIST] = {
1746 "%*s%*s%*s%*s%*s%*s",
1747 "%*s%*s%*s%*s%*s%*s%*s"
1749 const char *ccformat = "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf";
1750 int nr, i, j, nral, nral_fmt, nread, ftype;
1751 char format[STRLEN];
1752 /* One force parameter more, so we can check if we read too many */
1753 double cc[MAXFORCEPARAM+1];
1754 int aa[MAXATOMLIST+1];
1755 t_param param, *param_defA, *param_defB;
1756 gmx_bool bFoundA = FALSE, bFoundB = FALSE, bDef, bPert, bSwapParity = FALSE;
1757 int nparam_defA, nparam_defB;
1758 char errbuf[STRLEN];
1760 nparam_defA = nparam_defB = 0;
1762 ftype = ifunc_index(d, 1);
1764 for (j = 0; j < MAXATOMLIST; j++)
1768 bDef = (NRFP(ftype) > 0);
1770 if (ftype == F_SETTLE)
1772 /* SETTLE acts on 3 atoms, but the topology format only specifies
1773 * the first atom (for historical reasons).
1782 nread = sscanf(line, aaformat[nral_fmt-1],
1783 &aa[0], &aa[1], &aa[2], &aa[3], &aa[4], &aa[5]);
1785 if (ftype == F_SETTLE)
1792 if (nread < nral_fmt)
1797 else if (nread > nral_fmt)
1799 /* this is a hack to allow for virtual sites with swapped parity */
1800 bSwapParity = (aa[nral] < 0);
1803 aa[nral] = -aa[nral];
1805 ftype = ifunc_index(d, aa[nral]);
1814 sprintf(errbuf, "Negative function types only allowed for %s and %s",
1815 interaction_function[F_VSITE3FAD].longname,
1816 interaction_function[F_VSITE3OUT].longname);
1817 warning_error_and_exit(wi, errbuf, FARGS);
1823 /* Check for double atoms and atoms out of bounds */
1824 for (i = 0; (i < nral); i++)
1826 if (aa[i] < 1 || aa[i] > at->nr)
1828 sprintf(errbuf, "Atom index (%d) in %s out of bounds (1-%d).\n"
1829 "This probably means that you have inserted topology section \"%s\"\n"
1830 "in a part belonging to a different molecule than you intended to.\n"
1831 "In that case move the \"%s\" section to the right molecule.",
1832 aa[i], dir2str(d), at->nr, dir2str(d), dir2str(d));
1833 warning_error_and_exit(wi, errbuf, FARGS);
1835 for (j = i+1; (j < nral); j++)
1837 GMX_ASSERT(j < MAXATOMLIST + 1, "Values from nral=NRAL() will satisfy this, we assert to keep gcc 4 happy");
1840 sprintf(errbuf, "Duplicate atom index (%d) in %s", aa[i], dir2str(d));
1841 if (ftype == F_ANGRES)
1843 /* Since the angle restraints uses 2 pairs of atoms to
1844 * defines an angle between vectors, it can be useful
1845 * to use one atom twice, so we only issue a note here.
1847 warning_note(wi, errbuf);
1851 warning_error(wi, errbuf);
1857 /* default force parameters */
1858 for (j = 0; (j < MAXATOMLIST); j++)
1860 param.a[j] = aa[j]-1;
1862 for (j = 0; (j < MAXFORCEPARAM); j++)
1867 /* Get force params for normal and free energy perturbation
1868 * studies, as determined by types!
1873 bFoundA = default_params(ftype, bondtype, at, atype, ¶m, FALSE, ¶m_defA, &nparam_defA);
1876 /* Copy the A-state and B-state default parameters. */
1877 GMX_ASSERT(NRFPA(ftype)+NRFPB(ftype) <= MAXFORCEPARAM, "Bonded interactions may have at most 12 parameters");
1878 for (j = 0; (j < NRFPA(ftype)+NRFPB(ftype)); j++)
1880 param.c[j] = param_defA->c[j];
1883 bFoundB = default_params(ftype, bondtype, at, atype, ¶m, TRUE, ¶m_defB, &nparam_defB);
1886 /* Copy only the B-state default parameters */
1887 for (j = NRFPA(ftype); (j < NRFP(ftype)); j++)
1889 param.c[j] = param_defB->c[j];
1893 else if (ftype == F_LJ14)
1895 bFoundA = default_nb_params(ftype, bondtype, at, ¶m, 0, FALSE, bGenPairs);
1896 bFoundB = default_nb_params(ftype, bondtype, at, ¶m, 0, TRUE, bGenPairs);
1898 else if (ftype == F_LJC14_Q)
1900 param.c[0] = fudgeQQ;
1901 /* Fill in the A-state charges as default parameters */
1902 param.c[1] = at->atom[param.a[0]].q;
1903 param.c[2] = at->atom[param.a[1]].q;
1904 /* The default LJ parameters are the standard 1-4 parameters */
1905 bFoundA = default_nb_params(F_LJ14, bondtype, at, ¶m, 3, FALSE, bGenPairs);
1908 else if (ftype == F_LJC_PAIRS_NB)
1910 /* Defaults are not supported here */
1916 gmx_incons("Unknown function type in push_bond");
1919 if (nread > nral_fmt)
1921 /* Manually specified parameters - in this case we discard multiple torsion info! */
1923 strcpy(format, asformat[nral_fmt-1]);
1924 strcat(format, ccformat);
1926 nread = sscanf(line, format, &cc[0], &cc[1], &cc[2], &cc[3], &cc[4], &cc[5],
1927 &cc[6], &cc[7], &cc[8], &cc[9], &cc[10], &cc[11], &cc[12]);
1929 if ((nread == NRFPA(ftype)) && (NRFPB(ftype) != 0))
1931 /* We only have to issue a warning if these atoms are perturbed! */
1933 for (j = 0; (j < nral); j++)
1935 bPert = bPert || PERTURBED(at->atom[param.a[j]]);
1938 if (bPert && *bWarn_copy_A_B)
1941 "Some parameters for bonded interaction involving perturbed atoms are specified explicitly in state A, but not B - copying A to B");
1942 warning(wi, errbuf);
1943 *bWarn_copy_A_B = FALSE;
1946 /* If only the A parameters were specified, copy them to the B state */
1947 /* The B-state parameters correspond to the first nrfpB
1948 * A-state parameters.
1950 for (j = 0; (j < NRFPB(ftype)); j++)
1952 cc[nread++] = cc[j];
1956 /* If nread was 0 or EOF, no parameters were read => use defaults.
1957 * If nread was nrfpA we copied above so nread=nrfp.
1958 * If nread was nrfp we are cool.
1959 * For F_LJC14_Q we allow supplying fudgeQQ only.
1960 * Anything else is an error!
1962 if ((nread != 0) && (nread != EOF) && (nread != NRFP(ftype)) &&
1963 !(ftype == F_LJC14_Q && nread == 1))
1965 sprintf(errbuf, "Incorrect number of parameters - found %d, expected %d or %d for %s (after the function type).",
1966 nread, NRFPA(ftype), NRFP(ftype), interaction_function[ftype].longname);
1967 warning_error_and_exit(wi, errbuf, FARGS);
1970 for (j = 0; (j < nread); j++)
1975 /* Check whether we have to use the defaults */
1976 if (nread == NRFP(ftype))
1985 /* nread now holds the number of force parameters read! */
1990 /* When we have multiple terms it would be very dangerous to allow perturbations to a different atom type! */
1991 if (ftype == F_PDIHS)
1993 if ((nparam_defA != nparam_defB) || ((nparam_defA > 1 || nparam_defB > 1) && (param_defA != param_defB)))
1996 "Cannot automatically perturb a torsion with multiple terms to different form.\n"
1997 "Please specify perturbed parameters manually for this torsion in your topology!");
1998 warning_error(wi, errbuf);
2002 if (nread > 0 && nread < NRFPA(ftype))
2004 /* Issue an error, do not use defaults */
2005 sprintf(errbuf, "Not enough parameters, there should be at least %d (or 0 for defaults)", NRFPA(ftype));
2006 warning_error(wi, errbuf);
2009 if (nread == 0 || nread == EOF)
2013 if (interaction_function[ftype].flags & IF_VSITE)
2015 /* set them to NOTSET, will be calculated later */
2016 for (j = 0; (j < MAXFORCEPARAM); j++)
2018 param.c[j] = NOTSET;
2023 param.c1() = -1; /* flag to swap parity of vsite construction */
2030 fprintf(stderr, "NOTE: No default %s types, using zeroes\n",
2031 interaction_function[ftype].longname);
2035 sprintf(errbuf, "No default %s types", interaction_function[ftype].longname);
2036 warning_error(wi, errbuf);
2047 param.c0() = 360 - param.c0();
2050 param.c2() = -param.c2();
2057 /* We only have to issue a warning if these atoms are perturbed! */
2059 for (j = 0; (j < nral); j++)
2061 bPert = bPert || PERTURBED(at->atom[param.a[j]]);
2066 sprintf(errbuf, "No default %s types for perturbed atoms, "
2067 "using normal values", interaction_function[ftype].longname);
2068 warning(wi, errbuf);
2074 if ((ftype == F_PDIHS || ftype == F_ANGRES || ftype == F_ANGRESZ)
2075 && param.c[5] != param.c[2])
2077 sprintf(errbuf, "%s multiplicity can not be perturbed %f!=%f",
2078 interaction_function[ftype].longname,
2079 param.c[2], param.c[5]);
2080 warning_error_and_exit(wi, errbuf, FARGS);
2083 if (IS_TABULATED(ftype) && param.c[2] != param.c[0])
2085 sprintf(errbuf, "%s table number can not be perturbed %d!=%d",
2086 interaction_function[ftype].longname,
2087 (int)(param.c[0]+0.5), (int)(param.c[2]+0.5));
2088 warning_error_and_exit(wi, errbuf, FARGS);
2091 /* Dont add R-B dihedrals where all parameters are zero (no interaction) */
2092 if (ftype == F_RBDIHS)
2095 for (i = 0; i < NRFP(ftype); i++)
2097 if (param.c[i] != 0)
2108 /* Put the values in the appropriate arrays */
2109 add_param_to_list (&bond[ftype], ¶m);
2111 /* Push additional torsions from FF for ftype==9 if we have them.
2112 * We have already checked that the A/B states do not differ in this case,
2113 * so we do not have to double-check that again, or the vsite stuff.
2114 * In addition, those torsions cannot be automatically perturbed.
2116 if (bDef && ftype == F_PDIHS)
2118 for (i = 1; i < nparam_defA; i++)
2120 /* Advance pointer! */
2122 for (j = 0; (j < NRFPA(ftype)+NRFPB(ftype)); j++)
2124 param.c[j] = param_defA->c[j];
2126 /* And push the next term for this torsion */
2127 add_param_to_list (&bond[ftype], ¶m);
2132 void push_cmap(directive d, t_params bondtype[], t_params bond[],
2133 t_atoms *at, gpp_atomtype_t atype, char *line,
2136 const char *aaformat[MAXATOMLIST+1] =
2147 int i, j, ftype, nral, nread, ncmap_params;
2149 int aa[MAXATOMLIST+1];
2150 char errbuf[STRLEN];
2154 ftype = ifunc_index(d, 1);
2158 nread = sscanf(line, aaformat[nral-1],
2159 &aa[0], &aa[1], &aa[2], &aa[3], &aa[4], &aa[5]);
2166 else if (nread == nral)
2168 ftype = ifunc_index(d, 1);
2171 /* Check for double atoms and atoms out of bounds */
2172 for (i = 0; i < nral; i++)
2174 if (aa[i] < 1 || aa[i] > at->nr)
2176 sprintf(errbuf, "Atom index (%d) in %s out of bounds (1-%d).\n"
2177 "This probably means that you have inserted topology section \"%s\"\n"
2178 "in a part belonging to a different molecule than you intended to.\n"
2179 "In that case move the \"%s\" section to the right molecule.",
2180 aa[i], dir2str(d), at->nr, dir2str(d), dir2str(d));
2181 warning_error_and_exit(wi, errbuf, FARGS);
2184 for (j = i+1; (j < nral); j++)
2188 sprintf(errbuf, "Duplicate atom index (%d) in %s", aa[i], dir2str(d));
2189 warning_error(wi, errbuf);
2194 /* default force parameters */
2195 for (j = 0; (j < MAXATOMLIST); j++)
2197 param.a[j] = aa[j]-1;
2199 for (j = 0; (j < MAXFORCEPARAM); j++)
2204 /* Get the cmap type for this cmap angle */
2205 bFound = default_cmap_params(bondtype, at, atype, ¶m, FALSE, &cmap_type, &ncmap_params, wi);
2207 /* We want exactly one parameter (the cmap type in state A (currently no state B) back */
2208 if (bFound && ncmap_params == 1)
2210 /* Put the values in the appropriate arrays */
2211 param.c[0] = cmap_type;
2212 add_param_to_list(&bond[ftype], ¶m);
2216 /* This is essentially the same check as in default_cmap_params() done one more time */
2217 sprintf(errbuf, "Unable to assign a cmap type to torsion %d %d %d %d and %d\n",
2218 param.a[0]+1, param.a[1]+1, param.a[2]+1, param.a[3]+1, param.a[4]+1);
2219 warning_error_and_exit(wi, errbuf, FARGS);
2225 void push_vsitesn(directive d, t_params bond[],
2226 t_atoms *at, char *line,
2230 int type, ftype, j, n, ret, nj, a;
2232 double *weight = nullptr, weight_tot;
2234 char errbuf[STRLEN];
2236 /* default force parameters */
2237 for (j = 0; (j < MAXATOMLIST); j++)
2239 param.a[j] = NOTSET;
2241 for (j = 0; (j < MAXFORCEPARAM); j++)
2247 ret = sscanf(ptr, "%d%n", &a, &n);
2251 sprintf(errbuf, "Expected an atom index in section \"%s\"",
2253 warning_error_and_exit(wi, errbuf, FARGS);
2258 sscanf(ptr, "%d%n", &type, &n);
2260 ftype = ifunc_index(d, type);
2266 ret = sscanf(ptr, "%d%n", &a, &n);
2273 srenew(weight, nj+20);
2282 /* Here we use the A-state mass as a parameter.
2283 * Note that the B-state mass has no influence.
2285 weight[nj] = at->atom[atc[nj]].m;
2289 ret = sscanf(ptr, "%lf%n", &(weight[nj]), &n);
2293 sprintf(errbuf, "No weight or negative weight found for vsiten constructing atom %d (atom index %d)",
2295 warning_error_and_exit(wi, errbuf, FARGS);
2299 sprintf(errbuf, "Unknown vsiten type %d", type);
2300 warning_error_and_exit(wi, errbuf, FARGS);
2302 weight_tot += weight[nj];
2310 sprintf(errbuf, "Expected more than one atom index in section \"%s\"",
2312 warning_error_and_exit(wi, errbuf, FARGS);
2315 if (weight_tot == 0)
2317 warning_error_and_exit(wi, "The total mass of the construting atoms is zero", FARGS);
2320 for (j = 0; j < nj; j++)
2322 param.a[1] = atc[j];
2324 param.c[1] = weight[j]/weight_tot;
2325 /* Put the values in the appropriate arrays */
2326 add_param_to_list (&bond[ftype], ¶m);
2333 void push_mol(int nrmols, t_molinfo mols[], char *pline, int *whichmol,
2338 char errbuf[STRLEN];
2340 if (sscanf(pline, "%s%d", type, nrcopies) != 2)
2346 /* Search moleculename.
2347 * Here we originally only did case insensitive matching. But because
2348 * some PDB files can have many chains and use case to generate more
2349 * chain-identifiers, which in turn end up in our moleculetype name,
2350 * we added support for case-sensitivity.
2356 for (int i = 0; i < nrmols; i++)
2358 if (strcmp(type, *(mols[i].name)) == 0)
2363 if (gmx_strcasecmp(type, *(mols[i].name)) == 0)
2372 // select the case sensitive match
2373 *whichmol = matchcs;
2377 // avoid matching case-insensitive when we have multiple matches
2380 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);
2381 warning_error_and_exit(wi, errbuf, FARGS);
2385 // select the unique case insensitive match
2386 *whichmol = matchci;
2390 sprintf(errbuf, "No such moleculetype %s", type);
2391 warning_error_and_exit(wi, errbuf, FARGS);
2396 void init_block2(t_block2 *b2, int natom)
2401 snew(b2->nra, b2->nr);
2402 snew(b2->a, b2->nr);
2403 for (i = 0; (i < b2->nr); i++)
2409 void done_block2(t_block2 *b2)
2415 for (i = 0; (i < b2->nr); i++)
2425 void push_excl(char *line, t_block2 *b2, warninp_t wi)
2429 char base[STRLEN], format[STRLEN];
2430 char errbuf[STRLEN];
2432 if (sscanf(line, "%d", &i) == 0)
2437 if ((1 <= i) && (i <= b2->nr))
2445 fprintf(debug, "Unbound atom %d\n", i-1);
2449 strcpy(base, "%*d");
2452 strcpy(format, base);
2453 strcat(format, "%d");
2454 n = sscanf(line, format, &j);
2457 if ((1 <= j) && (j <= b2->nr))
2460 srenew(b2->a[i], ++(b2->nra[i]));
2461 b2->a[i][b2->nra[i]-1] = j;
2462 /* also add the reverse exclusion! */
2463 srenew(b2->a[j], ++(b2->nra[j]));
2464 b2->a[j][b2->nra[j]-1] = i;
2465 strcat(base, "%*d");
2469 sprintf(errbuf, "Invalid Atomnr j: %d, b2->nr: %d\n", j, b2->nr);
2470 warning_error_and_exit(wi, errbuf, FARGS);
2477 void b_to_b2(t_blocka *b, t_block2 *b2)
2482 for (i = 0; (i < b->nr); i++)
2484 for (j = b->index[i]; (j < b->index[i+1]); j++)
2487 srenew(b2->a[i], ++b2->nra[i]);
2488 b2->a[i][b2->nra[i]-1] = a;
2493 void b2_to_b(t_block2 *b2, t_blocka *b)
2499 for (i = 0; (i < b2->nr); i++)
2502 for (j = 0; (j < b2->nra[i]); j++)
2504 b->a[nra+j] = b2->a[i][j];
2508 /* terminate list */
2512 static int icomp(const void *v1, const void *v2)
2514 return (*((int *) v1))-(*((int *) v2));
2517 void merge_excl(t_blocka *excl, t_block2 *b2, warninp_t wi)
2522 char errbuf[STRLEN];
2528 else if (b2->nr != excl->nr)
2530 sprintf(errbuf, "DEATH HORROR: b2->nr = %d, while excl->nr = %d",
2532 warning_error_and_exit(wi, errbuf, FARGS);
2536 fprintf(debug, "Entering merge_excl\n");
2539 /* First copy all entries from excl to b2 */
2542 /* Count and sort the exclusions */
2544 for (i = 0; (i < b2->nr); i++)
2548 /* remove double entries */
2549 qsort(b2->a[i], (size_t)b2->nra[i], (size_t)sizeof(b2->a[i][0]), icomp);
2551 for (j = 1; (j < b2->nra[i]); j++)
2553 if (b2->a[i][j] != b2->a[i][k-1])
2555 b2->a[i][k] = b2->a[i][j];
2564 srenew(excl->a, excl->nra);
2569 int add_atomtype_decoupled(t_symtab *symtab, gpp_atomtype_t at,
2570 t_nbparam ***nbparam, t_nbparam ***pair)
2576 /* Define an atom type with all parameters set to zero (no interactions) */
2579 /* Type for decoupled atoms could be anything,
2580 * this should be changed automatically later when required.
2582 atom.ptype = eptAtom;
2583 for (i = 0; (i < MAXFORCEPARAM); i++)
2588 nr = add_atomtype(at, symtab, &atom, "decoupled", ¶m, -1, 0);
2590 /* Add space in the non-bonded parameters matrix */
2591 realloc_nb_params(at, nbparam, pair);
2596 static void convert_pairs_to_pairsQ(t_params *plist,
2597 real fudgeQQ, t_atoms *atoms)
2599 t_param *paramp1, *paramp2, *paramnew;
2600 int i, j, p1nr, p2nr, p2newnr;
2602 /* Add the pair list to the pairQ list */
2603 p1nr = plist[F_LJ14].nr;
2604 p2nr = plist[F_LJC14_Q].nr;
2605 p2newnr = p1nr + p2nr;
2606 snew(paramnew, p2newnr);
2608 paramp1 = plist[F_LJ14].param;
2609 paramp2 = plist[F_LJC14_Q].param;
2611 /* Fill in the new F_LJC14_Q array with the old one. NOTE:
2612 it may be possible to just ADD the converted F_LJ14 array
2613 to the old F_LJC14_Q array, but since we have to create
2614 a new sized memory structure, better just to deep copy it all.
2617 for (i = 0; i < p2nr; i++)
2619 /* Copy over parameters */
2620 for (j = 0; j < 5; j++) /* entries are 0-4 for F_LJC14_Q */
2622 paramnew[i].c[j] = paramp2[i].c[j];
2625 /* copy over atoms */
2626 for (j = 0; j < 2; j++)
2628 paramnew[i].a[j] = paramp2[i].a[j];
2632 for (i = p2nr; i < p2newnr; i++)
2636 /* Copy over parameters */
2637 paramnew[i].c[0] = fudgeQQ;
2638 paramnew[i].c[1] = atoms->atom[paramp1[j].a[0]].q;
2639 paramnew[i].c[2] = atoms->atom[paramp1[j].a[1]].q;
2640 paramnew[i].c[3] = paramp1[j].c[0];
2641 paramnew[i].c[4] = paramp1[j].c[1];
2643 /* copy over atoms */
2644 paramnew[i].a[0] = paramp1[j].a[0];
2645 paramnew[i].a[1] = paramp1[j].a[1];
2648 /* free the old pairlists */
2649 sfree(plist[F_LJC14_Q].param);
2650 sfree(plist[F_LJ14].param);
2652 /* now assign the new data to the F_LJC14_Q structure */
2653 plist[F_LJC14_Q].param = paramnew;
2654 plist[F_LJC14_Q].nr = p2newnr;
2656 /* Empty the LJ14 pairlist */
2657 plist[F_LJ14].nr = 0;
2658 plist[F_LJ14].param = nullptr;
2661 static void generate_LJCpairsNB(t_molinfo *mol, int nb_funct, t_params *nbp, warninp_t wi)
2663 int n, ntype, i, j, k;
2668 char errbuf[STRLEN];
2671 atom = mol->atoms.atom;
2673 ntype = static_cast<int>(std::sqrt(static_cast<double>(nbp->nr)));
2674 GMX_ASSERT(ntype * ntype == nbp->nr, "Number of pairs of generated non-bonded parameters should be a perfect square");
2676 for (i = 0; i < MAXATOMLIST; i++)
2678 param.a[i] = NOTSET;
2680 for (i = 0; i < MAXFORCEPARAM; i++)
2682 param.c[i] = NOTSET;
2685 /* Add a pair interaction for all non-excluded atom pairs */
2687 for (i = 0; i < n; i++)
2689 for (j = i+1; j < n; j++)
2692 for (k = excl->index[i]; k < excl->index[i+1]; k++)
2694 if (excl->a[k] == j)
2701 if (nb_funct != F_LJ)
2703 sprintf(errbuf, "Can only generate non-bonded pair interactions for Van der Waals type Lennard-Jones");
2704 warning_error_and_exit(wi, errbuf, FARGS);
2708 param.c[0] = atom[i].q;
2709 param.c[1] = atom[j].q;
2710 param.c[2] = nbp->param[ntype*atom[i].type+atom[j].type].c[0];
2711 param.c[3] = nbp->param[ntype*atom[i].type+atom[j].type].c[1];
2712 add_param_to_list(&mol->plist[F_LJC_PAIRS_NB], ¶m);
2718 static void set_excl_all(t_blocka *excl)
2722 /* Get rid of the current exclusions and exclude all atom pairs */
2724 excl->nra = nat*nat;
2725 srenew(excl->a, excl->nra);
2727 for (i = 0; i < nat; i++)
2730 for (j = 0; j < nat; j++)
2735 excl->index[nat] = k;
2738 static void decouple_atoms(t_atoms *atoms, int atomtype_decouple,
2739 int couple_lam0, int couple_lam1,
2740 const char *mol_name, warninp_t wi)
2743 char errbuf[STRLEN];
2745 for (i = 0; i < atoms->nr; i++)
2749 atom = &atoms->atom[i];
2751 if (atom->qB != atom->q || atom->typeB != atom->type)
2753 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.",
2754 i + 1, mol_name, "couple-moltype");
2755 warning_error_and_exit(wi, errbuf, FARGS);
2758 if (couple_lam0 == ecouplamNONE || couple_lam0 == ecouplamVDW)
2762 if (couple_lam0 == ecouplamNONE || couple_lam0 == ecouplamQ)
2764 atom->type = atomtype_decouple;
2766 if (couple_lam1 == ecouplamNONE || couple_lam1 == ecouplamVDW)
2770 if (couple_lam1 == ecouplamNONE || couple_lam1 == ecouplamQ)
2772 atom->typeB = atomtype_decouple;
2777 void convert_moltype_couple(t_molinfo *mol, int atomtype_decouple, real fudgeQQ,
2778 int couple_lam0, int couple_lam1,
2779 gmx_bool bCoupleIntra, int nb_funct, t_params *nbp,
2782 convert_pairs_to_pairsQ(mol->plist, fudgeQQ, &mol->atoms);
2785 generate_LJCpairsNB(mol, nb_funct, nbp, wi);
2786 set_excl_all(&mol->excls);
2788 decouple_atoms(&mol->atoms, atomtype_decouple, couple_lam0, couple_lam1,