1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
55 #include "gmx_fatal.h"
58 #include "mtop_util.h"
62 #include "thread_mpi.h"
67 #include <sys/schedctl.h>
68 #include <sys/sysmp.h>
71 /* The source code in this file should be thread-safe.
72 Please keep it that way. */
74 /******************************************************************
76 * T R A J E C T O R Y S T U F F
78 ******************************************************************/
80 /* inherently globally shared names: */
81 static const char *program_name=NULL;
82 static char *cmd_line=NULL;
85 /* For now, some things here are simply not re-entrant, so
86 we have to actively lock them. */
87 static tMPI_Thread_mutex_t init_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
91 /****************************************************************
93 * E X P O R T E D F U N C T I O N S
95 ****************************************************************/
98 /* progam names, etc. */
100 const char *ShortProgram(void)
103 #ifdef GMX_THREAD_MPI
104 tMPI_Thread_mutex_lock(&init_mutex);
107 #ifdef GMX_THREAD_MPI
108 tMPI_Thread_mutex_unlock(&init_mutex);
110 if ((pr=strrchr(ret,DIR_SEPARATOR)) != NULL)
115 const char *Program(void)
118 #ifdef GMX_THREAD_MPI
119 tMPI_Thread_mutex_lock(&init_mutex);
122 #ifdef GMX_THREAD_MPI
123 tMPI_Thread_mutex_unlock(&init_mutex);
128 const char *command_line(void)
131 #ifdef GMX_THREAD_MPI
132 tMPI_Thread_mutex_lock(&init_mutex);
135 #ifdef GMX_THREAD_MPI
136 tMPI_Thread_mutex_unlock(&init_mutex);
141 void set_program_name(const char *argvzero)
143 #ifdef GMX_THREAD_MPI
144 tMPI_Thread_mutex_lock(&init_mutex);
146 if (program_name == NULL)
148 /* if filename has file ending (e.g. .exe) then strip away */
149 char* extpos=strrchr(argvzero,'.');
150 if(extpos > strrchr(argvzero,DIR_SEPARATOR))
152 program_name=gmx_strndup(argvzero,extpos-argvzero);
156 program_name=gmx_strdup(argvzero);
159 if (program_name == NULL)
160 program_name="GROMACS";
161 #ifdef GMX_THREAD_MPI
162 tMPI_Thread_mutex_unlock(&init_mutex);
167 void set_command_line(int argc, char *argv[])
172 #ifdef GMX_THREAD_MPI
173 tMPI_Thread_mutex_lock(&init_mutex);
177 cmdlength = strlen(argv[0]);
178 for (i=1; i<argc; i++)
180 cmdlength += strlen(argv[i]);
183 /* Fill the cmdline string */
184 snew(cmd_line,cmdlength+argc+1);
185 for (i=0; i<argc; i++)
187 strcat(cmd_line,argv[i]);
188 strcat(cmd_line," ");
191 #ifdef GMX_THREAD_MPI
192 tMPI_Thread_mutex_unlock(&init_mutex);
197 /* utility functions */
199 gmx_bool bRmod_fd(double a, double b, double c, gmx_bool bDouble)
204 tol = 2*(bDouble ? GMX_DOUBLE_EPS : GMX_FLOAT_EPS);
206 iq = (a - b + tol*a)/c;
208 if (fabs(a - b - c*iq) <= tol*fabs(a))
214 int check_times2(real t,real t0,real tp, real tpp, gmx_bool bDouble)
220 /* since t is float, we can not use double precision for bRmod */
224 if (t-tp>0 && tp-tpp>0)
225 margin = 0.1*min(t-tp,tp-tpp);
230 if ((!bTimeSet(TBEGIN) || (t >= rTimeValue(TBEGIN))) &&
231 (!bTimeSet(TEND) || (t <= rTimeValue(TEND)))) {
232 if (bTimeSet(TDELTA) && !bRmod_fd(t,t0,rTimeValue(TDELTA),bDouble))
237 else if (bTimeSet(TEND) && (t >= rTimeValue(TEND)))
240 fprintf(debug,"t=%g, t0=%g, b=%g, e=%g, dt=%g: r=%d\n",
241 t,t0,rTimeValue(TBEGIN),rTimeValue(TEND),rTimeValue(TDELTA),r);
245 int check_times(real t)
247 return check_times2(t,t,t,t,FALSE);
253 static void set_default_time_unit(const char *time_list[], gmx_bool bCanTime)
256 const char *select = NULL;
260 select = getenv("GMXTIMEUNIT");
264 while(time_list[i] && strcmp(time_list[i], select) != 0)
270 if (!bCanTime || select == NULL ||
271 time_list[i]==NULL || strcmp(time_list[i], select) != 0)
273 /* Set it to the default: ps */
275 while(time_list[i] && strcmp(time_list[i], "ps") != 0)
281 time_list[0] = time_list[i];
285 static void set_default_xvg_format(const char *xvg_list[])
288 const char *select,*tmp;
290 select = getenv("GMX_VIEW_XVG");
293 /* The default is the first option */
294 xvg_list[0] = xvg_list[1];
299 while (xvg_list[i] && strcmp(xvg_list[i], select) != 0)
303 if (xvg_list[i] != NULL)
305 xvg_list[0] = xvg_list[i];
309 xvg_list[0] = xvg_list[exvgNONE];
315 /***** T O P O L O G Y S T U F F ******/
317 t_topology *read_top(const char *fn,int *ePBC)
323 epbc = read_tpx_top(fn,NULL,NULL,&natoms,NULL,NULL,NULL,top);
330 /*************************************************************
332 * P A R S I N G S T U F F
334 *************************************************************/
336 static void usage(const char *type,const char *arg)
339 gmx_fatal(FARGS,"Expected %s argument for option %s\n",type,arg);
342 int iscan(int argc,char *argv[],int *i)
347 if (!sscanf(argv[++(*i)],"%d",&var))
348 usage("an integer",argv[(*i)-1]);
350 usage("an integer",argv[*i]);
355 gmx_large_int_t istepscan(int argc,char *argv[],int *i)
360 if (!sscanf(argv[++(*i)],gmx_large_int_pfmt,&var))
361 usage("an integer",argv[(*i)-1]);
363 usage("an integer",argv[*i]);
368 double dscan(int argc,char *argv[],int *i)
373 if (!sscanf(argv[++(*i)],"%lf",&var))
374 usage("a real",argv[(*i)-1]);
376 usage("a real",argv[*i]);
381 char *sscan(int argc,char *argv[],int *i)
385 if ( (argv[(*i)+1][0]=='-') && (argc > (*i)+2) &&
386 (argv[(*i)+2][0]!='-') )
388 fprintf(stderr,"Possible missing string argument for option %s\n\n",
393 usage("a string",argv[*i]);
398 int nenum(const char *const enumc[])
403 /* we *can* compare pointers directly here! */
404 while(enumc[i] && enumc[0]!=enumc[i])
410 static void pdesc(char *desc)
415 if ((int)strlen(ptr) < 70)
416 fprintf(stderr,"\t%s\n",ptr);
418 for(nptr=ptr+70; (nptr != ptr) && (!isspace(*nptr)); nptr--)
421 fprintf(stderr,"\t%s\n",ptr);
425 fprintf(stderr,"\t%s\n",ptr);
431 static FILE *man_file(const output_env_t oenv,const char *mantp)
435 const char *pr = output_env_get_short_program_name(oenv);
437 if (strcmp(mantp,"ascii") != 0)
438 sprintf(buf,"%s.%s",pr,mantp);
440 sprintf(buf,"%s.txt",pr);
441 fp = gmx_fio_fopen(buf,"w");
446 static int add_parg(int npargs,t_pargs *pa,t_pargs *pa_add)
448 memcpy(&(pa[npargs]),pa_add,sizeof(*pa_add));
453 static char *mk_desc(t_pargs *pa, const char *time_unit_str)
455 char *newdesc=NULL,*ndesc=NULL,*nptr=NULL;
459 /* First compute length for description */
460 len = strlen(pa->desc)+1;
461 if ((ptr = strstr(pa->desc,"HIDDEN")) != NULL)
463 if (pa->type == etENUM) {
465 for(k=1; (pa->u.c[k] != NULL); k++) {
466 len += strlen(pa->u.c[k])+12;
471 /* add label for hidden options */
473 sprintf(newdesc,"[hidden] %s",ptr+6);
475 strcpy(newdesc,pa->desc);
477 /* change '%t' into time_unit */
478 #define TUNITLABEL "%t"
479 #define NTUNIT strlen(TUNITLABEL)
480 if (pa->type == etTIME)
481 while( (nptr=strstr(newdesc,TUNITLABEL)) != NULL ) {
484 len+=strlen(time_unit_str)-NTUNIT;
486 strcpy(ndesc,newdesc);
487 strcat(ndesc,time_unit_str);
496 /* Add extra comment for enumerateds */
497 if (pa->type == etENUM) {
498 strcat(newdesc,": ");
499 for(k=1; (pa->u.c[k] != NULL); k++) {
500 strcat(newdesc,"[TT]");
501 strcat(newdesc,pa->u.c[k]);
502 strcat(newdesc,"[tt]");
503 /* Print a comma everywhere but at the last one */
504 if (pa->u.c[k+1] != NULL) {
505 if (pa->u.c[k+2] == NULL)
506 strcat(newdesc," or ");
508 strcat(newdesc,", ");
516 void parse_common_args(int *argc,char *argv[],unsigned long Flags,
517 int nfile,t_filenm fnm[],int npargs,t_pargs *pa,
518 int ndesc,const char **desc,
519 int nbugs,const char **bugs,
522 gmx_bool bHelp=FALSE,bHidden=FALSE,bQuiet=FALSE,bVersion=FALSE;
523 const char *manstr[] = { NULL, "no", "html", "tex", "nroff", "ascii",
524 "completion", "py", "xml", "wiki", NULL };
525 /* This array should match the order of the enum in oenv.h */
526 const char *xvg_format[] = { NULL, "xmgrace", "xmgr", "none", NULL };
527 /* This array should match the order of the enum in oenv.h */
528 const char *time_units[] = { NULL, "fs", "ps", "ns", "us", "ms", "s",
530 int nicelevel=0,mantp=0,npri=0,debug_level=0,verbose_level=0;
532 real tbegin=0,tend=0,tdelta=0;
533 gmx_bool bView=FALSE;
535 t_pargs *all_pa=NULL;
537 t_pargs npri_pa = { "-npri", FALSE, etINT, {&npri},
538 "HIDDEN Set non blocking priority (try 128)" };
539 t_pargs nice_pa = { "-nice", FALSE, etINT, {&nicelevel},
540 "Set the nicelevel" };
541 t_pargs deffnm_pa = { "-deffnm", FALSE, etSTR, {&deffnm},
542 "Set the default filename for all file options" };
543 t_pargs begin_pa = { "-b", FALSE, etTIME, {&tbegin},
544 "First frame (%t) to read from trajectory" };
545 t_pargs end_pa = { "-e", FALSE, etTIME, {&tend},
546 "Last frame (%t) to read from trajectory" };
547 t_pargs dt_pa = { "-dt", FALSE, etTIME, {&tdelta},
548 "Only use frame when t MOD dt = first time (%t)" };
549 t_pargs view_pa = { "-w", FALSE, etBOOL, {&bView},
550 "View output [TT].xvg[tt], [TT].xpm[tt], [TT].eps[tt] and [TT].pdb[tt] files" };
551 t_pargs xvg_pa = { "-xvg", FALSE, etENUM, {xvg_format},
552 "xvg plot formatting" };
553 t_pargs time_pa = { "-tu", FALSE, etENUM, {time_units},
555 /* Maximum number of extra arguments */
559 { "-h", FALSE, etBOOL, {&bHelp},
560 "Print help info and quit" },
561 { "-version", FALSE, etBOOL, {&bVersion},
562 "Print version info and quit" },
563 { "-verb", FALSE, etINT, {&verbose_level},
564 "HIDDENLevel of verbosity for this program" },
565 { "-hidden", FALSE, etBOOL, {&bHidden},
566 "HIDDENPrint hidden options" },
567 { "-quiet",FALSE, etBOOL, {&bQuiet},
568 "HIDDENDo not print help info" },
569 { "-man", FALSE, etENUM, {manstr},
570 "HIDDENWrite manual and quit" },
571 { "-debug",FALSE, etINT, {&debug_level},
572 "HIDDENWrite file with debug information, 1: short, 2: also x and f" },
574 #define NPCA_PA asize(pca_pa)
576 gmx_bool bPrint,bExit,bXvgr;
577 int i,j,k,npall,max_pa,cmdlength;
581 #define FF(arg) ((Flags & arg)==arg)
585 cmdlength = strlen(argv[0]);
586 /* Check for double arguments */
587 for (i=1; (i<*argc); i++)
589 cmdlength += strlen(argv[i]);
590 if (argv[i] && (strlen(argv[i]) > 1) && (!isdigit(argv[i][1])))
592 for (j=i+1; (j<*argc); j++)
594 if ( (argv[i][0]=='-') && (argv[j][0]=='-') &&
595 (strcmp(argv[i],argv[j])==0) )
597 if (FF(PCA_NOEXIT_ON_ARGS))
598 fprintf(stderr,"Double command line argument %s\n",
601 gmx_fatal(FARGS,"Double command line argument %s\n",
608 set_program_name(argv[0]);
609 set_command_line(*argc, argv);
611 /* Handle the flags argument, which is a bit field
612 * The FF macro returns whether or not the bit is set
614 bPrint = !FF(PCA_SILENT);
616 /* Check ALL the flags ... */
617 max_pa = NPCA_PA + EXTRA_PA + npargs+1;
620 for(i=npall=0; (i<NPCA_PA); i++)
621 npall = add_parg(npall,all_pa,&(pca_pa[i]));
624 envstr = getenv("GMXNPRIALL");
626 npri=strtol(envstr,NULL,10);
627 if (FF(PCA_BE_NICE)) {
628 envstr = getenv("GMXNPRI");
630 npri=strtol(envstr,NULL,10);
632 npall = add_parg(npall,all_pa,&npri_pa);
637 npall = add_parg(npall,all_pa,&nice_pa);
639 if (FF(PCA_CAN_SET_DEFFNM))
640 npall = add_parg(npall,all_pa,&deffnm_pa);
641 if (FF(PCA_CAN_BEGIN))
642 npall = add_parg(npall,all_pa,&begin_pa);
644 npall = add_parg(npall,all_pa,&end_pa);
647 npall = add_parg(npall,all_pa,&dt_pa);
649 if (FF(PCA_TIME_UNIT)) {
650 npall = add_parg(npall,all_pa,&time_pa);
652 if (FF(PCA_CAN_VIEW))
653 npall = add_parg(npall,all_pa,&view_pa);
656 for(i=0; (i<nfile); i++)
658 bXvgr = bXvgr || (fnm[i].ftp == efXVG);
662 npall = add_parg(npall,all_pa,&xvg_pa);
665 /* Now append the program specific arguments */
666 for(i=0; (i<npargs); i++)
667 npall = add_parg(npall,all_pa,&(pa[i]));
669 /* set etENUM options to default */
670 for(i=0; (i<npall); i++)
672 if (all_pa[i].type==etENUM)
674 all_pa[i].u.c[0]=all_pa[i].u.c[1];
677 set_default_time_unit(time_units,FF(PCA_TIME_UNIT));
678 set_default_xvg_format(xvg_format);
680 /* Now parse all the command-line options */
681 get_pargs(argc,argv,npall,all_pa,FF(PCA_KEEP_ARGS));
683 /* set program name, command line, and default values for output options */
684 output_env_init(*oenv, *argc, argv, (time_unit_t)nenum(time_units), bView,
685 (xvg_format_t)nenum(xvg_format), verbose_level, debug_level);
688 printf("Program: %s\n",output_env_get_program_name(*oenv));
689 gmx_print_version_info(stdout);
693 if (FF(PCA_CAN_SET_DEFFNM) && (deffnm!=NULL))
694 set_default_file_name(deffnm);
696 /* Parse the file args */
697 parse_file_args(argc,argv,nfile,fnm,FF(PCA_KEEP_ARGS),!FF(PCA_NOT_READ_NODE));
699 /* Open the debug file */
700 if (debug_level > 0) {
703 if (gmx_mpi_initialized())
704 sprintf(buf,"%s%d.debug",output_env_get_short_program_name(*oenv),
707 sprintf(buf,"%s.debug",output_env_get_short_program_name(*oenv));
709 init_debug(debug_level,buf);
710 fprintf(stderr,"Opening debug file %s (src code file %s, line %d)\n",
711 buf,__FILE__,__LINE__);
714 /* Now copy the results back... */
715 for(i=0,k=npall-npargs; (i<npargs); i++,k++)
716 memcpy(&(pa[i]),&(all_pa[k]),(size_t)sizeof(pa[i]));
719 for(i=0; (i<npall); i++)
720 all_pa[i].desc = mk_desc(&(all_pa[i]), output_env_get_time_unit(*oenv));
722 bExit = bHelp || (strcmp(manstr[0],"no") != 0);
724 #if (defined __sgi && USE_SGI_FPE)
728 /* Set the nice level */
730 if (npri != 0 && !bExit) {
731 schedctl(MPTS_RTPRI,0,npri);
738 /* The some system, e.g. the catamount kernel on cray xt3 do not have nice(2). */
739 if (nicelevel != 0 && !bExit)
741 #ifdef GMX_THREAD_MPI
742 static gmx_bool nice_set=FALSE; /* only set it once */
743 tMPI_Thread_mutex_lock(&init_mutex);
747 i=nice(nicelevel); /* assign ret value to avoid warnings */
748 #ifdef GMX_THREAD_MPI
751 tMPI_Thread_mutex_unlock(&init_mutex);
757 /* Update oenv for parsed command line options settings. */
758 (*oenv)->xvg_format = (xvg_format_t)nenum(xvg_format);
759 (*oenv)->time_unit = (time_unit_t)nenum(time_units);
761 if (!(FF(PCA_QUIET) || bQuiet )) {
763 write_man(stderr,"help",output_env_get_program_name(*oenv),
764 ndesc,desc,nfile, fnm,npall,all_pa, nbugs,bugs,bHidden);
766 pr_fns(stderr,nfile,fnm);
767 print_pargs(stderr,npall,all_pa,FALSE);
771 if (strcmp(manstr[0],"no") != 0) {
772 if(!strcmp(manstr[0],"completion")) {
773 /* one file each for csh, bash and zsh if we do completions */
774 fp=man_file(*oenv,"completion-zsh");
776 write_man(fp,"completion-zsh",output_env_get_program_name(*oenv),
777 ndesc,desc,nfile, fnm, npall,all_pa,nbugs,bugs,bHidden);
779 fp=man_file(*oenv,"completion-bash");
780 write_man(fp,"completion-bash",output_env_get_program_name(*oenv),
781 ndesc,desc,nfile, fnm, npall,all_pa,nbugs,bugs,bHidden);
783 fp=man_file(*oenv,"completion-csh");
784 write_man(fp,"completion-csh",output_env_get_program_name(*oenv),
785 ndesc,desc,nfile, fnm, npall,all_pa,nbugs,bugs,bHidden);
788 fp=man_file(*oenv,manstr[0]);
789 write_man(fp,manstr[0],output_env_get_program_name(*oenv),
790 ndesc,desc,nfile,fnm, npall, all_pa,nbugs,bugs,bHidden);
795 /* convert time options, must be done after printing! */
797 for(i=0; i<npall; i++) {
798 if ((all_pa[i].type == etTIME) && (*all_pa[i].u.r >= 0)) {
799 *all_pa[i].u.r *= output_env_get_time_invfactor(*oenv);
803 /* Extract Time info from arguments */
804 if (FF(PCA_CAN_BEGIN) && opt2parg_bSet("-b",npall,all_pa))
805 setTimeValue(TBEGIN,opt2parg_real("-b",npall,all_pa));
807 if (FF(PCA_CAN_END) && opt2parg_bSet("-e",npall,all_pa))
808 setTimeValue(TEND,opt2parg_real("-e",npall,all_pa));
810 if (FF(PCA_CAN_DT) && opt2parg_bSet("-dt",npall,all_pa))
811 setTimeValue(TDELTA,opt2parg_real("-dt",npall,all_pa));
814 for (i = 0; i < npall; ++i)
815 sfree((void *)all_pa[i].desc);
818 if (!FF(PCA_NOEXIT_ON_ARGS)) {