Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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, (see \ref inputfiles, \ref logs, and
5  * \ref textlogs).
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  */
17 // Copyright 2001 California Institute of Technology
18 
19 #ifndef CT_GLOBAL_H
20 #define CT_GLOBAL_H
21 
22 #include "ct_defs.h"
23 
24 namespace Cantera
25 {
26 
27 class XML_Node;
28 class Logger;
29 
30 //! Return the number of errors that have been encountered so far
31 /*!
32  * @ingroup errorhandling
33  */
34 int nErrors();
35 
36 //! @copydoc Application::Messages::lastErrorMessage
37 std::string lastErrorMessage();
38 
39 //! @copydoc Application::Messages::addError
40 void setError(const std::string& r, const std::string& msg);
41 
42 //! @copydoc Application::Messages::getErrors
43 void showErrors(std::ostream& f);
44 
45 //! @copydoc Application::Messages::logErrors
46 void showErrors();
47 
48 //! @copydoc Application::Messages::popError
49 void popError();
50 
51 /*!
52  * @defgroup inputfiles Input File Handling
53  *
54  * The properties of phases and interfaces are specified in text files. These
55  * procedures handle various aspects of reading these files.
56  *
57  * For input files not specified by an absolute pathname, %Cantera searches
58  * for input files along a path that includes platform-specific default
59  * locations, and possibly user-specified locations.
60  *
61  * The current directory (".") is always searched first. Then, on Windows, the
62  * registry is checked to find the Cantera installation directory, and the
63  * 'data' subdirectory of the installation directory will be added to the search
64  * path.
65  *
66  * On the Mac, directory '/Applications/Cantera/data' is added to the
67  * search path.
68  *
69  * On any platform, if environment variable CANTERA_DATA is set to a directory
70  * name or a list of directory names separated with the OS-dependent path
71  * separator (i.e. ";" on Windows, ":" elsewhere), then these directories will
72  * be added to the search path.
73  *
74  * Finally, the location where the data files were installed when
75  * %Cantera was built is added to the search path.
76  *
77  * Additional directories may be added by calling function addDirectory.
78  *
79  * There are currently two different types of input files within %Cantera:
80  * - CTI: A human-readable input file written using Python syntax which
81  * defines species, phases, and reactions, and contains thermodynamic,
82  * chemical kinetic, and transport data needed by %Cantera. Some options for
83  * non-ideal equations of state available in the CTML format have not yet
84  * been implemented for the CTI format.
85  *
86  * - CTML: This is an XML file laid out in such a way that %Cantera can
87  * interpret the contents directly. Given a file in CTI format, %Cantera will
88  * convert the CTI file into the CTML format on-the-fly using a Python script
89  * (ctml_writer). This process is done in-memory without writing any new
90  * files to disk. Explicit use of the CTML format is not recommended unless
91  * using features not available in CTI or working on a computer where Python
92  * is not available.
93  *
94  * %Cantera provides a converter (ck2cti) for converting Chemkin-format
95  * gas-phase mechanisms to the CTI format.
96  *
97  * Other input routines in other modules:
98  * @see importKinetics()
99  *
100  * @{
101  */
102 
103 //! @copydoc Application::findInputFile
104 std::string findInputFile(const std::string& name);
105 
106 //! @copydoc Application::addDataDirectory
107 void addDirectory(const std::string& dir);
108 //@}
109 
110 //! Delete and free all memory associated with the application
111 /*!
112  * Delete all global data. It should be called at the end of the
113  * application if leak checking is to be done.
114  */
115 void appdelete();
116 
117 //! @copydoc Application::thread_complete
118 void thread_complete() ;
119 
120 //! Returns root directory where %Cantera is installed
121 /*!
122  * @return Returns a string containing the name of the base directory where
123  * %Cantera is installed. If the environmental variable CANTERA_ROOT is
124  * defined, this function will return its value, preferentially.
125  *
126  * @ingroup inputfiles
127  */
128 std::string canteraRoot();
129 
130 /*!
131  * @defgroup logs Diagnostic Output
132  *
133  * Writing diagnostic information to the screen or to a file. It is often
134  * useful to be able to write diagnostic messages to the screen or to a file.
135  * Cantera a set of procedures for this purpose designed to write text messages
136  * to the screen to document the progress of a complex calculation, such as a
137  * flame simulation.
138  */
139 
140 /*!
141  * @defgroup textlogs Writing messages to the screen
142  * @ingroup logs
143  */
144 
145 //! @copydoc Application::Messages::writelog(const std::string&)
146 void writelog(const std::string& msg);
147 
148 //! Write a message to the log only if loglevel > 0
149 inline void writelog(const std::string& msg, int loglevel)
150 {
151  if (loglevel > 0) {
152  writelog(msg);
153  }
154 }
155 
156 //! Write a formatted message to the screen
157 /*!
158  * Using the printf formatting of C, write a message to the screen
159  * with variable values.
160  *
161  * Here, we format an internal string with the correct values
162  * and then feed it into writelog().
163  *
164  * @param fmt c format string for the following arguments
165  * @ingroup textlogs
166  */
167 void writelogf(const char* fmt,...);
168 
169 //! Write an end of line character to the screen and flush output
170 void writelogendl();
171 
172 void writeline(char repeat, size_t count,
173  bool endl_after=true, bool endl_before=false);
174 
175 //! @copydoc Application::Messages::logerror
176 void error(const std::string& msg);
177 
178 //! @copydoc Application::warn_deprecated
179 void warn_deprecated(const std::string& method, const std::string& extra="");
180 
181 //! @copydoc Application::suppress_deprecation_warnings
183 
184 //! @copydoc Application::Messages::setLogger
185 void setLogger(Logger* logwriter);
186 
187 //! Return the conversion factor to convert unit std::string 'unit'
188 //! to SI units.
189 /*!
190  * @param unit String containing the units
191  */
192 doublereal toSI(const std::string& unit);
193 
194 /// Return the conversion factor to convert activation energy unit
195 /// std::string 'unit' to Kelvin.
196 /*!
197  * @param unit String containing the activation energy units
198  */
199 doublereal actEnergyToSI(const std::string& unit);
200 
201 //! @copydoc Application::get_XML_File
202 XML_Node* get_XML_File(const std::string& file, int debug = 0);
203 
204 //! @copydoc Application::get_XML_from_string
205 XML_Node* get_XML_from_string(const std::string& text);
206 
207 //! @copydoc Application::close_XML_File
208 void close_XML_File(const std::string& file);
209 
210 //! This routine will locate an XML node in either the input
211 //! XML tree or in another input file specified by the file
212 //! part of the file_ID string.
213 /*!
214  * Searches are based on the
215  * ID attribute of the XML element only.
216  *
217  * @param file_ID This is a concatenation of two strings separated
218  * by the "#" character. The string before the
219  * pound character is the file name of an XML
220  * file to carry out the search. The string after
221  * the # character is the ID attribute
222  * of the XML element to search for.
223  * The string is interpreted as a file string if
224  * no # character is in the string.
225  *
226  * @param root If the file string is empty, searches for the
227  * XML element with matching ID attribute are
228  * carried out from this XML node.
229  *
230  * @return
231  * Returns the XML_Node, if found. Returns null if not found.
232  */
233 XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root);
234 
235 
236 //! This routine will locate an XML node in either the input
237 //! XML tree or in another input file specified by the file
238 //! part of the file_ID string.
239 /*!
240  * Searches are based on the
241  * XML element name and the ID attribute of the XML element.
242  * An exact match of both is usually required. However, the
243  * ID attribute may be set to "", in which case the first
244  * XML element with the correct element name will be returned.
245  *
246  * @param nameTarget This is the XML element name to look for.
247  *
248  * @param file_ID This is a concatenation of two strings separated
249  * by the "#" character. The string before the
250  * pound character is the file name of an XML
251  * file to carry out the search. The string after
252  * the # character is the ID attribute
253  * of the XML element to search for.
254  * The string is interpreted as a file string if
255  * no # character is in the string.
256  *
257  * @param root If the file string is empty, searches for the
258  * XML element with matching ID attribute are
259  * carried out from this XML node.
260  *
261  * @return
262  * Returns the XML_Node, if found. Returns null if not found.
263  */
264 XML_Node* get_XML_NameID(const std::string& nameTarget,
265  const std::string& file_ID,
266  XML_Node* root);
267 
268 //! Clip *value* such that lower <= value <= upper
269 template <class T>
270 inline T clip(const T& value, const T& lower, const T& upper)
271 {
272  return std::max(lower, std::min(upper, value));
273 }
274 
275 //! Sign of a number. Returns -1 if x < 0, 1 if x > 0 and 0 if x == 0.
276 template <typename T> int sign(T x) {
277  return (T(0) < x) - (x < T(0));
278 }
279 
280 }
281 
282 #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:214
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 popError()
Discard the last error message.
Definition: global.cpp:126
string lastErrorMessage()
Retrieve the last error message in a string.
Definition: global.cpp:131
doublereal actEnergyToSI(const std::string &unit)
Return the conversion factor to convert activation energy unit std::string 'unit' to Kelvin...
Definition: global.cpp:172
void showErrors(std::ostream &f)
Prints all of the error messages to an ostream.
Definition: global.cpp:136
std::string findInputFile(const std::string &name)
Find an input file.
Definition: global.cpp:156
doublereal toSI(const std::string &unit)
Return the conversion factor to convert unit std::string 'unit' to SI units.
Definition: global.cpp:161
void thread_complete()
Delete and free memory allocated per thread in multithreaded applications.
Definition: global.cpp:100
void setLogger(Logger *logwriter)
Install a logger.
Definition: global.cpp:24
static std::string fmt(const std::string &r, size_t n)
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:146
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:270
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:78
int sign(T x)
Sign of a number. Returns -1 if x < 0, 1 if x > 0 and 0 if x == 0.
Definition: global.h:276
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:111
void close_XML_File(const std::string &file)
Close an XML File.
Definition: global.cpp:116
void writelogf(const char *fmt,...)
Write a formatted message to the screen.
Definition: global.cpp:38
void error(const std::string &msg)
Write an error message and quit.
Definition: global.cpp:72
void appdelete()
Delete and free all memory associated with the application.
Definition: global.cpp:93
void writelogendl()
Write an end of line character to the screen and flush output.
Definition: global.cpp:56
void suppress_deprecation_warnings()
Globally disable printing of deprecation warnings.
Definition: global.cpp:83
string canteraRoot()
Returns root directory where Cantera is installed.
Definition: global.cpp:181
void writelog(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:33
int nErrors()
Return the number of errors that have been encountered so far.
Definition: global.cpp:121
void addDirectory(const std::string &dir)
Add a directory to the data file search path.
Definition: global.cpp:151
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:252