DOLFINx 0.10.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{
36class NewtonSolver
37{
38public:
41 explicit NewtonSolver(MPI_Comm comm);
42
44 NewtonSolver(NewtonSolver&& solver) = default;
45
46 // Copy constructor (deleted)
47 NewtonSolver(const NewtonSolver& solver) = delete;
48
50 NewtonSolver& operator=(NewtonSolver&& solver) = default;
51
52 // Assignment operator (deleted)
53 NewtonSolver& operator=(const NewtonSolver& solver) = delete;
54
56 ~NewtonSolver();
57
64 void setF(std::function<void(const Vec, Vec)> F, Vec b);
65
72 void setJ(std::function<void(const Vec, Mat)> J, Mat Jmat);
73
83 void setP(std::function<void(const Vec, Mat)> P, Mat Pmat);
84
91 const la::petsc::KrylovSolver& get_krylov_solver() const;
92
99 la::petsc::KrylovSolver& get_krylov_solver();
100
106 void set_form(std::function<void(Vec)> form);
107
111 void set_convergence_check(
112 std::function<std::pair<double, bool>(const NewtonSolver&, const Vec)> c);
113
123 void set_update(
124 std::function<void(const NewtonSolver& solver, const Vec, Vec)> update);
125
131 std::pair<int, bool> solve(Vec x);
132
136 int iteration() const;
137
140 int krylov_iterations() const;
141
144 double residual() const;
145
148 double residual0() const;
149
151 MPI_Comm comm() const;
152
154 int max_it = 50;
155
157 double rtol = 1e-9;
158
160 double atol = 1e-10;
161
164 std::string convergence_criterion = "residual";
165
167 bool report = true;
168
170 bool error_on_nonconvergence = true;
171
173 double relaxation_parameter = 1.0;
174
175private:
176 // Function for computing the residual vector. The first argument is
177 // the latest solution vector x and the second argument is the
178 // residual vector.
179 std::function<void(const Vec x, Vec b)> _fnF;
180
181 // Function for computing the Jacobian matrix operator. The first
182 // argument is the latest solution vector x and the second argument is
183 // the matrix operator.
184 std::function<void(const Vec x, Mat J)> _fnJ;
185
186 // Function for computing the preconditioner matrix operator. The
187 // first argument is the latest solution vector x and the second
188 // argument is the matrix operator.
189 std::function<void(const Vec x, Mat P)> _fnP;
190
191 // Function called before the residual and Jacobian function at each
192 // iteration.
193 std::function<void(const Vec x)> _system;
194
195 // Residual vector
196 Vec _b = nullptr;
197
198 // Jacobian matrix and preconditioner matrix
199 Mat _matJ = nullptr, _matP = nullptr;
200
201 // Function to check for convergence
202 std::function<std::pair<double, bool>(const NewtonSolver& solver,
203 const Vec r)>
204 _converged;
205
206 // Function to update the solution after inner solve for the Newton increment
207 std::function<void(const NewtonSolver& solver, const Vec dx, Vec x)>
208 _update_solution;
209
210 // Accumulated number of Krylov iterations since solve began
211 int _krylov_iterations;
212
213 // Number of iterations
214 int _iteration;
215
216 // Most recent residual and initial residual
217 double _residual, _residual0;
218
219 // Linear solver
220 la::petsc::KrylovSolver _solver;
221
222 // Solution vector
223 Vec _dx = nullptr;
224
225 // MPI communicator
226 dolfinx::MPI::Comm _comm;
227};
228} // namespace nls::petsc
229} // namespace dolfinx
230
231#endif
A duplicate MPI communicator and manage lifetime of the communicator.
Definition MPI.h:44
Top-level namespace.
Definition defines.h:12