Cantera  2.3.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 http://www.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 (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 
60 {
62 }
63 
65 {
67 }
68 
69 void suppress_thermo_warnings(bool suppress)
70 {
71  app()->suppress_thermo_warnings(suppress);
72 }
73 
75 {
76  return app()->thermo_warnings_suppressed();
77 }
78 
79 // **************** Global Data ****************
80 
81 Unit* Unit::s_u = 0;
82 std::mutex Unit::units_mutex;
83 
84 void appdelete()
85 {
86  Application::ApplicationDestroy();
87  FactoryBase::deleteFactories();
88  Unit::deleteUnit();
89 }
90 
92 {
93  app()->thread_complete();
94 }
95 
96 XML_Node* get_XML_File(const std::string& file, int debug)
97 {
98  return app()->get_XML_File(file, debug);
99 }
100 
101 XML_Node* get_XML_from_string(const std::string& text)
102 {
103  return app()->get_XML_from_string(text);
104 }
105 
106 void close_XML_File(const std::string& file)
107 {
108  app()->close_XML_File(file);
109 }
110 
111 int nErrors()
112 {
113  warn_deprecated("nErrors", "To be removed after Cantera 2.3");
114  return app()->getErrorCount();
115 }
116 
117 void popError()
118 {
119  warn_deprecated("popError", "To be removed after Cantera 2.3");
120  app()->popError();
121 }
122 
124 {
125  warn_deprecated("lastErrorMessage", "To be removed after Cantera 2.3");
126  return app()->lastErrorMessage();
127 }
128 
129 void showErrors(std::ostream& f)
130 {
131  warn_deprecated("showErrors", "To be removed after Cantera 2.3");
132  app()->getErrors(f);
133 }
134 
136 {
137  app()->logErrors();
138 }
139 
140 void setError(const std::string& r, const std::string& msg)
141 {
142  app()->addError(r, msg);
143 }
144 
145 void addDirectory(const std::string& dir)
146 {
147  app()->addDataDirectory(dir);
148 }
149 
150 std::string getDataDirectories(const std::string& sep)
151 {
152  return app()->getDataDirectories(sep);
153 }
154 
155 std::string findInputFile(const std::string& name)
156 {
157  return app()->findInputFile(name);
158 }
159 
160 doublereal toSI(const std::string& unit)
161 {
162  doublereal f = Unit::units()->toSI(unit);
163  if (f) {
164  return f;
165  } else {
166  throw CanteraError("toSI","unknown unit string: "+unit);
167  }
168  return 1.0;
169 }
170 
171 doublereal actEnergyToSI(const std::string& unit)
172 {
173  doublereal f = Unit::units()->actEnergyToSI(unit);
174  if (f) {
175  return f;
176  }
177  return 1.0;
178 }
179 
180 string canteraRoot()
181 {
182  char* ctroot = getenv("CANTERA_ROOT");
183  if (ctroot != 0) {
184  return string(ctroot);
185  }
186 #ifdef CANTERA_ROOT
187  return string(CANTERA_ROOT);
188 #else
189  return "";
190 #endif
191 
192 }
193 
194 //! split a string at a '#' sign. Used to separate a file name from an id string.
195 /*!
196  * @param src Original string to be split up. This is unchanged.
197  * @param file Output string representing the first part of the string,
198  * which is the filename.
199  * @param id Output string representing the last part of the string,
200  * which is the id.
201  */
202 static void split_at_pound(const std::string& src, std::string& file, std::string& id)
203 {
204  string::size_type ipound = src.find('#');
205  if (ipound != string::npos) {
206  id = src.substr(ipound+1,src.size());
207  file = src.substr(0,ipound);
208  } else {
209  id = "";
210  file = src;
211  }
212 }
213 
214 XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root)
215 {
216  std::string fname, idstr;
217  XML_Node* db;
218  split_at_pound(file_ID, fname, idstr);
219  if (fname == "") {
220  if (!root) throw CanteraError("get_XML_Node",
221  "no file name given. file_ID = "+file_ID);
222  db = root->findID(idstr, 3);
223  } else {
224  try {
225  findInputFile(fname);
226  } catch (CanteraError& err) {
227  // See if the input file can be found with a different format
228  if (fname.rfind(".xml") == fname.size() - 4) {
229  fname.replace(fname.size() - 3, 3, "cti");
230  } else if (fname.rfind(".cti") == fname.size() - 4) {
231  fname.replace(fname.size() - 3, 3, "xml");
232  }
233  try {
234  findInputFile(fname);
235  } catch (CanteraError&) {
236  // rethrow the original error, which indicates the given file name
237  throw err;
238  }
239  }
240  XML_Node* doc = get_XML_File(fname);
241  if (!doc) throw CanteraError("get_XML_Node",
242  "get_XML_File failed trying to open "+fname);
243  db = doc->findID(idstr, 3);
244  }
245  if (!db) {
246  throw CanteraError("get_XML_Node",
247  "id tag '"+idstr+"' not found.");
248  }
249  return db;
250 }
251 
252 XML_Node* get_XML_NameID(const std::string& nameTarget,
253  const std::string& file_ID,
254  XML_Node* root)
255 {
256  string fname, idTarget;
257  XML_Node* db;
258  split_at_pound(file_ID, fname, idTarget);
259  if (fname == "") {
260  if (!root) {
261  return 0;
262  }
263  db = root->findNameID(nameTarget, idTarget);
264  } else {
265  XML_Node* doc = get_XML_File(fname);
266  if (!doc) {
267  return 0;
268  }
269  db = doc->findNameID(nameTarget, idTarget);
270  }
271  return db;
272 }
273 
274 std::vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
275 
276 }
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:214
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition: application.h:343
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:96
void writelogendl()
Write an endl to the screen and flush output.
Definition: application.h:325
Base class for &#39;loggers&#39; that write text messages to log files.
Definition: logger.h:40
void popError()
Discard the last error message.
Definition: global.cpp:117
Class to hold global data.
Definition: application.h:44
string lastErrorMessage()
Retrieve the last error message in a string.
Definition: global.cpp:123
doublereal actEnergyToSI(const std::string &unit)
Return the conversion factor to convert activation energy unit std::string &#39;unit&#39; to Kelvin...
Definition: global.cpp:171
std::string lastErrorMessage()
Retrieve the last error message in a string.
Definition: application.h:222
std::string findInputFile(const std::string &name)
Find an input file.
Definition: global.cpp:155
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string &#39;unit&#39; to SI units.
Definition: global.cpp:160
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition: global.cpp:91
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:165
void setLogger(Logger *logwriter)
Install a logger.
Definition: global.cpp:24
void setError(const std::string &r, const std::string &msg)
Set an error condition in the application class without throwing an exception.
Definition: global.cpp:140
static Application * app()
Return a pointer to the application object.
Definition: global.cpp:17
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:97
void showErrors()
Prints all of the error messages using writelog.
Definition: global.cpp:135
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:54
STL namespace.
virtual void error(const std::string &msg)
Write an error message and quit.
Definition: logger.h:81
void writelog(const std::string &msg)
Write a message to the screen.
Definition: application.h:320
XML_Node * get_XML_from_string(const std::string &text)
Read a CTI or CTML string and fill up an XML tree.
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:101
void suppress_thermo_warnings(bool suppress)
Globally disable printing of warnings about problematic thermo data, e.g.
Definition: global.cpp:69
void close_XML_File(const std::string &file)
Close an XML File.
Definition: global.cpp:106
Classes providing support for XML data files.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: application.h:337
void warn_deprecated(const std::string &method, const std::string &extra="")
Print a warning indicating that method is deprecated.
Header for units conversion utilities, which are used to translate user input from input files (See I...
void appdelete()
Delete and free all memory associated with the application.
Definition: global.cpp:84
void popError()
Discard the last error message.
Definition: application.h:217
XML_Node * get_XML_File(const std::string &file, int debug=0)
Return a pointer to the XML tree for a Cantera input file.
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
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
void writelogendl()
Write an end of line character to the screen and flush output.
Definition: global.cpp:38
void logErrors()
Prints all of the error messages using writelog.
Definition: application.h:232
void getErrors(std::ostream &f)
Prints all of the error messages to an ostream.
Definition: application.h:227
void addError(const std::string &r, const std::string &msg="")
Set an error condition in the application class without throwing an exception.
Definition: application.h:207
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition: global.cpp:74
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: global.cpp:59
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
Definition: global.cpp:64
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
Definition: application.h:354
string canteraRoot()
Returns root directory where Cantera is installed.
Definition: global.cpp:180
void addDataDirectory(const std::string &dir)
Add a directory to the data file search path.
Namespace for the Cantera kernel.
Definition: application.cpp:29
int nErrors()
Return the number of errors that have been encountered so far.
Definition: global.cpp:111
void suppress_thermo_warnings(bool suppress=true)
Globally disable printing of warnings about problematic thermo data, e.g.
Definition: application.h:349
void addDirectory(const std::string &dir)
Add a directory to the data file search path.
Definition: global.cpp:145
int getErrorCount()
Return the number of errors that have been encountered so far.
Definition: application.h:212
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:252
File contains the FactoryBase class declarations.
void writelog_direct(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:33
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...
Definition: xml.cpp:645
static void split_at_pound(const std::string &src, std::string &file, std::string &id)
split a string at a &#39;#&#39; sign. Used to separate a file name from an id string.
Definition: global.cpp:202
void setLogger(Logger *logwriter)
Install a logger.
Definition: application.h:359
void close_XML_File(const std::string &file)
Close an XML File.
std::string getDataDirectories(const std::string &sep)
Get the Cantera data directories.
Definition: global.cpp:150