https://mooseframework.inl.gov
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
NestedSolveTempl< is_ad > Class Template Reference

NestedSolveTempl<is_ad> and its instantiations NestedSolve and ADNestedSolve are utility classes that implement a non-linear solver. More...

#include <NestedSolve.h>

Classes

struct  CorrespondingJacobianTempl
 Deduce the Jacobian type from the solution type. More...
 

Public Types

enum  State {
  State::NONE, State::CONVERGED_ABS, State::CONVERGED_REL, State::CONVERGED_ACCEPTABLE_ABS,
  State::CONVERGED_ACCEPTABLE_REL, State::CONVERGED_BOUNDS, State::EXACT_GUESS, State::CONVERGED_XTOL,
  State::NOT_CONVERGED
}
 possible solver states More...
 
template<int N = 0>
using Value = typename std::conditional< N==1, NSReal, typename std::conditional< N==0, NestedSolveTempl< is_ad >::DynamicVector, Eigen::Matrix< NSReal, N, 1 > >::type >::type
 Residual and solution type. More...
 
template<int N = 0>
using Jacobian = typename std::conditional< N==1, NSReal, typename std::conditional< N==0, NestedSolveTempl< is_ad >::DynamicMatrix, Eigen::Matrix< NSReal, N, N > >::type >::type
 Jacobian matrix type. More...
 
using NSReal = Moose::GenericType< Real, is_ad >
 AD/non-AD switched type shortcuts. More...
 
using NSRealVectorValue = Moose::GenericType< RealVectorValue, is_ad >
 
using NSRankTwoTensor = Moose::GenericType< RankTwoTensor, is_ad >
 
using DynamicVector = Eigen::Matrix< NSReal, Eigen::Dynamic, 1 >
 Eigen type shortcuts. More...
 
using DynamicMatrix = Eigen::Matrix< NSReal, Eigen::Dynamic, Eigen::Dynamic >
 
template<typename T >
using CorrespondingJacobian = typename NestedSolveInternal::CorrespondingJacobianTempl< T >::type
 

Public Member Functions

 NestedSolveTempl ()
 
 NestedSolveTempl (const InputParameters &params)
 
template<typename V , typename T >
void nonlinear (V &guess, T &&compute)
 Solve the N*N nonlinear equation system using a built-in Netwon-Raphson loop. More...
 
template<typename V , typename T , typename C >
void nonlinearDamped (V &guess, T &&compute, C &&computeCondition)
 Solve the N*N nonlinear equation system using the damped Netwon-Raphson loop. More...
 
void setRelativeTolerance (Real rel)
 
void setAbsoluteTolerance (Real abs)
 
void setAcceptableMultiplier (Real am)
 
const StategetState () const
 Get the solver state. More...
 
const std::size_t & getIterations ()
 Get the number of iterations from the last solve. More...
 
template<typename R , typename J >
void nonlinear (NestedSolveTempl< is_ad >::DynamicVector &guess, R &computeResidual, J &computeJacobian)
 
template<typename R , typename J >
void nonlinearTight (NestedSolveTempl< is_ad >::DynamicVector &guess, R &computeResidual, J &computeJacobian)
 
template<typename R , typename J >
void nonlinear (DynamicVector &guess, R &computeResidual, J &computeJacobian)
 
template<typename R , typename J >
void nonlinear (NSReal &guess, R &computeResidual, J &computeJacobian)
 
template<typename R , typename J >
void nonlinear (NSRealVectorValue &guess, R &computeResidual, J &computeJacobian)
 
template<typename R , typename J >
void nonlinearTight (DynamicVector &guess, R &computeResidual, J &computeJacobian)
 
template<typename R , typename J >
void nonlinearTight (NSReal &guess, R &computeResidual, J &computeJacobian)
 
template<typename R , typename J >
void nonlinearTight (NSRealVectorValue &guess, R &computeResidual, J &computeJacobian)
 
template<typename R , typename J , typename B >
void nonlinearBounded (NSReal &guess, R &computeResidual, J &computeJacobian, B &computeBounds)
 Perform a bounded solve use Eigen::HybridNonLinearSolver. More...
 

Static Public Member Functions

static InputParameters validParams ()
 
static Real relativeToleranceDefault ()
 default values More...
 
static Real absoluteToleranceDefault ()
 
static Real xToleranceDefault ()
 
static unsigned int minIterationsDefault ()
 
static unsigned int maxIterationsDefault ()
 
static Real acceptableMultiplierDefault ()
 
static Real dampingFactorDefault ()
 
static unsigned int maxDampingIterationsDefault ()
 
template<typename V >
static Real normSquare (const V &v)
 Compute squared norm of v (dropping derivatives as this is only for convergence checking) More...
 
static Real normSquare (const NSReal &v)
 
static Real normSquare (const NSRealVectorValue &v)
 
template<typename V >
static bool isRelSmall (const V &a, const V &b, const V &c)
 Check if |a| < |b * c| for all elements in a and b. This checks if 'a' is small relative to. More...
 
static bool isRelSmall (const NSReal &a, const NSReal &b, const NSReal &c)
 
static bool isRelSmall (const NSRealVectorValue &a, const NSRealVectorValue &b, const NSReal &c)
 
static bool isRelSmall (const DynamicVector &a, const DynamicVector &b, const NSReal &c)
 

Public Attributes

Real _relative_tolerance_square
 
Real _absolute_tolerance_square
 
Real _delta_thresh
 Threshold for minimum step size of linear iterations. More...
 
Real _damping_factor
 Damping factor. More...
 
unsigned int _max_damping_iterations
 
unsigned int _min_iterations
 
unsigned int _max_iterations
 
Real _acceptable_multiplier
 

Protected Member Functions

