libMesh
Loading...
Searching...
No Matches
nonlinear_solver.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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// C++ includes
31#include <cstddef>
32#include <memory>
33
34namespace libMesh
35{
36
37// forward declarations
38template <typename T> class SparseMatrix;
39template <typename T> class NumericVector;
40template <typename T> class Preconditioner;
41class SolverConfiguration;
42enum SolverPackage : int;
43
51template <typename T>
52class NonlinearSolver : public ReferenceCountedObject<NonlinearSolver<T>>,
53 public ParallelObject
54{
55public:
60
64 explicit
66
70 virtual ~NonlinearSolver ();
71
76 static std::unique_ptr<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 = 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
110 virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> & /* jac_in */,
111 SparseMatrix<T> & /* pre_in */,
112 NumericVector<T> & /* x_in */,
113 NumericVector<T> & /* r_in */,
114 const double /* tol */,
115 const unsigned int /* m_its */)
116 {
117 libmesh_not_implemented();
118 }
119
124 virtual void print_converged_reason() { libmesh_not_implemented(); }
125
130
138 virtual unsigned get_current_nonlinear_iteration_number() const = 0;
139
144 void (* residual) (const NumericVector<Number> & X,
146 sys_type & S);
147
153
159
166
171 void (* jacobian) (const NumericVector<Number> & X,
173 sys_type & S);
174
180
188 void (* matvec) (const NumericVector<Number> & X,
191 sys_type & S);
192
201
207 sys_type & S);
212
219 void (* nullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
220
228
234 void (* transpose_nullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
235
242
248 void (* nearnullspace) (std::vector<NumericVector<Number> *> & sp, sys_type & S);
249
256
261 void (* user_presolve)(sys_type & S);
262
269 void (* postcheck) (const NumericVector<Number> & old_soln,
270 NumericVector<Number> & search_direction,
271 NumericVector<Number> & new_soln,
272 bool & changed_search_direction,
273 bool & changed_new_soln,
274 sys_type & S);
275
282
284
288 const sys_type & system () const { return _system; }
289
293 sys_type & system () { return _system; }
294
298 void attach_preconditioner(Preconditioner<T> * preconditioner);
299
304
309
322
331
345
351
357
362
368
372 void set_solver_configuration(SolverConfiguration & solver_configuration);
373
377 virtual bool reuse_preconditioner() const;
378
382 virtual void set_reuse_preconditioner(bool reuse);
383
387 virtual unsigned int reuse_preconditioner_max_linear_its() const;
388
392 virtual void set_reuse_preconditioner_max_linear_its(unsigned int i);
393
397 virtual void force_new_preconditioner() {};
398
413 virtual void set_exact_constraint_enforcement(bool enable)
414 {
416 }
417
422
423protected:
428
434
439
444
449
454
460};
461
462
463
464
465/*----------------------- inline functions ----------------------------------*/
466template <typename T>
467inline
469 ParallelObject (s),
470 residual (nullptr),
471 residual_object (nullptr),
472 fd_residual_object (nullptr),
473 mffd_residual_object (nullptr),
474 jacobian (nullptr),
475 jacobian_object (nullptr),
476 matvec (nullptr),
477 residual_and_jacobian_object (nullptr),
478 bounds (nullptr),
479 bounds_object (nullptr),
480 nullspace (nullptr),
481 nullspace_object (nullptr),
482 transpose_nullspace (nullptr),
483 transpose_nullspace_object (nullptr),
484 nearnullspace (nullptr),
485 nearnullspace_object (nullptr),
486 user_presolve (nullptr),
487 postcheck (nullptr),
488 postcheck_object (nullptr),
489 precheck_object (nullptr),
490 max_nonlinear_iterations(0),
491 max_function_evaluations(0),
492 absolute_residual_tolerance(0),
493 relative_residual_tolerance(0),
494 divergence_tolerance(0),
495 absolute_step_tolerance(0),
496 relative_step_tolerance(0),
497 max_linear_iterations(0),
498 initial_linear_tolerance(0),
499 minimum_linear_tolerance(0),
500 converged(false),
501 _reuse_preconditioner(false),
502 _exact_constraint_enforcement(true),
503 _reuse_preconditioner_max_linear_its(0),
504 _system(s),
505 _is_initialized (false),
506 _preconditioner (nullptr),
507 _solver_configuration(nullptr)
508{
509}
510
511
512
513template <typename T>
514inline
519
520
521} // namespace libMesh
522
523
524#endif // LIBMESH_NONLINEAR_SOLVER_H
void ErrorVector unsigned int
Abstract base class to be used to calculate the bounds on the degrees of freedom of a nonlinear syste...
Abstract base class to be used to calculate the Jacobian of a nonlinear system.
Abstract base class to be used for applying user modifications to the solution vector and/or Newton u...
Abstract base class to be used for applying user modifications to the Newton search direction \emph b...
Abstract base class to be used to calculate the residual of a nonlinear system.
Abstract base class to be used to calculate the residual and Jacobian simultaneously of a nonlinear s...
Callable abstract base class to be used as a callback to provide the solver with a basis for the syst...
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
This base class can be inherited from to provide interfaces to nonlinear solvers from different packa...
double minimum_linear_tolerance
The tolerance for linear solves is kept above this minimum.
const sys_type & system() const
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...
virtual unsigned get_current_nonlinear_iteration_number() const =0
virtual void set_reuse_preconditioner(bool reuse)
Set the reuse preconditioner flag.
bool _is_initialized
Flag indicating if the data structures have been initialized.
void attach_preconditioner(Preconditioner< T > *preconditioner)
Attaches a Preconditioner object to be used during the linear solves.
NonlinearImplicitSystem::ComputeResidual * mffd_residual_object
Object that computes the residual R(X) of the nonlinear system at the input iterate X for the purpose...
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.
NonlinearImplicitSystem sys_type
The type of system.
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...
virtual ~NonlinearSolver()
Destructor.
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.
virtual void set_exact_constraint_enforcement(bool enable)
Enable (or disable; it is true by default) exact enforcement of constraints at the solver level,...
NonlinearImplicitSystem::ComputeResidual * residual_object
Object that computes the residual R(X) of the nonlinear system at the input iterate X.
virtual bool reuse_preconditioner() const
Get the reuse_preconditioner flag.
virtual void print_converged_reason()
Prints a useful message about why the latest nonlinear solve con(di)verged.
unsigned int max_nonlinear_iterations
Maximum number of non-linear iterations.
bool _exact_constraint_enforcement
Whether we should enforce exact constraints globally during a solve.
NonlinearImplicitSystem::ComputeJacobian * jacobian_object
Object that computes the Jacobian J(X) of the nonlinear system at the input iterate X.
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...
SolverConfiguration * _solver_configuration
Optionally store a SolverOptions object that can be used to set parameters like solver type,...
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
Solves the nonlinear system.
NonlinearImplicitSystem::ComputeVectorSubspace * transpose_nullspace_object
A callable object that computes a basis for the transpose Jacobian's nullspace – when solving a degen...
void set_solver_configuration(SolverConfiguration &solver_configuration)
Set the solver configuration object.
virtual void set_reuse_preconditioner_max_linear_its(unsigned int i)
Set the reuse_preconditioner_max_linear_its parameter.
NonlinearImplicitSystem::ComputeVectorSubspace * nullspace_object
A callable object that computes a basis for the Jacobian's nullspace – the kernel or the "zero energy...
virtual void force_new_preconditioner()
Immediately force a new preconditioner.
unsigned int max_linear_iterations
Each linear solver step should exit after max_linear_iterations is exceeded.
double absolute_residual_tolerance
The NonlinearSolver should exit after the residual is reduced to either less than absolute_residual_t...
virtual void clear()
Release all memory and clear data structures.
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's nullspace – the kernel or the "zero energy modes" –...
NonlinearImplicitSystem::ComputeVectorSubspace * nearnullspace_object
A callable object that computes a basis for the Jacobian's near nullspace – the set of "low energy mo...
NonlinearImplicitSystem::ComputeBounds * bounds_object
Object that computes the bounds vectors and .
unsigned int max_function_evaluations
Maximum number of function evaluations.
unsigned int _reuse_preconditioner_max_linear_its
Number of linear iterations to retain the preconditioner.
virtual unsigned int reuse_preconditioner_max_linear_its() const
Get the reuse_preconditioner_max_linear_its parameter.
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.
double absolute_step_tolerance
The NonlinearSolver should exit after the full nonlinear step norm is reduced to either less than abs...
NonlinearImplicitSystem::ComputePreCheck * precheck_object
double initial_linear_tolerance
Any required linear solves will at first be done with this tolerance; the NonlinearSolver may tighten...
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)
Solves the nonlinear system using jac_in as the actual Jacobian operator and pre_in as the preconditi...
NonlinearImplicitSystem::ComputePostCheck * postcheck_object
A callable object that is executed after each nonlinear iteration.
sys_type & _system
A reference to the system we are solving.
Preconditioner< T > * _preconditioner
Holds the Preconditioner object to be used for the linear solves.
bool _reuse_preconditioner
Whether we should reuse the linear preconditioner.
NonlinearSolver(sys_type &s)
Constructor.
NonlinearImplicitSystem::ComputeResidualandJacobian * residual_and_jacobian_object
Object that computes either the residual or the Jacobian of the nonlinear system at the input itera...
virtual void init(const char *name=nullptr)=0
Initialize data structures if not done so already.
double divergence_tolerance
The NonlinearSolver should exit if the residual becomes greater than the initial residual times the d...
bool converged
After a call to solve this will reflect whether or not the nonlinear solve was successful.
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.
virtual int get_total_linear_iterations()=0
Get the total number of linear iterations done in the last solve.
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...
NonlinearImplicitSystem::ComputeResidual * fd_residual_object
Object that computes the residual R(X) of the nonlinear system at the input iterate X for the purpose...
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
An object whose state is distributed along a set of processors.
This class provides a uniform interface for preconditioners.
This class implements reference counting.
This class stores solver configuration data, e.g.
Generic sparse matrix.
The libMesh namespace provides an interface to certain functionality in the library.
SolverPackage default_solver_package()
Definition libmesh.C:1064
SolverPackage
Defines an enum for various linear solver packages.