Reference documentation for deal.II version GIT e22cb6a53e 2023-09-21 14:00:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
petsc_compatibility.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2023 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. This file contains functions that need access to
19  * internal PETSc headers that we don't want to expose to deal.II users
20  */
22 
23 #ifdef DEAL_II_WITH_PETSC
24 
25 # include <petsc/private/pcimpl.h>
26 # include <petsc/private/petscimpl.h>
27 # include <petsc/private/snesimpl.h>
28 # include <petsc/private/tsimpl.h>
29 # include <petscdm.h>
30 
31 // Shorthand notation for PETSc error codes.
32 # define AssertPETSc(code) \
33  do \
34  { \
35  PetscErrorCode ierr = (code); \
36  AssertThrow(ierr == 0, ExcPETScError(ierr)); \
37  } \
38  while (0)
39 
41 
42 
43 namespace PETScWrappers
44 {
45  void
47  {
48  AssertPETSc(PetscObjectStateIncrease(reinterpret_cast<PetscObject>(v)));
49  }
50 
51  void
53  {
54  AssertPETSc(PetscObjectStateIncrease(reinterpret_cast<PetscObject>(A)));
55  }
56 
57  PetscErrorCode
58  pc_set_failed_reason(PC pc, PCFailedReason reason)
59  {
60 # if DEAL_II_PETSC_VERSION_GTE(3, 14, 0)
61  return PCSetFailedReason(pc, reason);
62 # else
63  pc->failedreason = reason;
64  return 0;
65 # endif
66  }
67 
68  void
70  {
71 # if DEAL_II_PETSC_VERSION_GTE(3, 11, 0)
72  snes->jacobiandomainerror = PETSC_FALSE;
73 # endif
74  snes->domainerror = PETSC_FALSE;
75  }
76 
77  void
79  {
80 # if DEAL_II_PETSC_VERSION_GTE(3, 11, 0)
81  snes->jacobiandomainerror = PETSC_TRUE;
82 # else
83  // There is no equivalent, and since this used to stop
84  // computations, we opt to set the converged reason
85  snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
86 # endif
87  }
88 
89  void
90  set_use_matrix_free(SNES snes, bool mf_operator, bool mf)
91  {
92 # if DEAL_II_PETSC_VERSION_LT(3, 13, 1)
93  snes->mf = mf ? PETSC_TRUE : PETSC_FALSE;
94  snes->mf_operator = mf_operator ? PETSC_TRUE : PETSC_FALSE;
95 # else
96  AssertPETSc(SNESSetUseMatrixFree(snes,
97  mf_operator ? PETSC_TRUE : PETSC_FALSE,
98  mf ? PETSC_TRUE : PETSC_FALSE));
99 # endif
100  }
101 
102  void
103  set_use_matrix_free(TS ts, const bool mf_operator, const bool mf)
104  {
105  SNES snes;
106  AssertPETSc(TSGetSNES(ts, &snes));
107  set_use_matrix_free(snes, mf_operator, mf);
108  }
109 
110  void
111  ts_set_max_steps(TS ts, const PetscInt maxsteps)
112  {
113 # if DEAL_II_PETSC_VERSION_LT(3, 8, 0)
114  if (maxsteps >= 0)
115  ts->max_steps = maxsteps;
116 # else
117  AssertPETSc(TSSetMaxSteps(ts, maxsteps));
118 # endif
119  }
120 
121  void
122  ts_set_max_time(TS ts, const PetscReal maxtime)
123  {
124 # if DEAL_II_PETSC_VERSION_LT(3, 8, 0)
125  if (maxtime != PETSC_DEFAULT)
126  ts->max_time = maxtime;
127 # else
128  AssertPETSc(TSSetMaxTime(ts, maxtime));
129 # endif
130  }
131 
132  void
133  ts_reset_dm(TS ts)
134  {
135  AssertPETSc(DMDestroy(&ts->dm));
136  }
137 
138  unsigned int
140  {
141  PetscInt step;
142 # if DEAL_II_PETSC_VERSION_LT(3, 8, 0)
143  step = ts->steps;
144 # else
145  AssertPETSc(TSGetStepNumber(ts, &step));
146 # endif
147  return static_cast<unsigned int>(step);
148  }
149 
150  bool
151  ts_has_snes(TS ts)
152  {
153  return ts->snes ? true : false;
154  }
155 
156 } // namespace PETScWrappers
157 
159 
160 #endif // DEAL_II_WITH_PETSC
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
static const char A
void set_use_matrix_free(SNES snes, const bool mf_operator, const bool mf)
void ts_reset_dm(TS ts)
PetscErrorCode pc_set_failed_reason(PC pc, PCFailedReason reason)
void ts_set_max_time(TS ts, const PetscReal maxtime)
void snes_reset_domain_flags(SNES snes)
void snes_set_jacobian_domain_error(SNES snes)
bool ts_has_snes(TS ts)
void ts_set_max_steps(TS ts, const PetscInt maxsteps)
void petsc_increment_state_counter(Vec v)
unsigned int ts_get_step_number(TS ts)
#define AssertPETSc(code)