oomph_definitions.h
Go to the documentation of this file.
1// LIC// ====================================================================
2// LIC// This file forms part of oomph-lib, the object-oriented,
3// LIC// multi-physics finite-element library, available
4// LIC// at http://www.oomph-lib.org.
5// LIC//
6// LIC// Copyright (C) 2006-2025 Matthias Heil and Andrew Hazel
7// LIC//
8// LIC// This library is free software; you can redistribute it and/or
9// LIC// modify it under the terms of the GNU Lesser General Public
10// LIC// License as published by the Free Software Foundation; either
11// LIC// version 2.1 of the License, or (at your option) any later version.
12// LIC//
13// LIC// This library is distributed in the hope that it will be useful,
14// LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// LIC// Lesser General Public License for more details.
17// LIC//
18// LIC// You should have received a copy of the GNU Lesser General Public
19// LIC// License along with this library; if not, write to the Free Software
20// LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21// LIC// 02110-1301 USA.
22// LIC//
23// LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
24// LIC//
25// LIC//====================================================================
26// A header containing the definition of the Oomph run-time
27// exception classes and our standard (info) output stream
28//(essentially a wrapper to cout but it can be customised,
29// e.g. for mpi runs.
30
31// Include guard to prevent multiple inclusions of the header
32#ifndef OOMPH_DEFINITIONS_HEADER
33#define OOMPH_DEFINITIONS_HEADER
34
35// Config header
36#ifdef HAVE_CONFIG_H
37#include <oomph-lib-config.h>
38#endif
39
40// Standard libray headers
41#include <stdexcept>
42#include <iostream>
43#include <string>
44#include <vector>
45
46
47namespace oomph
48{
49// Pre-processor magic for error reporting
50// Macro that converts argument to string
51#define OOMPH_MAKE_STRING(x) #x
52
53// Macro wrapper to MAKE_STRING, required because calling
54// OOMPH_MAKE_STRING(__LINE__) directly returns __LINE__
55// i.e. the conversion of __LINE__ into a number must be performed before
56// its conversion into a string
57#define OOMPH_TO_STRING(x) OOMPH_MAKE_STRING(x)
58
59// Combine the FILE and LINE built-in macros into a string that can
60// be used in erorr messages.
61#define OOMPH_EXCEPTION_LOCATION __FILE__ ":" OOMPH_TO_STRING(__LINE__)
62
63// Get the current function name. All the mess is due to different
64// compilers naming the macro we need differently.
65#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || \
66 (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__)
67#define OOMPH_CURRENT_FUNCTION __PRETTY_FUNCTION__
68
69#elif defined(__DMC__) && (__DMC__ >= 0x810)
70#define OOMPH_CURRENT_FUNCTION __PRETTY_FUNCTION__
71
72#elif defined(__FUNCSIG__)
73#define OOMPH_CURRENT_FUNCTION __FUNCSIG__
74
75#elif (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 600)) || \
76 (defined(__IBMCPP__) && (__IBMCPP__ >= 500))
77#define OOMPH_CURRENT_FUNCTION __FUNCTION__
78
79#elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x550)
80#define OOMPH_CURRENT_FUNCTION __FUNC__
81
82#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
83#define OOMPH_CURRENT_FUNCTION __func__
84
85#else
86#define OOMPH_CURRENT_FUNCTION "[Unknown function -- unrecognised compiler]"
87
88#endif
89
90// Helper macro to throw an oomph-lib error
91#define OOMPH_ERROR(msg) \
92 throw OomphLibError(msg, OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
93
94// Helper macro to provide an oomph-lib warning
95#define OOMPH_WARNING(msg) \
96 OomphLibWarning(msg, OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION);
97
98 ///////////////////////////////////////////////////////////////////////
99 ///////////////////////////////////////////////////////////////////////
100 ///////////////////////////////////////////////////////////////////////
101
102
103 /// =====================================================================
104 /// Namespace to control level of comprehensive timings
105 //======================================================================
106 namespace Global_timings
107 {
108 /// Global boolean to switch on comprehensive timing -- can
109 /// probably be declared const false when development on hector
110 /// is complete
111 extern bool Doc_comprehensive_timings;
112
113 }; // namespace Global_timings
114
115
116 /////////////////////////////////////////////////////////////////////
117 /////////////////////////////////////////////////////////////////////
118 /////////////////////////////////////////////////////////////////////
119
120 //=======================================================================
121 /// Helper namespace for set_terminate function -- used to spawn
122 /// messages from uncaught errors (their destructor may not be called)
123 /// =======================================================================
124 namespace TerminateHelper
125 {
126 /// Setup terminate helper
127 extern void setup();
128
129 /// Suppress error messages (e.g. because error has been caught)
131
132 /// Function to spawn messages from uncaught errors
134
135 /// Clean up function that deletes anything dynamically allocated
136 /// in this namespace
137 extern void clean_up_memory();
138
139 /// Stream to output error messages
140 extern std::ostream* Error_message_stream_pt;
141
142 /// String stream that records the error message
143 extern std::stringstream* Exception_stringstream_pt;
144
145 } // namespace TerminateHelper
146
147 ///////////////////////////////////////////////////////////////////////
148 ///////////////////////////////////////////////////////////////////////
149 ///////////////////////////////////////////////////////////////////////
150
151
152 /// =====================================================================
153 /// A class for handling oomph-lib run-time exceptions quietly.
154 //======================================================================
155 class OomphLibQuietException : public std::runtime_error
156 {
157 public:
158 /// Constructor
160
161 /// The destructor cannot throw an exception (C++ STL standard)
163 };
164
165 ///////////////////////////////////////////////////////////////////////
166 ///////////////////////////////////////////////////////////////////////
167 ///////////////////////////////////////////////////////////////////////
168
169
170 /// =====================================================================
171 /// A Base class for oomph-lib run-time exception (error and warning)
172 /// handling.
173 ///
174 /// The class can only be instantiated by the derived classes
175 /// OomphLibError and OomphLibWarning. The (protected) constructor
176 /// combines its string arguments into a standard format
177 /// for uniform exception reports which are written to the specified
178 /// output stream.
179 //======================================================================
180 class OomphLibException : public std::runtime_error
181 {
182 public:
183 /// Suppress issueing of the error message in destructor
184 /// (useful if error is caught successfully!)
186 {
187 // Suppress output of message in destructor...
189
190 // ...and in the big cleanup operation at the end
192 }
193
194 protected:
195 /// Constructor takes the error description, function name
196 /// and a location string provided by the OOMPH_EXCEPTION_LOCATION
197 /// macro and combines them into a standard header. The exception type
198 /// will be the string "WARNING" or "ERROR" and the message is written to
199 /// the exception_stream, with a specified output_width. Optionally
200 /// provide a traceback of the function calls.
201 OomphLibException(const std::string& error_description,
202 const std::string& function_name,
203 const char* location,
204 const std::string& exception_type,
205 std::ostream& exception_stream,
206 const unsigned& output_width,
207 bool list_trace_back);
208
209 /// The destructor cannot throw an exception (C++ STL standard)
211
212 /// Exception stream to which we write message in destructor
213 std::ostream* Exception_stream_pt;
214
215 /// String stream that records the error message
217
218 /// Boolean to suppress issuing of the error message in destructor
219 /// (useful if error is caught successfully!)
221 };
222
223 //====================================================================
224 /// An OomphLibError object which should be thrown when an run-time
225 /// error is encountered. The error stream and stream width can be
226 /// specified. The default is cerr with a width of 70 characters.
227 //====================================================================
229 {
230 /// Output stream that is used to write the errors
231 static std::ostream* Stream_pt;
232
233 /// Width in characters of the output report
234 static unsigned Output_width;
235
236 public:
237 /// Constructor requires the error description and the function
238 /// in which the error occured and the location provided by the
239 /// OOMPH_EXCEPTION_LOCATION macro
241 const std::string& function_name,
242 const char* location)
245 location,
246 "ERROR",
247 *Stream_pt,
248 Output_width,
249 true)
250 {
251 }
252
253 /// Static member function used to specify the error stream,
254 /// which must be passed as a pointer because streams cannot be copied.
255 static inline void set_stream_pt(std::ostream* const& stream_pt)
256 {
257 Stream_pt = stream_pt;
258 }
259
260 /// Static member function used to specify the width (in characters)
261 /// of the error stream
262 static inline void set_output_width(const unsigned& output_width)
263 {
264 Output_width = output_width;
265 }
266 };
267
268 //====================================================================
269 /// An OomphLibWarning object which should be created as a temporary
270 /// object to issue a warning. The warning stream and stream width can be
271 /// specified. The default is cerr with a width of 70 characters.
272 //====================================================================
274 {
275 /// Output stream that is used to write the errors
276 static std::ostream* Stream_pt;
277
278 /// Width of output
279 static unsigned Output_width;
280
281 public:
282 /// Constructor requires the warning description and the function
283 /// in which the warning occurred.
285 const std::string& function_name,
286 const char* location)
289 location,
290 "WARNING",
291 *Stream_pt,
292 Output_width,
293 false)
294 {
295 }
296
297 /// Static member function used to specify the error stream,
298 /// which must be passed as a pointer because streams cannot be copied.
299 static inline void set_stream_pt(std::ostream* const& stream_pt)
300 {
301 Stream_pt = stream_pt;
302 }
303
304 /// Static member function used to specify the width (in characters)
305 /// of the error stream
306 static inline void set_output_width(const unsigned& output_width)
307 {
308 Output_width = output_width;
309 }
310 };
311
312
313 ////////////////////////////////////////////////////////////////////////
314 ////////////////////////////////////////////////////////////////////////
315 ////////////////////////////////////////////////////////////////////////
316
317
318 //=====================================================================
319 /// A small nullstream class that throws away everything sent to it.
320 //=====================================================================
321 class Nullstream : public std::ostream
322 {
323 public:
324 /// Constructor sets the buffer sizes to zero, suppressing all output
325 Nullstream() : std::ios(0), std::ostream(0) {}
326 };
327
328
329 //========================================================================
330 /// Single (global) instantiation of the Nullstream
331 //========================================================================
332 extern Nullstream oomph_nullstream;
333
334
335 ////////////////////////////////////////////////////////////////////////
336 ////////////////////////////////////////////////////////////////////////
337 ////////////////////////////////////////////////////////////////////////
338
339
340 //========================================================================
341 /// A base class that contains a single virtual member function:
342 /// The () operator that may be used to modify the output in
343 /// OomphOutput objects. The default implementation
344 /// =======================================================================
346 {
347 public:
348 /// Empty constructor
350
351 /// Empty virtual destructor
352 virtual ~OutputModifier() {}
353
354 /// Function that will be called before output from an
355 /// OomphOutput object. It returns a bool (true in this default
356 /// implementation) to indicate that output should be continued.
357 virtual bool operator()(std::ostream& stream)
358 {
359 return true;
360 }
361 };
362
363
364 //========================================================================
365 /// Single global instatiation of the default output modifier.
366 //========================================================================
367 extern OutputModifier default_output_modifier;
368
369
370 ////////////////////////////////////////////////////////////////////////
371 ////////////////////////////////////////////////////////////////////////
372 ////////////////////////////////////////////////////////////////////////
373
374
375 //=======================================================================
376 /// Namespace containing an output stream that can be used for
377 /// debugging. Use at your own risk -- global data is evil!
378 //=======================================================================
379 namespace Global_output_stream
380 {
381 /// Output stream
382 extern std::ofstream* Outfile;
383
384 } // namespace Global_output_stream
385
386
387 ////////////////////////////////////////////////////////////////////////
388 ////////////////////////////////////////////////////////////////////////
389 ////////////////////////////////////////////////////////////////////////
390
391
392 //=======================================================================
393 /// Namespace containing a number that can be used to annotate things for
394 /// debugging. Use at your own risk -- global data is evil!
395 //=======================================================================
396 namespace Global_unsigned
397 {
398 /// The unsigned
399 extern unsigned Number;
400
401 } // namespace Global_unsigned
402
403
404 ////////////////////////////////////////////////////////////////////////
405 ////////////////////////////////////////////////////////////////////////
406 ////////////////////////////////////////////////////////////////////////
407
408
409 //=======================================================================
410 /// Namespace containing a vector of strings that can be used to
411 /// to store global output modifiers. This is global data
412 /// and you use it at your own risk!
413 //=======================================================================
414 namespace Global_string_for_annotation
415 {
416 /// Return the i-th string or "" if the relevant string hasn't
417 /// been defined
418 extern std::string string(const unsigned& i);
419
420 /// Storage for strings that may be used for global annotations.
421 /// This is global data and you use it at your own risk!
422 extern std::vector<std::string> String;
423 } // namespace Global_string_for_annotation
424
425
426 ////////////////////////////////////////////////////////////////////////
427 ////////////////////////////////////////////////////////////////////////
428 ////////////////////////////////////////////////////////////////////////
429
430
431 //=======================================================================
432 /// This class is a wrapper to a stream and an output modifier that is
433 /// used to control the "info" output from OomphLib. Its instationiation
434 /// can be used like std::cout.
435 //=======================================================================
437 {
438 private:
439 /// Pointer to the output stream -- defaults to std::cout
440 std::ostream* Stream_pt;
441
442 /// Pointer to the output modifier object -- defaults to no modification
444
445 public:
446 /// Set default values for the output stream (cout)
447 /// and modifier (no modification)
449 : Stream_pt(&std::cout), Output_modifier_pt(&default_output_modifier)
450 {
451 }
452
453 /// Overload the << operator, writing output to the stream addressed
454 /// by Stream_pt and calling the function defined by the object addressed by
455 /// Output_modifier_pt
456 template<class _Tp>
457 std::ostream& operator<<(_Tp argument)
458 {
459 // If the Output_modifer function returns true
460 // echo the argument to the stream and return the (vanilla) stream
461 if ((*Output_modifier_pt)(*Stream_pt))
462 {
463 *Stream_pt << argument;
464 return (*Stream_pt);
465 }
466 // Otherwise return the null stream (suppress all future output)
467 return oomph_nullstream;
468 }
469
470 /// Access function for the stream pointer
471 std::ostream*& stream_pt()
472 {
473 return Stream_pt;
474 }
475
476 /// Overload insertor to handle stream modifiers
477 std::ostream& operator<<(std::ostream& (*f)(std::ostream&))
478 {
479 return f(*Stream_pt);
480 }
481
482 /// Access function for the output modifier pointer
484 {
485 return Output_modifier_pt;
486 }
487 };
488
489
490 //========================================================================
491 /// Single (global) instantiation of the OomphInfo object -- this
492 /// is used throughout the library as a "replacement" for std::cout
493 //========================================================================
494 extern OomphInfo oomph_info;
495
496
497} // namespace oomph
498
499#endif
cstr elem_len * i
Definition cfortran.h:603
A small nullstream class that throws away everything sent to it.
Nullstream()
Constructor sets the buffer sizes to zero, suppressing all output.
This class is a wrapper to a stream and an output modifier that is used to control the "info" output ...
std::ostream * Stream_pt
Pointer to the output stream – defaults to std::cout.
std::ostream *& stream_pt()
Access function for the stream pointer.
std::ostream & operator<<(std::ostream &(*f)(std::ostream &))
Overload insertor to handle stream modifiers.
OutputModifier *& output_modifier_pt()
Access function for the output modifier pointer.
OomphInfo()
Set default values for the output stream (cout) and modifier (no modification)
OutputModifier * Output_modifier_pt
Pointer to the output modifier object – defaults to no modification.
std::ostream & operator<<(_Tp argument)
Overload the << operator, writing output to the stream addressed by Stream_pt and calling the functio...
An OomphLibError object which should be thrown when an run-time error is encountered....
OomphLibError(const std::string &error_description, const std::string &function_name, const char *location)
Constructor requires the error description and the function in which the error occured and the locati...
static unsigned Output_width
Width in characters of the output report.
static void set_stream_pt(std::ostream *const &stream_pt)
Static member function used to specify the error stream, which must be passed as a pointer because st...
static std::ostream * Stream_pt
Output stream that is used to write the errors.
static void set_output_width(const unsigned &output_width)
Static member function used to specify the width (in characters) of the error stream.
===================================================================== A Base class for oomph-lib run-...
~OomphLibException()
The destructor cannot throw an exception (C++ STL standard)
void disable_error_message()
Suppress issueing of the error message in destructor (useful if error is caught successfully!...
std::stringstream * Exception_stringstream_pt
String stream that records the error message.
std::ostream * Exception_stream_pt
Exception stream to which we write message in destructor.
bool Suppress_error_message
Boolean to suppress issuing of the error message in destructor (useful if error is caught successfull...
===================================================================== A class for handling oomph-lib ...
~OomphLibQuietException()
The destructor cannot throw an exception (C++ STL standard)
An OomphLibWarning object which should be created as a temporary object to issue a warning....
static void set_output_width(const unsigned &output_width)
Static member function used to specify the width (in characters) of the error stream.
static unsigned Output_width
Width of output.
static std::ostream * Stream_pt
Output stream that is used to write the errors.
static void set_stream_pt(std::ostream *const &stream_pt)
Static member function used to specify the error stream, which must be passed as a pointer because st...
OomphLibWarning(const std::string &warning_description, const std::string &function_name, const char *location)
Constructor requires the warning description and the function in which the warning occurred.
A base class that contains a single virtual member function: The () operator that may be used to modi...
virtual ~OutputModifier()
Empty virtual destructor.
OutputModifier()
Empty constructor.
virtual bool operator()(std::ostream &stream)
Function that will be called before output from an OomphOutput object. It returns a bool (true in thi...
TAdvectionDiffusionReactionElement<NREAGENT,DIM,NNODE_1D> elements are isoparametric triangular DIM-d...
std::vector< std::string > String
Storage for strings that may be used for global annotations. This is global data and you use it at yo...
std::string string(const unsigned &i)
Return the i-th string or "" if the relevant string hasn't been defined.
bool Doc_comprehensive_timings
Global boolean to switch on comprehensive timing – can probably be declared const false when developm...
unsigned Number
The unsigned.
void clean_up_memory()
Clean up function that deletes anything dynamically allocated in this namespace.
void suppress_exception_error_messages()
Flush string stream of error messages (call when error has been caught)
void setup()
Setup terminate helper.
void spawn_errors_from_uncaught_errors()
Function to spawn messages from uncaught errors.
std::stringstream * Exception_stringstream_pt
String stream that records the error message.
std::ostream * Error_message_stream_pt
Stream to output error messages.
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).
Nullstream oomph_nullstream
Single (global) instantiation of the Nullstream.
OutputModifier default_output_modifier
Single global instatiation of the default output modifier.
OomphInfo oomph_info
Single (global) instantiation of the OomphInfo object – this is used throughout the library as a "rep...