void sizeItems (const NestedSolveTempl< is_ad >::DynamicVector &guess, NestedSolveTempl< is_ad >::DynamicVector &residual, NestedSolveTempl< is_ad >::DynamicMatrix &jacobian) const
 Size a dynamic Jacobian matrix correctly. More...
 
template<typename V , typename T >
void sizeItems (const V &, V &, T &) const
 Sizing is a no-op for compile time sized types (and scalars) More...
 
bool isConverged (Real r0_square, Real r_square, bool acceptable)
 Convergence check (updates _status) More...
 
template<typename J , typename V >
void linear (const J &A, V &x, const V &b) const
 Solve A*x=b for x. More...
 
void linear (const NSReal &A, NSReal &x, const NSReal &b) const
 
void linear (const NSRankTwoTensor &A, NSRealVectorValue &x, const NSRealVectorValue &b) const
 

Protected Attributes

State _state
 current solver state More...
 
std::size_t _n_iterations
 number of nested iterations More...
 

Private Member Functions

template<bool store_residual_norm, typename R , typename J >
auto make_adaptor (R &residual, J &jacobian)
 Build a suitable Eigen adaptor functors to interface between moose and Eigen types. More...
 
template<bool store_residual_norm, typename R , typename J >
auto make_real_adaptor (R &residual, J &jacobian)
 
template<bool store_residual_norm, typename R , typename J >
auto make_realvector_adaptor (R &residual, J &jacobian)
 

Detailed Description

template<bool is_ad>
class NestedSolveTempl< is_ad >

NestedSolveTempl<is_ad> and its instantiations NestedSolve and ADNestedSolve are utility classes that implement a non-linear solver.

These classes can be instantiated in any MOOSE object to perform local Newton-Raphson solves.

Definition at line 42 of file NestedSolve.h.

Member Typedef Documentation

◆ CorrespondingJacobian

template<bool is_ad>
template<typename T >
using NestedSolveTempl< is_ad >::CorrespondingJacobian = typename NestedSolveInternal::CorrespondingJacobianTempl<T>::type

Definition at line 65 of file NestedSolve.h.

◆ DynamicMatrix

template<bool is_ad>
using NestedSolveTempl< is_ad >::DynamicMatrix = Eigen::Matrix<NSReal, Eigen::Dynamic, Eigen::Dynamic>

Definition at line 58 of file NestedSolve.h.

◆ DynamicVector

template<bool is_ad>
using NestedSolveTempl< is_ad >::DynamicVector = Eigen::Matrix<NSReal, Eigen::Dynamic, 1>

Eigen type shortcuts.

Definition at line 57 of file NestedSolve.h.

◆ Jacobian

template<bool is_ad>
template<int N = 0>
using NestedSolveTempl< is_ad >::Jacobian = typename std::conditional<N == 1, NSReal, typename std::conditional<N == 0, NestedSolveTempl<is_ad>::DynamicMatrix, Eigen::Matrix<NSReal, N, N> >::type>::type

Jacobian matrix type.

Definition at line 84 of file NestedSolve.h.

◆ NSRankTwoTensor

template<bool is_ad>
using NestedSolveTempl< is_ad >::NSRankTwoTensor = Moose::GenericType<RankTwoTensor, is_ad>

Definition at line 53 of file NestedSolve.h.

◆ NSReal

template<bool is_ad>
using NestedSolveTempl< is_ad >::NSReal = Moose::GenericType<Real, is_ad>

AD/non-AD switched type shortcuts.

Definition at line 51 of file NestedSolve.h.

◆ NSRealVectorValue

template<bool is_ad>
using NestedSolveTempl< is_ad >::NSRealVectorValue = Moose::GenericType<RealVectorValue, is_ad>

Definition at line 52 of file NestedSolve.h.

◆ Value

template<bool is_ad>
template<int N = 0>
using NestedSolveTempl< is_ad >::Value = typename std::conditional<N == 1, NSReal, typename std::conditional<N == 0, NestedSolveTempl<is_ad>::DynamicVector, Eigen::Matrix<NSReal, N, 1> >::type>::type

Residual and solution type.

Definition at line 75 of file NestedSolve.h.

Member Enumeration Documentation

◆ State

template<bool is_ad>
enum NestedSolveTempl::State
strong

possible solver states

Enumerator
NONE 
CONVERGED_ABS 
CONVERGED_REL 
CONVERGED_ACCEPTABLE_ABS 
CONVERGED_ACCEPTABLE_REL 
CONVERGED_BOUNDS 
EXACT_GUESS 
CONVERGED_XTOL 
NOT_CONVERGED 

Definition at line 147 of file NestedSolve.h.

148  {
149  NONE,
150  CONVERGED_ABS,
151  CONVERGED_REL,
152  CONVERGED_ACCEPTABLE_ABS,
153  CONVERGED_ACCEPTABLE_REL,
154  CONVERGED_BOUNDS,
155  EXACT_GUESS,
156  CONVERGED_XTOL,
157  NOT_CONVERGED
158  };

Constructor & Destructor Documentation

◆ NestedSolveTempl() [1/2]

template<bool is_ad>
NestedSolveTempl< is_ad >::NestedSolveTempl ( )

Definition at line 52 of file NestedSolve.C.

