Merge remote-tracking branch 'origin/release-4-6'
[alexxy/gromacs.git] / src / gromacs / 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 <assert.h>
43 #include "sysstuff.h"
44 #include "macros.h"
45 #include "string2.h"
46 #include "smalloc.h"
47 #include "pbc.h"
48 #include "statutil.h"
49 #include "names.h"
50 #include "vec.h"
51 #include "futil.h"
52 #include "wman.h"
53 #include "tpxio.h"
54 #include "gmx_fatal.h"
55 #include "network.h"
56 #include "vec.h"
57 #include "mtop_util.h"
58 #include "gmxfio.h"
59 #include "oenv.h"
60
61 #ifdef GMX_THREAD_MPI
62 #include "thread_mpi.h"
63 #endif
64
65 struct output_env
66 {
67     time_unit_t time_unit; /* the time unit, enum defined in oenv.h */
68     gmx_bool view;  /* view of file requested */
69     xvg_format_t xvg_format; /* xvg output format, enum defined in oenv.h */
70     int  verbosity; /* The level of verbosity for this program */
71     int debug_level; /* the debug level */
72
73     char *program_name; /* the program name */
74     char *cmd_line; /* the re-assembled command line */
75 };
76
77 /* The source code in this file should be thread-safe. 
78       Please keep it that way. */
79
80 /******************************************************************
81  *
82  *             T R A J E C T O R Y   S T U F F
83  *
84  ******************************************************************/
85
86 /* read only time names */
87 /* These must correspond to the time units type time_unit_t in oenv.h */
88 static const real timefactors[] =   { 0,  1e3,  1, 1e-3, 1e-6, 1e-9, 1e-12, 0 };
89 static const real timeinvfactors[] ={ 0, 1e-3,  1,  1e3,  1e6,  1e9,  1e12, 0 };
90 static const char *time_units_str[] = { NULL, "fs", "ps", "ns", "us", 
91                                         "\\mus", "ms", "s" };
92 static const char *time_units_xvgr[] = { NULL, "fs", "ps", "ns",  
93                                         "ms", "s", NULL };
94
95
96
97 /***** OUTPUT_ENV MEMBER FUNCTIONS ******/
98
99 void output_env_init(output_env_t *oenvp, int argc, char *argv[],
100                      time_unit_t tmu, gmx_bool view, xvg_format_t xvg_format,
101                      int verbosity, int debug_level)
102 {
103     int i;
104     int cmdlength=0;
105     char *argvzero=NULL;
106     output_env_t oenv;
107
108     snew(oenv, 1);
109     *oenvp = oenv;
110     oenv->time_unit  = tmu;
111     oenv->view=view;
112     oenv->xvg_format = xvg_format;
113     oenv->verbosity=verbosity;
114     oenv->debug_level=debug_level;
115     oenv->program_name=NULL;
116
117     if (argv)
118     {
119         argvzero=argv[0];
120         assert(argvzero);
121     }
122     /* set program name */
123     if (argvzero)
124     {
125         oenv->program_name=strdup(argvzero);
126     }
127     if (oenv->program_name == NULL)
128         oenv->program_name = strdup("GROMACS");
129    
130     /* copy command line */ 
131     if (argv) 
132     {
133         cmdlength = strlen(argvzero);
134         for (i=1; i<argc; i++) 
135         {
136             cmdlength += strlen(argv[i]);
137         }
138     }
139         
140     /* Fill the cmdline string */
141     snew(oenv->cmd_line,cmdlength+argc+1);
142     if (argv)
143     {
144         for (i=0; i<argc; i++)
145         {
146             strcat(oenv->cmd_line,argv[i]);
147             strcat(oenv->cmd_line," ");
148         }
149     }
150 }
151
152
153 void output_env_init_default(output_env_t *oenvp)
154 {
155     output_env_init(oenvp, 0, NULL, time_ps, FALSE, exvgNONE, 0, 0);
156 }
157
158
159 void output_env_done(output_env_t oenv)
160 {
161     sfree(oenv->program_name);
162     sfree(oenv->cmd_line);
163     sfree(oenv);
164 }
165
166
167
168 int output_env_get_verbosity(const output_env_t oenv)
169 {
170     return oenv->verbosity;
171 }
172
173 int output_env_get_debug_level(const output_env_t oenv)
174 {
175     return oenv->debug_level;
176 }
177
178
179 const char *output_env_get_time_unit(const output_env_t oenv)
180 {
181     return time_units_str[oenv->time_unit];
182 }
183
184 const char *output_env_get_time_label(const output_env_t oenv)
185 {
186     char *label;
187     snew(label, 20);
188     
189     sprintf(label,"Time (%s)",time_units_str[oenv->time_unit] ? 
190             time_units_str[oenv->time_unit]: "ps");
191     
192     return label;
193 }
194
195 const char *output_env_get_xvgr_tlabel(const output_env_t oenv)
196 {
197     char *label;
198     snew(label, 20);
199     
200     sprintf(label,"Time (%s)", time_units_xvgr[oenv->time_unit] ?
201             time_units_xvgr[oenv->time_unit] : "ps");
202     
203     return label;
204 }
205
206
207 real output_env_get_time_factor(const output_env_t oenv)
208 {
209     return timefactors[oenv->time_unit];
210 }
211
212 real output_env_get_time_invfactor(const output_env_t oenv)
213 {
214     return timeinvfactors[oenv->time_unit];
215 }
216
217 real output_env_conv_time(const output_env_t oenv, real time)
218 {
219     return time*timefactors[oenv->time_unit];
220 }
221
222
223 void output_env_conv_times(const output_env_t oenv, int n, real *time)
224 {
225     int i;
226     double fact=timefactors[oenv->time_unit];
227     
228     if (fact!=1.)
229         for(i=0; i<n; i++)
230             time[i] *= fact;
231 }
232
233 gmx_bool output_env_get_view(const output_env_t oenv)
234 {
235     return oenv->view;
236 }
237
238 xvg_format_t output_env_get_xvg_format(const output_env_t oenv)
239 {
240     return oenv->xvg_format;
241 }
242
243 const char *output_env_get_program_name(const output_env_t oenv)
244 {
245     return oenv->program_name;
246 }
247
248 const char *output_env_get_short_program_name(const output_env_t oenv)
249 {
250     const char *pr,*ret;
251     pr=ret=oenv->program_name; 
252     if ((pr=strrchr(ret,DIR_SEPARATOR)) != NULL)
253         ret=pr+1;
254     return ret;
255 }
256
257
258
259 const char *output_env_get_cmd_line(const output_env_t oenv)
260 {
261     return oenv->cmd_line;
262 }
263
264