#include <SimpleNewtonNonlinearSolver.hpp>
Public Member Functions | |
SimpleNewtonNonlinearSolver (double linearSolverRelativeTolerance=1e-6) | |
virtual | ~SimpleNewtonNonlinearSolver () |
virtual Vec | Solve (PetscErrorCode(*pComputeResidual)(SNES, Vec, Vec, void *), PetscErrorCode(*pComputeJacobian)(SNES, Vec, Mat *, Mat *, MatStructure *, void *), Vec initialGuess, unsigned fill, void *pContext) |
void | SetTolerance (double tolerance) |
void | SetWriteStats (bool writeStats=true) |
Private Attributes | |
double | mLinearSolverRelativeTolerance |
double | mTolerance |
bool | mWriteStats |
std::vector< double > | mTestDampingValues |
Definition at line 40 of file SimpleNewtonNonlinearSolver.hpp.
SimpleNewtonNonlinearSolver::SimpleNewtonNonlinearSolver | ( | double | linearSolverRelativeTolerance = 1e-6 |
) |
Constructor.
linearSolverRelativeTolerance | defaults to 1e-6 |
Definition at line 35 of file SimpleNewtonNonlinearSolver.cpp.
References mTestDampingValues.
SimpleNewtonNonlinearSolver::~SimpleNewtonNonlinearSolver | ( | ) | [virtual] |
Destructor.
Definition at line 49 of file SimpleNewtonNonlinearSolver.cpp.
Vec SimpleNewtonNonlinearSolver::Solve | ( | PetscErrorCode(*)(SNES, Vec, Vec, void *) | pComputeResidual, | |
PetscErrorCode(*)(SNES, Vec, Mat *, Mat *, MatStructure *, void *) | pComputeJacobian, | |||
Vec | initialGuess, | |||
unsigned | fill, | |||
void * | pContext | |||
) | [virtual] |
Solve a nonlinear system using Newton's method with damping. Newton's algorithm is
x_new = x_old - J^{-1} f
where J is the Jacobian matrix evaluated at x_old and f the residual evaluated at x_old. The Newton method with damping is
x_new = x_old - s J^{-1} f
where s is some damping factor. Here s is chosen by just looked at a fixed set of possible damping factors and choosing the one which gives the best x_new (the one for which the residual evaluated at x_new has the lowest norm).
The solver quits once the ||f||/numVariables
pComputeResidual | points to the function which computes the residual, it must take arguments SNES (a PETSc nonlinear solver object), Vec (current guess - a vector of the correct size), Vec (a Vec of the correct size in which the residual is returned), void* (a pointer to anything you may need to refer to when calculating the residual) | |
pComputeJacobian | points to the function which computes the Jacobian, it must take arguments SNES (a PETSc nonlinear solver * object), Mat* (a pointer to the Jacobian matrix) ,Mat* (a pointer to a preconditioner matrix), MatStructure* (points to the PETSc matrix type e.g. AIJ), void* (a pointer to anything you may need to refer to when calculating the residual). | |
initialGuess | A PETSc Vec of the correct size, containing initial guesses for the nonlinear solver. | |
pContext | [optional] A pointer to a class that may have to be used in the ComputeResidual and ComputeJacobian functions | |
fill | the expected maximum number of nonzeros in a row of the Jacobian matrix |
In the same file, but outside this class the functions ComputeResidual and ComputeJacobian must sit, using the input arguments specified above.
Implements AbstractNonlinearSolver.
Definition at line 52 of file SimpleNewtonNonlinearSolver.cpp.
References PetscVecTools::AddScaledVector(), EXCEPTION, mTestDampingValues, mTolerance, mWriteStats, LinearSystem::rGetLhsMatrix(), LinearSystem::rGetRhsVector(), LinearSystem::Solve(), PetscVecTools::WAXPY(), and LinearSystem::ZeroLinearSystem().
void SimpleNewtonNonlinearSolver::SetTolerance | ( | double | tolerance | ) |
Set a tolerance other than the default.
tolerance |
Definition at line 165 of file SimpleNewtonNonlinearSolver.cpp.
References mTolerance.
void SimpleNewtonNonlinearSolver::SetWriteStats | ( | bool | writeStats = true |
) | [inline] |
Call to set the solver to write details as it solves.
writeStats | defaults to true |
Definition at line 128 of file SimpleNewtonNonlinearSolver.hpp.
References mWriteStats.
double SimpleNewtonNonlinearSolver::mLinearSolverRelativeTolerance [private] |
The linear solver relative tolerance.
Definition at line 43 of file SimpleNewtonNonlinearSolver.hpp.
double SimpleNewtonNonlinearSolver::mTolerance [private] |
The tolerance (set to 1e-5 in constructor).
Definition at line 44 of file SimpleNewtonNonlinearSolver.hpp.
Referenced by SetTolerance(), and Solve().
bool SimpleNewtonNonlinearSolver::mWriteStats [private] |
Whether the solver writes details as it solves (set to false in constructor).
Definition at line 45 of file SimpleNewtonNonlinearSolver.hpp.
Referenced by SetWriteStats(), and Solve().
std::vector<double> SimpleNewtonNonlinearSolver::mTestDampingValues [private] |
Vector of possible damping factors (set in the constructor).
Definition at line 47 of file SimpleNewtonNonlinearSolver.hpp.
Referenced by SimpleNewtonNonlinearSolver(), and Solve().