Cantera  3.1.0
Loading...
Searching...
No Matches
global.h
Go to the documentation of this file.
1/**
2 * @file global.h
3 * This file contains definitions for utility functions and text for modules,
4 * inputfiles and logging, (see @ref inputGroup, and @ref logGroup).
5 *
6 * These functions store some parameters in global storage that are accessible
7 * at all times from the calling application. Contains module definitions for
8 * - inputfiles (see @ref inputGroup)
9 * - text logs (see @ref logGroup)
10 */
11
12// This file is part of Cantera. See License.txt in the top-level directory or
13// at https://cantera.org/license.txt for license and copyright information.
14
15#ifndef CT_GLOBAL_H
16#define CT_GLOBAL_H
17
18#include "ct_defs.h"
19#include "cantera/base/fmt.h"
20
21namespace Cantera
22{
23
24class Logger;
25class AnyMap;
26
27//! @defgroup ioGroup File Input/Output
28//! @details Classes and functions used for reading and writing of %Cantera input files.
29
30/**
31 * @defgroup inputGroup Input File Handling
32 * @brief Handling of %Cantera input files.
33 *
34 * The properties of phases and interfaces are specified in text files. These
35 * procedures handle various aspects of reading these files. Input files should be read
36 * using the newSolution() or newInterface() service functions (see @ref solnGroup).
37 *
38 * For input files not specified by an absolute pathname, %Cantera searches
39 * for input files along a path that includes platform-specific default
40 * locations, and possibly user-specified locations:
41 *
42 * - The current directory @c "." is always searched first. Then, on Windows, the
43 * registry is checked to find the %Cantera installation directory, and the
44 * @c data subdirectory of the installation directory will be added to the search
45 * path.
46 * - On any platform, if environment variable @c CANTERA_DATA is set to a directory
47 * name or a list of directory names separated with the OS-dependent path
48 * separator (that is, @c ";" on Windows, @c ":" elsewhere), then these directories
49 * will be added to the search path.
50 * - Finally, the location where the data files were installed when %Cantera was
51 * built is added to the search path.
52 * - Additional directories may be added dynamically by calling the addDirectory()
53 * function.
54 *
55 * %Cantera input files are written using YAML syntax. For more information on using
56 * YAML files in %Cantera, see the [YAML Users' Guide](../userguide/input-tutorial.html)
57 * or the [YAML Input File API Reference](../yaml/index.html). %Cantera provides the
58 * [`ck2yaml`](../userguide/ck2yaml-tutorial.html) tool for converting Chemkin-format
59 * mechanisms to the YAML format. The utilities [`cti2yaml`](../yaml/cti2yaml.html) and
60 * [`ctml2yaml`](../yaml/ctml2yaml.html) should be used to convert legacy CTI and XML
61 * input files (from %Cantera 2.6 and earlier) to the YAML format.
62 *
63 * @ingroup ioGroup
64 * @{
65 */
66
67//! @copydoc Application::findInputFile
68string findInputFile(const string& name);
69
70//! @copydoc Application::addDataDirectory
71void addDirectory(const string& dir);
72
73//! @copydoc Application::getDataDirectories
74string getDataDirectories(const string& sep);
75//! @}
76
77//! @copydoc Application::loadExtension
78void loadExtension(const string& extType, const string& name);
79
80//! Load extensions providing user-defined models from the `extensions` section of the
81//! given node. @see Application::loadExtension
82//!
83//! @since New in %Cantera 3.0
84void loadExtensions(const AnyMap& node);
85
86//! @copydoc Application::searchPythonVersions
87void searchPythonVersions(const string& versions);
88
89//! Delete and free all memory associated with the application
90/*!
91 * Delete all global data. It should be called at the end of the
92 * application if leak checking is to be done.
93 */
94void appdelete();
95
96//! @copydoc Application::thread_complete
97void thread_complete();
98
99//! @defgroup globalSettings Global Cantera Settings
100//! @brief Functions for accessing global %Cantera settings.
101//! @ingroup globalData
102
103//! @addtogroup globalSettings
104//! @{
105
106//! Returns `true` if %Cantera was loaded as a shared library in the current
107//! application. Returns `false` if it was statically linked.
108//! @since New in %Cantera 3.0
110
111//! @name %Cantera Version Information
112//! @{
113
114//! Returns the %Cantera version. This function is used to access the version from a
115//! library, rather than the @c CANTERA_VERSION macro that is available at compile time.
116//! @since New in %Cantera 3.0
117string version();
118
119//! Returns the hash of the git commit from which %Cantera was compiled, if known
120string gitCommit();
121
122//! @}
123
124//! Returns true if %Cantera was compiled in debug mode. Used for handling some cases
125//! where behavior tested in the test suite changes depending on whether the `NDEBUG`
126//! preprocessor macro is defined.
127bool debugModeEnabled();
128
129//! Returns true if %Cantera was compiled with C++ @c HDF5 support.
130//! @since New in %Cantera 3.0.
131bool usesHDF5();
132
133//! @}
134
135/**
136 * @defgroup logGroup Logging
137 * @brief Logging and generation of diagnostic output.
138 *
139 * Writing diagnostic information to the screen or to a file. It is often
140 * useful to be able to write diagnostic messages to the screen or to a file.
141 * %Cantera defines a set of procedures for this purpose designed to write text messages
142 * to the screen to document the progress of a complex calculation, such as a
143 * flame simulation.
144 * @ingroup debugGroup
145 */
146
147//! @addtogroup logGroup
148//! @{
149
150//! @copydoc Application::Messages::writelog(const string&)
151void writelog_direct(const string& msg);
152
153//! Write a message to the log only if loglevel > 0
154inline void debuglog(const string& msg, int loglevel)
155{
156 if (loglevel > 0) {
157 writelog_direct(msg);
158 }
159}
160
161//! Write a formatted message to the screen.
162//!
163//! This function passes its arguments to the fmt library 'format' function to
164//! generate a formatted string from a Python-style (curly braces) format
165//! string. This method is used throughout %Cantera to write log messages. It can
166//! also be called by user programs. The advantage of using writelog over
167//! writing directly to the standard output is that messages written with
168//! writelog will display correctly even when %Cantera is used from MATLAB or
169//! other application that do not have a standard output stream.
170template <typename... Args>
171void writelog(const string& fmt, const Args&... args) {
172 if (sizeof...(args) == 0) {
173 writelog_direct(fmt);
174 } else {
175 writelog_direct(fmt::format(fmt::runtime(fmt), args...));
176 }
177}
178
179//! Write a formatted message to the screen
180/*!
181 * Using the printf formatting of C, write a message to the screen
182 * with variable values.
183 *
184 * Here, we format an internal string with the correct values
185 * and then feed it into writelog().
186 *
187 * @param fmt c format string for the following arguments
188 * @param args arguments used to interpolate the format string
189 */
190template <typename... Args>
191void writelogf(const char* fmt, const Args& ... args) {
192 writelog_direct(fmt::sprintf(fmt, args...));
193}
194
195//! Write an end of line character to the screen and flush output
196void writelogendl();
197
198void writeline(char repeat, size_t count,
199 bool endl_after=true, bool endl_before=false);
200
201//! @} End of logging group
202
203//! @defgroup warnGroup Warnings
204//! @ingroup debugGroup
205//! @brief Warnings raised by %Cantera
206//! @{
207
208//! @cond
209
210//! helper function passing deprecation warning to global handler
211void _warn_deprecated(const string& method, const string& extra="");
212
213//! @endcond
214
215//! Print a deprecation warning raised from *method*.
216/*!
217* @see Application::warn_deprecated
218 * @param method method name
219 * @param msg Python-style format string with the following arguments
220 * @param args arguments for the format string
221 */
222template <typename... Args>
223void warn_deprecated(const string& method, const string& msg, const Args&... args) {
224 if (sizeof...(args) == 0) {
225 _warn_deprecated(method, msg);
226 } else {
227 _warn_deprecated(method, fmt::format(fmt::runtime(msg), args...));
228 }
229}
230
231//! @cond
232
233//! helper function passing generic warning to global handler
234void _warn(const string& warning, const string& method, const string& extra);
235
236//! @endcond
237
238//! Print a generic warning raised from *method*.
239/*!
240 * @see Application::warn
241 * @param warning type of warning; See Logger::warn
242 * @param method method name
243 * @param msg Python-style format string with the following arguments
244 * @param args arguments for the format string
245 */
246template <typename... Args>
247void warn(const string& warning, const string& method,
248 const string& msg, const Args&... args) {
249 if (sizeof...(args) == 0) {
250 _warn(warning, method, msg);
251 } else {
252 _warn(warning, method, fmt::format(fmt::runtime(msg), args...));
253 }
254}
255
256//! Print a user warning raised from *method* as `CanteraWarning`.
257/*!
258 * @param method method name
259 * @param msg Python-style format string with the following arguments
260 * @param args arguments for the format string
261 */
262template <typename... Args>
263void warn_user(const string& method, const string& msg, const Args&... args) {
264 if (sizeof...(args) == 0) {
265 _warn("Cantera", method, msg);
266 } else {
267 _warn("Cantera", method, fmt::format(fmt::runtime(msg), args...));
268 }
269}
270
271//! @} End of warnings group
272
273//! @addtogroup globalSettings
274//! @{
275
276//! @name Global Warning Settings
277//! @{
278
279//! @copydoc Application::suppress_deprecation_warnings
281
282//! @copydoc Application::make_deprecation_warnings_fatal
284
285//! @copydoc Application::make_warnings_fatal
287
288//! @copydoc Application::suppress_thermo_warnings
289void suppress_thermo_warnings(bool suppress=true);
290
291//! @copydoc Application::thermo_warnings_suppressed
293
294//! @copydoc Application::suppress_warnings
295void suppress_warnings();
296
297//! @copydoc Application::warnings_suppressed
299
300//! @} End of warning settings
301
302//! @copydoc Application::use_legacy_rate_constants
303void use_legacy_rate_constants(bool legacy=true);
304
305//! @copydoc Application::legacy_rate_constants_used
307
308// @} End of globalSettings group
309
310//! @copydoc Application::Messages::setLogger
311//! @ingroup logGroup
312void setLogger(Logger* logwriter);
313
314//! Enables printing a stacktrace to `std::err` if a segfault occurs. The Boost
315//! documentation says doing this from an error handler is not safe on all platforms
316//! and risks deadlocking. However, it can be useful for debugging and is therefore
317//! enabled when running tests.
318//! @since New in %Cantera 3.0
319//! @ingroup globalSettings
321
322//! Clip *value* such that lower <= value <= upper
323//! @ingroup mathTemplates
324template <class T>
325inline T clip(const T& value, const T& lower, const T& upper)
326{
327 return std::max(lower, std::min(upper, value));
328}
329
330//! Sign of a number. Returns -1 if x < 0, 1 if x > 0 and 0 if x == 0.
331//! @ingroup mathTemplates
332template <typename T> int sign(T x) {
333 return (T(0) < x) - (x < T(0));
334}
335
336//! Convert a type name to a human readable string, using `boost::core::demangle` if
337//! available. Also has a set of short names for some common types.
338//! @note Mainly for internal use by AnyMap and Delegator
339string demangle(const std::type_info& type);
340
341}
342
343#endif
This file contains definitions of constants, types and terms that are used in internal routines and a...
Wrapper for either system-installed or local headers for fmt.
bool warnings_suppressed()
Returns true if warnings should be suppressed.
Definition global.cpp:82
void use_legacy_rate_constants(bool legacy)
Set definition used for rate constant calculation.
Definition global.cpp:102
bool debugModeEnabled()
Returns true if Cantera was compiled in debug mode.
Definition global.cpp:193
string demangle(const std::type_info &type)
Convert a type name to a human readable string, using boost::core::demangle if available.
Definition global.cpp:213
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition global.cpp:67
void printStackTraceOnSegfault()
Enables printing a stacktrace to std::err if a segfault occurs.
Definition global.cpp:125
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition global.cpp:97
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
Definition global.cpp:87
string version()
Returns the Cantera version.
Definition global.cpp:145
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition global.cpp:72
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition is used.
Definition global.cpp:107
void suppress_warnings()
Globally disable printing of (user) warnings.
Definition global.cpp:77
void suppress_thermo_warnings(bool suppress)
Globally disable printing of warnings about problematic thermo data, such as NASA polynomials with di...
Definition global.cpp:92
string gitCommit()
Returns the hash of the git commit from which Cantera was compiled, if known.
Definition global.cpp:150
bool usingSharedLibrary()
Returns true if Cantera was loaded as a shared library in the current application.
bool usesHDF5()
Returns true if Cantera was compiled with C++ HDF5 support.
Definition global.cpp:202
string getDataDirectories(const string &sep)
Get the Cantera data directories.
Definition global.cpp:164
string findInputFile(const string &name)
Find an input file.
Definition global.cpp:169
void addDirectory(const string &dir)
Add a directory to the data file search path.
Definition global.cpp:159
void writelog_direct(const string &msg)
Write a message to the screen.
Definition global.cpp:36
void setLogger(Logger *logwriter)
Install a logger.
Definition global.cpp:27
void debuglog(const string &msg, int loglevel)
Write a message to the log only if loglevel > 0.
Definition global.h:154
void writelogf(const char *fmt, const Args &... args)
Write a formatted message to the screen.
Definition global.h:191
void writelog(const string &fmt, const Args &... args)
Write a formatted message to the screen.
Definition global.h:171
void writelogendl()
Write an end of line character to the screen and flush output.
Definition global.cpp:41
int sign(T x)
Sign of a number.
Definition global.h:332
T clip(const T &value, const T &lower, const T &upper)
Clip value such that lower <= value <= upper.
Definition global.h:325
void warn(const string &warning, const string &method, const string &msg, const Args &... args)
Print a generic warning raised from method.
Definition global.h:247
void warn_user(const string &method, const string &msg, const Args &... args)
Print a user warning raised from method as CanteraWarning.
Definition global.h:263
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
void loadExtensions(const AnyMap &node)
Load extensions providing user-defined models from the extensions section of the given node.
Definition global.cpp:179
void loadExtension(const string &extType, const string &name)
Load an extension implementing user-defined models.
Definition global.cpp:174
void searchPythonVersions(const string &versions)
Set the versions of Python to try when loading user-defined extensions, in order of preference.
Definition global.cpp:189
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition global.cpp:140
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
Definition AnyMap.cpp:1997
void appdelete()
Delete and free all memory associated with the application.
Definition global.cpp:134