OGS 6.1.0-1721-g6382411ad
NumLib::NonlinearSolver< NonlinearSolverTag::Newton > Class Template Referencefinal

Detailed Description

template<>
class NumLib::NonlinearSolver< NonlinearSolverTag::Newton >

Find a solution to a nonlinear equation using the Newton-Raphson method.

Definition at line 79 of file NonlinearSolver.h.

#include <NonlinearSolver.h>

Inheritance diagram for NumLib::NonlinearSolver< NonlinearSolverTag::Newton >:
Collaboration diagram for NumLib::NonlinearSolver< NonlinearSolverTag::Newton >:

Public Types

using System = NonlinearSystem< NonlinearSolverTag::Newton >
 Type of the nonlinear equation system to be solved. More...
 

Public Member Functions

 NonlinearSolver (GlobalLinearSolver &linear_solver, int const maxiter, double const damping=1.0)
 
void setEquationSystem (System &eq, ConvergenceCriterion &conv_crit)
 
void assemble (GlobalVector const &x) const override
 
NonlinearSolverStatus solve (GlobalVector &x, std::function< void(int, GlobalVector const &)> const &postIterationCallback) override
 
- Public Member Functions inherited from NumLib::NonlinearSolverBase
virtual ~NonlinearSolverBase ()=default
 

Private Attributes

GlobalLinearSolver & _linear_solver
 
System_equation_system = nullptr
 
ConvergenceCriterion_convergence_criterion = nullptr
 
int const _maxiter
 maximum number of iterations More...
 
double const _damping
 
std::size_t _res_id = 0u
 ID of the residual vector. More...
 
std::size_t _J_id = 0u
 ID of the Jacobian matrix. More...
 
std::size_t _minus_delta_x_id = 0u
 ID of the $ -\Delta x$ vector. More...
 
std::size_t _x_new_id
 ID of the vector storing $ x - (-\Delta x) $. More...
 

Member Typedef Documentation

◆ System

Type of the nonlinear equation system to be solved.

Definition at line 84 of file NonlinearSolver.h.

Constructor & Destructor Documentation

◆ NonlinearSolver()

NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::NonlinearSolver ( GlobalLinearSolver &  linear_solver,
int const  maxiter,
double const  damping = 1.0 
)
inlineexplicit

Constructs a new instance.

Parameters
linear_solverthe linear solver used by this nonlinear solver.
maxiterthe maximum number of iterations used to solve the equation.
dampingA positive damping factor.
See also
_damping

Definition at line 94 of file NonlinearSolver.h.

97  : _linear_solver(linear_solver), _maxiter(maxiter), _damping(damping)
98  {
99  }
int const _maxiter
maximum number of iterations

Member Function Documentation

◆ assemble()

void NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::assemble ( GlobalVector const &  x) const
overridevirtual

Only assemble the equation system.

Note
This method is needed to preload CrankNicolson time discretization scheme. It is not used for the general solver steps; in those only the solve() method is needed.
Parameters
xthe state at which the equation system will be assembled.

Implements NumLib::NonlinearSolverBase.

Definition at line 172 of file NonlinearSolver.cpp.

174 {
176  // TODO if the equation system would be reset to nullptr after each
177  // assemble() or solve() call, the user would be forced to set the
178  // equation every time and could not forget it.
179 }
virtual void assemble(GlobalVector const &x)=0

◆ setEquationSystem()

void NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::setEquationSystem ( System eq,
ConvergenceCriterion conv_crit 
)
inline

Set the nonlinear equation system that will be solved. TODO doc

Definition at line 103 of file NonlinearSolver.h.

◆ solve()

NonlinearSolverStatus NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::solve ( GlobalVector &  x,
std::function< void(int, GlobalVector const &)> const &  postIterationCallback 
)
overridevirtual

Assemble and solve the equation system.

Parameters
xin: the initial guess, out: the solution.
postIterationCallbackcalled after each iteration if set.
Return values
trueif the equation system could be solved
falseotherwise

Implements NumLib::NonlinearSolverBase.

Definition at line 181 of file NonlinearSolver.cpp.

References MathLib::LinAlg::axpy(), MathLib::LinAlg::copy(), BaseLib::RunTime::elapsed(), NumLib::FAILURE, NumLib::MatrixProvider::getMatrix(), NumLib::VectorProvider::getVector(), NumLib::GlobalVectorProvider::provider, NumLib::GlobalMatrixProvider::provider, NumLib::MatrixProvider::releaseMatrix(), NumLib::VectorProvider::releaseVector(), NumLib::REPEAT_ITERATION, BaseLib::RunTime::start(), and NumLib::SUCCESS.

