NewtonRaphsonSolver< number, VectorType > Class Template Reference

Developer Documentation: MeltPoolDG::NewtonRaphsonSolver< number, VectorType > Class Template Reference
Developer Documentation
MeltPoolDG::NewtonRaphsonSolver< number, VectorType > Class Template Reference

#include <newton_raphson_solver.hpp>

Collaboration diagram for MeltPoolDG::NewtonRaphsonSolver< number, VectorType >:
[legend]

Public Member Functions

 NewtonRaphsonSolver (const NonlinearSolverData< number > &nlsolve_data)
 
void solve (VectorType &solution)
 
const VectorType & get_residual () const
 
const VectorType & get_solution_update () const
 

Public Attributes

std::function< void(const VectorType &evaluation_point, VectorType &residual)> residual = {}
 
std::function< int(const VectorType &rhs, VectorType &dst)> solve_with_jacobian = {}
 
std::function< void(VectorType &v)> reinit_vector = {}
 
std::function< void(VectorType &v)> distribute_constraints = {}
 
std::function< number()> norm_of_solution_vector = {}
 

Private Member Functions

void solve_increment (const VectorType &current_solution)
 
number suggest_new_time_increment ()
 
void set_tolerances_to_alternative_values ()
 
bool is_converged (const VectorType &solution)
 
void print_header () const
 
std::string print_checkmark (bool is_converged) const
 

Private Attributes

const NonlinearSolverData< number > nlsolve_data
 
dealii::ConditionalOStream pcout
 
const int max_number_of_iterations
 
number residual_tolerance
 
number field_correction_tolerance
 
VectorType rhs
 
VectorType solution_update
 
int linear_iter_acc = 0
 
number initial_residual_norm = 0.0
 
std::ostringstream str_
 

Detailed Description

template<typename number, typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
class MeltPoolDG::NewtonRaphsonSolver< number, VectorType >

Newton-Raphson solver for a nonlinear system of equations \(F(x) = 0\).

The solver does not know the nonlinear problem itself. Instead, the problem is defined by the public std::function members residual, solve_with_jacobian, reinit_vector, distribute_constraints and norm_of_solution_vector, which have to be set before calling solve().

Algorithm

Starting from the initial guess \(x_0\) passed to solve(), each Newton iteration \(k\)

  1. computes the right-hand side \(r_k = -F(x_k)\) with residual(),
  2. solves the linearized system \(J(x_k)\,\Delta x_k = r_k\) with solve_with_jacobian(), where \(J = \partial F / \partial x\) is the Jacobian,
  3. checks for convergence, and, if not converged,
  4. updates the solution \(x_{k+1} = x_k + \Delta x_k\) and applies the constraints with distribute_constraints().

Convergence criterion

The solver is converged if both the residual and the solution update are small, i.e.

\[ \|r_k\| < \mathrm{tol}_r \quad \mathrm{and} \quad \|\Delta x_k\| < \mathrm{tol}_{\Delta x}, \]

with the l2 norms of the vectors and the tolerances NonlinearSolverData::residual_tolerance and NonlinearSolverData::field_correction_tolerance.

If NonlinearSolverData::use_relative_tolerances is set, the tolerances are interpreted relative to reference norms:

\[ \frac{\|r_k\|}{\|r_0\|} < \mathrm{tol}_r \quad \mathrm{and} \quad \frac{\|\Delta x_k\|}{\|x_k\|} < \mathrm{tol}_{\Delta x}, \]

where \(r_0\) is the right-hand side of the first Newton iteration of the current call to solve() and \(x_k\) the current iterate. If a reference norm is zero, the respective tolerance is applied as an absolute value.

If the solver does not converge within NonlinearSolverData::max_nonlinear_iterations iterations, it continues for up to NonlinearSolverData::max_nonlinear_iterations_alt iterations with the alternative tolerances NonlinearSolverData::residual_tolerance_alt and NonlinearSolverData::field_correction_tolerance_alt. If it does not converge with those either, ExcNewtonDidNotConverge is thrown. Each call to solve() starts with the standard tolerances.

Note
If a linear solver with an absolute tolerance is used in solve_with_jacobian(), this tolerance limits how far the residual can be reduced: once \(\|r_k\|\) falls below it, the linear solver returns a zero update. In combination with relative tolerances, make sure that the absolute tolerance of the linear solver is well below \(\mathrm{tol}_r \|r_0\|\).

Output

For NonlinearSolverData::verbosity_level >= 2, the number of linear iterations and the norms compared against the tolerances are printed in each Newton iteration. For NonlinearSolverData::verbosity_level >= 1, the norm of the converged solution is printed.

Template Parameters
numberThe floating point type.
VectorTypeThe type of the solution vector.

Constructor & Destructor Documentation

◆ NewtonRaphsonSolver()

template<typename number , typename VectorType >
MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::NewtonRaphsonSolver ( const NonlinearSolverData< number > &  nlsolve_data)
explicit

Constructor.

Parameters
nlsolve_dataParameters of the nonlinear solver.

Member Function Documentation

◆ get_residual()

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
const VectorType & MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::get_residual ( ) const
inline

Return the right-hand side of the last Newton iteration, i.e. the negative nonlinear residual \(-F(x)\) at the solution returned by solve().

◆ get_solution_update()

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
const VectorType & MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::get_solution_update ( ) const
inline

