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, 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.
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.
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.
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.
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.
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.
48 #include "gmx_fatal.h"
54 #include "thread_mpi.h"
57 /* NOTE: this was a cesspool of thread-unsafe code, has now been
58 properly proteced by mutexes (hopefully). */
60 /* XDR should be available on all platforms now,
61 * but we keep the possibility of turning it off...
66 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
67 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
68 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
69 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
70 #define DO_SET(fn) (fn.flag = (fn.flag | ffSET))
74 eftASC, eftBIN, eftXDR, eftGEN, eftNR
77 /* To support multiple file types with one general (eg TRX) we have
80 static const int trxs[] =
85 efTRJ, efGRO, efG96, efPDB, efG87
87 #define NTRXS asize(trxs)
89 static const int tros[] =
94 efTRJ, efGRO, efG96, efPDB, efG87
96 #define NTROS asize(tros)
98 static const int trns[] =
105 #define NTRNS asize(trns)
107 static const int stos[] =
108 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ };
109 #define NSTOS asize(stos)
111 static const int stxs[] =
113 efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
119 #define NSTXS asize(stxs)
121 static const int tpxs[] =
128 #define NTPXS asize(tpxs)
130 static const int tpss[] =
135 efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT
137 #define NTPSS asize(tpss)
150 /* this array should correspond to the enum in filenm.h */
151 static const t_deffile
154 { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
155 { eftGEN, ".???", "traj", "-f",
156 "Trajectory: xtc trr trj gro g96 pdb cpt", NTRXS, trxs },
157 { eftGEN, ".???", "trajout", "-f",
158 "Trajectory: xtc trr trj gro g96 pdb", NTROS, tros },
159 { eftGEN, ".???", "traj", NULL,
160 "Full precision trajectory: trr trj cpt", NTRNS, trns },
161 { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
162 { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
163 { eftXDR, ".xtc", "traj", NULL,
164 "Compressed trajectory (portable xdr format)" },
165 { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
166 { eftXDR, ".edr", "ener", NULL, "Energy file"},
167 { eftGEN, ".???", "conf", "-c", "Structure file: gro g96 pdb tpr etc.",
169 { eftGEN, ".???", "out", "-o", "Structure file: gro g96 pdb etc.",
171 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
172 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
173 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
174 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
175 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
176 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
177 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
178 { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
179 { eftXDR, ".cpt", "state", "-cp", "Checkpoint file"},
180 { eftASC, ".log", "run", "-l", "Log file"},
181 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
182 { eftASC, ".out", "hello", "-o", "Generic output file"},
183 { eftASC, ".ndx", "index", "-n", "Index file", },
184 { eftASC, ".top", "topol", "-p", "Topology file"},
185 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
186 { eftGEN, ".???", "topol", "-s", "Run input file: tpr tpb tpa",
188 { eftGEN, ".???", "topol", "-s",
189 "Structure+mass(db): tpr tpb tpa gro g96 pdb", NTPSS, tpss },
190 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
191 { eftASC, ".tpa", "topol", "-s", "Ascii run input file"},
192 { eftBIN, ".tpb", "topol", "-s", "Binary run input file"},
193 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
194 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
195 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
196 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
197 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
198 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
199 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
200 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
201 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
202 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
203 { eftXDR, ".mtx", "hessian", "-m", "Hessian matrix"},
204 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
205 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
206 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
207 { eftASC, "", "rundir", NULL, "Run directory" }
210 static char *default_file_name = NULL;
212 #ifdef GMX_THREAD_MPI
213 static tMPI_Thread_mutex_t filenm_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
217 static const char *z_ext[NZEXT] =
220 void set_default_file_name(const char *name)
223 #ifdef GMX_THREAD_MPI
224 tMPI_Thread_mutex_lock(&filenm_mutex);
226 default_file_name = strdup(name);
227 #ifdef GMX_THREAD_MPI
228 tMPI_Thread_mutex_unlock(&filenm_mutex);
233 const char *ftp2ext(int ftp)
235 if ((0 <= ftp) && (ftp < efNR))
237 return deffile[ftp].ext[0] != '\0' ? deffile[ftp].ext + 1 : "";
245 const char *ftp2ext_generic(int ftp)
247 if ((0 <= ftp) && (ftp < efNR))
273 const char *ftp2ext_with_dot(int ftp)
275 if ((0 <= ftp) && (ftp < efNR))
277 return deffile[ftp].ext;
285 int ftp2generic_count(int ftp)
287 if ((0 <= ftp) && (ftp < efNR))
289 return deffile[ftp].ntps;
297 const int *ftp2generic_list(int ftp)
299 if ((0 <= ftp) && (ftp < efNR))
301 return deffile[ftp].tps;
309 const char *ftp2desc(int ftp)
311 if ((0 <= ftp) && (ftp < efNR))
313 return deffile[ftp].descr;
317 return "unknown filetype";
321 const char *ftp2ftype(int ftp)
323 if ((ftp >= 0) && (ftp < efNR))
325 switch (deffile[ftp].ftype)
332 return "XDR portable";
336 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype", deffile[ftp].ftype);
343 const char *ftp2defnm(int ftp)
345 const char *buf = NULL;
347 #ifdef GMX_THREAD_MPI
348 tMPI_Thread_mutex_lock(&filenm_mutex);
351 if (default_file_name)
353 buf = default_file_name;
357 if ((0 <= ftp) && (ftp < efNR))
359 buf = deffile[ftp].defnm;
362 #ifdef GMX_THREAD_MPI
363 tMPI_Thread_mutex_unlock(&filenm_mutex);
369 static void check_opts(int nf, t_filenm fnm[])
374 for (i = 0; (i < nf); i++)
376 df = &(deffile[fnm[i].ftp]);
377 if (fnm[i].opt == NULL)
379 if (df->defopt == NULL)
381 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
382 deffile[fnm[i].ftp].ext, fnm[i].ftp);
386 fnm[i].opt = df->defopt;
392 int fn2ftp(const char *fn)
404 if ((len >= 4) && (fn[len - 4] == '.'))
406 feptr = &(fn[len - 4]);
413 for (i = 0; (i < efNR); i++)
415 if ((eptr = deffile[i].ext) != NULL)
417 if (gmx_strcasecmp(feptr, eptr) == 0)
427 static void set_extension(char *buf, int ftp)
432 /* check if extension is already at end of filename */
433 df = &(deffile[ftp]);
435 extlen = strlen(df->ext);
436 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
438 strcat(buf, df->ext);
442 static void add_filenm(t_filenm *fnm, const char *filenm)
444 srenew(fnm->fns, fnm->nfiles+1);
445 fnm->fns[fnm->nfiles] = strdup(filenm);
449 static void set_grpfnm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride)
451 char buf[256], buf2[256];
457 nopts = deffile[fnm->ftp].ntps;
458 ftps = deffile[fnm->ftp].tps;
459 if ((nopts == 0) || (ftps == NULL))
461 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
465 if (name && (bCanNotOverride || (default_file_name == NULL)))
468 /* First check whether we have a valid filename already */
470 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
472 /*if file exist don't add an extension for trajectory reading*/
473 bValidExt = gmx_fexist(name);
475 for (i = 0; (i < nopts) && !bValidExt; i++)
485 /* No name given, set the default name */
486 strcpy(buf, ftp2defnm(fnm->ftp));
489 if (!bValidExt && (fnm->flag & ffREAD))
491 /* for input-files only: search for filenames in the directory */
492 for (i = 0; (i < nopts) && !bValidExt; i++)
496 set_extension(buf2, type);
497 if (gmx_fexist(buf2))
507 /* Use the first extension type */
508 set_extension(buf, ftps[0]);
511 add_filenm(fnm, buf);
514 static void set_filenm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride,
517 /* Set the default filename, extension and option for those fields that
518 * are not already set. An extension is added if not present, if fn = NULL
519 * or empty, the default filename is given.
524 if ((fnm->flag & ffREAD) && !bReadNode)
529 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
531 gmx_fatal(FARGS, "file type out of range (%d)", fnm->ftp);
538 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
540 /* check if filename ends in .gz or .Z, if so remove that: */
542 for (i = 0; i < NZEXT; i++)
544 extlen = strlen(z_ext[i]);
547 if (gmx_strcasecmp(name+len-extlen, z_ext[i]) == 0)
549 buf[len-extlen] = '\0';
556 if (deffile[fnm->ftp].ntps)
558 set_grpfnm(fnm, name ? buf : NULL, bCanNotOverride);
562 if ((name == NULL) || !(bCanNotOverride || (default_file_name == NULL)))
564 const char *defnm = ftp2defnm(fnm->ftp);
567 set_extension(buf, fnm->ftp);
569 add_filenm(fnm, buf);
573 static void set_filenms(int nf, t_filenm fnm[], gmx_bool bReadNode)
577 for (i = 0; (i < nf); i++)
581 set_filenm(&(fnm[i]), fnm[i].fn, FALSE, bReadNode);
586 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
587 gmx_bool bKeep, gmx_bool bReadNode)
594 for (i = 0; (i < nf); i++)
601 snew(bRemove, (*argc)+1);
605 for (j = 0; (j < nf); j++)
607 if (strcmp(argv[i], fnm[j].opt) == 0)
612 /* check if we are out of arguments for this option */
613 if ((i >= *argc) || (argv[i][0] == '-'))
615 set_filenm(&fnm[j], fnm[j].fn, FALSE, bReadNode);
617 /* sweep up all file arguments for this option */
618 while ((i < *argc) && (argv[i][0] != '-'))
620 set_filenm(&fnm[j], argv[i], TRUE, bReadNode);
623 /* only repeat for 'multiple' file options: */
624 if (!IS_MULT(fnm[j]))
630 break; /* jump out of 'j' loop */
633 /* No file found corresponding to option argv[i] */
643 /* Remove used entries */
644 for (i = j = 0; (i <= *argc); i++)
656 set_filenms(nf, fnm, bReadNode);
660 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
664 for (i = 0; (i < nfile); i++)
666 if (strcmp(opt, fnm[i].opt) == 0)
668 return fnm[i].fns[0];
672 fprintf(stderr, "No option %s\n", opt);
677 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
680 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
683 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
687 for (i = 0; (i < nfile); i++)
689 if (strcmp(opt, fnm[i].opt) == 0)
692 return fnm[i].nfiles;
696 fprintf(stderr, "No option %s\n", opt);
700 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
704 for (i = 0; (i < nfile); i++)
706 if (ftp == fnm[i].ftp)
708 return fnm[i].fns[0];
712 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
716 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
720 for (i = 0; (i < nfile); i++)
722 if (ftp == fnm[i].ftp)
725 return fnm[i].nfiles;
729 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
733 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
737 for (i = 0; (i < nfile); i++)
739 if (ftp == fnm[i].ftp)
741 return (gmx_bool) IS_SET(fnm[i]);
745 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
750 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
754 for (i = 0; (i < nfile); i++)
756 if (strcmp(opt, fnm[i].opt) == 0)
758 return (gmx_bool) IS_SET(fnm[i]);
762 fprintf(stderr, "No option %s\n", opt);
767 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
771 for (i = 0; (i < nfile); i++)
773 if (strcmp(opt, fnm[i].opt) == 0)
775 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
781 return fnm[i].fns[0];
785 fprintf(stderr, "No option %s\n", opt);
789 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
793 for (i = 0; (i < nfile); i++)
795 if (ftp == fnm[i].ftp)
797 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
803 return fnm[i].fns[0];
807 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
811 gmx_bool is_optional(const t_filenm *fnm)
813 return ((fnm->flag & ffOPT) == ffOPT);
816 gmx_bool is_output(const t_filenm *fnm)
818 return ((fnm->flag & ffWRITE) == ffWRITE);
821 gmx_bool is_set(const t_filenm *fnm)
823 return ((fnm->flag & ffSET) == ffSET);
826 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
829 char buf[STRLEN], newname[STRLEN];
832 for (i = 0; i < nfile; i++)
834 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
836 /* We never use multiple _outputs_, but we might as well check
837 for it, just in case... */
838 for (j = 0; j < fnm[i].nfiles; j++)
840 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
841 extpos = strrchr(buf, '.');
843 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
845 fnm[i].fns[j] = strdup(newname);
852 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
858 for (i = 0; i < nf; i++)
860 ret[i] = tfn[i]; /* just directly copy all non-string fields */
863 ret[i].opt = strdup(tfn[i].opt);
872 ret[i].fn = strdup(tfn[i].fn);
879 if (tfn[i].nfiles > 0)
881 snew(ret[i].fns, tfn[i].nfiles);
882 for (j = 0; j < tfn[i].nfiles; j++)
884 ret[i].fns[j] = strdup(tfn[i].fns[j]);
891 void done_filenms(int nf, t_filenm fnm[])
895 for (i = 0; i < nf; ++i)
897 for (j = 0; j < fnm[i].nfiles; ++j)
899 sfree(fnm[i].fns[j]);