Cantera  3.2.0a2
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 app()->addDataDirectory(dir);
171}
172
173string getDataDirectories(const string& sep)
174{
175 return app()->getDataDirectories(sep);
176}
177
178string findInputFile(const string& name)
179{
180 return app()->findInputFile(name);
181}
182
183void loadExtension(const string& extType, const string& name)
184{
185 app()->loadExtension(extType, name);
186}
187
188void loadExtensions(const AnyMap& node)
189{
190 if (!node.hasKey("extensions")) {
191 return;
192 }
193 for (auto& extension : node["extensions"].asVector<AnyMap>()) {
194 loadExtension(extension["type"].asString(), extension["name"].asString());
195 }
196}
197
198void searchPythonVersions(const string& versions) {
199 app()->searchPythonVersions(versions);
200}
201
203{
204#ifdef NDEBUG
205 return false;
206#else
207 return true;
208#endif
209}
210
212{
213#if CT_USE_HDF5
214 return true;
215#else
216 return false;
217#endif
218}
219
220vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
221
222string demangle(const std::type_info& type)
223{
224 static map<string, string> typenames = {
225 {typeid(void).name(), "void"},
226 {typeid(double).name(), "double"},
227 {typeid(long int).name(), "long int"},
228 {typeid(bool).name(), "bool"},
229 {typeid(string).name(), "string"},
230 {typeid(vector<AnyValue>).name(), "vector<AnyValue>"},
231 {typeid(vector<AnyMap>).name(), "vector<AnyMap>"},
232 {typeid(vector<double>).name(), "vector<double>"},
233 {typeid(vector<long int>).name(), "vector<long int>"},
234 {typeid(vector<bool>).name(), "vector<bool>"},
235 {typeid(vector<string>).name(), "vector<string>"},
236 {typeid(vector<vector<double>>).name(), "vector<vector<double>>"},
237 {typeid(vector<vector<long int>>).name(), "vector<vector<long int>>"},
238 {typeid(vector<vector<bool>>).name(), "vector<vector<bool>>"},
239 {typeid(vector<vector<string>>).name(), "vector<vector<string>>"},
240 {typeid(AnyMap).name(), "AnyMap"},
241 };
242
243 if (typenames.count(type.name())) {
244 return typenames[type.name()];
245 } else {
246 return boost::core::demangle(type.name());
247 }
248}
249
250}
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:202
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:222
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:211
string getDataDirectories(const string &sep)
Get the Cantera data directories.
Definition global.cpp:173
string findInputFile(const string &name)
Find an input file.
Definition global.cpp:178
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:188
void loadExtension(const string &extType, const string &name)
Load an extension implementing user-defined models.
Definition global.cpp:183
void searchPythonVersions(const string &versions)
Set the versions of Python to try when loading user-defined extensions, in order of preference.
Definition global.cpp:198
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 appdelete()
Delete and free all memory associated with the application.
Definition global.cpp:143