DOLFINx 0.11.0.0
DOLFINx C++ interface
Loading...
Searching...
No Matches
NewtonSolver.h
1// Copyright (C) 2005-2021 Garth N. Wells
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 <dolfinx/common/MPI.h>
12#include <dolfinx/la/petsc.h>
13#include <functional>
14#include <memory>
15#include <petscmat.h>
16#include <petscvec.h>
17#include <utility>
18
19namespace dolfinx
20{
21
22namespace la::petsc
23{
24class KrylovSolver;
25} // namespace la::petsc
26
27namespace nls::petsc
28{
39{
40public:
43 explicit NewtonSolver(MPI_Comm comm);
44
46 NewtonSolver(NewtonSolver&& solver) = default;
47
48 // Copy constructor (deleted)
49 NewtonSolver(const NewtonSolver& solver) = delete;
50
52 NewtonSolver& operator=(NewtonSolver&& solver) = default;
53
54 // Assignment operator (deleted)
55 NewtonSolver& operator=(const NewtonSolver& solver) = delete;
56
59
66 void setF(std::function<void(const Vec, Vec)> F, Vec b);
67
74 void setJ(std::function<void(const Vec, Mat)> J, Mat Jmat);
75
85 void setP(std::function<void(const Vec, Mat)> P, Mat Pmat);
86
94
102
108 void set_form(std::function<void(Vec)> form);
109
114 std::function<std::pair<double, bool>(const NewtonSolver&, const Vec)> c);
115
125 void set_update(
126 std::function<void(const NewtonSolver& solver, const Vec, Vec)> update);
127
133 std::pair<int, bool> solve(Vec x);
134
138 int iteration() const;
139
142 int krylov_iterations() const;
143
146 double residual() const;
147
150 double residual0() const;
151
153 MPI_Comm comm() const;
154
156 int max_it = 50;
157
159 double rtol = 1e-9;
160
162 double atol = 1e-10;
163
166 std::string convergence_criterion = "residual";
167
169 bool report = true;
170
173
176
177private:
178 // Function for computing the residual vector. The first argument is
179 // the latest solution vector x and the second argument is the
180 // residual vector.
181 std::function<void(const Vec x, Vec b)> _fnF;
182
183 // Function for computing the Jacobian matrix operator. The first
184 // argument is the latest solution vector x and the second argument is
185 // the matrix operator.
186 std::function<void(const Vec x, Mat J)> _fnJ;
187
188 // Function for computing the preconditioner matrix operator. The
189 // first argument is the latest solution vector x and the second
190 // argument is the matrix operator.
191 std::function<void(const Vec x, Mat P)> _fnP;
192
193 // Function called before the residual and Jacobian function at each
194 // iteration.
195 std::function<void(const Vec x)> _system;
196
197 // Residual vector
198 Vec _b = nullptr;
199
200 // Jacobian matrix and preconditioner matrix
201 Mat _matJ = nullptr, _matP = nullptr;
202
203 // Function to check for convergence
204 std::function<std::pair<double, bool>(const NewtonSolver& solver,
205 const Vec r)>
206 _converged;
207
208 // Function to update the solution after inner solve for the Newton increment
209 std::function<void(const NewtonSolver& solver, const Vec dx, Vec x)>
210 _update_solution;
211
212 // Accumulated number of Krylov iterations since solve began
213 int _krylov_iterations;
214
215 // Number of iterations
216 int _iteration;
217
218 // Most recent residual and initial residual
219 double _residual, _residual0;
220
221 // Linear solver
223
224 // Solution vector
225 Vec _dx = nullptr;
226
227 // MPI communicator
228 dolfinx::MPI::Comm _comm;
229};
230} // namespace nls::petsc
231} // namespace dolfinx
232
233#endif
A duplicate MPI communicator and manage lifetime of the communicator.
Definition MPI.h:42
Definition petsc.h:454
double relaxation_parameter
Relaxation parameter.
Definition NewtonSolver.h:175
int krylov_iterations() const
Number of Krylov iterations elapsed since solve started.
Definition NewtonSolver.cpp:278
void set_form(std::function< void(Vec)> form)
Set the function that is called before the residual or Jacobian are computed. It is commonly used to ...
Definition NewtonSolver.cpp:132
void setF(std::function< void(const Vec, Vec)> F, Vec b)
Set the function for computing the residual and the vector to assemble the residual into.
Definition NewtonSolver.cpp:97
NewtonSolver(NewtonSolver &&solver)=default
Move constructor.
double rtol
Relative convergence tolerance.
Definition NewtonSolver.h:159
~NewtonSolver()
Destructor.
Definition NewtonSolver.cpp:85
NewtonSolver & operator=(NewtonSolver &&solver)=default
Move assignment constructor.
double atol
Absolute convergence tolerance.
Definition NewtonSolver.h:162
NewtonSolver(MPI_Comm comm)
Create nonlinear solver.
Definition NewtonSolver.cpp:71
double residual() const
Get current residual.
Definition NewtonSolver.cpp:285
void setP(std::function< void(const Vec, Mat)> P, Mat Pmat)
Set the function for computing the preconditioner matrix.
Definition NewtonSolver.cpp:113
double residual0() const
Get initial residual.
Definition NewtonSolver.cpp:287
std::string convergence_criterion
Convergence criterion.
Definition NewtonSolver.h:166
int iteration() const
Get number of Newton iterations. It can be called by functions that check for convergence during a so...
Definition NewtonSolver.cpp:283
void set_update(std::function< void(const NewtonSolver &solver, const Vec, Vec)> update)
Optional set function that is called after each inner solve for the Newton increment to update the so...
Definition NewtonSolver.cpp:143
void setJ(std::function< void(const Vec, Mat)> J, Mat Jmat)
Set the function for computing the Jacobian and the matrix to assemble the Jacobian into.
Definition NewtonSolver.cpp:105
bool report
Monitor convergence.
Definition NewtonSolver.h:169
void set_convergence_check(std::function< std::pair< double, bool >(const NewtonSolver &, const Vec)> c)
Set function that is called at the end of each Newton iteration to test for convergence.
Definition NewtonSolver.cpp:137
bool error_on_nonconvergence
Throw error if solver fails to converge.
Definition NewtonSolver.h:172
MPI_Comm comm() const
Get MPI communicator.
Definition NewtonSolver.cpp:289
const la::petsc::KrylovSolver & get_krylov_solver() const
Get the internal Krylov solver used to solve for the Newton updates (const version).
Definition NewtonSolver.cpp:122
std::pair< int, bool > solve(Vec x)
Solve the nonlinear problem.
Definition NewtonSolver.cpp:149
int max_it
Maximum number of iterations.
Definition NewtonSolver.h:156
PETSc linear algebra functions.
Definition petsc.h:36
Top-level namespace.
Definition defines.h:12