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, ".hat", "gk", NULL, "Fourier transform of spread function" },
201 { eftASC, ".cub", "pot", NULL, "Gaussian cube file" },
202 { eftASC, ".xpm", "root", NULL, "X PixMap compatible matrix file" },
203 { eftASC, "", "rundir", NULL, "Run directory" }
206 static char *default_file_name = NULL;
208 #ifdef GMX_THREAD_MPI
209 static tMPI_Thread_mutex_t filenm_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
213 const char *z_ext[NZEXT] =
216 void set_default_file_name(const char *name)
219 #ifdef GMX_THREAD_MPI
220 tMPI_Thread_mutex_lock(&filenm_mutex);
222 default_file_name = strdup(name);
223 #ifdef GMX_THREAD_MPI
224 tMPI_Thread_mutex_unlock(&filenm_mutex);
228 for(i=0; i<efNR; i++)
229 deffile[i].defnm = default_file_name;
233 const char *ftp2ext(int ftp)
235 if ((0 <= ftp) && (ftp < efNR))
236 return deffile[ftp].ext + 1;
241 const char *ftp2ext_generic(int ftp)
243 if ((0 <= ftp) && (ftp < efNR))
267 const char *ftp2desc(int ftp)
269 if ((0 <= ftp) && (ftp < efNR))
270 return deffile[ftp].descr;
272 return "unknown filetype";
275 const char *ftp2ftype(int ftp)
277 if ((ftp >= 0) && (ftp < efNR))
279 switch (deffile[ftp].ftype)
286 return "XDR portable";
290 gmx_fatal(FARGS, "Unknown filetype %d in ftp2ftype",deffile[ftp].ftype);
297 const char *ftp2defnm(int ftp)
299 const char *buf = NULL;
301 #ifdef GMX_THREAD_MPI
302 tMPI_Thread_mutex_lock(&filenm_mutex);
305 if (default_file_name)
307 buf = default_file_name;
311 if ((0 <= ftp) && (ftp < efNR))
313 buf = deffile[ftp].defnm;
316 #ifdef GMX_THREAD_MPI
317 tMPI_Thread_mutex_unlock(&filenm_mutex);
323 void pr_def(FILE *fp, int ftp)
326 const char *s = NULL;
327 char *flst, *tmp, *desc;
331 df = &(deffile[ftp]);
332 defnm = ftp2defnm(ftp);
333 /* find default file extension and \tt-ify description */
334 /* FIXME: The constness should not be cast away */
336 desc = strdup(df->descr);
340 ext = deffile[df->tps[0]].ext;
341 tmp = strstr(desc, ": ") + 1;
346 snew(flst,strlen(tmp)+6);
347 strcpy(flst, " \\tt ");
360 /* set file contents type */
376 gmx_fatal(FARGS, "Unimplemented filetype %d %d",ftp,
379 fprintf(fp,"\\tt %8s & \\tt %3s & %3s & \\tt %2s & %s%s \\\\[-0.1ex]\n",
380 defnm, ext, s, df->defopt ? df->defopt : "",
381 check_tex(desc),check_tex(flst));
385 void pr_fns(FILE *fp, int nf, const t_filenm tfn[])
389 char buf[256], *wbuf, opt_buf[32];
392 fprintf(fp, "%6s %12s %-12s %s\n", "Option", "Filename", "Type",
395 "------------------------------------------------------------\n");
396 for (i = 0; (i < nf); i++)
398 for (f = 0; (f < tfn[i].nfiles); f++)
400 sprintf(buf, "%4s %14s %-12s ", (f == 0) ? tfn[i].opt : "",
401 tfn[i].fns[f], (f == 0) ? fileopt(tfn[i].flag, opt_buf, 32)
403 if (f < tfn[i].nfiles - 1)
404 fprintf(fp, "%s\n", buf);
406 if (tfn[i].nfiles > 0)
408 strcat(buf, deffile[tfn[i].ftp].descr);
409 if ((strlen(tfn[i].opt) > OPTLEN)
410 && (strlen(tfn[i].opt) <= ((OPTLEN + NAMELEN)
411 - strlen(tfn[i].fns[tfn[i].nfiles - 1]))))
413 for (j = strlen(tfn[i].opt); j < strlen(buf)
414 - (strlen(tfn[i].opt) - OPTLEN) + 1; j++)
415 buf[j] = buf[j + strlen(tfn[i].opt) - OPTLEN];
417 wbuf = wrap_lines(buf, 78, 35, FALSE);
418 fprintf(fp, "%s\n", wbuf);
426 void pr_fopts(FILE *fp, int nf, const t_filenm tfn[], int shell)
433 for (i = 0; (i < nf); i++)
435 fprintf(fp, " \"n/%s/f:*.", tfn[i].opt);
436 if (deffile[tfn[i].ftp].ntps)
439 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
443 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
449 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
451 for (j = 0; j < NZEXT; j++)
452 fprintf(fp, ",%s", z_ext[j]);
457 for (i = 0; (i < nf); i++)
459 fprintf(fp, "%s) COMPREPLY=( $(compgen -X '!*.", tfn[i].opt);
460 if (deffile[tfn[i].ftp].ntps)
463 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
467 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
473 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
475 for (j = 0; j < NZEXT; j++)
479 fprintf(fp, "%s", z_ext[j]);
481 fprintf(fp, ")' -f $c ; compgen -S '/' -X '.*' -d $c ));;\n");
485 for (i = 0; (i < nf); i++)
487 fprintf(fp, "- 'c[-1,%s]' -g '*.", tfn[i].opt);
488 if (deffile[tfn[i].ftp].ntps)
491 for (j = 0; j < deffile[tfn[i].ftp].ntps; j++)
495 fprintf(fp, "%s", deffile[deffile[tfn[i].ftp].tps[j]].ext
501 fprintf(fp, "%s", deffile[tfn[i].ftp].ext + 1);
503 for (j = 0; j < NZEXT; j++)
504 fprintf(fp, "|%s", z_ext[j]);
505 fprintf(fp, ") *(/)' ");
511 static void check_opts(int nf, t_filenm fnm[])
516 for (i = 0; (i < nf); i++)
518 df = &(deffile[fnm[i].ftp]);
519 if (fnm[i].opt == NULL)
521 if (df->defopt == NULL)
523 gmx_fatal(FARGS, "No default cmd-line option for %s (type %d)\n",
524 deffile[fnm[i].ftp].ext,fnm[i].ftp);
528 fnm[i].opt=df->defopt;
534 int fn2ftp(const char *fn)
544 if ((len >= 4) && (fn[len - 4] == '.'))
545 feptr = &(fn[len - 4]);
549 for (i = 0; (i < efNR); i++)
550 if ((eptr = deffile[i].ext) != NULL)
551 if (gmx_strcasecmp(feptr, eptr) == 0)
557 static void set_extension(char *buf, int ftp)
562 /* check if extension is already at end of filename */
563 df = &(deffile[ftp]);
565 extlen = strlen(df->ext);
566 if ((len <= extlen) || (gmx_strcasecmp(&(buf[len - extlen]), df->ext) != 0))
567 strcat(buf, df->ext);
570 static void add_filenm(t_filenm *fnm, const char *filenm)
572 srenew(fnm->fns, fnm->nfiles+1);
573 fnm->fns[fnm->nfiles] = strdup(filenm);
577 static void set_grpfnm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride)
579 char buf[256], buf2[256];
585 nopts = deffile[fnm->ftp].ntps;
586 ftps = deffile[fnm->ftp].tps;
587 if ((nopts == 0) || (ftps == NULL))
588 gmx_fatal(FARGS, "nopts == 0 || ftps == NULL");
591 if (name && (bCanNotOverride || (default_file_name == NULL)))
594 /* First check whether we have a valid filename already */
596 if ((fnm->flag & ffREAD) && (fnm->ftp == efTRX))
598 /*if file exist don't add an extension for trajectory reading*/
599 bValidExt = gmx_fexist(name);
601 for(i=0; (i<nopts) && !bValidExt; i++)
610 /* No name given, set the default name */
611 strcpy(buf,ftp2defnm(fnm->ftp));
613 if (!bValidExt && (fnm->flag & ffREAD))
615 /* for input-files only: search for filenames in the directory */
616 for(i=0; (i<nopts) && !bValidExt; i++)
620 set_extension(buf2,type);
621 if (gmx_fexist(buf2))
631 /* Use the first extension type */
632 set_extension(buf,ftps[0]);
635 add_filenm(fnm, buf);
638 static void set_filenm(t_filenm *fnm, const char *name, gmx_bool bCanNotOverride,
641 /* Set the default filename, extension and option for those fields that
642 * are not already set. An extension is added if not present, if fn = NULL
643 * or empty, the default filename is given.
648 if ((fnm->flag & ffREAD) && !bReadNode)
653 if ((fnm->ftp < 0) || (fnm->ftp >= efNR))
654 gmx_fatal(FARGS, "file type out of range (%d)",fnm->ftp);
658 if ((fnm->flag & ffREAD) && name && gmx_fexist(name))
660 /* check if filename ends in .gz or .Z, if so remove that: */
662 for (i=0; i<NZEXT; i++)
664 extlen = strlen(z_ext[i]);
667 if (gmx_strcasecmp(name+len-extlen,z_ext[i]) == 0)
669 buf[len-extlen]='\0';
676 if (deffile[fnm->ftp].ntps)
678 set_grpfnm(fnm,name ? buf : NULL,bCanNotOverride);
682 if ((name == NULL) || !(bCanNotOverride || (default_file_name ==NULL)))
684 const char *defnm=ftp2defnm(fnm->ftp);
687 set_extension(buf,fnm->ftp);
689 add_filenm(fnm, buf);
693 static void set_filenms(int nf, t_filenm fnm[], gmx_bool bReadNode)
697 for (i = 0; (i < nf); i++)
699 set_filenm(&(fnm[i]), fnm[i].fn, FALSE, bReadNode);
702 void parse_file_args(int *argc, char *argv[], int nf, t_filenm fnm[],
703 gmx_bool bKeep, gmx_bool bReadNode)
710 for (i = 0; (i < nf); i++)
715 snew(bRemove,(*argc)+1);
719 for (j = 0; (j < nf); j++)
721 if (strcmp(argv[i], fnm[j].opt) == 0)
726 /* check if we are out of arguments for this option */
727 if ((i >= *argc) || (argv[i][0] == '-'))
728 set_filenm(&fnm[j], fnm[j].fn, FALSE, bReadNode);
729 /* sweep up all file arguments for this option */
730 while ((i < *argc) && (argv[i][0] != '-'))
732 set_filenm(&fnm[j], argv[i], TRUE, bReadNode);
735 /* only repeat for 'multiple' file options: */
736 if (!IS_MULT(fnm[j]))
740 break; /* jump out of 'j' loop */
743 /* No file found corresponding to option argv[i] */
750 /* Remove used entries */
751 for (i = j = 0; (i <= *argc); i++)
761 set_filenms(nf, fnm, bReadNode);
765 const char *opt2fn(const char *opt, int nfile, const t_filenm fnm[])
769 for (i = 0; (i < nfile); i++)
770 if (strcmp(opt, fnm[i].opt) == 0)
772 return fnm[i].fns[0];
775 fprintf(stderr, "No option %s\n", opt);
780 const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
783 return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : NULL;
786 int opt2fns(char **fns[], const char *opt, int nfile, const t_filenm fnm[])
790 for (i = 0; (i < nfile); i++)
791 if (strcmp(opt, fnm[i].opt) == 0)
794 return fnm[i].nfiles;
797 fprintf(stderr, "No option %s\n", opt);
801 const char *ftp2fn(int ftp, int nfile, const t_filenm fnm[])
805 for (i = 0; (i < nfile); i++)
806 if (ftp == fnm[i].ftp)
807 return fnm[i].fns[0];
809 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
813 int ftp2fns(char **fns[], int ftp, int nfile, const t_filenm fnm[])
817 for (i = 0; (i < nfile); i++)
818 if (ftp == fnm[i].ftp)
821 return fnm[i].nfiles;
824 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
828 gmx_bool ftp2bSet(int ftp, int nfile, const t_filenm fnm[])
832 for (i = 0; (i < nfile); i++)
833 if (ftp == fnm[i].ftp)
834 return (gmx_bool) IS_SET(fnm[i]);
836 fprintf(stderr, "ftp2bSet: No filetype %s\n", deffile[ftp].ext);
841 gmx_bool opt2bSet(const char *opt, int nfile, const t_filenm fnm[])
845 for (i = 0; (i < nfile); i++)
846 if (strcmp(opt, fnm[i].opt) == 0)
847 return (gmx_bool) IS_SET(fnm[i]);
849 fprintf(stderr, "No option %s\n", opt);
854 const char *opt2fn_null(const char *opt, int nfile, const t_filenm fnm[])
858 for (i = 0; (i < nfile); i++)
859 if (strcmp(opt, fnm[i].opt) == 0)
861 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
864 return fnm[i].fns[0];
866 fprintf(stderr, "No option %s\n", opt);
870 const char *ftp2fn_null(int ftp, int nfile, const t_filenm fnm[])
874 for (i = 0; (i < nfile); i++)
875 if (ftp == fnm[i].ftp)
877 if (IS_OPT(fnm[i]) && !IS_SET(fnm[i]))
880 return fnm[i].fns[0];
882 fprintf(stderr, "ftp2fn: No filetype %s\n", deffile[ftp].ext);
887 static void add_filters(char *filter,int *n,int nf,const int ftp[])
892 sprintf(filter,"*.{");
893 for(i=0; (i<nf); i++)
895 sprintf(buf,"%s",ftp2ext(ftp[i]));
904 char *ftp2filter(int ftp)
907 static char filter[128];
914 add_filters(filter,&n,NTRXS,trxs);
917 add_filters(filter,&n,NTRNS,trns);
920 add_filters(filter,&n,NSTOS,stos);
923 add_filters(filter,&n,NSTXS,stxs);
926 add_filters(filter,&n,NTPXS,tpxs);
929 sprintf(filter,"*%s",ftp2ext(ftp));
936 gmx_bool is_optional(const t_filenm *fnm)
938 return ((fnm->flag & ffOPT) == ffOPT);
941 gmx_bool is_output(const t_filenm *fnm)
943 return ((fnm->flag & ffWRITE) == ffWRITE);
946 gmx_bool is_set(const t_filenm *fnm)
948 return ((fnm->flag & ffSET) == ffSET);
951 int add_suffix_to_output_names(t_filenm *fnm, int nfile, const char *suffix)
954 char buf[STRLEN], newname[STRLEN];
957 for (i = 0; i < nfile; i++)
959 if (is_output(&fnm[i]) && fnm[i].ftp != efCPT)
961 /* We never use multiple _outputs_, but we might as well check
962 for it, just in case... */
963 for (j = 0; j < fnm[i].nfiles; j++)
965 strncpy(buf, fnm[i].fns[j], STRLEN - 1);
966 extpos = strrchr(buf, '.');
968 sprintf(newname, "%s%s.%s", buf, suffix, extpos + 1);
970 fnm[i].fns[j] = strdup(newname);
977 t_filenm *dup_tfn(int nf, const t_filenm tfn[])
983 for (i = 0; i < nf; i++)
985 ret[i] = tfn[i]; /* just directly copy all non-string fields */
987 ret[i].opt = strdup(tfn[i].opt);
992 ret[i].fn = strdup(tfn[i].fn);
996 if (tfn[i].nfiles > 0)
998 snew(ret[i].fns,tfn[i].nfiles);
999 for (j = 0; j < tfn[i].nfiles; j++)
1001 ret[i].fns[j] = strdup(tfn[i].fns[j]);
1008 void done_filenms(int nf, t_filenm fnm[])
1012 for (i = 0; i < nf; ++i)
1014 for (j = 0; j < fnm[i].nfiles; ++j)
1016 sfree(fnm[i].fns[j]);