| Cantera 3.0.0

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
6#include "cantera/base/FactoryBase.h"
7#include "application.h"
8#include "cantera/base/AnyMap.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
16 namespace Cantera
17{
18
19//! Return a pointer to the application object
20 static Application* app()
21{
22 return Application::Instance();
23}
24
25// **************** Text Logging ****************
26
27 void 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
36 void writelog_direct(const string& msg)
37{
38 app()->writelog(msg);
39}
40
41 void writelogendl()
42{
43 app()->writelogendl();
44}
45
46 void writeline(char repeat, size_t count, bool endl_after, bool endl_before)
47{
48 if (endl_before) {
49 writelogendl();
50 }
51 writelog_direct(string(count, repeat));
52 if (endl_after) {
53 writelogendl();
54 }
55}
56
57 void _warn_deprecated(const string& method, const string& extra)
58{
59 app()->warn_deprecated(method, extra);
60}
61
62 void _warn(const string& warning, const string& method, const string& extra)
63{
64 app()->warn(warning, method, extra);
65}
66
67 void suppress_deprecation_warnings()
68{
69 app()->suppress_deprecation_warnings();
70}
71
72 void make_deprecation_warnings_fatal()
73{
74 app()->make_deprecation_warnings_fatal();
75}
76
77 void suppress_warnings()
78{
79 app()->suppress_warnings();
80}
81
82 bool warnings_suppressed()
83{
84 return app()->warnings_suppressed();
85}
86
87 void make_warnings_fatal()
88{
89 app()->make_warnings_fatal();
90}
91
92 void suppress_thermo_warnings(bool suppress)
93{
94 app()->suppress_thermo_warnings(suppress);
95}
96
97 bool thermo_warnings_suppressed()
98{
99 return app()->thermo_warnings_suppressed();
100}
101
102 void use_legacy_rate_constants(bool legacy)
103{
104 app()->use_legacy_rate_constants(legacy);
105}
106
107 bool legacy_rate_constants_used()
108{
109 return app()->legacy_rate_constants_used();
110}
111
112 namespace {
113 void stacktraceWriter(int signum) {
114 if (signum == SIGSEGV) {
115 std::cerr << "\nSegmentation fault. Stack trace:\n";
116 } else {
117 std::cerr << "\nProcess terminated abnormally. Stack trace:\n";
118 }
119 ::signal(signum, SIG_DFL);
120 std::cerr << boost::stacktrace::stacktrace();
121 ::raise(signum);
122}
123}
124
125 void printStackTraceOnSegfault()
126{
127 // Install signal handler to print stacktrace in case of segfault.
128 ::signal(SIGSEGV, &stacktraceWriter);
129 ::signal(SIGABRT, &stacktraceWriter);
130}
131
132// **************** Global Data ****************
133
134 void appdelete()
135{
136 Application::ApplicationDestroy();
137 FactoryBase::deleteFactories();
138}
139
140 void thread_complete()
141{
142 app()->thread_complete();
143}
144
145 string version()
146{
147 return CANTERA_VERSION;
148}
149
150 string gitCommit()
151{
152#ifdef GIT_COMMIT
153 return GIT_COMMIT;
154#else
155 return "unknown";
156#endif
157}
158
159 void addDirectory(const string& dir)
160{
161 app()->addDataDirectory(dir);
162}
163
164 string getDataDirectories(const string& sep)
165{
166 return app()->getDataDirectories(sep);
167}
168
169 string findInputFile(const string& name)
170{
171 return app()->findInputFile(name);
172}
173
174 void loadExtension(const string& extType, const string& name)
175{
176 app()->loadExtension(extType, name);
177}
178
179 void loadExtensions(const AnyMap& node)
180{
181 if (!node.hasKey("extensions")) {
182 return;
183 }
184 for (auto& extension : node["extensions"].asVector<AnyMap>()) {
185 loadExtension(extension["type"].asString(), extension["name"].asString());
186 }
187}
188
189 void searchPythonVersions(const string& versions) {
190 app()->searchPythonVersions(versions);
191}
192
193 bool debugModeEnabled()
194{
195#ifdef NDEBUG
196 return false;
197#else
198 return true;
199#endif
200}
201
202 bool usesHDF5()
203{
204#if CT_USE_HDF5
205 return true;
206#else
207 return false;
208#endif
209}
210
211vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
212
213 string demangle(const std::type_info& type)
214{
215 static map<string, string> typenames = {
216 {typeid(void).name(), "void"},
217 {typeid(double).name(), "double"},
218 {typeid(long int).name(), "long int"},
219 {typeid(bool).name(), "bool"},
220 {typeid(string).name(), "string"},
221 {typeid(vector<AnyValue>).name(), "vector<AnyValue>"},
222 {typeid(vector<AnyMap>).name(), "vector<AnyMap>"},
223 {typeid(vector<double>).name(), "vector<double>"},
224 {typeid(vector<long int>).name(), "vector<long int>"},
225 {typeid(vector<bool>).name(), "vector<bool>"},
226 {typeid(vector<string>).name(), "vector<string>"},
227 {typeid(vector<vector<double>>).name(), "vector<vector<double>>"},
228 {typeid(vector<vector<long int>>).name(), "vector<vector<long int>>"},
229 {typeid(vector<vector<bool>>).name(), "vector<vector<bool>>"},
230 {typeid(vector<vector<string>>).name(), "vector<vector<string>>"},
231 {typeid(AnyMap).name(), "AnyMap"},
232 };
233
234 if (typenames.count(type.name())) {
235 return typenames[type.name()];
236 } else {
237 return boost::core::demangle(type.name());
238 }
239}
240
241}
AnyMap.h
FactoryBase.h
File contains the FactoryBase class declarations.
application.h
Cantera::AnyMap
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:427
Cantera::AnyMap::hasKey
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1423
Cantera::Application
Class to hold global data.
Definition application.h:42
Cantera::Application::warnings_suppressed
bool warnings_suppressed()
Returns true if warnings should be suppressed.
Definition application.h:352
Cantera::Application::use_legacy_rate_constants
void use_legacy_rate_constants(bool legacy=true)
Set definition used for rate constant calculation.
Definition application.h:383
Cantera::Application::suppress_deprecation_warnings
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition application.h:327
Cantera::Application::thermo_warnings_suppressed
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition application.h:369
Cantera::Application::make_warnings_fatal
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
Definition application.h:358
Cantera::Application::setLogger
void setLogger(Logger *logwriter)
Install a logger.
Definition application.h:393
Cantera::Application::make_deprecation_warnings_fatal
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition application.h:334
Cantera::Application::Instance
static Application * Instance()
Return a pointer to the one and only instance of class Application.
Definition application.cpp:117
Cantera::Application::loadExtension
void loadExtension(const string &extType, const string &name)
Load an extension implementing user-defined models.
Definition application.cpp:378
Cantera::Application::warn_deprecated
void warn_deprecated(const string &method, const string &extra="")
Print a warning indicating that method is deprecated.
Definition application.cpp:135
Cantera::Application::searchPythonVersions
void searchPythonVersions(const string &versions)
Set the versions of Python to try when loading user-defined extensions, in order of preference.
Definition application.cpp:429
Cantera::Application::legacy_rate_constants_used
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition is used.
Definition application.h:388
Cantera::Application::suppress_warnings
void suppress_warnings()
Globally disable printing of (user) warnings.
Definition application.h:346
Cantera::Application::thread_complete
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition application.cpp:156
Cantera::Application::writelog
void writelog(const string &msg)
Write a message to the screen.
Definition application.h:305
Cantera::Application::suppress_thermo_warnings
void suppress_thermo_warnings(bool suppress=true)
Globally disable printing of warnings about problematic thermo data, such as NASA polynomials with di...
Definition application.h:364
Cantera::Application::writelogendl
void writelogendl()
Write an endl to the screen and flush output.
Definition application.h:310
Cantera::Application::ApplicationDestroy
static void ApplicationDestroy()
Static function that destroys the application class's data.
Definition application.cpp:126
Cantera::Application::warn
void warn(const string &warning, const string &method, const string &extra="")
Generate a general purpose warning; repeated warnings are not suppressed.
Definition application.cpp:146
Cantera::FactoryBase::s_vFactoryRegistry
static vector< FactoryBase * > s_vFactoryRegistry
statically held list of Factories.
Definition FactoryBase.h:60
Cantera::FactoryBase::deleteFactories
static void deleteFactories()
static function that deletes all factories in the internal registry maintained in a static variable
Definition FactoryBase.h:35
Cantera::Logger
Base class for 'loggers' that write text messages to log files.
Definition logger.h:41
Cantera::Logger::error
virtual void error(const string &msg)
Write an error message and quit.
Definition logger.h:91
Cantera::warnings_suppressed
bool warnings_suppressed()
Returns true if warnings should be suppressed.
Definition global.cpp:82
Cantera::use_legacy_rate_constants
void use_legacy_rate_constants(bool legacy)
Set definition used for rate constant calculation.
Definition global.cpp:102
Cantera::debugModeEnabled
bool debugModeEnabled()
Returns true if Cantera was compiled in debug mode.
Definition global.cpp:193
Cantera::demangle
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
Cantera::suppress_deprecation_warnings
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition global.cpp:67
Cantera::printStackTraceOnSegfault
void printStackTraceOnSegfault()
Enables printing a stacktrace to std::err if a segfault occurs.
Definition global.cpp:125
Cantera::thermo_warnings_suppressed
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition global.cpp:97
Cantera::make_warnings_fatal
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
Definition global.cpp:87
Cantera::version
string version()
Returns the Cantera version.
Definition global.cpp:145
Cantera::make_deprecation_warnings_fatal
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition global.cpp:72
Cantera::legacy_rate_constants_used
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition is used.
Definition global.cpp:107
Cantera::suppress_warnings
void suppress_warnings()
Globally disable printing of (user) warnings.
Definition global.cpp:77
Cantera::suppress_thermo_warnings
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
Cantera::gitCommit
string gitCommit()
Returns the hash of the git commit from which Cantera was compiled, if known.
Definition global.cpp:150
Cantera::usesHDF5
bool usesHDF5()
Returns true if Cantera was compiled with C++ HDF5 support.
Definition global.cpp:202
Cantera::getDataDirectories
string getDataDirectories(const string &sep)
Get the Cantera data directories.
Definition global.cpp:164
Cantera::findInputFile
string findInputFile(const string &name)
Find an input file.
Definition global.cpp:169
Cantera::addDirectory
void addDirectory(const string &dir)
Add a directory to the data file search path.
Definition global.cpp:159
Cantera::Application::addDataDirectory
void addDataDirectory(const string &dir)
Add a directory to the data file search path.
Definition application.cpp:284
Cantera::writelog_direct
void writelog_direct(const string &msg)
Write a message to the screen.
Definition global.cpp:36
Cantera::setLogger
void setLogger(Logger *logwriter)
Install a logger.
Definition global.cpp:27
Cantera::writelogendl
void writelogendl()
Write an end of line character to the screen and flush output.
Definition global.cpp:41
Cantera
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564
Cantera::loadExtensions
void loadExtensions(const AnyMap &node)
Load extensions providing user-defined models from the extensions section of the given node.
Definition global.cpp:179
Cantera::loadExtension
void loadExtension(const string &extType, const string &name)
Load an extension implementing user-defined models.
Definition global.cpp:174
Cantera::searchPythonVersions
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
Cantera::app
static Application * app()
Return a pointer to the application object.
Definition global.cpp:20
Cantera::thread_complete
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition global.cpp:140
Cantera::appdelete
void appdelete()
Delete and free all memory associated with the application.
Definition global.cpp:134