nonlinear_solver.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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"
27 #include "libmesh/libmesh.h"
29 #include "libmesh/auto_ptr.h" // deprecated
30 
31 #ifdef LIBMESH_FORWARD_DECLARE_ENUMS
32 namespace libMesh
33 {
34 enum SolverPackage : int;
35 }
36 #else
38 #endif
39 
40 // C++ includes
41 #include <cstddef>
42 #include <memory>
43 
44 namespace libMesh
45 {
46 
47 // forward declarations
48 template <typename T> class SparseMatrix;
49 template <typename T> class NumericVector;
50 template <typename T> class Preconditioner;
51 class SolverConfiguration;
52 
60 template <typename T>
61 class NonlinearSolver : public ReferenceCountedObject<NonlinearSolver<T>>,
62  public ParallelObject
63 {
64 public:
69 
73  explicit
75 
79  virtual ~NonlinearSolver ();
80 
85  static std::unique_ptr<NonlinearSolver<T>> build(sys_type & s,
86  const SolverPackage solver_package = libMesh::default_solver_package());
87 
92  bool initialized () const { return _is_initialized; }
93 
97  virtual void clear () {}
98 
103  virtual void init (const char * name = nullptr) = 0;
104 
108  virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> &, // System Jacobian Matrix
109  NumericVector<T> &, // Solution vector
110  NumericVector<T> &, // Residual vector
111  const double, // Stopping tolerance
112  const unsigned int) = 0; // N. Iterations
113 
118  virtual void print_converged_reason() { libmesh_not_implemented(); }
119 
123  virtual int get_total_linear_iterations() = 0;
124 
132  virtual unsigned get_current_nonlinear_iteration_number() const = 0;
133 
138  void (* residual) (const NumericVector<Number> & X,
140  sys_type & S);
141 
147 
153 
160 
165  void (* jacobian) (const NumericVector<Number> & X,
167  sys_type & S);
168 
174 
182  void (* matvec) (const NumericVector<Number> & X,
185  sys_type & S);
186 
195 
201  sys_type & S);
206 
213  void (* nullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
214 
222 
228  void (* transpose_nullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
229 
236 
242  void (* nearnullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
243 
250 
255  void (* user_presolve)(sys_type & S);
256 
263  void (* postcheck) (const NumericVector<Number> & old_soln,
264  NumericVector<Number> & search_direction,
265  NumericVector<Number> & new_soln,
266  bool & changed_search_direction,
267  bool & changed_new_soln,
268  sys_type & S);
269 
276 
280  const sys_type & system () const { return _system; }
281 
285  sys_type & system () { return _system; }
286 
290  void attach_preconditioner(Preconditioner<T> * preconditioner);
291 
296 
301 
314 
328 
333  unsigned int max_linear_iterations;
334 
340 
345 
350  bool converged;
351 
355  void set_solver_configuration(SolverConfiguration & solver_configuration);
356 
357 protected:
362 
367 
372 
378 };
379 
380 
381 
382 
383 /*----------------------- inline functions ----------------------------------*/
384 template <typename T>
385 inline
387  ParallelObject (s),
388  residual (nullptr),
389  residual_object (nullptr),
390  fd_residual_object (nullptr),
391  mffd_residual_object (nullptr),
392  jacobian (nullptr),
393  jacobian_object (nullptr),
394  matvec (nullptr),
395  residual_and_jacobian_object (nullptr),
396  bounds (nullptr),
397  bounds_object (nullptr),
398  nullspace (nullptr),
399  nullspace_object (nullptr),
400  transpose_nullspace (nullptr),
401  transpose_nullspace_object (nullptr),
402  nearnullspace (nullptr),
403  nearnullspace_object (nullptr),
404  user_presolve (nullptr),
405  postcheck (nullptr),
406  postcheck_object (nullptr),
407  max_nonlinear_iterations(0),
408  max_function_evaluations(0),
409  absolute_residual_tolerance(0),
410  relative_residual_tolerance(0),
411  absolute_step_tolerance(0),
412  relative_step_tolerance(0),
413  max_linear_iterations(0),
414  initial_linear_tolerance(0),
415  minimum_linear_tolerance(0),
416  converged(false),
417  _system(s),
418  _is_initialized (false),
419  _preconditioner (nullptr),
420  _solver_configuration(nullptr)
421 {
422 }
423 
424 
425 
426 template <typename T>
427 inline
429 {
430  this->clear ();
431 }
432 
433 
434 } // namespace libMesh
435 
436 
437 #endif // LIBMESH_NONLINEAR_SOLVER_H
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
virtual void init(const char *name=nullptr)=0
void(* residual)(const NumericVector< Number > &X, NumericVector< Number > &R, sys_type &S)
NonlinearImplicitSystem::ComputeResidualandJacobian * residual_and_jacobian_object
unsigned int max_function_evaluations
Preconditioner< T > * _preconditioner
void(* user_presolve)(sys_type &S)
void attach_preconditioner(Preconditioner< T > *preconditioner)
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: diff_context.h:40
unsigned int max_linear_iterations
SolverConfiguration * _solver_configuration
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
NonlinearImplicitSystem::ComputeResidual * residual_object
void(* jacobian)(const NumericVector< Number > &X, SparseMatrix< Number > &J, sys_type &S)
SolverPackage default_solver_package()
Definition: libmesh.C:971
const sys_type & system() const
NonlinearImplicitSystem::ComputeJacobian * jacobian_object
void set_solver_configuration(SolverConfiguration &solver_configuration)
virtual int get_total_linear_iterations()=0
NonlinearImplicitSystem::ComputeBounds * bounds_object
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
void(* transpose_nullspace)(std::vector< NumericVector< Number > *> &sp, sys_type &S)
virtual unsigned get_current_nonlinear_iteration_number() const =0
void(* nullspace)(std::vector< NumericVector< Number > *> &sp, sys_type &S)
An object whose state is distributed along a set of processors.
void(* nearnullspace)(std::vector< NumericVector< Number > *> &sp, sys_type &S)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
NonlinearImplicitSystem::ComputePostCheck * postcheck_object
virtual void print_converged_reason()
static std::unique_ptr< NonlinearSolver< T > > build(sys_type &s, const SolverPackage solver_package=libMesh::default_solver_package())
void(* bounds)(NumericVector< Number > &XL, NumericVector< Number > &XU, sys_type &S)
NonlinearImplicitSystem::ComputeResidual * mffd_residual_object
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)
NonlinearImplicitSystem::ComputeVectorSubspace * nearnullspace_object
NonlinearImplicitSystem sys_type
unsigned int max_nonlinear_iterations
NonlinearImplicitSystem::ComputeVectorSubspace * transpose_nullspace_object
void(* matvec)(const NumericVector< Number > &X, NumericVector< Number > *R, SparseMatrix< Number > *J, sys_type &S)
NonlinearImplicitSystem::ComputeResidual * fd_residual_object
NonlinearImplicitSystem::ComputeVectorSubspace * nullspace_object