libMesh
petsc_solver_exception.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 #ifndef LIBMESH_PETSC_SOLVER_EXCEPTION_H
19 #define LIBMESH_PETSC_SOLVER_EXCEPTION_H
20 
21 #include "libmesh/libmesh_common.h"
22 
23 #ifdef LIBMESH_HAVE_PETSC
24 
25 #include "libmesh_exceptions.h"
26 #ifdef I
27 # define LIBMESH_SAW_I
28 #endif
29 #include <petscsys.h>
30 #ifndef LIBMESH_SAW_I
31 # undef I // Avoid complex.h contamination
32 #endif
33 
34 namespace libMesh
35 {
36 
37 // The SolverException class is only defined when exceptions are enabled.
38 #ifdef LIBMESH_ENABLE_EXCEPTIONS
39 
44 {
45 public:
46  PetscSolverException(int error_code_in) :
47  SolverException(error_code_in)
48  {
49  const char * text;
50  // This is one scenario where we don't catch the error code
51  // returned by a PETSc function :)
52  PetscErrorMessage(error_code, &text, nullptr);
53  what_message = text;
54  }
55 };
56 
57 
58 
59 // Macro which we call after every PETSc function that returns an error code.
60 #define LIBMESH_CHKERR(ierr) \
61  do { \
62  if (ierr != 0) { \
63  throw PetscSolverException(ierr); \
64  } } while (0)
65 
66 #else
67 
68 // If we don't have exceptions enabled, just fall back on calling
69 // PETSc's CHKERRABORT macro.
70 #define LIBMESH_CHKERR(ierr) CHKERRABORT(this->comm().get(), ierr);
71 
72 // Let's also be backwards-compatible with the old macro name.
73 #define LIBMESH_CHKERRABORT(ierr) LIBMESH_CHKERR(ierr)
74 
75 #endif
76 
77 } // namespace libMesh
78 
79 #endif // LIBMESH_HAVE_PETSC
80 #endif // LIBMESH_PETSC_SOLVER_EXCEPTION_H
libMesh::SolverException::what_message
std::string what_message
string which holds the message built in the constructor.
Definition: libmesh_exceptions.h:142
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libmesh_exceptions.h
libMesh::PetscSolverException::PetscSolverException
PetscSolverException(int error_code_in)
Definition: petsc_solver_exception.h:46
libMesh::SolverException::error_code
int error_code
The error code generated by the solver.
Definition: libmesh_exceptions.h:137
libMesh::PetscSolverException
A specialization of the SolverException class for PETSc.
Definition: petsc_solver_exception.h:43
libMesh::SolverException
A class representing an exception during a solve.
Definition: libmesh_exceptions.h:107