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