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