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) 2012,2014,2015,2017,2018,2019, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
47 #include "gromacs/gmxpreprocess/gpp_atomtype.h"
48 #include "gromacs/gmxpreprocess/grompp_impl.h"
49 #include "gromacs/gmxpreprocess/notset.h"
50 #include "gromacs/gmxpreprocess/topdirs.h"
51 #include "gromacs/topology/block.h"
52 #include "gromacs/topology/ifunc.h"
53 #include "gromacs/topology/symtab.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/gmxassert.h"
56 #include "gromacs/utility/smalloc.h"
60 void set_p_string(t_param *p, const char *s)
64 if (strlen(s) < sizeof(p->s)-1)
66 strncpy(p->s, s, sizeof(p->s));
70 gmx_fatal(FARGS, "Increase MAXSLEN in the grompp code to at least %zu,"
71 " or shorten your definition of bonds like %s to at most %d",
72 strlen(s)+1, s, MAXSLEN-1);
81 void pr_alloc (int extra, InteractionTypeParameters *pr)
85 /* get new space for arrays */
88 gmx_fatal(FARGS, "Trying to make array smaller.\n");
94 GMX_ASSERT(pr->nr != 0 || pr->param == nullptr, "Invalid InteractionTypeParameters object");
95 if (pr->nr+extra > pr->maxnr)
97 pr->maxnr = std::max(static_cast<int>(1.2*pr->maxnr), pr->maxnr + extra);
98 srenew(pr->param, pr->maxnr);
99 for (i = pr->nr; (i < pr->maxnr); i++)
101 for (j = 0; (j < MAXATOMLIST); j++)
103 pr->param[i].a[j] = 0;
105 for (j = 0; (j < MAXFORCEPARAM); j++)
107 pr->param[i].c[j] = 0;
109 set_p_string(&(pr->param[i]), "");
114 void init_plist(gmx::ArrayRef<InteractionTypeParameters> plist)
118 for (i = 0; (i < F_NRE); i++)
122 plist[i].param = nullptr;
126 void done_plist(gmx::ArrayRef<InteractionTypeParameters> plist)
128 for (int i = 0; i < F_NRE; i++)
130 sfree(plist[i].param);
134 void cp_param(t_param *dest, t_param *src)
138 for (j = 0; (j < MAXATOMLIST); j++)
140 dest->a[j] = src->a[j];
142 for (j = 0; (j < MAXFORCEPARAM); j++)
144 dest->c[j] = src->c[j];
146 strncpy(dest->s, src->s, sizeof(dest->s));
149 void add_param_to_list(InteractionTypeParameters *list, t_param *b)
153 /* allocate one position extra */
156 /* fill the arrays */
157 for (j = 0; (j < MAXFORCEPARAM); j++)
159 list->param[list->nr].c[j] = b->c[j];
161 for (j = 0; (j < MAXATOMLIST); j++)
163 list->param[list->nr].a[j] = b->a[j];
165 memset(list->param[list->nr].s, 0, sizeof(list->param[list->nr].s));
170 /* PRINTING STRUCTURES */
172 static void print_bt(FILE *out, Directive d, gpp_atomtype *at,
173 int ftype, int fsubtype, gmx::ArrayRef<const InteractionTypeParameters> plist,
176 /* This dihp is a DIRTY patch because the dih-types do not use
177 * all four atoms to determine the type.
179 const int dihp[2][2] = { { 1, 2 }, { 0, 3 } };
180 int i, j, f, nral, nrfp;
181 bool bDih = FALSE, bSwapParity;
183 const InteractionTypeParameters *bt = &(plist[ftype]);
211 case F_CROSS_BOND_ANGLES:
214 case F_CROSS_BOND_BONDS:
264 fprintf(out, "[ %s ]\n", dir2str(d));
268 fprintf (out, "%3s %4s", "ai", "aj");
269 for (j = 2; (j < nral); j++)
271 fprintf (out, " %3c%c", 'a', 'i'+j);
276 for (j = 0; (j < 2); j++)
278 fprintf (out, "%3c%c", 'a', 'i'+dihp[f][j]);
282 fprintf (out, " funct");
283 for (j = 0; (j < nrfp); j++)
285 fprintf (out, " %12c%1d", 'c', j);
289 /* print bondtypes */
290 for (i = 0; (i < bt->nr); i++)
292 bSwapParity = (bt->param[i].c0() == NOTSET) && (bt->param[i].c1() == -1);
295 for (j = 0; (j < nral); j++)
297 fprintf (out, "%5s ", get_atomtype_name(bt->param[i].a[j], at));
302 for (j = 0; (j < 2); j++)
304 fprintf (out, "%5s ", get_atomtype_name(bt->param[i].a[dihp[f][j]], at));
307 fprintf (out, "%5d ", bSwapParity ? -f-1 : f+1);
309 if (bt->param[i].s[0])
311 fprintf(out, " %s", bt->param[i].s);
315 for (j = 0; (j < nrfp && (bt->param[i].c[j] != NOTSET)); j++)
317 fprintf (out, "%13.6e ", bt->param[i].c[j]);
327 void print_blocka(FILE *out, const char *szName,
328 const char *szIndex, const char *szA,
333 fprintf (out, "; %s\n", szName);
334 fprintf (out, "; %4s %s\n", szIndex, szA);
335 for (i = 0; (i < block->nr); i++)
337 for (i = 0; (i < block->nr); i++)
339 fprintf (out, "%6d", i+1);
340 for (j = block->index[i]; (j < (block->index[i+1])); j++)
342 fprintf (out, "%5d", block->a[j]+1);
350 void print_excl(FILE *out, int natoms, t_excls excls[])
357 for (i = 0; i < natoms && !have_excl; i++)
359 have_excl = (excls[i].nr > 0);
364 fprintf (out, "[ %s ]\n", dir2str(Directive::d_exclusions));
365 fprintf (out, "; %4s %s\n", "i", "excluded from i");
366 for (i = 0; i < natoms; i++)
370 fprintf (out, "%6d ", i+1);
371 for (j = 0; j < excls[i].nr; j++)
373 fprintf (out, " %5d", excls[i].e[j]+1);
383 static double get_residue_charge(const t_atoms *atoms, int at)
388 ri = atoms->atom[at].resind;
390 while (at < atoms->nr && atoms->atom[at].resind == ri)
392 q += atoms->atom[at].q;
399 void print_atoms(FILE *out, gpp_atomtype *atype, t_atoms *at, int *cgnr,
408 as = dir2str(Directive::d_atoms);
409 fprintf(out, "[ %s ]\n", as);
410 fprintf(out, "; %4s %10s %6s %7s%6s %6s %10s %10s %6s %10s %10s\n",
411 "nr", "type", "resnr", "residue", "atom", "cgnr", "charge", "mass", "typeB", "chargeB", "massB");
417 /* if the information is present... */
418 for (i = 0; (i < at->nr); i++)
420 ri = at->atom[i].resind;
421 if ((i == 0 || ri != at->atom[i-1].resind) &&
422 at->resinfo[ri].rtp != nullptr)
424 qres = get_residue_charge(at, i);
425 fprintf(out, "; residue %3d %-3s rtp %-4s q ",
427 *at->resinfo[ri].name,
428 *at->resinfo[ri].rtp);
429 if (fabs(qres) < 0.001)
431 fprintf(out, " %s", "0.0");
435 fprintf(out, "%+3.1f", qres);
439 tpA = at->atom[i].type;
440 if ((tpnmA = get_atomtype_name(tpA, atype)) == nullptr)
442 gmx_fatal(FARGS, "tpA = %d, i= %d in print_atoms", tpA, i);
445 /* This is true by construction, but static analysers don't know */
446 GMX_ASSERT(!bRTPresname || at->resinfo[at->atom[i].resind].rtp, "-rtpres did not have residue name available");
447 fprintf(out, "%6d %10s %6d%c %5s %6s %6d %10g %10g",
452 *(at->resinfo[at->atom[i].resind].rtp) :
453 *(at->resinfo[at->atom[i].resind].name),
454 *(at->atomname[i]), cgnr[i],
455 at->atom[i].q, at->atom[i].m);
456 if (PERTURBED(at->atom[i]))
458 tpB = at->atom[i].typeB;
459 if ((tpnmB = get_atomtype_name(tpB, atype)) == nullptr)
461 gmx_fatal(FARGS, "tpB = %d, i= %d in print_atoms", tpB, i);
463 fprintf(out, " %6s %10g %10g",
464 tpnmB, at->atom[i].qB, at->atom[i].mB);
466 // Accumulate the total charge to help troubleshoot issues.
467 qtot += static_cast<double>(at->atom[i].q);
468 // Round it to zero if it is close to zero, because
469 // printing -9.34e-5 confuses users.
470 if (fabs(qtot) < 0.0001)
474 // Write the total charge for the last atom of the system
475 // and/or residue, because generally that's where it is
476 // expected to be an integer.
477 if (i == at->nr-1 || ri != at->atom[i+1].resind)
479 fprintf(out, " ; qtot %.4g\n", qtot);
491 void print_bondeds(FILE *out, int natoms, Directive d,
492 int ftype, int fsubtype, gmx::ArrayRef<const InteractionTypeParameters> plist)
500 atype = init_atomtype();
504 for (i = 0; (i < natoms); i++)
507 sprintf(buf, "%4d", (i+1));
508 add_atomtype(atype, &stab, a, buf, param, 0, 0);
510 print_bt(out, d, atype, ftype, fsubtype, plist, TRUE);
515 done_atomtype(atype);