62  _n_iterations(0)
63 {
64 }
unsigned int _min_iterations
Definition: NestedSolve.h:142
Real _acceptable_multiplier
Definition: NestedSolve.h:144
static unsigned int maxDampingIterationsDefault()
Definition: NestedSolve.h:127
State _state
current solver state
Definition: NestedSolve.h:184
static Real dampingFactorDefault()
Definition: NestedSolve.h:126
static Real xToleranceDefault()
Definition: NestedSolve.h:122
Real _delta_thresh
Threshold for minimum step size of linear iterations.
Definition: NestedSolve.h:137
unsigned int _max_iterations
Definition: NestedSolve.h:143
Real _absolute_tolerance_square
Definition: NestedSolve.h:135
static Real relativeToleranceDefault()
default values
Definition: NestedSolve.h:120
std::size_t _n_iterations
number of nested iterations
Definition: NestedSolve.h:187
Real _relative_tolerance_square
Definition: NestedSolve.h:134
static unsigned int minIterationsDefault()
Definition: NestedSolve.h:123
static unsigned int maxIterationsDefault()
Definition: NestedSolve.h:124
static Real absoluteToleranceDefault()
Definition: NestedSolve.h:121
static Real acceptableMultiplierDefault()
Definition: NestedSolve.h:125
unsigned int _max_damping_iterations
Definition: NestedSolve.h:140
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template pow< 2 >(tan(_arg))+1.0) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(sqrt
Real _damping_factor
Damping factor.
Definition: NestedSolve.h:139

◆ NestedSolveTempl() [2/2]

template<bool is_ad>
NestedSolveTempl< is_ad >::NestedSolveTempl ( const InputParameters params)

Definition at line 67 of file NestedSolve.C.

68  : _relative_tolerance_square(Utility::pow<2>(params.get<Real>("relative_tolerance"))),
69  _absolute_tolerance_square(Utility::pow<2>(params.get<Real>("absolute_tolerance"))),
70  _delta_thresh(params.get<Real>("step_size_tolerance")),
71  _damping_factor(params.get<Real>("damping_factor")),
72  _max_damping_iterations(params.get<unsigned int>("max_damping_iterations")),
73  _min_iterations(params.get<unsigned int>("min_iterations")),
74  _max_iterations(params.get<unsigned int>("max_iterations")),
75  _acceptable_multiplier(params.get<Real>("acceptable_multiplier")),
77  _n_iterations(0)
78 {
79 }
unsigned int _min_iterations
Definition: NestedSolve.h:142
Real _acceptable_multiplier
Definition: NestedSolve.h:144
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
State _state
current solver state
Definition: NestedSolve.h:184
Real _delta_thresh
Threshold for minimum step size of linear iterations.
Definition: NestedSolve.h:137
unsigned int _max_iterations
Definition: NestedSolve.h:143
Real _absolute_tolerance_square
Definition: NestedSolve.h:135
std::size_t _n_iterations
number of nested iterations
Definition: NestedSolve.h:187
Real _relative_tolerance_square
Definition: NestedSolve.h:134
unsigned int _max_damping_iterations
Definition: NestedSolve.h:140
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template pow< 2 >(tan(_arg))+1.0) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(sqrt
Real _damping_factor
Damping factor.
Definition: NestedSolve.h:139

Member Function Documentation

◆ absoluteToleranceDefault()

template<bool is_ad>
static Real NestedSolveTempl< is_ad >::absoluteToleranceDefault ( )
inlinestatic

Definition at line 121 of file NestedSolve.h.

121 { return 1e-13; }

◆ acceptableMultiplierDefault()

template<bool is_ad>
static Real NestedSolveTempl< is_ad >::acceptableMultiplierDefault ( )
inlinestatic

Definition at line 125 of file NestedSolve.h.

125 { return 10.0; }

◆ dampingFactorDefault()

template<bool is_ad>
static Real NestedSolveTempl< is_ad >::dampingFactorDefault ( )
inlinestatic

Definition at line 126 of file NestedSolve.h.

126 { return 0.8; }

◆ getIterations()

template<bool is_ad>
const std::size_t& NestedSolveTempl< is_ad >::getIterations ( )
inline

Get the number of iterations from the last solve.

Definition at line 163 of file NestedSolve.h.

163 { return _n_iterations; };
std::size_t _n_iterations
number of nested iterations
Definition: NestedSolve.h:187

◆ getState()

template<bool is_ad>
const State& NestedSolveTempl< is_ad >::getState ( ) const
inline

Get the solver state.

Definition at line 161 of file NestedSolve.h.

161 { return _state; }
State _state
current solver state
Definition: NestedSolve.h:184

◆ isConverged()

template<bool is_ad>
bool NestedSolveTempl< is_ad >::isConverged ( Real  r0_square,
Real  r_square,
bool  acceptable 
)
protected

Convergence check (updates _status)

Definition at line 779 of file NestedSolve.h.

780 {
781  if (acceptable)
783  if (r_square < _absolute_tolerance_square)
784  {
786  return true;
787  }
788  if (r_square / r0_square < _relative_tolerance_square)
789  {
791  return true;
792  }
793  return false;
794 }
Real _acceptable_multiplier
Definition: NestedSolve.h:144
State _state
current solver state
Definition: NestedSolve.h:184
Real _absolute_tolerance_square
Definition: NestedSolve.h:135
Real _relative_tolerance_square
Definition: NestedSolve.h:134

◆ isRelSmall() [1/4]

template<bool is_ad>
template<typename V >
static bool NestedSolveTempl< is_ad >::isRelSmall ( const V &  a,
const V &  b,
const V &  c 
)
static

Check if |a| < |b * c| for all elements in a and b. This checks if 'a' is small relative to.

◆ isRelSmall() [2/4]

template<bool is_ad>
bool NestedSolveTempl< is_ad >::isRelSmall ( const NSReal a,
const NSReal b,
const NSReal c 
)
static

Definition at line 117 of file NestedSolve.C.

118 {
119  return (abs(MetaPhysicL::raw_value(a)) <
121 }
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:42
auto raw_value(const Eigen::Map< T > &in)
Definition: EigenADReal.h:73

◆ isRelSmall() [3/4]

template<bool is_ad>
bool NestedSolveTempl< is_ad >::isRelSmall ( const NSRealVectorValue a,
const NSRealVectorValue b,
const NSReal c 
)
static

Definition at line 125 of file NestedSolve.C.

128 {
129  for (const auto i : make_range(LIBMESH_DIM))
130  {
131  if (abs(MetaPhysicL::raw_value(a)(i)) >=
133  return false;
134  }
135  return true;
136 }
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:42
auto raw_value(const Eigen::Map< T > &in)
Definition: EigenADReal.h:73
IntRange< T > make_range(T beg, T end)

◆ isRelSmall() [4/4]

template<bool is_ad>
bool NestedSolveTempl< is_ad >::isRelSmall ( const DynamicVector a,
const DynamicVector b,
const NSReal c 
)
static

Definition at line 140 of file NestedSolve.C.

143 {
144  return (a.cwiseAbs().array() < b.cwiseAbs().array() * MetaPhysicL::raw_value(c)).all();
145 }
auto raw_value(const Eigen::Map< T > &in)
Definition: EigenADReal.h:73

◆ linear() [1/3]

template<bool is_ad>
template<typename J , typename V >
void NestedSolveTempl< is_ad >::linear ( const J &  A,
V &  x,
const V &  b 
) const
protected

Solve A*x=b for x.

Definition at line 546 of file NestedSolve.h.

547 {
548  // we could make the linear solve method configurable here
549  x = A.partialPivLu().solve(b);
550 }

◆ linear() [2/3]

template<bool is_ad>
void NestedSolveTempl< is_ad >::linear ( const NSReal A,
NSReal x,
const NSReal b 
) const
inlineprotected

Definition at line 203 of file NestedSolve.h.

203 { x = b / A; }

◆ linear() [3/3]

template<bool is_ad>
void NestedSolveTempl< is_ad >::linear ( const NSRankTwoTensor A,
NSRealVectorValue x,
const NSRealVectorValue b 
) const
protected

Definition at line 94 of file NestedSolve.C.

97 {
98  x = A.inverse() * b;
99 }

◆ make_adaptor()

template<bool is_ad>
template<bool store_residual_norm, typename R , typename J >
auto NestedSolveTempl< is_ad >::make_adaptor ( R &  residual,
J &  jacobian 
)
private

Build a suitable Eigen adaptor functors to interface between moose and Eigen types.

Definition at line 753 of file NestedSolve.h.

754 {
756  residual_lambda, jacobian_lambda);
757 }
Adaptor functor for Eigen::Matrix based residual and Jacobian types.
Definition: NestedSolve.h:624

◆ make_real_adaptor()

template<bool is_ad>
template<bool store_residual_norm, typename R , typename J >
auto NestedSolveTempl< is_ad >::make_real_adaptor ( R &  residual,
J &  jacobian 
)
private

Definition at line 761 of file NestedSolve.h.

762 {
764  residual_lambda, jacobian_lambda);
765 }
Adaptor functor for scalar Real valued residual and Jacobian types.
Definition: NestedSolve.h:664

◆ make_realvector_adaptor()

template<bool is_ad>
template<bool store_residual_norm, typename R , typename J >
auto NestedSolveTempl< is_ad >::make_realvector_adaptor ( R &  residual,
J &  jacobian 
)
private

Definition at line 770 of file NestedSolve.h.

771 {
773  residual_lambda, jacobian_lambda);
774 }
Adaptor functor for MOOSE RealVectorValue/RankTwoTensor residual and Jacobian types.
Definition: NestedSolve.h:704

