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.h
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) 2016 - 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.
16 *
17 * Functions that are not inlined are:
18 * - Functions returning PetscErrorCode that are supposed to be called within
19 * PETSc callbacks.
20 * - Functions that need access to internal PETSc headers that we don't want
21 * to expose to deal.II users
22 */
23#ifndef dealii_petsc_compatibility_h
24#define dealii_petsc_compatibility_h
25
26#include <deal.II/base/config.h>
27
29
30#ifdef DEAL_II_WITH_PETSC
31
32# include <petscksp.h>
33# include <petscmat.h>
34# include <petscpc.h>
35# include <petscsnes.h>
36# include <petscts.h>
37# if DEAL_II_PETSC_VERSION_LT(3, 19, 0)
38# define PETSC_SUCCESS 0
39# endif
40
41# include <string>
42
43#endif // DEAL_II_WITH_PETSC
44
46
47#ifdef DEAL_II_WITH_PETSC
48namespace PETScWrappers
49{
54 inline void
55 set_option_value(const std::string &name, const std::string &value)
56 {
57 const PetscErrorCode ierr =
58 PetscOptionsSetValue(nullptr, name.c_str(), value.c_str());
59 AssertThrow(ierr == 0, ExcPETScError(ierr));
60 }
61
62
63
71 inline void
72 set_matrix_option(Mat &matrix,
73 const MatOption option_name,
74 const PetscBool option_value = PETSC_FALSE)
75 {
76 const PetscErrorCode ierr = MatSetOption(matrix, option_name, option_value);
77 AssertThrow(ierr == 0, ExcPETScError(ierr));
78 }
79
80
81
86 inline void
87 close_matrix(Mat &matrix)
88 {
89 if constexpr (running_in_debug_mode())
90 {
91 set_matrix_option(matrix, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
92 }
93 else
94 {
95 set_matrix_option(matrix, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
96 }
97 }
98
99
100
106 inline void
108 {
109 set_matrix_option(matrix, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
110 }
111
112
113
117 void
119
120
121
125 void
127
128
132 PetscErrorCode
133 pc_set_failed_reason(PC pc, PCFailedReason reason);
134
135
136
140 void
141 snes_reset_domain_flags(SNES snes);
142
143
144
148 void
150
151
152
162 void
163 set_use_matrix_free(SNES snes, const bool mf_operator, const bool mf);
164
165
166
176 void
177 set_use_matrix_free(TS ts, const bool mf_operator, const bool mf);
178
179
180
184 void
185 ts_reset_dm(TS ts);
186
187
188
192 void
193 ts_set_max_time(TS ts, const PetscReal maxtime);
194
195
196
200 void
201 ts_set_max_steps(TS ts, const PetscInt maxsteps);
202
203
204
208 unsigned int
209 ts_get_step_number(TS ts);
210
211
212
216 bool
217 ts_has_snes(TS ts);
218
219} // namespace PETScWrappers
220
221#endif // DEAL_II_WITH_PETSC
222
224#endif // dealii_petsc_compatibility_h
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
constexpr bool running_in_debug_mode()
Definition config.h:76
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define AssertThrow(cond, exc)
void set_use_matrix_free(SNES snes, const bool mf_operator, const bool mf)
void set_option_value(const std::string &name, const std::string &value)
void set_keep_zero_rows(Mat &matrix)
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 set_matrix_option(Mat &matrix, const MatOption option_name, const PetscBool option_value=PETSC_FALSE)
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)
void close_matrix(Mat &matrix)
unsigned int ts_get_step_number(TS ts)