Refactor t_molinfo to MoleculeInformation class
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / toputil.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include "toputil.h"
40
41 #include <climits>
42 #include <cmath>
43 #include <cstring>
44
45 #include <algorithm>
46
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"
57
58 /* UTILITIES */
59
60 void set_p_string(t_param *p, const char *s)
61 {
62     if (s)
63     {
64         if (strlen(s) < sizeof(p->s)-1)
65         {
66             strncpy(p->s, s, sizeof(p->s));
67         }
68         else
69         {
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);
73         }
74     }
75     else
76     {
77         strcpy(p->s, "");
78     }
79 }
80
81 void pr_alloc (int extra, t_params *pr)
82 {
83     int i, j;
84
85     /* get new space for arrays */
86     if (extra < 0)
87     {
88         gmx_fatal(FARGS, "Trying to make array smaller.\n");
89     }
90     if (extra == 0)
91     {
92         return;
93     }
94     GMX_ASSERT(pr->nr != 0 || pr->param == nullptr, "Invalid t_params object");
95     if (pr->nr+extra > pr->maxnr)
96     {
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++)
100         {
101             for (j = 0; (j < MAXATOMLIST); j++)
102             {
103                 pr->param[i].a[j] = 0;
104             }
105             for (j = 0; (j < MAXFORCEPARAM); j++)
106             {
107                 pr->param[i].c[j] = 0;
108             }
109             set_p_string(&(pr->param[i]), "");
110         }
111     }
112 }
113
114 void init_plist(t_params plist[])
115 {
116     int i;
117
118     for (i = 0; (i < F_NRE); i++)
119     {
120         plist[i].nr    = 0;
121         plist[i].maxnr = 0;
122         plist[i].param = nullptr;
123
124         /* CMAP */
125         plist[i].ncmap        = 0;
126         plist[i].cmap         = nullptr;
127         plist[i].grid_spacing = 0;
128         plist[i].nc           = 0;
129         plist[i].nct          = 0;
130         plist[i].cmap_types   = nullptr;
131     }
132 }
133
134 void done_plist(t_params *plist)
135 {
136     for (int i = 0; i < F_NRE; i++)
137     {
138         t_params *pl = &plist[i];
139         sfree(pl->param);
140         sfree(pl->cmap);
141         sfree(pl->cmap_types);
142     }
143 }
144
145 void cp_param(t_param *dest, t_param *src)
146 {
147     int j;
148
149     for (j = 0; (j < MAXATOMLIST); j++)
150     {
151         dest->a[j] = src->a[j];
152     }
153     for (j = 0; (j < MAXFORCEPARAM); j++)
154     {
155         dest->c[j] = src->c[j];
156     }
157     strncpy(dest->s, src->s, sizeof(dest->s));
158 }
159
160 void add_param_to_list(t_params *list, t_param *b)
161 {
162     int j;
163
164     /* allocate one position extra */
165     pr_alloc (1, list);
166
167     /* fill the arrays */
168     for (j = 0; (j < MAXFORCEPARAM); j++)
169     {
170         list->param[list->nr].c[j]   = b->c[j];
171     }
172     for (j = 0; (j < MAXATOMLIST); j++)
173     {
174         list->param[list->nr].a[j]   = b->a[j];
175     }
176     memset(list->param[list->nr].s, 0, sizeof(list->param[list->nr].s));
177
178     list->nr++;
179 }
180
181 /* PRINTING STRUCTURES */
182
183 static void print_bt(FILE *out, Directive d, gpp_atomtype *at,
184                      int ftype, int fsubtype, t_params plist[],
185                      bool bFullDih)
186 {
187     /* This dihp is a DIRTY patch because the dih-types do not use
188      * all four atoms to determine the type.
189      */
190     const int    dihp[2][2] = { { 1, 2 }, { 0, 3 } };
191     t_params    *bt;
192     int          i, j, f, nral, nrfp;
193     bool         bDih = FALSE, bSwapParity;
194
195     bt = &(plist[ftype]);
196
197     if (!bt->nr)
198     {
199         return;
200     }
201
202     f = 0;
203     switch (ftype)
204     {
205         case F_G96ANGLES:
206             f = 1;
207             break;
208         case F_G96BONDS:
209             f = 1;
210             break;
211         case F_MORSE:
212             f = 2;
213             break;
214         case F_CUBICBONDS:
215             f = 3;
216             break;
217         case F_CONNBONDS:
218             f = 4;
219             break;
220         case F_HARMONIC:
221             f = 5;
222             break;
223         case F_CROSS_BOND_ANGLES:
224             f = 2;
225             break;
226         case F_CROSS_BOND_BONDS:
227             f = 3;
228             break;
229         case F_UREY_BRADLEY:
230             f = 4;
231             break;
232         case F_PDIHS:
233         case F_RBDIHS:
234         case F_FOURDIHS:
235             bDih = TRUE;
236             break;
237         case F_IDIHS:
238             f    = 1;
239             bDih = TRUE;
240             break;
241         case F_CONSTRNC:
242             f = 1;
243             break;
244         case F_VSITE3FD:
245             f = 1;
246             break;
247         case F_VSITE3FAD:
248             f = 2;
249             break;
250         case F_VSITE3OUT:
251             f = 3;
252             break;
253         case F_VSITE4FDN:
254             f = 1;
255             break;
256         case F_CMAP:
257             f = 1;
258             break;
259
260         default:
261             bDih = FALSE;
262     }
263     if (bFullDih)
264     {
265         bDih = FALSE;
266     }
267     if (fsubtype)
268     {
269         f = fsubtype-1;
270     }
271
272     nral = NRAL(ftype);
273     nrfp = NRFP(ftype);
274
275     /* header */
276     fprintf(out, "[ %s ]\n", dir2str(d));
277     fprintf(out, "; ");
278     if (!bDih)
279     {
280         fprintf (out, "%3s  %4s", "ai", "aj");
281         for (j = 2; (j < nral); j++)
282         {
283             fprintf (out, "  %3c%c", 'a', 'i'+j);
284         }
285     }
286     else
287     {
288         for (j = 0; (j < 2); j++)
289         {
290             fprintf (out, "%3c%c", 'a', 'i'+dihp[f][j]);
291         }
292     }
293
294     fprintf (out, " funct");
295     for (j = 0; (j < nrfp); j++)
296     {
297         fprintf (out, " %12c%1d", 'c', j);
298     }
299     fprintf (out, "\n");
300
301     /* print bondtypes */
302     for (i = 0; (i < bt->nr); i++)
303     {
304         bSwapParity = (bt->param[i].c0() == NOTSET) && (bt->param[i].c1() == -1);
305         if (!bDih)
306         {
307             for (j = 0; (j < nral); j++)
308             {
309                 fprintf (out, "%5s ", get_atomtype_name(bt->param[i].a[j], at));
310             }
311         }
312         else
313         {
314             for (j = 0; (j < 2); j++)
315             {
316                 fprintf (out, "%5s ", get_atomtype_name(bt->param[i].a[dihp[f][j]], at));
317             }
318         }
319         fprintf (out, "%5d ", bSwapParity ? -f-1 : f+1);
320
321         if (bt->param[i].s[0])
322         {
323             fprintf(out, "   %s", bt->param[i].s);
324         }
325         else
326         {
327             for (j = 0; (j < nrfp && (bt->param[i].c[j] != NOTSET)); j++)
328             {
329                 fprintf (out, "%13.6e ", bt->param[i].c[j]);
330             }
331         }
332
333         fprintf (out, "\n");
334     }
335     fprintf (out, "\n");
336     fflush (out);
337 }
338
339 void print_blocka(FILE *out, const char *szName,
340                   const char *szIndex, const char *szA,
341                   t_blocka *block)
342 {
343     int i, j;
344
345     fprintf (out, "; %s\n", szName);
346     fprintf (out, "; %4s    %s\n", szIndex, szA);
347     for (i = 0; (i < block->nr); i++)
348     {
349         for (i = 0; (i < block->nr); i++)
350         {
351             fprintf (out, "%6d", i+1);
352             for (j = block->index[i]; (j < (block->index[i+1])); j++)
353             {
354                 fprintf (out, "%5d", block->a[j]+1);
355             }
356             fprintf (out, "\n");
357         }
358         fprintf (out, "\n");
359     }
360 }
361
362 void print_excl(FILE *out, int natoms, t_excls excls[])
363 {
364     int         i;
365     bool        have_excl;
366     int         j;
367
368     have_excl = FALSE;
369     for (i = 0; i < natoms && !have_excl; i++)
370     {
371         have_excl = (excls[i].nr > 0);
372     }
373
374     if (have_excl)
375     {
376         fprintf (out, "[ %s ]\n", dir2str(Directive::d_exclusions));
377         fprintf (out, "; %4s    %s\n", "i", "excluded from i");
378         for (i = 0; i < natoms; i++)
379         {
380             if (excls[i].nr > 0)
381             {
382                 fprintf (out, "%6d ", i+1);
383                 for (j = 0; j < excls[i].nr; j++)
384                 {
385                     fprintf (out, " %5d", excls[i].e[j]+1);
386                 }
387                 fprintf (out, "\n");
388             }
389         }
390         fprintf (out, "\n");
391         fflush(out);
392     }
393 }
394
395 static double get_residue_charge(const t_atoms *atoms, int at)
396 {
397     int    ri;
398     double q;
399
400     ri = atoms->atom[at].resind;
401     q  = 0;
402     while (at < atoms->nr && atoms->atom[at].resind == ri)
403     {
404         q += atoms->atom[at].q;
405         at++;
406     }
407
408     return q;
409 }
410
411 void print_atoms(FILE *out, gpp_atomtype *atype, t_atoms *at, int *cgnr,
412                  bool bRTPresname)
413 {
414     int         i, ri;
415     int         tpA, tpB;
416     const char *as;
417     char       *tpnmA, *tpnmB;
418     double      qres, qtot;
419
420     as = dir2str(Directive::d_atoms);
421     fprintf(out, "[ %s ]\n", as);
422     fprintf(out, "; %4s %10s %6s %7s%6s %6s %10s %10s %6s %10s %10s\n",
423             "nr", "type", "resnr", "residue", "atom", "cgnr", "charge", "mass", "typeB", "chargeB", "massB");
424
425     qtot  = 0;
426
427     if (at->nres)
428     {
429         /* if the information is present... */
430         for (i = 0; (i < at->nr); i++)
431         {
432             ri = at->atom[i].resind;
433             if ((i == 0 || ri != at->atom[i-1].resind) &&
434                 at->resinfo[ri].rtp != nullptr)
435             {
436                 qres = get_residue_charge(at, i);
437                 fprintf(out, "; residue %3d %-3s rtp %-4s q ",
438                         at->resinfo[ri].nr,
439                         *at->resinfo[ri].name,
440                         *at->resinfo[ri].rtp);
441                 if (fabs(qres) < 0.001)
442                 {
443                     fprintf(out, " %s", "0.0");
444                 }
445                 else
446                 {
447                     fprintf(out, "%+3.1f", qres);
448                 }
449                 fprintf(out, "\n");
450             }
451             tpA = at->atom[i].type;
452             if ((tpnmA = get_atomtype_name(tpA, atype)) == nullptr)
453             {
454                 gmx_fatal(FARGS, "tpA = %d, i= %d in print_atoms", tpA, i);
455             }
456
457             /* This is true by construction, but static analysers don't know */
458             GMX_ASSERT(!bRTPresname || at->resinfo[at->atom[i].resind].rtp, "-rtpres did not have residue name available");
459             fprintf(out, "%6d %10s %6d%c %5s %6s %6d %10g %10g",
460                     i+1, tpnmA,
461                     at->resinfo[ri].nr,
462                     at->resinfo[ri].ic,
463                     bRTPresname ?
464                     *(at->resinfo[at->atom[i].resind].rtp) :
465                     *(at->resinfo[at->atom[i].resind].name),
466                     *(at->atomname[i]), cgnr[i],
467                     at->atom[i].q, at->atom[i].m);
468             if (PERTURBED(at->atom[i]))
469             {
470                 tpB = at->atom[i].typeB;
471                 if ((tpnmB = get_atomtype_name(tpB, atype)) == nullptr)
472                 {
473                     gmx_fatal(FARGS, "tpB = %d, i= %d in print_atoms", tpB, i);
474                 }
475                 fprintf(out, " %6s %10g %10g",
476                         tpnmB, at->atom[i].qB, at->atom[i].mB);
477             }
478             // Accumulate the total charge to help troubleshoot issues.
479             qtot += static_cast<double>(at->atom[i].q);
480             // Round it to zero if it is close to zero, because
481             // printing -9.34e-5 confuses users.
482             if (fabs(qtot) < 0.0001)
483             {
484                 qtot = 0;
485             }
486             // Write the total charge for the last atom of the system
487             // and/or residue, because generally that's where it is
488             // expected to be an integer.
489             if (i == at->nr-1 || ri != at->atom[i+1].resind)
490             {
491                 fprintf(out, "   ; qtot %.4g\n", qtot);
492             }
493             else
494             {
495                 fputs("\n", out);
496             }
497         }
498     }
499     fprintf(out, "\n");
500     fflush(out);
501 }
502
503 void print_bondeds(FILE *out, int natoms, Directive d,
504                    int ftype, int fsubtype, t_params plist[])
505 {
506     t_symtab       stab;
507     gpp_atomtype  *atype;
508     t_param       *param;
509     t_atom        *a;
510     int            i;
511
512     atype = init_atomtype();
513     snew(a, 1);
514     snew(param, 1);
515     open_symtab(&stab);
516     for (i = 0; (i < natoms); i++)
517     {
518         char buf[12];
519         sprintf(buf, "%4d", (i+1));
520         add_atomtype(atype, &stab, a, buf, param, 0, 0);
521     }
522     print_bt(out, d, atype, ftype, fsubtype, plist, TRUE);
523
524     done_symtab(&stab);
525     sfree(a);
526     sfree(param);
527     done_atomtype(atype);
528 }