Reference documentation for deal.II version GIT 5ac9d67d2d 2023-06-07 21:45:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
exceptions.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2022 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_exceptions_h
17 #define dealii_exceptions_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <Kokkos_Core.hpp>
22 
23 #include <exception>
24 #include <ostream>
25 #include <string>
26 #include <type_traits>
27 
28 #ifdef DEAL_II_WITH_CUDA
29 # include <cusolverSp.h>
30 # include <cusparse.h>
31 #endif
32 
34 
35 
49 class ExceptionBase : public std::exception
50 {
51 public:
55  ExceptionBase();
56 
61 
65  virtual ~ExceptionBase() noexcept override = default;
66 
72  operator=(const ExceptionBase &) = delete;
73 
79  void
80  set_fields(const char *file,
81  const int line,
82  const char *function,
83  const char *cond,
84  const char *exc_name);
85 
86 
90  virtual const char *
91  what() const noexcept override;
92 
96  const char *
97  get_exc_name() const;
98 
102  void
103  print_exc_data(std::ostream &out) const;
104 
109  virtual void
110  print_info(std::ostream &out) const;
111 
116  void
117  print_stack_trace(std::ostream &out) const;
118 
119 protected:
123  const char *file;
124 
128  unsigned int line;
129 
133  const char *function;
134 
138  const char *cond;
139 
143  const char *exc;
144 
150 
151 #ifdef DEAL_II_HAVE_GLIBC_STACKTRACE
155  void *raw_stacktrace[25];
156 #endif
157 
158 private:
162  void
163  generate_message() const;
164 
169  mutable std::string what_str;
170 };
171 
172 #ifndef DOXYGEN
173 
190 # define DeclException0(Exception0) \
191  class Exception0 : public ::ExceptionBase \
192  {}
193 
194 
214 # define DeclExceptionMsg(Exception, defaulttext) \
215  class Exception : public ::ExceptionBase \
216  { \
217  public: \
218  Exception(const std::string &msg = defaulttext) \
219  : arg(msg) \
220  {} \
221  virtual ~Exception() noexcept \
222  {} \
223  virtual void \
224  print_info(std::ostream &out) const override \
225  { \
226  out << " " << arg << std::endl; \
227  } \
228  \
229  private: \
230  const std::string arg; \
231  }
232 
250 # define DeclException1(Exception1, type1, outsequence) \
251  class Exception1 : public ::ExceptionBase \
252  { \
253  public: \
254  Exception1(type1 const &a1) \
255  : arg1(a1) \
256  {} \
257  virtual ~Exception1() noexcept \
258  {} \
259  virtual void \
260  print_info(std::ostream &out) const override \
261  { \
262  out << " " outsequence << std::endl; \
263  } \
264  \
265  private: \
266  type1 const arg1; \
267  }
268 
269 
287 # define DeclException2(Exception2, type1, type2, outsequence) \
288  class Exception2 : public ::ExceptionBase \
289  { \
290  public: \
291  Exception2(type1 const &a1, type2 const &a2) \
292  : arg1(a1) \
293  , arg2(a2) \
294  {} \
295  virtual ~Exception2() noexcept \
296  {} \
297  virtual void \
298  print_info(std::ostream &out) const override \
299  { \
300  out << " " outsequence << std::endl; \
301  } \
302  \
303  private: \
304  type1 const arg1; \
305  type2 const arg2; \
306  }
307 
308 
326 # define DeclException3(Exception3, type1, type2, type3, outsequence) \
327  class Exception3 : public ::ExceptionBase \
328  { \
329  public: \
330  Exception3(type1 const &a1, type2 const &a2, type3 const &a3) \
331  : arg1(a1) \
332  , arg2(a2) \
333  , arg3(a3) \
334  {} \
335  virtual ~Exception3() noexcept \
336  {} \
337  virtual void \
338  print_info(std::ostream &out) const override \
339  { \
340  out << " " outsequence << std::endl; \
341  } \
342  \
343  private: \
344  type1 const arg1; \
345  type2 const arg2; \
346  type3 const arg3; \
347  }
348 
349 
367 # define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
368  class Exception4 : public ::ExceptionBase \
369  { \
370  public: \
371  Exception4(type1 const &a1, \
372  type2 const &a2, \
373  type3 const &a3, \
374  type4 const &a4) \
375  : arg1(a1) \
376  , arg2(a2) \
377  , arg3(a3) \
378  , arg4(a4) \
379  {} \
380  virtual ~Exception4() noexcept \
381  {} \
382  virtual void \
383  print_info(std::ostream &out) const override \
384  { \
385  out << " " outsequence << std::endl; \
386  } \
387  \
388  private: \
389  type1 const arg1; \
390  type2 const arg2; \
391  type3 const arg3; \
392  type4 const arg4; \
393  }
394 
395 
413 # define DeclException5( \
414  Exception5, type1, type2, type3, type4, type5, outsequence) \
415  class Exception5 : public ::ExceptionBase \
416  { \
417  public: \
418  Exception5(type1 const &a1, \
419  type2 const &a2, \
420  type3 const &a3, \
421  type4 const &a4, \
422  type5 const &a5) \
423  : arg1(a1) \
424  , arg2(a2) \
425  , arg3(a3) \
426  , arg4(a4) \
427  , arg5(a5) \
428  {} \
429  virtual ~Exception5() noexcept \
430  {} \
431  virtual void \
432  print_info(std::ostream &out) const override \
433  { \
434  out << " " outsequence << std::endl; \
435  } \
436  \
437  private: \
438  type1 const arg1; \
439  type2 const arg2; \
440  type3 const arg3; \
441  type4 const arg4; \
442  type5 const arg5; \
443  }
444 
445 #else /*ifndef DOXYGEN*/
446 
447 // Dummy definitions for doxygen:
448 
465 # define DeclException0(Exception0) \
466  \
467  static ::ExceptionBase &Exception0()
468 
488 # define DeclExceptionMsg(Exception, defaulttext) \
489  \
490  \
491  static ::ExceptionBase &Exception()
492 
510 # define DeclException1(Exception1, type1, outsequence) \
511  \
512  \
513  static ::ExceptionBase &Exception1(type1 arg1)
514 
515 
533 # define DeclException2(Exception2, type1, type2, outsequence) \
534  \
535  \
536  static ::ExceptionBase &Exception2(type1 arg1, type2 arg2)
537 
538 
556 # define DeclException3(Exception3, type1, type2, type3, outsequence) \
557  \
558  \
559  static ::ExceptionBase &Exception3(type1 arg1, type2 arg2, type3 arg3)
560 
561 
579 # define DeclException4(Exception4, type1, type2, type3, type4, outsequence) \
580  \
581  \
582  static ::ExceptionBase &Exception4(type1 arg1, \
583  type2 arg2, \
584  type3 arg3, \
585  type4 arg4)
586 
587 
605 # define DeclException5( \
606  Exception5, type1, type2, type3, type4, type5, outsequence) \
607  \
608  \
609  static ::ExceptionBase &Exception5( \
610  type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5)
611 
612 #endif /*ifndef DOXYGEN*/
613 
614 
627 {
637  "A piece of code is attempting a division by zero. This is "
638  "likely going to lead to results that make no sense.");
639 
652  std::complex<double>,
653  << "In a significant number of places, deal.II checks that some intermediate "
654  << "value is a finite number (as opposed to plus or minus infinity, or "
655  << "NaN/Not a Number). In the current function, we encountered a number "
656  << "that is not finite (its value is " << arg1 << " and therefore "
657  << "violates the current assertion).\n\n"
658  << "This may be due to the fact that some operation in this function "
659  << "created such a value, or because one of the arguments you passed "
660  << "to the function already had this value from some previous "
661  << "operation. In the latter case, this function only triggered the "
662  << "error but may not actually be responsible for the computation of "
663  << "the number that is not finite.\n\n"
664  << "There are two common cases where this situation happens. First, your "
665  << "code (or something in deal.II) divides by zero in a place where this "
666  << "should not happen. Or, you are trying to solve a linear system "
667  << "with an unsuitable solver (such as an indefinite or non-symmetric "
668  << "linear system using a Conjugate Gradient solver); such attempts "
669  << "oftentimes yield an operation somewhere that tries to divide "
670  << "by zero or take the square root of a negative value.\n\n"
671  << "In any case, when trying to find the source of the error, "
672  << "recall that the location where you are getting this error is "
673  << "simply the first place in the program where there is a check "
674  << "that a number (e.g., an element of a solution vector) is in fact "
675  << "finite, but that the actual error that computed the number "
676  << "may have happened far earlier. To find this location, you "
677  << "may want to add checks for finiteness in places of your "
678  << "program visited before the place where this error is produced. "
679  << "One way to check for finiteness is to use the 'AssertIsFinite' "
680  << "macro.");
681 
686  std::size_t,
687  "Your program tried to allocate some memory but this "
688  "allocation failed. Typically, this either means that "
689  "you simply do not have enough memory in your system, "
690  "or that you are (erroneously) trying to allocate "
691  "a chunk of memory that is simply beyond all reasonable "
692  "size, for example because the size of the object has "
693  "been computed incorrectly."
694  "\n\n"
695  "In the current case, the request was for "
696  << arg1 << " bytes.");
697 
703  int,
704  << "Destroying memory handler while " << arg1
705  << " objects are still allocated.");
706 
711  "An input/output error has occurred. There are a number of "
712  "reasons why this may be happening, both for reading and "
713  "writing operations."
714  "\n\n"
715  "If this happens during an operation that tries to read "
716  "data: First, you may be "
717  "trying to read from a file that doesn't exist or that is "
718  "not readable given its file permissions. Second, deal.II "
719  "uses this error at times if it tries to "
720  "read information from a file but where the information "
721  "in the file does not correspond to the expected format. "
722  "An example would be a truncated file, or a mesh file "
723  "that contains not only sections that describe the "
724  "vertices and cells, but also sections for additional "
725  "data that deal.II does not understand."
726  "\n\n"
727  "If this happens during an operation that tries to write "
728  "data: you may be trying to write to a file to which file "
729  "or directory permissions do not allow you to write. A "
730  "typical example is where you specify an output file in "
731  "a directory that does not exist.");
732 
740  std::string,
741  << "Could not open file " << arg1
742  << "."
743  "\n\n"
744  "If this happens during an operation that tries to read "
745  "data: you may be "
746  "trying to read from a file that doesn't exist or that is "
747  "not readable given its file permissions."
748  "\n\n"
749  "If this happens during an operation that tries to write "
750  "data: you may be trying to write to a file to which file "
751  "or directory permissions do not allow you to write. A "
752  "typical example is where you specify an output file in "
753  "a directory that does not exist.");
754 
764  "You are trying to use functionality in deal.II that is "
765  "currently not implemented. In many cases, this indicates "
766  "that there simply didn't appear much of a need for it, or "
767  "that the author of the original code did not have the "
768  "time to implement a particular case. If you hit this "
769  "exception, it is therefore worth the time to look into "
770  "the code to find out whether you may be able to "
771  "implement the missing functionality. If you do, please "
772  "consider providing a patch to the deal.II development "
773  "sources (see the deal.II website on how to contribute).");
774 
796  "This exception -- which is used in many places in the "
797  "library -- usually indicates that some condition which "
798  "the author of the code thought must be satisfied at a "
799  "certain point in an algorithm, is not fulfilled. An "
800  "example would be that the first part of an algorithm "
801  "sorts elements of an array in ascending order, and "
802  "a second part of the algorithm later encounters an "
803  "element that is not larger than the previous one."
804  "\n\n"
805  "There is usually not very much you can do if you "
806  "encounter such an exception since it indicates an error "
807  "in deal.II, not in your own program. Try to come up with "
808  "the smallest possible program that still demonstrates "
809  "the error and contact the deal.II mailing lists with it "
810  "to obtain help.");
811 
820  "You (or a place in the library) are trying to call a "
821  "function that is declared as a virtual function in a "
822  "base class but that has not been overridden in your "
823  "derived class."
824  "\n\n"
825  "This exception happens in cases where the base class "
826  "cannot provide a useful default implementation for "
827  "the virtual function, but where we also do not want "
828  "to mark the function as abstract (i.e., with '=0' at the end) "
829  "because the function is not essential to the class in many "
830  "contexts. In cases like this, the base class provides "
831  "a dummy implementation that makes the compiler happy, but "
832  "that then throws the current exception."
833  "\n\n"
834  "A concrete example would be the 'Function' class. It declares "
835  "the existence of 'value()' and 'gradient()' member functions, "
836  "and both are marked as 'virtual'. Derived classes have to "
837  "override these functions for the values and gradients of a "
838  "particular function. On the other hand, not every function "
839  "has a gradient, and even for those that do, not every program "
840  "actually needs to evaluate it. Consequently, there is no "
841  "*requirement* that a derived class actually override the "
842  "'gradient()' function (as there would be had it been marked "
843  "as abstract). But, since the base class cannot know how to "
844  "compute the gradient, if a derived class does not override "
845  "the 'gradient()' function and it is called anyway, then the "
846  "default implementation in the base class will simply throw "
847  "an exception."
848  "\n\n"
849  "The exception you see is what happens in cases such as the "
850  "one just illustrated. To fix the problem, you need to "
851  "investigate whether the function being called should indeed have "
852  "been called; if the answer is 'yes', then you need to "
853  "implement the missing override in your class.");
854 
859  std::string,
860  << "Please provide an implementation for the function \""
861  << arg1 << "\"");
862 
868  std::string,
869  int,
870  << "The function \"" << arg1 << "\" returned the nonzero value " << arg2
871  << ", but the calling site expected the return value to be zero. "
872  "This error often happens when the function in question is a 'callback', "
873  "that is a user-provided function called from somewhere within deal.II "
874  "or within an external library such as PETSc, Trilinos, SUNDIALS, etc., "
875  "that expect these callbacks to indicate errors via nonzero return "
876  "codes.");
877 
882 
887 
895  int,
896  << "You are trying to execute functionality that is "
897  << "impossible in " << arg1
898  << "d or simply does not make any sense.");
899 
908  int,
909  int,
910  << "You are trying to execute functionality that is "
911  << "impossible in dimensions <" << arg1 << ',' << arg2
912  << "> or simply does not make any sense.");
913 
914 
919  "In a check in the code, deal.II encountered a zero in "
920  "a place where this does not make sense. See the condition "
921  "that was being checked and that is printed further up "
922  "in the error message to get more information on what "
923  "the erroneous zero corresponds to.");
924 
930  "The object you are trying to access is empty but it makes "
931  "no sense to attempt the operation you are trying on an "
932  "empty object.");
933 
942  std::size_t,
943  std::size_t,
944  << "Two sizes or dimensions were supposed to be equal, "
945  << "but aren't. They are " << arg1 << " and " << arg2 << '.');
946 
952  std::size_t,
953  std::size_t,
954  std::size_t,
955  << "The size or dimension of one object, " << arg1
956  << " was supposed to be "
957  << "equal to one of two values, but isn't. The two possible "
958  << "values are " << arg2 << " and " << arg3 << '.');
959 
974  std::size_t,
975  std::size_t,
976  std::size_t,
977  << "Index " << arg1 << " is not in the half-open range [" << arg2 << ','
978  << arg3 << ")."
979  << (arg2 == arg3 ?
980  " In the current case, this half-open range is in fact empty, "
981  "suggesting that you are accessing an element of an empty "
982  "collection such as a vector that has not been set to the "
983  "correct size." :
984  ""));
985 
1001  template <typename T>
1004  T,
1005  T,
1006  T,
1007  << "Index " << arg1 << " is not in the half-open range [" << arg2 << ','
1008  << arg3 << ")."
1009  << (arg2 == arg3 ?
1010  " In the current case, this half-open range is in fact empty, "
1011  "suggesting that you are accessing an element of an empty "
1012  "collection such as a vector that has not been set to the "
1013  "correct size." :
1014  ""));
1015 
1020  int,
1021  int,
1022  << "Number " << arg1 << " must be larger than or equal "
1023  << arg2 << '.');
1024 
1028  template <typename T>
1030  T,
1031  T,
1032  << "Number " << arg1 << " must be larger than or equal "
1033  << arg2 << '.');
1034 
1040  int,
1041  int,
1042  << "Division " << arg1 << " by " << arg2
1043  << " has remainder different from zero.");
1044 
1054  "You are trying to use an iterator, but the iterator is "
1055  "in an invalid state. This may indicate that the iterator "
1056  "object has not been initialized, or that it has been "
1057  "moved beyond the end of the range of valid elements.");
1058 
1064  "You are trying to use an iterator, but the iterator is "
1065  "pointing past the end of the range of valid elements. "
1066  "It is not valid to dereference the iterator in this "
1067  "case.");
1068 
1082  DeclException1(ExcMessage, std::string, << arg1);
1083 
1088  "You are trying an operation on a vector that is only "
1089  "allowed if the vector has no ghost elements, but the "
1090  "vector you are operating on does have ghost elements. "
1091  "Specifically, vectors with ghost elements are read-only "
1092  "and cannot appear in operations that write into these "
1093  "vectors."
1094  "\n\n"
1095  "See the glossary entry on 'Ghosted vectors' for more "
1096  "information.");
1097 
1103  int,
1104  << "Something went wrong when making cell " << arg1
1105  << ". Read the docs and the source code "
1106  << "for more information.");
1107 
1116  "You are trying an operation of the form 'vector = C', "
1117  "'matrix = C', or 'tensor = C' with a nonzero scalar value "
1118  "'C'. However, such assignments are only allowed if the "
1119  "C is zero, since the semantics for assigning any other "
1120  "value are not clear. For example: one could interpret "
1121  "assigning a matrix a value of 1 to mean the matrix has a "
1122  "norm of 1, the matrix is the identity matrix, or the "
1123  "matrix contains only 1s. Similar problems exist with "
1124  "vectors and tensors. Hence, to avoid this ambiguity, such "
1125  "assignments are not permitted.");
1126 
1132  "You are attempting to use functionality that is only available "
1133  "if deal.II was configured to use LAPACK, but cmake did not "
1134  "find a valid LAPACK library.");
1135 
1140  ExcNeedsHDF5,
1141  "You are attempting to use functionality that requires that deal.II is configured "
1142  "with HDF5 support. However, when you called 'cmake', HDF5 support "
1143  "was not detected.");
1144 
1149  ExcNeedsMPI,
1150  "You are attempting to use functionality that is only available "
1151  "if deal.II was configured to use MPI.");
1152 
1158  "You are attempting to use functionality that is only available "
1159  "if deal.II was configured to use the function parser which "
1160  "relies on the muparser library, but cmake did not "
1161  "find a valid muparser library on your system and also did "
1162  "not choose the one that comes bundled with deal.II.");
1163 
1169  "You are attempting to use functionality that is only available "
1170  "if deal.II was configured to use Assimp, but cmake did not "
1171  "find a valid Assimp library.");
1172 
1173 #ifdef DEAL_II_WITH_CUDA
1180  DeclException1(ExcCudaError, const char *, << arg1);
1185  std::string,
1186  << "There was an error in a cuSPARSE function: " << arg1);
1187 #endif
1195  "You are attempting to use functionality that is only available if deal.II "
1196  "was configured to use Trilinos' SEACAS library (which provides ExodusII), "
1197  "but cmake did not find a valid SEACAS library.");
1198 
1203  ExcNeedsCGAL,
1204  "You are attempting to use functionality that is only available "
1205  "if deal.II was configured to use CGAL, but cmake did not "
1206  "find a valid CGAL library.");
1207 
1208 #ifdef DEAL_II_WITH_MPI
1230  class ExcMPI : public ::ExceptionBase
1231  {
1232  public:
1233  ExcMPI(const int error_code);
1234 
1235  virtual void
1236  print_info(std::ostream &out) const override;
1237 
1238  const int error_code;
1239  };
1240 #endif // DEAL_II_WITH_MPI
1241 
1242 
1243 
1244 #ifdef DEAL_II_TRILINOS_WITH_SEACAS
1253  class ExcExodusII : public ExceptionBase
1254  {
1255  public:
1261  ExcExodusII(const int error_code);
1262 
1266  virtual void
1267  print_info(std::ostream &out) const override;
1268 
1272  const int error_code;
1273  };
1274 #endif // DEAL_II_TRILINOS_WITH_SEACAS
1275 
1282  "A user call-back function encountered a recoverable error, "
1283  "but the underlying library that called the call-back did not "
1284  "manage to recover from the error and aborted its operation."
1285  "\n\n"
1286  "See the glossary entry on user call-back functions for more "
1287  "information.");
1288 } /*namespace StandardExceptions*/
1289 
1290 
1291 
1299 {
1300  namespace internals
1301  {
1309  extern bool allow_abort_on_exception;
1310  } // namespace internals
1311 
1330  void
1331  set_additional_assert_output(const char *const p);
1332 
1343  void
1345 
1360  void
1362 
1371  void
1373 
1381  namespace internals
1382  {
1387  [[noreturn]] void
1388  abort(const ExceptionBase &exc) noexcept;
1389 
1394  {
1405  };
1406 
1424  template <class ExceptionType>
1425  [[noreturn]] void
1427  const char * file,
1428  int line,
1429  const char * function,
1430  const char * cond,
1431  const char * exc_name,
1432  ExceptionType e)
1433  {
1434  // Fill the fields of the exception object
1435  e.set_fields(file, line, function, cond, exc_name);
1436 
1437  switch (handling)
1438  {
1440  {
1444  else
1445  {
1446  // We are not allowed to abort, so just throw the error:
1447  throw e;
1448  }
1449  }
1451  throw e;
1452  // this function should never return (and AssertNothrow can);
1453  // something must have gone wrong in the error handling code for us
1454  // to get this far, so throw an exception.
1455  default:
1457  }
1458  }
1459 
1463  void
1464  do_issue_error_nothrow(const ExceptionBase &e) noexcept;
1465 
1474  template <class ExceptionType>
1475  void
1476  issue_error_nothrow(const char * file,
1477  int line,
1478  const char * function,
1479  const char * cond,
1480  const char * exc_name,
1481  ExceptionType e) noexcept
1482  {
1483  static_assert(std::is_base_of<ExceptionBase, ExceptionType>::value,
1484  "The provided exception must inherit from ExceptionBase.");
1485  // Fill the fields of the exception object
1486  e.set_fields(file, line, function, cond, exc_name);
1487  // avoid moving a bunch of code into the header by dispatching to
1488  // another function:
1490  }
1491 #ifdef DEAL_II_WITH_CUDA
1497  std::string
1498  get_cusparse_error_string(const cusparseStatus_t error_code);
1499 
1505  std::string
1506  get_cusolver_error_string(const cusolverStatus_t error_code);
1507 #endif
1508  } /*namespace internals*/
1509 
1510 } /*namespace deal_II_exceptions*/
1511 
1512 
1513 
1539 #ifdef DEBUG
1540 # if KOKKOS_VERSION >= 30600
1541 # ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1542 # define Assert(cond, exc) \
1543  { \
1544  KOKKOS_IF_ON_HOST(({ \
1545  if (__builtin_expect(!(cond), false)) \
1546  ::deal_II_exceptions::internals::issue_error_noreturn( \
1547  ::deal_II_exceptions::internals::ExceptionHandling:: \
1548  abort_or_throw_on_exception, \
1549  __FILE__, \
1550  __LINE__, \
1551  __PRETTY_FUNCTION__, \
1552  #cond, \
1553  #exc, \
1554  exc); \
1555  })) \
1556  KOKKOS_IF_ON_DEVICE(({ \
1557  if (!(cond)) \
1558  Kokkos::abort(#cond); \
1559  })) \
1560  }
1561 # else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1562 # define Assert(cond, exc) \
1563  { \
1564  KOKKOS_IF_ON_HOST(({ \
1565  if (!(cond)) \
1566  ::deal_II_exceptions::internals::issue_error_noreturn( \
1567  ::deal_II_exceptions::internals::ExceptionHandling:: \
1568  abort_or_throw_on_exception, \
1569  __FILE__, \
1570  __LINE__, \
1571  __PRETTY_FUNCTION__, \
1572  #cond, \
1573  #exc, \
1574  exc); \
1575  })) \
1576  KOKKOS_IF_ON_DEVICE(({ \
1577  if (!(cond)) \
1578  Kokkos::abort(#cond); \
1579  })) \
1580  }
1581 # endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1582 # else /*if KOKKOS_VERSION >= 30600*/
1583 # ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
1584 # ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1585 # define Assert(cond, exc) \
1586  { \
1587  if (__builtin_expect(!(cond), false)) \
1588  ::deal_II_exceptions::internals::issue_error_noreturn( \
1589  ::deal_II_exceptions::internals::ExceptionHandling:: \
1590  abort_or_throw_on_exception, \
1591  __FILE__, \
1592  __LINE__, \
1593  __PRETTY_FUNCTION__, \
1594  #cond, \
1595  #exc, \
1596  exc); \
1597  }
1598 # else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1599 # define Assert(cond, exc) \
1600  { \
1601  if (!(cond)) \
1602  ::deal_II_exceptions::internals::issue_error_noreturn( \
1603  ::deal_II_exceptions::internals::ExceptionHandling:: \
1604  abort_or_throw_on_exception, \
1605  __FILE__, \
1606  __LINE__, \
1607  __PRETTY_FUNCTION__, \
1608  #cond, \
1609  #exc, \
1610  exc); \
1611  }
1612 # endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1613 # else /*#ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
1614 # define Assert(cond, exc) \
1615  { \
1616  if (!(cond)) \
1617  Kokkos::abort(#cond); \
1618  }
1619 # endif /*ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
1620 # endif /*KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
1621 #else /*ifdef DEBUG*/
1622 # define Assert(cond, exc) \
1623  {}
1624 #endif /*ifdef DEBUG*/
1625 
1626 
1627 
1654 #ifdef DEBUG
1655 # ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1656 # define AssertNothrow(cond, exc) \
1657  { \
1658  if (__builtin_expect(!(cond), false)) \
1659  ::deal_II_exceptions::internals::issue_error_nothrow( \
1660  __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
1661  }
1662 # else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1663 # define AssertNothrow(cond, exc) \
1664  { \
1665  if (!(cond)) \
1666  ::deal_II_exceptions::internals::issue_error_nothrow( \
1667  __FILE__, __LINE__, __PRETTY_FUNCTION__, #cond, #exc, exc); \
1668  }
1669 # endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1670 #else
1671 # define AssertNothrow(cond, exc) \
1672  {}
1673 #endif
1674 
1702 #ifdef DEAL_II_HAVE_BUILTIN_EXPECT
1703 # define AssertThrow(cond, exc) \
1704  { \
1705  if (__builtin_expect(!(cond), false)) \
1706  ::deal_II_exceptions::internals::issue_error_noreturn( \
1707  ::deal_II_exceptions::internals::ExceptionHandling:: \
1708  throw_on_exception, \
1709  __FILE__, \
1710  __LINE__, \
1711  __PRETTY_FUNCTION__, \
1712  #cond, \
1713  #exc, \
1714  exc); \
1715  }
1716 #else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1717 # define AssertThrow(cond, exc) \
1718  { \
1719  if (!(cond)) \
1720  ::deal_II_exceptions::internals::issue_error_noreturn( \
1721  ::deal_II_exceptions::internals::ExceptionHandling:: \
1722  throw_on_exception, \
1723  __FILE__, \
1724  __LINE__, \
1725  __PRETTY_FUNCTION__, \
1726  #cond, \
1727  #exc, \
1728  exc); \
1729  }
1730 #endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
1731 
1732 
1733 namespace deal_II_exceptions
1734 {
1735  namespace internals
1736  {
1742  template <typename T, typename U>
1743  inline DEAL_II_HOST_DEVICE constexpr bool
1744  compare_for_equality(const T &t, const U &u)
1745  {
1746  using common_type = std::common_type_t<T, U>;
1747  return static_cast<common_type>(t) == static_cast<common_type>(u);
1748  }
1749 
1750 
1756  template <typename T, typename U>
1757  inline DEAL_II_HOST_DEVICE constexpr bool
1758  compare_less_than(const T &t, const U &u)
1759  {
1760  using common_type = std::common_type_t<T, U>;
1761  return (static_cast<common_type>(t) < static_cast<common_type>(u));
1762  }
1763  } // namespace internals
1764 } // namespace deal_II_exceptions
1765 
1766 
1787 #define AssertDimension(dim1, dim2) \
1788  Assert(::deal_II_exceptions::internals::compare_for_equality(dim1, \
1789  dim2), \
1790  ::ExcDimensionMismatch((dim1), (dim2)))
1791 
1792 
1810 #define AssertVectorVectorDimension(VEC, DIM1, DIM2) \
1811  AssertDimension(VEC.size(), DIM1); \
1812  for (const auto &subvector : VEC) \
1813  { \
1814  (void)subvector; \
1815  AssertDimension(subvector.size(), DIM2); \
1816  }
1817 
1818 namespace internal
1819 {
1820  // Workaround to allow for commas in template parameter lists
1821  // in preprocessor macros as found in
1822  // https://stackoverflow.com/questions/13842468/comma-in-c-c-macro
1823  template <typename F>
1825 
1826  template <typename T, typename U>
1827  struct argument_type<T(U)>
1828  {
1829  using type = U;
1830  };
1831 
1832  template <typename F>
1834 } // namespace internal
1835 
1855 #define AssertIndexRange(index, range) \
1856  Assert(::deal_II_exceptions::internals::compare_less_than(index, \
1857  range), \
1858  ExcIndexRangeType<::internal::argument_type_t<void( \
1859  std::common_type_t<decltype(index), decltype(range)>)>>((index), \
1860  0, \
1861  (range)))
1862 
1882 #define AssertIsFinite(number) \
1883  Assert(::numbers::is_finite(number), \
1884  ::ExcNumberNotFinite(std::complex<double>(number)))
1885 
1891 #define AssertIsNotUsed(obj) Assert((obj)->used() == false, ExcInternalError())
1892 
1893 #ifdef DEAL_II_WITH_MPI
1914 # define AssertThrowMPI(error_code) \
1915  AssertThrow(error_code == MPI_SUCCESS, ::ExcMPI(error_code))
1916 #else
1917 # define AssertThrowMPI(error_code) \
1918  {}
1919 #endif // DEAL_II_WITH_MPI
1920 
1921 #ifdef DEAL_II_WITH_CUDA
1939 # ifdef DEBUG
1940 # define AssertCuda(error_code) \
1941  Assert(error_code == cudaSuccess, \
1942  ::ExcCudaError(cudaGetErrorString(error_code)))
1943 # else
1944 # define AssertCuda(error_code) \
1945  { \
1946  (void)(error_code); \
1947  }
1948 # endif
1949 
1966 # ifdef DEBUG
1967 # define AssertNothrowCuda(error_code) \
1968  AssertNothrow(error_code == cudaSuccess, \
1969  ::ExcCudaError(cudaGetErrorString(error_code)))
1970 # else
1971 # define AssertNothrowCuda(error_code) \
1972  { \
1973  (void)(error_code); \
1974  }
1975 # endif
1976 
1994 # ifdef DEBUG
1995 # define AssertCudaKernel() \
1996  { \
1997  cudaError_t local_error_code = cudaPeekAtLastError(); \
1998  AssertCuda(local_error_code); \
1999  local_error_code = cudaDeviceSynchronize(); \
2000  AssertCuda(local_error_code) \
2001  }
2002 # else
2003 # define AssertCudaKernel() \
2004  {}
2005 # endif
2006 
2024 # ifdef DEBUG
2025 # define AssertCusparse(error_code) \
2026  Assert( \
2027  error_code == CUSPARSE_STATUS_SUCCESS, \
2028  ExcCusparseError( \
2029  deal_II_exceptions::internals::get_cusparse_error_string( \
2030  error_code)))
2031 # else
2032 # define AssertCusparse(error_code) \
2033  { \
2034  (void)(error_code); \
2035  }
2036 # endif
2037 
2054 # ifdef DEBUG
2055 # define AssertNothrowCusparse(error_code) \
2056  AssertNothrow( \
2057  error_code == CUSPARSE_STATUS_SUCCESS, \
2058  ExcCusparseError( \
2059  deal_II_exceptions::internals::get_cusparse_error_string( \
2060  error_code)))
2061 # else
2062 # define AssertNothrowCusparse(error_code) \
2063  { \
2064  (void)(error_code); \
2065  }
2066 # endif
2067 
2085 # ifdef DEBUG
2086 # define AssertCusolver(error_code) \
2087  Assert( \
2088  error_code == CUSOLVER_STATUS_SUCCESS, \
2089  ExcCusparseError( \
2090  deal_II_exceptions::internals::get_cusolver_error_string( \
2091  error_code)))
2092 # else
2093 # define AssertCusolver(error_code) \
2094  { \
2095  (void)(error_code); \
2096  }
2097 # endif
2098 
2099 #endif
2100 
2101 #ifdef DEAL_II_TRILINOS_WITH_SEACAS
2119 # define AssertThrowExodusII(error_code) \
2120  AssertThrow(error_code == 0, \
2121  ::StandardExceptions::ExcExodusII(error_code));
2122 #endif // DEAL_II_TRILINOS_WITH_SEACAS
2123 
2124 using namespace StandardExceptions;
2125 
2127 
2128 #endif
void generate_message() const
Definition: exceptions.cc:322
void print_stack_trace(std::ostream &out) const
Definition: exceptions.cc:221
const char * file
Definition: exceptions.h:123
std::string what_str
Definition: exceptions.h:169
const char * get_exc_name() const
Definition: exceptions.cc:173
virtual ~ExceptionBase() noexcept override=default
int n_stacktrace_frames
Definition: exceptions.h:149
void set_fields(const char *file, const int line, const char *function, const char *cond, const char *exc_name)
Definition: exceptions.cc:136
virtual void print_info(std::ostream &out) const
Definition: exceptions.cc:213
virtual const char * what() const noexcept override
Definition: exceptions.cc:161
const char * exc
Definition: exceptions.h:143
const char * function
Definition: exceptions.h:133
void print_exc_data(std::ostream &out) const
Definition: exceptions.cc:181
unsigned int line
Definition: exceptions.h:128
void * raw_stacktrace[25]
Definition: exceptions.h:155
const char * cond
Definition: exceptions.h:138
virtual void print_info(std::ostream &out) const override
Definition: exceptions.cc:388
ExcMPI(const int error_code)
Definition: exceptions.cc:383
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:474
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:475
static ::ExceptionBase & ExcImpossibleInDimSpacedim(int arg1, int arg2)
#define DeclException0(Exception0)
Definition: exceptions.h:465
static ::ExceptionBase & ExcZero()
static ::ExceptionBase & ExcGridHasInvalidCell(int arg1)
static ::ExceptionBase & ExcCudaError(const char *arg1)
static ::ExceptionBase & ExcLowerRange(int arg1, int arg2)
static ::ExceptionBase & ExcDivideByZero()
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcNeedsLAPACK()
static ::ExceptionBase & ExcNumberNotFinite(std::complex< double > arg1)
static ::ExceptionBase & ExcNeedsHDF5()
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & RecoverableUserCallbackError()
static ::ExceptionBase & ExcNeedsMPI()
static ::ExceptionBase & ExcFileNotOpen(std::string arg1)
static ::ExceptionBase & ExcOutOfMemory(std::size_t arg1)
static ::ExceptionBase & ExcNeedsAssimp()
static ::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
static ::ExceptionBase & ExcPureFunctionCalled()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcFunctionNonzeroReturn(std::string arg1, int arg2)
static ::ExceptionBase & ExcIteratorPastEnd()
static ::ExceptionBase & ExcInvalidIterator()
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcInvalidState()
static ::ExceptionBase & ExcImpossibleInDim(int arg1)
static ::ExceptionBase & ExcLowerRangeType(T arg1, T arg2)
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:533
static ::ExceptionBase & ExcEmptyObject()
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:488
static ::ExceptionBase & ExcFunctionNotProvided(std::string arg1)
static ::ExceptionBase & ExcMemoryLeak(int arg1)
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcIO()
#define DeclException3(Exception3, type1, type2, type3, outsequence)
Definition: exceptions.h:556
static ::ExceptionBase & ExcCusparseError(std::string arg1)
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:510
static ::ExceptionBase & ExcNeedsFunctionparser()
static ::ExceptionBase & ExcDimensionMismatch2(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcIndexRangeType(T arg1, T arg2, T arg3)
static ::ExceptionBase & ExcNeedsCGAL()
static ::ExceptionBase & ExcGhostsPresent()
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcNotMultiple(int arg1, int arg2)
static ::ExceptionBase & ExcNeedsExodusII()
static const char U
static const char T
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
std::string get_cusolver_error_string(const cusolverStatus_t error_code)
Definition: exceptions.cc:569
constexpr bool compare_less_than(const T &t, const U &u)
Definition: exceptions.h:1758
std::string get_cusparse_error_string(const cusparseStatus_t error_code)
Definition: exceptions.cc:523
constexpr bool compare_for_equality(const T &t, const U &u)
Definition: exceptions.h:1744
void issue_error_noreturn(ExceptionHandling handling, const char *file, int line, const char *function, const char *cond, const char *exc_name, ExceptionType e)
Definition: exceptions.h:1426
void abort(const ExceptionBase &exc) noexcept
Definition: exceptions.cc:460
void do_issue_error_nothrow(const ExceptionBase &e) noexcept
Definition: exceptions.cc:506
void issue_error_nothrow(const char *file, int line, const char *function, const char *cond, const char *exc_name, ExceptionType e) noexcept
Definition: exceptions.h:1476
void enable_abort_on_exception()
Definition: exceptions.cc:89
void disable_abort_on_exception()
Definition: exceptions.cc:81
void set_additional_assert_output(const char *const p)
void suppress_stacktrace_in_exceptions()
Definition: exceptions.cc:73
typename argument_type< F >::type argument_type_t
Definition: exceptions.h:1833
#define DEAL_II_HOST_DEVICE
Definition: numbers.h:35