Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
global.cpp
Go to the documentation of this file.
1 //! @file global.cpp
2 
4 #include "cantera/base/xml.h"
5 #include "application.h"
6 #include "units.h"
7 
8 #include <cstdio>
9 #include <stdarg.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(const std::string& msg)
34 {
35  app()->writelog(msg);
36 }
37 
38 void writelogf(const char* fmt,...)
39 {
40  enum { BUFSIZE = 2048 } ;
41  char sbuf[BUFSIZE] ;
42 
43  va_list args ;
44  va_start(args, fmt) ;
45 
46 #ifdef _MSC_VER
47  _vsnprintf(sbuf, BUFSIZE, fmt, args) ;
48 #else
49  vsprintf(sbuf, fmt, args) ;
50 #endif
51 
52  writelog(sbuf) ;
53  va_end(args) ;
54 }
55 
57 {
58  app()->writelogendl();
59 }
60 
61 void writeline(char repeat, size_t count, bool endl_after, bool endl_before)
62 {
63  if (endl_before) {
64  writelogendl();
65  }
66  writelog(std::string(count, repeat));
67  if (endl_after) {
68  writelogendl();
69  }
70 }
71 
72 void error(const std::string& msg)
73 {
74  warn_deprecated("error", "To be removed after Cantera 2.2");
75  app()->logerror(msg);
76 }
77 
78 void warn_deprecated(const std::string& method, const std::string& extra)
79 {
80  app()->warn_deprecated(method, extra);
81 }
82 
84 {
86 }
87 
88 // **************** Global Data ****************
89 
90 Unit* Unit::s_u = 0;
91 mutex_t Unit::units_mutex;
92 
93 void appdelete()
94 {
95  Application::ApplicationDestroy() ;
96  FactoryBase::deleteFactories();
97  Unit::deleteUnit();
98 }
99 
101 {
102  app()->thread_complete() ;
103 }
104 
105 XML_Node* get_XML_File(const std::string& file, int debug)
106 {
107  XML_Node* xtmp = app()->get_XML_File(file, debug) ;
108  return xtmp;
109 }
110 
111 XML_Node* get_XML_from_string(const std::string& text)
112 {
113  return app()->get_XML_from_string(text);
114 }
115 
116 void close_XML_File(const std::string& file)
117 {
118  app()->close_XML_File(file) ;
119 }
120 
121 int nErrors()
122 {
123  return app()->getErrorCount();
124 }
125 
126 void popError()
127 {
128  app()->popError() ;
129 }
130 
132 {
133  return app()->lastErrorMessage() ;
134 }
135 
136 void showErrors(std::ostream& f)
137 {
138  app()->getErrors(f) ;
139 }
140 
142 {
143  app()->logErrors() ;
144 }
145 
146 void setError(const std::string& r, const std::string& msg)
147 {
148  app()->addError(r, msg) ;
149 }
150 
151 void addDirectory(const std::string& dir)
152 {
153  app()->addDataDirectory(dir) ;
154 }
155 
156 std::string findInputFile(const std::string& name)
157 {
158  return app()->findInputFile(name) ;
159 }
160 
161 doublereal toSI(const std::string& unit)
162 {
163  doublereal f = Unit::units()->toSI(unit);
164  if (f) {
165  return f;
166  } else {
167  throw CanteraError("toSI","unknown unit string: "+unit);
168  }
169  return 1.0;
170 }
171 
172 doublereal actEnergyToSI(const std::string& unit)
173 {
174  doublereal f = Unit::units()->actEnergyToSI(unit);
175  if (f) {
176  return f;
177  }
178  return 1.0;
179 }
180 
181 string canteraRoot()
182 {
183  char* ctroot = 0;
184  ctroot = getenv("CANTERA_ROOT");
185  if (ctroot != 0) {
186  return string(ctroot);
187  }
188 #ifdef CANTERA_ROOT
189  return string(CANTERA_ROOT);
190 #else
191  return "";
192 #endif
193 
194 }
195 
196 //! split a string at a '#' sign. Used to separate a file name from an id string.
197 /*!
198  * @param src Original string to be split up. This is unchanged.
199  * @param file Output string representing the first part of the string, which is the filename.
200  * @param id Output string representing the last part of the string, 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, *doc;
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  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, *doc;
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  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 addError(const std::string &r, const std::string &msg)
Set an error condition in the application class without throwing an exception.
Definition: application.h:227
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:615
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:105
void writelogendl()
Write an endl to the screen and flush output.
Definition: application.h:333
Base class for 'loggers' that write text messages to log files.
Definition: logger.h:39
void popError()
Discard the last error message.
Definition: global.cpp:126
Class to hold global data.
Definition: application.h:40
string lastErrorMessage()
Retrieve the last error message in a string.
Definition: global.cpp:131
doublereal actEnergyToSI(const std::string &unit)
Return the conversion factor to convert activation energy unit std::string 'unit' to Kelvin...
Definition: global.cpp:172
std::string lastErrorMessage()
Retrieve the last error message in a string.
Definition: application.h:242
std::string findInputFile(const std::string &name)
Find an input file.
Definition: global.cpp:156
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string 'unit' to SI units.
Definition: global.cpp:161
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition: global.cpp:100
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
static std::string fmt(const std::string &r, size_t n)
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:146
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:100
void showErrors()
Prints all of the error messages using writelog.
Definition: global.cpp:141
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:78
virtual void error(const std::string &msg)
Write an error message and quit.
Definition: logger.h:84
void writelog(const std::string &msg)
Write a message to the screen.
Definition: application.h:328
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:111
void logerror(const std::string &msg)
Write an error message and quit.
Definition: application.h:338
void close_XML_File(const std::string &file)
Close an XML File.
Definition: global.cpp:116
void writelogf(const char *fmt,...)
Write a formatted message to the screen.
Definition: global.cpp:38
void error(const std::string &msg)
Write an error message and quit.
Definition: global.cpp:72
Classes providing support for XML data files.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: application.h:350
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:93
void popError()
Discard the last error message.
Definition: application.h:237
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 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:56
void logErrors()
Prints all of the error messages using writelog.
Definition: application.h:252
void getErrors(std::ostream &f)
Prints all of the error messages to an ostream.
Definition: application.h:247
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: global.cpp:83
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:685
string canteraRoot()
Returns root directory where Cantera is installed.
Definition: global.cpp:181
void addDataDirectory(const std::string &dir)
Add a directory to the data file search path.
void writelog(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:33
int nErrors()
Return the number of errors that have been encountered so far.
Definition: global.cpp:121
void addDirectory(const std::string &dir)
Add a directory to the data file search path.
Definition: global.cpp:151
int getErrorCount()
Return the number of errors that have been encountered so far.
Definition: application.h:232
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.
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:202
void setLogger(Logger *logwriter)
Install a logger.
Definition: application.h:355
void close_XML_File(const std::string &file)
Close an XML File.