Return the solution update of the last Newton iteration. If solve() converged, this update has not been added to the solution.

◆ is_converged()

template<typename number , typename VectorType >
bool MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::is_converged ( const VectorType &  solution)
private

Check the convergence criterion for the right-hand side and the solution update of the current Newton iteration. For NonlinearSolverData::verbosity_level >= 2, the norms are printed.

Parameters
solutionThe current iterate. It is the reference for the relative field correction tolerance.
Returns
Whether both the residual and the field correction criterion are fulfilled.

◆ print_checkmark()

template<typename number , typename VectorType >
std::string MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::print_checkmark ( bool  is_converged) const
private

Return a check mark if is_converged is true and a cross otherwise.

◆ print_header()

template<typename number , typename VectorType >
void MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::print_header ( ) const
private

Print the header of the table of Newton iterations for NonlinearSolverData::verbosity_level >= 2.

◆ set_tolerances_to_alternative_values()

template<typename number , typename VectorType >
void MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::set_tolerances_to_alternative_values ( )
private

Replace the standard tolerances by NonlinearSolverData::residual_tolerance_alt and NonlinearSolverData::field_correction_tolerance_alt. It is called by solve() if the solver did not converge within NonlinearSolverData::max_nonlinear_iterations iterations.

◆ solve()

template<typename number , typename VectorType >
void MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::solve ( VectorType &  solution)

Solve the nonlinear system. All std::function members have to be set before calling this function. The number of Newton iterations and linear iterations are reported to the IterationMonitor.

Parameters
solutionOn input, the initial guess. On output, the converged solution.
Note
On convergence, the solution update of the last Newton iteration is not added to solution. Thus, get_residual() corresponds to the returned solution.
Exceptions
ExcNewtonDidNotConvergeif the solver does not converge within the maximum number of iterations.

◆ solve_increment()

template<typename number , typename VectorType >
void MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::solve_increment ( const VectorType &  current_solution)
private

Compute the right-hand side at current_solution and solve for the solution update. The results are stored in rhs and solution_update.

Parameters
current_solutionThe current iterate.

◆ suggest_new_time_increment()

template<typename number , typename VectorType >
number MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::suggest_new_time_increment ( )
private

Not implemented yet.

Member Data Documentation

◆ distribute_constraints

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
std::function<void(VectorType &v)> MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::distribute_constraints = {}

Apply the constraints to v. It is called after each update of the solution.

◆ field_correction_tolerance

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
number MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::field_correction_tolerance
private

Currently used field correction tolerance. It is set to the standard tolerance at the beginning of solve() and replaced by the alternative one in set_tolerances_to_alternative_values().

◆ initial_residual_norm

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
number MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::initial_residual_norm = 0.0
private

Norm of the right-hand side of the first Newton iteration of the current call to solve(). It is the reference for the relative residual tolerance.

◆ linear_iter_acc

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
int MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::linear_iter_acc = 0
private

Accumulated number of linear iterations of the current call to solve().

◆ max_number_of_iterations

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
const int MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::max_number_of_iterations
private

◆ nlsolve_data

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
const NonlinearSolverData<number> MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::nlsolve_data
private

Parameters of the nonlinear solver.

◆ norm_of_solution_vector

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
std::function<number()> MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::norm_of_solution_vector = {}

Compute the norm of the solution vector. It is only used for the console output for NonlinearSolverData::verbosity_level >= 1.

◆ pcout

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
dealii::ConditionalOStream MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::pcout
private

Output stream that prints on the first MPI process only.

◆ reinit_vector

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
std::function<void(VectorType &v)> MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::reinit_vector = {}

Initialize v with the parallel layout of the solution vector. It is used to set up the internal vectors for the right-hand side and the solution update.

◆ residual

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
std::function<void(const VectorType &evaluation_point, VectorType &residual)> MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::residual = {}

Compute the right-hand side of the linearized system at evaluation_point, i.e. the negative nonlinear residual \(-F(x)\). The vector residual is set to zero before the call.

Parameters
evaluation_pointThe current iterate, i.e. the vector passed to solve() in its current state.
residualThe vector to store the right-hand side in.

◆ residual_tolerance

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
number MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::residual_tolerance
private

Currently used residual tolerance. It is set to the standard tolerance at the beginning of solve() and replaced by the alternative one in set_tolerances_to_alternative_values().

◆ rhs

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
VectorType MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::rhs
private

Right-hand side of the current Newton iteration.

◆ solution_update

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
VectorType MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::solution_update
private

Solution update of the current Newton iteration.

◆ solve_with_jacobian

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
std::function<int(const VectorType &rhs, VectorType &dst)> MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::solve_with_jacobian = {}

Solve the linearized system \(J\,\Delta x = r\) with the Jacobian \(J\) evaluated at the evaluation point of the preceding call to residual(). The vector dst is set to zero before the call.

Parameters
rhsThe right-hand side \(r\) computed by residual().
dstThe vector to store the solution update \(\Delta x\) in.
Returns
The number of linear iterations.

◆ str_

template<typename number , typename VectorType = dealii::LinearAlgebra::distributed::Vector<number>>
std::ostringstream MeltPoolDG::NewtonRaphsonSolver< number, VectorType >::str_
private

Buffer for the console output of the current Newton iteration.


The documentation for this class was generated from the following files: