Cantera 2.6.0
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 "cantera/base/xml.h"
8#include "application.h"
9#include "units.h"
10#include "cantera/base/AnyMap.h"
11#ifdef CT_USE_DEMANGLE
12 #include <boost/core/demangle.hpp>
13#endif
14
15using namespace std;
16
17namespace Cantera
18{
19
20//! Return a pointer to the application object
22{
23 return Application::Instance();
24}
25
26// **************** Text Logging ****************
27
28void setLogger(Logger* logwriter)
29{
30 try {
31 app()->setLogger(logwriter);
32 } catch (const std::bad_alloc&) {
33 logwriter->error("bad alloc thrown by app()");
34 }
35}
36
37void writelog_direct(const std::string& msg)
38{
39 app()->writelog(msg);
40}
41
43{
44 app()->writelogendl();
45}
46
47void writeline(char repeat, size_t count, bool endl_after, bool endl_before)
48{
49 if (endl_before) {
51 }
52 writelog_direct(std::string(count, repeat));
53 if (endl_after) {
55 }
56}
57
58void _warn_deprecated(const std::string& method, const std::string& extra)
59{
60 app()->warn_deprecated(method, extra);
61}
62
63void _warn(const std::string& warning,
64 const std::string& method, const std::string& extra)
65{
66 app()->warn(warning, method, extra);
67}
68
70{
72}
73
75{
77}
78
80{
82}
83
85{
86 return app()->warnings_suppressed();
87}
88
90{
92}
93
94void suppress_thermo_warnings(bool suppress)
95{
96 app()->suppress_thermo_warnings(suppress);
97}
98
100{
102}
103
105{
107}
108
110{
112}
113
114// **************** Global Data ****************
115
116Unit* Unit::s_u = 0;
117std::mutex Unit::units_mutex;
118
120{
124}
125
127{
128 app()->thread_complete();
129}
130
131std::string gitCommit()
132{
133#ifdef GIT_COMMIT
134 return GIT_COMMIT;
135#else
136 return "unknown";
137#endif
138}
139
140XML_Node* get_XML_File(const std::string& file, int debug)
141{
142 return app()->get_XML_File(file, debug);
143}
144
145XML_Node* get_XML_from_string(const std::string& text)
146{
147 return app()->get_XML_from_string(text);
148}
149
150void close_XML_File(const std::string& file)
151{
152 app()->close_XML_File(file);
153}
154
155void addDirectory(const std::string& dir)
156{
157 app()->addDataDirectory(dir);
158}
159
160std::string getDataDirectories(const std::string& sep)
161{
162 return app()->getDataDirectories(sep);
163}
164
165std::string findInputFile(const std::string& name)
166{
167 return app()->findInputFile(name);
168}
169
170doublereal toSI(const std::string& unit)
171{
172 doublereal f = Unit::units()->toSI(unit);
173 if (f) {
174 return f;
175 } else {
176 throw CanteraError("toSI","unknown unit string: "+unit);
177 }
178 return 1.0;
179}
180
181doublereal actEnergyToSI(const std::string& unit)
182{
183 doublereal f = Unit::units()->actEnergyToSI(unit);
184 if (f) {
185 return f;
186 }
187 return 1.0;
188}
189
191{
192 warn_deprecated("canteraRoot",
193 "Unused in Cantera. To be removed after Cantera 2.6");
194 char* ctroot = getenv("CANTERA_ROOT");
195 if (ctroot != 0) {
196 return string(ctroot);
197 }
198#ifdef CANTERA_ROOT
199 return string(CANTERA_ROOT);
200#else
201 return "";
202#endif
203
204}
205
207{
208#ifdef NDEBUG
209 return false;
210#else
211 return true;
212#endif
213}
214
215//! split a string at a '#' sign. Used to separate a file name from an id string.
216/*!
217 * @param src Original string to be split up. This is unchanged.
218 * @param file Output string representing the first part of the string,
219 * which is the filename.
220 * @param id Output string representing the last part of the string,
221 * which is the id.
222 */
223static void split_at_pound(const std::string& src, std::string& file, std::string& id)
224{
225 string::size_type ipound = src.find('#');
226 if (ipound != string::npos) {
227 id = src.substr(ipound+1,src.size());
228 file = src.substr(0,ipound);
229 } else {
230 id = "";
231 file = src;
232 }
233}
234
235XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root)
236{
237 std::string fname, idstr;
238 XML_Node* db;
239 split_at_pound(file_ID, fname, idstr);
240 if (fname == "") {
241 if (!root) throw CanteraError("get_XML_Node",
242 "no file name given. file_ID = "+file_ID);
243 db = root->findID(idstr, 3);
244 } else {
245 try {
246 findInputFile(fname);
247 } catch (CanteraError& err) {
248 // See if the input file can be found with a different format
249 if (fname.rfind(".xml") == fname.size() - 4) {
250 fname.replace(fname.size() - 3, 3, "cti");
251 } else if (fname.rfind(".cti") == fname.size() - 4) {
252 fname.replace(fname.size() - 3, 3, "xml");
253 }
254 try {
255 findInputFile(fname);
256 } catch (CanteraError&) {
257 // rethrow the original error, which indicates the given file name
258 throw err;
259 }
260 }
261 XML_Node* doc = get_XML_File(fname);
262 if (!doc) throw CanteraError("get_XML_Node",
263 "get_XML_File failed trying to open "+fname);
264 db = doc->findID(idstr, 3);
265 }
266 if (!db) {
267 throw CanteraError("get_XML_Node",
268 "id tag '"+idstr+"' not found.");
269 }
270 return db;
271}
272
273XML_Node* get_XML_NameID(const std::string& nameTarget,
274 const std::string& file_ID,
275 XML_Node* root)
276{
277 string fname, idTarget;
278 XML_Node* db;
279 split_at_pound(file_ID, fname, idTarget);
280 if (fname == "") {
281 if (!root) {
282 return 0;
283 }
284 db = root->findNameID(nameTarget, idTarget);
285 } else {
286 XML_Node* doc = get_XML_File(fname);
287 if (!doc) {
288 return 0;
289 }
290 db = doc->findNameID(nameTarget, idTarget);
291 }
292 return db;
293}
294
295std::vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
296
297std::string demangle(const std::type_info& type)
298{
299 static std::map<std::string, std::string> typenames = {
300 {typeid(void).name(), "void"},
301 {typeid(double).name(), "double"},
302 {typeid(long int).name(), "long int"},
303 {typeid(bool).name(), "bool"},
304 {typeid(std::string).name(), "string"},
305 {typeid(vector<AnyValue>).name(), "vector<AnyValue>"},
306 {typeid(vector<AnyMap>).name(), "vector<AnyMap>"},
307 {typeid(vector<double>).name(), "vector<double>"},
308 {typeid(vector<long int>).name(), "vector<long int>"},
309 {typeid(vector<bool>).name(), "vector<bool>"},
310 {typeid(vector<string>).name(), "vector<string>"},
311 {typeid(vector<vector<double>>).name(), "vector<vector<double>>"},
312 {typeid(vector<vector<long int>>).name(), "vector<vector<long int>>"},
313 {typeid(vector<vector<bool>>).name(), "vector<vector<bool>>"},
314 {typeid(vector<vector<string>>).name(), "vector<vector<string>>"},
315 {typeid(AnyMap).name(), "AnyMap"},
316 };
317
318 if (typenames.count(type.name())) {
319 return typenames[type.name()];
320 } else {
321 #ifdef CT_USE_DEMANGLE
322 return boost::core::demangle(type.name());
323 #else
324 return type.name();
325 #endif
326 }
327}
328
329}
File contains the FactoryBase class declarations.
A map of string keys to values whose type can vary at runtime.
Definition: AnyMap.h:399
Class to hold global data.
Definition: application.h:47
bool warnings_suppressed()
Returns true if warnings should be suppressed.
Definition: application.h:381
void warn(const std::string &warning, const std::string &method, const std::string &extra="")
Generate a general purpose warning; repeated warnings are not suppressed.
void use_legacy_rate_constants(bool legacy=true)
Set definition used for rate constant calculation.
Definition: application.h:421
XML_Node * get_XML_File(const std::string &file, int debug=0)
Return a pointer to the XML tree for a Cantera input file.
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: application.h:355
void writelog(const std::string &msg)
Write a message to the screen.
Definition: application.h:333
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition: application.h:398
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
Definition: application.h:387
void setLogger(Logger *logwriter)
Install a logger.
Definition: application.h:431
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition: application.h:362
static Application * Instance()
Return a pointer to the one and only instance of class Application.
XML_Node * get_XML_from_string(const std::string &text)
Read a CTI or CTML string and fill up an XML tree.
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition should be used.
Definition: application.h:426
void close_XML_File(const std::string &file)
Close an XML File.
void suppress_warnings()
Globally disable printing of (user) warnings.
Definition: application.h:375
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...
Definition: application.h:393
void warn_deprecated(const std::string &method, const std::string &extra="")
Print a warning indicating that method is deprecated.
void writelogendl()
Write an endl to the screen and flush output.
Definition: application.h:338
static void ApplicationDestroy()
Static function that destroys the application class's data.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
static std::vector< FactoryBase * > s_vFactoryRegistry
statically held list of Factories.
Definition: FactoryBase.h:61
static void deleteFactories()
static function that deletes all factories in the internal registry maintained in a static variable
Definition: FactoryBase.h:36
Base class for 'loggers' that write text messages to log files.
Definition: logger.h:41
virtual void error(const std::string &msg)
Write an error message and quit.
Definition: logger.h:91
static void deleteUnit()
Destroy the static Unit class.
Definition: units.h:42
static std::mutex units_mutex
Decl for static locker for Units singleton.
Definition: units.h:159
doublereal actEnergyToSI(const std::string &units_)
Return the multiplier required to convert an activation energy to SI units.
Definition: units.h:56
doublereal toSI(const std::string &units_)
Return the multiplier required to convert a dimensional quantity with units specified by string 'unit...
Definition: units.h:72
static Unit * s_u
pointer to the single instance of Unit
Definition: units.h:141
static Unit * units()
Initialize the static Unit class.
Definition: units.h:30
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:103
XML_Node * findID(const std::string &id, const int depth=100) const
This routine carries out a recursive search for an XML node based on the XML element attribute "id".
Definition: xml.cpp:646
XML_Node * findNameID(const std::string &nameTarget, const std::string &idTarget) const
This routine carries out a recursive search for an XML node based on both the XML element name and th...
Definition: xml.cpp:587
std::string findInputFile(const std::string &name)
Find an input file.
Definition: global.cpp:165
void addDirectory(const std::string &dir)
Add a directory to the data file search path.
Definition: global.cpp:155
std::string getDataDirectories(const std::string &sep)
Get the Cantera data directories.
Definition: global.cpp:160
void addDataDirectory(const std::string &dir)
Add a directory to the data file search path.
std::string canteraRoot()
Returns root directory where Cantera is installed.
Definition: global.cpp:190
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:192
bool warnings_suppressed()
Returns true if warnings should be suppressed.
Definition: global.cpp:84
void _warn(const std::string &warning, const std::string &method, const std::string &extra)
helper function passing generic warning to global handler
Definition: global.cpp:63
void use_legacy_rate_constants(bool legacy=true)
Set definition used for rate constant calculation.
Definition: global.cpp:104
XML_Node * get_XML_File(const std::string &file, int debug=0)
Return a pointer to the XML tree for a Cantera input file.
Definition: global.cpp:140
bool debugModeEnabled()
Returns true if Cantera was compiled in debug mode.
Definition: global.cpp:206
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: global.cpp:69
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition: global.cpp:99
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
Definition: global.cpp:89
void setLogger(Logger *logwriter)
Install a logger.
Definition: global.cpp:28
XML_Node * get_XML_Node(const std::string &file_ID, XML_Node *root)
This routine will locate an XML node in either the input XML tree or in another input file specified ...
Definition: global.cpp:235
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition: global.cpp:74
void warn_deprecated(const std::string &source, const AnyBase &node, const std::string &message)
A deprecation warning for syntax in an input file.
Definition: AnyMap.cpp:1901
XML_Node * get_XML_from_string(const std::string &text)
Read a CTI or CTML string and fill up an XML tree.
Definition: global.cpp:145
doublereal actEnergyToSI(const std::string &unit)
Return the conversion factor to convert activation energy unit std::string 'unit' to Kelvin.
Definition: global.cpp:181
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition should be used.
Definition: global.cpp:109
std::string gitCommit()
Returns the hash of the git commit from which Cantera was compiled, if known.
Definition: global.cpp:131
void close_XML_File(const std::string &file)
Close an XML File.
Definition: global.cpp:150
void suppress_warnings()
Globally disable printing of (user) warnings.
Definition: global.cpp:79
void _warn_deprecated(const std::string &method, const std::string &extra="")
helper function passing deprecation warning to global handler
Definition: global.cpp:58
static Application * app()
Return a pointer to the application object.
Definition: global.cpp:21
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition: global.cpp:126
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string 'unit' to SI units.
Definition: global.cpp:170
void writelog_direct(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:37
void suppress_thermo_warnings(bool suppress=true)
Globally disable printing of warnings about problematic thermo data, such as NASA polynomials with di...
Definition: global.cpp:94
void appdelete()
Delete and free all memory associated with the application.
Definition: global.cpp:119
XML_Node * get_XML_NameID(const std::string &nameTarget, const std::string &file_ID, XML_Node *root)
This routine will locate an XML node in either the input XML tree or in another input file specified ...
Definition: global.cpp:273
void writelogendl()
Write an end of line character to the screen and flush output.
Definition: global.cpp:42
static void split_at_pound(const std::string &src, std::string &file, std::string &id)
split a string at a '#' sign. Used to separate a file name from an id string.
Definition: global.cpp:223
std::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:297
Header for units conversion utilities, which are used to translate user input from input files (See I...
Classes providing support for XML data files.