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-2022 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 generated by autoconfig
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 
47 namespace 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 
94 
95 
98  //======================================================================
99  namespace Global_timings
100  {
104  extern bool Doc_comprehensive_timings;
105 
106  }; // namespace Global_timings
107 
108 
112 
113  //=======================================================================
117  namespace TerminateHelper
118  {
120  extern void setup();
121 
123  extern void suppress_exception_error_messages();
124 
126  extern void spawn_errors_from_uncaught_errors();
127 
130  extern void clean_up_memory();
131 
133  extern std::ostream* Error_message_stream_pt;
134 
136  extern std::stringstream* Exception_stringstream_pt;
137 
138  } // namespace TerminateHelper
139 
143 
144 
147  //======================================================================
148  class OomphLibQuietException : public std::runtime_error
149  {
150  public:
153 
156  };
157 
161 
162 
172  //======================================================================
173  class OomphLibException : public std::runtime_error
174  {
175  public:
179  {
180  // Suppress output of message in destructor...
181  Suppress_error_message = true;
182 
183  // ...and in the big cleanup operation at the end
185  }
186 
187  protected:
194  OomphLibException(const std::string& error_description,
195  const std::string& function_name,
196  const char* location,
197  const std::string& exception_type,
198  std::ostream& exception_stream,
199  const unsigned& output_width,
200  bool list_trace_back);
201 
203  ~OomphLibException() throw();
204 
206  std::ostream* Exception_stream_pt;
207 
209  std::stringstream* Exception_stringstream_pt;
210 
214  };
215 
216  //====================================================================
220  //====================================================================
222  {
224  static std::ostream* Stream_pt;
225 
227  static unsigned Output_width;
228 
229  public:
233  OomphLibError(const std::string& error_description,
234  const std::string& function_name,
235  const char* location)
236  : OomphLibException(error_description,
237  function_name,
238  location,
239  "ERROR",
240  *Stream_pt,
241  Output_width,
242  true)
243  {
244  }
245 
248  static inline void set_stream_pt(std::ostream* const& stream_pt)
249  {
250  Stream_pt = stream_pt;
251  }
252 
255  static inline void set_output_width(const unsigned& output_width)
256  {
257  Output_width = output_width;
258  }
259  };
260 
261  //====================================================================
265  //====================================================================
267  {
269  static std::ostream* Stream_pt;
270 
272  static unsigned Output_width;
273 
274  public:
277  OomphLibWarning(const std::string& warning_description,
278  const std::string& function_name,
279  const char* location)
280  : OomphLibException(warning_description,
281  function_name,
282  location,
283  "WARNING",
284  *Stream_pt,
285  Output_width,
286  false)
287  {
288  }
289 
292  static inline void set_stream_pt(std::ostream* const& stream_pt)
293  {
294  Stream_pt = stream_pt;
295  }
296 
299  static inline void set_output_width(const unsigned& output_width)
300  {
301  Output_width = output_width;
302  }
303  };
304 
305 
309 
310 
311  //=====================================================================
313  //=====================================================================
314  class Nullstream : public std::ostream
315  {
316  public:
318  Nullstream() : std::ios(0), std::ostream(0) {}
319  };
320 
321 
322  //========================================================================
324  //========================================================================
325  extern Nullstream oomph_nullstream;
326 
327 
331 
332 
333  //========================================================================
339  {
340  public:
343 
345  virtual ~OutputModifier() {}
346 
350  virtual bool operator()(std::ostream& stream)
351  {
352  return true;
353  }
354  };
355 
356 
357  //========================================================================
359  //========================================================================
360  extern OutputModifier default_output_modifier;
361 
362 
366 
367 
368  //=======================================================================
371  //=======================================================================
372  namespace Global_output_stream
373  {
375  extern std::ofstream* Outfile;
376 
377  } // namespace Global_output_stream
378 
379 
383 
384 
385  //=======================================================================
388  //=======================================================================
389  namespace Global_unsigned
390  {
392  extern unsigned Number;
393 
394  } // namespace Global_unsigned
395 
396 
400 
401 
402  //=======================================================================
406  //=======================================================================
407  namespace Global_string_for_annotation
408  {
411  extern std::string string(const unsigned& i);
412 
415  extern std::vector<std::string> String;
416  } // namespace Global_string_for_annotation
417 
418 
422 
423 
424  //=======================================================================
428  //=======================================================================
429  class OomphInfo
430  {
431  private:
433  std::ostream* Stream_pt;
434 
437 
438  public:
442  : Stream_pt(&std::cout), Output_modifier_pt(&default_output_modifier)
443  {
444  }
445 
449  template<class _Tp>
450  std::ostream& operator<<(_Tp argument)
451  {
452  // If the Output_modifer function returns true
453  // echo the argument to the stream and return the (vanilla) stream
454  if ((*Output_modifier_pt)(*Stream_pt))
455  {
456  *Stream_pt << argument;
457  return (*Stream_pt);
458  }
459  // Otherwise return the null stream (suppress all future output)
460  return oomph_nullstream;
461  }
462 
464  std::ostream*& stream_pt()
465  {
466  return Stream_pt;
467  }
468 
470  std::ostream& operator<<(std::ostream& (*f)(std::ostream&))
471  {
472  return f(*Stream_pt);
473  }
474 
477  {
478  return Output_modifier_pt;
479  }
480  };
481 
482 
483  //========================================================================
486  //========================================================================
487  extern OomphInfo oomph_info;
488 
489 
490 } // namespace oomph
491 
492 #endif
int i
Definition: BiCGSTAB_step_by_step.cpp:9
A small nullstream class that throws away everything sent to it.
Definition: oomph_definitions.h:315
Nullstream()
Constructor sets the buffer sizes to zero, suppressing all output.
Definition: oomph_definitions.h:318
Definition: oomph_definitions.h:430
std::ostream & operator<<(std::ostream &(*f)(std::ostream &))
Overload insertor to handle stream modifiers.
Definition: oomph_definitions.h:470
std::ostream * Stream_pt
Pointer to the output stream – defaults to std::cout.
Definition: oomph_definitions.h:433
OomphInfo()
Definition: oomph_definitions.h:441
std::ostream & operator<<(_Tp argument)
Definition: oomph_definitions.h:450
std::ostream *& stream_pt()
Access function for the stream pointer.
Definition: oomph_definitions.h:464
OutputModifier * Output_modifier_pt
Pointer to the output modifier object – defaults to no modification.
Definition: oomph_definitions.h:436
OutputModifier *& output_modifier_pt()
Access function for the output modifier pointer.
Definition: oomph_definitions.h:476
Definition: oomph_definitions.h:222
OomphLibError(const std::string &error_description, const std::string &function_name, const char *location)
Definition: oomph_definitions.h:233
static unsigned Output_width
Width in characters of the output report.
Definition: oomph_definitions.h:227
static void set_stream_pt(std::ostream *const &stream_pt)
Definition: oomph_definitions.h:248
static std::ostream * Stream_pt
Output stream that is used to write the errors.
Definition: oomph_definitions.h:224
static void set_output_width(const unsigned &output_width)
Definition: oomph_definitions.h:255
Definition: oomph_definitions.h:174
~OomphLibException()
The destructor cannot throw an exception (C++ STL standard)
Definition: oomph_definitions.cc:126
OomphLibException(const std::string &error_description, const std::string &function_name, const char *location, const std::string &exception_type, std::ostream &exception_stream, const unsigned &output_width, bool list_trace_back)
Definition: oomph_definitions.cc:144
void disable_error_message()
Definition: oomph_definitions.h:178
std::stringstream * Exception_stringstream_pt
String stream that records the error message.
Definition: oomph_definitions.h:209
std::ostream * Exception_stream_pt
Exception stream to which we write message in destructor.
Definition: oomph_definitions.h:206
bool Suppress_error_message
Definition: oomph_definitions.h:213
Definition: oomph_definitions.h:149
~OomphLibQuietException()
The destructor cannot throw an exception (C++ STL standard)
Definition: oomph_definitions.h:155
OomphLibQuietException()
Constructor.
Definition: oomph_definitions.cc:114
Definition: oomph_definitions.h:267
static void set_output_width(const unsigned &output_width)
Definition: oomph_definitions.h:299
static unsigned Output_width
Width of output.
Definition: oomph_definitions.h:272
static std::ostream * Stream_pt
Output stream that is used to write the errors.
Definition: oomph_definitions.h:269
static void set_stream_pt(std::ostream *const &stream_pt)
Definition: oomph_definitions.h:292
OomphLibWarning(const std::string &warning_description, const std::string &function_name, const char *location)
Definition: oomph_definitions.h:277
Definition: oomph_definitions.h:339
virtual ~OutputModifier()
Empty virtual destructor.
Definition: oomph_definitions.h:345
OutputModifier()
Empty constructor.
Definition: oomph_definitions.h:342
virtual bool operator()(std::ostream &stream)
Definition: oomph_definitions.h:350
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
Definition: cxx11_tensor_map.cpp:237
std::ostream * Stream_pt
Definition: structured_cubic_point_source.cc:104
std::ofstream * Outfile
Output stream.
Definition: oomph_definitions.cc:252
std::vector< std::string > String
Definition: oomph_definitions.cc:300
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
bool Doc_comprehensive_timings
Definition: oomph_definitions.cc:49
unsigned Number
The unsigned.
Definition: oomph_definitions.cc:269
void clean_up_memory()
Definition: oomph_definitions.cc:86
void suppress_exception_error_messages()
Suppress error messages (e.g. because error has been caught)
Definition: oomph_definitions.cc:72
void setup()
Setup terminate helper.
Definition: oomph_definitions.cc:63
void spawn_errors_from_uncaught_errors()
Function to spawn messages from uncaught errors.
Definition: oomph_definitions.cc:79
std::stringstream * Exception_stringstream_pt
String stream that records the error message.
Definition: oomph_definitions.cc:103
std::ostream * Error_message_stream_pt
Stream to output error messages.
Definition: oomph_definitions.cc:100
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).
Definition: AnisotropicHookean.h:10
Nullstream oomph_nullstream
Single (global) instantiation of the Nullstream.
Definition: oomph_definitions.cc:313
OutputModifier default_output_modifier
Single global instatiation of the default output modifier.
Definition: oomph_definitions.cc:325
OomphInfo oomph_info
Definition: oomph_definitions.cc:319