◆ maxDampingIterationsDefault()

template<bool is_ad>
static unsigned int NestedSolveTempl< is_ad >::maxDampingIterationsDefault ( )
inlinestatic

Definition at line 127 of file NestedSolve.h.

127 { return 100; }

◆ maxIterationsDefault()

template<bool is_ad>
static unsigned int NestedSolveTempl< is_ad >::maxIterationsDefault ( )
inlinestatic

Definition at line 124 of file NestedSolve.h.

124 { return 1000; }

◆ minIterationsDefault()

template<bool is_ad>
static unsigned int NestedSolveTempl< is_ad >::minIterationsDefault ( )
inlinestatic

Definition at line 123 of file NestedSolve.h.

123 { return 3; }

◆ nonlinear() [1/5]

template<bool is_ad>
template<typename V , typename T >
void NestedSolveTempl< is_ad >::nonlinear ( V &  guess,
T &&  compute 
)

Solve the N*N nonlinear equation system using a built-in Netwon-Raphson loop.

if we exceed the max iterations, we could still be converged (considering the acceptable multiplier)

Definition at line 427 of file NestedSolve.h.

428 {
429  V delta;
430  V residual;
431  CorrespondingJacobian<V> jacobian;
432  sizeItems(guess, residual, jacobian);
433 
434  _n_iterations = 0;
435  compute(guess, residual, jacobian);
436 
437  // compute first residual norm for relative convergence checks
438  auto r0_square = normSquare(residual);
439  if (r0_square == 0)
440  {
442  return;
443  }
444  auto r_square = r0_square;
445 
446  // perform non-linear iterations
448  {
449  // check convergence
450  if (_n_iterations >= _min_iterations && isConverged(r0_square, r_square, /*acceptable=*/false))
451  return;
452 
453  // solve and apply next increment
454  linear(jacobian, delta, residual);
455 
456  // Check if step size is smaller than the floating point tolerance
457  if (isRelSmall(delta, guess, _delta_thresh))
458  {
460  return;
461  }
462 
463  guess -= delta;
464  _n_iterations++;
465 
466  // compute residual and jacobian for the next iteration
467  compute(guess, residual, jacobian);
468 
469  r_square = normSquare(residual);
470  }
471 
475  if (!isConverged(r0_square, r_square, /*acceptable=*/true))
477 }
unsigned int _min_iterations
Definition: NestedSolve.h:142
State _state
current solver state
Definition: NestedSolve.h:184
static bool isRelSmall(const V &a, const V &b, const V &c)
Check if |a| < |b * c| for all elements in a and b. This checks if &#39;a&#39; is small relative to...
Real _delta_thresh
Threshold for minimum step size of linear iterations.
Definition: NestedSolve.h:137
void sizeItems(const NestedSolveTempl< is_ad >::DynamicVector &guess, NestedSolveTempl< is_ad >::DynamicVector &residual, NestedSolveTempl< is_ad >::DynamicMatrix &jacobian) const
Size a dynamic Jacobian matrix correctly.
Definition: NestedSolve.C:83
unsigned int _max_iterations
Definition: NestedSolve.h:143
void linear(const J &A, V &x, const V &b) const
Solve A*x=b for x.
Definition: NestedSolve.h:546
bool isConverged(Real r0_square, Real r_square, bool acceptable)
Convergence check (updates _status)
Definition: NestedSolve.h:779
static Real normSquare(const V &v)
Compute squared norm of v (dropping derivatives as this is only for convergence checking) ...
Definition: NestedSolve.h:555
std::size_t _n_iterations
number of nested iterations
Definition: NestedSolve.h:187

