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-2026 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 /// Compile-time test to determine if a type T has a notion of
101 /// "magnitude", i.e. if std::abs(T) is a valid expression.
102 ///
103 /// The primary template defaults to "false".
104 ///
105 /// A partial specialisation (below) is selected automatically via
106 /// SFINAE ("Substitution Failure Is Not An Error") if the expression
107 ///
108 /// std::abs(std::declval<T>())
109 ///
110 /// is valid. In that case HasMagnitude<T>::value evaluates to true.
111 ///
112 /// This facility is used to prevent compilation failures in generic
113 /// code where operations involving std::abs() only make sense for
114 /// numerical types.
115 ///
116 /// Example:
117 /// HasMagnitude<double>::value -> true
118 /// HasMagnitude<std::complex<double>>::value -> true
119 /// HasMagnitude<Vector<double>>::value -> false
120 //==================================================================
121 template<class T, class = void>
122 struct HasMagnitude : std::false_type
123 {
124 };
125
126
127 //==================================================================
128 /// Partial specialisation of HasMagnitude<T> which is selected
129 /// when std::abs(T) is a valid expression. In that case
130 /// HasMagnitude<T>::value is true.
131 /// std::declval<T>() creates a fictitious object of type T for use in
132 /// unevaluated expressions (decltype in this case) without requiring
133 /// construction of an actual object.
134 ///
135 /// Example:
136 /// HasMagnitude<double>::value -> true
137 /// HasMagnitude<std::complex<double>>::value -> true
138 /// HasMagnitude<Vector<double>>::value -> false
139 //==================================================================
140 template<class T>
141 struct HasMagnitude<T, std::void_t<decltype(std::abs(std::declval<T>()))>>
142 : std::true_type
143 {
144 };
145
146
147 ///////////////////////////////////////////////////////////////////////
148 ///////////////////////////////////////////////////////////////////////
149 ///////////////////////////////////////////////////////////////////////
150
151
152 /// =====================================================================
153 /// Namespace to control level of comprehensive timings
154 //======================================================================
155 namespace Global_timings
156 {
157 /// Global boolean to switch on comprehensive timing -- can
158 /// probably be declared const false when development on hector
159 /// is complete
160 extern bool Doc_comprehensive_timings;
161
162 }; // namespace Global_timings
163
164
165 /////////////////////////////////////////////////////////////////////
166 /////////////////////////////////////////////////////////////////////
167 /////////////////////////////////////////////////////////////////////
168
169 //=======================================================================
170 /// Helper namespace for set_terminate function -- used to spawn
171 /// messages from uncaught errors (their destructor may not be called)
172 /// =======================================================================
173 namespace TerminateHelper
174 {
175 /// Setup terminate helper
176 extern void setup();
177
178 /// Suppress error messages (e.g. because error has been caught)
180
181 /// Function to spawn messages from uncaught errors
183
184 /// Clean up function that deletes anything dynamically allocated
185 /// in this namespace
186 extern void clean_up_memory();
187
188 /// Stream to output error messages
189 extern std::ostream* Error_message_stream_pt;
190
191 /// String stream that records the error message
192 extern std::stringstream* Exception_stringstream_pt;
193
194 } // namespace TerminateHelper
195
196 ///////////////////////////////////////////////////////////////////////
197 ///////////////////////////////////////////////////////////////////////
198 ///////////////////////////////////////////////////////////////////////
199
200
201 /// =====================================================================
202 /// A class for handling oomph-lib run-time exceptions quietly.
203 //======================================================================
204 class OomphLibQuietException : public std::runtime_error
205 {
206 public:
207 /// Constructor
209
210 /// The destructor cannot throw an exception (C++ STL standard)
212 };
213
214 ///////////////////////////////////////////////////////////////////////
215 ///////////////////////////////////////////////////////////////////////
216 ///////////////////////////////////////////////////////////////////////
217
218
219 /// =====================================================================
220 /// A Base class for oomph-lib run-time exception (error and warning)
221 /// handling.
222 ///
223 /// The class can only be instantiated by the derived classes
224 /// OomphLibError and OomphLibWarning. The (protected) constructor
225 /// combines its string arguments into a standard format
226 /// for uniform exception reports which are written to the specified
227 /// output stream.
228 //======================================================================
229 class OomphLibException : public std::runtime_error
230 {
231 public:
232 /// Suppress issueing of the error message in destructor
233 /// (useful if error is caught successfully!)
235 {
236 // Suppress output of message in destructor...
238
239 // ...and in the big cleanup operation at the end
241 }
242
243 protected:
244 /// Constructor takes the error description, function name
245 /// and a location string provided by the OOMPH_EXCEPTION_LOCATION
246 /// macro and combines them into a standard header. The exception type
247 /// will be the string "WARNING" or "ERROR" and the message is written to
248 /// the exception_stream, with a specified output_width. Optionally
249 /// provide a traceback of the function calls.
250 OomphLibException(const std::string& error_description,
251 const std::string& function_name,
252 const char* location,
253 const std::string& exception_type,
254 std::ostream& exception_stream,
255 const unsigned& output_width,
256 bool list_trace_back);
257
258 /// The destructor cannot throw an exception (C++ STL standard)
260
261 /// Exception stream to which we write message in destructor
262 std::ostream* Exception_stream_pt;
263
264 /// String stream that records the error message
266
267 /// Boolean to suppress issuing of the error message in destructor
268 /// (useful if error is caught successfully!)
270 };
271
272 //====================================================================
273 /// An OomphLibError object which should be thrown when an run-time
274 /// error is encountered. The error stream and stream width can be
275 /// specified. The default is cerr with a width of 70 characters.
276 //====================================================================
278 {
279 /// Output stream that is used to write the errors
280 static std::ostream* Stream_pt;
281
282 /// Width in characters of the output report
283 static unsigned Output_width;
284
285 public:
286 /// Constructor requires the error description and the function
287 /// in which the error occured and the location provided by the
288 /// OOMPH_EXCEPTION_LOCATION macro
290 const std::string& function_name,
291 const char* location)
294 location,
295 "ERROR",
296 *Stream_pt,
297 Output_width,
298 true)
299 {
300 }
301
302 /// Static member function used to specify the error stream,
303 /// which must be passed as a pointer because streams cannot be copied.
304 static inline void set_stream_pt(std::ostream* const& stream_pt)
305 {
306 Stream_pt = stream_pt;
307 }
308
309 /// Static member function used to specify the width (in characters)
310 /// of the error stream
311 static inline void set_output_width(const unsigned& output_width)
312 {
313 Output_width = output_width;
314 }
315 };
316
317 //====================================================================
318 /// An OomphLibWarning object which should be created as a temporary
319 /// object to issue a warning. The warning stream and stream width can be
320 /// specified. The default is cerr with a width of 70 characters.
321 //====================================================================
323 {
324 /// Output stream that is used to write the errors
325 static std::ostream* Stream_pt;
326
327 /// Width of output
328 static unsigned Output_width;
329
330 public:
331 /// Constructor requires the warning description and the function
332 /// in which the warning occurred.
334 const std::string& function_name,
335 const char* location)
338 location,
339 "WARNING",
340 *Stream_pt,
341 Output_width,
342 false)
343 {
344 }
345
346 /// Static member function used to specify the error stream,
347 /// which must be passed as a pointer because streams cannot be copied.
348 static inline void set_stream_pt(std::ostream* const& stream_pt)
349 {
350 Stream_pt = stream_pt;
351 }
352
353 /// Static member function used to specify the width (in characters)
354 /// of the error stream
355 static inline void set_output_width(const unsigned& output_width)
356 {
357 Output_width = output_width;
358 }
359 };
360
361
362 ////////////////////////////////////////////////////////////////////////
363 ////////////////////////////////////////////////////////////////////////
364 ////////////////////////////////////////////////////////////////////////
365
366
367 //=====================================================================
368 /// A small nullstream class that throws away everything sent to it.
369 //=====================================================================
370 class Nullstream : public std::ostream
371 {
372 public:
373 /// Constructor sets the buffer sizes to zero, suppressing all output
374 Nullstream() : std::ios(0), std::ostream(0) {}
375 };
376
377
378 //========================================================================
379 /// Single (global) instantiation of the Nullstream
380 //========================================================================
381 extern Nullstream oomph_nullstream;
382
383
384 ////////////////////////////////////////////////////////////////////////
385 ////////////////////////////////////////////////////////////////////////
386 ////////////////////////////////////////////////////////////////////////
387
388
389 //========================================================================
390 /// A base class that contains a single virtual member function:
391 /// The () operator that may be used to modify the output in
392 /// OomphOutput objects. The default implementation
393 /// =======================================================================
395 {
396 public:
397 /// Empty constructor
399
400 /// Empty virtual destructor
401 virtual ~OutputModifier() {}
402
403 /// Function that will be called before output from an
404 /// OomphOutput object. It returns a bool (true in this default
405 /// implementation) to indicate that output should be continued.
406 virtual bool operator()(std::ostream& stream)
407 {
408 return true;
409 }
410 };
411
412
413 //========================================================================
414 /// Single global instatiation of the default output modifier.
415 //========================================================================
416 extern OutputModifier default_output_modifier;
417
418
419 ////////////////////////////////////////////////////////////////////////
420 ////////////////////////////////////////////////////////////////////////
421 ////////////////////////////////////////////////////////////////////////
422
423
424 //=======================================================================
425 /// Namespace containing an output stream that can be used for
426 /// debugging. Use at your own risk -- global data is evil!
427 //=======================================================================
428 namespace Global_output_stream
429 {
430 /// Output stream
431 extern std::ofstream* Outfile;
432
433 } // namespace Global_output_stream
434
435
436 ////////////////////////////////////////////////////////////////////////
437 ////////////////////////////////////////////////////////////////////////
438 ////////////////////////////////////////////////////////////////////////
439
440
441 //=======================================================================
442 /// Namespace containing a number that can be used to annotate things for
443 /// debugging. Use at your own risk -- global data is evil!
444 //=======================================================================
445 namespace Global_unsigned
446 {
447 /// The unsigned
448 extern unsigned Number;
449
450 } // namespace Global_unsigned
451
452
453 ////////////////////////////////////////////////////////////////////////
454 ////////////////////////////////////////////////////////////////////////
455 ////////////////////////////////////////////////////////////////////////
456
457
458 //=======================================================================
459 /// Namespace containing a vector of strings that can be used to
460 /// to store global output modifiers. This is global data
461 /// and you use it at your own risk!
462 //=======================================================================
463 namespace Global_string_for_annotation
464 {
465 /// Return the i-th string or "" if the relevant string hasn't
466 /// been defined
467 extern std::string string(const unsigned& i);
468
469 /// Storage for strings that may be used for global annotations.
470 /// This is global data and you use it at your own risk!
471 extern std::vector<std::string> String;
472 } // namespace Global_string_for_annotation
473
474
475 ////////////////////////////////////////////////////////////////////////
476 ////////////////////////////////////////////////////////////////////////
477 ////////////////////////////////////////////////////////////////////////
478
479
480 //=======================================================================
481 /// This class is a wrapper to a stream and an output modifier that is
482 /// used to control the "info" output from OomphLib. Its instationiation
483 /// can be used like std::cout.
484 //=======================================================================
486 {
487 private:
488 /// Pointer to the output stream -- defaults to std::cout
489 std::ostream* Stream_pt;
490
491 /// Pointer to the output modifier object -- defaults to no modification
493
494 public:
495 /// Set default values for the output stream (cout)
496 /// and modifier (no modification)
498 : Stream_pt(&std::cout), Output_modifier_pt(&default_output_modifier)
499 {
500 }
501
502 /// Overload the << operator, writing output to the stream addressed
503 /// by Stream_pt and calling the function defined by the object addressed by
504 /// Output_modifier_pt
505 template<class _Tp>
506 std::ostream& operator<<(_Tp argument)
507 {
508 // If the Output_modifer function returns true
509 // echo the argument to the stream and return the (vanilla) stream
510 if ((*Output_modifier_pt)(*Stream_pt))
511 {
512 *Stream_pt << argument;
513 return (*Stream_pt);
514 }
515 // Otherwise return the null stream (suppress all future output)
516 return oomph_nullstream;
517 }
518
519 /// Access function for the stream pointer
520 std::ostream*& stream_pt()
521 {
522 return Stream_pt;
523 }
524
525 /// Overload insertor to handle stream modifiers
526 std::ostream& operator<<(std::ostream& (*f)(std::ostream&))
527 {
528 return f(*Stream_pt);
529 }
530
531 /// Access function for the output modifier pointer
533 {
534 return Output_modifier_pt;
535 }
536 };
537
538
539 //========================================================================
540 /// Single (global) instantiation of the OomphInfo object -- this
541 /// is used throughout the library as a "replacement" for std::cout
542 //========================================================================
543 extern OomphInfo oomph_info;
544
545
546} // namespace oomph
547
548#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...
Compile-time test to determine if a type T has a notion of "magnitude", i.e. if std::abs(T) is a vali...