renamed GMX_THREADS to GMX_THREAD_MPI
[alexxy/gromacs.git] / src / gmxlib / oenv.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
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.
15  
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.
20  * 
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.
27  * 
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.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * GROningen Mixture of Alchemy and Childrens' Stories
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40
41 #include <ctype.h>
42 #include "sysstuff.h"
43 #include "macros.h"
44 #include "string2.h"
45 #include "smalloc.h"
46 #include "pbc.h"
47 #include "statutil.h"
48 #include "names.h"
49 #include "vec.h"
50 #include "futil.h"
51 #include "wman.h"
52 #include "tpxio.h"
53 #include "gmx_fatal.h"
54 #include "network.h"
55 #include "vec.h"
56 #include "mtop_util.h"
57 #include "gmxfio.h"
58 #include "oenv.h"
59
60 #ifdef GMX_THREAD_MPI
61 #include "thread_mpi.h"
62 #endif
63
64
65
66 /* The source code in this file should be thread-safe. 
67       Please keep it that way. */
68
69 /******************************************************************
70  *
71  *             T R A J E C T O R Y   S T U F F
72  *
73  ******************************************************************/
74
75 /* read only time names */
76 /* These must correspond to the time units type time_unit_t in statutil.h */
77 static const real timefactors[] =   { 0,  1e3,  1, 1e-3, 1e-6, 1e-9, 1e-12, 0 };
78 static const real timeinvfactors[] ={ 0, 1e-3,  1,  1e3,  1e6,  1e9,  1e12, 0 };
79 static const char *time_units_str[] = { NULL, "fs", "ps", "ns", "us", 
80                                         "\\mus", "ms", "s" };
81 static const char *time_units_xvgr[] = { NULL, "fs", "ps", "ns",  
82                                         "ms", "s", NULL };
83
84
85
86 /***** OUTPUT_ENV MEMBER FUNCTIONS ******/
87
88 void output_env_init(output_env_t oenv,  int argc, char *argv[],
89                      time_unit_t tmu, gmx_bool view, xvg_format_t xvg_format,
90                      int verbosity, int debug_level)
91 {
92     int i;
93     int cmdlength=0;
94     char *argvzero=NULL;
95
96     oenv->time_unit  = tmu;
97     oenv->view=view;
98     oenv->xvg_format = xvg_format;
99     oenv->verbosity=verbosity;
100     oenv->debug_level=debug_level;
101     oenv->program_name=NULL;
102
103     if (argv)
104         argvzero=argv[0];
105
106     /* set program name */
107     /* When you run a dynamically linked program before installing
108      * it, libtool uses wrapper scripts and prefixes the name with "lt-".
109      * Until libtool is fixed to set argv[0] right, rip away the prefix:
110      */
111     if (argvzero)
112     {
113         if(strlen(argvzero)>3 && !strncmp(argvzero,"lt-",3))
114             oenv->program_name=strdup(argvzero+3);
115         else
116             oenv->program_name=strdup(argvzero);
117     }
118     if (oenv->program_name == NULL)
119         oenv->program_name = strdup("GROMACS");
120    
121     /* copy command line */ 
122     if (argv) 
123     {
124         cmdlength = strlen(argvzero);
125         for (i=1; i<argc; i++) 
126         {
127             cmdlength += strlen(argv[i]);
128         }
129     }
130         
131     /* Fill the cmdline string */
132     snew(oenv->cmd_line,cmdlength+argc+1);
133     for (i=0; i<argc; i++) 
134     {
135         strcat(oenv->cmd_line,argv[i]);
136         strcat(oenv->cmd_line," ");
137     }
138 }
139
140
141 void output_env_init_default(output_env_t oenv)
142 {
143     output_env_init(oenv, 0, NULL, time_ps, FALSE, exvgNONE, 0, 0);
144 }
145
146
147 void output_env_done(output_env_t oenv)
148 {
149     sfree(oenv->program_name);
150     sfree(oenv->cmd_line);
151     sfree(oenv);
152 }
153
154
155
156 int output_env_get_verbosity(const output_env_t oenv)
157 {
158     return oenv->verbosity;
159 }
160
161 int output_env_get_debug_level(const output_env_t oenv)
162 {
163     return oenv->debug_level;
164 }
165
166
167 const char *output_env_get_time_unit(const output_env_t oenv)
168 {
169     return time_units_str[oenv->time_unit];
170 }
171
172 const char *output_env_get_time_label(const output_env_t oenv)
173 {
174     char *label;
175     snew(label, 20);
176     
177     sprintf(label,"Time (%s)",time_units_str[oenv->time_unit] ? 
178             time_units_str[oenv->time_unit]: "ps");
179     
180     return label;
181 }
182
183 const char *output_env_get_xvgr_tlabel(const output_env_t oenv)
184 {
185     char *label;
186     snew(label, 20);
187     
188     sprintf(label,"Time (%s)", time_units_xvgr[oenv->time_unit] ?
189             time_units_xvgr[oenv->time_unit] : "ps");
190     
191     return label;
192 }
193
194
195 real output_env_get_time_factor(const output_env_t oenv)
196 {
197     return timefactors[oenv->time_unit];
198 }
199
200 real output_env_get_time_invfactor(const output_env_t oenv)
201 {
202     return timeinvfactors[oenv->time_unit];
203 }
204
205 real output_env_conv_time(const output_env_t oenv, real time)
206 {
207     return time*timefactors[oenv->time_unit];
208 }
209
210
211 void output_env_conv_times(const output_env_t oenv, int n, real *time)
212 {
213     int i;
214     double fact=timefactors[oenv->time_unit];
215     
216     if (fact!=1.)
217         for(i=0; i<n; i++)
218             time[i] *= fact;
219 }
220
221 gmx_bool output_env_get_view(const output_env_t oenv)
222 {
223     return oenv->view;
224 }
225
226 xvg_format_t output_env_get_xvg_format(const output_env_t oenv)
227 {
228     return oenv->xvg_format;
229 }
230
231 const char *output_env_get_program_name(const output_env_t oenv)
232 {
233     return oenv->program_name;
234 }
235
236 const char *output_env_get_short_program_name(const output_env_t oenv)
237 {
238     const char *pr,*ret;
239     pr=ret=oenv->program_name; 
240     if ((pr=strrchr(ret,DIR_SEPARATOR)) != NULL)
241         ret=pr+1;
242     /* Strip away the libtool prefix if it's still there. */
243     if(strlen(ret) > 3 && !strncmp(ret, "lt-", 3))
244         ret = ret + 3;
245     return ret;
246 }
247
248
249
250 const char *output_env_get_cmd_line(const output_env_t oenv)
251 {
252     return oenv->cmd_line;
253 }
254
255