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