forked from Cantera/cantera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
435 lines (380 loc) · 12.8 KB
/
application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! @file application.cpp
// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.
#include "application.h"
#include "cantera/base/ctexceptions.h"
#include "cantera/base/stringUtils.h"
#include "cantera/base/ExtensionManagerFactory.h"
#define BOOST_DLL_USE_STD_FS
#include <boost/dll/import.hpp>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <sstream>
#include <mutex>
namespace ba = boost::algorithm;
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef _MSC_VER
#pragma comment(lib, "advapi32")
#endif
namespace Cantera
{
//! Mutex for input directory access
static std::mutex dir_mutex;
//! Mutex for creating singletons within the application object
static std::mutex app_mutex;
Application::Messages::Messages()
{
// install a default logwriter that writes to standard
// output / standard error
logwriter = make_unique<Logger>();
}
void Application::Messages::addError(const string& r, const string& msg)
{
if (msg.size() != 0) {
errorMessage.push_back(
"\n\n************************************************\n"
" Cantera Error! \n"
"************************************************\n\n"
"Procedure: " + r +
"\nError: " + msg + "\n");
} else {
errorMessage.push_back(r);
}
}
int Application::Messages::getErrorCount()
{
return static_cast<int>(errorMessage.size());
}
void Application::Messages::setLogger(Logger* _logwriter)
{
logwriter.reset(_logwriter);
}
void Application::Messages::writelog(const string& msg)
{
logwriter->write(msg);
}
void Application::Messages::writelogendl()
{
logwriter->writeendl();
}
void Application::Messages::warnlog(const string& warning, const string& msg)
{
logwriter->warn(warning, msg);
}
//! Mutex for access to string messages
static std::mutex msg_mutex;
Application::Messages* Application::ThreadMessages::operator ->()
{
std::unique_lock<std::mutex> msgLock(msg_mutex);
std::thread::id curId = std::this_thread::get_id();
auto iter = m_threadMsgMap.find(curId);
if (iter != m_threadMsgMap.end()) {
return iter->second.get();
}
pMessages_t pMsgs(new Messages());
m_threadMsgMap.insert({curId, pMsgs});
return pMsgs.get();
}
void Application::ThreadMessages::removeThreadMessages()
{
std::unique_lock<std::mutex> msgLock(msg_mutex);
std::thread::id curId = std::this_thread::get_id();
auto iter = m_threadMsgMap.find(curId);
if (iter != m_threadMsgMap.end()) {
m_threadMsgMap.erase(iter);
}
}
Application::Application()
{
// install a default logwriter that writes to standard
// output / standard error
setDefaultDirectories();
}
Application* Application::Instance()
{
std::unique_lock<std::mutex> appLock(app_mutex);
if (Application::s_app == 0) {
Application::s_app = new Application();
}
return s_app;
}
void Application::ApplicationDestroy()
{
std::unique_lock<std::mutex> appLock(app_mutex);
if (Application::s_app != 0) {
delete Application::s_app;
Application::s_app = 0;
}
}
void Application::warn_deprecated(const string& method, const string& extra)
{
if (m_fatal_deprecation_warnings) {
throw CanteraError(method, "Deprecated: " + extra);
} else if (m_suppress_deprecation_warnings || warnings.count(method)) {
return;
}
warnings.insert(method);
warnlog("Deprecation", fmt::format("{}: {}", method, extra));
}
void Application::warn(const string& warning, const string& method, const string& extra)
{
if (m_fatal_warnings) {
throw CanteraError(method, extra);
} else if (m_suppress_warnings) {
return;
}
warnlog(warning, fmt::format("{}: {}", method, extra));
}
void Application::thread_complete()
{
pMessenger.removeThreadMessages();
}
#ifdef _WIN32
long int Application::readStringRegistryKey(const string& keyName, const string& valueName,
string& value, const string& defaultValue)
{
HKEY key;
long open_error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName.c_str(), 0, KEY_READ, &key);
if (open_error != ERROR_SUCCESS) {
return open_error;
}
value = defaultValue;
CHAR buffer[1024];
DWORD bufferSize = sizeof(buffer);
ULONG error;
error = RegQueryValueEx(key, valueName.c_str(), 0, NULL, (LPBYTE) buffer, &bufferSize);
if (ERROR_SUCCESS == error) {
value = buffer;
}
RegCloseKey(key);
return error;
}
#endif
void Application::Messages::popError()
{
if (!errorMessage.empty()) {
errorMessage.pop_back();
}
}
string Application::Messages::lastErrorMessage()
{
if (!errorMessage.empty()) {
return errorMessage.back();
} else {
return "<no Cantera error>";
}
}
void Application::Messages::getErrors(std::ostream& f)
{
for (size_t j = 0; j < errorMessage.size(); j++) {
f << errorMessage[j] << std::endl;
}
errorMessage.clear();
}
void Application::Messages::logErrors()
{
for (size_t j = 0; j < errorMessage.size(); j++) {
writelog(errorMessage[j]);
writelogendl();
}
errorMessage.clear();
}
void Application::setDefaultDirectories()
{
// always look in the local directory first
inputDirs.push_back(".");
// if environment variable CANTERA_DATA is defined, then add it to the
// search path. CANTERA_DATA may include multiple directory, separated by
// the OS-dependent path separator (in the same manner as the PATH
// environment variable).
#ifdef _WIN32
string pathsep = ";";
string dirsep = "\\";
#else
string pathsep = ":";
string dirsep = "/";
#endif
if (getenv("CANTERA_DATA") != nullptr) {
string s(getenv("CANTERA_DATA"));
size_t start = 0;
size_t end = s.find(pathsep);
while (end != npos) {
inputDirs.push_back(s.substr(start, end-start));
start = end + 1;
end = s.find(pathsep, start);
}
inputDirs.push_back(s.substr(start,end));
}
// If a conda environment is active, add the location of the Cantera data directory
// that may exist in that environment.
if (getenv("CONDA_PREFIX") != nullptr) {
string s(getenv("CONDA_PREFIX"));
inputDirs.push_back(s + dirsep + "share" + dirsep + "cantera" + dirsep + "data");
}
#ifdef _WIN32
// Under Windows, the Cantera setup utility records the installation
// directory in the registry. Data files are stored in the 'data'
// subdirectory of the main installation directory.
string installDir;
readStringRegistryKey("SOFTWARE\\Cantera\\Cantera " CANTERA_SHORT_VERSION,
"InstallDir", installDir, "");
if (installDir != "") {
inputDirs.push_back(installDir + "data");
// Scripts for converting mechanisms to YAML are installed in
// the 'bin' subdirectory. Add that directory to the PYTHONPATH.
const char* old_pythonpath = getenv("PYTHONPATH");
string pythonpath = "PYTHONPATH=" + installDir + "\\bin";
if (old_pythonpath) {
pythonpath += ";";
pythonpath.append(old_pythonpath);
}
_putenv(pythonpath.c_str());
}
#endif
// CANTERA_DATA is defined in file config.h. This file is written during the
// build process (unix), and points to the directory specified by the
// 'prefix' option to 'configure', or else to /usr/local/cantera.
#ifdef CANTERA_DATA
string datadir = stripnonprint(string(CANTERA_DATA));
inputDirs.push_back(datadir);
#endif
}
void Application::addDataDirectory(const string& dir)
{
std::unique_lock<std::mutex> dirLock(dir_mutex);
if (inputDirs.empty()) {
setDefaultDirectories();
}
string d = stripnonprint(dir);
// Expand "~/" to user's home directory, if possible
if (d.find("~/") == 0 || d.find("~\\") == 0) {
char* home = getenv("HOME"); // POSIX systems
if (!home) {
home = getenv("USERPROFILE"); // Windows systems
}
if (home) {
d = home + d.substr(1, npos);
}
}
// Remove any existing entry for this directory
auto iter = std::find(inputDirs.begin(), inputDirs.end(), d);
if (iter != inputDirs.end()) {
inputDirs.erase(iter);
}
// Insert this directory at the beginning of the search path
inputDirs.insert(inputDirs.begin(), d);
}
string Application::findInputFile(const string& name)
{
std::unique_lock<std::mutex> dirLock(dir_mutex);
string::size_type islash = name.find('/');
string::size_type ibslash = name.find('\\');
string::size_type icolon = name.find(':');
vector<string>& dirs = inputDirs;
// Expand "~/" to user's home directory, if possible
if (name.find("~/") == 0 || name.find("~\\") == 0) {
char* home = getenv("HOME"); // POSIX systems
if (!home) {
home = getenv("USERPROFILE"); // Windows systems
}
if (home) {
string full_name = home + name.substr(1, npos);
std::ifstream fin(full_name);
if (fin) {
return full_name;
} else {
throw CanteraError("Application::findInputFile",
"Input file '{}' not found", name);
}
}
}
// If this is an absolute path, just look for the file there
if (islash == 0 || ibslash == 0
|| (icolon == 1 && (ibslash == 2 || islash == 2)))
{
std::ifstream fin(name);
if (fin) {
return name;
} else {
throw CanteraError("Application::findInputFile",
"Input file '{}' not found", name);
}
}
// Search the Cantera data directories for the input file, and return
// the full path if a match is found
size_t nd = dirs.size();
for (size_t i = 0; i < nd; i++) {
string full_name = dirs[i] + "/" + name;
std::ifstream fin(full_name);
if (fin) {
return full_name;
}
}
string msg = "\nInput file " + name + " not found in director";
msg += (nd == 1 ? "y " : "ies ");
for (size_t i = 0; i < nd; i++) {
msg += "\n'" + dirs[i] + "'";
if (i+1 < nd) {
msg += ", ";
}
}
msg += "\n\n";
msg += "To fix this problem, either:\n";
msg += " a) move the missing files into the local directory;\n";
msg += " b) define environment variable CANTERA_DATA to\n";
msg += " point to the directory containing the file.";
throw CanteraError("Application::findInputFile", msg);
}
void Application::loadExtension(const string& extType, const string& name)
{
if (!usingSharedLibrary()) {
throw CanteraError("Application::loadExtension",
"Loading extensions requires linking to the Cantera shared library\n"
"rather than the static library");
}
if (m_loaded_extensions.count({extType, name})) {
return;
}
if (extType == "python" && !ExtensionManagerFactory::factory().exists("python")) {
string errors;
// type of imported symbol: void function with no arguments
typedef void (loader_t)();
// Only one Python module can be loaded at a time, and a handle needs to be held
// to prevent it from being unloaded.
static function<loader_t> loader;
bool loaded = false;
for (const auto& py_ver : m_pythonSearchVersions) {
string py_ver_underscore = ba::replace_all_copy(py_ver, ".", "_");
try {
loader = boost::dll::import_alias<loader_t>(
"cantera_python" + py_ver_underscore, // library name
"registerPythonExtensionManager", // symbol to import
// append extensions and prefixes, search normal library path, and
// expose all loaded symbols (specifically, those from libpython)
boost::dll::load_mode::search_system_folders
| boost::dll::load_mode::append_decorations
| boost::dll::load_mode::rtld_global
);
loader();
loaded = true;
break;
} catch (std::exception& err) {
errors += fmt::format("\nPython {}: {}\n", py_ver, err.what());
}
}
if (!loaded) {
throw CanteraError("Application::loadExtension",
"Error loading Python extension support. Tried the following:{}",
errors);
}
}
ExtensionManagerFactory::build(extType)->registerRateBuilders(name);
m_loaded_extensions.insert({extType, name});
}
void Application::searchPythonVersions(const string& versions) {
ba::split(m_pythonSearchVersions, versions, ba::is_any_of(","));
}
Application* Application::s_app = 0;
} // namespace Cantera