Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / gmxlib / oenv.cpp
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  * Copyright (c) 2013, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "oenv.h"
38
39 #include "smalloc.h"
40
41 #include "gromacs/utility/exceptions.h"
42 #include "gromacs/utility/programinfo.h"
43
44 struct output_env
45 {
46     output_env()
47     {
48         setDefaults();
49     }
50     output_env(int argc, const char *const argv[])
51         : programInfo(argc, argv)
52     {
53         setDefaults();
54     }
55
56     void setDefaults()
57     {
58         time_unit   = time_ps;
59         view        = FALSE;
60         xvg_format  = exvgNONE;
61         verbosity   = 0;
62         debug_level = 0;
63     }
64
65     gmx::ProgramInfo programInfo;
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
74 /* The source code in this file should be thread-safe.
75       Please keep it that way. */
76
77 /******************************************************************
78  *
79  *             T R A J E C T O R Y   S T U F F
80  *
81  ******************************************************************/
82
83 /* read only time names */
84 /* These must correspond to the time units type time_unit_t in oenv.h */
85 static const real  timefactors[] =   { 0,  1e3,  1, 1e-3, 1e-6, 1e-9, 1e-12, 0 };
86 static const real  timeinvfactors[] = { 0, 1e-3,  1,  1e3,  1e6,  1e9,  1e12, 0 };
87 static const char *time_units_str[] = {
88     NULL, "fs", "ps", "ns", "us",
89     "\\mus", "ms", "s"
90 };
91 static const char *time_units_xvgr[] = {
92     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     try
104     {
105         output_env_t oenv = new output_env(argc, argv);
106         *oenvp            = oenv;
107         oenv->time_unit   = tmu;
108         oenv->view        = view;
109         oenv->xvg_format  = xvg_format;
110         oenv->verbosity   = verbosity;
111         oenv->debug_level = debug_level;
112     }
113     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
114 }
115
116 void output_env_init_default(output_env_t *oenvp)
117 {
118     try
119     {
120         output_env_t oenv = new output_env();
121         *oenvp = oenv;
122     }
123     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
124 }
125
126 void output_env_done(output_env_t oenv)
127 {
128     delete oenv;
129 }
130
131
132 int output_env_get_verbosity(const output_env_t oenv)
133 {
134     return oenv->verbosity;
135 }
136
137 int output_env_get_debug_level(const output_env_t oenv)
138 {
139     return oenv->debug_level;
140 }
141
142 const char *output_env_get_time_unit(const output_env_t oenv)
143 {
144     return time_units_str[oenv->time_unit];
145 }
146
147 const char *output_env_get_time_label(const output_env_t oenv)
148 {
149     char *label;
150     snew(label, 20);
151
152     sprintf(label, "Time (%s)", time_units_str[oenv->time_unit] ?
153             time_units_str[oenv->time_unit] : "ps");
154
155     return label;
156 }
157
158 const char *output_env_get_xvgr_tlabel(const output_env_t oenv)
159 {
160     char *label;
161     snew(label, 20);
162
163     sprintf(label, "Time (%s)", time_units_xvgr[oenv->time_unit] ?
164             time_units_xvgr[oenv->time_unit] : "ps");
165
166     return label;
167 }
168
169 real output_env_get_time_factor(const output_env_t oenv)
170 {
171     return timefactors[oenv->time_unit];
172 }
173
174 real output_env_get_time_invfactor(const output_env_t oenv)
175 {
176     return timeinvfactors[oenv->time_unit];
177 }
178
179 real output_env_conv_time(const output_env_t oenv, real time)
180 {
181     return time*timefactors[oenv->time_unit];
182 }
183
184 void output_env_conv_times(const output_env_t oenv, int n, real *time)
185 {
186     int    i;
187     double fact = timefactors[oenv->time_unit];
188
189     if (fact != 1.)
190     {
191         for (i = 0; i < n; i++)
192         {
193             time[i] *= fact;
194         }
195     }
196 }
197
198 gmx_bool output_env_get_view(const output_env_t oenv)
199 {
200     return oenv->view;
201 }
202
203 xvg_format_t output_env_get_xvg_format(const output_env_t oenv)
204 {
205     return oenv->xvg_format;
206 }
207
208 const char *output_env_get_program_name(const output_env_t oenv)
209 {
210     try
211     {
212         return oenv->programInfo.fullBinaryPath().c_str();
213     }
214     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
215 }
216
217 const char *output_env_get_short_program_name(const output_env_t oenv)
218 {
219     try
220     {
221         // TODO: Use the display name once it doesn't break anything.
222         return oenv->programInfo.programName().c_str();
223     }
224     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
225 }
226
227 const char *output_env_get_cmd_line(const output_env_t oenv)
228 {
229     try
230     {
231         return oenv->programInfo.commandLine().c_str();
232     }
233     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
234 }