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,2017,2018 by the GROMACS development team.
7 * Copyright (c) 2019,2020, 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.
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.
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.
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.
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.
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.
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"
53 /* Must correspond to the Directive enum in grompp_impl.h */
54 static gmx::EnumerationArray<Directive, const char*> directive_names = {
63 "implicit_genborn_params",
64 "implicit_surface_params",
66 /* All the directives above can not appear after moleculetype */
86 "position_restraints",
89 "distance_restraints",
90 "orientation_restraints",
91 "dihedral_restraints",
93 "intermolecular_interactions",
99 int ifunc_index(Directive d, int type)
103 case Directive::d_bondtypes:
104 case Directive::d_bonds:
107 case 1: return F_BONDS;
108 case 2: return F_G96BONDS;
109 case 3: return F_MORSE;
110 case 4: return F_CUBICBONDS;
111 case 5: return F_CONNBONDS;
112 case 6: return F_HARMONIC;
113 case 7: return F_FENEBONDS;
114 case 8: return F_TABBONDS;
115 case 9: return F_TABBONDSNC;
116 case 10: return F_RESTRBONDS;
117 default: gmx_fatal(FARGS, "Invalid bond type %d", type);
119 case Directive::d_angles:
120 case Directive::d_angletypes:
123 case 1: return F_ANGLES;
124 case 2: return F_G96ANGLES;
125 case 3: return F_CROSS_BOND_BONDS;
126 case 4: return F_CROSS_BOND_ANGLES;
127 case 5: return F_UREY_BRADLEY;
128 case 6: return F_QUARTIC_ANGLES;
129 case 8: return F_TABANGLES;
130 case 9: return F_LINEAR_ANGLES;
131 case 10: return F_RESTRANGLES;
132 default: gmx_fatal(FARGS, "Invalid angle type %d", type);
134 case Directive::d_pairs:
135 case Directive::d_pairtypes:
136 if (type == 1 || (d == Directive::d_pairtypes && type == 2))
146 gmx_fatal(FARGS, "Invalid pairs type %d", type);
148 case Directive::d_pairs_nb: return F_LJC_PAIRS_NB;
149 case Directive::d_dihedrals:
150 case Directive::d_dihedraltypes:
153 case 1: return F_PDIHS;
154 case 2: return F_IDIHS;
155 case 3: return F_RBDIHS;
156 case 4: return F_PIDIHS;
157 case 5: return F_FOURDIHS;
158 case 8: return F_TABDIHS;
160 return F_PDIHS; /* proper dihedrals where we allow multiple terms over single bond */
161 case 10: return F_RESTRDIHS;
162 case 11: return F_CBTDIHS;
163 default: gmx_fatal(FARGS, "Invalid dihedral type %d", type);
165 case Directive::d_cmaptypes:
166 case Directive::d_cmap: return F_CMAP;
168 case Directive::d_nonbond_params:
177 case Directive::d_vsites2:
180 case 1: return F_VSITE2;
181 case 2: return F_VSITE2FD;
182 default: gmx_fatal(FARGS, "Invalid vsites2 type %d", type);
184 case Directive::d_vsites3:
187 case 1: return F_VSITE3;
188 case 2: return F_VSITE3FD;
189 case 3: return F_VSITE3FAD;
190 case 4: return F_VSITE3OUT;
191 default: gmx_fatal(FARGS, "Invalid vsites3 type %d", type);
193 case Directive::d_vsites4:
196 case 1: return F_VSITE4FD;
197 case 2: return F_VSITE4FDN;
198 default: gmx_fatal(FARGS, "Invalid vsites4 type %d", type);
200 case Directive::d_vsitesn: return F_VSITEN;
201 case Directive::d_constraints:
202 case Directive::d_constrainttypes:
205 case 1: return F_CONSTR;
206 case 2: return F_CONSTRNC;
207 default: gmx_fatal(FARGS, "Invalid constraints type %d", type);
209 case Directive::d_settles: return F_SETTLE;
210 case Directive::d_position_restraints:
213 case 1: return F_POSRES;
214 case 2: return F_FBPOSRES;
215 default: gmx_fatal(FARGS, "Invalid position restraint type %d", type);
217 case Directive::d_polarization:
220 case 1: return F_POLARIZATION;
221 case 2: return F_ANHARM_POL;
222 default: gmx_fatal(FARGS, "Invalid polarization type %d", type);
224 case Directive::d_thole_polarization: return F_THOLE_POL;
225 case Directive::d_water_polarization: return F_WATER_POL;
226 case Directive::d_angle_restraints: return F_ANGRES;
227 case Directive::d_angle_restraints_z: return F_ANGRESZ;
228 case Directive::d_distance_restraints: return F_DISRES;
229 case Directive::d_orientation_restraints: return F_ORIRES;
230 case Directive::d_dihedral_restraints: return F_DIHRES;
232 gmx_fatal(FARGS, "invalid directive %s in ifunc_index (%s:%d)", dir2str(d), __FILE__, __LINE__);
236 const char* dir2str(Directive d)
238 int index = static_cast<int>(d);
239 return directive_names[index];
242 Directive str2dir(char* dstr)
244 char buf[STRLEN], *ptr;
246 /* Hack to be able to read old topologies */
247 if (gmx_strncasecmp_min(dstr, "dummies", 7) == 0)
249 sprintf(buf, "virtual_sites%s", dstr + 7);
257 for (auto d : gmx::EnumerationWrapper<Directive>())
259 if (gmx_strcasecmp_min(ptr, dir2str(static_cast<Directive>(d))) == 0)
261 return static_cast<Directive>(d);
265 return Directive::d_invalid;
268 static gmx::EnumerationArray<Directive, Directive*> necessary = { { nullptr } };
270 static void set_nec(Directive** n, ...)
271 /* Must always have at least one extra argument */
280 d = static_cast<Directive>(va_arg(ap, int));
283 } while (d != Directive::d_none);
287 void DS_Init(DirStack** DS)
289 if (necessary[0] == nullptr)
291 set_nec(&(necessary[Directive::d_defaults]), Directive::d_none);
292 set_nec(&(necessary[Directive::d_atomtypes]), Directive::d_defaults, Directive::d_none);
293 set_nec(&(necessary[Directive::d_bondtypes]), Directive::d_atomtypes, Directive::d_none);
294 set_nec(&(necessary[Directive::d_constrainttypes]), Directive::d_atomtypes, Directive::d_none);
295 set_nec(&(necessary[Directive::d_pairtypes]), Directive::d_atomtypes, Directive::d_none);
296 set_nec(&(necessary[Directive::d_angletypes]), Directive::d_atomtypes, Directive::d_none);
297 set_nec(&(necessary[Directive::d_dihedraltypes]), Directive::d_atomtypes, Directive::d_none);
298 set_nec(&(necessary[Directive::d_nonbond_params]), Directive::d_atomtypes, Directive::d_none);
299 // Note that the content of the next two directives are
300 // ignored, but if grompp reads them in old force field files,
301 // it still needs to understand that they are in a valid place
302 // in the .top structure. It doesn't have to require them to
303 // be in the same place that was valid in old versions (ie. child
304 // directive of [atomtypes]) but any relevant case will
306 set_nec(&(necessary[Directive::d_implicit_genborn_params]), Directive::d_atomtypes, Directive::d_none);
307 set_nec(&(necessary[Directive::d_implicit_surface_params]), Directive::d_atomtypes, Directive::d_none);
308 set_nec(&(necessary[Directive::d_cmaptypes]), Directive::d_atomtypes, Directive::d_none);
309 set_nec(&(necessary[Directive::d_moleculetype]), Directive::d_atomtypes, Directive::d_none);
310 set_nec(&(necessary[Directive::d_atoms]), Directive::d_moleculetype, Directive::d_none);
311 set_nec(&(necessary[Directive::d_vsites2]), Directive::d_atoms, Directive::d_none);
312 set_nec(&(necessary[Directive::d_vsites3]), Directive::d_atoms, Directive::d_none);
313 set_nec(&(necessary[Directive::d_vsites4]), Directive::d_atoms, Directive::d_none);
314 set_nec(&(necessary[Directive::d_vsitesn]), Directive::d_atoms, Directive::d_none);
315 set_nec(&(necessary[Directive::d_bonds]), Directive::d_atoms, Directive::d_none);
316 set_nec(&(necessary[Directive::d_exclusions]),
318 Directive::d_constraints,
319 Directive::d_settles,
321 set_nec(&(necessary[Directive::d_pairs]), Directive::d_atoms, Directive::d_none);
322 set_nec(&(necessary[Directive::d_pairs_nb]), Directive::d_atoms, Directive::d_none);
323 set_nec(&(necessary[Directive::d_angles]), Directive::d_atoms, Directive::d_none);
324 set_nec(&(necessary[Directive::d_polarization]), Directive::d_atoms, Directive::d_none);
325 set_nec(&(necessary[Directive::d_water_polarization]), Directive::d_atoms, Directive::d_none);
326 set_nec(&(necessary[Directive::d_thole_polarization]), Directive::d_atoms, Directive::d_none);
327 set_nec(&(necessary[Directive::d_dihedrals]), Directive::d_atoms, Directive::d_none);
328 set_nec(&(necessary[Directive::d_constraints]), Directive::d_atoms, Directive::d_none);
329 set_nec(&(necessary[Directive::d_settles]), Directive::d_atoms, Directive::d_none);
330 set_nec(&(necessary[Directive::d_system]), Directive::d_moleculetype, Directive::d_none);
331 set_nec(&(necessary[Directive::d_molecules]), Directive::d_system, Directive::d_none);
332 set_nec(&(necessary[Directive::d_position_restraints]), Directive::d_atoms, Directive::d_none);
333 set_nec(&(necessary[Directive::d_angle_restraints]), Directive::d_atoms, Directive::d_none);
334 set_nec(&(necessary[Directive::d_angle_restraints_z]), Directive::d_atoms, Directive::d_none);
335 set_nec(&(necessary[Directive::d_distance_restraints]), Directive::d_atoms, Directive::d_none);
336 set_nec(&(necessary[Directive::d_orientation_restraints]), Directive::d_atoms, Directive::d_none);
337 set_nec(&(necessary[Directive::d_dihedral_restraints]), Directive::d_atoms, Directive::d_none);
338 set_nec(&(necessary[Directive::d_cmap]), Directive::d_atoms, Directive::d_none);
339 set_nec(&(necessary[Directive::d_intermolecular_interactions]),
340 Directive::d_molecules,
346 void DS_Done(DirStack** DS)
350 while (*DS != nullptr)
358 void DS_Push(DirStack** DS, Directive d)
368 int DS_Search(DirStack* DS, Directive d)
373 while ((D != nullptr) && (D->d != d))
378 return static_cast<int>(D != nullptr);
381 int DS_Check_Order(DirStack* DS, Directive d)
386 /* Check if parameter definitions appear after a moleculetype directive */
387 if (d < Directive::d_moleculetype && DS_Search(DS, Directive::d_moleculetype))
392 /* Check if all the necessary directives have appeared before directive d */
393 if (necessary[d][0] == Directive::d_none)
401 d0 = necessary[d][i++];
402 if (DS_Search(DS, d0))
406 } while (d0 != Directive::d_none);