Reference documentation for deal.II version Git 83f3a2a 2017-12-11 23:13:16 +0100
petsc_compatibility.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 /*
17  * Rather than using ifdefs everywhere, try to wrap older versions of PETSc
18  * functions in one place.
19  */
20 #ifndef dealii_petsc_compatibility_h
21 #define dealii_petsc_compatibility_h
22 
23 #include <deal.II/base/config.h>
24 #include <deal.II/lac/exceptions.h>
25 
26 #ifdef DEAL_II_WITH_PETSC
27 
28 #include <petscconf.h>
29 #include <petscksp.h>
30 #include <petscmat.h>
31 #include <petscpc.h>
32 
33 #include <string>
34 
35 DEAL_II_NAMESPACE_OPEN
36 
37 namespace PETScWrappers
38 {
46  typedef PetscBool PetscBooleanType DEAL_II_DEPRECATED;
47 
53  inline void set_option_value (const std::string &name,
54  const std::string &value)
55  {
56 #if DEAL_II_PETSC_VERSION_LT(3, 7, 0)
57  const PetscErrorCode ierr = PetscOptionsSetValue (name.c_str (), value.c_str ());
58 #else
59  const PetscErrorCode ierr = PetscOptionsSetValue (nullptr, name.c_str (), value.c_str ());
60 #endif
61  AssertThrow (ierr == 0, ExcPETScError(ierr));
62  }
63 
64 
65 
76  inline PetscErrorCode destroy_matrix (Mat &matrix)
77  {
78  // PETSc will check whether or not matrix is nullptr.
79  return MatDestroy (&matrix);
80  }
81 
82 
83 
94  inline PetscErrorCode destroy_krylov_solver (KSP &krylov_solver)
95  {
96  // PETSc will check whether or not matrix is nullptr.
97  return KSPDestroy (&krylov_solver);
98  }
99 
100 
101 
109  inline void set_matrix_option (Mat &matrix,
110  const MatOption option_name,
111  const PetscBool option_value = PETSC_FALSE)
112  {
113  const PetscErrorCode ierr = MatSetOption (matrix, option_name, option_value);
114  AssertThrow (ierr == 0, ExcPETScError(ierr));
115  }
116 
117 
118 
123  inline void close_matrix (Mat &matrix)
124  {
125 # ifdef DEBUG
126  set_matrix_option (matrix, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
127 # else
128  set_matrix_option (matrix, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
129 # endif
130  }
131 
132 
133 
139  inline void set_keep_zero_rows (Mat &matrix)
140  {
141  set_matrix_option (matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
142  }
143 }
144 
145 DEAL_II_NAMESPACE_CLOSE
146 
147 #endif // DEAL_II_WITH_PETSC
148 #endif // dealii_petsc_compatibility_h
PetscErrorCode destroy_matrix(Mat &matrix)
#define AssertThrow(cond, exc)
Definition: exceptions.h:398
void set_matrix_option(Mat &matrix, const MatOption option_name, const PetscBool option_value=PETSC_FALSE)
void set_option_value(const std::string &name, const std::string &value)
PetscBool PetscBooleanType
void set_keep_zero_rows(Mat &matrix)
PetscErrorCode destroy_krylov_solver(KSP &krylov_solver)
void close_matrix(Mat &matrix)