Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / gmxlib / oenv.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42
43 #include <ctype.h>
44 #include <assert.h>
45 #include "sysstuff.h"
46 #include "macros.h"
47 #include "string2.h"
48 #include "smalloc.h"
49 #include "pbc.h"
50 #include "statutil.h"
51 #include "names.h"
52 #include "vec.h"
53 #include "futil.h"
54 #include "wman.h"
55 #include "tpxio.h"
56 #include "gmx_fatal.h"
57 #include "network.h"
58 #include "vec.h"
59 #include "mtop_util.h"
60 #include "gmxfio.h"
61 #include "oenv.h"
62
63 #ifdef GMX_THREAD_MPI
64 #include "thread_mpi.h"
65 #endif
66
67
68
69 /* The source code in this file should be thread-safe. 
70       Please keep it that way. */
71
72 /******************************************************************
73  *
74  *             T R A J E C T O R Y   S T U F F
75  *
76  ******************************************************************/
77
78 /* read only time names */
79 /* These must correspond to the time units type time_unit_t in statutil.h */
80 static const real timefactors[] =   { 0,  1e3,  1, 1e-3, 1e-6, 1e-9, 1e-12, 0 };
81 static const real timeinvfactors[] ={ 0, 1e-3,  1,  1e3,  1e6,  1e9,  1e12, 0 };
82 static const char *time_units_str[] = { NULL, "fs", "ps", "ns", "us", 
83                                         "\\mus", "ms", "s" };
84 static const char *time_units_xvgr[] = { NULL, "fs", "ps", "ns",  
85                                         "ms", "s", NULL };
86
87
88
89 /***** OUTPUT_ENV MEMBER FUNCTIONS ******/
90
91 void output_env_init(output_env_t oenv,  int argc, char *argv[],
92                      time_unit_t tmu, gmx_bool view, xvg_format_t xvg_format,
93                      int verbosity, int debug_level)
94 {
95     int i;
96     int cmdlength=0;
97     char *argvzero=NULL, *extpos;
98
99     oenv->time_unit  = tmu;
100     oenv->view=view;
101     oenv->xvg_format = xvg_format;
102     oenv->verbosity=verbosity;
103     oenv->debug_level=debug_level;
104     oenv->program_name=NULL;
105
106     if (argv)
107     {
108         argvzero=argv[0];
109         assert(argvzero);
110     }
111     /* set program name */
112     if (argvzero)
113     {
114         /* if filename has file ending (e.g. .exe) then strip away */
115         extpos=strrchr(argvzero,'.');
116         if(extpos > strrchr(argvzero,DIR_SEPARATOR))
117         {
118             oenv->program_name=gmx_strndup(argvzero,extpos-argvzero);
119         }
120         else
121         {
122             oenv->program_name=gmx_strdup(argvzero);
123         }
124     }
125     if (oenv->program_name == NULL)
126     {
127         oenv->program_name = gmx_strdup("GROMACS");
128     }
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 oenv)
154 {
155     output_env_init(oenv, 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     /* Strip away the libtool prefix if it's still there. */
255     if(strlen(ret) > 3 && !strncmp(ret, "lt-", 3))
256         ret = ret + 3;
257     return ret;
258 }
259
260
261
262 const char *output_env_get_cmd_line(const output_env_t oenv)
263 {
264     return oenv->cmd_line;
265 }
266
267