◆ nonlinear() [2/5]

template<bool is_ad>
template<typename R , typename J >
void NestedSolveTempl< is_ad >::nonlinear ( DynamicVector guess,
R &  computeResidual,
J &  computeJacobian 
)

The separate residual/Jacobian functor versions use Eigen::HybridNonLinearSolver with a custom backwards compatible convergence check that allows for looser tolerances.

◆ nonlinear() [3/5]

template<bool is_ad>
template<typename R , typename J >
void NestedSolveTempl< is_ad >::nonlinear ( NSReal guess,
R &  computeResidual,
J &  computeJacobian 
)

if we exceed the max iterations, we could still be converged (considering the acceptable multiplier)

Definition at line 242 of file NestedSolve.h.

243 {
245 
246  // adaptor functor to utilize the Eigen non-linear solver
247  auto functor = make_real_adaptor<true>(computeResidual, computeJacobian);
248  Eigen::HybridNonLinearSolver<decltype(functor), NSReal> solver(functor);
249  V guess_eigen(1);
250  guess_eigen << guess;
251  const auto & r_square = functor.getResidualNorm();
252  solver.solveInit(guess_eigen);
253 
254  // compute first residual norm for relative convergence checks
255  auto r0_square = r_square;
256  if (r0_square == 0)
257  {
259  return;
260  }
261 
262  // perform non-linear iterations
263  _n_iterations = 1;
264  while (_n_iterations <= _max_iterations &&
265  solver.solveOneStep(guess_eigen) == Eigen::HybridNonLinearSolverSpace::Running)
266  {
267  // check convergence
268  if (_n_iterations >= _min_iterations && isConverged(r0_square, r_square, /*acceptable=*/false))
269  {
270  guess = guess_eigen(0);
271  return;
272  }
273  _n_iterations++;
274  }
275 
279  if (!isConverged(r0_square, r_square, /*acceptable=*/true))
281 
282  guess = guess_eigen(0);
283 }
unsigned int _min_iterations
Definition: NestedSolve.h:142
State _state
current solver state
Definition: NestedSolve.h:184
unsigned int _max_iterations
Definition: NestedSolve.h:143
Eigen::Matrix< NSReal, Eigen::Dynamic, 1 > DynamicVector
Eigen type shortcuts.
Definition: NestedSolve.h:57
bool isConverged(Real r0_square, Real r_square, bool acceptable)
Convergence check (updates _status)
Definition: NestedSolve.h:779
std::size_t _n_iterations
number of nested iterations
Definition: NestedSolve.h:187

◆ nonlinear() [4/5]

template<bool is_ad>
template<typename R , typename J >
void NestedSolveTempl< is_ad >::nonlinear ( NSRealVectorValue guess,
R &  computeResidual,
J &  computeJacobian 
)

Definition at line 288 of file NestedSolve.h.

291 {
293 
294  // adaptor functor to utilize the Eigen non-linear solver
295  auto functor = make_realvector_adaptor<true>(computeResidual, computeJacobian);
296  Eigen::HybridNonLinearSolver<decltype(functor), NSReal> solver(functor);
297  V guess_eigen(3);
298  guess_eigen << guess(0), guess(1), guess(2);
299  auto status = solver.solve(guess_eigen);
300  guess(0) = guess_eigen(0);
301  guess(1) = guess_eigen(1);
302  guess(2) = guess_eigen(2);
303 
304  if (status == Eigen::HybridNonLinearSolverSpace::RelativeErrorTooSmall)
306  else
308 }
State _state
current solver state
Definition: NestedSolve.h:184
MPI_Status status
Eigen::Matrix< NSReal, Eigen::Dynamic, 1 > DynamicVector
Eigen type shortcuts.
Definition: NestedSolve.h:57

◆ nonlinear() [5/5]

template<bool is_ad>
template<typename R , typename J >
void NestedSolveTempl< is_ad >::nonlinear ( NestedSolveTempl< is_ad >::DynamicVector guess,
R &  computeResidual,
J &  computeJacobian 
)

Definition at line 224 of file NestedSolve.h.

227 {
228  // adaptor functor to utilize the Eigen non-linear solver
229  auto functor = make_adaptor<true>(computeResidual, computeJacobian);
230  Eigen::HybridNonLinearSolver<decltype(functor), NSReal> solver(functor);
231  auto status = solver.solve(guess);
232 
233  if (status == Eigen::HybridNonLinearSolverSpace::RelativeErrorTooSmall)
235  else
237 }
State _state
current solver state
Definition: NestedSolve.h:184
MPI_Status status

