DOLFIN-X
DOLFIN-X C++ interface
NewtonSolver.h
1 // Copyright (C) 2005-2019 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 <memory>
12 #include <petscvec.h>
13 #include <utility>
14 
15 namespace dolfinx
16 {
17 
18 namespace la
19 {
20 class PETScKrylovSolver;
21 } // namespace la
22 
23 namespace nls
24 {
25 
26 class NonlinearProblem;
27 
30 
32 {
33 public:
36  explicit NewtonSolver(MPI_Comm comm);
37 
39  virtual ~NewtonSolver();
40 
48  std::pair<int, bool> solve(NonlinearProblem& nonlinear_function, Vec x);
49 
53  int krylov_iterations() const;
54 
57  double residual() const;
58 
61  double residual0() const;
62 
64  int max_it = 50;
65 
67  double rtol = 1e-9;
68 
70  double atol = 1e-10;
71 
72  // FIXME: change to string to enum
74  std::string convergence_criterion = "residual";
75 
77  bool report = true;
78 
81 
83  double relaxation_parameter = 1.0;
84 
85 protected:
94  virtual bool converged(const Vec r, const NonlinearProblem& nonlinear_problem,
95  std::size_t iteration);
96 
107  virtual void update_solution(Vec x, const Vec dx, double relaxation_parameter,
108  const NonlinearProblem& nonlinear_problem,
109  std::size_t iteration);
110 
111 private:
112  // Accumulated number of Krylov iterations since solve began
113  int _krylov_iterations;
114 
115  // Most recent residual and initial residual
116  double _residual, _residual0;
117 
118  // Solver
119  la::PETScKrylovSolver _solver;
120 
121  // Solution vector
122  Vec _dx;
123 
124  // MPI communicator
125  dolfinx::MPI::Comm _mpi_comm;
126 };
127 } // namespace nls
128 } // namespace dolfinx
A duplicate MPI communicator and manage lifetime of the communicator.
Definition: MPI.h:36
This class implements Krylov methods for linear systems of the form Ax = b. It is a wrapper for the K...
Definition: PETScKrylovSolver.h:30
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:64
double relaxation_parameter
Relaxation parameter.
Definition: NewtonSolver.h:83
std::pair< int, bool > solve(NonlinearProblem &nonlinear_function, Vec x)
Solve abstract nonlinear problem for given and Jacobian .
Definition: NewtonSolver.cpp:41
double atol
Absolute tolerance.
Definition: NewtonSolver.h:70
NewtonSolver(MPI_Comm comm)
Create nonlinear solver.
Definition: NewtonSolver.cpp:20
int krylov_iterations() const
Return number of Krylov iterations elapsed since solve started.
Definition: NewtonSolver.cpp:147
bool report
Monitor convergence.
Definition: NewtonSolver.h:77
double rtol
Relative tolerance.
Definition: NewtonSolver.h:67
double residual0() const
Return initial residual.
Definition: NewtonSolver.cpp:151
virtual ~NewtonSolver()
Destructor.
Definition: NewtonSolver.cpp:34
bool error_on_nonconvergence
Throw error if solver fails to converge.
Definition: NewtonSolver.h:80
double residual() const
Return current residual.
Definition: NewtonSolver.cpp:149
std::string convergence_criterion
Convergence criterion.
Definition: NewtonSolver.h:74
This is a base class for nonlinear problems which can return the nonlinear function F(u) and its Jaco...
Definition: NonlinearProblem.h:19