deal.II version GIT relicensing-6842-g793a97d2aa 2026-10-02 14:00:01+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\}}\)
Loading...
Searching...
No Matches
petsc_compatibility.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2023 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13/*
14 * Rather than using ifdefs everywhere, try to wrap older versions of PETSc
15 * functions in one place. This file contains functions that need access to
16 * internal PETSc headers that we don't want to expose to deal.II users
17 */
19
20#ifdef DEAL_II_WITH_PETSC
21
22# include <petsc/private/pcimpl.h>
23# include <petsc/private/petscimpl.h>
24# include <petsc/private/snesimpl.h>
25# include <petsc/private/tsimpl.h>
26# include <petscdm.h>
27
28// Shorthand notation for PETSc error codes.
29# define AssertPETSc(code) \
30 do \
31 { \
32 PetscErrorCode ierr = (code); \
33 AssertThrow(ierr == 0, ExcPETScError(ierr)); \
34 } \
35 while (false)
36
37
38#endif // DEAL_II_WITH_PETSC
39
41
42#ifdef DEAL_II_WITH_PETSC
43
44
45namespace PETScWrappers
46{
47 void
49 {
50 AssertPETSc(PetscObjectStateIncrease(reinterpret_cast<PetscObject>(v)));
51 }
52
53 void
55 {
56 AssertPETSc(PetscObjectStateIncrease(reinterpret_cast<PetscObject>(A)));
57 }
58
59 PetscErrorCode
60 pc_set_failed_reason(PC pc, PCFailedReason reason)
61 {
62# if DEAL_II_PETSC_VERSION_GTE(3, 14, 0)
63 return PCSetFailedReason(pc, reason);
64# else
65 pc->failedreason = reason;
66 return 0;
67# endif
68 }
69
70 void
72 {
73# if DEAL_II_PETSC_VERSION_GTE(3, 11, 0)
74 snes->jacobiandomainerror = PETSC_FALSE;
75# endif
76# if DEAL_II_PETSC_VERSION_GTE(3, 25, 0)
77 snes->functiondomainerror = PETSC_FALSE;
78 snes->objectivedomainerror = PETSC_FALSE;
79# else
80 snes->domainerror = PETSC_FALSE;
81# endif
82 }
83
84 void
86 {
87# if DEAL_II_PETSC_VERSION_GTE(3, 11, 0)
88 snes->jacobiandomainerror = PETSC_TRUE;
89# else
90 // There is no equivalent, and since this used to stop
91 // computations, we opt to set the converged reason
92 snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
93# endif
94 }
95
96 void
97 set_use_matrix_free(SNES snes, bool mf_operator, bool mf)
98 {
99# if DEAL_II_PETSC_VERSION_LT(3, 13, 1)
100 snes->mf = mf ? PETSC_TRUE : PETSC_FALSE;
101 snes->mf_operator = mf_operator ? PETSC_TRUE : PETSC_FALSE;
102# else
103 AssertPETSc(SNESSetUseMatrixFree(snes,
104 mf_operator ? PETSC_TRUE : PETSC_FALSE,
105 mf ? PETSC_TRUE : PETSC_FALSE));
106# endif
107 }
108
109 void
110 set_use_matrix_free(TS ts, const bool mf_operator, const bool mf)
111 {
112 SNES snes;
113 AssertPETSc(TSGetSNES(ts, &snes));
114 set_use_matrix_free(snes, mf_operator, mf);
115 }
116
117 void
118 ts_set_max_steps(TS ts, const PetscInt maxsteps)
119 {
120# if DEAL_II_PETSC_VERSION_LT(3, 8, 0)
121 if (maxsteps >= 0)
122 ts->max_steps = maxsteps;
123# else
124 AssertPETSc(TSSetMaxSteps(ts, maxsteps));
125# endif
126 }
127
128 void
129 ts_set_max_time(TS ts, const PetscReal maxtime)
130 {
131# if DEAL_II_PETSC_VERSION_LT(3, 8, 0)
132 if (maxtime != PETSC_DEFAULT)
133 ts->max_time = maxtime;
134# else
135 AssertPETSc(TSSetMaxTime(ts, maxtime));
136# endif
137 }
138
139 void
141 {
142 AssertPETSc(DMDestroy(&ts->dm));
143 }
144
145 unsigned int
147 {
148 PetscInt step;
149# if DEAL_II_PETSC_VERSION_LT(3, 8, 0)
150 step = ts->steps;
151# else
152 AssertPETSc(TSGetStepNumber(ts, &step));
153# endif
154 return static_cast<unsigned int>(step);
155 }
156
157 bool
159 {
160 return ts->snes ? true : false;
161 }
162
163} // namespace PETScWrappers
164
165
166#endif // DEAL_II_WITH_PETSC
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
void set_use_matrix_free(SNES snes, const bool mf_operator, const bool mf)
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)
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)