Cantera  3.2.0a4
Loading...
Searching...
No Matches
global.cpp
Go to the documentation of this file.
1//! @file global.cpp
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
7#include "application.h"
9
10#define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED
11#include <boost/stacktrace.hpp>
12#include <boost/core/demangle.hpp>
13
14#include <signal.h>
15
16namespace Cantera
17{
18
19//! Return a pointer to the application object
21{
22 return Application::Instance();
23}
24
25// **************** Text Logging ****************
26
27void setLogger(Logger* logwriter)
28{
29 try {
30 app()->setLogger(logwriter);
31 } catch (const std::bad_alloc&) {
32 logwriter->error("bad alloc thrown by app()");
33 }
34}
35
36void setLogger(unique_ptr<Logger> logwriter)
37{
38 try {
39 app()->setLogger(std::move(logwriter));
40 } catch (const std::bad_alloc&) {
41 logwriter->error("bad alloc thrown by app()");
42 }
43}
44
45void writelog_direct(const string& msg)
46{
47 app()->writelog(msg);
48}
49
51{
52 app()->writelogendl();
53}
54
55void writeline(char repeat, size_t count, bool endl_after, bool endl_before)
56{
57 if (endl_before) {
59 }
60 writelog_direct(string(count, repeat));
61 if (endl_after) {
63 }
64}
65
66void _warn_deprecated(const string& method, const string& extra)
67{
68 app()->warn_deprecated(method, extra);
69}
70
71void _warn(const string& warning, const string& method, const string& extra)
72{
73 app()->warn(warning, method, extra);
74}
75
77{
79}
80
82{
84}
85
87{
89}
90
92{
93 return app()->warnings_suppressed();
94}
95
97{
99}
100
101void suppress_thermo_warnings(bool suppress)
102{
103 app()->suppress_thermo_warnings(suppress);
104}
105
107{
109}
110
112{
114}
115
117{
119}
120
121namespace {
122void stacktraceWriter(int signum) {
123 if (signum == SIGSEGV) {
124 std::cerr << "\nSegmentation fault. Stack trace:\n";
125 } else {
126 std::cerr << "\nProcess terminated abnormally. Stack trace:\n";
127 }
128 ::signal(signum, SIG_DFL);
129 std::cerr << boost::stacktrace::stacktrace();
130 ::raise(signum);
131}
132}
133
135{
136 // Install signal handler to print stacktrace in case of segfault.
137 ::signal(SIGSEGV, &stacktraceWriter);
138 ::signal(SIGABRT, &stacktraceWriter);
139}
140
141// **************** Global Data ****************
142
144{
147}
148
150{
151 app()->thread_complete();
152}
153
154string version()
155{
156 return CANTERA_VERSION;
157}
158
159string gitCommit()
160{
161#ifdef GIT_COMMIT
162 return GIT_COMMIT;
163#else
164 return "unknown";
165#endif
166}
167
168void addDirectory(const string& dir)
169{
170 warn_deprecated("addDirectory",
171 "To be removed after Cantera 3.2. Renamed to addDataDirectory.");
172 app()->addDataDirectory(dir);
173}
174
175void addDataDirectory(const string& dir)
176{
177 app()->addDataDirectory(dir);
178}
179
180string getDataDirectories(const string& sep)
181{
182 return app()->getDataDirectories(sep);
183}
184
185string findInputFile(const string& name)
186{
187 return app()->findInputFile(name);
188}
189
190void loadExtension(const string& extType, const string& name)
191{
192 app()->loadExtension(extType, name);
193}
194
195void loadExtensions(const AnyMap& node)
196{
197 if (!node.hasKey("extensions")) {
198 return;
199 }
200 for (auto& extension : node["extensions"].asVector<AnyMap>()) {
201 loadExtension(extension["type"].asString(), extension["name"].asString());
202 }
203}
204
205void searchPythonVersions(const string& versions) {
206 app()->searchPythonVersions(versions);
207}
208
210{
211#ifdef NDEBUG
212 return false;
213#else
214 return true;
215#endif
216}
217
219{
220#if CT_USE_HDF5
221 return true;
222#else
223 return false;
224#endif
225}
226
227vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
228
229string demangle(const std::type_info& type)
230{
231 static map<string, string> typenames = {
232 {typeid(void).name(), "void"},
233 {typeid(double).name(), "double"},
234 {typeid(long int).name(), "long int"},
235 {typeid(bool).name(), "bool"},
236 {typeid(string).name(), "string"},
237 {typeid(vector<AnyValue>).name(), "vector<AnyValue>"},
238 {typeid(vector<AnyMap>).name(), "vector<AnyMap>"},
239 {typeid(vector<double>).name(), "vector<double>"},
240 {typeid(vector<long int>).name(), "vector<long int>"},
241 {typeid(vector<bool>).name(), "vector<bool>"},
242 {typeid(vector<string>).name(), "vector<string>"},
243 {typeid(vector<vector<double>>).name(), "vector<vector<double>>"},
244 {typeid(vector<vector<long int>>).name(), "vector<vector<long int>>"},
245 {typeid(vector<vector<bool>>).name(), "vector<vector<bool>>"},
246 {typeid(vector<vector<string>>).name(), "vector<vector<string>>"},
247 {typeid(AnyMap).name(), "AnyMap"},
248 };
249
250 if (typenames.count(type.name())) {
251 return typenames[type.name()];
252 } else {
253 return boost::core::demangle(type.name());
254 }
255}
256
257}
File contains the FactoryBase class declarations.
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:431
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1477
Class to hold global data.
Definition application.h:42
bool warnings_suppressed()
Returns true if warnings should be suppressed.
void use_legacy_rate_constants(bool legacy=true)
Set definition used for rate constant calculation.
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
static Application * Instance()
Return a pointer to the one and only instance of class Application.
void loadExtension(const string &extType, const string &name)
Load an extension implementing user-defined models.
void warn_deprecated(const string &method, const string &extra="")
Print a warning indicating that method is deprecated.
void searchPythonVersions(const string &versions)
Set the versions of Python to try when loading user-defined extensions, in order of preference.
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition is used.
void suppress_warnings()
Globally disable printing of (user) warnings.
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
void suppress_thermo_warnings(bool suppress=true)
Globally disable printing of warnings about problematic thermo data, such as NASA polynomials with di...
void writelogendl()
Write an end of line character to the logger and flush output.
static void ApplicationDestroy()
Static function that destroys the application class's data.
void warn(const string &warning, const string &method, const string &extra="")
Generate a general purpose warning; repeated warnings are not suppressed.
static vector< FactoryBase * > s_vFactoryRegistry
statically held list of Factories.
Definition FactoryBase.h:60
static void deleteFactories()
static function that deletes all factories in the internal registry maintained in a static variable
Definition FactoryBase.h:35
Base class for 'loggers' that write text messages to log files.
Definition logger.h:40
virtual void error(const string &msg)
Write an error message and quit.
Definition logger.h:90
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 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.
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 writelog(const string &msg)
Write a message to the logger.
void writelogendl()
Write an end of line character to the screen and flush output.
Definition global.cpp:50
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
static Application * app()
Return a pointer to the application object.
Definition global.cpp:20
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