Reference documentation for deal.II version 9.1.0-pre
petsc_compatibility.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 2017 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 /*
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 
25 #include <deal.II/lac/exceptions.h>
26 
27 #ifdef DEAL_II_WITH_PETSC
28 
29 # include <petscconf.h>
30 # include <petscksp.h>
31 # include <petscmat.h>
32 # include <petscpc.h>
33 
34 # include <string>
35 
36 DEAL_II_NAMESPACE_OPEN
37 
38 namespace PETScWrappers
39 {
45  inline void
46  set_option_value(const std::string &name, const std::string &value)
47  {
48 # if DEAL_II_PETSC_VERSION_LT(3, 7, 0)
49  const PetscErrorCode ierr =
50  PetscOptionsSetValue(name.c_str(), value.c_str());
51 # else
52  const PetscErrorCode ierr =
53  PetscOptionsSetValue(nullptr, name.c_str(), value.c_str());
54 # endif
55  AssertThrow(ierr == 0, ExcPETScError(ierr));
56  }
57 
58 
59 
70  inline PetscErrorCode
71  destroy_matrix(Mat &matrix)
72  {
73  // PETSc will check whether or not matrix is nullptr.
74  return MatDestroy(&matrix);
75  }
76 
77 
78 
89  inline PetscErrorCode
90  destroy_krylov_solver(KSP &krylov_solver)
91  {
92  // PETSc will check whether or not matrix is nullptr.
93  return KSPDestroy(&krylov_solver);
94  }
95 
96 
97 
105  inline void
106  set_matrix_option(Mat & matrix,
107  const MatOption option_name,
108  const PetscBool option_value = PETSC_FALSE)
109  {
110  const PetscErrorCode ierr = MatSetOption(matrix, option_name, option_value);
111  AssertThrow(ierr == 0, ExcPETScError(ierr));
112  }
113 
114 
115 
120  inline void
121  close_matrix(Mat &matrix)
122  {
123 # ifdef DEBUG
124  set_matrix_option(matrix, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
125 # else
126  set_matrix_option(matrix, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
127 # endif
128  }
129 
130 
131 
137  inline void
138  set_keep_zero_rows(Mat &matrix)
139  {
140  set_matrix_option(matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
141  }
142 } // namespace PETScWrappers
143 
144 DEAL_II_NAMESPACE_CLOSE
145 
146 #endif // DEAL_II_WITH_PETSC
147 #endif // dealii_petsc_compatibility_h
PetscErrorCode destroy_matrix(Mat &matrix)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1329
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)
void set_keep_zero_rows(Mat &matrix)
PetscErrorCode destroy_krylov_solver(KSP &krylov_solver)
void close_matrix(Mat &matrix)