Cantera  2.5.1
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 
11 using namespace std;
12 
13 namespace Cantera
14 {
15 
16 //! Return a pointer to the application object
17 static Application* app()
18 {
19  return Application::Instance();
20 }
21 
22 // **************** Text Logging ****************
23 
24 void setLogger(Logger* logwriter)
25 {
26  try {
27  app()->setLogger(logwriter);
28  } catch (const std::bad_alloc&) {
29  logwriter->error("bad alloc thrown by app()");
30  }
31 }
32 
33 void writelog_direct(const std::string& msg)
34 {
35  app()->writelog(msg);
36 }
37 
39 {
40  app()->writelogendl();
41 }
42 
43 void writeline(char repeat, size_t count, bool endl_after, bool endl_before)
44 {
45  if (endl_before) {
46  writelogendl();
47  }
48  writelog_direct(std::string(count, repeat));
49  if (endl_after) {
50  writelogendl();
51  }
52 }
53 
54 void warn_deprecated(const std::string& method, const std::string& extra)
55 {
56  app()->warn_deprecated(method, extra);
57 }
58 
59 void _warn_user(const std::string& method, const std::string& extra)
60 {
61  app()->warn_user(method, extra);
62 }
63 
65 {
67 }
68 
70 {
72 }
73 
74 void suppress_thermo_warnings(bool suppress)
75 {
76  app()->suppress_thermo_warnings(suppress);
77 }
78 
80 {
81  return app()->thermo_warnings_suppressed();
82 }
83 
84 // **************** Global Data ****************
85 
86 Unit* Unit::s_u = 0;
87 std::mutex Unit::units_mutex;
88 
89 void appdelete()
90 {
91  Application::ApplicationDestroy();
92  FactoryBase::deleteFactories();
93  Unit::deleteUnit();
94 }
95 
97 {
98  app()->thread_complete();
99 }
100 
101 std::string gitCommit()
102 {
103 #ifdef GIT_COMMIT
104  return GIT_COMMIT;
105 #else
106  return "unknown";
107 #endif
108 }
109 
110 XML_Node* get_XML_File(const std::string& file, int debug)
111 {
112  return app()->get_XML_File(file, debug);
113 }
114 
115 XML_Node* get_XML_from_string(const std::string& text)
116 {
117  return app()->get_XML_from_string(text);
118 }
119 
120 void close_XML_File(const std::string& file)
121 {
122  app()->close_XML_File(file);
123 }
124 
125 void addDirectory(const std::string& dir)
126 {
127  app()->addDataDirectory(dir);
128 }
129 
130 std::string getDataDirectories(const std::string& sep)
131 {
132  return app()->getDataDirectories(sep);
133 }
134 
135 std::string findInputFile(const std::string& name)
136 {
137  return app()->findInputFile(name);
138 }
139 
140 doublereal toSI(const std::string& unit)
141 {
142  doublereal f = Unit::units()->toSI(unit);
143  if (f) {
144  return f;
145  } else {
146  throw CanteraError("toSI","unknown unit string: "+unit);
147  }
148  return 1.0;
149 }
150 
151 doublereal actEnergyToSI(const std::string& unit)
152 {
153  doublereal f = Unit::units()->actEnergyToSI(unit);
154  if (f) {
155  return f;
156  }
157  return 1.0;
158 }
159 
160 string canteraRoot()
161 {
162  char* ctroot = getenv("CANTERA_ROOT");
163  if (ctroot != 0) {
164  return string(ctroot);
165  }
166 #ifdef CANTERA_ROOT
167  return string(CANTERA_ROOT);
168 #else
169  return "";
170 #endif
171 
172 }
173 
174 //! split a string at a '#' sign. Used to separate a file name from an id string.
175 /*!
176  * @param src Original string to be split up. This is unchanged.
177  * @param file Output string representing the first part of the string,
178  * which is the filename.
179  * @param id Output string representing the last part of the string,
180  * which is the id.
181  */
182 static void split_at_pound(const std::string& src, std::string& file, std::string& id)
183 {
184  string::size_type ipound = src.find('#');
185  if (ipound != string::npos) {
186  id = src.substr(ipound+1,src.size());
187  file = src.substr(0,ipound);
188  } else {
189  id = "";
190  file = src;
191  }
192 }
193 
194 XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root)
195 {
196  std::string fname, idstr;
197  XML_Node* db;
198  split_at_pound(file_ID, fname, idstr);
199  if (fname == "") {
200  if (!root) throw CanteraError("get_XML_Node",
201  "no file name given. file_ID = "+file_ID);
202  db = root->findID(idstr, 3);
203  } else {
204  try {
205  findInputFile(fname);
206  } catch (CanteraError& err) {
207  // See if the input file can be found with a different format
208  if (fname.rfind(".xml") == fname.size() - 4) {
209  fname.replace(fname.size() - 3, 3, "cti");
210  } else if (fname.rfind(".cti") == fname.size() - 4) {
211  fname.replace(fname.size() - 3, 3, "xml");
212  }
213  try {
214  findInputFile(fname);
215  } catch (CanteraError&) {
216  // rethrow the original error, which indicates the given file name
217  throw err;
218  }
219  }
220  XML_Node* doc = get_XML_File(fname);
221  if (!doc) throw CanteraError("get_XML_Node",
222  "get_XML_File failed trying to open "+fname);
223  db = doc->findID(idstr, 3);
224  }
225  if (!db) {
226  throw CanteraError("get_XML_Node",
227  "id tag '"+idstr+"' not found.");
228  }
229  return db;
230 }
231 
232 XML_Node* get_XML_NameID(const std::string& nameTarget,
233  const std::string& file_ID,
234  XML_Node* root)
235 {
236  string fname, idTarget;
237  XML_Node* db;
238  split_at_pound(file_ID, fname, idTarget);
239  if (fname == "") {
240  if (!root) {
241  return 0;
242  }
243  db = root->findNameID(nameTarget, idTarget);
244  } else {
245  XML_Node* doc = get_XML_File(fname);
246  if (!doc) {
247  return 0;
248  }
249  db = doc->findNameID(nameTarget, idTarget);
250  }
251  return db;
252 }
253 
254 std::vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
255 
256 }
File contains the FactoryBase class declarations.
Class to hold global data.
Definition: application.h:47
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:342
void writelog(const std::string &msg)
Write a message to the screen.
Definition: application.h:325
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition: application.h:364
void setLogger(Logger *logwriter)
Install a logger.
Definition: application.h:369
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition: application.h:349
XML_Node * get_XML_from_string(const std::string &text)
Read a CTI or CTML string and fill up an XML tree.
void close_XML_File(const std::string &file)
Close an XML File.
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, e.g.
Definition: application.h:359
void warn_deprecated(const std::string &method, const std::string &extra="")
Print a warning indicating that method is deprecated.
void warn_user(const std::string &method, const std::string &extra="")
Print a user warning arising during usage of method.
void writelogendl()
Write an endl to the screen and flush output.
Definition: application.h:330
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
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:81
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:104
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:645
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:586
std::string findInputFile(const std::string &name)
Find an input file.
Definition: global.cpp:135
void addDirectory(const std::string &dir)
Add a directory to the data file search path.
Definition: global.cpp:125
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:110
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: global.cpp:64
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition: global.cpp:79
void setLogger(Logger *logwriter)
Install a logger.
Definition: global.cpp:24
std::string getDataDirectories(const std::string &sep)
Get the Cantera data directories.
Definition: global.cpp:130
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:194
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition: global.cpp:69
void addDataDirectory(const std::string &dir)
Add a directory to the data file search path.
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:115
doublereal actEnergyToSI(const std::string &unit)
Return the conversion factor to convert activation energy unit std::string 'unit' to Kelvin.
Definition: global.cpp:151
std::string gitCommit()
Returns the hash of the git commit from which Cantera was compiled, if known.
Definition: global.cpp:101
void close_XML_File(const std::string &file)
Close an XML File.
Definition: global.cpp:120
void _warn_user(const std::string &method, const std::string &extra)
helper function passing user warning to global handler
Definition: global.cpp:59
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition: global.cpp:96
string canteraRoot()
Returns root directory where Cantera is installed.
Definition: global.cpp:160
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string 'unit' to SI units.
Definition: global.cpp:140
void writelog_direct(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:33
void suppress_thermo_warnings(bool suppress)
Globally disable printing of warnings about problematic thermo data, e.g.
Definition: global.cpp:74
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:54
void appdelete()
Delete and free all memory associated with the application.
Definition: global.cpp:89
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:232
void writelogendl()
Write an end of line character to the screen and flush output.
Definition: global.cpp:38
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:188
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
static Application * app()
Return a pointer to the application object.
Definition: global.cpp:17
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:182
Header for units conversion utilities, which are used to translate user input from input files (See I...
Classes providing support for XML data files.