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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * 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 "gmx_fatal.h"
55 #include "thread_mpi.h"
58 /* NOTE: this was a cesspool of thread-unsafe code, has now been
59 properly proteced by mutexes (hopefully). */
61 /* XDR should be available on all platforms now,
62 * but we keep the possibility of turning it off...
67 #define IS_SET(fn) ((fn.flag & ffSET) != 0)
68 #define IS_OPT(fn) ((fn.flag & ffOPT) != 0)
69 #define IS_MULT(fn) ((fn.flag & ffMULT) != 0)
70 #define UN_SET(fn) (fn.flag = (fn.flag & ~ffSET))
71 #define DO_SET(fn) (fn.flag = (fn.flag | ffSET))
75 eftASC, eftBIN, eftXDR, eftGEN, eftNR
78 /* To support multiple file types with one general (eg TRX) we have
81 static const int trxs[] =
86 efTRJ, efGRO, efG96, efPDB, efG87 };
87 #define NTRXS asize(trxs)
89 static const int tros[] =
94 efTRJ, efGRO, efG96, efPDB, efG87 };
95 #define NTROS asize(tros)
97 static const int trns[] =
103 #define NTRNS asize(trns)
105 static const int stos[] =
106 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ };
107 #define NSTOS asize(stos)
109 static const int stxs[] =
110 { efGRO, efG96, efPDB, efBRK, efENT, efESP, efXYZ,
115 #define NSTXS asize(stxs)
117 static const int tpxs[] =
123 #define NTPXS asize(tpxs)
125 static const int tpss[] =
130 efTPB, efTPA, efGRO, efG96, efPDB, efBRK, efENT };
131 #define NTPSS asize(tpss)
144 /* this array should correspond to the enum in include/types/filenm.h */
145 static const t_deffile
148 { eftASC, ".mdp", "grompp", "-f", "grompp input file with MD parameters" },
149 { eftASC, ".gct", "gct", "-f", "General coupling stuff"},
150 { eftGEN, ".???", "traj", "-f",
151 "Trajectory: xtc trr trj gro g96 pdb cpt", NTRXS, trxs },
152 { eftGEN, ".???", "trajout", "-f",
153 "Trajectory: xtc trr trj gro g96 pdb", NTROS, tros },
154 { eftGEN, ".???", "traj", NULL,
155 "Full precision trajectory: trr trj cpt", NTRNS, trns },
156 { eftXDR, ".trr", "traj", NULL, "Trajectory in portable xdr format" },
157 { eftBIN, ".trj", "traj", NULL, "Trajectory file (architecture specific)" },
158 { eftXDR, ".xtc", "traj", NULL,
159 "Compressed trajectory (portable xdr format)" },
160 { eftASC, ".g87", "gtraj", NULL, "Gromos-87 ASCII trajectory format" },
161 { eftXDR, ".edr", "ener", NULL, "Energy file"},
162 { eftGEN, ".???", "conf", "-c", "Structure file: gro g96 pdb tpr etc.",
164 { eftGEN, ".???", "out", "-o", "Structure file: gro g96 pdb etc.",
166 { eftASC, ".gro", "conf", "-c", "Coordinate file in Gromos-87 format" },
167 { eftASC, ".g96", "conf", "-c", "Coordinate file in Gromos-96 format" },
168 { eftASC, ".pdb", "eiwit", "-f", "Protein data bank file"},
169 { eftASC, ".brk", "eiwit", "-f", "Brookhaven data bank file"},
170 { eftASC, ".ent", "eiwit", "-f", "Entry in the protein date bank" },
171 { eftASC, ".esp", "conf", "-f", "Coordinate file in Espresso format" },
172 { eftASC, ".pqr", "state", "-o", "Coordinate file for MEAD"},
173 { eftASC, ".xyz", "conf", "-o", "Coordinate file for some other programs" },
174 { eftXDR, ".cpt", "state", "-cp","Checkpoint file"},
175 { eftASC, ".log", "run", "-l", "Log file"},
176 { eftASC, ".xvg", "graph", "-o", "xvgr/xmgr file"},
177 { eftASC, ".out", "hello", "-o", "Generic output file"},
178 { eftASC, ".ndx", "index", "-n", "Index file",},
179 { eftASC, ".top", "topol", "-p", "Topology file"},
180 { eftASC, ".itp", "topinc", NULL, "Include file for topology"},
181 { eftGEN, ".???", "topol", "-s", "Run input file: tpr tpb tpa",
183 { eftGEN, ".???", "topol", "-s",
184 "Structure+mass(db): tpr tpb tpa gro g96 pdb", NTPSS, tpss },
185 { eftXDR, ".tpr", "topol", "-s", "Portable xdr run input file"},
186 { eftASC, ".tpa", "topol", "-s", "Ascii run input file"},
187 { eftBIN, ".tpb", "topol", "-s", "Binary run input file"},
188 { eftASC, ".tex", "doc", "-o", "LaTeX file"},
189 { eftASC, ".rtp", "residue", NULL, "Residue Type file used by pdb2gmx" },
190 { eftASC, ".atp", "atomtp", NULL, "Atomtype file used by pdb2gmx" },
191 { eftASC, ".hdb", "polar", NULL, "Hydrogen data base"},
192 { eftASC, ".dat", "nnnice", NULL, "Generic data file"},
193 { eftASC, ".dlg", "user", NULL, "Dialog Box data for ngmx"},
194 { eftASC, ".map", "ss", NULL, "File that maps matrix data to colors" },
195 { eftASC, ".eps", "plot", NULL, "Encapsulated PostScript (tm) file" },
196 { eftASC, ".mat", "ss", NULL, "Matrix Data file"},
197 { eftASC, ".m2p", "ps", NULL, "Input file for mat2ps"},
198 { eftXDR, ".mtx", "hessian","-m", "Hessian matrix"},
199 { eftASC, ".edi", "sam", NULL, "ED sampling input"},
200 { eftASC, ".edo", "sam", NULL, "ED sampling output"},
201 { eftASC, ".hat", "gk", NULL, "Fourier transform of spread function" },
202 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
203 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
204 { eftASC, "", "rundir", NULL, "Run directory" }
207 static char *default_file_name = NULL;
209 #ifdef GMX_THREAD_MPI
210 static tMPI_Thread_mutex_t filenm_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
214 const char *z_ext[NZEXT] =
217 void set_default_file_name(const char *name)
220 #ifdef GMX_THREAD_MPI
221 tMPI_Thread_mutex_lock(&filenm_mutex);
223 default_file_name = strdup(name);
224 #ifdef GMX_THREAD_MPI
225 tMPI_Thread_mutex_unlock(&filenm_mutex);
229 for(i=0; i<efNR; i++)
230 deffile[i].defnm = default_file_name;
234 const char *ftp2ext(int ftp)
236 if ((0 <= ftp) && (ftp < efNR))
237 return deffile[ftp].ext + 1;
242 const char *ftp2ext_generic(int ftp)
244 if ((0 <= ftp) && (ftp < efNR))
268 const char *ftp2desc(int ftp)
270 if ((0 <= ftp) && (ftp < efNR))
271 return deffile[ftp].descr;
273 return "unknown filetype";
276 const char *ftp2ftype(int ftp)
278 if ((ftp >= 0) && (ftp < efNR))
280 switch (deffile[ftp].ftype)
287 return "XDR portable";
291 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype",deffile[ftp].ftype);
298 const char *ftp2defnm(int ftp)
300 const char *buf = NULL;
302 #ifdef GMX_THREAD_MPI
303 tMPI_Thread_mutex_lock(&filenm_mutex);
306 if (default_file_name)
308 buf = default_file_name;
312 if ((0 <= ftp) && (ftp < efNR))
314 buf = deffile[ftp].defnm;
317 #ifdef GMX_THREAD_MPI
318 tMPI_Thread_mutex_unlock(&filenm_mutex);
324 void pr_def(FILE *fp, int ftp)
327 const char *s = NULL;
328 char *flst, *tmp, *desc;
332 df = &(deffile[ftp]);
333 defnm = ftp2defnm(ftp);
334 /* find default file extension and \tt-ify description */
335 /* FIXME: The constness should not be cast away */
337 desc = strdup(df->descr);
341 ext = deffile[df->tps[0]].ext;
342 tmp = strstr(desc, ": ") + 1;
347 snew(flst,strlen(tmp)+6);
348 strcpy(flst, " \\tt ");
361 /* set file contents type */
377 gmx_fatal(FARGS, "Unimplemented filetype %d %d",ftp,
380 fprintf(fp,"\\tt %8s & \\tt %3s & %3s & \\tt %2s & %s%s \\\\[-0.1ex]\n",
381 defnm, ext, s, df->defopt ? df->defopt : "",
382 check_tex(desc),check_tex(flst));
386 void pr_fns(FILE *fp, int nf, const t_filenm tfn[])
390 char buf[256], *wbuf, opt_buf[32];
393 fprintf(fp, "%6s %12s %-12s %s\n", "Option", "Filename", "Type",
396 "------------------------------------------------------------\n");
397 for (i = 0; (i < nf); i++)
399 for (f = 0; (f < tfn[i].nfiles); f++)
401 sprintf(buf, "%4s %14s %-12s ", (f == 0) ? tfn[i].opt : "",
402 tfn[i].fns[f], (f == 0) ? fileopt(tfn[i].flag, opt_buf, 32)
404 if (f < tfn[i].nfiles - 1)
405 fprintf(fp, "%s\n", buf);
407 if (tfn[i].nfiles > 0)
409 strcat(buf, deffile[tfn[i].ftp].descr);
410 if ((strlen(tfn[i].opt) > OPTLEN)
411 && (strlen(tfn[i].opt) <= ((OPTLEN + NAMELEN)
412 - strlen(tfn[i].fns[tfn[i].nfiles - 1]))))
414 for (j = strlen(tfn[i].opt); j < strlen(buf)
415 - (strlen(tfn[i].opt) - OPTLEN) + 1; j++)
416 buf[j] = buf[j + strlen(tfn[i].opt) - OPTLEN];
418 wbuf = wrap_lines(buf, 78, 35, FALSE);
419 fprintf(fp, "%s\n", wbuf);
427 void pr_fopts(FILE *fp, int nf, const t_filenm tfn[], int shell)
434 for (i = 0; (i < nf); i++)
436 fprintf(fp, " \"n/%s/f:*.", tfn[i].opt);
437 if (deffile[tfn[i].ftp].ntps)
440 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
444 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
450 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
452 for (j = 0; j < NZEXT; j++)
453 fprintf(fp, ",%s", z_ext[j]);
458 for (i = 0; (i < nf); i++)
460 fprintf(fp, "%s) COMPREPLY=( $(compgen -X '!*.", tfn[i].opt);
461 if (deffile[tfn[i].ftp].ntps)
464 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
468 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
474 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
476 for (j = 0; j < NZEXT; j++)
480 fprintf(fp, "%s", z_ext[j]);
482 fprintf(fp, ")' -f $c ; compgen -S '/' -X '.*' -d $c ));;\n");
486 for (i = 0; (i < nf); i++)
488 fprintf(fp, "- 'c[-1,%s]' -g '*.", tfn[i].opt);
489 if (deffile[tfn[i].ftp].ntps)
492 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
496 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
502 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
504 for (j = 0; j < NZEXT; j++)
505 fprintf(fp, "|%s", z_ext[j]);
506 fprintf(fp, ") *(/)' ");
512 static void check_opts(int nf, t_filenm fnm[])
517 for (i = 0; (i < nf); i++)
519 df = &(deffile[fnm[i].ftp]);
520 if (fnm[i].opt == NULL)
522 if (df->defopt == NULL)
524 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
525 deffile[fnm[i].ftp].ext,fnm[i].ftp);
529 fnm[i].opt=df->defopt;
535 int fn2ftp(const char *fn)
545 if ((len >= 4) && (fn[len - 4] == '.'))
546 feptr = &(fn[len - 4]);
550 for (i = 0; (i < efNR); i++)
551 if ((eptr = deffile[i].ext) != NULL)
552 if (gmx_strcasecmp(feptr, eptr) == 0)
558 static void set_extension(char *buf, int ftp)
563 /* check if extension is already at end of filename */
564 df = &(deffile[ftp]);
566 extlen = strlen(df->ext);
567 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
568 strcat(buf, df->ext);
571 static void add_filenm(t_filenm *fnm, const char *filenm)
573 srenew(fnm->fns, fnm->nfiles+1);
574 fnm->fns[fnm->nfiles] = strdup(filenm);
578 static void set_grpfnm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride)
580 char buf[256], buf2[256];
586 nopts = deffile[fnm->ftp].ntps;
587 ftps = deffile[fnm->ftp].tps;
588 if ((nopts == 0) || (ftps == NULL))
589 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
592 if (name && (bCanNotOverride || (default_file_name == NULL)))
595 /* First check whether we have a valid filename already */
597 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
599 /*if file exist don't add an extension for trajectory reading*/
600 bValidExt = gmx_fexist(name);
602 for(i=0; (i<nopts) && !bValidExt; i++)
611 /* No name given, set the default name */
612 strcpy(buf,ftp2defnm(fnm->ftp));
614 if (!bValidExt && (fnm->flag & ffREAD))
616 /* for input-files only: search for filenames in the directory */
617 for(i=0; (i<nopts) && !bValidExt; i++)
621 set_extension(buf2,type);
622 if (gmx_fexist(buf2))
632 /* Use the first extension type */
633 set_extension(buf,ftps[0]);
636 add_filenm(fnm, buf);
639 static void set_filenm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride,
642 /* Set the default filename, extension and option for those fields that
643 * are not already set. An extension is added if not present, if fn = NULL
644 * or empty, the default filename is given.
649 if ((fnm->flag & ffREAD) && !bReadNode)
654 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
655 gmx_fatal(FARGS, "file type out of range (%d)",fnm->ftp);
659 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
661 /* check if filename ends in .gz or .Z, if so remove that: */
663 for (i=0; i<NZEXT; i++)
665 extlen = strlen(z_ext[i]);
668 if (gmx_strcasecmp(name+len-extlen,z_ext[i]) == 0)
670 buf[len-extlen]='\0';
677 if (deffile[fnm->ftp].ntps)
679 set_grpfnm(fnm,name ? buf : NULL,bCanNotOverride);
683 if ((name == NULL) || !(bCanNotOverride || (default_file_name ==NULL)))
685 const char *defnm=ftp2defnm(fnm->ftp);
688 set_extension(buf,fnm->ftp);
690 add_filenm(fnm, buf);
694 static void set_filenms(int nf, t_filenm fnm[], gmx_bool bReadNode)
698 for (i = 0; (i < nf); i++)
700 set_filenm(&(fnm[i]), fnm[i].fn, FALSE, bReadNode);
703 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
704 gmx_bool bKeep, gmx_bool bReadNode)
711 for (i = 0; (i < nf); i++)
716 snew(bRemove,(*argc)+1);
720 for (j = 0; (j < nf); j++)
722 if (strcmp(argv[i], fnm[j].opt) == 0)
727 /* check if we are out of arguments for this option */
728 if ((i >= *argc) || (argv[i][0] == '-'))
729 set_filenm(&fnm[j], fnm[j].fn, FALSE, bReadNode);
730 /* sweep up all file arguments for this option */
731 while ((i < *argc) && (argv[i][0] != '-'))
733 set_filenm(&fnm[j], argv[i], TRUE, bReadNode);
736 /* only repeat for 'multiple' file options: */
737 if (!IS_MULT(fnm[j]))
741 break; /* jump out of 'j' loop */
744 /* No file found corresponding to option argv[i] */
751 /* Remove used entries */
752 for (i = j = 0; (i <= *argc); i++)
762 set_filenms(nf, fnm, bReadNode);
766 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
770 for (i = 0; (i < nfile); i++)
771 if (strcmp(opt, fnm[i].opt) == 0)
773 return fnm[i].fns[0];
776 fprintf(stderr, "No option %s\n", opt);
781 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
784 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
787 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
791 for (i = 0; (i < nfile); i++)
792 if (strcmp(opt, fnm[i].opt) == 0)
795 return fnm[i].nfiles;
798 fprintf(stderr, "No option %s\n", opt);
802 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
806 for (i = 0; (i < nfile); i++)
807 if (ftp == fnm[i].ftp)
808 return fnm[i].fns[0];
810 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
814 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
818 for (i = 0; (i < nfile); i++)
819 if (ftp == fnm[i].ftp)
822 return fnm[i].nfiles;
825 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
829 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
833 for (i = 0; (i < nfile); i++)
834 if (ftp == fnm[i].ftp)
835 return (gmx_bool) IS_SET(fnm[i]);
837 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
842 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
846 for (i = 0; (i < nfile); i++)
847 if (strcmp(opt, fnm[i].opt) == 0)
848 return (gmx_bool) IS_SET(fnm[i]);
850 fprintf(stderr, "No option %s\n", opt);
855 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
859 for (i = 0; (i < nfile); i++)
860 if (strcmp(opt, fnm[i].opt) == 0)
862 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
865 return fnm[i].fns[0];
867 fprintf(stderr, "No option %s\n", opt);
871 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
875 for (i = 0; (i < nfile); i++)
876 if (ftp == fnm[i].ftp)
878 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
881 return fnm[i].fns[0];
883 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
888 static void add_filters(char *filter,int *n,int nf,const int ftp[])
893 sprintf(filter,"*.{");
894 for(i=0; (i<nf); i++)
896 sprintf(buf,"%s",ftp2ext(ftp[i]));
905 char *ftp2filter(int ftp)
908 static char filter[128];
915 add_filters(filter,&n,NTRXS,trxs);
918 add_filters(filter,&n,NTRNS,trns);
921 add_filters(filter,&n,NSTOS,stos);
924 add_filters(filter,&n,NSTXS,stxs);
927 add_filters(filter,&n,NTPXS,tpxs);
930 sprintf(filter,"*%s",ftp2ext(ftp));
937 gmx_bool is_optional(const t_filenm *fnm)
939 return ((fnm->flag & ffOPT) == ffOPT);
942 gmx_bool is_output(const t_filenm *fnm)
944 return ((fnm->flag & ffWRITE) == ffWRITE);
947 gmx_bool is_set(const t_filenm *fnm)
949 return ((fnm->flag & ffSET) == ffSET);
952 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
955 char buf[STRLEN], newname[STRLEN];
958 for (i = 0; i < nfile; i++)
960 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
962 /* We never use multiple _outputs_, but we might as well check
963 for it, just in case... */
964 for (j = 0; j < fnm[i].nfiles; j++)
966 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
967 extpos = strrchr(buf, '.');
969 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
971 fnm[i].fns[j] = strdup(newname);
978 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
984 for (i = 0; i < nf; i++)
986 ret[i] = tfn[i]; /* just directly copy all non-string fields */
988 ret[i].opt = strdup(tfn[i].opt);
993 ret[i].fn = strdup(tfn[i].fn);
997 if (tfn[i].nfiles > 0)
999 snew(ret[i].fns,tfn[i].nfiles);
1000 for (j = 0; j < tfn[i].nfiles; j++)
1002 ret[i].fns[j] = strdup(tfn[i].fns[j]);
1009 void done_filenms(int nf, t_filenm fnm[])
1013 for (i = 0; i < nf; ++i)
1015 for (j = 0; j < fnm[i].nfiles; ++j)
1017 sfree(fnm[i].fns[j]);