libMesh
nonlinear_solver.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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/enum_solver_package.h"
26 #include "libmesh/reference_counted_object.h"
27 #include "libmesh/nonlinear_implicit_system.h"
28 #include "libmesh/libmesh.h"
29 #include "libmesh/parallel_object.h"
30 #include "libmesh/auto_ptr.h"
31 
32 // C++ includes
33 #include <cstddef>
34 
35 namespace libMesh
36 {
37 
38 // forward declarations
39 template <typename T> class SparseMatrix;
40 template <typename T> class NumericVector;
41 template <typename T> class Preconditioner;
42 class SolverConfiguration;
43 
51 template <typename T>
52 class NonlinearSolver : public ReferenceCountedObject<NonlinearSolver<T>>,
53  public ParallelObject
54 {
55 public:
60 
64  explicit
65  NonlinearSolver (sys_type & s);
66 
70  virtual ~NonlinearSolver ();
71 
76  static UniquePtr<NonlinearSolver<T>> build(sys_type & s,
77  const SolverPackage solver_package = libMesh::default_solver_package());
78 
83  bool initialized () const { return _is_initialized; }
84 
88  virtual void clear () {}
89 
94  virtual void init (const char * name = libmesh_nullptr) = 0;
95 
99  virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> &, // System Jacobian Matrix
100  NumericVector<T> &, // Solution vector
101  NumericVector<T> &, // Residual vector
102  const double, // Stopping tolerance
103  const unsigned int) = 0; // N. Iterations
104 
109  virtual void print_converged_reason() { libmesh_not_implemented(); }
110 
114  virtual int get_total_linear_iterations() = 0;
115 
123  virtual unsigned get_current_nonlinear_iteration_number() const = 0;
124 
129  void (* residual) (const NumericVector<Number> & X,
131  sys_type & S);
132 
138 
143  void (* jacobian) (const NumericVector<Number> & X,
145  sys_type & S);
146 
152 
160  void (* matvec) (const NumericVector<Number> & X,
163  sys_type & S);
164 
173 
179  sys_type & S);
184 
191  void (* nullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
192 
200 
206  void (* transpose_nullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
207 
214 
220  void (* nearnullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
221 
228 
233  void (* user_presolve)(sys_type & S);
234 
241  void (* postcheck) (const NumericVector<Number> & old_soln,
242  NumericVector<Number> & search_direction,
243  NumericVector<Number> & new_soln,
244  bool & changed_search_direction,
245  bool & changed_new_soln,
246  sys_type & S);
247 
254 
258  const sys_type & system () const { return _system; }
259 
263  sys_type & system () { return _system; }
264 
268  void attach_preconditioner(Preconditioner<T> * preconditioner);
269 
274 
279 
292 
306 
311  unsigned int max_linear_iterations;
312 
318 
323 
328  bool converged;
329 
333  void set_solver_configuration(SolverConfiguration & solver_configuration);
334 
335 protected:
339  sys_type & _system;
340 
345 
350 
356 };
357 
358 
359 
360 
361 /*----------------------- inline functions ----------------------------------*/
362 template <typename T>
363 inline
365  ParallelObject (s),
392  converged(false),
393  _system(s),
394  _is_initialized (false),
397 {
398 }
399 
400 
401 
402 template <typename T>
403 inline
405 {
406  this->clear ();
407 }
408 
409 
410 } // namespace libMesh
411 
412 
413 #endif // LIBMESH_NONLINEAR_SOLVER_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
virtual void clear()
Release all memory and clear data structures.
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...
NonlinearImplicitSystem::ComputeResidualandJacobian * residual_and_jacobian_object
Object that computes either the residual or the Jacobian of the nonlinear system at the input itera...
sys_type & _system
A reference to the system we are solving.
void(* transpose_nullspace)(std::vector< NumericVector< Number > * > &sp, sys_type &S)
Function that computes a basis for the transpose Jacobian&#39;s nullspace – when solving a degenerate pr...
bool converged
After a call to solve this will reflect whether or not the nonlinear solve was successful.
unsigned int max_function_evaluations
Maximum number of function evaluations.
Preconditioner< T > * _preconditioner
Holds the Preconditioner object to be used for the linear solves.
virtual void init(const char *name=libmesh_nullptr)=0
Initialize data structures if not done so already.
void(* user_presolve)(sys_type &S)
Customizable function pointer which users can attach to the solver.
void(* nullspace)(std::vector< NumericVector< Number > * > &sp, sys_type &S)
Function that computes a basis for the Jacobian&#39;s nullspace – the kernel or the "zero energy modes" ...
SolverPackage
Defines an enum for various linear solver packages.
void attach_preconditioner(Preconditioner< T > *preconditioner)
Attaches a Preconditioner object to be used during the linear solves.
const class libmesh_nullptr_t libmesh_nullptr
Numeric vector.
Definition: dof_map.h:66
This base class can be inherited from to provide interfaces to nonlinear solvers from different packa...
virtual unsigned get_current_nonlinear_iteration_number() const =0
unsigned int max_linear_iterations
Each linear solver step should exit after max_linear_iterations is exceeded.
The libMesh namespace provides an interface to certain functionality in the library.
SolverConfiguration * _solver_configuration
Optionally store a SolverOptions object that can be used to set parameters like solver type...
Abstract base class to be used to calculate the bounds on the degrees of freedom of a nonlinear syste...
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
Solves the nonlinear system.
NonlinearImplicitSystem::ComputeResidual * residual_object
Object that computes the residual R(X) of the nonlinear system at the input iterate X...
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...
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
Real absolute_step_tolerance
The NonlinearSolver should exit after the full nonlinear step norm is reduced to either less than abs...
Generic sparse matrix.
Definition: dof_map.h:65
SolverPackage default_solver_package()
Definition: libmesh.C:995
bool _is_initialized
Flag indicating if the data structures have been initialized.
Abstract base class to be used to calculate the residual of a nonlinear system.
NonlinearImplicitSystem::ComputeJacobian * jacobian_object
Object that computes the Jacobian J(X) of the nonlinear system at the input iterate X...
void set_solver_configuration(SolverConfiguration &solver_configuration)
Set the solver configuration object.
This class provides a uniform interface for preconditioners.
virtual int get_total_linear_iterations()=0
Get the total number of linear iterations done in the last solve.
Abstract base class to be used to calculate the Jacobian of a nonlinear system.
NonlinearImplicitSystem::ComputeBounds * bounds_object
Object that computes the bounds vectors and .
virtual ~NonlinearSolver()
Destructor.
This class provides a specific system class.
This class stores solver configuration data, e.g.
static UniquePtr< 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.
Real minimum_linear_tolerance
The tolerance for linear solves is kept above this minimum.
Abstract base class to be used to calculate the residual and Jacobian simultaneously of a nonlinear s...
This class implements reference counting.
This class forms the base class for all other classes that are expected to be implemented in parallel...
Real absolute_residual_tolerance
The NonlinearSolver should exit after the residual is reduced to either less than absolute_residual_t...
const sys_type & system() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void(* nearnullspace)(std::vector< NumericVector< Number > * > &sp, sys_type &S)
Function that computes a basis for the Jacobian&#39;s near nullspace – the set of "low energy modes" – ...
NonlinearImplicitSystem::ComputePostCheck * postcheck_object
A callable object that is executed after each nonlinear iteration.
virtual void print_converged_reason()
Prints a useful message about why the latest nonlinear solve con(di)verged.
NonlinearSolver(sys_type &s)
Constructor.
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...
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...
NonlinearImplicitSystem::ComputeVectorSubspace * nearnullspace_object
A callable object that computes a basis for the Jacobian&#39;s near nullspace – the set of "low energy m...
Real initial_linear_tolerance
Any required linear solves will at first be done with this tolerance; the NonlinearSolver may tighten...
NonlinearImplicitSystem sys_type
The type of system.
Abstract base class to be used for applying user modifications to the solution vector and/or Newton u...
unsigned int max_nonlinear_iterations
Maximum number of non-linear iterations.
NonlinearImplicitSystem::ComputeVectorSubspace * transpose_nullspace_object
A callable object that computes a basis for the transpose Jacobian&#39;s nullspace – when solving a dege...
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...
Callable abstract base class to be used as a callback to provide the solver with a basis for the syst...
NonlinearImplicitSystem::ComputeVectorSubspace * nullspace_object
A callable object that computes a basis for the Jacobian&#39;s nullspace – the kernel or the "zero energ...