Newton's method
Newton's method is intended to compute updates to some q by solving the following equation.
\begin{equation} \frac{\partial R(q)}{\partial q} \Delta q = -R(q) \end{equation}
In the most basic implementation of Newton's method in PDESolver, q corresponds to the solution, and f(q) corresponds to the residual evaluation of the currently selected physics module.
An example of a more sophisticated use of Newton's method is within the Crank-Nicolson timestepper, which adds another layer on top of the physics residual:
\begin{equation} \frac{\partial g(R(q))}{\partial q} \Delta q = -g(R(q)) \end{equation}
Features
PDESolver's Newton's method has a wide variety of features. It contains the Jacobian calculation routines, which can be performed currently using:
finite-differencing
complex-step
The Jacobian functions can act upon any arbitrary residual.
Additionally, the following matrix forms are supported:
Julia dense
Julia sparse
PETSc sparse
Matrix-free
The function that performs the Newton iteration is
NonlinearSolvers.newtonInner
— Function.This function contains the algorithm for Newton's method. It is intended to be used by other functions rather than invoked as a solver directly. For example newton
uses newtonInner to solve steady problems while crank_nicolson
uses it to solve the nonlinear problem arising from the time discretization.
For reference, Newton's method solves the problem R(q) = 0 using dR/dq delta q = -R(q) where R is the rsidual and q is the solution
On entry, eqn.q_vec must contain the initial guess for q. On exit, eqn.q_vec will contain the solution to f(q) = 0. eqn.q will also be consistent with eqn.q_vec, as will the send and receive buffers in eqn.shared_data
On exit, rhs_vec will have the residual corresponding to eqn.q_vec in it, with the imaginary part set to zero.
The AbstractLO
and AbstractPC
supplied inside the LinearSolver
object must match the jac_type
.
When doing inexact Newton-Krylov, newonInner
modifies the tolerances of the linear solver. Users calling newtonInner
repeatedly with the same linear solver object should reset the initial tolerances as needed.
Inputs:
newton_data: NewtonData object, typically obtained from setupNewton
mesh: a mesh object
sbp: an SBP operator
eqn: a solution data object
opts: options dictionary
ls: a LinearSolver with the preconditioner and linear operator fully initialized.
rhs_vec: vector to store R(q) in
ctx_residual: extra data required by rhs_func
The user must supply two functions, one to calculate the residual vector (referred to as rhs_vec), and another to compute the Jacobian.
rhs_func should compute (eqn.q_vec) -> (rhs_vec) and have the signature
rhs_func(mesh, sbp, eqn, opts, rhs_vec, ctx_residual, t=0.0)
Note that the ctx_residual passed into newtonInner is passed directly to rhs_func. The contents of ctx_residual can be whatever is needed by rhs_func to perform its computation. See physicsRhs
for the specific requirements on rhs_func and for an example implementation.
The same ctx_residual passed into newtonInner is passed directly to calcLinearOperator
..
This function supports jacobian/preconditioner freezing using the prefix "newton".
Aliasing restrictions: None. In particular, rhs_vec can alias eqn.res_vec, and this leads so some efficiency because it avoids needlessly copying data.
The private data required by Newtons method is stored in the NewtonData
object.
NonlinearSolvers.setupNewton
— Function.NonlinearSolvers.setupNewton
Performs setup work for newtonInner
, including creating a NewtonData
object.
This function also resets the implicit Euler globalization.
alloc_rhs: keyword arg to allocate a new object or not for rhs_vec true (default) allocates a new vector false will use eqn.res_vec
rhs_func: only used for Petsc in matrix-free mode to do Jac-vec products should be the rhs_func passed into newtonInner
ctx_residual: ctx_residual passed into newtonInner
Allocates Jac & RHS
See cleanupNewton
to the cleanup function
Utils.free
— Method.Cleans up after running Newton's method.
Inputs
newton_data: the NewtonData object
NonlinearSolvers.getNewtonPCandLO
— Function.Returns the Newton precondtioner and linear operator specified by the options dictionary
Inputs
mesh
sbp
eqn
opts
rhs_func: rhs_func required by
newtonInner
Newton internals
NewtonData API
NewtonData
has a small API used by Newton's method.
NonlinearSolvers.NewtonData
— Type.This type holds the data required by newtonInner
as well as configuration settings.
Public Fields
myrank: MPI rank
commsize: MPI communicator size
itr: number of iterations
res_norm_i: current iteration residual norm
res_norm_i_1: previous iteration residual norm
step_norm_i: current iteration newton step norm
step_norm_i_1: previous iteration newton step norm
res_norm_rel: norm of residual used as the reference point when computing relative residuals. If this is -1 on entry to newtonInner, then the norm of the initial residual is used.
step_fac: factor used in step size limiter
res_reltol: nonlinear relative residual tolerance
res_abstol: nonlinear residual absolute tolerance
step_tol: step norm tolerance
itermax: maximum number of newton iterations
use_inexact_nk: true if inexact-NK should be used, false otherwise
krylov_gamma: parameter used by inexact newton-krylov
recalc_policy: a
RecalculationPolicy
.ls: a
LinearSolver
fconv: convergence.dat file handle (or DevNull if not used)
verbose: how much logging/output to do
Options Keys
If res_reltol0
is negative, the residual of the initial condition will be used for res_norm_rel
newton_verbosity
is used to the verbose
field
NonlinearSolvers.reinitNewtonData
— Function.Reinitialized the NewtonData object for a new solve.
Note that this does not reset the linear solver, which might be a problem if inexact newton-krylov was used for the previous solve
NonlinearSolvers.recordResNorm
— Function.Records the most recent nonlinear residual norm in the NewtonData object. Also updates the implicit Euler globalization
Inputs
newton_data: the NewtonData
res_norm: the residual norm
NonlinearSolvers.recordStepNorm
— Function.Records norm of the most recent newton step (ie. the norm of delta q) in the NewtonData object
Inputs
newton_data: the NewtonData object
step_norm: the norm of the step
Linear Operators and Preconditioners
A full set of linear operators and preconditioners are provided for solving a linear system where the linear operator is the jacobian of the physics.. These often are a good start for constructing linear operators for unsteady problems or more advanced methods for solving steady problems.
NonlinearSolvers.NewtonMatPC
— Type.Matrix-based Petsc preconditioner for Newton's method
NonlinearSolvers.NewtonMatPC
— Method.Outer constructor for NewtonMatPC
NonlinearSolvers.NewtonVolumePC
— Type.This type holds all the data needed to calculate a preconditioner based only on the volume integrals. For DG methods, this matrix is block diagonal and thus easily invertible. This preconditioner is applied matrix-free.
jac_size is numDofPerNode * numNodesPerElememnt (the total number of unknowns on an element).
This preconditioner is not suitable for use as an inner preconditioner, but the functions calcVolumePC
, factorVolumePC
, and applyVolumPC
can be easily used to make a new PC.
Fields
volume_jac: jacobian of the volume integrals of each element, jac_size x jac_size x numEl
ipiv: permutation information, jac_size x numEl
is_factored: if true, volume_jac has been factored (LU with partial pivoting), false otherwise
Regular constructor
Inputs
mesh: the mesh
sbp: the SBP operator
eqn: AbstractSolutionData
opts: options dictionary
NonlinearSolvers.NewtonDenseLO
— Type.Dense linear operator for Newton's method.
Subtype of AbstractDenseLO
NonlinearSolvers.NewtonDenseLO
— Method.Outer constructor for NewtonDenseLO
Sparse direct linear operator for Newton's method. Subtype of AbstractSparseDirectLO
NonlinearSolvers.NewtonSparseDirectLO
— Method.Outer constructor for NewtonSparseDirectLO
Petsc matrix based linear operator for Newton's method.
Subtype of AbstractPetscMatLO
NonlinearSolvers.NewtonPetscMatLO
— Method.Outer constructor for NewtonPetscMatLO
Petsc matrix-free linear operator for Newton's method.
Subtype of AbstractPetscMatFreeLO
NonlinearSolvers.NewtonPetscMatFreeLO
— Method.Newton mat-free linear operator constructor
Inputs
pc
mesh
sbp
eqn
opts
rhs_func: rhs_func from
newtonInner