◆ nonlinearBounded()

template<bool is_ad>
template<typename R , typename J , typename B >
void NestedSolveTempl< is_ad >::nonlinearBounded ( NSReal guess,
R &  computeResidual,
J &  computeJacobian,
B computeBounds 
)

Perform a bounded solve use Eigen::HybridNonLinearSolver.

Definition at line 377 of file NestedSolve.h.

381 {
382  auto functor = make_real_adaptor<false>(computeResidual, computeJacobian);
383  Eigen::HybridNonLinearSolver<decltype(functor), NSReal> solver(functor);
384 
386  guess_eigen << guess;
387 
388  auto status = solver.solveInit(guess_eigen);
389  if (status == Eigen::HybridNonLinearSolverSpace::ImproperInputParameters)
390  return;
391 
392  bool bounds_hit = false;
393  while (status == Eigen::HybridNonLinearSolverSpace::Running)
394  {
395  status = solver.solveOneStep(guess_eigen);
396  const std::pair<Real, Real> & bounds = computeBounds();
397 
398  // Snap to bounds. Terminate solve if we snap twice consecutively.
399  if (guess_eigen(0) < bounds.first || guess_eigen(0) > bounds.second)
400  {
401  if (guess_eigen(0) < bounds.first)
402  guess_eigen(0) = bounds.first;
403  else
404  guess_eigen(0) = bounds.second;
405 
406  if (bounds_hit)
407  {
409  return;
410  }
411 
412  bounds_hit = true;
413  }
414  }
415 
416  if (status == Eigen::HybridNonLinearSolverSpace::RelativeErrorTooSmall)
418  else
420 
421  guess = guess_eigen(0);
422 }
State _state
current solver state
Definition: NestedSolve.h:184
MPI_Status status
Eigen::Matrix< NSReal, Eigen::Dynamic, 1 > DynamicVector
Eigen type shortcuts.
Definition: NestedSolve.h:57

◆ nonlinearDamped()

template<bool is_ad>
template<typename V , typename T , typename C >
void NestedSolveTempl< is_ad >::nonlinearDamped ( V &  guess,
T &&  compute,
C &&  computeCondition 
)

Solve the N*N nonlinear equation system using the damped Netwon-Raphson loop.

Definition at line 482 of file NestedSolve.h.

483 {
484  V delta;
485  V residual;
486  CorrespondingJacobian<V> jacobian;
487  sizeItems(guess, residual, jacobian);
488 
489  _n_iterations = 0;
490  compute(guess, residual, jacobian);
491 
492  // compute first residual norm for relative convergence checks
493  auto r0_square = normSquare(residual);
494  if (r0_square == 0)
495  {
497  return;
498  }
499  auto r_square = r0_square;
500 
501  // perform non-linear iterations
503  {
504  // check convergence
505  if (_n_iterations >= _min_iterations && isConverged(r0_square, r_square, /*acceptable=*/false))
506  return;
507 
508  // solve and apply next increment
509  linear(jacobian, delta, residual);
510 
511  // Check if step size is smaller than the floating point tolerance
512  if (isRelSmall(delta, guess, _delta_thresh))
513  {
515  return;
516  }
517 
518  Real alpha = 1;
519  unsigned int damping_iterations = 0;
520  auto prev_guess = guess;
521  guess -= delta;
522 
523  while (!computeCondition(guess) && (damping_iterations < _max_damping_iterations))
524  {
525  alpha *= _damping_factor;
526  guess = prev_guess - alpha * delta;
527  damping_iterations += 1;
528  }
529  _n_iterations++;
530 
531  // compute residual and jacobian for the next iteration
532  compute(guess, residual, jacobian);
533 
534  r_square = normSquare(residual);
535  }
536 
537  // if we exceed the max iterations, we could still be converged (considering the acceptable
538  // multiplier)
539  if (!isConverged(r0_square, r_square, /*acceptable=*/true))
541 }
unsigned int _min_iterations
Definition: NestedSolve.h:142
State _state
current solver state
Definition: NestedSolve.h:184
static bool isRelSmall(const V &a, const V &b, const V &c)
Check if |a| < |b * c| for all elements in a and b. This checks if &#39;a&#39; is small relative to...
Real _delta_thresh
Threshold for minimum step size of linear iterations.
Definition: NestedSolve.h:137
void sizeItems(const NestedSolveTempl< is_ad >::DynamicVector &guess, NestedSolveTempl< is_ad >::DynamicVector &residual, NestedSolveTempl< is_ad >::DynamicMatrix &jacobian) const
Size a dynamic Jacobian matrix correctly.
Definition: NestedSolve.C:83
unsigned int _max_iterations
Definition: NestedSolve.h:143
void linear(const J &A, V &x, const V &b) const
Solve A*x=b for x.
Definition: NestedSolve.h:546
bool isConverged(Real r0_square, Real r_square, bool acceptable)
Convergence check (updates _status)
Definition: NestedSolve.h:779
static Real normSquare(const V &v)
Compute squared norm of v (dropping derivatives as this is only for convergence checking) ...
Definition: NestedSolve.h:555
std::size_t _n_iterations
number of nested iterations
Definition: NestedSolve.h:187
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
unsigned int _max_damping_iterations
Definition: NestedSolve.h:140
Real _damping_factor
Damping factor.
Definition: NestedSolve.h:139

◆ nonlinearTight() [1/4]

template<bool is_ad>
template<typename R , typename J >
void NestedSolveTempl< is_ad >::nonlinearTight ( DynamicVector guess,
R &  computeResidual,
J &  computeJacobian 
)

The separate residual/Jacobian functor versions use Eigen::HybridNonLinearSolver with the built-in convergence check to tight numerical tolerance.

◆ nonlinearTight() [2/4]

