Cantera  3.2.0a2
Loading...
Searching...
No Matches
application.h
Go to the documentation of this file.
1//! @file application.h
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#ifndef CT_BASE_APPLICATION_H
7#define CT_BASE_APPLICATION_H
8
9#include "cantera/base/config.h"
10#include "cantera/base/logger.h"
11
12#include <boost/algorithm/string/join.hpp>
13
14#include <thread>
15
16namespace Cantera
17{
18
19/**
20 * @defgroup globalData Global Data
21 *
22 * Global data are available anywhere. There are two kinds. %Cantera has an
23 * assortment of constant values for physical parameters. Also, Cantera
24 * maintains a collection of global data which is specific to each process that
25 * invokes %Cantera functions. This process-specific data is stored in the class
26 * Application.
27 */
28
29
30//! Class to hold global data.
31/*!
32 * Class Application is the top-level class that stores data that should persist
33 * for the duration of the process. The class should not be instantiated
34 * directly; instead, it is instantiated as needed by the functions declared
35 * here. At most one instance is created, and it is not destroyed until the
36 * process terminates.
37 *
38 * @ingroup globalData
39 * @ingroup debugGroup
40 */
42{
43protected:
44 //! Class to carry out messages
46 {
47 public:
48 Messages() = default;
49 Messages(const Messages& r) = delete;
50 Messages& operator=(const Messages& r) = delete;
51
52 //! Set an error condition in the application class without
53 //! throwing an exception.
54 /*!
55 * This routine adds an error message to the end of the stack of errors
56 * that %Cantera accumulates in the Application class.
57 * @param r Procedure name which is generating the error condition
58 * @param msg Descriptive message of the error condition.
59 *
60 * If only one argument is specified, that string is used as the
61 * entire message.
62 * @ingroup errGroup
63 */
64 void addError(const string& r, const string& msg="");
65
66 //! Return the number of errors that have been encountered so far.
67 /*!
68 * @ingroup errGroup
69 */
70 int getErrorCount();
71
72 //! Discard the last error message
73 /*!
74 * %Cantera saves a stack of exceptions that it has caught in the
75 * Application class. This routine eliminates the last exception to be
76 * added to that stack.
77 *
78 * @ingroup errGroup
79 */
80 void popError();
81
82 //! Retrieve the last error message in a string
83 /*!
84 * This routine will retrieve the last error message and return it in
85 * the return string.
86 *
87 * @ingroup errGroup
88 */
89 string lastErrorMessage();
90
91 //! Prints all of the error messages to an ostream
92 /*!
93 * Write out all of the saved error messages to the ostream f using
94 * the function Logger::writelog. %Cantera saves a stack of exceptions
95 * that it has caught in the Application class. This routine writes
96 * out all of the error messages to the ostream and then clears them
97 * from internal storage.
98 *
99 * @param f ostream which will receive the error messages
100 *
101 * @ingroup errGroup
102 */
103 void getErrors(std::ostream& f);
104
105 //! Prints all of the error messages using writelog
106 /*!
107 * Print all of the error messages using function writelog. Cantera
108 * saves a stack of exceptions that it has caught in the Application
109 * class. This routine writes out all of the error messages and then
110 * clears them from internal storage.
111 *
112 * @ingroup errGroup
113 */
114 void logErrors();
115
116 protected:
117 //! Current list of error messages
118 vector<string> errorMessage;
119 };
120
121 //! Typedef for thread specific messages
122 typedef shared_ptr<Messages> pMessages_t;
123
124 //! Class that stores thread messages for each thread, and retrieves them
125 //! based on the thread id.
127 {
128 public:
129 //! Constructor
131
132 //! Provide a pointer dereferencing overloaded operator
133 /*!
134 * @returns a pointer to Messages
135 */
137
138 //! Remove a local thread message
140
141 //! Typedef for map between a thread and the message
142 typedef map<std::thread::id, pMessages_t> threadMsgMap_t;
143
144 private:
145 //! Thread Msg Map
147 };
148
149protected:
150 //! Constructor for class sets up the initial conditions
151 //! Protected ctor access thru static member function Instance
152 Application();
153
154public:
155 //! Return a pointer to the one and only instance of class Application
156 /*!
157 * If the Application object has not yet been created, it is created
158 */
159 static Application* Instance();
160
161 //! Destructor for class deletes global data
162 virtual ~Application() {}
163
164 //! Static function that destroys the application class's data
165 static void ApplicationDestroy();
166
167 //! @copydoc Messages::addError
168 void addError(const string& r, const string& msg="") {
169 pMessenger->addError(r, msg);
170 }
171
172 //! @copydoc Messages::getErrorCount
174 return pMessenger->getErrorCount();
175 }
176
177 //! @copydoc Messages::popError
178 void popError() {
179 pMessenger->popError();
180 }
181
182 //! @copydoc Messages::lastErrorMessage
184 return pMessenger->lastErrorMessage();
185 }
186
187 //! @copydoc Messages::getErrors
188 void getErrors(std::ostream& f) {
189 pMessenger->getErrors(f);
190 }
191
192 //! @copydoc Messages::logErrors
193 void logErrors() {
194 pMessenger->logErrors();
195 }
196
197 //! Add a directory to the data file search path.
198 /*!
199 * @ingroup inputGroup
200 *
201 * @param dir String name for the directory to be added to the search path
202 */
203 void addDataDirectory(const string& dir);
204
205 //! Find an input file.
206 /*!
207 * This routine will search for a file in the default locations specified
208 * for the application. See the routine setDefaultDirectories() listed
209 * above. The first directory searched is usually the current working
210 * directory.
211 *
212 * The default set of directories will not be searched if an absolute path
213 * (for example, one starting with `/` or `C:\`) or a path relative to the
214 * user's home directory (for example, starting with `~/`) is specified.
215 *
216 * The presence of the file is determined by whether the file can be
217 * opened for reading by the current user.
218 *
219 * @param name Name of the input file to be searched for
220 * @return The absolute path name of the first matching file
221 *
222 * If the file is not found a CanteraError exception is thrown.
223 *
224 * @ingroup inputGroup
225 */
226 string findInputFile(const string& name);
227
228 //! Get the %Cantera data directories
229 /*!
230 * This routine returns a string including the names of all the
231 * directories searched by %Cantera for data files.
232 *
233 * @param sep Separator to use between directories in the string
234 * @return A string of directories separated by the input sep
235 *
236 * @ingroup inputGroup
237 */
238 string getDataDirectories(const string& sep) {
239 return boost::algorithm::join(inputDirs, sep);
240 }
241
242 //! Load an extension implementing user-defined models
243 //! @param extType Specifies the interface / language of the extension, for example
244 //! "python"
245 //! @param name Specifies the name of the extension. The meaning of this
246 //! parameter depends on the specific extension interface. For example, for
247 //! Python extensions, this is the name of the Python module containing the
248 //! models.
249 //! @since New in %Cantera 3.0
250 void loadExtension(const string& extType, const string& name);
251
252 //! Set the versions of Python to try when loading user-defined extensions,
253 //! in order of preference. Separate multiple versions with commas, for example
254 //! `"3.11,3.10"`.
255 //! @since New in %Cantera 3.0
256 void searchPythonVersions(const string& versions);
257
258#ifdef _WIN32
259 long int readStringRegistryKey(const string& keyName, const string& valueName,
260 string& value, const string& defaultValue);
261#endif
262
263 //! Write a message to the logger.
264 /*!
265 * The string may be of any length, and may contain end-of-line characters. This
266 * method is used throughout %Cantera to write log messages. It can also be called
267 * by user programs. The advantage of using writelog over writing directly to the
268 * standard output is that messages written with writelog will display correctly
269 * even when %Cantera is used from MATLAB or other application that do not have a
270 * standard output stream.
271 *
272 * @param msg C++ string to be written to the logger
273 * @ingroup logGroup
274 */
275 void writelog(const string& msg);
276
277 //! Write an end of line character to the logger and flush output
278 void writelogendl();
279
280 //! Write a warning message to the logger.
281 /*!
282 * @param warning Type of warning; see Logger::warn()
283 * @param msg Message to be written to the logger
284 * @ingroup logGroup
285 */
286 void warnlog(const string& warning, const string& msg);
287
288 //! Print a warning indicating that *method* is deprecated. Additional
289 //! information (removal version, alternatives) can be specified in
290 //! *extra*. Deprecation warnings are printed once per method per
291 //! invocation of the application.
292 void warn_deprecated(const string& method, const string& extra="");
293
294 //! Globally disable printing of deprecation warnings. Used primarily to
295 //! prevent certain tests from failing.
297 m_suppress_deprecation_warnings = true;
298 m_fatal_deprecation_warnings = false;
299 }
300
301 //! Turns deprecation warnings into exceptions. Activated within the test
302 //! suite to make sure that no deprecated methods are being used.
304 m_fatal_deprecation_warnings = true;
305 }
306
307 //! Generate a general purpose warning; repeated warnings are not suppressed
308 //! @param warning Warning type; see Logger::warn()
309 //! @param method Name of method triggering the warning
310 //! @param extra Additional information printed for the warning
311 void warn(const string& warning, const string& method, const string& extra="");
312
313 //! Globally disable printing of (user) warnings. Used primarily to
314 //! prevent certain tests from failing.
316 m_suppress_warnings = true;
317 m_fatal_warnings = false;
318 }
319
320 //! Returns `true` if warnings should be suppressed.
322 return m_suppress_warnings;
323 }
324
325 //! Turns %Cantera warnings into exceptions. Activated within the test
326 //! suite to make sure that your warning message are being raised.
328 m_fatal_warnings = true;
329 }
330
331 //! Globally disable printing of warnings about problematic thermo data,
332 //! such as NASA polynomials with discontinuities at the midpoint temperature.
333 void suppress_thermo_warnings(bool suppress=true) {
334 m_suppress_thermo_warnings = suppress;
335 }
336
337 //! Returns `true` if thermo warnings should be suppressed.
339 return m_suppress_thermo_warnings;
340 }
341
342 //! Set definition used for rate constant calculation.
343 //! @see Kinetics::getFwdRateConstants()
344 /*!
345 * If set to `false` (default value), rate constants of three-body reactions are
346 * consistent with conventional definitions (for example Eq. 9.75 in Kee, et al.
347 * @cite kee2003).
348 * If set to `true`, output for rate constants of three-body reactions is
349 * multiplied by third-body concentrations, consistent with %Cantera's behavior
350 * prior to version 3.0.
351 */
352 void use_legacy_rate_constants(bool legacy=true) {
353 m_use_legacy_rate_constants = legacy;
354 }
355
356 //! Returns `true` if legacy rate constant definition is used
358 return m_use_legacy_rate_constants;
359 }
360
361 //! Install a logger.
362 /*!
363 * Called by the language interfaces to install an appropriate logger.
364 * The logger is used for the writelog() function
365 *
366 * @param logwriter Pointer to a logger object
367 * @see Logger.
368 * @ingroup logGroup
369 *
370 * @deprecated To be removed after %Cantera 3.2. Replaced by version taking
371 * `unique_ptr`.
372 */
373 void setLogger(Logger* logwriter);
374
375 //! Install a Logger.
376 /*!
377 * Called by the language interfaces to install an appropriate logger.
378 * The logger is used for the writelog() function
379 *
380 * @since Changed in %Cantera 3.2 to take `unique_ptr` instead of bare pointer.
381 * @ingroup logGroup
382 */
383 void setLogger(unique_ptr<Logger> logwriter);
384
385 //! Delete and free memory allocated per thread in multithreaded applications
386 /*!
387 * Delete the memory allocated per thread by Cantera. It should be called
388 * from within the thread just before the thread terminates. If your
389 * version of %Cantera has not been specifically compiled for thread safety
390 * this function does nothing.
391 */
392 void thread_complete();
393
394protected:
395 //! Set the default directories for input files.
396 /*!
397 * %Cantera searches for input files along a path that includes platform-
398 * specific default locations, and possibly user-specified locations.
399 * This function installs the platform-specific directories on the search
400 * path. It is invoked at startup by appinit(), and never should need to
401 * be called by user programs.
402 *
403 * The current directory (".") is always searched first. Then, on Windows, the
404 * registry is checked to find the %Cantera installation directory, and the
405 * 'data' subdirectory of the installation directory will be added to the search
406 * path.
407 *
408 * On any platform, if environment variable CANTERA_DATA is set to a directory
409 * name or a list of directory names separated with the OS-dependent path
410 * separator (that is, ";" on Windows, ":" elsewhere), then these directories will
411 * be added to the search path.
412 *
413 * Finally, the location where the data files were installed when
414 * %Cantera was built is added to the search path.
415 *
416 * Additional directories may be added by calling function addDirectory.
417 * @ingroup inputGroup
418 */
420
421 //! Current vector of input directories to search for input files
422 vector<string> inputDirs;
423
424 //! Versions of Python to consider when attempting to load user extensions
425 vector<string> m_pythonSearchVersions = {"3.14", "3.13", "3.12", "3.11", "3.10"};
426
427 //! Set of deprecation warnings that have been emitted (to suppress duplicates)
428 set<string> warnings;
429
430 bool m_suppress_deprecation_warnings = false;
431 bool m_fatal_deprecation_warnings = false;
432 bool m_suppress_thermo_warnings = false;
433 bool m_suppress_warnings = false;
434 bool m_fatal_warnings = false;
435 bool m_use_legacy_rate_constants = false;
436
437 set<pair<string, string>> m_loaded_extensions;
438
439 ThreadMessages pMessenger;
440
441 //! Current log writer
442 unique_ptr<Logger> m_logwriter;
443
444private:
445 //! Pointer to the single Application instance
447};
448
449}
450
451#endif
Class to carry out messages.
Definition application.h:46
vector< string > errorMessage
Current list of error messages.
Class that stores thread messages for each thread, and retrieves them based on the thread id.
Messages * operator->()
Provide a pointer dereferencing overloaded operator.
threadMsgMap_t m_threadMsgMap
Thread Msg Map.
map< std::thread::id, pMessages_t > threadMsgMap_t
Typedef for map between a thread and the message.
void removeThreadMessages()
Remove a local thread message.
Class to hold global data.
Definition application.h:42
bool warnings_suppressed()
Returns true if warnings should be suppressed.
void use_legacy_rate_constants(bool legacy=true)
Set definition used for rate constant calculation.
static Application * s_app
Pointer to the single Application instance.
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
vector< string > m_pythonSearchVersions
Versions of Python to consider when attempting to load user extensions.
bool thermo_warnings_suppressed()
Returns true if thermo warnings should be suppressed.
void make_warnings_fatal()
Turns Cantera warnings into exceptions.
unique_ptr< Logger > m_logwriter
Current log writer.
void make_deprecation_warnings_fatal()
Turns deprecation warnings into exceptions.
static Application * Instance()
Return a pointer to the one and only instance of class Application.
void addError(const string &r, const string &msg="")
Set an error condition in the application class without throwing an exception.
void logErrors()
Prints all of the error messages using writelog.
void loadExtension(const string &extType, const string &name)
Load an extension implementing user-defined models.
void warn_deprecated(const string &method, const string &extra="")
Print a warning indicating that method is deprecated.
void searchPythonVersions(const string &versions)
Set the versions of Python to try when loading user-defined extensions, in order of preference.
bool legacy_rate_constants_used()
Returns true if legacy rate constant definition is used.
void suppress_warnings()
Globally disable printing of (user) warnings.
vector< string > inputDirs
Current vector of input directories to search for input files.
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
set< string > warnings
Set of deprecation warnings that have been emitted (to suppress duplicates)
void suppress_thermo_warnings(bool suppress=true)
Globally disable printing of warnings about problematic thermo data, such as NASA polynomials with di...
shared_ptr< Messages > pMessages_t
Typedef for thread specific messages.
void getErrors(std::ostream &f)
Prints all of the error messages to an ostream.
string lastErrorMessage()
Retrieve the last error message in a string.
int getErrorCount()
Return the number of errors that have been encountered so far.
virtual ~Application()
Destructor for class deletes global data.
Application()
Constructor for class sets up the initial conditions Protected ctor access thru static member functio...
void writelogendl()
Write an end of line character to the logger and flush output.
static void ApplicationDestroy()
Static function that destroys the application class's data.
void warn(const string &warning, const string &method, const string &extra="")
Generate a general purpose warning; repeated warnings are not suppressed.
void popError()
Discard the last error message.
Base class for 'loggers' that write text messages to log files.
Definition logger.h:40
void addError(const string &r, const string &msg="")
Set an error condition in the application class without throwing an exception.
void logErrors()
Prints all of the error messages using writelog.
void getErrors(std::ostream &f)
Prints all of the error messages to an ostream.
string lastErrorMessage()
Retrieve the last error message in a string.
int getErrorCount()
Return the number of errors that have been encountered so far.
void popError()
Discard the last error message.
string getDataDirectories(const string &sep)
Get the Cantera data directories.
string findInputFile(const string &name)
Find an input file.
void addDataDirectory(const string &dir)
Add a directory to the data file search path.
void setDefaultDirectories()
Set the default directories for input files.
void warnlog(const string &warning, const string &msg)
Write a warning message to the logger.
void setLogger(Logger *logwriter)
Install a logger.
void writelog(const string &msg)
Write a message to the logger.
Header for Base class for 'loggers' that write text messages to log files (see Logging and class Logg...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595