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
6#include "cantera/base/FactoryBase.h"
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
15 using namespace std;
16
17 namespace Cantera
18{
19
20//! Return a pointer to the application object
21 static Application* app()
22{
23 return Application::Instance();
24}
25
26// **************** Text Logging ****************
27
28 void 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
37 void writelog_direct(const std::string& msg)
38{
39 app()->writelog(msg);
40}
41
42 void writelogendl()
43{
44 app()->writelogendl();
45}
46
47 void writeline(char repeat, size_t count, bool endl_after, bool endl_before)
48{
49 if (endl_before) {
50 writelogendl();
51 }
52 writelog_direct(std::string(count, repeat));
53 if (endl_after) {
54 writelogendl();
55 }
56}
57
58 void _warn_deprecated(const std::string& method, const std::string& extra)
59{
60 app()->warn_deprecated(method, extra);
61}
62
63 void _warn(const std::string& warning,
64 const std::string& method, const std::string& extra)
65{
66 app()->warn(warning, method, extra);
67}
68
69 void suppress_deprecation_warnings()
70{
71 app()->suppress_deprecation_warnings();
72}
73
74 void make_deprecation_warnings_fatal()
75{
76 app()->make_deprecation_warnings_fatal();
77}
78
79 void suppress_warnings()
80{
81 app()->suppress_warnings();
82}
83
84 bool warnings_suppressed()
85{
86 return app()->warnings_suppressed();
87}
88
89 void make_warnings_fatal()
90{
91 app()->make_warnings_fatal();
92}
93
94 void suppress_thermo_warnings(bool suppress)
95{
96 app()->suppress_thermo_warnings(suppress);
97}
98
99 bool thermo_warnings_suppressed()
100{
101 return app()->thermo_warnings_suppressed();
102}
103
104 void use_legacy_rate_constants(bool legacy)
105{
106 app()->use_legacy_rate_constants(legacy);
107}
108
109 bool legacy_rate_constants_used()
110{
111 return app()->legacy_rate_constants_used();
112}
113
114// **************** Global Data ****************
115
116Unit* Unit::s_u = 0;
117std::mutex Unit::units_mutex;
118
119 void appdelete()
120{
121 Application::ApplicationDestroy();
122 FactoryBase::deleteFactories();
123 Unit::deleteUnit();
124}
125
126 void thread_complete()
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
140 XML_Node* get_XML_File(const std::string& file, int debug)
141{
142 return app()->get_XML_File(file, debug);
143}
144
145 XML_Node* get_XML_from_string(const std::string& text)
146{
147 return app()->get_XML_from_string(text);
148}
149
150 void close_XML_File(const std::string& file)
151{
152 app()->close_XML_File(file);
153}
154
155 void 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
190 string canteraRoot()
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
206 bool debugModeEnabled()
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 */
223 static 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
235 XML_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
273 XML_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}
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:399
Cantera::Application
Class to hold global data.
Definition: application.h:47
Cantera::Application::warnings_suppressed
bool warnings_suppressed()
Returns true if warnings should be suppressed.
Definition: application.h:381
Cantera::Application::warn
void warn(const std::string &warning, const std::string &method, const std::string &extra="")
Generate a general purpose warning; repeated warnings are not suppressed.
Definition: application.cpp:188
Cantera::Application::use_legacy_rate_constants
void use_legacy_rate_constants(bool legacy=true)
Set definition used for rate constant calculation.
Definition: application.h:421
Cantera::Application::get_XML_File
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: application.cpp:205
Cantera::Application::suppress_deprecation_warnings
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: application.h:355
Cantera::Application::writelog
void writelog(const std::string &msg)
Write a message to the screen.
Definition: application.h:333
Cantera::Application::thermo_warnings_suppressed
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition: application.h:398
Cantera::Application::make_warnings_fatal
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
Definition: application.h:387
Cantera::Application::setLogger
void setLogger(Logger *logwriter)
Install a logger.
Definition: application.h:431
Cantera::Application::make_deprecation_warnings_fatal
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition: application.h:362
Cantera::Application::Instance
static Application * Instance()
Return a pointer to the one and only instance of class Application.
Definition: application.cpp:149
Cantera::Application::get_XML_from_string
XML_Node * get_XML_from_string(const std::string &text)
Read a CTI or CTML string and fill up an XML tree.
Definition: application.cpp:242
Cantera::Application::legacy_rate_constants_used
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition should be used.
Definition: application.h:426
Cantera::Application::close_XML_File
void close_XML_File(const std::string &file)
Close an XML File.
Definition: application.cpp:262
Cantera::Application::suppress_warnings
void suppress_warnings()
Globally disable printing of (user) warnings.
Definition: application.h:375
Cantera::Application::thread_complete
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition: application.cpp:200
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:393
Cantera::Application::warn_deprecated
void warn_deprecated(const std::string &method, const std::string &extra="")
Print a warning indicating that method is deprecated.
Definition: application.cpp:176
Cantera::Application::writelogendl
void writelogendl()
Write an endl to the screen and flush output.
Definition: application.h:338
Cantera::Application::ApplicationDestroy
static void ApplicationDestroy()
Static function that destroys the application class's data.
Definition: application.cpp:167
Cantera::CanteraError
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Cantera::FactoryBase::s_vFactoryRegistry
static std::vector< FactoryBase * > s_vFactoryRegistry
statically held list of Factories.
Definition: FactoryBase.h:61
Cantera::FactoryBase::deleteFactories
static void deleteFactories()
static function that deletes all factories in the internal registry maintained in a static variable
Definition: FactoryBase.h:36
Cantera::Logger
Base class for 'loggers' that write text messages to log files.
Definition: logger.h:41
Cantera::Logger::error
virtual void error(const std::string &msg)
Write an error message and quit.
Definition: logger.h:91
Cantera::Unit::deleteUnit
static void deleteUnit()
Destroy the static Unit class.
Definition: units.h:42
Cantera::Unit::units_mutex
static std::mutex units_mutex
Decl for static locker for Units singleton.
Definition: units.h:159
Cantera::Unit::actEnergyToSI
doublereal actEnergyToSI(const std::string &units_)
Return the multiplier required to convert an activation energy to SI units.
Definition: units.h:56
Cantera::Unit::toSI
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
Cantera::Unit::s_u
static Unit * s_u
pointer to the single instance of Unit
Definition: units.h:141
Cantera::Unit::units
static Unit * units()
Initialize the static Unit class.
Definition: units.h:30
Cantera::XML_Node
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:103
Cantera::XML_Node::findID
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
Cantera::XML_Node::findNameID
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
Cantera::findInputFile
std::string findInputFile(const std::string &name)
Find an input file.
Definition: global.cpp:165
Cantera::addDirectory
void addDirectory(const std::string &dir)
Add a directory to the data file search path.
Definition: global.cpp:155
Cantera::getDataDirectories
std::string getDataDirectories(const std::string &sep)
Get the Cantera data directories.
Definition: global.cpp:160
Cantera::Application::addDataDirectory
void addDataDirectory(const std::string &dir)
Add a directory to the data file search path.
Definition: application.cpp:397
Cantera::canteraRoot
string canteraRoot()
Returns root directory where Cantera is installed.
Definition: global.cpp:190
Cantera
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:557
Cantera::npos
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:192
Cantera::warnings_suppressed
bool warnings_suppressed()
Returns true if warnings should be suppressed.
Definition: global.cpp:84
Cantera::_warn
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
Cantera::use_legacy_rate_constants
void use_legacy_rate_constants(bool legacy)
Set definition used for rate constant calculation.
Definition: global.cpp:104
Cantera::get_XML_File
XML_Node * get_XML_File(const std::string &file, int debug)
Return a pointer to the XML tree for a Cantera input file.
Definition: global.cpp:140
Cantera::debugModeEnabled
bool debugModeEnabled()
Returns true if Cantera was compiled in debug mode.
Definition: global.cpp:206
Cantera::suppress_deprecation_warnings
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: global.cpp:69
Cantera::thermo_warnings_suppressed
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition: global.cpp:99
Cantera::make_warnings_fatal
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
Definition: global.cpp:89
Cantera::setLogger
void setLogger(Logger *logwriter)
Install a logger.
Definition: global.cpp:28
Cantera::get_XML_Node
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
Cantera::make_deprecation_warnings_fatal
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition: global.cpp:74
Cantera::warn_deprecated
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
Cantera::get_XML_from_string
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
Cantera::actEnergyToSI
doublereal actEnergyToSI(const std::string &unit)
Return the conversion factor to convert activation energy unit std::string 'unit' to Kelvin.
Definition: global.cpp:181
Cantera::legacy_rate_constants_used
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition should be used.
Definition: global.cpp:109
Cantera::gitCommit
std::string gitCommit()
Returns the hash of the git commit from which Cantera was compiled, if known.
Definition: global.cpp:131
Cantera::close_XML_File
void close_XML_File(const std::string &file)
Close an XML File.
Definition: global.cpp:150
Cantera::suppress_warnings
void suppress_warnings()
Globally disable printing of (user) warnings.
Definition: global.cpp:79
Cantera::_warn_deprecated
void _warn_deprecated(const std::string &method, const std::string &extra)
helper function passing deprecation warning to global handler
Definition: global.cpp:58
Cantera::app
static Application * app()
Return a pointer to the application object.
Definition: global.cpp:21
Cantera::thread_complete
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition: global.cpp:126
Cantera::toSI
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string 'unit' to SI units.
Definition: global.cpp:170
Cantera::writelog_direct
void writelog_direct(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:37
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:94
Cantera::split_at_pound
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
Cantera::appdelete
void appdelete()
Delete and free all memory associated with the application.
Definition: global.cpp:119
Cantera::get_XML_NameID
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
Cantera::writelogendl
void writelogendl()
Write an end of line character to the screen and flush output.
Definition: global.cpp:42
Cantera::demangle
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
units.h
Header for units conversion utilities, which are used to translate user input from input files (See I...
xml.h
Classes providing support for XML data files.