Cantera  2.4.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 std::string gitCommit()
97 {
98 #ifdef GIT_COMMIT
99  return GIT_COMMIT;
100 #else
101  return "unknown";
102 #endif
103 }
104 
105 XML_Node* get_XML_File(const std::string& file, int debug)
106 {
107  return app()->get_XML_File(file, debug);
108 }
109 
110 XML_Node* get_XML_from_string(const std::string& text)
111 {
112  return app()->get_XML_from_string(text);
113 }
114 
115 void close_XML_File(const std::string& file)
116 {
117  app()->close_XML_File(file);
118 }
119 
120 void addDirectory(const std::string& dir)
121 {
122  app()->addDataDirectory(dir);
123 }
124 
125 std::string getDataDirectories(const std::string& sep)
126 {
127  return app()->getDataDirectories(sep);
128 }
129 
130 std::string findInputFile(const std::string& name)
131 {
132  return app()->findInputFile(name);
133 }
134 
135 doublereal toSI(const std::string& unit)
136 {
137  doublereal f = Unit::units()->toSI(unit);
138  if (f) {
139  return f;
140  } else {
141  throw CanteraError("toSI","unknown unit string: "+unit);
142  }
143  return 1.0;
144 }
145 
146 doublereal actEnergyToSI(const std::string& unit)
147 {
148  doublereal f = Unit::units()->actEnergyToSI(unit);
149  if (f) {
150  return f;
151  }
152  return 1.0;
153 }
154 
155 string canteraRoot()
156 {
157  char* ctroot = getenv("CANTERA_ROOT");
158  if (ctroot != 0) {
159  return string(ctroot);
160  }
161 #ifdef CANTERA_ROOT
162  return string(CANTERA_ROOT);
163 #else
164  return "";
165 #endif
166 
167 }
168 
169 //! split a string at a '#' sign. Used to separate a file name from an id string.
170 /*!
171  * @param src Original string to be split up. This is unchanged.
172  * @param file Output string representing the first part of the string,
173  * which is the filename.
174  * @param id Output string representing the last part of the string,
175  * which is the id.
176  */
177 static void split_at_pound(const std::string& src, std::string& file, std::string& id)
178 {
179  string::size_type ipound = src.find('#');
180  if (ipound != string::npos) {
181  id = src.substr(ipound+1,src.size());
182  file = src.substr(0,ipound);
183  } else {
184  id = "";
185  file = src;
186  }
187 }
188 
189 XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root)
190 {
191  std::string fname, idstr;
192  XML_Node* db;
193  split_at_pound(file_ID, fname, idstr);
194  if (fname == "") {
195  if (!root) throw CanteraError("get_XML_Node",
196  "no file name given. file_ID = "+file_ID);
197  db = root->findID(idstr, 3);
198  } else {
199  try {
200  findInputFile(fname);
201  } catch (CanteraError& err) {
202  // See if the input file can be found with a different format
203  if (fname.rfind(".xml") == fname.size() - 4) {
204  fname.replace(fname.size() - 3, 3, "cti");
205  } else if (fname.rfind(".cti") == fname.size() - 4) {
206  fname.replace(fname.size() - 3, 3, "xml");
207  }
208  try {
209  findInputFile(fname);
210  } catch (CanteraError&) {
211  // rethrow the original error, which indicates the given file name
212  throw err;
213  }
214  }
215  XML_Node* doc = get_XML_File(fname);
216  if (!doc) throw CanteraError("get_XML_Node",
217  "get_XML_File failed trying to open "+fname);
218  db = doc->findID(idstr, 3);
219  }
220  if (!db) {
221  throw CanteraError("get_XML_Node",
222  "id tag '"+idstr+"' not found.");
223  }
224  return db;
225 }
226 
227 XML_Node* get_XML_NameID(const std::string& nameTarget,
228  const std::string& file_ID,
229  XML_Node* root)
230 {
231  string fname, idTarget;
232  XML_Node* db;
233  split_at_pound(file_ID, fname, idTarget);
234  if (fname == "") {
235  if (!root) {
236  return 0;
237  }
238  db = root->findNameID(nameTarget, idTarget);
239  } else {
240  XML_Node* doc = get_XML_File(fname);
241  if (!doc) {
242  return 0;
243  }
244  db = doc->findNameID(nameTarget, idTarget);
245  }
246  return db;
247 }
248 
249 std::vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
250 
251 }
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:189
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:105
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
Class to hold global data.
Definition: application.h:44
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:146
std::string findInputFile(const std::string &name)
Find an input file.
Definition: global.cpp:130
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string &#39;unit&#39; to SI units.
Definition: global.cpp:135
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
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 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:110
std::string gitCommit()
Returns the hash of the git commit from which Cantera was compiled, if known.
Definition: global.cpp:96
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:115
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
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
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:155
void addDataDirectory(const std::string &dir)
Add a directory to the data file search path.
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:8
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:120
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:227
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:177
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:125