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