Reference documentation for deal.II version 8.5.1
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 {
39 #if DEAL_II_PETSC_VERSION_LT(3,2,0)
40  typedef PetscTruth PetscBooleanType;
41 #else
42  typedef PetscBool PetscBooleanType;
43 #endif
44 
50  inline void set_option_value (const std::string &name,
51  const std::string &value)
52  {
53 #if DEAL_II_PETSC_VERSION_LT(3, 7, 0)
54  const PetscErrorCode ierr = PetscOptionsSetValue (name.c_str (), value.c_str ());
55 #else
56  const PetscErrorCode ierr = PetscOptionsSetValue (NULL, name.c_str (), value.c_str ());
57 #endif
58  AssertThrow (ierr == 0, ExcPETScError(ierr));
59  }
60 
61 
62 
73  inline PetscErrorCode destroy_matrix (Mat &matrix)
74  {
75  // PETSc will check whether or not matrix is NULL.
76 #if DEAL_II_PETSC_VERSION_LT(3, 2, 0)
77  return MatDestroy (matrix);
78 #else
79  return MatDestroy (&matrix);
80 #endif
81  }
82 
83 
84 
95  inline PetscErrorCode destroy_krylov_solver (KSP &krylov_solver)
96  {
97  // PETSc will check whether or not matrix is NULL.
98 #if DEAL_II_PETSC_VERSION_LT(3, 2, 0)
99  return KSPDestroy (krylov_solver);
100 #else
101  return KSPDestroy (&krylov_solver);
102 #endif
103  }
104 
105 
106 
114  inline void set_matrix_option (Mat &matrix,
115  const MatOption option_name,
116  const PetscBooleanType option_value = PETSC_FALSE)
117  {
118 #if DEAL_II_PETSC_VERSION_LT(3,0,0)
119  const PetscErrorCode ierr = MatSetOption (matrix, option_name);
120 #else
121  const PetscErrorCode ierr = MatSetOption (matrix, option_name, option_value);
122 #endif
123 
124  AssertThrow (ierr == 0, ExcPETScError(ierr));
125  }
126 
127 
128 
133  inline void close_matrix (Mat &matrix)
134  {
135 #if DEAL_II_PETSC_VERSION_LT(3, 0, 0)
136 # ifdef DEBUG
137  set_matrix_option (matrix, MAT_NEW_NONZERO_LOCATION_ERR);
138 # else
139  set_matrix_option (matrix, MAT_NO_NEW_NONZERO_LOCATIONS);
140 # endif
141 #else
142 # ifdef DEBUG
143  set_matrix_option (matrix, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
144 # else
145  set_matrix_option (matrix, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
146 # endif
147 #endif
148  }
149 
150 
151 
157  inline void set_keep_zero_rows (Mat &matrix)
158  {
159 #if DEAL_II_PETSC_VERSION_LT(3, 1, 0)
160  set_matrix_option (matrix, MAT_KEEP_ZEROED_ROWS, PETSC_TRUE);
161 #else
162  set_matrix_option (matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
163 #endif
164  }
165 }
166 
167 DEAL_II_NAMESPACE_CLOSE
168 
169 #endif // DEAL_II_WITH_PETSC
170 #endif // dealii__petsc_compatibility_h
PetscErrorCode destroy_matrix(Mat &matrix)
#define AssertThrow(cond, exc)
Definition: exceptions.h:369
void set_option_value(const std::string &name, const std::string &value)
void set_keep_zero_rows(Mat &matrix)
PetscErrorCode destroy_krylov_solver(KSP &krylov_solver)
void set_matrix_option(Mat &matrix, const MatOption option_name, const PetscBooleanType option_value=PETSC_FALSE)
void close_matrix(Mat &matrix)