template<bool is_ad>
template<typename R , typename J >
void NestedSolveTempl< is_ad >::nonlinearTight ( NSReal guess,
R &  computeResidual,
J &  computeJacobian 
)

Definition at line 331 of file NestedSolve.h.

332 {
334 
335  // adaptor functor to utilize the Eigen non-linear solver
336  auto functor = make_real_adaptor<false>(computeResidual, computeJacobian);
337  Eigen::HybridNonLinearSolver<decltype(functor), NSReal> solver(functor);
338  V guess_eigen(1);
339  guess_eigen << guess;
340  auto status = solver.solve(guess_eigen);
341  guess = guess_eigen(0);
342 
343  if (status == Eigen::HybridNonLinearSolverSpace::RelativeErrorTooSmall)
345  else
347 }
State _state
current solver state
Definition: NestedSolve.h:184
MPI_Status status
Eigen::Matrix< NSReal, Eigen::Dynamic, 1 > DynamicVector
Eigen type shortcuts.
Definition: NestedSolve.h:57

◆ nonlinearTight() [3/4]

template<bool is_ad>
template<typename R , typename J >
void NestedSolveTempl< is_ad >::nonlinearTight ( NSRealVectorValue guess,
R &  computeResidual,
J &  computeJacobian 
)

Definition at line 352 of file NestedSolve.h.

355 {
357 
358  // adaptor functor to utilize the Eigen non-linear solver
359  auto functor = make_realvector_adaptor<false>(computeResidual, computeJacobian);
360  Eigen::HybridNonLinearSolver<decltype(functor), NSReal> solver(functor);
361  V guess_eigen(3);
362  guess_eigen << guess(0), guess(1), guess(2);
363  auto status = solver.solve(guess_eigen);
364  guess(0) = guess_eigen(0);
365  guess(1) = guess_eigen(1);
366  guess(2) = guess_eigen(2);
367 
368  if (status == Eigen::HybridNonLinearSolverSpace::RelativeErrorTooSmall)
370  else
372 }
State _state
current solver state
Definition: NestedSolve.h:184
MPI_Status status
Eigen::Matrix< NSReal, Eigen::Dynamic, 1 > DynamicVector
Eigen type shortcuts.
Definition: NestedSolve.h:57

◆ nonlinearTight() [4/4]

template<bool is_ad>
template<typename R , typename J >
void NestedSolveTempl< is_ad >::nonlinearTight ( NestedSolveTempl< is_ad >::DynamicVector guess,
R &  computeResidual,
J &  computeJacobian 
)

Definition at line 313 of file NestedSolve.h.

316 {
317  // adaptor functor to utilize the Eigen non-linear solver
318  auto functor = make_adaptor<false>(computeResidual, computeJacobian);
319  Eigen::HybridNonLinearSolver<decltype(functor), NSReal> solver(functor);
320  auto status = solver.solve(guess);
321 
322  if (status == Eigen::HybridNonLinearSolverSpace::RelativeErrorTooSmall)
324  else
326 }
State _state
current solver state
Definition: NestedSolve.h:184
MPI_Status status

◆ normSquare() [1/3]

template<bool is_ad>
template<typename V >
Real NestedSolveTempl< is_ad >::normSquare ( const V &  v)
static

Compute squared norm of v (dropping derivatives as this is only for convergence checking)

Definition at line 555 of file NestedSolve.h.

Referenced by NestedSolveInternal::DynamicMatrixEigenAdaptorFunctor< is_ad, R, J, store_residual_norm >::operator()(), NestedSolveInternal::RealEigenAdaptorFunctor< is_ad, R, J, store_residual_norm >::operator()(), and NestedSolveInternal::RealVectorEigenAdaptorFunctor< is_ad, R, J, store_residual_norm >::operator()().

