Added QM input file name as command-line parameter for grompp
[alexxy/gromacs.git] / src / gromacs / fileio / filetypes.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,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,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 "gromacs/fileio/filetypes.h"
41
42 #include <cstring>
43
44 #include "gromacs/utility/arraysize.h"
45 #include "gromacs/utility/cstringutil.h"
46
47 enum
48 {
49     eftASC,
50     eftXDR,
51     eftTNG,
52     eftGEN,
53     eftNR
54 };
55
56 /* To support multiple file types with one general (eg TRX) we have
57  * these arrays.
58  */
59 static const int trxs[] = { efXTC, efTRR, efCPT, efGRO, efG96, efPDB, efTNG };
60 #define NTRXS asize(trxs)
61
62 static const int trcompressed[] = { efXTC, efTNG };
63 #define NTRCOMPRESSED asize(trcompressed)
64
65 static const int tros[] = { efXTC, efTRR, efGRO, efG96, efPDB, efTNG };
66 #define NTROS asize(tros)
67
68 static const int trns[] = { efTRR, efCPT, efTNG };
69 #define NTRNS asize(trns)
70
71 static const int stos[] = { efGRO, efG96, efPDB, efBRK, efENT, efESP };
72 #define NSTOS asize(stos)
73
74 static const int stxs[] = { efGRO, efG96, efPDB, efBRK, efENT, efESP, efTPR };
75 #define NSTXS asize(stxs)
76
77 static const int tpss[] = { efTPR, efGRO, efG96, efPDB, efBRK, efENT };
78 #define NTPSS asize(tpss)
79
80 typedef struct // NOLINT(clang-analyzer-optin.performance.Padding)
81 {
82     int         ftype;
83     const char* ext;
84     const char* defnm;
85     const char* defopt;
86     const char* descr;
87     int         ntps;
88     const int*  tps;
89 } t_deffile;
90
91 /* this array should correspond to the enum in filetypes.h */
92 static const t_deffile deffile[efNR] = {
93     { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
94     { eftGEN, ".???", "traj", "-f", "Trajectory", NTRXS, trxs },
95     { eftGEN, ".???", "trajout", "-f", "Trajectory", NTROS, tros },
96     { eftGEN, ".???", "traj", nullptr, "Full precision trajectory", NTRNS, trns },
97     { eftXDR, ".trr", "traj", nullptr, "Trajectory in portable xdr format" },
98     { eftGEN,
99       ".???",
100       "traj_comp",
101       nullptr,
102       "Compressed trajectory (tng format or portable xdr format)",
103       NTRCOMPRESSED,
104       trcompressed },
105     { eftXDR, ".xtc", "traj", nullptr, "Compressed trajectory (portable xdr format): xtc" },
106     { eftTNG, ".tng", "traj", nullptr, "Trajectory file (tng format)" },
107     { eftXDR, ".edr", "ener", nullptr, "Energy file" },
108     { eftGEN, ".???", "conf", "-c", "Structure file", NSTXS, stxs },
109     { eftGEN, ".???", "out", "-o", "Structure file", NSTOS, stos },
110     { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
111     { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
112     { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file" },
113     { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file" },
114     { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
115     { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
116     { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD" },
117     { eftXDR, ".cpt", "state", "-cp", "Checkpoint file" },
118     { eftASC, ".log", "run", "-l", "Log file" },
119     { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file" },
120     { eftASC, ".out", "hello", "-o", "Generic output file" },
121     {
122             eftASC,
123             ".ndx",
124             "index",
125             "-n",
126             "Index file",
127     },
128     { eftASC, ".top", "topol", "-p", "Topology file" },
129     { eftASC, ".itp", "topinc", nullptr, "Include file for topology" },
130     { eftGEN, ".???", "topol", "-s", "Structure+mass(db)", NTPSS, tpss },
131     { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file" },
132     { eftASC, ".tex", "doc", "-o", "LaTeX file" },
133     { eftASC, ".rtp", "residue", nullptr, "Residue Type file used by pdb2gmx" },
134     { eftASC, ".atp", "atomtp", nullptr, "Atomtype file used by pdb2gmx" },
135     { eftASC, ".hdb", "polar", nullptr, "Hydrogen data base" },
136     { eftASC, ".dat", "nnnice", nullptr, "Generic data file" },
137     { eftASC, ".dlg", "user", nullptr, "Dialog Box data for ngmx" },
138     { eftASC, ".map", "ss", nullptr, "File that maps matrix data to colors" },
139     { eftASC, ".eps", "plot", nullptr, "Encapsulated PostScript (tm) file" },
140     { eftASC, ".mat", "ss", nullptr, "Matrix Data file" },
141     { eftASC, ".m2p", "ps", nullptr, "Input file for mat2ps" },
142     { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix" },
143     { eftASC, ".edi", "sam", nullptr, "ED sampling input" },
144     { eftASC, ".cub", "pot", nullptr, "Gaussian cube file" },
145     { eftASC, ".xpm", "root", nullptr, "X PixMap compatible matrix file" },
146     { eftASC, "", "rundir", nullptr, "Run directory" },
147     { eftASC, ".csv", "bench", nullptr, "CSV data file" },
148     { eftASC, ".inp", "topol-qmmm", nullptr, "Input file for QM program" }
149 };
150
151 const char* ftp2ext(int ftp)
152 {
153     if ((0 <= ftp) && (ftp < efNR))
154     {
155         return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
156     }
157     else
158     {
159         return "unknown";
160     }
161 }
162
163 const char* ftp2ext_generic(int ftp)
164 {
165     if ((0 <= ftp) && (ftp < efNR))
166     {
167         switch (ftp)
168         {
169             case efTRX: return "trx";
170             case efTRN: return "trn";
171             case efSTO: return "sto";
172             case efSTX: return "stx";
173             case efTPS: return "tps";
174             default: return ftp2ext(ftp);
175         }
176     }
177     else
178     {
179         return "unknown";
180     }
181 }
182
183 const char* ftp2ext_with_dot(int ftp)
184 {
185     if ((0 <= ftp) && (ftp < efNR))
186     {
187         return deffile[ftp].ext;
188     }
189     else
190     {
191         return "unknown";
192     }
193 }
194
195 int ftp2generic_count(int ftp)
196 {
197     if ((0 <= ftp) && (ftp < efNR))
198     {
199         return deffile[ftp].ntps;
200     }
201     else
202     {
203         return 0;
204     }
205 }
206
207 const int* ftp2generic_list(int ftp)
208 {
209     if ((0 <= ftp) && (ftp < efNR))
210     {
211         return deffile[ftp].tps;
212     }
213     else
214     {
215         return nullptr;
216     }
217 }
218
219 const char* ftp2desc(int ftp)
220 {
221     if ((0 <= ftp) && (ftp < efNR))
222     {
223         return deffile[ftp].descr;
224     }
225     else
226     {
227         return "unknown filetype";
228     }
229 }
230
231 gmx_bool ftp_is_text(int ftp)
232 {
233     if ((ftp >= 0) && (ftp < efNR))
234     {
235         return deffile[ftp].ftype == eftASC;
236     }
237     return FALSE;
238 }
239
240 gmx_bool ftp_is_xdr(int ftp)
241 {
242     if ((ftp >= 0) && (ftp < efNR))
243     {
244         return deffile[ftp].ftype == eftXDR;
245     }
246     return FALSE;
247 }
248
249 const char* ftp2defnm(int ftp)
250 {
251     if ((0 <= ftp) && (ftp < efNR))
252     {
253         return deffile[ftp].defnm;
254     }
255     else
256     {
257         return nullptr;
258     }
259 }
260
261 const char* ftp2defopt(int ftp)
262 {
263     if ((0 <= ftp) && (ftp < efNR))
264     {
265         return deffile[ftp].defopt;
266     }
267     else
268     {
269         return nullptr;
270     }
271 }
272
273 int fn2ftp(const char* fn)
274 {
275     int         i, len;
276     const char* feptr;
277     const char* eptr;
278
279     if (!fn)
280     {
281         return efNR;
282     }
283
284     len = std::strlen(fn);
285     if ((len >= 4) && (fn[len - 4] == '.'))
286     {
287         feptr = &(fn[len - 4]);
288     }
289     else
290     {
291         return efNR;
292     }
293
294     for (i = 0; (i < efNR); i++)
295     {
296         if ((eptr = deffile[i].ext) != nullptr)
297         {
298             if (gmx_strcasecmp(feptr, eptr) == 0)
299             {
300                 break;
301             }
302         }
303     }
304
305     return i;
306 }