Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/v0.3.0/v0.9.0/cpp
DOLFINx  0.3.0
DOLFINx C++ interface
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 #include <dolfinx/common/MPI.h>
10 #include <dolfinx/la/PETScKrylovSolver.h>
11 #include <functional>
12 #include <memory>
13 #include <petscmat.h>
14 #include <petscvec.h>
15 #include <utility>
16 
17 namespace dolfinx
18 {
19 
20 namespace la
21 {
22 class PETScKrylovSolver;
23 } // namespace la
24 
25 namespace nls
26 {
27 
30 
32 {
33 public:
36  explicit NewtonSolver(MPI_Comm comm);
37 
38  // Move constructor (deleted)
39  NewtonSolver(NewtonSolver&& solver) = delete;
40 
41  // Copy constructor (deleted)
42  NewtonSolver(const NewtonSolver& solver) = delete;
43 
44  // Assignment operator (deleted)
45  NewtonSolver& operator=(const NewtonSolver& solver) = delete;
46 
47  // Move assignment constructor (deleted)
48  NewtonSolver& operator=(const NewtonSolver&& solver) = delete;
49 
51  ~NewtonSolver();
52 
57  void setF(const std::function<void(const Vec, Vec)>& F, Vec b);
58 
63  void setJ(const std::function<void(const Vec, Mat)>& J, Mat Jmat);
64 
68  void setP(const std::function<void(const Vec, Mat)>& P, Mat Pmat);
69 
75 
81 
86  void set_form(const std::function<void(Vec x)>& form);
87 
91  void
92  set_convergence_check(const std::function<std::pair<double, bool>(
93  const nls::NewtonSolver& solver, const Vec r)>& c);
94 
98  void set_update(const std::function<void(const nls::NewtonSolver& solver,
99  const Vec dx, Vec x)>& update);
100 
106  std::pair<int, bool> solve(Vec x);
107 
111  int iteration() const;
112 
116  int krylov_iterations() const;
117 
120  double residual() const;
121 
124  double residual0() const;
125 
127  MPI_Comm mpi_comm() const;
128 
130  int max_it = 50;
131 
133  double rtol = 1e-9;
134 
136  double atol = 1e-10;
137 
138  // FIXME: change to string to enum
140  std::string convergence_criterion = "residual";
141 
143  bool report = true;
144 
147 
149  double relaxation_parameter = 1.0;
150 
151 private:
152  // Function for computing the residual vector. The first argument is
153  // the latest solution vector x and the second argument is the
154  // residual vector.
155  std::function<void(const Vec x, Vec b)> _fnF;
156 
157  // Function for computing the Jacobian matrix operator. The first
158  // argument is the latest solution vector x and the second argument is
159  // the matrix operator.
160  std::function<void(const Vec x, Mat J)> _fnJ;
161 
162  // Function for computing the preconditioner matrix operator. The
163  // first argument is the latest solution vector x and the second
164  // argument is the matrix operator.
165  std::function<void(const Vec x, Mat P)> _fnP;
166 
167  // Function called before the residual and Jacobian function at each
168  // iteration.
169  std::function<void(const Vec x)> _system;
170 
171  // Residual vector
172  Vec _b = nullptr;
173 
174  // Jacobian matrix and preconditioner matrix
175  Mat _matJ = nullptr, _matP = nullptr;
176 
177  // Function to check for convergence
178  std::function<std::pair<double, bool>(const nls::NewtonSolver& solver,
179  const Vec r)>
180  _converged;
181 
182  // Function to update the solution once convergence is reached
183  std::function<void(const nls::NewtonSolver& solver, const Vec dx, Vec x)>
184  _update_solution;
185 
186  // Accumulated number of Krylov iterations since solve began
187  int _krylov_iterations;
188 
189  // Number of iterations
190  int _iteration;
191 
192  // Most recent residual and initial residual
193  double _residual, _residual0;
194 
195  // Linear solver
196  la::PETScKrylovSolver _solver;
197 
198  // Solution vector
199  Vec _dx = nullptr;
200 
201  // MPI communicator
202  dolfinx::MPI::Comm _mpi_comm;
203 };
204 } // namespace nls
205 } // namespace dolfinx
A duplicate MPI communicator and manage lifetime of the communicator.
Definition: MPI.h:32
This class implements Krylov methods for linear systems of the form Ax = b. It is a wrapper for the K...
Definition: PETScKrylovSolver.h:27
This class defines a Newton solver for nonlinear systems of equations of the form .
Definition: NewtonSolver.h:32
int max_it
Maximum number of iterations.
Definition: NewtonSolver.h:130
void setF(const std::function< void(const Vec, Vec)> &F, Vec b)
Set the function for computing the residual and the vector to the assemble the residual into.
Definition: NewtonSolver.cpp:92
std::pair< int, bool > solve(Vec x)
Solve the nonlinear problem for given and Jacobian .
Definition: NewtonSolver.cpp:145
void setP(const std::function< void(const Vec, Mat)> &P, Mat Pmat)
Set the function for computing the preconditioner matrix (optional)
Definition: NewtonSolver.cpp:108
double relaxation_parameter
Relaxation parameter.
Definition: NewtonSolver.h:149
const la::PETScKrylovSolver & get_krylov_solver() const
Get the internal Krylov solver used to solve for the Newton updates const version The Krylov solver p...
Definition: NewtonSolver.cpp:116
double atol
Absolute tolerance.
Definition: NewtonSolver.h:136
void set_update(const std::function< void(const nls::NewtonSolver &solver, const Vec dx, Vec x)> &update)
Set function that is called after each Newton iteration to update the solution.
Definition: NewtonSolver.cpp:138
NewtonSolver(MPI_Comm comm)
Create nonlinear solver.
Definition: NewtonSolver.cpp:65
void set_form(const std::function< void(Vec x)> &form)
Set the function that is called before the residual or Jacobian are computed. It is commonly used to ...
Definition: NewtonSolver.cpp:126
int iteration() const
The number of Newton interations. It can can called by functions that check for convergence during a ...
Definition: NewtonSolver.cpp:278
int krylov_iterations() const
Return number of Krylov iterations elapsed since solve started.
Definition: NewtonSolver.cpp:276
bool report
Monitor convergence.
Definition: NewtonSolver.h:143
double rtol
Relative tolerance.
Definition: NewtonSolver.h:133
double residual0() const
Return initial residual.
Definition: NewtonSolver.cpp:282
~NewtonSolver()
Destructor.
Definition: NewtonSolver.cpp:80
bool error_on_nonconvergence
Throw error if solver fails to converge.
Definition: NewtonSolver.h:146
double residual() const
Return current residual.
Definition: NewtonSolver.cpp:280
std::string convergence_criterion
Convergence criterion.
Definition: NewtonSolver.h:140
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: NewtonSolver.cpp:284
void set_convergence_check(const std::function< std::pair< double, bool >(const nls::NewtonSolver &solver, const Vec r)> &c)
Set function that is called at the end of each Newton iteration to test for convergence.
Definition: NewtonSolver.cpp:131
void setJ(const std::function< void(const Vec, Mat)> &J, Mat Jmat)
Set the function for computing the Jacobian (dF/dx) and the matrix to assemble the residual into.
Definition: NewtonSolver.cpp:100