Merge branch release-2021
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / topdirs.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) 2013,2014,2015,2017,2018 by the GROMACS development team.
7  * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39
40 #include "topdirs.h"
41
42 #include <cstdarg>
43 #include <cstdio>
44
45 #include <algorithm>
46
47 #include "gromacs/topology/idef.h"
48 #include "gromacs/utility/cstringutil.h"
49 #include "gromacs/utility/enumerationhelpers.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/smalloc.h"
52
53 /* Must correspond to the Directive enum in grompp_impl.h */
54 static gmx::EnumerationArray<Directive, const char*> directive_names = {
55     { "defaults",
56       "atomtypes",
57       "bondtypes",
58       "constrainttypes",
59       "pairtypes",
60       "angletypes",
61       "dihedraltypes",
62       "nonbond_params",
63       "implicit_genborn_params",
64       "implicit_surface_params",
65       "cmaptypes",
66       /* All the directives above can not appear after moleculetype */
67       "moleculetype",
68       "atoms",
69       "virtual_sites1",
70       "virtual_sites2",
71       "virtual_sites3",
72       "virtual_sites4",
73       "virtual_sitesn",
74       "bonds",
75       "exclusions",
76       "pairs",
77       "pairs_nb",
78       "angles",
79       "dihedrals",
80       "constraints",
81       "settles",
82       "polarization",
83       "water_polarization",
84       "thole_polarization",
85       "system",
86       "molecules",
87       "position_restraints",
88       "angle_restraints",
89       "angle_restraints_z",
90       "distance_restraints",
91       "orientation_restraints",
92       "dihedral_restraints",
93       "cmap",
94       "intermolecular_interactions",
95       "maxdirs",
96       "invalid",
97       "none" }
98 };
99
100 int ifunc_index(Directive d, int type)
101 {
102     switch (d)
103     {
104         case Directive::d_bondtypes:
105         case Directive::d_bonds:
106             switch (type)
107             {
108                 case 1: return F_BONDS;
109                 case 2: return F_G96BONDS;
110                 case 3: return F_MORSE;
111                 case 4: return F_CUBICBONDS;
112                 case 5: return F_CONNBONDS;
113                 case 6: return F_HARMONIC;
114                 case 7: return F_FENEBONDS;
115                 case 8: return F_TABBONDS;
116                 case 9: return F_TABBONDSNC;
117                 case 10: return F_RESTRBONDS;
118                 default: gmx_fatal(FARGS, "Invalid bond type %d", type);
119             }
120         case Directive::d_angles:
121         case Directive::d_angletypes:
122             switch (type)
123             {
124                 case 1: return F_ANGLES;
125                 case 2: return F_G96ANGLES;
126                 case 3: return F_CROSS_BOND_BONDS;
127                 case 4: return F_CROSS_BOND_ANGLES;
128                 case 5: return F_UREY_BRADLEY;
129                 case 6: return F_QUARTIC_ANGLES;
130                 case 8: return F_TABANGLES;
131                 case 9: return F_LINEAR_ANGLES;
132                 case 10: return F_RESTRANGLES;
133                 default: gmx_fatal(FARGS, "Invalid angle type %d", type);
134             }
135         case Directive::d_pairs:
136         case Directive::d_pairtypes:
137             if (type == 1 || (d == Directive::d_pairtypes && type == 2))
138             {
139                 return F_LJ14;
140             }
141             else if (type == 2)
142             {
143                 return F_LJC14_Q;
144             }
145             else
146             {
147                 gmx_fatal(FARGS, "Invalid pairs type %d", type);
148             }
149         case Directive::d_pairs_nb: return F_LJC_PAIRS_NB;
150         case Directive::d_dihedrals:
151         case Directive::d_dihedraltypes:
152             switch (type)
153             {
154                 case 1: return F_PDIHS;
155                 case 2: return F_IDIHS;
156                 case 3: return F_RBDIHS;
157                 case 4: return F_PIDIHS;
158                 case 5: return F_FOURDIHS;
159                 case 8: return F_TABDIHS;
160                 case 9:
161                     return F_PDIHS; /* proper dihedrals where we allow multiple terms over single bond */
162                 case 10: return F_RESTRDIHS;
163                 case 11: return F_CBTDIHS;
164                 default: gmx_fatal(FARGS, "Invalid dihedral type %d", type);
165             }
166         case Directive::d_cmaptypes:
167         case Directive::d_cmap: return F_CMAP;
168
169         case Directive::d_nonbond_params:
170             if (type == 1)
171             {
172                 return F_LJ;
173             }
174             else
175             {
176                 return F_BHAM;
177             }
178         case Directive::d_vsites1:
179             if (type == 1)
180             {
181                 return F_VSITE1;
182             }
183             else
184             {
185                 gmx_fatal(FARGS, "Invalid vsites1 type %d", type);
186             }
187         case Directive::d_vsites2:
188             switch (type)
189             {
190                 case 1: return F_VSITE2;
191                 case 2: return F_VSITE2FD;
192                 default: gmx_fatal(FARGS, "Invalid vsites2 type %d", type);
193             }
194         case Directive::d_vsites3:
195             switch (type)
196             {
197                 case 1: return F_VSITE3;
198                 case 2: return F_VSITE3FD;
199                 case 3: return F_VSITE3FAD;
200                 case 4: return F_VSITE3OUT;
201                 default: gmx_fatal(FARGS, "Invalid vsites3 type %d", type);
202             }
203         case Directive::d_vsites4:
204             switch (type)
205             {
206                 case 1: return F_VSITE4FD;
207                 case 2: return F_VSITE4FDN;
208                 default: gmx_fatal(FARGS, "Invalid vsites4 type %d", type);
209             }
210         case Directive::d_vsitesn: return F_VSITEN;
211         case Directive::d_constraints:
212         case Directive::d_constrainttypes:
213             switch (type)
214             {
215                 case 1: return F_CONSTR;
216                 case 2: return F_CONSTRNC;
217                 default: gmx_fatal(FARGS, "Invalid constraints type %d", type);
218             }
219         case Directive::d_settles: return F_SETTLE;
220         case Directive::d_position_restraints:
221             switch (type)
222             {
223                 case 1: return F_POSRES;
224                 case 2: return F_FBPOSRES;
225                 default: gmx_fatal(FARGS, "Invalid position restraint type %d", type);
226             }
227         case Directive::d_polarization:
228             switch (type)
229             {
230                 case 1: return F_POLARIZATION;
231                 case 2: return F_ANHARM_POL;
232                 default: gmx_fatal(FARGS, "Invalid polarization type %d", type);
233             }
234         case Directive::d_thole_polarization: return F_THOLE_POL;
235         case Directive::d_water_polarization: return F_WATER_POL;
236         case Directive::d_angle_restraints: return F_ANGRES;
237         case Directive::d_angle_restraints_z: return F_ANGRESZ;
238         case Directive::d_distance_restraints: return F_DISRES;
239         case Directive::d_orientation_restraints: return F_ORIRES;
240         case Directive::d_dihedral_restraints: return F_DIHRES;
241         default:
242             gmx_fatal(FARGS, "invalid directive %s in ifunc_index (%s:%d)", dir2str(d), __FILE__, __LINE__);
243     }
244 }
245
246 const char* dir2str(Directive d)
247 {
248     int index = static_cast<int>(d);
249     return directive_names[index];
250 }
251
252 Directive str2dir(char* dstr)
253 {
254     char buf[STRLEN], *ptr;
255
256     /* Hack to be able to read old topologies */
257     if (gmx_strncasecmp_min(dstr, "dummies", 7) == 0)
258     {
259         sprintf(buf, "virtual_sites%s", dstr + 7);
260         ptr = buf;
261     }
262     else
263     {
264         ptr = dstr;
265     }
266
267     for (auto d : gmx::EnumerationWrapper<Directive>())
268     {
269         if (gmx_strcasecmp_min(ptr, dir2str(static_cast<Directive>(d))) == 0)
270         {
271             return static_cast<Directive>(d);
272         }
273     }
274
275     return Directive::d_invalid;
276 }
277
278 static gmx::EnumerationArray<Directive, Directive*> necessary = { { nullptr } };
279
280 static void set_nec(Directive** n, ...)
281 /* Must always have at least one extra argument */
282 {
283     va_list   ap;
284     int       ind = 0;
285     Directive d;
286
287     va_start(ap, n);
288     do
289     {
290         d = static_cast<Directive>(va_arg(ap, int));
291         srenew(*n, ++ind);
292         (*n)[ind - 1] = d;
293     } while (d != Directive::d_none);
294     va_end(ap);
295 }
296
297 void DS_Init(DirStack** DS)
298 {
299     if (necessary[0] == nullptr)
300     {
301         set_nec(&(necessary[Directive::d_defaults]), Directive::d_none);
302         set_nec(&(necessary[Directive::d_atomtypes]), Directive::d_defaults, Directive::d_none);
303         set_nec(&(necessary[Directive::d_bondtypes]), Directive::d_atomtypes, Directive::d_none);
304         set_nec(&(necessary[Directive::d_constrainttypes]), Directive::d_atomtypes, Directive::d_none);
305         set_nec(&(necessary[Directive::d_pairtypes]), Directive::d_atomtypes, Directive::d_none);
306         set_nec(&(necessary[Directive::d_angletypes]), Directive::d_atomtypes, Directive::d_none);
307         set_nec(&(necessary[Directive::d_dihedraltypes]), Directive::d_atomtypes, Directive::d_none);
308         set_nec(&(necessary[Directive::d_nonbond_params]), Directive::d_atomtypes, Directive::d_none);
309         // Note that the content of the next two directives are
310         // ignored, but if grompp reads them in old force field files,
311         // it still needs to understand that they are in a valid place
312         // in the .top structure. It doesn't have to require them to
313         // be in the same place that was valid in old versions (ie. child
314         // directive of [atomtypes]) but any relevant case will
315         // satisfy that.
316         set_nec(&(necessary[Directive::d_implicit_genborn_params]), Directive::d_atomtypes, Directive::d_none);
317         set_nec(&(necessary[Directive::d_implicit_surface_params]), Directive::d_atomtypes, Directive::d_none);
318         set_nec(&(necessary[Directive::d_cmaptypes]), Directive::d_atomtypes, Directive::d_none);
319         set_nec(&(necessary[Directive::d_moleculetype]), Directive::d_atomtypes, Directive::d_none);
320         set_nec(&(necessary[Directive::d_atoms]), Directive::d_moleculetype, Directive::d_none);
321         set_nec(&(necessary[Directive::d_vsites1]), Directive::d_atoms, Directive::d_none);
322         set_nec(&(necessary[Directive::d_vsites2]), Directive::d_atoms, Directive::d_none);
323         set_nec(&(necessary[Directive::d_vsites3]), Directive::d_atoms, Directive::d_none);
324         set_nec(&(necessary[Directive::d_vsites4]), Directive::d_atoms, Directive::d_none);
325         set_nec(&(necessary[Directive::d_vsitesn]), Directive::d_atoms, Directive::d_none);
326         set_nec(&(necessary[Directive::d_bonds]), Directive::d_atoms, Directive::d_none);
327         set_nec(&(necessary[Directive::d_exclusions]),
328                 Directive::d_bonds,
329                 Directive::d_constraints,
330                 Directive::d_settles,
331                 Directive::d_none);
332         set_nec(&(necessary[Directive::d_pairs]), Directive::d_atoms, Directive::d_none);
333         set_nec(&(necessary[Directive::d_pairs_nb]), Directive::d_atoms, Directive::d_none);
334         set_nec(&(necessary[Directive::d_angles]), Directive::d_atoms, Directive::d_none);
335         set_nec(&(necessary[Directive::d_polarization]), Directive::d_atoms, Directive::d_none);
336         set_nec(&(necessary[Directive::d_water_polarization]), Directive::d_atoms, Directive::d_none);
337         set_nec(&(necessary[Directive::d_thole_polarization]), Directive::d_atoms, Directive::d_none);
338         set_nec(&(necessary[Directive::d_dihedrals]), Directive::d_atoms, Directive::d_none);
339         set_nec(&(necessary[Directive::d_constraints]), Directive::d_atoms, Directive::d_none);
340         set_nec(&(necessary[Directive::d_settles]), Directive::d_atoms, Directive::d_none);
341         set_nec(&(necessary[Directive::d_system]), Directive::d_moleculetype, Directive::d_none);
342         set_nec(&(necessary[Directive::d_molecules]), Directive::d_system, Directive::d_none);
343         set_nec(&(necessary[Directive::d_position_restraints]), Directive::d_atoms, Directive::d_none);
344         set_nec(&(necessary[Directive::d_angle_restraints]), Directive::d_atoms, Directive::d_none);
345         set_nec(&(necessary[Directive::d_angle_restraints_z]), Directive::d_atoms, Directive::d_none);
346         set_nec(&(necessary[Directive::d_distance_restraints]), Directive::d_atoms, Directive::d_none);
347         set_nec(&(necessary[Directive::d_orientation_restraints]), Directive::d_atoms, Directive::d_none);
348         set_nec(&(necessary[Directive::d_dihedral_restraints]), Directive::d_atoms, Directive::d_none);
349         set_nec(&(necessary[Directive::d_cmap]), Directive::d_atoms, Directive::d_none);
350         set_nec(&(necessary[Directive::d_intermolecular_interactions]),
351                 Directive::d_molecules,
352                 Directive::d_none);
353     }
354     *DS = nullptr;
355 }
356
357 void DS_Done(DirStack** DS)
358 {
359     DirStack* D;
360
361     while (*DS != nullptr)
362     {
363         D   = *DS;
364         *DS = (*DS)->prev;
365         sfree(D);
366     }
367 }
368
369 void DS_Push(DirStack** DS, Directive d)
370 {
371     DirStack* D;
372
373     snew(D, 1);
374     D->d    = d;
375     D->prev = *DS;
376     *DS     = D;
377 }
378
379 int DS_Search(DirStack* DS, Directive d)
380 {
381     DirStack* D;
382
383     D = DS;
384     while ((D != nullptr) && (D->d != d))
385     {
386         D = D->prev;
387     }
388
389     return static_cast<int>(D != nullptr);
390 }
391
392 int DS_Check_Order(DirStack* DS, Directive d)
393 {
394     Directive d0;
395     int       i = 0;
396
397     /* Check if parameter definitions appear after a moleculetype directive */
398     if (d < Directive::d_moleculetype && DS_Search(DS, Directive::d_moleculetype))
399     {
400         return FALSE;
401     }
402
403     /* Check if all the necessary directives have appeared before directive d */
404     if (necessary[d][0] == Directive::d_none)
405     {
406         return TRUE;
407     }
408     else
409     {
410         do
411         {
412             d0 = necessary[d][i++];
413             if (DS_Search(DS, d0))
414             {
415                 return TRUE;
416             }
417         } while (d0 != Directive::d_none);
418     }
419     return FALSE;
420 }