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
49 #include "gromacs/fileio/tpxio.h"
50 #include "gmx_fatal.h"
52 #include "gromacs/fileio/gmxfio.h"
53 #include "gromacs/fileio/trxio.h"
55 #include "gromacs/onlinehelp/wman.h"
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/programinfo.h"
60 #include "thread_mpi/threads.h"
68 #include <sys/schedctl.h>
69 #include <sys/sysmp.h>
72 /* The source code in this file should be thread-safe.
73 Please keep it that way. */
75 /******************************************************************
77 * T R A J E C T O R Y S T U F F
79 ******************************************************************/
81 /****************************************************************
83 * E X P O R T E D F U N C T I O N S
85 ****************************************************************/
88 /* progam names, etc. */
90 const char *ShortProgram(void)
94 // TODO: Use the display name once it doesn't break anything.
95 return gmx::ProgramInfo::getInstance().programName().c_str();
97 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
100 const char *Program(void)
104 return gmx::ProgramInfo::getInstance().programNameWithPath().c_str();
106 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
109 /* utility functions */
111 gmx_bool bRmod_fd(double a, double b, double c, gmx_bool bDouble)
116 tol = 2*(bDouble ? GMX_DOUBLE_EPS : GMX_FLOAT_EPS);
118 iq = static_cast<int>((a - b + tol*a)/c);
120 if (std::fabs(a - b - c*iq) <= tol*std::fabs(a))
132 static void set_default_time_unit(const char *time_list[], gmx_bool bCanTime)
135 const char *select = NULL;
139 select = getenv("GMXTIMEUNIT");
143 while (time_list[i] && strcmp(time_list[i], select) != 0)
149 if (!bCanTime || select == NULL ||
150 time_list[i] == NULL || strcmp(time_list[i], select) != 0)
152 /* Set it to the default: ps */
154 while (time_list[i] && strcmp(time_list[i], "ps") != 0)
160 time_list[0] = time_list[i];
164 static void set_default_xvg_format(const char *xvg_list[])
169 select = getenv("GMX_VIEW_XVG");
172 /* The default is the first option */
173 xvg_list[0] = xvg_list[1];
178 while (xvg_list[i] && strcmp(xvg_list[i], select) != 0)
182 if (xvg_list[i] != NULL)
184 xvg_list[0] = xvg_list[i];
188 xvg_list[0] = xvg_list[exvgNONE];
194 /***** T O P O L O G Y S T U F F ******/
196 t_topology *read_top(const char *fn, int *ePBC)
202 epbc = read_tpx_top(fn, NULL, NULL, &natoms, NULL, NULL, NULL, top);
211 /*************************************************************
213 * P A R S I N G S T U F F
215 *************************************************************/
217 static void usage(const char *type, const char *arg)
219 GMX_ASSERT(arg != NULL, "NULL command-line argument should not occur");
220 gmx_fatal(FARGS, "Expected %s argument for option %s\n", type, arg);
223 int iscan(int argc, char *argv[], int *i)
225 const char *const arg = argv[*i];
228 usage("an integer", arg);
230 const char *const value = argv[++(*i)];
232 int var = std::strtol(value, &endptr, 10);
233 if (*value == '\0' || *endptr != '\0')
235 usage("an integer", arg);
240 gmx_large_int_t istepscan(int argc, char *argv[], int *i)
242 const char *const arg = argv[*i];
245 usage("an integer", arg);
247 const char *const value = argv[++(*i)];
249 gmx_large_int_t var = str_to_large_int_t(value, &endptr);
250 if (*value == '\0' || *endptr != '\0')
252 usage("an integer", arg);
257 double dscan(int argc, char *argv[], int *i)
259 const char *const arg = argv[*i];
262 usage("a real", arg);
264 const char *const value = argv[++(*i)];
266 double var = std::strtod(value, &endptr);
267 if (*value == '\0' || *endptr != '\0')
269 usage("a real", arg);
274 char *sscan(int argc, char *argv[], int *i)
278 if ( (argv[(*i)+1][0] == '-') && (argc > (*i)+2) &&
279 (argv[(*i)+2][0] != '-') )
281 fprintf(stderr, "Possible missing string argument for option %s\n\n",
287 usage("a string", argv[*i]);
293 int nenum(const char *const enumc[])
298 /* we *can* compare pointers directly here! */
299 while (enumc[i] && enumc[0] != enumc[i])
307 static void pdesc(char *desc)
312 if ((int)strlen(ptr) < 70)
314 fprintf(stderr, "\t%s\n", ptr);
318 for (nptr = ptr+70; (nptr != ptr) && (!std::isspace(*nptr)); nptr--)
324 fprintf(stderr, "\t%s\n", ptr);
330 fprintf(stderr, "\t%s\n", ptr);
336 static int add_parg(int npargs, t_pargs *pa, t_pargs *pa_add)
338 memcpy(&(pa[npargs]), pa_add, sizeof(*pa_add));
343 static char *mk_desc(t_pargs *pa, const char *time_unit_str)
345 char *newdesc = NULL, *ndesc = NULL, *nptr = NULL;
346 const char*ptr = NULL;
349 /* First compute length for description */
350 len = strlen(pa->desc)+1;
351 if ((ptr = strstr(pa->desc, "HIDDEN")) != NULL)
355 if (pa->type == etENUM)
358 for (k = 1; (pa->u.c[k] != NULL); k++)
360 len += strlen(pa->u.c[k])+12;
365 /* add label for hidden options */
368 sprintf(newdesc, "[hidden] %s", ptr+6);
372 strcpy(newdesc, pa->desc);
375 /* change '%t' into time_unit */
376 #define TUNITLABEL "%t"
377 #define NTUNIT strlen(TUNITLABEL)
378 if (pa->type == etTIME)
380 while ( (nptr = strstr(newdesc, TUNITLABEL)) != NULL)
384 len += strlen(time_unit_str)-NTUNIT;
386 strcpy(ndesc, newdesc);
387 strcat(ndesc, time_unit_str);
397 /* Add extra comment for enumerateds */
398 if (pa->type == etENUM)
400 strcat(newdesc, ": ");
401 for (k = 1; (pa->u.c[k] != NULL); k++)
403 strcat(newdesc, "[TT]");
404 strcat(newdesc, pa->u.c[k]);
405 strcat(newdesc, "[tt]");
406 /* Print a comma everywhere but at the last one */
407 if (pa->u.c[k+1] != NULL)
409 if (pa->u.c[k+2] == NULL)
411 strcat(newdesc, " or ");
415 strcat(newdesc, ", ");
424 gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags,
425 int nfile, t_filenm fnm[], int npargs, t_pargs *pa,
426 int ndesc, const char **desc,
427 int nbugs, const char **bugs,
430 const char *manstr[] = {
431 NULL, "no", "help", "html", "nroff", "completion", NULL
433 /* This array should match the order of the enum in oenv.h */
434 const char *xvg_format[] = { NULL, "xmgrace", "xmgr", "none", NULL };
435 /* This array should match the order of the enum in oenv.h */
436 const char *time_units[] = {
437 NULL, "fs", "ps", "ns", "us", "ms", "s",
440 int nicelevel = 0, debug_level = 0;
442 real tbegin = 0, tend = 0, tdelta = 0;
443 gmx_bool bView = FALSE;
445 t_pargs *all_pa = NULL;
450 "-npri", FALSE, etINT, {&npri},
451 "HIDDEN Set non blocking priority (try 128)"
455 "-nice", FALSE, etINT, {&nicelevel},
458 t_pargs deffnm_pa = {
459 "-deffnm", FALSE, etSTR, {&deffnm},
460 "Set the default filename for all file options"
463 "-b", FALSE, etTIME, {&tbegin},
464 "First frame (%t) to read from trajectory"
467 "-e", FALSE, etTIME, {&tend},
468 "Last frame (%t) to read from trajectory"
471 "-dt", FALSE, etTIME, {&tdelta},
472 "Only use frame when t MOD dt = first time (%t)"
475 "-w", FALSE, etBOOL, {&bView},
476 "View output [TT].xvg[tt], [TT].xpm[tt], [TT].eps[tt] and [TT].pdb[tt] files"
479 "-xvg", FALSE, etENUM, {xvg_format},
480 "xvg plot formatting"
483 "-tu", FALSE, etENUM, {time_units},
486 /* Maximum number of extra arguments */
490 { "-man", FALSE, etENUM, {manstr},
491 "HIDDENWrite manual and quit" },
492 { "-debug", FALSE, etINT, {&debug_level},
493 "HIDDENWrite file with debug information, 1: short, 2: also x and f" },
495 #define NPCA_PA asize(pca_pa)
496 gmx_bool bExit, bXvgr;
497 int i, j, k, npall, max_pa;
499 // Handle the flags argument, which is a bit field
500 // The FF macro returns whether or not the bit is set
501 #define FF(arg) ((Flags & arg) == arg)
503 /* Check for double arguments */
504 for (i = 1; (i < *argc); i++)
506 if (argv[i] && (strlen(argv[i]) > 1) && (!std::isdigit(argv[i][1])))
508 for (j = i+1; (j < *argc); j++)
510 if ( (argv[i][0] == '-') && (argv[j][0] == '-') &&
511 (strcmp(argv[i], argv[j]) == 0) )
513 if (FF(PCA_NOEXIT_ON_ARGS))
515 fprintf(stderr, "Double command line argument %s\n",
520 gmx_fatal(FARGS, "Double command line argument %s\n",
529 /* Check ALL the flags ... */
530 max_pa = NPCA_PA + EXTRA_PA + npargs+1;
531 snew(all_pa, max_pa);
533 for (i = npall = 0; (i < static_cast<int>(NPCA_PA)); i++)
535 npall = add_parg(npall, all_pa, &(pca_pa[i]));
539 const char *envstr = getenv("GMXNPRIALL");
542 npri = strtol(envstr, NULL, 10);
546 envstr = getenv("GMXNPRI");
549 npri = strtol(envstr, NULL, 10);
552 npall = add_parg(npall, all_pa, &npri_pa);
559 npall = add_parg(npall, all_pa, &nice_pa);
561 if (FF(PCA_CAN_SET_DEFFNM))
563 npall = add_parg(npall, all_pa, &deffnm_pa);
565 if (FF(PCA_CAN_BEGIN))
567 npall = add_parg(npall, all_pa, &begin_pa);
571 npall = add_parg(npall, all_pa, &end_pa);
575 npall = add_parg(npall, all_pa, &dt_pa);
577 if (FF(PCA_TIME_UNIT))
579 npall = add_parg(npall, all_pa, &time_pa);
581 if (FF(PCA_CAN_VIEW))
583 npall = add_parg(npall, all_pa, &view_pa);
587 for (i = 0; (i < nfile); i++)
589 bXvgr = bXvgr || (fnm[i].ftp == efXVG);
593 npall = add_parg(npall, all_pa, &xvg_pa);
596 /* Now append the program specific arguments */
597 for (i = 0; (i < npargs); i++)
599 npall = add_parg(npall, all_pa, &(pa[i]));
602 /* set etENUM options to default */
603 for (i = 0; (i < npall); i++)
605 if (all_pa[i].type == etENUM)
607 all_pa[i].u.c[0] = all_pa[i].u.c[1];
610 set_default_time_unit(time_units, FF(PCA_TIME_UNIT));
611 set_default_xvg_format(xvg_format);
613 /* Now parse all the command-line options */
614 get_pargs(argc, argv, npall, all_pa, FF(PCA_KEEP_ARGS));
616 /* set program name, command line, and default values for output options */
617 output_env_init(oenv, *argc, argv, (time_unit_t)nenum(time_units), bView,
618 (xvg_format_t)nenum(xvg_format), 0, debug_level);
620 if (FF(PCA_CAN_SET_DEFFNM) && (deffnm != NULL))
622 set_default_file_name(deffnm);
625 /* Parse the file args */
626 parse_file_args(argc, argv, nfile, fnm, FF(PCA_KEEP_ARGS), !FF(PCA_NOT_READ_NODE));
628 /* Open the debug file */
633 if (gmx_mpi_initialized())
635 sprintf(buf, "%s%d.debug", output_env_get_short_program_name(*oenv),
640 sprintf(buf, "%s.debug", output_env_get_short_program_name(*oenv));
643 init_debug(debug_level, buf);
644 fprintf(stderr, "Opening debug file %s (src code file %s, line %d)\n",
645 buf, __FILE__, __LINE__);
648 /* Now copy the results back... */
649 for (i = 0, k = npall-npargs; (i < npargs); i++, k++)
651 memcpy(&(pa[i]), &(all_pa[k]), (size_t)sizeof(pa[i]));
655 for (i = 0; (i < npall); i++)
657 all_pa[i].desc = mk_desc(&(all_pa[i]), output_env_get_time_unit(*oenv));
661 GMX_ASSERT(manstr[0] != NULL,
662 "Enum option default assignment should have changed this");
663 bExit = (strcmp(manstr[0], "no") != 0);
665 #if (defined __sgi && USE_SGI_FPE)
669 /* Set the nice level */
671 if (npri != 0 && !bExit)
673 schedctl(MPTS_RTPRI, 0, npri);
679 /* The some system, e.g. the catamount kernel on cray xt3 do not have nice(2). */
680 if (nicelevel != 0 && !bExit)
682 static gmx_bool nice_set = FALSE; /* only set it once */
683 static tMPI_Thread_mutex_t init_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
684 tMPI_Thread_mutex_lock(&init_mutex);
687 if (nice(nicelevel) == -1)
689 /* Do nothing, but use the return value to avoid warnings. */
693 tMPI_Thread_mutex_unlock(&init_mutex);
698 if (strcmp(manstr[0], "no") != 0 && !(FF(PCA_QUIET)))
700 if (!strcmp(manstr[0], "completion"))
702 /* one file each for csh, bash and zsh if we do completions */
703 write_man("completion-zsh", output_env_get_short_program_name(*oenv),
704 ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
705 write_man("completion-bash", output_env_get_short_program_name(*oenv),
706 ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
707 write_man("completion-csh", output_env_get_short_program_name(*oenv),
708 ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
712 write_man(manstr[0], output_env_get_short_program_name(*oenv),
713 ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
717 /* convert time options, must be done after printing! */
719 for (i = 0; i < npall; i++)
721 if ((all_pa[i].type == etTIME) && (*all_pa[i].u.r >= 0))
723 *all_pa[i].u.r *= output_env_get_time_invfactor(*oenv);
727 /* Extract Time info from arguments */
728 if (FF(PCA_CAN_BEGIN) && opt2parg_bSet("-b", npall, all_pa))
730 setTimeValue(TBEGIN, opt2parg_real("-b", npall, all_pa));
733 if (FF(PCA_CAN_END) && opt2parg_bSet("-e", npall, all_pa))
735 setTimeValue(TEND, opt2parg_real("-e", npall, all_pa));
738 if (FF(PCA_CAN_DT) && opt2parg_bSet("-dt", npall, all_pa))
740 setTimeValue(TDELTA, opt2parg_real("-dt", npall, all_pa));
744 for (i = 0; i < npall; ++i)
746 sfree((void *)all_pa[i].desc);
750 if (!FF(PCA_NOEXIT_ON_ARGS))