Cantera  3.2.0a4
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
53 * addDataDirectory() 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
71//! @deprecated Deprecated in %Cantera 3.2 and to be removed thereafter. Renamed to
72//! addDataDirectory().
73void addDirectory(const string& dir);
74
75//! @copydoc Application::addDataDirectory
76void addDataDirectory(const string& dir);
77
78//! @copydoc Application::getDataDirectories
79string getDataDirectories(const string& sep);
80//! @}
81
82//! @copydoc Application::loadExtension
83void loadExtension(const string& extType, const string& name);
84
85//! Load extensions providing user-defined models from the `extensions` section of the
86//! given node. @see Application::loadExtension
87//!
88//! @since New in %Cantera 3.0
89void loadExtensions(const AnyMap& node);
90
91//! @copydoc Application::searchPythonVersions
92void searchPythonVersions(const string& versions);
93
94//! Delete and free all memory associated with the application
95/*!
96 * Delete all global data. It should be called at the end of the
97 * application if leak checking is to be done.
98 */
99void appdelete();
100
101//! @copydoc Application::thread_complete
102void thread_complete();
103
104//! @defgroup globalSettings Global Cantera Settings
105//! @brief Functions for accessing global %Cantera settings.
106//! @ingroup globalData
107
108//! @addtogroup globalSettings
109//! @{
110
111//! Returns `true` if %Cantera was loaded as a shared library in the current
112//! application. Returns `false` if it was statically linked.
113//! @since New in %Cantera 3.0
115
116//! @name %Cantera Version Information
117//! @{
118
119//! Returns the %Cantera version. This function is used to access the version from a
120//! library, rather than the @c CANTERA_VERSION macro that is available at compile time.
121//! @since New in %Cantera 3.0
122string version();
123
124//! Returns the hash of the git commit from which %Cantera was compiled, if known
125string gitCommit();
126
127//! @}
128
129//! Returns true if %Cantera was compiled in debug mode. Used for handling some cases
130//! where behavior tested in the test suite changes depending on whether the `NDEBUG`
131//! preprocessor macro is defined.
132bool debugModeEnabled();
133
134//! Returns true if %Cantera was compiled with C++ @c HDF5 support.
135//! @since New in %Cantera 3.0.
136bool usesHDF5();
137
138//! @}
139
140/**
141 * @defgroup logGroup Logging
142 * @brief Logging and generation of diagnostic output.
143 *
144 * Writing diagnostic information to the screen or to a file. It is often
145 * useful to be able to write diagnostic messages to the screen or to a file.
146 * %Cantera defines a set of procedures for this purpose designed to write text messages
147 * to the screen to document the progress of a complex calculation, such as a
148 * flame simulation.
149 * @ingroup debugGroup
150 */
151
152//! @addtogroup logGroup
153//! @{
154
155//! @copydoc Application::writelog(const string&)
156void writelog_direct(const string& msg);
157
158//! Write a message to the log only if loglevel > 0
159inline void debuglog(const string& msg, int loglevel)
160{
161 if (loglevel > 0) {
162 writelog_direct(msg);
163 }
164}
165
166//! Write a formatted message to the screen.
167//!
168//! This function passes its arguments to the fmt library 'format' function to
169//! generate a formatted string from a Python-style (curly braces) format
170//! string. This method is used throughout %Cantera to write log messages. It can
171//! also be called by user programs. The advantage of using writelog over
172//! writing directly to the standard output is that messages written with
173//! writelog will display correctly even when %Cantera is used from MATLAB or
174//! other application that do not have a standard output stream.
175template <typename... Args>
176void writelog(const string& fmt, const Args&... args) {
177 if (sizeof...(args) == 0) {
178 writelog_direct(fmt);
179 } else {
180 writelog_direct(fmt::format(fmt::runtime(fmt), args...));
181 }
182}
183
184//! Write a formatted message to the screen
185/*!
186 * Using the printf formatting of C, write a message to the screen
187 * with variable values.
188 *
189 * Here, we format an internal string with the correct values
190 * and then feed it into writelog().
191 *
192 * @param fmt c format string for the following arguments
193 * @param args arguments used to interpolate the format string
194 */
195template <typename... Args>
196void writelogf(const char* fmt, const Args& ... args) {
197 writelog_direct(fmt::sprintf(fmt, args...));
198}
199
200//! Write an end of line character to the screen and flush output
201void writelogendl();
202
203void writeline(char repeat, size_t count,
204 bool endl_after=true, bool endl_before=false);
205
206//! @} End of logging group
207
208//! @defgroup warnGroup Warnings
209//! @ingroup debugGroup
210//! @brief Warnings raised by %Cantera
211//! @{
212
213//! @cond
214
215//! helper function passing deprecation warning to global handler
216void _warn_deprecated(const string& method, const string& extra="");
217
218//! @endcond
219
220//! Print a deprecation warning raised from *method*.
221/*!
222* @see Application::warn_deprecated
223 * @param method method name
224 * @param msg Python-style format string with the following arguments
225 * @param args arguments for the format string
226 */
227template <typename... Args>
228void warn_deprecated(const string& method, const string& msg, const Args&... args) {
229 if (sizeof...(args) == 0) {
230 _warn_deprecated(method, msg);
231 } else {
232 _warn_deprecated(method, fmt::format(fmt::runtime(msg), args...));
233 }
234}
235
236//! @cond
237
238//! helper function passing generic warning to global handler
239void _warn(const string& warning, const string& method, const string& extra);
240
241//! @endcond
242
243//! Print a generic warning raised from *method*.
244/*!
245 * @see Application::warn
246 * @param warning type of warning; See Logger::warn
247 * @param method method name
248 * @param msg Python-style format string with the following arguments
249 * @param args arguments for the format string
250 */
251template <typename... Args>
252void warn(const string& warning, const string& method,
253 const string& msg, const Args&... args) {
254 if (sizeof...(args) == 0) {
255 _warn(warning, method, msg);
256 } else {
257 _warn(warning, method, fmt::format(fmt::runtime(msg), args...));
258 }
259}
260
261//! Print a user warning raised from *method* as `CanteraWarning`.
262/*!
263 * @param method method name
264 * @param msg Python-style format string with the following arguments
265 * @param args arguments for the format string
266 */
267template <typename... Args>
268void warn_user(const string& method, const string& msg, const Args&... args) {
269 if (sizeof...(args) == 0) {
270 _warn("Cantera", method, msg);
271 } else {
272 _warn("Cantera", method, fmt::format(fmt::runtime(msg), args...));
273 }
274}
275
276//! @} End of warnings group
277
278//! @addtogroup globalSettings
279//! @{
280
281//! @name Global Warning Settings
282//! @{
283
284//! @copydoc Application::suppress_deprecation_warnings
286
287//! @copydoc Application::make_deprecation_warnings_fatal
289
290//! @copydoc Application::make_warnings_fatal
292
293//! @copydoc Application::suppress_thermo_warnings
294void suppress_thermo_warnings(bool suppress=true);
295
296//! @copydoc Application::thermo_warnings_suppressed
298
299//! @copydoc Application::suppress_warnings
300void suppress_warnings();
301
302//! @copydoc Application::warnings_suppressed
304
305//! @} End of warning settings
306
307//! @copydoc Application::use_legacy_rate_constants
308void use_legacy_rate_constants(bool legacy=true);
309
310//! @copydoc Application::legacy_rate_constants_used
312
313// @} End of globalSettings group
314
315//! @copydoc Application::setLogger(Logger*)
316//! @deprecated To be removed after %Cantera 3.2. Replaced by version taking
317//! `unique_ptr`.
318//! @ingroup logGroup
319void setLogger(Logger* logwriter);
320
321//! @copydoc Application::setLogger(unique_ptr<Logger>)
322//! @since Changed in %Cantera 3.2 to take `unique_ptr` instead of bare pointer.
323//! @ingroup logGroup
324void setLogger(unique_ptr<Logger> logwriter);
325
326//! Enables printing a stacktrace to `std::err` if a segfault occurs. The Boost
327//! documentation says doing this from an error handler is not safe on all platforms
328//! and risks deadlocking. However, it can be useful for debugging and is therefore
329//! enabled when running tests.
330//! @since New in %Cantera 3.0
331//! @ingroup globalSettings
333
334//! Clip *value* such that lower <= value <= upper
335//! @ingroup mathTemplates
336template <class T>
337inline T clip(const T& value, const T& lower, const T& upper)
338{
339 return std::max(lower, std::min(upper, value));
340}
341
342//! Sign of a number. Returns -1 if x < 0, 1 if x > 0 and 0 if x == 0.
343//! @ingroup mathTemplates
344template <typename T> int sign(T x) {
345 return (T(0) < x) - (x < T(0));
346}
347
348//! Convert a type name to a human readable string, using `boost::core::demangle` if
349//! available. Also has a set of short names for some common types.
350//! @note Mainly for internal use by AnyMap and Delegator
351string demangle(const std::type_info& type);
352
353}
354
355#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:91
void use_legacy_rate_constants(bool legacy)
Set definition used for rate constant calculation.
Definition global.cpp:111
bool debugModeEnabled()
Returns true if Cantera was compiled in debug mode.
Definition global.cpp:209
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:229
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition global.cpp:76
void printStackTraceOnSegfault()
Enables printing a stacktrace to std::err if a segfault occurs.
Definition global.cpp:134
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition global.cpp:106
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
Definition global.cpp:96
string version()
Returns the Cantera version.
Definition global.cpp:154
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition global.cpp:81
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition is used.
Definition global.cpp:116
void suppress_warnings()
Globally disable printing of (user) warnings.
Definition global.cpp:86
void suppress_thermo_warnings(bool suppress)
Globally disable printing of warnings about problematic thermo data, such as NASA polynomials with di...
Definition global.cpp:101
string gitCommit()
Returns the hash of the git commit from which Cantera was compiled, if known.
Definition global.cpp:159
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:218
string getDataDirectories(const string &sep)
Get the Cantera data directories.
Definition global.cpp:180
string findInputFile(const string &name)
Find an input file.
Definition global.cpp:185
void addDirectory(const string &dir)
Add a directory to the data file search path.
Definition global.cpp:168
void addDataDirectory(const string &dir)
Add a directory to the data file search path.
Definition global.cpp:175
void writelog_direct(const string &msg)
Write a message to the logger.
Definition global.cpp:45
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:159
void writelogf(const char *fmt, const Args &... args)
Write a formatted message to the screen.
Definition global.h:196
void writelog(const string &fmt, const Args &... args)
Write a formatted message to the screen.
Definition global.h:176
void writelogendl()
Write an end of line character to the screen and flush output.
Definition global.cpp:50
int sign(T x)
Sign of a number.
Definition global.h:344
T clip(const T &value, const T &lower, const T &upper)
Clip value such that lower <= value <= upper.
Definition global.h:337
void warn(const string &warning, const string &method, const string &msg, const Args &... args)
Print a generic warning raised from method.
Definition global.h:252
void warn_user(const string &method, const string &msg, const Args &... args)
Print a user warning raised from method as CanteraWarning.
Definition global.h:268
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:195
void loadExtension(const string &extType, const string &name)
Load an extension implementing user-defined models.
Definition global.cpp:190
void searchPythonVersions(const string &versions)
Set the versions of Python to try when loading user-defined extensions, in order of preference.
Definition global.cpp:205
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition global.cpp:149
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:143