185 {
186  namespace LinAlg = MathLib::LinAlg;
187  auto& sys = *_equation_system;
188 
190  _res_id);
191  auto& minus_delta_x =
194  auto& J =
196 
197  bool error_norms_met = false;
198 
199  // TODO be more efficient
200  // init minus_delta_x to the right size
201  LinAlg::copy(x, minus_delta_x);
202 
204 
205  int iteration = 1;
206  for (; iteration <= _maxiter;
207  ++iteration, _convergence_criterion->reset())
208  {
209  BaseLib::RunTime timer_dirichlet;
210  double time_dirichlet = 0.0;
211 
212  BaseLib::RunTime time_iteration;
213  time_iteration.start();
214 
215  timer_dirichlet.start();
216  sys.computeKnownSolutions(x);
217  sys.applyKnownSolutions(x);
218  time_dirichlet += timer_dirichlet.elapsed();
219 
220  sys.preIteration(iteration, x);
221 
222  BaseLib::RunTime time_assembly;
223  time_assembly.start();
224  sys.assemble(x);
225  sys.getResidual(x, res);
226  sys.getJacobian(J);
227  INFO("[time] Assembly took %g s.", time_assembly.elapsed());
228 
229  minus_delta_x.setZero();
230 
231  timer_dirichlet.start();
232  sys.applyKnownSolutionsNewton(J, res, minus_delta_x);
233  time_dirichlet += timer_dirichlet.elapsed();
234  INFO("[time] Applying Dirichlet BCs took %g s.", time_dirichlet);
235 
236  if (!sys.isLinear() && _convergence_criterion->hasResidualCheck())
238 
239  BaseLib::RunTime time_linear_solver;
240  time_linear_solver.start();
241  bool iteration_succeeded = _linear_solver.solve(J, res, minus_delta_x);
242  INFO("[time] Linear solver took %g s.", time_linear_solver.elapsed());
243 
244  if (!iteration_succeeded)
245  {
246  ERR("Newton: The linear solver failed.");
247  }
248  else
249  {
250  // TODO could be solved in a better way
251  // cf.
252  // http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Vec/VecWAXPY.html
253  auto& x_new =
255  LinAlg::axpy(x_new, -_damping, minus_delta_x);
256 
257  if (postIterationCallback)
258  postIterationCallback(iteration, x_new);
259 
260  switch(sys.postIteration(x_new))
261  {
263  break;
265  ERR("Newton: The postIteration() hook reported a "
266  "non-recoverable error.");
267  iteration_succeeded = false;
268  break;
270  INFO(
271  "Newton: The postIteration() hook decided that this "
272  "iteration"
273  " has to be repeated.");
274  // TODO introduce some onDestroy hook.
276  continue; // That throws the iteration result away.
277  }
278 
279  LinAlg::copy(x_new, x); // copy new solution to x
281  }
282 
283  if (!iteration_succeeded)
284  {
285  // Don't compute further error norms, but break here.
286  error_norms_met = false;
287  break;
288  }
289 
290  if (sys.isLinear()) {
291  error_norms_met = true;
292  } else {
294  // Note: x contains the new solution!
295  _convergence_criterion->checkDeltaX(minus_delta_x, x);
296  }
297 
298  error_norms_met = _convergence_criterion->isSatisfied();
299  }
300 
301  INFO("[time] Iteration #%u took %g s.", iteration,
302  time_iteration.elapsed());
303 
304  if (error_norms_met)
305  break;
306 
307  // Avoid increment of the 'iteration' if the error norms are not met,
308  // but maximum number of iterations is reached.
309  if (iteration >= _maxiter)
310  break;
311  }
312 
313  if (iteration > _maxiter)
314  {
315  ERR("Newton: Could not solve the given nonlinear system within %u "
316  "iterations",
317  _maxiter);
318  }
319 
323  minus_delta_x);
324 
325  return {error_norms_met, iteration};
326 }
std::size_t _J_id
ID of the Jacobian matrix.
virtual void releaseMatrix(GlobalMatrix const &A)=0
virtual GlobalVector & getVector()=0
Get an uninitialized vector.
static NUMLIB_EXPORT MatrixProvider & provider
virtual bool hasResidualCheck() const =0
static NUMLIB_EXPORT VectorProvider & provider
std::size_t _x_new_id
ID of the vector storing .
int const _maxiter
maximum number of iterations
virtual void checkDeltaX(GlobalVector const &minus_delta_x, GlobalVector const &x)=0
virtual GlobalMatrix & getMatrix()=0
Get an uninitialized matrix.
Count the running time.
Definition: RunTime.h:32
void start()
Start the timer.
Definition: RunTime.h:36
std::size_t _res_id
ID of the residual vector.
virtual bool hasDeltaXCheck() const =0
virtual void releaseVector(GlobalVector const &x)=0
virtual bool isSatisfied() const
Tell if the convergence criterion is satisfied.
void axpy(MatrixOrVector &y, double const a, MatrixOrVector const &x)
Computes .
Definition: LinAlg.h:57
void copy(MatrixOrVector const &x, MatrixOrVector &y)
Copies x to y.
Definition: LinAlg.h:36
virtual void checkResidual(GlobalVector const &residual)=0
Check if the residual satisfies the convergence criterion.
double elapsed() const
Get the elapsed time after started.
Definition: RunTime.h:52

Member Data Documentation

◆ _convergence_criterion

ConvergenceCriterion* NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::_convergence_criterion = nullptr
private

Definition at line 121 of file NonlinearSolver.h.

◆ _damping

double const NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::_damping
private

A positive damping factor. The default value 1.0 gives a non-damped Newton method. Common values are in the range 0.5 to 0.7 for somewhat conservative method and seldom become smaller than 0.2 for very conservative approach.

Definition at line 128 of file NonlinearSolver.h.

◆ _equation_system

System* NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::_equation_system = nullptr
private

Definition at line 118 of file NonlinearSolver.h.

◆ _J_id

std::size_t NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::_J_id = 0u
private

ID of the Jacobian matrix.

Definition at line 131 of file NonlinearSolver.h.

◆ _linear_solver

GlobalLinearSolver& NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::_linear_solver
private

Definition at line 117 of file NonlinearSolver.h.

◆ _maxiter

int const NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::_maxiter
private

maximum number of iterations

Definition at line 122 of file NonlinearSolver.h.

◆ _minus_delta_x_id

std::size_t NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::_minus_delta_x_id = 0u
private

ID of the $ -\Delta x$ vector.

Definition at line 132 of file NonlinearSolver.h.

◆ _res_id

std::size_t NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::_res_id = 0u
private

ID of the residual vector.

Definition at line 130 of file NonlinearSolver.h.

◆ _x_new_id

std::size_t NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::_x_new_id
private
Initial value:
=
0u

ID of the vector storing $ x - (-\Delta x) $.

Definition at line 133 of file NonlinearSolver.h.


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