556 {
557  // we currently cannot get the raw_value of an AD Eigen Matrix, so we loop over components
558  Real norm_square = 0.0;
559  for (const auto i : index_range(v))
560  norm_square += Utility::pow<2>(MetaPhysicL::raw_value(v.data()[i]));
561  return norm_square;
562 }
auto raw_value(const Eigen::Map< T > &in)
Definition: EigenADReal.h:73
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
auto index_range(const T &sizable)
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template pow< 2 >(tan(_arg))+1.0) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(sqrt

◆ normSquare() [2/3]

template<bool is_ad>
Real NestedSolveTempl< is_ad >::normSquare ( const NSReal v)
static

Definition at line 103 of file NestedSolve.C.

104 {
106 }
auto raw_value(const Eigen::Map< T > &in)
Definition: EigenADReal.h:73
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template pow< 2 >(tan(_arg))+1.0) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(sqrt

◆ normSquare() [3/3]

template<bool is_ad>
Real NestedSolveTempl< is_ad >::normSquare ( const NSRealVectorValue v)
static

Definition at line 110 of file NestedSolve.C.

111 {
113 }
auto raw_value(const Eigen::Map< T > &in)
Definition: EigenADReal.h:73

◆ relativeToleranceDefault()

template<bool is_ad>
static Real NestedSolveTempl< is_ad >::relativeToleranceDefault ( )
inlinestatic

default values

Definition at line 120 of file NestedSolve.h.

120 { return 1e-8; }

◆ setAbsoluteTolerance()

template<bool is_ad>
void NestedSolveTempl< is_ad >::setAbsoluteTolerance ( Real  abs)
inline

Definition at line 131 of file NestedSolve.h.

MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:42
Real _absolute_tolerance_square
Definition: NestedSolve.h:135

◆ setAcceptableMultiplier()

template<bool is_ad>
void NestedSolveTempl< is_ad >::setAcceptableMultiplier ( Real  am)
inline

Definition at line 132 of file NestedSolve.h.

132 { _acceptable_multiplier = am; }
Real _acceptable_multiplier
Definition: NestedSolve.h:144

◆ setRelativeTolerance()

template<bool is_ad>
void NestedSolveTempl< is_ad >::setRelativeTolerance ( Real  rel)
inline

Definition at line 130 of file NestedSolve.h.

130 { _relative_tolerance_square = rel * rel; }
Real _relative_tolerance_square
Definition: NestedSolve.h:134

◆ sizeItems() [1/2]

template<bool is_ad>
void NestedSolveTempl< is_ad >::sizeItems ( const NestedSolveTempl< is_ad >::DynamicVector guess,
NestedSolveTempl< is_ad >::DynamicVector residual,
NestedSolveTempl< is_ad >::DynamicMatrix jacobian 
) const
protected

Size a dynamic Jacobian matrix correctly.

Definition at line 83 of file NestedSolve.C.

86 {
87  const auto N = guess.size();
88  residual.resize(N, 1);
89  jacobian.resize(N, N);
90 }

◆ sizeItems() [2/2]

template<bool is_ad>
template<typename V , typename T >
void NestedSolveTempl< is_ad >::sizeItems ( const V &  ,
V &  ,
T &   
) const
inlineprotected

Sizing is a no-op for compile time sized types (and scalars)

Definition at line 196 of file NestedSolve.h.

197  {
198  }

◆ validParams()

template<bool is_ad>
InputParameters NestedSolveTempl< is_ad >::validParams ( )
static

Definition at line 16 of file NestedSolve.C.

17 {
19 
20  // Newton iteration control parameters
21  params.addParam<Real>("relative_tolerance",
23  "Relative convergence tolerance for Newton iteration");
24  params.addParam<Real>("absolute_tolerance",
26  "Absolute convergence tolerance for Newton iteration");
27  params.addParam<Real>("step_size_tolerance",
29  "Minimum step size of linear iterations relative to value of the solution");
30  params.addParam<unsigned int>(
31  "min_iterations",
33  "Minimum number of nonlinear iterations to execute before accepting convergence");
34  params.addParam<unsigned int>(
35  "max_iterations", maxIterationsDefault(), "Maximum number of nonlinear iterations");
36  params.addParam<Real>("acceptable_multiplier",
38  "Factor applied to relative and absolute "
39  "tolerance for acceptable nonlinear convergence if "
40  "iterations are no longer making progress");
41  params.addParam<Real>("damping_factor",
43  "Factor applied to step size if guess does not satisfy damping criteria");
44  params.addParam<unsigned int>(
45  "max_damping_iterations",
47  "Maximum number of damping steps per linear iteration of nested solve");
48  return params;
49 }
static unsigned int maxDampingIterationsDefault()
Definition: NestedSolve.h:127
static Real dampingFactorDefault()
Definition: NestedSolve.h:126
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static Real xToleranceDefault()
Definition: NestedSolve.h:122
InputParameters emptyInputParameters()
static Real relativeToleranceDefault()
default values
Definition: NestedSolve.h:120
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static unsigned int minIterationsDefault()
Definition: NestedSolve.h:123
static unsigned int maxIterationsDefault()
Definition: NestedSolve.h:124
static Real absoluteToleranceDefault()
Definition: NestedSolve.h:121
static Real acceptableMultiplierDefault()
Definition: NestedSolve.h:125
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...

◆ xToleranceDefault()

template<bool is_ad>
static Real NestedSolveTempl< is_ad >::xToleranceDefault ( )
inlinestatic

Definition at line 122 of file NestedSolve.h.

122 { return 1e-15; }

Member Data Documentation

◆ _absolute_tolerance_square

template<bool is_ad>
Real NestedSolveTempl< is_ad >::_absolute_tolerance_square

Definition at line 135 of file NestedSolve.h.

Referenced by NestedSolveTempl< is_ad >::setAbsoluteTolerance().

◆ _acceptable_multiplier

template<bool is_ad>
Real NestedSolveTempl< is_ad >::_acceptable_multiplier

Definition at line 144 of file NestedSolve.h.

Referenced by NestedSolveTempl< is_ad >::setAcceptableMultiplier().

◆ _damping_factor

template<bool is_ad>
Real NestedSolveTempl< is_ad >::_damping_factor

Damping factor.

Definition at line 139 of file NestedSolve.h.

◆ _delta_thresh

template<bool is_ad>
Real NestedSolveTempl< is_ad >::_delta_thresh

Threshold for minimum step size of linear iterations.

Definition at line 137 of file NestedSolve.h.

◆ _max_damping_iterations

template<bool is_ad>
unsigned int NestedSolveTempl< is_ad >::_max_damping_iterations

Definition at line 140 of file NestedSolve.h.

◆ _max_iterations

template<bool is_ad>
unsigned int NestedSolveTempl< is_ad >::_max_iterations

Definition at line 143 of file NestedSolve.h.

◆ _min_iterations

template<bool is_ad>
unsigned int NestedSolveTempl< is_ad >::_min_iterations

Definition at line 142 of file NestedSolve.h.

◆ _n_iterations

template<bool is_ad>
std::size_t NestedSolveTempl< is_ad >::_n_iterations
protected

number of nested iterations

Definition at line 187 of file NestedSolve.h.

Referenced by NestedSolveTempl< is_ad >::getIterations().

◆ _relative_tolerance_square

template<bool is_ad>
Real NestedSolveTempl< is_ad >::_relative_tolerance_square

Definition at line 134 of file NestedSolve.h.

Referenced by NestedSolveTempl< is_ad >::setRelativeTolerance().

◆ _state

template<bool is_ad>
State NestedSolveTempl< is_ad >::_state
protected

current solver state

Definition at line 184 of file NestedSolve.h.

Referenced by NestedSolveTempl< is_ad >::getState().


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