petsc_solver_exception.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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 #include <petscsys.h>
27 
28 namespace libMesh
29 {
30 
31 // The SolverException class is only defined when exceptions are enabled.
32 #ifdef LIBMESH_ENABLE_EXCEPTIONS
33 
38 {
39 public:
40  PetscSolverException(int error_code_in) :
41  SolverException(error_code_in)
42  {
43  const char * text;
44  // This is one scenario where we don't catch the error code
45  // returned by a PETSc function :)
46  PetscErrorMessage(error_code, &text, nullptr);
47  what_message = text;
48  }
49 };
50 
51 
52 
53 // Macro which we call after every PETSc function that returns an error code.
54 #define LIBMESH_CHKERR(ierr) \
55  do { \
56  if (ierr != 0) { \
57  throw PetscSolverException(ierr); \
58  } } while (0)
59 
60 #else
61 
62 // If we don't have exceptions enabled, just fall back on calling
63 // PETSc's CHKERRABORT macro.
64 #define LIBMESH_CHKERR(ierr) CHKERRABORT(this->comm().get(), ierr);
65 
66 // Let's also be backwards-compatible with the old macro name.
67 #define LIBMESH_CHKERRABORT(ierr) LIBMESH_CHKERR(ierr)
68 
69 #endif
70 
71 } // namespace libMesh
72 
73 #endif // LIBMESH_HAVE_PETSC
74 #endif // LIBMESH_PETSC_SOLVER_EXCEPTION_H