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
11 using namespace std;
12
13 namespace Cantera
14 {
15
16 //! Return a pointer to the application object
app()17 static Application* app()
18 {
19 return Application::Instance();
20 }
21
22 // **************** Text Logging ****************
23
setLogger(Logger * logwriter)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
writelog_direct(const std::string & msg)33 void writelog_direct(const std::string& msg)
34 {
35 app()->writelog(msg);
36 }
37
writelogendl()38 void writelogendl()
39 {
40 app()->writelogendl();
41 }
42
writeline(char repeat,size_t count,bool endl_after,bool endl_before)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
warn_deprecated(const std::string & method,const std::string & extra)54 void warn_deprecated(const std::string& method, const std::string& extra)
55 {
56 app()->warn_deprecated(method, extra);
57 }
58
_warn_user(const std::string & method,const std::string & extra)59 void _warn_user(const std::string& method, const std::string& extra)
60 {
61 app()->warn_user(method, extra);
62 }
63
suppress_deprecation_warnings()64 void suppress_deprecation_warnings()
65 {
66 app()->suppress_deprecation_warnings();
67 }
68
make_deprecation_warnings_fatal()69 void make_deprecation_warnings_fatal()
70 {
71 app()->make_deprecation_warnings_fatal();
72 }
73
suppress_thermo_warnings(bool suppress)74 void suppress_thermo_warnings(bool suppress)
75 {
76 app()->suppress_thermo_warnings(suppress);
77 }
78
thermo_warnings_suppressed()79 bool thermo_warnings_suppressed()
80 {
81 return app()->thermo_warnings_suppressed();
82 }
83
use_legacy_rate_constants(bool legacy)84 void use_legacy_rate_constants(bool legacy)
85 {
86 app()->use_legacy_rate_constants(legacy);
87 }
88
legacy_rate_constants_used()89 bool legacy_rate_constants_used()
90 {
91 return app()->legacy_rate_constants_used();
92 }
93
94 // **************** Global Data ****************
95
96 Unit* Unit::s_u = 0;
97 std::mutex Unit::units_mutex;
98
appdelete()99 void appdelete()
100 {
101 Application::ApplicationDestroy();
102 FactoryBase::deleteFactories();
103 Unit::deleteUnit();
104 }
105
thread_complete()106 void thread_complete()
107 {
108 app()->thread_complete();
109 }
110
gitCommit()111 std::string gitCommit()
112 {
113 #ifdef GIT_COMMIT
114 return GIT_COMMIT;
115 #else
116 return "unknown";
117 #endif
118 }
119
get_XML_File(const std::string & file,int debug)120 XML_Node* get_XML_File(const std::string& file, int debug)
121 {
122 return app()->get_XML_File(file, debug);
123 }
124
get_XML_from_string(const std::string & text)125 XML_Node* get_XML_from_string(const std::string& text)
126 {
127 return app()->get_XML_from_string(text);
128 }
129
close_XML_File(const std::string & file)130 void close_XML_File(const std::string& file)
131 {
132 app()->close_XML_File(file);
133 }
134
addDirectory(const std::string & dir)135 void addDirectory(const std::string& dir)
136 {
137 app()->addDataDirectory(dir);
138 }
139
getDataDirectories(const std::string & sep)140 std::string getDataDirectories(const std::string& sep)
141 {
142 return app()->getDataDirectories(sep);
143 }
144
findInputFile(const std::string & name)145 std::string findInputFile(const std::string& name)
146 {
147 return app()->findInputFile(name);
148 }
149
toSI(const std::string & unit)150 doublereal toSI(const std::string& unit)
151 {
152 doublereal f = Unit::units()->toSI(unit);
153 if (f) {
154 return f;
155 } else {
156 throw CanteraError("toSI","unknown unit string: "+unit);
157 }
158 return 1.0;
159 }
160
actEnergyToSI(const std::string & unit)161 doublereal actEnergyToSI(const std::string& unit)
162 {
163 doublereal f = Unit::units()->actEnergyToSI(unit);
164 if (f) {
165 return f;
166 }
167 return 1.0;
168 }
169
canteraRoot()170 string canteraRoot()
171 {
172 char* ctroot = getenv("CANTERA_ROOT");
173 if (ctroot != 0) {
174 return string(ctroot);
175 }
176 #ifdef CANTERA_ROOT
177 return string(CANTERA_ROOT);
178 #else
179 return "";
180 #endif
181
182 }
183
184 //! split a string at a '#' sign. Used to separate a file name from an id string.
185 /*!
186 * @param src Original string to be split up. This is unchanged.
187 * @param file Output string representing the first part of the string,
188 * which is the filename.
189 * @param id Output string representing the last part of the string,
190 * which is the id.
191 */
split_at_pound(const std::string & src,std::string & file,std::string & id)192 static void split_at_pound(const std::string& src, std::string& file, std::string& id)
193 {
194 string::size_type ipound = src.find('#');
195 if (ipound != string::npos) {
196 id = src.substr(ipound+1,src.size());
197 file = src.substr(0,ipound);
198 } else {
199 id = "";
200 file = src;
201 }
202 }
203
get_XML_Node(const std::string & file_ID,XML_Node * root)204 XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root)
205 {
206 std::string fname, idstr;
207 XML_Node* db;
208 split_at_pound(file_ID, fname, idstr);
209 if (fname == "") {
210 if (!root) throw CanteraError("get_XML_Node",
211 "no file name given. file_ID = "+file_ID);
212 db = root->findID(idstr, 3);
213 } else {
214 try {
215 findInputFile(fname);
216 } catch (CanteraError& err) {
217 // See if the input file can be found with a different format
218 if (fname.rfind(".xml") == fname.size() - 4) {
219 fname.replace(fname.size() - 3, 3, "cti");
220 } else if (fname.rfind(".cti") == fname.size() - 4) {
221 fname.replace(fname.size() - 3, 3, "xml");
222 }
223 try {
224 findInputFile(fname);
225 } catch (CanteraError&) {
226 // rethrow the original error, which indicates the given file name
227 throw err;
228 }
229 }
230 XML_Node* doc = get_XML_File(fname);
231 if (!doc) throw CanteraError("get_XML_Node",
232 "get_XML_File failed trying to open "+fname);
233 db = doc->findID(idstr, 3);
234 }
235 if (!db) {
236 throw CanteraError("get_XML_Node",
237 "id tag '"+idstr+"' not found.");
238 }
239 return db;
240 }
241
get_XML_NameID(const std::string & nameTarget,const std::string & file_ID,XML_Node * root)242 XML_Node* get_XML_NameID(const std::string& nameTarget,
243 const std::string& file_ID,
244 XML_Node* root)
245 {
246 string fname, idTarget;
247 XML_Node* db;
248 split_at_pound(file_ID, fname, idTarget);
249 if (fname == "") {
250 if (!root) {
251 return 0;
252 }
253 db = root->findNameID(nameTarget, idTarget);
254 } else {
255 XML_Node* doc = get_XML_File(fname);
256 if (!doc) {
257 return 0;
258 }
259 db = doc->findNameID(nameTarget, idTarget);
260 }
261 return db;
262 }
263
264 std::vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
265
266 }
267