8f19e66c225aeb8f1c9d51992c7e79d47aea2356
[alexxy/gromacs.git] / src / gromacs / fileio / 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,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39
40 #include "oenv.h"
41
42 #include "gromacs/utility/exceptions.h"
43 #include "gromacs/utility/programcontext.h"
44 #include "gromacs/utility/smalloc.h"
45 #include "gromacs/utility/stringutil.h"
46
47 struct gmx_output_env_t
48 {
49     explicit gmx_output_env_t(const gmx::IProgramContext& context) :
50         programContext(context),
51         time_unit(time_ps),
52         view(FALSE),
53         xvg_format(exvgNONE),
54         verbosity(0),
55         trajectory_io_verbosity(0)
56     {
57     }
58
59
60     const gmx::IProgramContext& programContext;
61
62     /* the time unit, enum defined in oenv.h */
63     time_unit_t time_unit;
64     /* view of file requested */
65     gmx_bool view;
66     /* xvg output format, enum defined in oenv.h */
67     xvg_format_t xvg_format;
68     /* The level of verbosity for this program */
69     int verbosity;
70     /* The level of verbosity during trajectory I/O. Default=1, quiet=0. */
71     int trajectory_io_verbosity;
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[]     = { real(0),    real(1e3),  real(1),     real(1e-3),
86                                     real(1e-6), real(1e-9), real(1e-12), real(0) };
87 static const real  timeinvfactors[]  = { real(0),   real(1e-3), real(1),    real(1e3),
88                                        real(1e6), real(1e9),  real(1e12), real(0) };
89 static const char* time_units_str[]  = { nullptr, "fs", "ps", "ns", "us", "\\mus", "ms", "s" };
90 static const char* time_units_xvgr[] = { nullptr, "fs", "ps", "ns", "ms", "s", nullptr };
91
92
93 /***** OUTPUT_ENV MEMBER FUNCTIONS ******/
94
95 void output_env_init(gmx_output_env_t**          oenvp,
96                      const gmx::IProgramContext& context,
97                      time_unit_t                 tmu,
98                      gmx_bool                    view,
99                      xvg_format_t                xvg_format,
100                      int                         verbosity)
101 {
102     try
103     {
104         gmx_output_env_t* oenv        = new gmx_output_env_t(context);
105         *oenvp                        = oenv;
106         oenv->time_unit               = tmu;
107         oenv->view                    = view;
108         oenv->xvg_format              = xvg_format;
109         oenv->verbosity               = verbosity;
110         const char* env               = getenv("GMX_TRAJECTORY_IO_VERBOSITY");
111         oenv->trajectory_io_verbosity = (env != nullptr ? strtol(env, nullptr, 10) : 1);
112     }
113     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
114 }
115
116 void output_env_init_default(gmx_output_env_t** oenvp)
117 {
118     try
119     {
120         gmx_output_env_t* oenv = new gmx_output_env_t(gmx::getProgramContext());
121         *oenvp                 = oenv;
122     }
123     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
124 }
125
126 void output_env_done(gmx_output_env_t* oenv)
127 {
128     delete oenv;
129 }
130
131
132 int output_env_get_verbosity(const gmx_output_env_t* oenv)
133 {
134     return oenv->verbosity;
135 }
136
137 int output_env_get_trajectory_io_verbosity(const gmx_output_env_t* oenv)
138 {
139     return oenv->trajectory_io_verbosity;
140 }
141
142 std::string output_env_get_time_unit(const gmx_output_env_t* oenv)
143 {
144     return time_units_str[oenv->time_unit];
145 }
146
147 std::string output_env_get_time_label(const gmx_output_env_t* oenv)
148 {
149     return gmx::formatString(
150             "Time (%s)", time_units_str[oenv->time_unit] ? time_units_str[oenv->time_unit] : "ps");
151 }
152
153 std::string output_env_get_xvgr_tlabel(const gmx_output_env_t* oenv)
154 {
155     return gmx::formatString(
156             "Time (%s)", time_units_xvgr[oenv->time_unit] ? time_units_xvgr[oenv->time_unit] : "ps");
157 }
158
159 real output_env_get_time_factor(const gmx_output_env_t* oenv)
160 {
161     return timefactors[oenv->time_unit];
162 }
163
164 real output_env_get_time_invfactor(const gmx_output_env_t* oenv)
165 {
166     return timeinvfactors[oenv->time_unit];
167 }
168
169 real output_env_conv_time(const gmx_output_env_t* oenv, real time)
170 {
171     return time * timefactors[oenv->time_unit];
172 }
173
174 void output_env_conv_times(const gmx_output_env_t* oenv, int n, real* time)
175 {
176     int    i;
177     double fact = timefactors[oenv->time_unit];
178
179     if (fact != 1.)
180     {
181         for (i = 0; i < n; i++)
182         {
183             time[i] *= fact;
184         }
185     }
186 }
187
188 gmx_bool output_env_get_view(const gmx_output_env_t* oenv)
189 {
190     return oenv->view;
191 }
192
193 xvg_format_t output_env_get_xvg_format(const gmx_output_env_t* oenv)
194 {
195     return oenv->xvg_format;
196 }
197
198 const char* output_env_get_program_display_name(const gmx_output_env_t* oenv)
199 {
200     const char* displayName = nullptr;
201
202     try
203     {
204         displayName = oenv->programContext.displayName();
205     }
206     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
207
208     return displayName;
209 }
210
211 const gmx::IProgramContext& output_env_get_program_context(const gmx_output_env_t* oenv)
212 {
213     return oenv->programContext;
214 }