Cantera  3.1.0a1
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"
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 
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 
68 {
70 }
71 
73 {
75 }
76 
78 {
80 }
81 
83 {
84  return app()->warnings_suppressed();
85 }
86 
88 {
90 }
91 
92 void suppress_thermo_warnings(bool suppress)
93 {
94  app()->suppress_thermo_warnings(suppress);
95 }
96 
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 
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 
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 {
138 }
139 
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 
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 
211 vector<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 }
File contains the FactoryBase class declarations.
A map of string keys to values whose type can vary at runtime.
Definition: AnyMap.h:427
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:1423
Class to hold global data.
Definition: application.h:42
bool warnings_suppressed()
Returns true if warnings should be suppressed.
Definition: application.h:352
void use_legacy_rate_constants(bool legacy=true)
Set definition used for rate constant calculation.
Definition: application.h:383
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: application.h:327
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition: application.h:369
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
Definition: application.h:358
void setLogger(Logger *logwriter)
Install a logger.
Definition: application.h:393
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition: application.h:334
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.
Definition: application.h:388
void suppress_warnings()
Globally disable printing of (user) warnings.
Definition: application.h:346
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
void writelog(const string &msg)
Write a message to the screen.
Definition: application.h:305
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
void writelogendl()
Write an endl to the screen and flush output.
Definition: application.h:310
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:41
virtual void error(const string &msg)
Write an error message and quit.
Definition: logger.h:91
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 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 addDataDirectory(const string &dir)
Add a directory to the data file search path.
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 writelogendl()
Write an end of line character to the screen and flush output.
Definition: global.cpp:41
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:564
static Application * app()
Return a pointer to the application object.
Definition: global.cpp:20
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 appdelete()
Delete and free all memory associated with the application.
Definition: global.cpp:134