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