LCOV - code coverage report
Current view: top level - include/base - libmesh_common.h (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4521 (694f88) with base 93e23a Lines: 28 44 63.6 %
Date: 2026-08-12 15:52:29 Functions: 198 246 80.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // The libMesh Finite Element Library.
       2             : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
       3             : 
       4             : // This library is free software; you can redistribute it and/or
       5             : // modify it under the terms of the GNU Lesser General Public
       6             : // License as published by the Free Software Foundation; either
       7             : // version 2.1 of the License, or (at your option) any later version.
       8             : 
       9             : // This library is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             : // Lesser General Public License for more details.
      13             : 
      14             : // You should have received a copy of the GNU Lesser General Public
      15             : // License along with this library; if not, write to the Free Software
      16             : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      17             : 
      18             : 
      19             : 
      20             : #ifndef LIBMESH_LIBMESH_COMMON_H
      21             : #define LIBMESH_LIBMESH_COMMON_H
      22             : 
      23             : // These flags should never be used together.  -DDEBUG means "turn on
      24             : // all the ridiculously expensive error checking"; -DNDEBUG means
      25             : // "turn off all the somewhat affordable error checking"
      26             : #if defined(DEBUG) && defined(NDEBUG)
      27             : #  error DEBUG and NDEBUG should never be defined simultaneously
      28             : #endif
      29             : 
      30             : // The library configuration options
      31             : #include "libmesh/libmesh_config.h"
      32             : 
      33             : // Use actual timestamps or constant dummies (to aid ccache)
      34             : #ifdef LIBMESH_ENABLE_TIMESTAMPS
      35             : #  define  LIBMESH_TIME __TIME__
      36             : #  define  LIBMESH_DATE __DATE__
      37             : #else
      38             : #  define  LIBMESH_TIME "notime"
      39             : #  define  LIBMESH_DATE "nodate"
      40             : #endif
      41             : 
      42             : // C/C++ includes everyone should know about
      43             : #include <cstdlib>
      44             : #ifdef __PGI
      45             : // BSK, Thu Feb 20 08:32:06 CST 2014 - For some reason, unless PGI gets
      46             : // <cmath> early this nonsense shows up:
      47             : // "/software/x86_64/pgi/12.9/linux86-64/12.9/include/CC/cmath", line 57: error:
      48             : //           the global scope has no "abs"
      49             : //   using _STLP_VENDOR_CSTD::abs;
      50             : // So include <cmath> as early as possible under the PGI compilers.
      51             : #  include <cmath>
      52             : #endif
      53             : #include <complex>
      54             : #include <typeinfo> // std::bad_cast
      55             : #include <type_traits> // std::decay
      56             : #include <functional> // std::less, etc
      57             : #include <string>
      58             : 
      59             : // Include the MPI definition
      60             : #ifdef LIBMESH_HAVE_MPI
      61             : # include "libmesh/ignore_warnings.h"
      62             : # include <mpi.h>
      63             : # include "libmesh/restore_warnings.h"
      64             : #endif
      65             : 
      66             : // Quad precision if we need it
      67             : #ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION
      68             : #include "libmesh/float128_shims.h"
      69             : #endif
      70             : 
      71             : // _basic_ library functionality
      72             : #include "libmesh/libmesh_base.h"
      73             : #include "libmesh/libmesh_exceptions.h"
      74             : 
      75             : // Proxy class for libMesh::out/err output
      76             : #include "libmesh/ostream_proxy.h"
      77             : 
      78             : // Make sure the libmesh_nullptr define is available for backwards
      79             : // compatibility, although we no longer use it in the library.
      80             : #include "libmesh/libmesh_nullptr.h"
      81             : 
      82             : // C++ headers
      83             : #include <iomanip> // setprecision, in assertion macros
      84             : 
      85             : namespace libMesh
      86             : {
      87             : namespace Threads
      88             : {
      89             : // For thread-safe error-messaging. Definitions in threads.h
      90             : void lock_singleton_spin_mutex();
      91             : void unlock_singleton_spin_mutex();
      92             : }
      93             : 
      94             : // Let's define a couple output streams - these will default
      95             : // to cout/cerr, but LibMeshInit (or the user) can also set them to
      96             : // something more sophisticated.
      97             : //
      98             : // We use a proxy class rather than references so they can be
      99             : // reseated at runtime.
     100             : 
     101             : extern OStreamProxy out;
     102             : extern OStreamProxy err;
     103             : 
     104             : // A namespace for functions used in the bodies of the macros below.
     105             : // The macros generally call these functions with __FILE__, __LINE__,
     106             : // __DATE__, and __TIME__ in the appropriate order.  These should not
     107             : // be called by users directly!  The implementations can be found in
     108             : // libmesh_common.C.
     109             : namespace MacroFunctions
     110             : {
     111             : void here(const char * file, int line, const char * date, const char * time, std::ostream & os = libMesh::err);
     112             : void stop(const char * file, int line, const char * date, const char * time);
     113             : void report_error(const char * file, int line, const char * date, const char * time, std::ostream & os = libMesh::err);
     114             : }
     115             : 
     116             : // Undefine any existing macros
     117             : #ifdef Real
     118             : #  undef Real
     119             : #endif
     120             : 
     121             : //#ifdef REAL
     122             : //#  undef REAL
     123             : //#endif
     124             : 
     125             : #ifdef Complex
     126             : #  undef Complex
     127             : #endif
     128             : 
     129             : #ifdef COMPLEX
     130             : #  undef COMPLEX
     131             : #endif
     132             : 
     133             : // Check to see if TOLERANCE has been defined by another
     134             : // package, if so we might want to change the name...
     135             : #ifdef TOLERANCE
     136             : DIE A HORRIBLE DEATH HERE...
     137             : #  undef TOLERANCE
     138             : #endif
     139             : 
     140             : 
     141             : 
     142             : // Define the type to use for real numbers
     143             : 
     144             : typedef LIBMESH_DEFAULT_SCALAR_TYPE Real;
     145             : 
     146             : // Define a corresponding tolerance.  This is what should be
     147             : // considered "good enough" when doing floating point comparisons.
     148             : // For example, v == 0 is changed to std::abs(v) < TOLERANCE.
     149             : 
     150             : #ifdef LIBMESH_DEFAULT_SINGLE_PRECISION
     151             : static constexpr Real TOLERANCE = 2.5e-3;
     152             : # if defined (LIBMESH_DEFAULT_TRIPLE_PRECISION) || \
     153             :      defined (LIBMESH_DEFAULT_QUADRUPLE_PRECISION)
     154             : #  error Cannot define multiple precision levels
     155             : # endif
     156             : #endif
     157             : 
     158             : #ifdef LIBMESH_DEFAULT_TRIPLE_PRECISION
     159             : static constexpr Real TOLERANCE = 1.e-8;
     160             : # if defined (LIBMESH_DEFAULT_QUADRUPLE_PRECISION)
     161             : #  error Cannot define multiple precision levels
     162             : # endif
     163             : #endif
     164             : 
     165             : #ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION
     166             : static constexpr Real TOLERANCE = 1.e-11;
     167             : #endif
     168             : 
     169             : #if !defined (LIBMESH_DEFAULT_SINGLE_PRECISION) && \
     170             :     !defined (LIBMESH_DEFAULT_TRIPLE_PRECISION) && \
     171             :     !defined (LIBMESH_DEFAULT_QUADRUPLE_PRECISION)
     172             : static constexpr Real TOLERANCE = 1.e-6;
     173             : #endif
     174             : 
     175             : // Define the type to use for complex numbers
     176             : // Always use std::complex<double>, as required by Petsc?
     177             : // If your version of Petsc doesn't support
     178             : // std::complex<other_precision>, then you'd better just leave
     179             : // Real==double
     180             : typedef std::complex<Real> Complex;
     181             : typedef std::complex<Real> COMPLEX;
     182             : 
     183             : 
     184             : // Helper functions for complex/real numbers
     185             : // to clean up #ifdef LIBMESH_USE_COMPLEX_NUMBERS elsewhere
     186    20240710 : template<typename T> inline T libmesh_real(T a) { return a; }
     187             : template<typename T> inline T libmesh_imag(T /*a*/) { return 0; }
     188    36672082 : template<typename T> inline T libmesh_conj(T a) { return a; }
     189             : 
     190             : template<typename T>
     191           0 : inline T libmesh_real(std::complex<T> a) { return std::real(a); }
     192             : 
     193             : template<typename T>
     194             : inline T libmesh_imag(std::complex<T> a) { return std::imag(a); }
     195             : 
     196             : template<typename T>
     197             : inline std::complex<T> libmesh_conj(std::complex<T> a) { return std::conj(a); }
     198             : 
     199             : // std::isnan() is in <cmath> as of C++11.
     200             : template <typename T>
     201      203273 : inline bool libmesh_isnan(T x) { return std::isnan(x); }
     202             : 
     203             : template <typename T>
     204             : inline bool libmesh_isnan(std::complex<T> a)
     205       42964 : { return (std::isnan(std::real(a)) || std::isnan(std::imag(a))); }
     206             : 
     207             : // std::isinf() is in <cmath> as of C++11.
     208             : template <typename T>
     209         660 : inline bool libmesh_isinf(T x) { return std::isinf(x); }
     210             : 
     211             : template <typename T>
     212             : inline bool libmesh_isinf(std::complex<T> a)
     213             : { return (std::isinf(std::real(a)) || std::isinf(std::imag(a))); }
     214             : 
     215             : // std::isfinite() doesn't support complex, but we really want an
     216             : // isfinite to support Number, at least in our own namespace
     217             : template <typename T>
     218             : inline bool isfinite(std::complex<T> a)
     219             : {
     220             :   using std::isfinite;
     221             :   return (isfinite(std::real(a)) && isfinite(std::imag(a)));
     222             : }
     223             : 
     224             : // Define the value type for unknowns in simulations.
     225             : // This is either Real or Complex, depending on how
     226             : // the library was configures
     227             : #if   defined (LIBMESH_USE_REAL_NUMBERS)
     228             : typedef Real Number;
     229             : #elif defined (LIBMESH_USE_COMPLEX_NUMBERS)
     230             : typedef Complex Number;
     231             : #else
     232             : DIE A HORRIBLE DEATH HERE...
     233             : #endif
     234             : 
     235             : 
     236             : // Define the value type for error estimates.
     237             : // Since AMR/C decisions don't have to be precise,
     238             : // we default to float for memory efficiency.
     239             : typedef float ErrorVectorReal;
     240             : #define MPI_ERRORVECTORREAL MPI_FLOAT
     241             : 
     242             : 
     243             : #ifdef LIBMESH_HAVE_MPI
     244             : 
     245             : /**
     246             :  * MPI Communicator used to initialize libMesh.
     247             :  */
     248             : extern MPI_Comm GLOBAL_COMM_WORLD;
     249             : #else
     250             : 
     251             : /**
     252             :  * Something to use with CHKERRABORT if we're just using PETSc's MPI
     253             :  * "uni" stub.
     254             :  */
     255             : extern int GLOBAL_COMM_WORLD;
     256             : #endif
     257             : 
     258             : // This global variable is to help us deprecate AutoPtr.  We can't
     259             : // just use libmesh_deprecated() because then you get one print out
     260             : // per template instantiation, instead of one total print out.
     261             : extern bool warned_about_auto_ptr;
     262             : 
     263             : // These are useful macros that behave like functions in the code.
     264             : // If you want to make sure you are accessing a section of code just
     265             : // stick a libmesh_here(); in it, for example
     266             : #define libmesh_here()                                                  \
     267             :   do {                                                                  \
     268             :     libMesh::MacroFunctions::here(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME); \
     269             :   } while (0)
     270             : 
     271             : // the libmesh_stop() macro will stop the code until a SIGCONT signal
     272             : // is received.  This is useful, for example, when determining the
     273             : // memory used by a given operation.  A libmesh_stop() could be
     274             : // inserted before and after a questionable operation and the delta
     275             : // memory can be obtained from a ps or top.  This macro only works for
     276             : // serial cases.
     277             : #define libmesh_stop()                                                  \
     278             :   do {                                                                  \
     279             :     libMesh::MacroFunctions::stop(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME); \
     280             :   } while (0)
     281             : 
     282             : // The libmesh_dbg_var() macro indicates that an argument to a function
     283             : // is used only in debug and devel modes (i.e., when NDEBUG is not defined).
     284             : #ifndef NDEBUG
     285             : #define libmesh_dbg_var(var) var
     286             : #else
     287             : #define libmesh_dbg_var(var)
     288             : #endif
     289             : 
     290             : // The libmesh_inf_var() macro indicates that an argument to a function
     291             : // is used only when infinite elements are enabled
     292             : #ifdef LIBMESH_ENABLE_INFINITE_ELEMENTS
     293             : #define libmesh_inf_var(var) var
     294             : #else
     295             : #define libmesh_inf_var(var)
     296             : #endif
     297             : 
     298             : // The libmesh_assert() macro acts like C's assert(), but throws a
     299             : // libmesh_error() (including stack trace, etc) instead of just exiting
     300             : #ifdef NDEBUG
     301             : 
     302             : #define libmesh_assert_msg(asserted, msg)  ((void) 0)
     303             : #define libmesh_exceptionless_assert_msg(asserted, msg)  ((void) 0)
     304             : #define libmesh_assert_equal_to_msg(expr1,expr2, msg)  ((void) 0)
     305             : #define libmesh_assert_not_equal_to_msg(expr1,expr2, msg)  ((void) 0)
     306             : #define libmesh_assert_less_msg(expr1,expr2, msg)  ((void) 0)
     307             : #define libmesh_assert_greater_msg(expr1,expr2, msg)  ((void) 0)
     308             : #define libmesh_assert_less_equal_msg(expr1,expr2, msg)  ((void) 0)
     309             : #define libmesh_assert_greater_equal_msg(expr1,expr2, msg)  ((void) 0)
     310             : 
     311             : #else
     312             : 
     313             : #define libmesh_assertion_types(expr1,expr2)                            \
     314             :   typedef typename std::decay<decltype(expr1)>::type libmesh_type1;     \
     315             :   typedef typename std::decay<decltype(expr2)>::type libmesh_type2
     316             : 
     317             : #define libmesh_assert_msg(asserted, msg)                               \
     318             :   do {                                                                  \
     319             :     if (!(asserted)) {                                                  \
     320             :       libmesh_error_msg("Assertion `" #asserted "' failed.\n" << msg);                                           \
     321             :     } } while (0)
     322             : 
     323             : #define libmesh_exceptionless_assert_msg(asserted, msg)                 \
     324             :   do {                                                                  \
     325             :     if (!(asserted)) {                                                  \
     326             :       libMesh::Threads::lock_singleton_spin_mutex();                    \
     327             :       libMesh::err << "Assertion `" #asserted "' failed." << std::endl; \
     328             :       libMesh::Threads::unlock_singleton_spin_mutex();                  \
     329             :       libmesh_exceptionless_error();                                    \
     330             :     } } while (0)
     331             : 
     332             : #define libmesh_assert_equal_to_msg(expr1,expr2, msg)                   \
     333             :   do {                                                                  \
     334             :     if (!((expr1) == (expr2))) {                                        \
     335             :       libmesh_error_msg(std::setprecision(17) << "Assertion `" #expr1 " == " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl); \
     336             :     } } while (0)
     337             : 
     338             : #define libmesh_assert_not_equal_to_msg(expr1,expr2, msg)               \
     339             :   do {                                                                  \
     340             :     if (!((expr1) != (expr2))) {                                        \
     341             :       libmesh_error_msg(std::setprecision(17) << "Assertion `" #expr1 " != " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl); \
     342             :     } } while (0)
     343             : 
     344             : template <template <class> class Comp>
     345             : struct casting_compare {
     346             : 
     347             :   template <typename T1, typename T2>
     348  1023534400 :   bool operator()(const T1 & e1, const T2 & e2) const
     349             :   {
     350             :     typedef typename std::decay<T1>::type DT1;
     351             :     typedef typename std::decay<T2>::type DT2;
     352  2047068800 :     return (Comp<DT2>()(static_cast<DT2>(e1), e2) &&
     353  2047068800 :             Comp<DT1>()(e1, static_cast<DT1>(e2)));
     354             :   }
     355             : 
     356             :   template <typename T1>
     357   185868619 :   bool operator()(const T1 & e1, const T1 & e2) const
     358             :   {
     359   185868619 :     return Comp<T1>()(e1, e2);
     360             :   }
     361             : };
     362             : 
     363             : #define libmesh_assert_less_msg(expr1,expr2, msg)                       \
     364             :   do {                                                                  \
     365             :     if (!libMesh::casting_compare<std::less>()(expr1, expr2)) {         \
     366             :       libmesh_error_msg(std::setprecision(17) << "Assertion `" #expr1 " < " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl); \
     367             :     } } while (0)
     368             : 
     369             : #define libmesh_assert_greater_msg(expr1,expr2, msg)                    \
     370             :   do {                                                                  \
     371             :     if (!libMesh::casting_compare<std::greater>()(expr1, expr2)) {      \
     372             :       libmesh_error_msg(std::setprecision(17) << "Assertion `" #expr1 " > " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl); \
     373             :     } } while (0)
     374             : 
     375             : #define libmesh_assert_less_equal_msg(expr1,expr2, msg)                 \
     376             :   do {                                                                  \
     377             :     if (!libMesh::casting_compare<std::less_equal>()(expr1, expr2)) {   \
     378             :       libmesh_error_msg(std::setprecision(17) << "Assertion `" #expr1 " <= " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl); \
     379             :     } } while (0)
     380             : 
     381             : #define libmesh_assert_greater_equal_msg(expr1,expr2, msg)              \
     382             :   do {                                                                  \
     383             :     if (!libMesh::casting_compare<std::greater_equal>()(expr1, expr2)) { \
     384             :       libmesh_error_msg(std::setprecision(17) << "Assertion `" #expr1 " >= " #expr2 "' failed.\n" #expr1 " = " << (expr1) << "\n" #expr2 " = " << (expr2) << '\n' << msg << std::endl); \
     385             :     } } while (0)
     386             : 
     387             : #endif
     388             : 
     389             : 
     390             : #define libmesh_assert(asserted) libmesh_assert_msg(asserted, "")
     391             : #define libmesh_exceptionless_assert(asserted) libmesh_exceptionless_assert_msg(asserted, "")
     392             : #define libmesh_assert_equal_to(expr1,expr2) libmesh_assert_equal_to_msg(expr1,expr2, "")
     393             : #define libmesh_assert_not_equal_to(expr1,expr2) libmesh_assert_not_equal_to_msg(expr1,expr2, "")
     394             : #define libmesh_assert_less(expr1,expr2) libmesh_assert_less_msg(expr1,expr2, "")
     395             : #define libmesh_assert_greater(expr1,expr2) libmesh_assert_greater_msg(expr1,expr2, "")
     396             : #define libmesh_assert_less_equal(expr1,expr2) libmesh_assert_less_equal_msg(expr1,expr2, "")
     397             : #define libmesh_assert_greater_equal(expr1,expr2) libmesh_assert_greater_equal_msg(expr1,expr2, "")
     398             : 
     399             : // The libmesh_error() macro prints a message and throws a LogicError
     400             : // exception
     401             : //
     402             : // The libmesh_not_implemented() macro prints a message and throws a
     403             : // NotImplemented exception
     404             : //
     405             : // The libmesh_file_error(const std::string & filename) macro prints a message
     406             : // and throws a FileError exception
     407             : //
     408             : // The libmesh_convergence_failure() macro
     409             : // throws a ConvergenceFailure exception
     410             : //
     411             : // The libmesh_degenerate_mapping() macro prints a message into and
     412             : // throws a DegenerateMap exception
     413             : //
     414             : // The libmesh_terminate() macro prints a message and throws a
     415             : // TerminationException exception
     416             : #define libmesh_error_msg(msg)                                          \
     417             :   do {                                                                  \
     418             :     std::stringstream message_stream;                                   \
     419             :     message_stream << msg << '\n';                                      \
     420             :     libMesh::MacroFunctions::report_error(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME, message_stream); \
     421             :     LIBMESH_THROW(libMesh::LogicError(message_stream.str()));           \
     422             :   } while (0)
     423             : 
     424             : #define libmesh_error() libmesh_error_msg("")
     425             : 
     426             : #define libmesh_error_msg_if(cond, msg)         \
     427             :   do {                                          \
     428             :     if (cond)                                   \
     429             :       libmesh_error_msg(msg);                   \
     430             :   } while (0)
     431             : 
     432             : #define libmesh_exceptionless_error_msg(msg)                            \
     433             :   do {                                                                  \
     434             :     libMesh::Threads::lock_singleton_spin_mutex();                      \
     435             :     libMesh::err << msg << '\n';                                        \
     436             :     libMesh::Threads::unlock_singleton_spin_mutex();                    \
     437             :     libmesh_try { libMesh::MacroFunctions::report_error(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME); } \
     438             :     libmesh_catch (...) {}                                              \
     439             :     std::terminate();                                                   \
     440             :   } while (0)
     441             : 
     442             : #define libmesh_exceptionless_error() libmesh_exceptionless_error_msg("")
     443             : 
     444             : #define libmesh_not_implemented_msg(msg)                                \
     445             :   do {                                                                  \
     446             :     std::stringstream message_stream;                                   \
     447             :     message_stream << msg << '\n';                                      \
     448             :     libMesh::MacroFunctions::report_error(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME, message_stream); \
     449             :     LIBMESH_THROW(libMesh::NotImplemented(message_stream.str()));       \
     450             :   } while (0)
     451             : 
     452             : #define libmesh_not_implemented() libmesh_not_implemented_msg("")
     453             : 
     454             : #define libmesh_file_error_msg(filename, msg)                           \
     455             :   do {                                                                  \
     456             :     std::stringstream message_stream;                                   \
     457             :     message_stream << msg << '\n';                                      \
     458             :     libMesh::MacroFunctions::report_error(__FILE__, __LINE__, LIBMESH_DATE, LIBMESH_TIME, message_stream); \
     459             :     LIBMESH_THROW(libMesh::FileError(filename, message_stream.str()));  \
     460             :   } while (0)
     461             : 
     462             : #define libmesh_file_error(filename) libmesh_file_error_msg(filename,"")
     463             : 
     464             : #define libmesh_convergence_failure()                   \
     465             :   do {                                                  \
     466             :     LIBMESH_THROW(libMesh::ConvergenceFailure());       \
     467             :   } while (0)
     468             : 
     469             : #define libmesh_degenerate_mapping_msg(msg)                          \
     470             :   do {                                                               \
     471             :     std::stringstream message_stream;                                \
     472             :     message_stream << msg << '\n';                                   \
     473             :     LIBMESH_THROW(libMesh::DegenerateMap(message_stream.str())); \
     474             :   } while (0)
     475             : 
     476             : #define libmesh_degenerate_mapping(filename) libmesh_degenerate_mapping_msg("")
     477             : 
     478             : #define libmesh_terminate()                         \
     479             :   do {                                              \
     480             :     LIBMESH_THROW(libMesh::TerminationException()); \
     481             :   } while (0)
     482             : 
     483             : // The libmesh_example_requires() macro prints a message and calls
     484             : // "return 77;" if the condition specified by the macro is not true.  This
     485             : // macro is used in the example executables, which should run when the
     486             : // configure-time libMesh options support them but which should exit
     487             : // without failure otherwise.
     488             : //
     489             : // This macro only works in main(), because we have no better way than
     490             : // "return" from main to immediately exit successfully - std::exit()
     491             : // gets seen by at least some MPI stacks as failure.
     492             : //
     493             : // 77 is the automake code for a skipped test.
     494             : 
     495             : #define libmesh_example_requires(condition, option)                     \
     496             :   do {                                                                  \
     497             :     if (!(condition)) {                                                 \
     498             :       libMesh::out << "Configuring libMesh with " << option << " is required to run this example." << std::endl; \
     499             :       return 77;                                                        \
     500             :     } } while (0)
     501             : 
     502             : // The libmesh_do_once macro helps us avoid redundant repeated
     503             : // repetitions of the same warning messages
     504             : #undef libmesh_do_once
     505             : #define libmesh_do_once(do_this)                \
     506             :   do {                                          \
     507             :     static bool did_this_already = false;       \
     508             :     if (!did_this_already) {                    \
     509             :       did_this_already = true;                  \
     510             :       do_this;                                  \
     511             :     } } while (0)
     512             : 
     513             : 
     514             : // The libmesh_warning macro outputs a file/line/time stamped warning
     515             : // message, if warnings are enabled.
     516             : #ifdef LIBMESH_ENABLE_WARNINGS
     517             : #define libmesh_warning(message)                                        \
     518             :   libmesh_do_once(libMesh::out << message                               \
     519             :                   << __FILE__ << ", line " << __LINE__ << ", compiled " << LIBMESH_DATE << " at " << LIBMESH_TIME << " ***" << std::endl;)
     520             : #else
     521             : #define libmesh_warning(message)  ((void) 0)
     522             : #endif
     523             : 
     524             : // The libmesh_experimental macro warns that you are using
     525             : // bleeding-edge code
     526             : #undef libmesh_experimental
     527             : #define libmesh_experimental()                                          \
     528             :   libmesh_warning("*** Warning, This code is untested, experimental, or likely to see future API changes: ");
     529             : 
     530             : 
     531             : // The libmesh_deprecated macro warns that you are using obsoleted code
     532             : #undef libmesh_deprecated
     533             : #ifndef LIBMESH_ENABLE_DEPRECATED
     534             : #define libmesh_deprecated()                                            \
     535             :   libmesh_error_msg("*** Error, This code is deprecated, and likely to be removed in future library versions! ");
     536             : #else
     537             : #define libmesh_deprecated()                                            \
     538             :   libmesh_warning("*** Warning, This code is deprecated, and likely to be removed in future library versions! ");
     539             : #endif
     540             : 
     541             : // A function template for ignoring unused variables.  This is a way
     542             : // to shut up unused variable compiler warnings on a case by case
     543             : // basis.
     544   329304404 : template<class ...Args> inline void libmesh_ignore( const Args&... ) { }
     545             : 
     546             : 
     547             : // A workaround for the lack of C++17 merge() support in some
     548             : // compilers
     549             : 
     550             : #ifdef LIBMESH_HAVE_CXX17_SPLICING
     551             : template <typename T>
     552       50184 : void libmesh_merge_move(T & target, T & source)
     553             : {
     554       50184 :   target.merge(std::move(source));
     555     1495998 : }
     556             : #else
     557             : template <typename T>
     558             : void libmesh_merge_move(T & target, T & source)
     559             : {
     560             :   target.insert(source.begin(), source.end());
     561             :   source.clear(); // Avoid forwards-incompatibility
     562             : }
     563             : #endif // LIBMESH_HAVE_CXX17_SPLICING
     564             : 
     565             : /**
     566             :  * Mostly system independent demangler
     567             :  */
     568             :  std::string demangle(const char * name);
     569             : 
     570             : // cast_ref and cast_ptr do a dynamic cast and assert
     571             : // the result, if we have RTTI enabled and we're in debug or
     572             : // development modes, but they just do a faster static cast if we're
     573             : // in optimized mode.
     574             : //
     575             : // Use these casts when you're certain that a cast will succeed in
     576             : // correct code but you want to be able to double-check.
     577             : template <typename Tnew, typename Told>
     578     8605826 : inline Tnew cast_ref(Told & oldvar)
     579             : {
     580             : #if !defined(NDEBUG) && defined(LIBMESH_HAVE_RTTI) && defined(LIBMESH_ENABLE_EXCEPTIONS)
     581             :   try
     582             :     {
     583     8605826 :       Tnew newvar = dynamic_cast<Tnew>(oldvar);
     584     8605826 :       return newvar;
     585             :     }
     586           0 :   catch (std::bad_cast &)
     587             :     {
     588           0 :       libMesh::err << "Failed to convert " << demangle(typeid(Told).name())
     589           0 :                    << " reference to " << demangle(typeid(Tnew).name())
     590           0 :                    << std::endl;
     591           0 :       libMesh::err << "The " << demangle(typeid(Told).name())
     592           0 :                    << " appears to be a "
     593           0 :                    << demangle(typeid(*(&oldvar)).name()) << std::endl;
     594           0 :       libmesh_error();
     595             :     }
     596             : #else
     597             :   return(static_cast<Tnew>(oldvar));
     598             : #endif
     599             : }
     600             : 
     601             : // We use two different function names to avoid an odd overloading
     602             : // ambiguity bug with icc 10.1.008
     603             : template <typename Tnew, typename Told>
     604   741194664 : inline Tnew cast_ptr (Told * oldvar)
     605             : {
     606             : #if !defined(NDEBUG) && defined(LIBMESH_HAVE_RTTI)
     607   741194664 :   Tnew newvar = dynamic_cast<Tnew>(oldvar);
     608   741194664 :   if (!newvar)
     609             :     {
     610           0 :       libMesh::err << "Failed to convert " << demangle(typeid(Told).name())
     611           0 :                    << " pointer to " << demangle(typeid(Tnew).name())
     612           0 :                    << std::endl;
     613           0 :       libMesh::err << "The " << demangle(typeid(Told).name())
     614           0 :                    << " appears to be a "
     615           0 :                    << demangle(typeid(*oldvar).name()) << std::endl;
     616           0 :       libmesh_error();
     617             :     }
     618   741194664 :   return newvar;
     619             : #else
     620             :   return(static_cast<Tnew>(oldvar));
     621             : #endif
     622             : }
     623             : 
     624             : 
     625             : #ifdef LIBMESH_ENABLE_DEPRECATED
     626             : template <typename Tnew, typename Told>
     627             : inline Tnew libmesh_cast_ptr (Told * oldvar)
     628             : {
     629             :   libmesh_deprecated();
     630             : 
     631             :   // we use the less redundantly named libMesh::cast_ptr now
     632             :   return cast_ptr<Tnew>(oldvar);
     633             : }
     634             : #endif // LIBMESH_ENABLE_DEPRECATED
     635             : 
     636             : 
     637             : // cast_int asserts that the value of the castee is within the
     638             : // bounds which are exactly representable by the output type, if we're
     639             : // in debug or development modes, but it just does a faster static
     640             : // cast if we're in optimized mode.
     641             : //
     642             : // Use these casts when you're certain that a cast will succeed in
     643             : // correct code but you want to be able to double-check.
     644             : template <typename Tnew, typename Told>
     645  9032516887 : inline Tnew cast_int (Told oldvar)
     646             : {
     647   134400077 :   libmesh_assert_equal_to
     648             :     (oldvar, static_cast<Told>(static_cast<Tnew>(oldvar)));
     649             : 
     650 21673651669 :   return(static_cast<Tnew>(oldvar));
     651             : }
     652             : 
     653             : 
     654             : template <typename Tnew, typename Told>
     655             : inline Tnew libmesh_cast_int (Told oldvar)
     656             : {
     657             :   // we use the less redundantly named libMesh::cast_int now
     658             :   return cast_int<Tnew>(oldvar);
     659             : }
     660             : 
     661             : 
     662             : /**
     663             :  * restrict_int checks that the value of the castee is within the
     664             :  * bounds which are exactly representable by the output type, even in
     665             :  * optimized modes.
     666             :  *
     667             :  * Use this cast when you suspect that the input may not succeed in
     668             :  * correct code (e.g. when an input file is being read from a format
     669             :  * that may allow wider integer types than the current libMesh
     670             :  * configuration).
     671             :  */
     672             : template <typename Tnew, typename Told>
     673      733940 : inline Tnew restrict_int (Told oldvar)
     674             : {
     675             :   if constexpr (!std::is_same_v<Tnew, Told>)
     676             :     {
     677      733940 :       const Tnew returnval = static_cast<Tnew>(oldvar);
     678             : 
     679      733940 :       libmesh_error_msg_if (oldvar != static_cast<Told>(returnval),
     680             :                             "restrict_int failed: " << oldvar << " does not fit in type " << typeid(returnval).name());
     681             :     }
     682             : 
     683      733940 :   return oldvar;
     684             : }
     685             : 
     686             : 
     687             : /**
     688             :  * This is a helper variable template for cases when we want to use a default compile-time
     689             :  * error with constexpr-based if conditions. The templating delays the triggering
     690             :  * of the static assertion until the template is instantiated.
     691             :  */
     692             : template <class T>
     693             : constexpr std::false_type always_false{};
     694             : 
     695             : static constexpr std::size_t libmesh_dim = LIBMESH_DIM;
     696             : 
     697             : // build a integer representation of version
     698             : #define LIBMESH_VERSION_ID(major,minor,patch) (((major) << 16) | ((minor) << 8) | ((patch) & 0xFF))
     699             : 
     700             : 
     701             : // libmesh_override is simply a synonym for override as we now require
     702             : // a C++11 compiler that supports this keyword.
     703             : #define libmesh_override override
     704             : 
     705             : // libmesh_delete is simply a synonym for '=delete' as we now require
     706             : // a C++11 compiler that supports this keyword.
     707             : #define libmesh_delete =delete
     708             : 
     709             : // libmesh_final is simply a synonym for 'final' as we now require
     710             : // a C++11 compiler that supports this keyword.
     711             : #define libmesh_final final
     712             : 
     713             : // Define backwards-compatible fallthrough attribute.  We could
     714             : // eventually also add support for other compiler-specific fallthrough
     715             : // attributes.
     716             : #ifdef LIBMESH_HAVE_CXX17_FALLTHROUGH_ATTRIBUTE
     717             : #define libmesh_fallthrough() [[fallthrough]]
     718             : #elif defined(LIBMESH_HAVE_DOUBLE_UNDERSCORE_ATTRIBUTE_FALLTHROUGH)
     719             : #define libmesh_fallthrough() __attribute__((fallthrough))
     720             : #else
     721             : #define libmesh_fallthrough() ((void) 0)
     722             : #endif
     723             : 
     724             : template <typename T>
     725             : class PassKey
     726             : {
     727             :   friend T;
     728             :   constexpr PassKey() = default;
     729             : };
     730             : } // namespace libMesh
     731             : 
     732             : 
     733             : // Backwards compatibility
     734             : namespace libMeshEnums
     735             : {
     736             : using namespace libMesh;
     737             : }
     738             : 
     739             : // Backwards compatibility with pre-TIMPI reference
     740             : namespace TIMPI {}
     741             : 
     742             : namespace libMesh {
     743             :   namespace Parallel {
     744             :     using namespace TIMPI;
     745             :   }
     746             : }
     747             : 
     748             : 
     749             : // Here we add missing types to the standard namespace.  For example,
     750             : // std::max(double, float) etc... are well behaved but not defined
     751             : // by the standard.  This also includes workarounds for super-strict
     752             : // implementations, for example Sun Studio and PGI C++.  However,
     753             : // this necessarily requires breaking the ISO-C++ standard, and is
     754             : // really just a hack.  As such, only do it if we are building the
     755             : // libmesh library itself.  Specifically, *DO NOT* export this to
     756             : // user code or install this header.
     757             : //
     758             : // We put this at the end of libmesh_common.h so we can make use of
     759             : // any exotic definitions of Real above.
     760             : #ifdef LIBMESH_IS_COMPILING_ITSELF
     761             : #  include "libmesh/libmesh_augment_std_namespace.h"
     762             : #endif
     763             : 
     764             : 
     765             : #ifdef _MSC_VER
     766             : #define LIBMESH_EXPORT __declspec(dllexport)
     767             : #else
     768             : #define LIBMESH_EXPORT
     769             : #endif
     770             : 
     771             : 
     772             : #endif // LIBMESH_LIBMESH_COMMON_H

Generated by: LCOV version 1.14