DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
SNESSolver.h
1// Copyright (C) 2026 Jack S. Hale
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#ifdef HAS_PETSC
10
11#include <exception>
12#include <functional>
13#include <mpi.h>
14#include <petscmat.h>
15#include <petscsnes.h>
16#include <petscsystypes.h>
17#include <petscvec.h>
18#include <string>
19#include <string_view>
20
21namespace dolfinx::nls::petsc
22{
43{
44public:
47 explicit SNESSolver(MPI_Comm comm);
48
65 SNESSolver(SNES snes, bool inc_ref_count);
66
67 // Copy constructor (deleted)
68 SNESSolver(const SNESSolver& solver) = delete;
69
73 SNESSolver(SNESSolver&& solver) noexcept;
74
77
78 // Copy assignment (deleted)
79 SNESSolver& operator=(const SNESSolver& solver) = delete;
80
82 SNESSolver& operator=(SNESSolver&& solver) noexcept;
83
100 void set_F(std::function<void(const Vec x, Vec b)> F, Vec b_layout);
101
121 void set_J(std::function<void(const Vec x, Mat Jmat, Mat Pmat)> J,
122 Mat J_layout, Mat P_layout = nullptr);
123
128 void set_update(std::function<void(PetscInt step)> update);
129
144 [[nodiscard(
145 "check the converged reason - positive on convergence, negative on "
146 "divergence")]] SNESConvergedReason
147 solve(Vec x);
148
153 void set_options_prefix(std::string_view options_prefix);
154
158 std::string get_options_prefix() const;
159
161 void set_from_options() const;
162
166 SNES snes() const;
167
168private:
169 // Callbacks passed to SNESSetFunction/SNESSetJacobian. The context
170 // pointer is the owning SNESSolver.
171 static PetscErrorCode residual(SNES snes, Vec x, Vec b, void* ctx);
172 static PetscErrorCode jacobian(SNES snes, Vec x, Mat Jmat, Mat Pmat,
173 void* ctx);
174
175 // Callback passed to SNESSetUpdate. It takes no context argument, so
176 // the owning SNESSolver is recovered from the context that set_F
177 // registered on the SNES object.
178 static PetscErrorCode update_step(SNES snes, PetscInt step);
179
180 // Register the callbacks that have been set, and attach this as the
181 // context that the callbacks recover the solver from
182 void set_callbacks();
183
184 // Function for computing the residual vector
185 std::function<void(const Vec x, Vec b)> _fnF;
186
187 // Function for computing the Jacobian and preconditioner matrices
188 std::function<void(const Vec x, Mat Jmat, Mat Pmat)> _fnJ;
189
190 // Function called before each nonlinear iteration
191 std::function<void(PetscInt step)> _fnupdate;
192
193 // Exception thrown by a callback during a solve, re-thrown by solve
194 std::exception_ptr _exception;
195
196 // Residual vector
197 Vec _b = nullptr;
198
199 // Jacobian matrix
200 Mat _matJ = nullptr;
201
202 // Preconditioner matrix
203 Mat _matP = nullptr;
204
205 // PETSc solver pointer
206 SNES _snes;
207};
208} // namespace dolfinx::nls::petsc
209
210#endif
~SNESSolver()
Destructor.
Definition SNESSolver.cpp:88
std::string get_options_prefix() const
Get the prefix used by PETSc when searching the PETSc options database.
Definition SNESSolver.cpp:223
void set_F(std::function< void(const Vec x, Vec b)> F, Vec b_layout)
Set the function for computing the residual , and the vector that defines its layout.
Definition SNESSolver.cpp:118
SNESConvergedReason solve(Vec x)
Solve .
Definition SNESSolver.cpp:173
SNESSolver(MPI_Comm comm)
Create a nonlinear solver.
Definition SNESSolver.cpp:55
void set_J(std::function< void(const Vec x, Mat Jmat, Mat Pmat)> J, Mat J_layout, Mat P_layout=nullptr)
Set the function for computing the Jacobian , and the matrices that define its layout.
Definition SNESSolver.cpp:136
SNES snes() const
Get the wrapped PETSc SNES object, e.g. to configure the line search or the Krylov solver used for ea...
Definition SNESSolver.cpp:239
void set_update(std::function< void(PetscInt step)> update)
Set a function called before each nonlinear iteration, e.g. to update a time- or step-dependent term.
Definition SNESSolver.cpp:165
void set_options_prefix(std::string_view options_prefix)
Set the prefix used by PETSc when searching the PETSc options database.
Definition SNESSolver.cpp:215
void set_from_options() const
Set options from the PETSc options database.
Definition SNESSolver.cpp:233