Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / gmxlib / oenv.cpp
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 #include "oenv.h"
37
38 #include "smalloc.h"
39
40 #include "gromacs/utility/exceptions.h"
41 #include "gromacs/utility/programinfo.h"
42
43 struct output_env
44 {
45     output_env()
46     {
47         setDefaults();
48     }
49     output_env(int argc, const char *const argv[])
50         : programInfo(argc, argv)
51     {
52         setDefaults();
53     }
54
55     void setDefaults()
56     {
57         time_unit   = time_ps;
58         view        = FALSE;
59         xvg_format  = exvgNONE;
60         verbosity   = 0;
61         debug_level = 0;
62     }
63
64     gmx::ProgramInfo programInfo;
65
66     time_unit_t time_unit; /* the time unit, enum defined in oenv.h */
67     gmx_bool view;  /* view of file requested */
68     xvg_format_t xvg_format; /* xvg output format, enum defined in oenv.h */
69     int  verbosity; /* The level of verbosity for this program */
70     int debug_level; /* the debug level */
71 };
72
73 /* The source code in this file should be thread-safe. 
74       Please keep it that way. */
75
76 /******************************************************************
77  *
78  *             T R A J E C T O R Y   S T U F F
79  *
80  ******************************************************************/
81
82 /* read only time names */
83 /* These must correspond to the time units type time_unit_t in oenv.h */
84 static const real timefactors[] =   { 0,  1e3,  1, 1e-3, 1e-6, 1e-9, 1e-12, 0 };
85 static const real timeinvfactors[] ={ 0, 1e-3,  1,  1e3,  1e6,  1e9,  1e12, 0 };
86 static const char *time_units_str[] = { NULL, "fs", "ps", "ns", "us", 
87                                         "\\mus", "ms", "s" };
88 static const char *time_units_xvgr[] = { NULL, "fs", "ps", "ns",  
89                                         "ms", "s", NULL };
90
91
92 /***** OUTPUT_ENV MEMBER FUNCTIONS ******/
93
94 void output_env_init(output_env_t *oenvp, int argc, char *argv[],
95                      time_unit_t tmu, gmx_bool view, xvg_format_t xvg_format,
96                      int verbosity, int debug_level)
97 {
98     try
99     {
100         output_env_t oenv = new output_env(argc, argv);
101         *oenvp = oenv;
102         oenv->time_unit   = tmu;
103         oenv->view        = view;
104         oenv->xvg_format  = xvg_format;
105         oenv->verbosity   = verbosity;
106         oenv->debug_level = debug_level;
107     }
108     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
109 }
110
111 void output_env_init_default(output_env_t *oenvp)
112 {
113     try
114     {
115         output_env_t oenv = new output_env();
116         *oenvp = oenv;
117     }
118     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
119 }
120
121 void output_env_done(output_env_t oenv)
122 {
123     delete oenv;
124 }
125
126
127 int output_env_get_verbosity(const output_env_t oenv)
128 {
129     return oenv->verbosity;
130 }
131
132 int output_env_get_debug_level(const output_env_t oenv)
133 {
134     return oenv->debug_level;
135 }
136
137 const char *output_env_get_time_unit(const output_env_t oenv)
138 {
139     return time_units_str[oenv->time_unit];
140 }
141
142 const char *output_env_get_time_label(const output_env_t oenv)
143 {
144     char *label;
145     snew(label, 20);
146     
147     sprintf(label,"Time (%s)",time_units_str[oenv->time_unit] ? 
148             time_units_str[oenv->time_unit]: "ps");
149     
150     return label;
151 }
152
153 const char *output_env_get_xvgr_tlabel(const output_env_t oenv)
154 {
155     char *label;
156     snew(label, 20);
157     
158     sprintf(label,"Time (%s)", time_units_xvgr[oenv->time_unit] ?
159             time_units_xvgr[oenv->time_unit] : "ps");
160     
161     return label;
162 }
163
164 real output_env_get_time_factor(const output_env_t oenv)
165 {
166     return timefactors[oenv->time_unit];
167 }
168
169 real output_env_get_time_invfactor(const output_env_t oenv)
170 {
171     return timeinvfactors[oenv->time_unit];
172 }
173
174 real output_env_conv_time(const output_env_t oenv, real time)
175 {
176     return time*timefactors[oenv->time_unit];
177 }
178
179 void output_env_conv_times(const output_env_t oenv, int n, real *time)
180 {
181     int i;
182     double fact=timefactors[oenv->time_unit];
183     
184     if (fact!=1.)
185         for(i=0; i<n; i++)
186             time[i] *= fact;
187 }
188
189 gmx_bool output_env_get_view(const output_env_t oenv)
190 {
191     return oenv->view;
192 }
193
194 xvg_format_t output_env_get_xvg_format(const output_env_t oenv)
195 {
196     return oenv->xvg_format;
197 }
198
199 const char *output_env_get_program_name(const output_env_t oenv)
200 {
201     try
202     {
203         return oenv->programInfo.programNameWithPath().c_str();
204     }
205     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
206 }
207
208 const char *output_env_get_short_program_name(const output_env_t oenv)
209 {
210     try
211     {
212         return oenv->programInfo.programName().c_str();
213     }
214     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
215 }
216
217 const char *output_env_get_cmd_line(const output_env_t oenv)
218 {
219     try
220     {
221         return oenv->programInfo.commandLine().c_str();
222     }
223     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
224 }