Apply clang-format to source tree
[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,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 "topdirs.h"
40
41 #include <cstdarg>
42 #include <cstdio>
43
44 #include <algorithm>
45
46 #include "gromacs/topology/idef.h"
47 #include "gromacs/utility/cstringutil.h"
48 #include "gromacs/utility/enumerationhelpers.h"
49 #include "gromacs/utility/fatalerror.h"
50 #include "gromacs/utility/smalloc.h"
51
52 /* Must correspond to the Directive enum in grompp_impl.h */
53 static gmx::EnumerationArray<Directive, const char*> directive_names = {
54     { "defaults", "atomtypes", "bondtypes", "constrainttypes", "pairtypes", "angletypes",
55       "dihedraltypes", "nonbond_params", "implicit_genborn_params", "implicit_surface_params",
56       "cmaptypes",
57       /* All the directives above can not appear after moleculetype */
58       "moleculetype", "atoms", "virtual_sites2", "virtual_sites3", "virtual_sites4",
59       "virtual_sitesn", "bonds", "exclusions", "pairs", "pairs_nb", "angles", "dihedrals",
60       "constraints", "settles", "polarization", "water_polarization", "thole_polarization",
61       "system", "molecules", "position_restraints", "angle_restraints", "angle_restraints_z",
62       "distance_restraints", "orientation_restraints", "dihedral_restraints", "cmap",
63       "intermolecular_interactions", "maxdirs", "invalid", "none" }
64 };
65
66 int ifunc_index(Directive d, int type)
67 {
68     switch (d)
69     {
70         case Directive::d_bondtypes:
71         case Directive::d_bonds:
72             switch (type)
73             {
74                 case 1: return F_BONDS;
75                 case 2: return F_G96BONDS;
76                 case 3: return F_MORSE;
77                 case 4: return F_CUBICBONDS;
78                 case 5: return F_CONNBONDS;
79                 case 6: return F_HARMONIC;
80                 case 7: return F_FENEBONDS;
81                 case 8: return F_TABBONDS;
82                 case 9: return F_TABBONDSNC;
83                 case 10: return F_RESTRBONDS;
84                 default: gmx_fatal(FARGS, "Invalid bond type %d", type);
85             }
86         case Directive::d_angles:
87         case Directive::d_angletypes:
88             switch (type)
89             {
90                 case 1: return F_ANGLES;
91                 case 2: return F_G96ANGLES;
92                 case 3: return F_CROSS_BOND_BONDS;
93                 case 4: return F_CROSS_BOND_ANGLES;
94                 case 5: return F_UREY_BRADLEY;
95                 case 6: return F_QUARTIC_ANGLES;
96                 case 8: return F_TABANGLES;
97                 case 9: return F_LINEAR_ANGLES;
98                 case 10: return F_RESTRANGLES;
99                 default: gmx_fatal(FARGS, "Invalid angle type %d", type);
100             }
101         case Directive::d_pairs:
102         case Directive::d_pairtypes:
103             if (type == 1 || (d == Directive::d_pairtypes && type == 2))
104             {
105                 return F_LJ14;
106             }
107             else if (type == 2)
108             {
109                 return F_LJC14_Q;
110             }
111             else
112             {
113                 gmx_fatal(FARGS, "Invalid pairs type %d", type);
114             }
115         case Directive::d_pairs_nb: return F_LJC_PAIRS_NB;
116         case Directive::d_dihedrals:
117         case Directive::d_dihedraltypes:
118             switch (type)
119             {
120                 case 1: return F_PDIHS;
121                 case 2: return F_IDIHS;
122                 case 3: return F_RBDIHS;
123                 case 4: return F_PIDIHS;
124                 case 5: return F_FOURDIHS;
125                 case 8: return F_TABDIHS;
126                 case 9:
127                     return F_PDIHS; /* proper dihedrals where we allow multiple terms over single bond */
128                 case 10: return F_RESTRDIHS;
129                 case 11: return F_CBTDIHS;
130                 default: gmx_fatal(FARGS, "Invalid dihedral type %d", type);
131             }
132         case Directive::d_cmaptypes:
133         case Directive::d_cmap: return F_CMAP;
134
135         case Directive::d_nonbond_params:
136             if (type == 1)
137             {
138                 return F_LJ;
139             }
140             else
141             {
142                 return F_BHAM;
143             }
144         case Directive::d_vsites2:
145             switch (type)
146             {
147                 case 1: return F_VSITE2;
148                 case 2: return F_VSITE2FD;
149                 default: gmx_fatal(FARGS, "Invalid vsites2 type %d", type);
150             }
151         case Directive::d_vsites3:
152             switch (type)
153             {
154                 case 1: return F_VSITE3;
155                 case 2: return F_VSITE3FD;
156                 case 3: return F_VSITE3FAD;
157                 case 4: return F_VSITE3OUT;
158                 default: gmx_fatal(FARGS, "Invalid vsites3 type %d", type);
159             }
160         case Directive::d_vsites4:
161             switch (type)
162             {
163                 case 1: return F_VSITE4FD;
164                 case 2: return F_VSITE4FDN;
165                 default: gmx_fatal(FARGS, "Invalid vsites4 type %d", type);
166             }
167         case Directive::d_vsitesn: return F_VSITEN;
168         case Directive::d_constraints:
169         case Directive::d_constrainttypes:
170             switch (type)
171             {
172                 case 1: return F_CONSTR;
173                 case 2: return F_CONSTRNC;
174                 default: gmx_fatal(FARGS, "Invalid constraints type %d", type);
175             }
176         case Directive::d_settles: return F_SETTLE;
177         case Directive::d_position_restraints:
178             switch (type)
179             {
180                 case 1: return F_POSRES;
181                 case 2: return F_FBPOSRES;
182                 default: gmx_fatal(FARGS, "Invalid position restraint type %d", type);
183             }
184         case Directive::d_polarization:
185             switch (type)
186             {
187                 case 1: return F_POLARIZATION;
188                 case 2: return F_ANHARM_POL;
189                 default: gmx_fatal(FARGS, "Invalid polarization type %d", type);
190             }
191         case Directive::d_thole_polarization: return F_THOLE_POL;
192         case Directive::d_water_polarization: return F_WATER_POL;
193         case Directive::d_angle_restraints: return F_ANGRES;
194         case Directive::d_angle_restraints_z: return F_ANGRESZ;
195         case Directive::d_distance_restraints: return F_DISRES;
196         case Directive::d_orientation_restraints: return F_ORIRES;
197         case Directive::d_dihedral_restraints: return F_DIHRES;
198         default:
199             gmx_fatal(FARGS, "invalid directive %s in ifunc_index (%s:%d)", dir2str(d), __FILE__, __LINE__);
200     }
201 }
202
203 const char* dir2str(Directive d)
204 {
205     int index = static_cast<int>(d);
206     return directive_names[index];
207 }
208
209 Directive str2dir(char* dstr)
210 {
211     char buf[STRLEN], *ptr;
212
213     /* Hack to be able to read old topologies */
214     if (gmx_strncasecmp_min(dstr, "dummies", 7) == 0)
215     {
216         sprintf(buf, "virtual_sites%s", dstr + 7);
217         ptr = buf;
218     }
219     else
220     {
221         ptr = dstr;
222     }
223
224     for (auto d : gmx::EnumerationWrapper<Directive>())
225     {
226         if (gmx_strcasecmp_min(ptr, dir2str(static_cast<Directive>(d))) == 0)
227         {
228             return static_cast<Directive>(d);
229         }
230     }
231
232     return Directive::d_invalid;
233 }
234
235 static gmx::EnumerationArray<Directive, Directive*> necessary = { { nullptr } };
236
237 static void set_nec(Directive** n, ...)
238 /* Must always have at least one extra argument */
239 {
240     va_list   ap;
241     int       ind = 0;
242     Directive d;
243
244     va_start(ap, n);
245     do
246     {
247         d = static_cast<Directive>(va_arg(ap, int));
248         srenew(*n, ++ind);
249         (*n)[ind - 1] = d;
250     } while (d != Directive::d_none);
251     va_end(ap);
252 }
253
254 void DS_Init(DirStack** DS)
255 {
256     if (necessary[0] == nullptr)
257     {
258         set_nec(&(necessary[Directive::d_defaults]), Directive::d_none);
259         set_nec(&(necessary[Directive::d_atomtypes]), Directive::d_defaults, Directive::d_none);
260         set_nec(&(necessary[Directive::d_bondtypes]), Directive::d_atomtypes, Directive::d_none);
261         set_nec(&(necessary[Directive::d_constrainttypes]), Directive::d_atomtypes, Directive::d_none);
262         set_nec(&(necessary[Directive::d_pairtypes]), Directive::d_atomtypes, Directive::d_none);
263         set_nec(&(necessary[Directive::d_angletypes]), Directive::d_atomtypes, Directive::d_none);
264         set_nec(&(necessary[Directive::d_dihedraltypes]), Directive::d_atomtypes, Directive::d_none);
265         set_nec(&(necessary[Directive::d_nonbond_params]), Directive::d_atomtypes, Directive::d_none);
266         // Note that the content of the next two directives are
267         // ignored, but if grompp reads them in old force field files,
268         // it still needs to understand that they are in a valid place
269         // in the .top structure. It doesn't have to require them to
270         // be in the same place that was valid in old versions (ie. child
271         // directive of [atomtypes]) but any relevant case will
272         // satisfy that.
273         set_nec(&(necessary[Directive::d_implicit_genborn_params]), Directive::d_atomtypes,
274                 Directive::d_none);
275         set_nec(&(necessary[Directive::d_implicit_surface_params]), Directive::d_atomtypes,
276                 Directive::d_none);
277         set_nec(&(necessary[Directive::d_cmaptypes]), Directive::d_atomtypes, Directive::d_none);
278         set_nec(&(necessary[Directive::d_moleculetype]), Directive::d_atomtypes, Directive::d_none);
279         set_nec(&(necessary[Directive::d_atoms]), Directive::d_moleculetype, Directive::d_none);
280         set_nec(&(necessary[Directive::d_vsites2]), Directive::d_atoms, Directive::d_none);
281         set_nec(&(necessary[Directive::d_vsites3]), Directive::d_atoms, Directive::d_none);
282         set_nec(&(necessary[Directive::d_vsites4]), Directive::d_atoms, Directive::d_none);
283         set_nec(&(necessary[Directive::d_vsitesn]), Directive::d_atoms, Directive::d_none);
284         set_nec(&(necessary[Directive::d_bonds]), Directive::d_atoms, Directive::d_none);
285         set_nec(&(necessary[Directive::d_exclusions]), Directive::d_bonds, Directive::d_constraints,
286                 Directive::d_settles, Directive::d_none);
287         set_nec(&(necessary[Directive::d_pairs]), Directive::d_atoms, Directive::d_none);
288         set_nec(&(necessary[Directive::d_pairs_nb]), Directive::d_atoms, Directive::d_none);
289         set_nec(&(necessary[Directive::d_angles]), Directive::d_atoms, Directive::d_none);
290         set_nec(&(necessary[Directive::d_polarization]), Directive::d_atoms, Directive::d_none);
291         set_nec(&(necessary[Directive::d_water_polarization]), Directive::d_atoms, Directive::d_none);
292         set_nec(&(necessary[Directive::d_thole_polarization]), Directive::d_atoms, Directive::d_none);
293         set_nec(&(necessary[Directive::d_dihedrals]), Directive::d_atoms, Directive::d_none);
294         set_nec(&(necessary[Directive::d_constraints]), Directive::d_atoms, Directive::d_none);
295         set_nec(&(necessary[Directive::d_settles]), Directive::d_atoms, Directive::d_none);
296         set_nec(&(necessary[Directive::d_system]), Directive::d_moleculetype, Directive::d_none);
297         set_nec(&(necessary[Directive::d_molecules]), Directive::d_system, Directive::d_none);
298         set_nec(&(necessary[Directive::d_position_restraints]), Directive::d_atoms, Directive::d_none);
299         set_nec(&(necessary[Directive::d_angle_restraints]), Directive::d_atoms, Directive::d_none);
300         set_nec(&(necessary[Directive::d_angle_restraints_z]), Directive::d_atoms, Directive::d_none);
301         set_nec(&(necessary[Directive::d_distance_restraints]), Directive::d_atoms, Directive::d_none);
302         set_nec(&(necessary[Directive::d_orientation_restraints]), Directive::d_atoms, Directive::d_none);
303         set_nec(&(necessary[Directive::d_dihedral_restraints]), Directive::d_atoms, Directive::d_none);
304         set_nec(&(necessary[Directive::d_cmap]), Directive::d_atoms, Directive::d_none);
305         set_nec(&(necessary[Directive::d_intermolecular_interactions]), Directive::d_molecules,
306                 Directive::d_none);
307     }
308     *DS = nullptr;
309 }
310
311 void DS_Done(DirStack** DS)
312 {
313     DirStack* D;
314
315     while (*DS != nullptr)
316     {
317         D   = *DS;
318         *DS = (*DS)->prev;
319         sfree(D);
320     }
321 }
322
323 void DS_Push(DirStack** DS, Directive d)
324 {
325     DirStack* D;
326
327     snew(D, 1);
328     D->d    = d;
329     D->prev = *DS;
330     *DS     = D;
331 }
332
333 int DS_Search(DirStack* DS, Directive d)
334 {
335     DirStack* D;
336
337     D = DS;
338     while ((D != nullptr) && (D->d != d))
339     {
340         D = D->prev;
341     }
342
343     return static_cast<int>(D != nullptr);
344 }
345
346 int DS_Check_Order(DirStack* DS, Directive d)
347 {
348     Directive d0;
349     int       i = 0;
350
351     /* Check if parameter definitions appear after a moleculetype directive */
352     if (d < Directive::d_moleculetype && DS_Search(DS, Directive::d_moleculetype))
353     {
354         return FALSE;
355     }
356
357     /* Check if all the necessary directives have appeared before directive d */
358     if (necessary[d][0] == Directive::d_none)
359     {
360         return TRUE;
361     }
362     else
363     {
364         do
365         {
366             d0 = necessary[d][i++];
367             if (DS_Search(DS, d0))
368             {
369                 return TRUE;
370             }
371         } while (d0 != Directive::d_none);
372     }
373     return FALSE;
374 }