Cantera  2.1.2
global.h
Go to the documentation of this file.
1 /**
2  * @file global.h
3  * This file contains definitions for utility functions and text for modules,
4  * inputfiles, logs, textlogs, HTML_logs (see \ref inputfiles, \ref logs,
5  * \ref textlogs and \ref HTML_logs).
6  *
7  * @ingroup utils
8  *
9  * These functions store
10  * some parameters in global storage that are accessible at all times
11  * from the calling application.
12  * Contains module definitions for
13  * - inputfiles (see \ref inputfiles)
14  * - logs (see \ref logs)
15  * - textlogs (see \ref textlogs)
16  * - HTML_logs (see \ref HTML_logs)
17  */
18 // Copyright 2001 California Institute of Technology
19 
20 #ifndef CT_GLOBAL_H
21 #define CT_GLOBAL_H
22 
23 #include "ct_defs.h"
24 
25 namespace Cantera
26 {
27 
28 class XML_Node;
29 class Logger;
30 
31 //! Declaration for whether the Debug mode is turned on within Cantera
32 /*!
33  * Turn on the mode by using the following compile time syntax
34  *
35  * scons debug_verbose=True build
36  */
37 extern const int g_DEBUG_MODE;
38 
39 //! Return the number of errors that have been encountered so far
40 /*!
41  * @ingroup errorhandling
42  */
43 int nErrors();
44 
45 //! @copydoc Application::Messages::lastErrorMessage
46 std::string lastErrorMessage();
47 
48 //! @copydoc Application::Messages::addError
49 void setError(const std::string& r, const std::string& msg);
50 
51 //! @copydoc Application::Messages::getErrors
52 void showErrors(std::ostream& f);
53 
54 //! @copydoc Application::Messages::logErrors
55 void showErrors();
56 
57 //! @copydoc Application::Messages::popError
58 void popError();
59 
60 /*!
61  * @defgroup inputfiles Input File Handling
62  *
63  * The properties of phases and interfaces are specified in text files. These
64  * procedures handle various aspects of reading these files.
65  *
66  * For input files not specified by an absolute pathname, %Cantera searches
67  * for input files along a path that includes platform-specific default
68  * locations, and possibly user-specified locations.
69  *
70  * The current directory (".") is always searched first. Then, on Windows
71  * platforms, if environment variable COMMONPROGRAMFILES is set (which it
72  * should be on Win XP or Win 2000), then directories under this one will be
73  * added to the search path. The %Cantera Windows installer installs data
74  * files to this location.
75  *
76  * On the Mac, directory '/Applications/Cantera/data' is added to the
77  * search path.
78  *
79  * On any platform, if environment variable CANTERA_DATA is set to a
80  * directory name, then this directory is added to the search path.
81  *
82  * Finally, the location where the data files were installed when
83  * %Cantera was built is added to the search path.
84  *
85  * Additional directories may be added by calling function addDirectory.
86  *
87  * There are two different types of input files within %Cantera:
88  * - ctml: This is an xml file laid out in such a way that %Cantera can
89  * interpret the contents.
90  * - cti: A human-readable ascii format for information that %Cantera
91  * will read.
92  *
93  * %Cantera can take its input from both types of files. However, given a file
94  * in cti format, the initial operation that %Cantera will perform is to
95  * translate the cti file into a ctml file. The translation is carried out via
96  * a system call to a python interpreter program that actually carries out the
97  * translation. In general, a new ctml file is created by the translation that
98  * is written to the current local directory. The ctml file is then read back
99  * into %Cantera as the input.
100  *
101  * Other input routines in other modules:
102  * @see importKinetics()
103  * @{
104  */
105 
106 //! @copydoc Application::findInputFile
107 std::string findInputFile(const std::string& name);
108 
109 //! @copydoc Application::addDataDirectory
110 void addDirectory(const std::string& dir);
111 //@}
112 
113 //! Delete and free all memory associated with the application
114 /*!
115  * Delete all global data. It should be called at the end of the
116  * application if leak checking is to be done.
117  */
118 void appdelete();
119 
120 //! @copydoc Application::thread_complete
121 void thread_complete() ;
122 
123 //! Returns root directory where %Cantera is installed
124 /*!
125  * @return Returns a string containing the name of the base directory where
126  * %Cantera is installed. If the environmental variable CANTERA_ROOT is
127  * defined, this function will return its value, preferentially.
128  *
129  * @ingroup inputfiles
130  */
131 std::string canteraRoot();
132 
133 /*!
134  * @defgroup logs Diagnostic Output
135  *
136  * Writing diagnostic information to the screen or to a file. It is often
137  * useful to be able to write diagnostic messages to the screen or to a file.
138  * Cantera provides two sets of procedures for this purpose. The first set is
139  * designed to write text messages to the screen to document the progress of a
140  * complex calculation, such as a flame simulation.The second set writes
141  * nested lists in HTML format. This is useful to print debugging output for a
142  * complex calculation that calls many different procedures.
143  */
144 
145 /*!
146  * @defgroup textlogs Writing messages to the screen
147  * @ingroup logs
148  */
149 
150 //! @copydoc Application::Messages::writelog(const std::string&)
151 void writelog(const std::string& msg);
152 
153 //! Write a message to the log only if loglevel > 0
154 inline void writelog(const std::string& msg, int loglevel)
155 {
156  if (loglevel > 0) {
157  writelog(msg);
158  }
159 }
160 
161 //! Write a formatted message to the screen
162 /*!
163  * Using the printf formatting of C, write a message to the screen
164  * with variable values.
165  *
166  * Here, we format an internal string with the correct values
167  * and then feed it into writelog().
168  *
169  * @param fmt c format string for the following arguments
170  * @ingroup textlogs
171  */
172 void writelogf(const char* fmt,...);
173 
174 //! Write an end of line character to the screen and flush output
175 void writelogendl();
176 
177 //! @copydoc Application::Messages::logerror
178 void error(const std::string& msg);
179 
180 //! @copydoc Application::warn_deprecated
181 void warn_deprecated(const std::string& method, const std::string& extra="");
182 
183 //! @copydoc Application::suppress_deprecation_warnings
185 
186 //! @copydoc Application::Messages::setLogger
187 void setLogger(Logger* logwriter);
188 
189 //! Return the conversion factor to convert unit std::string 'unit'
190 //! to SI units.
191 /*!
192  * @param unit String containing the units
193  */
194 doublereal toSI(const std::string& unit);
195 
196 /// Return the conversion factor to convert activation energy unit
197 /// std::string 'unit' to Kelvin.
198 /*!
199  * @param unit String containing the activation energy units
200  */
201 doublereal actEnergyToSI(const std::string& unit);
202 
203 //! @copydoc Application::get_XML_File
204 XML_Node* get_XML_File(const std::string& file, int debug = 0);
205 
206 //! @copydoc Application::close_XML_File
207 void close_XML_File(const std::string& file);
208 
209 #ifdef WITH_HTML_LOGS
210 
211 /*!
212  * @defgroup HTML_logs Writing HTML Logfiles
213  * @ingroup logs
214  *
215  * These functions are designed to allow writing HTML diagnostic
216  * messages in a manner that allows users to control how much
217  * diagnostic output to print. It works like this: Suppose you
218  * have function A that invokes function B that invokes function
219  * C. You want to be able to print diagnostic messages just from
220  * function A, or from A and B, or from A, B, and C, or to turn
221  * off printing diagnostic messages altogether. All you need to
222  * do is call 'beginLogGroup' within function A, and specify a
223  * loglevel value. Then in B, call beginLogGroup again, but
224  * without an explicit value for loglevel. By default, the
225  * current level is decremented by one in beginLogGroup. If it
226  * is <= 0, no log messages are written. Thus, if each function
227  * begins with beginLogGroup and calls endLogGroup before
228  * returning, then setting loglevel = 3 will cause messages from
229  * A, B, and C to be written (in nested HTML lists), loglevel =
230  * 2 results in messages only being written from A and B, etc.
231  */
232 
233 //! @copydoc Application::Messages::beginLogGroup
234 void beginLogGroup(const std::string& title, int loglevel=-99);
235 
236 //! @copydoc Application::Messages::addLogEntry(const std::string&, const std::string&)
237 void addLogEntry(const std::string& tag, const std::string& value);
238 
239 //! @copydoc Application::Messages::addLogEntry(const std::string&, doublereal)
240 void addLogEntry(const std::string& tag, doublereal value);
241 
242 //! @copydoc Application::Messages::addLogEntry(const std::string&, int)
243 void addLogEntry(const std::string& tag, int value);
244 
245 //! @copydoc Application::Messages::addLogEntry(const std::string&)
246 void addLogEntry(const std::string& msg);
247 
248 //! @copydoc Application::Messages::endLogGroup
249 void endLogGroup(const std::string& title="");
250 
251 //! @copydoc Application::Messages::write_logfile
252 void write_logfile(const std::string& file = "log.html");
253 
254 #else
255 inline void beginLogGroup(const std::string& title, int loglevel=-99) {}
256 inline void addLogEntry(const std::string& tag, const std::string& value) {}
257 inline void addLogEntry(const std::string& tag, doublereal value) {}
258 inline void addLogEntry(const std::string& tag, int value) {}
259 inline void addLogEntry(const std::string& msg) {}
260 inline void endLogGroup(const std::string& title="") {}
261 inline void write_logfile(const std::string& file = "log.html") {}
262 #endif
263 
264 //! This routine will locate an XML node in either the input
265 //! XML tree or in another input file specified by the file
266 //! part of the file_ID string.
267 /*!
268  * Searches are based on the
269  * ID attribute of the XML element only.
270  *
271  * @param file_ID This is a concatenation of two strings separated
272  * by the "#" character. The string before the
273  * pound character is the file name of an xml
274  * file to carry out the search. The string after
275  * the # character is the ID attribute
276  * of the xml element to search for.
277  * The string is interpreted as a file string if
278  * no # character is in the string.
279  *
280  * @param root If the file string is empty, searches for the
281  * xml element with matching ID attribute are
282  * carried out from this XML node.
283  *
284  * @return
285  * Returns the XML_Node, if found. Returns null if not found.
286  */
287 XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root);
288 
289 
290 //! This routine will locate an XML node in either the input
291 //! XML tree or in another input file specified by the file
292 //! part of the file_ID string.
293 /*!
294  * Searches are based on the
295  * XML element name and the ID attribute of the XML element.
296  * An exact match of both is usually required. However, the
297  * ID attribute may be set to "", in which case the first
298  * xml element with the correct element name will be returned.
299  *
300  * @param nameTarget This is the XML element name to look for.
301  *
302  * @param file_ID This is a concatenation of two strings separated
303  * by the "#" character. The string before the
304  * pound character is the file name of an xml
305  * file to carry out the search. The string after
306  * the # character is the ID attribute
307  * of the xml element to search for.
308  * The string is interpreted as a file string if
309  * no # character is in the string.
310  *
311  * @param root If the file string is empty, searches for the
312  * xml element with matching ID attribute are
313  * carried out from this XML node.
314  *
315  * @return
316  * Returns the XML_Node, if found. Returns null if not found.
317  */
318 XML_Node* get_XML_NameID(const std::string& nameTarget,
319  const std::string& file_ID,
320  XML_Node* root);
321 
322 //! Clip *value* such that lower <= value <= upper
323 template <class T>
324 inline T clip(const T& value, const T& lower, const T& upper)
325 {
326  return std::max(lower, std::min(upper, value));
327 }
328 
329 }
330 
331 #endif
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:249
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:144
void popError()
Discard the last error message.
Definition: global.cpp:161
void beginLogGroup(const std::string &title, int loglevel)
Create a new group for log messages.
Definition: global.cpp:90
string lastErrorMessage()
Retrieve the last error message in a string.
Definition: global.cpp:166
doublereal actEnergyToSI(const std::string &unit)
Return the conversion factor to convert activation energy unit std::string 'unit' to Kelvin...
Definition: global.cpp:207
void showErrors(std::ostream &f)
Prints all of the error messages to an ostream.
Definition: global.cpp:171
std::string findInputFile(const std::string &name)
Find an input file.
Definition: global.cpp:191
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string 'unit' to SI units.
Definition: global.cpp:196
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition: global.cpp:139
void setLogger(Logger *logwriter)
Install a logger.
Definition: global.cpp:34
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:181
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
T clip(const T &value, const T &lower, const T &upper)
Clip value such that lower <= value <= upper.
Definition: global.h:324
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:76
void close_XML_File(const std::string &file)
Close an XML File.
Definition: global.cpp:151
void writelogf(const char *fmt,...)
Write a formatted message to the screen.
Definition: global.cpp:48
const int g_DEBUG_MODE
Definition of whether the DEBUG_MODE environment is turned on within Cantera.
Definition: global.cpp:22
void error(const std::string &msg)
Write an error message and quit.
Definition: global.cpp:71
void appdelete()
Delete and free all memory associated with the application.
Definition: global.cpp:132
void endLogGroup(const std::string &title)
Close the current group of log messages.
Definition: global.cpp:115
void addLogEntry(const std::string &tag, const std::string &value)
Add an entry to an HTML log file.
Definition: global.cpp:95
void writelogendl()
Write an end of line character to the screen and flush output.
Definition: global.cpp:66
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: global.cpp:81
string canteraRoot()
Returns root directory where Cantera is installed.
Definition: global.cpp:216
void write_logfile(const std::string &file)
Write the HTML log file.
Definition: global.cpp:120
void writelog(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:43
int nErrors()
Return the number of errors that have been encountered so far.
Definition: global.cpp:156
void addDirectory(const std::string &dir)
Add a directory to the data file search path.
Definition: global.cpp:186
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:271