libMesh
nonlinear_solver.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_NONLINEAR_SOLVER_H
21 #define LIBMESH_NONLINEAR_SOLVER_H
22 
23 // Local includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/reference_counted_object.h"
26 #include "libmesh/nonlinear_implicit_system.h"
27 #include "libmesh/libmesh.h"
28 #include "libmesh/parallel_object.h"
29 
30 #ifdef LIBMESH_FORWARD_DECLARE_ENUMS
31 namespace libMesh
32 {
33 enum SolverPackage : int;
34 }
35 #else
36 #include "libmesh/enum_solver_package.h"
37 #endif
38 
39 // C++ includes
40 #include <cstddef>
41 #include <memory>
42 
43 namespace libMesh
44 {
45 
46 // forward declarations
47 template <typename T> class SparseMatrix;
48 template <typename T> class NumericVector;
49 template <typename T> class Preconditioner;
50 class SolverConfiguration;
51 
59 template <typename T>
60 class NonlinearSolver : public ReferenceCountedObject<NonlinearSolver<T>>,
61  public ParallelObject
62 {
63 public:
68 
72  explicit
74 
78  virtual ~NonlinearSolver ();
79 
84  static std::unique_ptr<NonlinearSolver<T>> build(sys_type & s,
85  const SolverPackage solver_package = libMesh::default_solver_package());
86 
91  bool initialized () const { return _is_initialized; }
92 
96  virtual void clear () {}
97 
102  virtual void init (const char * name = nullptr) = 0;
103 
107  virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> &, // System Jacobian Matrix
108  NumericVector<T> &, // Solution vector
109  NumericVector<T> &, // Residual vector
110  const double, // Stopping tolerance
111  const unsigned int) = 0; // N. Iterations
112 
117  virtual void print_converged_reason() { libmesh_not_implemented(); }
118 
122  virtual int get_total_linear_iterations() = 0;
123 
131  virtual unsigned get_current_nonlinear_iteration_number() const = 0;
132 
137  void (* residual) (const NumericVector<Number> & X,
139  sys_type & S);
140 
146 
152 
159 
164  void (* jacobian) (const NumericVector<Number> & X,
166  sys_type & S);
167 
173 
181  void (* matvec) (const NumericVector<Number> & X,
184  sys_type & S);
185 
194 
200  sys_type & S);
205 
212  void (* nullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
213 
221 
227  void (* transpose_nullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
228 
235 
241  void (* nearnullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
242 
249 
254  void (* user_presolve)(sys_type & S);
255 
262  void (* postcheck) (const NumericVector<Number> & old_soln,
263  NumericVector<Number> & search_direction,
264  NumericVector<Number> & new_soln,
265  bool & changed_search_direction,
266  bool & changed_new_soln,
267  sys_type & S);
268 
275 
279  const sys_type & system () const { return _system; }
280 
284  sys_type & system () { return _system; }
285 
289  void attach_preconditioner(Preconditioner<T> * preconditioner);
290 
295 
300 
313 
322 
336 
341  unsigned int max_linear_iterations;
342 
348 
353 
358  bool converged;
359 
363  void set_solver_configuration(SolverConfiguration & solver_configuration);
364 
365 protected:
370 
375 
380 
386 };
387 
388 
389 
390 
391 /*----------------------- inline functions ----------------------------------*/
392 template <typename T>
393 inline
395  ParallelObject (s),
396  residual (nullptr),
397  residual_object (nullptr),
398  fd_residual_object (nullptr),
399  mffd_residual_object (nullptr),
400  jacobian (nullptr),
401  jacobian_object (nullptr),
402  matvec (nullptr),
403  residual_and_jacobian_object (nullptr),
404  bounds (nullptr),
405  bounds_object (nullptr),
406  nullspace (nullptr),
407  nullspace_object (nullptr),
408  transpose_nullspace (nullptr),
409  transpose_nullspace_object (nullptr),
410  nearnullspace (nullptr),
411  nearnullspace_object (nullptr),
412  user_presolve (nullptr),
413  postcheck (nullptr),
414  postcheck_object (nullptr),
415  max_nonlinear_iterations(0),
416  max_function_evaluations(0),
417  absolute_residual_tolerance(0),
418  relative_residual_tolerance(0),
419  divergence_tolerance(0),
420  absolute_step_tolerance(0),
421  relative_step_tolerance(0),
422  max_linear_iterations(0),
423  initial_linear_tolerance(0),
424  minimum_linear_tolerance(0),
425  converged(false),
426  _system(s),
427  _is_initialized (false),
428  _preconditioner (nullptr),
429  _solver_configuration(nullptr)
430 {
431 }
432 
433 
434 
435 template <typename T>
436 inline
438 {
439  this->clear ();
440 }
441 
442 
443 } // namespace libMesh
444 
445 
446 #endif // LIBMESH_NONLINEAR_SOLVER_H
libMesh::NonlinearSolver::get_total_linear_iterations
virtual int get_total_linear_iterations()=0
Get the total number of linear iterations done in the last solve.
libMesh::SolverPackage
SolverPackage
Defines an enum for various linear solver packages.
Definition: enum_solver_package.h:34
libMesh::NonlinearSolver::solve
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
Solves the nonlinear system.
libMesh::NonlinearSolver::initial_linear_tolerance
double initial_linear_tolerance
Any required linear solves will at first be done with this tolerance; the NonlinearSolver may tighten...
Definition: nonlinear_solver.h:347
libMesh::NonlinearSolver::relative_residual_tolerance
double relative_residual_tolerance
Definition: nonlinear_solver.h:312
libMesh::NonlinearSolver
This base class can be inherited from to provide interfaces to nonlinear solvers from different packa...
Definition: nonlinear_solver.h:60
libMesh::NonlinearSolver::transpose_nullspace
void(* transpose_nullspace)(std::vector< NumericVector< Number > * > &sp, sys_type &S)
Function that computes a basis for the transpose Jacobian's nullspace – when solving a degenerate pro...
Definition: nonlinear_solver.h:227
libMesh::NonlinearSolver::fd_residual_object
NonlinearImplicitSystem::ComputeResidual * fd_residual_object
Object that computes the residual R(X) of the nonlinear system at the input iterate X for the purpose...
Definition: nonlinear_solver.h:151
libMesh::NonlinearSolver::set_solver_configuration
void set_solver_configuration(SolverConfiguration &solver_configuration)
Set the solver configuration object.
Definition: nonlinear_solver.C:80
libMesh::ReferenceCountedObject
This class implements reference counting.
Definition: reference_counted_object.h:65
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::NonlinearSolver::residual
void(* residual)(const NumericVector< Number > &X, NumericVector< Number > &R, sys_type &S)
Function that computes the residual R(X) of the nonlinear system at the input iterate X.
Definition: nonlinear_solver.h:137
libMesh::NonlinearSolver::nullspace_object
NonlinearImplicitSystem::ComputeVectorSubspace * nullspace_object
A callable object that computes a basis for the Jacobian's nullspace – the kernel or the "zero energy...
Definition: nonlinear_solver.h:220
libMesh::NonlinearSolver::divergence_tolerance
double divergence_tolerance
The NonlinearSolver should exit if the residual becomes greater than the initial residual times the d...
Definition: nonlinear_solver.h:321
libMesh::NonlinearSolver::system
sys_type & system()
Definition: nonlinear_solver.h:284
libMesh::NonlinearSolver::bounds
void(* bounds)(NumericVector< Number > &XL, NumericVector< Number > &XU, sys_type &S)
Function that computes the lower and upper bounds XL and XU on the solution of the nonlinear system.
Definition: nonlinear_solver.h:198
libMesh::default_solver_package
SolverPackage default_solver_package()
Definition: libmesh.C:993
libMesh::NonlinearSolver::absolute_step_tolerance
double absolute_step_tolerance
The NonlinearSolver should exit after the full nonlinear step norm is reduced to either less than abs...
Definition: nonlinear_solver.h:334
libMesh::NonlinearSolver::_system
sys_type & _system
A reference to the system we are solving.
Definition: nonlinear_solver.h:369
libMesh::NonlinearSolver::attach_preconditioner
void attach_preconditioner(Preconditioner< T > *preconditioner)
Attaches a Preconditioner object to be used during the linear solves.
Definition: nonlinear_solver.C:71
libMesh::NonlinearSolver::nearnullspace_object
NonlinearImplicitSystem::ComputeVectorSubspace * nearnullspace_object
A callable object that computes a basis for the Jacobian's near nullspace – the set of "low energy mo...
Definition: nonlinear_solver.h:248
libMesh::NonlinearSolver::_solver_configuration
SolverConfiguration * _solver_configuration
Optionally store a SolverOptions object that can be used to set parameters like solver type,...
Definition: nonlinear_solver.h:385
libMesh::NonlinearSolver::matvec
void(* matvec)(const NumericVector< Number > &X, NumericVector< Number > *R, SparseMatrix< Number > *J, sys_type &S)
Function that computes either the residual or the Jacobian of the nonlinear system at the input ite...
Definition: nonlinear_solver.h:181
libMesh::NonlinearImplicitSystem
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
Definition: nonlinear_implicit_system.h:54
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::NonlinearSolver::init
virtual void init(const char *name=nullptr)=0
Initialize data structures if not done so already.
libMesh::NonlinearImplicitSystem::ComputeBounds
Abstract base class to be used to calculate the bounds on the degrees of freedom of a nonlinear syste...
Definition: nonlinear_implicit_system.h:122
libMesh::NonlinearSolver::absolute_residual_tolerance
double absolute_residual_tolerance
The NonlinearSolver should exit after the residual is reduced to either less than absolute_residual_t...
Definition: nonlinear_solver.h:311
libMesh::NonlinearSolver::transpose_nullspace_object
NonlinearImplicitSystem::ComputeVectorSubspace * transpose_nullspace_object
A callable object that computes a basis for the transpose Jacobian's nullspace – when solving a degen...
Definition: nonlinear_solver.h:234
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::NonlinearSolver::jacobian_object
NonlinearImplicitSystem::ComputeJacobian * jacobian_object
Object that computes the Jacobian J(X) of the nonlinear system at the input iterate X.
Definition: nonlinear_solver.h:172
libMesh::NonlinearSolver::max_nonlinear_iterations
unsigned int max_nonlinear_iterations
Maximum number of non-linear iterations.
Definition: nonlinear_solver.h:294
libMesh::NonlinearSolver::user_presolve
void(* user_presolve)(sys_type &S)
Customizable function pointer which users can attach to the solver.
Definition: nonlinear_solver.h:254
libMesh::NonlinearSolver::_preconditioner
Preconditioner< T > * _preconditioner
Holds the Preconditioner object to be used for the linear solves.
Definition: nonlinear_solver.h:379
libMesh::Preconditioner
This class provides a uniform interface for preconditioners.
Definition: preconditioner.h:66
libMesh::NonlinearImplicitSystem::ComputeVectorSubspace
Callable abstract base class to be used as a callback to provide the solver with a basis for the syst...
Definition: nonlinear_implicit_system.h:147
libMesh::NonlinearImplicitSystem::ComputeResidualandJacobian
Abstract base class to be used to calculate the residual and Jacobian simultaneously of a nonlinear s...
Definition: nonlinear_implicit_system.h:165
libMesh::NonlinearSolver::residual_object
NonlinearImplicitSystem::ComputeResidual * residual_object
Object that computes the residual R(X) of the nonlinear system at the input iterate X.
Definition: nonlinear_solver.h:145
libMesh::NonlinearSolver::NonlinearSolver
NonlinearSolver(sys_type &s)
Constructor.
Definition: nonlinear_solver.h:394
libMesh::NonlinearSolver::get_current_nonlinear_iteration_number
virtual unsigned get_current_nonlinear_iteration_number() const =0
libMesh::NonlinearSolver::~NonlinearSolver
virtual ~NonlinearSolver()
Destructor.
Definition: nonlinear_solver.h:437
libMesh::NonlinearSolver::residual_and_jacobian_object
NonlinearImplicitSystem::ComputeResidualandJacobian * residual_and_jacobian_object
Object that computes either the residual or the Jacobian of the nonlinear system at the input itera...
Definition: nonlinear_solver.h:193
libMesh::NonlinearSolver::relative_step_tolerance
double relative_step_tolerance
Definition: nonlinear_solver.h:335
libMesh::NonlinearSolver::clear
virtual void clear()
Release all memory and clear data structures.
Definition: nonlinear_solver.h:96
libMesh::libMeshPrivateData::_is_initialized
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:246
libMesh::NonlinearSolver::jacobian
void(* jacobian)(const NumericVector< Number > &X, SparseMatrix< Number > &J, sys_type &S)
Function that computes the Jacobian J(X) of the nonlinear system at the input iterate X.
Definition: nonlinear_solver.h:164
libMesh::NonlinearSolver::bounds_object
NonlinearImplicitSystem::ComputeBounds * bounds_object
Object that computes the bounds vectors and .
Definition: nonlinear_solver.h:204
libMesh::NonlinearSolver::mffd_residual_object
NonlinearImplicitSystem::ComputeResidual * mffd_residual_object
Object that computes the residual R(X) of the nonlinear system at the input iterate X for the purpose...
Definition: nonlinear_solver.h:158
libMesh::NonlinearSolver::postcheck
void(* postcheck)(const NumericVector< Number > &old_soln, NumericVector< Number > &search_direction, NumericVector< Number > &new_soln, bool &changed_search_direction, bool &changed_new_soln, sys_type &S)
Function that performs a "check" on the Newton search direction and solution after each nonlinear ste...
Definition: nonlinear_solver.h:262
libMesh::NonlinearImplicitSystem::ComputeJacobian
Abstract base class to be used to calculate the Jacobian of a nonlinear system.
Definition: nonlinear_implicit_system.h:103
libMesh::NonlinearSolver::sys_type
NonlinearImplicitSystem sys_type
The type of system.
Definition: nonlinear_solver.h:67
libMesh::SolverConfiguration
This class stores solver configuration data, e.g.
Definition: solver_configuration.h:39
libMesh::NonlinearSolver::postcheck_object
NonlinearImplicitSystem::ComputePostCheck * postcheck_object
A callable object that is executed after each nonlinear iteration.
Definition: nonlinear_solver.h:274
libMesh::NonlinearSolver::minimum_linear_tolerance
double minimum_linear_tolerance
The tolerance for linear solves is kept above this minimum.
Definition: nonlinear_solver.h:352
libMesh::NonlinearSolver::converged
bool converged
After a call to solve this will reflect whether or not the nonlinear solve was successful.
Definition: nonlinear_solver.h:358
libMesh::NonlinearSolver::nearnullspace
void(* nearnullspace)(std::vector< NumericVector< Number > * > &sp, sys_type &S)
Function that computes a basis for the Jacobian's near nullspace – the set of "low energy modes" – th...
Definition: nonlinear_solver.h:241
libMesh::ParallelObject
An object whose state is distributed along a set of processors.
Definition: parallel_object.h:55
libMesh::NonlinearSolver::build
static std::unique_ptr< NonlinearSolver< T > > build(sys_type &s, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a NonlinearSolver using the nonlinear solver package specified by solver_package.
Definition: nonlinear_solver.C:37
libMesh::NonlinearSolver::system
const sys_type & system() const
Definition: nonlinear_solver.h:279
libMesh::NonlinearImplicitSystem::ComputeResidual
Abstract base class to be used to calculate the residual of a nonlinear system.
Definition: nonlinear_implicit_system.h:85
libMesh::NonlinearSolver::print_converged_reason
virtual void print_converged_reason()
Prints a useful message about why the latest nonlinear solve con(di)verged.
Definition: nonlinear_solver.h:117
libMesh::NonlinearSolver::_is_initialized
bool _is_initialized
Flag indicating if the data structures have been initialized.
Definition: nonlinear_solver.h:374
libMesh::NonlinearSolver::max_linear_iterations
unsigned int max_linear_iterations
Each linear solver step should exit after max_linear_iterations is exceeded.
Definition: nonlinear_solver.h:341
libMesh::NonlinearSolver::max_function_evaluations
unsigned int max_function_evaluations
Maximum number of function evaluations.
Definition: nonlinear_solver.h:299
libMesh::NonlinearImplicitSystem::ComputePostCheck
Abstract base class to be used for applying user modifications to the solution vector and/or Newton u...
Definition: nonlinear_implicit_system.h:187
int
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360
libMesh::Quality::name
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
libMesh::NonlinearSolver::nullspace
void(* nullspace)(std::vector< NumericVector< Number > * > &sp, sys_type &S)
Function that computes a basis for the Jacobian's nullspace – the kernel or the "zero energy modes" –...
Definition: nonlinear_solver.h:212
libMesh::NonlinearSolver::initialized
bool initialized() const
Definition: nonlinear_solver.h:91