Move binary directory search to ProgramInfo.
[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[] = {
87     NULL, "fs", "ps", "ns", "us",
88     "\\mus", "ms", "s"
89 };
90 static const char *time_units_xvgr[] = {
91     NULL, "fs", "ps", "ns",
92     "ms", "s", NULL
93 };
94
95
96 /***** OUTPUT_ENV MEMBER FUNCTIONS ******/
97
98 void output_env_init(output_env_t *oenvp, int argc, char *argv[],
99                      time_unit_t tmu, gmx_bool view, xvg_format_t xvg_format,
100                      int verbosity, int debug_level)
101 {
102     try
103     {
104         output_env_t oenv = new output_env(argc, argv);
105         *oenvp            = oenv;
106         oenv->time_unit   = tmu;
107         oenv->view        = view;
108         oenv->xvg_format  = xvg_format;
109         oenv->verbosity   = verbosity;
110         oenv->debug_level = debug_level;
111     }
112     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
113 }
114
115 void output_env_init_default(output_env_t *oenvp)
116 {
117     try
118     {
119         output_env_t oenv = new output_env();
120         *oenvp = oenv;
121     }
122     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
123 }
124
125 void output_env_done(output_env_t oenv)
126 {
127     delete oenv;
128 }
129
130
131 int output_env_get_verbosity(const output_env_t oenv)
132 {
133     return oenv->verbosity;
134 }
135
136 int output_env_get_debug_level(const output_env_t oenv)
137 {
138     return oenv->debug_level;
139 }
140
141 const char *output_env_get_time_unit(const output_env_t oenv)
142 {
143     return time_units_str[oenv->time_unit];
144 }
145
146 const char *output_env_get_time_label(const output_env_t oenv)
147 {
148     char *label;
149     snew(label, 20);
150
151     sprintf(label, "Time (%s)", time_units_str[oenv->time_unit] ?
152             time_units_str[oenv->time_unit] : "ps");
153
154     return label;
155 }
156
157 const char *output_env_get_xvgr_tlabel(const output_env_t oenv)
158 {
159     char *label;
160     snew(label, 20);
161
162     sprintf(label, "Time (%s)", time_units_xvgr[oenv->time_unit] ?
163             time_units_xvgr[oenv->time_unit] : "ps");
164
165     return label;
166 }
167
168 real output_env_get_time_factor(const output_env_t oenv)
169 {
170     return timefactors[oenv->time_unit];
171 }
172
173 real output_env_get_time_invfactor(const output_env_t oenv)
174 {
175     return timeinvfactors[oenv->time_unit];
176 }
177
178 real output_env_conv_time(const output_env_t oenv, real time)
179 {
180     return time*timefactors[oenv->time_unit];
181 }
182
183 void output_env_conv_times(const output_env_t oenv, int n, real *time)
184 {
185     int    i;
186     double fact = timefactors[oenv->time_unit];
187
188     if (fact != 1.)
189     {
190         for (i = 0; i < n; i++)
191         {
192             time[i] *= fact;
193         }
194     }
195 }
196
197 gmx_bool output_env_get_view(const output_env_t oenv)
198 {
199     return oenv->view;
200 }
201
202 xvg_format_t output_env_get_xvg_format(const output_env_t oenv)
203 {
204     return oenv->xvg_format;
205 }
206
207 const char *output_env_get_program_name(const output_env_t oenv)
208 {
209     try
210     {
211         return oenv->programInfo.fullBinaryPath().c_str();
212     }
213     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
214 }
215
216 const char *output_env_get_short_program_name(const output_env_t oenv)
217 {
218     try
219     {
220         // TODO: Use the display name once it doesn't break anything.
221         return oenv->programInfo.programName().c_str();
222     }
223     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
224 }
225
226 const char *output_env_get_cmd_line(const output_env_t oenv)
227 {
228     try
229     {
230         return oenv->programInfo.commandLine().c_str();
231     }
232     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
233 }