xref: /petsc/doc/manual/snes.md (revision 4e8208cbcbc709572b8abe32f33c78b69c819375)
17f296bb3SBarry Smith(ch_snes)=
27f296bb3SBarry Smith
37f296bb3SBarry Smith# SNES: Nonlinear Solvers
47f296bb3SBarry Smith
57f296bb3SBarry SmithThe solution of large-scale nonlinear problems pervades many facets of
67f296bb3SBarry Smithcomputational science and demands robust and flexible solution
77f296bb3SBarry Smithstrategies. The `SNES` library of PETSc provides a powerful suite of
87f296bb3SBarry Smithdata-structure-neutral numerical routines for such problems. Built on
97f296bb3SBarry Smithtop of the linear solvers and data structures discussed in preceding
107f296bb3SBarry Smithchapters, `SNES` enables the user to easily customize the nonlinear
117f296bb3SBarry Smithsolvers according to the application at hand. Also, the `SNES`
127f296bb3SBarry Smithinterface is *identical* for the uniprocess and parallel cases; the only
137f296bb3SBarry Smithdifference in the parallel version is that each process typically forms
147f296bb3SBarry Smithonly its local contribution to various matrices and vectors.
157f296bb3SBarry Smith
167f296bb3SBarry SmithThe `SNES` class includes methods for solving systems of nonlinear
177f296bb3SBarry Smithequations of the form
187f296bb3SBarry Smith
197f296bb3SBarry Smith$$
207f296bb3SBarry Smith\mathbf{F}(\mathbf{x}) = 0,
217f296bb3SBarry Smith$$ (fx0)
227f296bb3SBarry Smith
237f296bb3SBarry Smithwhere $\mathbf{F}: \, \Re^n \to \Re^n$. Newton-like methods provide the
247f296bb3SBarry Smithcore of the package, including both line search and trust region
257f296bb3SBarry Smithtechniques. A suite of nonlinear Krylov methods and methods based upon
267f296bb3SBarry Smithproblem decomposition are also included. The solvers are discussed
277f296bb3SBarry Smithfurther in {any}`sec_nlsolvers`. Following the PETSc design
287f296bb3SBarry Smithphilosophy, the interfaces to the various solvers are all virtually
297f296bb3SBarry Smithidentical. In addition, the `SNES` software is completely flexible, so
307f296bb3SBarry Smiththat the user can at runtime change any facet of the solution process.
317f296bb3SBarry Smith
327f296bb3SBarry SmithPETSc’s default method for solving the nonlinear equation is Newton’s
337f296bb3SBarry Smithmethod with line search, `SNESNEWTONLS`. The general form of the $n$-dimensional Newton’s method
347f296bb3SBarry Smithfor solving {math:numref}`fx0` is
357f296bb3SBarry Smith
367f296bb3SBarry Smith$$
377f296bb3SBarry Smith\mathbf{x}_{k+1} = \mathbf{x}_k - \mathbf{J}(\mathbf{x}_k)^{-1} \mathbf{F}(\mathbf{x}_k), \;\; k=0,1, \ldots,
387f296bb3SBarry Smith$$ (newton)
397f296bb3SBarry Smith
407f296bb3SBarry Smithwhere $\mathbf{x}_0$ is an initial approximation to the solution and
417f296bb3SBarry Smith$\mathbf{J}(\mathbf{x}_k) = \mathbf{F}'(\mathbf{x}_k)$, the Jacobian, is nonsingular at each
427f296bb3SBarry Smithiteration. In practice, the Newton iteration {math:numref}`newton` is
437f296bb3SBarry Smithimplemented by the following two steps:
447f296bb3SBarry Smith
457f296bb3SBarry Smith$$
467f296bb3SBarry Smith\begin{aligned}
477f296bb3SBarry Smith1. & \text{(Approximately) solve} & \mathbf{J}(\mathbf{x}_k) \Delta \mathbf{x}_k &= -\mathbf{F}(\mathbf{x}_k). \\
487f296bb3SBarry Smith2. & \text{Update} & \mathbf{x}_{k+1} &\gets \mathbf{x}_k + \Delta \mathbf{x}_k.
497f296bb3SBarry Smith\end{aligned}
507f296bb3SBarry Smith$$
517f296bb3SBarry Smith
527f296bb3SBarry SmithOther defect-correction algorithms can be implemented by using different
537f296bb3SBarry Smithchoices for $J(\mathbf{x}_k)$.
547f296bb3SBarry Smith
557f296bb3SBarry Smith(sec_snesusage)=
567f296bb3SBarry Smith
577f296bb3SBarry Smith## Basic SNES Usage
587f296bb3SBarry Smith
597f296bb3SBarry SmithIn the simplest usage of the nonlinear solvers, the user must merely
607f296bb3SBarry Smithprovide a C, C++, Fortran, or Python routine to evaluate the nonlinear function
617f296bb3SBarry Smith{math:numref}`fx0`. The corresponding Jacobian matrix
627f296bb3SBarry Smithcan be approximated with finite differences. For codes that are
637f296bb3SBarry Smithtypically more efficient and accurate, the user can provide a routine to
647f296bb3SBarry Smithcompute the Jacobian; details regarding these application-provided
657f296bb3SBarry Smithroutines are discussed below. To provide an overview of the use of the
667f296bb3SBarry Smithnonlinear solvers, browse the concrete example in {ref}`ex1.c <snes-ex1>` or skip ahead to the discussion.
677f296bb3SBarry Smith
687f296bb3SBarry Smith(snes_ex1)=
697f296bb3SBarry Smith
707f296bb3SBarry Smith:::{admonition} Listing: `src/snes/tutorials/ex1.c`
717f296bb3SBarry Smith```{literalinclude} /../src/snes/tutorials/ex1.c
727f296bb3SBarry Smith:end-before: /*TEST
737f296bb3SBarry Smith```
747f296bb3SBarry Smith:::
757f296bb3SBarry Smith
767f296bb3SBarry SmithTo create a `SNES` solver, one must first call `SNESCreate()` as
777f296bb3SBarry Smithfollows:
787f296bb3SBarry Smith
797f296bb3SBarry Smith```
807f296bb3SBarry SmithSNESCreate(MPI_Comm comm, SNES *snes);
817f296bb3SBarry Smith```
827f296bb3SBarry Smith
837f296bb3SBarry SmithThe user must then set routines for evaluating the residual function {math:numref}`fx0`
847f296bb3SBarry Smithand, *possibly*, its associated Jacobian matrix, as
857f296bb3SBarry Smithdiscussed in the following sections.
867f296bb3SBarry Smith
877f296bb3SBarry SmithTo choose a nonlinear solution method, the user can either call
887f296bb3SBarry Smith
897f296bb3SBarry Smith```
907f296bb3SBarry SmithSNESSetType(SNES snes, SNESType method);
917f296bb3SBarry Smith```
927f296bb3SBarry Smith
937f296bb3SBarry Smithor use the option `-snes_type <method>`, where details regarding the
947f296bb3SBarry Smithavailable methods are presented in {any}`sec_nlsolvers`. The
957f296bb3SBarry Smithapplication code can take complete control of the linear and nonlinear
967f296bb3SBarry Smithtechniques used in the Newton-like method by calling
977f296bb3SBarry Smith
987f296bb3SBarry Smith```
997f296bb3SBarry SmithSNESSetFromOptions(snes);
1007f296bb3SBarry Smith```
1017f296bb3SBarry Smith
1027f296bb3SBarry SmithThis routine provides an interface to the PETSc options database, so
1037f296bb3SBarry Smiththat at runtime the user can select a particular nonlinear solver, set
1047f296bb3SBarry Smithvarious parameters and customized routines (e.g., specialized line
1057f296bb3SBarry Smithsearch variants), prescribe the convergence tolerance, and set
1067f296bb3SBarry Smithmonitoring routines. With this routine the user can also control all
1077f296bb3SBarry Smithlinear solver options in the `KSP`, and `PC` modules, as discussed
1087f296bb3SBarry Smithin {any}`ch_ksp`.
1097f296bb3SBarry Smith
1107f296bb3SBarry SmithAfter having set these routines and options, the user solves the problem
1117f296bb3SBarry Smithby calling
1127f296bb3SBarry Smith
1137f296bb3SBarry Smith```
1147f296bb3SBarry SmithSNESSolve(SNES snes, Vec b, Vec x);
1157f296bb3SBarry Smith```
1167f296bb3SBarry Smith
1177f296bb3SBarry Smithwhere `x` should be initialized to the initial guess before calling and contains the solution on return.
1187f296bb3SBarry SmithIn particular, to employ an initial guess of
1197f296bb3SBarry Smithzero, the user should explicitly set this vector to zero by calling
1207f296bb3SBarry Smith`VecZeroEntries(x)`. Finally, after solving the nonlinear system (or several
1217f296bb3SBarry Smithsystems), the user should destroy the `SNES` context with
1227f296bb3SBarry Smith
1237f296bb3SBarry Smith```
1247f296bb3SBarry SmithSNESDestroy(SNES *snes);
1257f296bb3SBarry Smith```
1267f296bb3SBarry Smith
1277f296bb3SBarry Smith(sec_snesfunction)=
1287f296bb3SBarry Smith
1297f296bb3SBarry Smith### Nonlinear Function Evaluation
1307f296bb3SBarry Smith
1317f296bb3SBarry SmithWhen solving a system of nonlinear equations, the user must provide a
1327f296bb3SBarry Smitha residual function {math:numref}`fx0`, which is set using
1337f296bb3SBarry Smith
1347f296bb3SBarry Smith```
135*2a8381b2SBarry SmithSNESSetFunction(SNES snes, Vec f, PetscErrorCode (*FormFunction)(SNES snes, Vec x, Vec f, PetscCtx ctx), PetscCtx ctx);
1367f296bb3SBarry Smith```
1377f296bb3SBarry Smith
1387f296bb3SBarry SmithThe argument `f` is an optional vector for storing the solution; pass `NULL` to have the `SNES` allocate it for you.
1397f296bb3SBarry SmithThe argument `ctx` is an optional user-defined context, which can
1407f296bb3SBarry Smithstore any private, application-specific data required by the function
1417f296bb3SBarry Smithevaluation routine; `NULL` should be used if such information is not
1427f296bb3SBarry Smithneeded. In C and C++, a user-defined context is merely a structure in
1437f296bb3SBarry Smithwhich various objects can be stashed; in Fortran a user context can be
1447f296bb3SBarry Smithan integer array that contains both parameters and pointers to PETSc
1457f296bb3SBarry Smithobjects.
1467f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tutorials/ex5.c.html">SNES Tutorial ex5</a>
1477f296bb3SBarry Smithand
1487f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tutorials/ex5f90.F90.html">SNES Tutorial ex5f90</a>
1497f296bb3SBarry Smithgive examples of user-defined application contexts in C and Fortran,
1507f296bb3SBarry Smithrespectively.
1517f296bb3SBarry Smith
1527f296bb3SBarry Smith(sec_snesjacobian)=
1537f296bb3SBarry Smith
1547f296bb3SBarry Smith### Jacobian Evaluation
1557f296bb3SBarry Smith
1567f296bb3SBarry SmithThe user may also specify a routine to form some approximation of the
1577f296bb3SBarry SmithJacobian matrix, `A`, at the current iterate, `x`, as is typically
1587f296bb3SBarry Smithdone with
1597f296bb3SBarry Smith
1607f296bb3SBarry Smith```
161*2a8381b2SBarry SmithSNESSetJacobian(SNES snes, Mat Amat, Mat Pmat, PetscErrorCode (*FormJacobian)(SNES snes, Vec x, Mat A, Mat B, PetscCtx ctx), PetscCtx ctx);
1627f296bb3SBarry Smith```
1637f296bb3SBarry Smith
1647f296bb3SBarry SmithThe arguments of the routine `FormJacobian()` are the current iterate,
1657f296bb3SBarry Smith`x`; the (approximate) Jacobian matrix, `Amat`; the matrix from
1667f296bb3SBarry Smithwhich the preconditioner is constructed, `Pmat` (which is usually the
1677f296bb3SBarry Smithsame as `Amat`); and an optional user-defined Jacobian context,
1687f296bb3SBarry Smith`ctx`, for application-specific data. The `FormJacobian()`
1697f296bb3SBarry Smithcallback is only invoked if the solver requires it, always
1707f296bb3SBarry Smith*after* `FormFunction()` has been called at the current iterate.
1717f296bb3SBarry Smith
1727f296bb3SBarry SmithNote that the `SNES` solvers
1737f296bb3SBarry Smithare all data-structure neutral, so the full range of PETSc matrix
1747f296bb3SBarry Smithformats (including “matrix-free” methods) can be used.
1757f296bb3SBarry Smith{any}`ch_matrices` discusses information regarding
1767f296bb3SBarry Smithavailable matrix formats and options, while {any}`sec_nlmatrixfree` focuses on matrix-free methods in
1777f296bb3SBarry Smith`SNES`. We briefly touch on a few details of matrix usage that are
1787f296bb3SBarry Smithparticularly important for efficient use of the nonlinear solvers.
1797f296bb3SBarry Smith
1807f296bb3SBarry SmithA common usage paradigm is to assemble the problem Jacobian in the
1817f296bb3SBarry Smithpreconditioner storage `B`, rather than `A`. In the case where they
1827f296bb3SBarry Smithare identical, as in many simulations, this makes no difference.
1837f296bb3SBarry SmithHowever, it allows us to check the analytic Jacobian we construct in
1847f296bb3SBarry Smith`FormJacobian()` by passing the `-snes_mf_operator` flag. This
1857f296bb3SBarry Smithcauses PETSc to approximate the Jacobian using finite differencing of
1867f296bb3SBarry Smiththe function evaluation (discussed in {any}`sec_fdmatrix`),
1877f296bb3SBarry Smithand the analytic Jacobian becomes merely the preconditioner. Even if the
1887f296bb3SBarry Smithanalytic Jacobian is incorrect, it is likely that the finite difference
1897f296bb3SBarry Smithapproximation will converge, and thus this is an excellent method to
1907f296bb3SBarry Smithverify the analytic Jacobian. Moreover, if the analytic Jacobian is
1917f296bb3SBarry Smithincomplete (some terms are missing or approximate),
1927f296bb3SBarry Smith`-snes_mf_operator` may be used to obtain the exact solution, where
1937f296bb3SBarry Smiththe Jacobian approximation has been transferred to the preconditioner.
1947f296bb3SBarry Smith
1957f296bb3SBarry SmithOne such approximate Jacobian comes from “Picard linearization”, use `SNESSetPicard()`, which
1967f296bb3SBarry Smithwrites the nonlinear system as
1977f296bb3SBarry Smith
1987f296bb3SBarry Smith$$
1997f296bb3SBarry Smith\mathbf{F}(\mathbf{x}) := \mathbf{A}(\mathbf{x}) \mathbf{x} - \mathbf{b} = 0
2007f296bb3SBarry Smith$$
2017f296bb3SBarry Smith
2027f296bb3SBarry Smithwhere $\mathbf{A}(\mathbf{x})$ usually contains the lower-derivative parts of the
2037f296bb3SBarry Smithequation. For example, the nonlinear diffusion problem
2047f296bb3SBarry Smith
2057f296bb3SBarry Smith$$
2067f296bb3SBarry Smith- \nabla\cdot(\kappa(u) \nabla u) = 0
2077f296bb3SBarry Smith$$
2087f296bb3SBarry Smith
2097f296bb3SBarry Smithwould be linearized as
2107f296bb3SBarry Smith
2117f296bb3SBarry Smith$$
2127f296bb3SBarry SmithA(u) v \simeq -\nabla\cdot(\kappa(u) \nabla v).
2137f296bb3SBarry Smith$$
2147f296bb3SBarry Smith
2157f296bb3SBarry SmithUsually this linearization is simpler to implement than Newton and the
2167f296bb3SBarry Smithlinear problems are somewhat easier to solve. In addition to using
2177f296bb3SBarry Smith`-snes_mf_operator` with this approximation to the Jacobian, the
2187f296bb3SBarry SmithPicard iterative procedure can be performed by defining $\mathbf{J}(\mathbf{x})$
2197f296bb3SBarry Smithto be $\mathbf{A}(\mathbf{x})$. Sometimes this iteration exhibits better global
2207f296bb3SBarry Smithconvergence than Newton linearization.
2217f296bb3SBarry Smith
2227f296bb3SBarry SmithDuring successive calls to `FormJacobian()`, the user can either
2237f296bb3SBarry Smithinsert new matrix contexts or reuse old ones, depending on the
2247f296bb3SBarry Smithapplication requirements. For many sparse matrix formats, reusing the
2257f296bb3SBarry Smithold space (and merely changing the matrix elements) is more efficient;
2267f296bb3SBarry Smithhowever, if the matrix nonzero structure completely changes, creating an
2277f296bb3SBarry Smithentirely new matrix context may be preferable. Upon subsequent calls to
2287f296bb3SBarry Smiththe `FormJacobian()` routine, the user may wish to reinitialize the
2297f296bb3SBarry Smithmatrix entries to zero by calling `MatZeroEntries()`. See
2307f296bb3SBarry Smith{any}`sec_othermat` for details on the reuse of the matrix
2317f296bb3SBarry Smithcontext.
2327f296bb3SBarry Smith
2337f296bb3SBarry SmithThe directory `$PETSC_DIR/src/snes/tutorials` provides a variety of
2347f296bb3SBarry Smithexamples.
2357f296bb3SBarry Smith
2367f296bb3SBarry SmithSometimes a nonlinear solver may produce a step that is not within the domain
2377f296bb3SBarry Smithof a given function, for example one with a negative pressure. When this occurs
2387f296bb3SBarry Smithone can call `SNESSetFunctionDomainError()` or `SNESSetJacobianDomainError()`
2397f296bb3SBarry Smithto indicate to `SNES` the step is not valid. One must also use `SNESGetConvergedReason()`
2407f296bb3SBarry Smithand check the reason to confirm if the solver succeeded. See {any}`sec_vi` for how to
2417f296bb3SBarry Smithprovide `SNES` with bounds on the variables to solve (differential) variational inequalities
2427f296bb3SBarry Smithand how to control properties of the line step computed.
2437f296bb3SBarry Smith
24476c63389SBarry Smith## Function Domain Errors and infinity or NaN
24576c63389SBarry Smith
24676c63389SBarry SmithOccasionally nonlinear solvers will propose solutions $u$, where the function value (or the objective function set with `SNESSetObjective()`) contains infinity or NaN.
24776c63389SBarry SmithThis can be due to bugs in the application code or because the proposed solution is not in the domain of the function. The application function can call `SNESSetFunctionDomainError()` or
24876c63389SBarry Smith`SNESSetObjectiveDomainError()` to indicate $u$ is not in the function's domain.
24976c63389SBarry Smith
25076c63389SBarry SmithSome `SNESSolve()` implementations (and related `SNESLineSearchApply()` routines) attempt to recover from the infinity or NaN; generally by shrinking the step size.
25176c63389SBarry SmithIf they are unable to recover the `SNESConvergedReason` returned will be `SNES_DIVERGED_FUNCTION_DOMAIN`, `SNES_DIVERGED_OJECTIVE_DOMAIN`, `SNES_DIVERGED_FUNCTION_NANORINF`, or `SNES_DIVERGED_OJECTIVE_NANORINF`.
25276c63389SBarry Smith
25376c63389SBarry Smith
2547f296bb3SBarry Smith(sec_nlsolvers)=
2557f296bb3SBarry Smith
2567f296bb3SBarry Smith## The Nonlinear Solvers
2577f296bb3SBarry Smith
2587f296bb3SBarry SmithAs summarized in Table {any}`tab-snesdefaults`, `SNES` includes
2597f296bb3SBarry Smithseveral Newton-like nonlinear solvers based on line search techniques
2607f296bb3SBarry Smithand trust region methods. Also provided are several nonlinear Krylov
2617f296bb3SBarry Smithmethods, as well as nonlinear methods involving decompositions of the
2627f296bb3SBarry Smithproblem.
2637f296bb3SBarry Smith
2647f296bb3SBarry SmithEach solver may have associated with it a set of options, which can be
2657f296bb3SBarry Smithset with routines and options database commands provided for this
2667f296bb3SBarry Smithpurpose. A complete list can be found by consulting the manual pages or
2677f296bb3SBarry Smithby running a program with the `-help` option; we discuss just a few in
2687f296bb3SBarry Smiththe sections below.
2697f296bb3SBarry Smith
2707f296bb3SBarry Smith```{eval-rst}
2717f296bb3SBarry Smith.. list-table:: PETSc Nonlinear Solvers
2727f296bb3SBarry Smith   :name: tab-snesdefaults
2737f296bb3SBarry Smith   :header-rows: 1
2747f296bb3SBarry Smith
2757f296bb3SBarry Smith   * - Method
2767f296bb3SBarry Smith     - SNESType
2777f296bb3SBarry Smith     - Options Name
2787f296bb3SBarry Smith     - Default Line Search
2797f296bb3SBarry Smith   * - Line Search Newton
2807f296bb3SBarry Smith     - ``SNESNEWTONLS``
2817f296bb3SBarry Smith     - ``newtonls``
2827f296bb3SBarry Smith     - ``SNESLINESEARCHBT``
2837f296bb3SBarry Smith   * - Trust region Newton
2847f296bb3SBarry Smith     - ``SNESNEWTONTR``
2857f296bb3SBarry Smith     - ``newtontr``
2867f296bb3SBarry Smith     - —
2877f296bb3SBarry Smith   * - Newton with Arc Length Continuation
2887f296bb3SBarry Smith     - ``SNESNEWTONAL``
2897f296bb3SBarry Smith     - ``newtonal``
2907f296bb3SBarry Smith     - —
2917f296bb3SBarry Smith   * - Nonlinear Richardson
2927f296bb3SBarry Smith     - ``SNESNRICHARDSON``
2937f296bb3SBarry Smith     - ``nrichardson``
294a99ef635SJonas Heinzmann     - ``SNESLINESEARCHSECANT``
2957f296bb3SBarry Smith   * - Nonlinear CG
2967f296bb3SBarry Smith     - ``SNESNCG``
2977f296bb3SBarry Smith     - ``ncg``
2987f296bb3SBarry Smith     - ``SNESLINESEARCHCP``
2997f296bb3SBarry Smith   * - Nonlinear GMRES
3007f296bb3SBarry Smith     - ``SNESNGMRES``
3017f296bb3SBarry Smith     - ``ngmres``
302a99ef635SJonas Heinzmann     - ``SNESLINESEARCHSECANT``
3037f296bb3SBarry Smith   * - Quasi-Newton
3047f296bb3SBarry Smith     - ``SNESQN``
3057f296bb3SBarry Smith     - ``qn``
3067f296bb3SBarry Smith     - see :any:`tab-qndefaults`
3077f296bb3SBarry Smith   * - Full Approximation Scheme
3087f296bb3SBarry Smith     - ``SNESFAS``
3097f296bb3SBarry Smith     - ``fas``
3107f296bb3SBarry Smith     - —
3117f296bb3SBarry Smith   * - Nonlinear ASM
3127f296bb3SBarry Smith     - ``SNESNASM``
3137f296bb3SBarry Smith     - ``nasm``
3147f296bb3SBarry Smith     - –
3157f296bb3SBarry Smith   * - ASPIN
3167f296bb3SBarry Smith     - ``SNESASPIN``
3177f296bb3SBarry Smith     - ``aspin``
3187f296bb3SBarry Smith     - ``SNESLINESEARCHBT``
3197f296bb3SBarry Smith   * - Nonlinear Gauss-Seidel
3207f296bb3SBarry Smith     - ``SNESNGS``
3217f296bb3SBarry Smith     - ``ngs``
3227f296bb3SBarry Smith     - –
3237f296bb3SBarry Smith   * - Anderson Mixing
3247f296bb3SBarry Smith     - ``SNESANDERSON``
3257f296bb3SBarry Smith     - ``anderson``
3267f296bb3SBarry Smith     - –
3277f296bb3SBarry Smith   * -  Newton with constraints (1)
3287f296bb3SBarry Smith     - ``SNESVINEWTONRSLS``
3297f296bb3SBarry Smith     - ``vinewtonrsls``
3307f296bb3SBarry Smith     - ``SNESLINESEARCHBT``
3317f296bb3SBarry Smith   * -  Newton with constraints (2)
3327f296bb3SBarry Smith     - ``SNESVINEWTONSSLS``
3337f296bb3SBarry Smith     - ``vinewtonssls``
3347f296bb3SBarry Smith     - ``SNESLINESEARCHBT``
3357f296bb3SBarry Smith   * - Multi-stage Smoothers
3367f296bb3SBarry Smith     - ``SNESMS``
3377f296bb3SBarry Smith     - ``ms``
3387f296bb3SBarry Smith     - –
3397f296bb3SBarry Smith   * - Composite
3407f296bb3SBarry Smith     - ``SNESCOMPOSITE``
3417f296bb3SBarry Smith     - ``composite``
3427f296bb3SBarry Smith     - –
3437f296bb3SBarry Smith   * - Linear solve only
3447f296bb3SBarry Smith     - ``SNESKSPONLY``
3457f296bb3SBarry Smith     - ``ksponly``
3467f296bb3SBarry Smith     - –
3477f296bb3SBarry Smith   * - Python Shell
3487f296bb3SBarry Smith     - ``SNESPYTHON``
3497f296bb3SBarry Smith     - ``python``
3507f296bb3SBarry Smith     - –
3517f296bb3SBarry Smith   * - Shell (user-defined)
3527f296bb3SBarry Smith     - ``SNESSHELL``
3537f296bb3SBarry Smith     - ``shell``
3547f296bb3SBarry Smith     - –
3557f296bb3SBarry Smith
3567f296bb3SBarry Smith```
3577f296bb3SBarry Smith
3587f296bb3SBarry Smith### Line Search Newton
3597f296bb3SBarry Smith
3607f296bb3SBarry SmithThe method `SNESNEWTONLS` (`-snes_type newtonls`) provides a
3617f296bb3SBarry Smithline search Newton method for solving systems of nonlinear equations. By
3627f296bb3SBarry Smithdefault, this technique employs cubic backtracking
3637f296bb3SBarry Smith{cite}`dennis:83`. Alternative line search techniques are
3647f296bb3SBarry Smithlisted in Table {any}`tab-linesearches`.
3657f296bb3SBarry Smith
3667f296bb3SBarry Smith```{eval-rst}
3677f296bb3SBarry Smith.. table:: PETSc Line Search Methods
3687f296bb3SBarry Smith   :name: tab-linesearches
3697f296bb3SBarry Smith
3707f296bb3SBarry Smith   ==================== =========================== ================
3717f296bb3SBarry Smith   **Line Search**      **SNESLineSearchType**      **Options Name**
3727f296bb3SBarry Smith   ==================== =========================== ================
3737f296bb3SBarry Smith   Backtracking         ``SNESLINESEARCHBT``        ``bt``
3747f296bb3SBarry Smith   (damped) step        ``SNESLINESEARCHBASIC``     ``basic``
3757f296bb3SBarry Smith   identical to above   ``SNESLINESEARCHNONE``      ``none``
376a99ef635SJonas Heinzmann   Secant method        ``SNESLINESEARCHSECANT``    ``secant``
3777f296bb3SBarry Smith   Critical point       ``SNESLINESEARCHCP``        ``cp``
378a99ef635SJonas Heinzmann   Error-oriented       ``SNESLINESEARCHNLEQERR``   ``nleqerr``
3797f296bb3SBarry Smith   Bisection            ``SNESLINESEARCHBISECTION`` ``bisection``
3807f296bb3SBarry Smith   Shell                ``SNESLINESEARCHSHELL``     ``shell``
3817f296bb3SBarry Smith   ==================== =========================== ================
3827f296bb3SBarry Smith```
3837f296bb3SBarry Smith
3847f296bb3SBarry SmithEvery `SNES` has a line search context of type `SNESLineSearch` that
3857f296bb3SBarry Smithmay be retrieved using
3867f296bb3SBarry Smith
3877f296bb3SBarry Smith```
3887f296bb3SBarry SmithSNESGetLineSearch(SNES snes, SNESLineSearch *ls);.
3897f296bb3SBarry Smith```
3907f296bb3SBarry Smith
3917f296bb3SBarry SmithThere are several default options for the line searches. The order of
3927f296bb3SBarry Smithpolynomial approximation may be set with `-snes_linesearch_order` or
3937f296bb3SBarry Smith
3947f296bb3SBarry Smith```
3957f296bb3SBarry SmithSNESLineSearchSetOrder(SNESLineSearch ls, PetscInt order);
3967f296bb3SBarry Smith```
3977f296bb3SBarry Smith
3987f296bb3SBarry Smithfor instance, 2 for quadratic or 3 for cubic. Sometimes, it may not be
3997f296bb3SBarry Smithnecessary to monitor the progress of the nonlinear iteration. In this
4007f296bb3SBarry Smithcase, `-snes_linesearch_norms` or
4017f296bb3SBarry Smith
4027f296bb3SBarry Smith```
4037f296bb3SBarry SmithSNESLineSearchSetComputeNorms(SNESLineSearch ls, PetscBool norms);
4047f296bb3SBarry Smith```
4057f296bb3SBarry Smith
4067f296bb3SBarry Smithmay be used to turn off function, step, and solution norm computation at
4077f296bb3SBarry Smiththe end of the linesearch.
4087f296bb3SBarry Smith
4097f296bb3SBarry SmithThe default line search for the line search Newton method,
4107f296bb3SBarry Smith`SNESLINESEARCHBT` involves several parameters, which are set to
4117f296bb3SBarry Smithdefaults that are reasonable for many applications. The user can
4127f296bb3SBarry Smithoverride the defaults by using the following options:
4137f296bb3SBarry Smith
4147f296bb3SBarry Smith- `-snes_linesearch_alpha <alpha>`
4157f296bb3SBarry Smith- `-snes_linesearch_maxstep <max>`
4167f296bb3SBarry Smith- `-snes_linesearch_minlambda <tol>`
4177f296bb3SBarry Smith
418a99ef635SJonas HeinzmannBesides the backtracking linesearch, there are `SNESLINESEARCHSECANT`,
419a99ef635SJonas Heinzmannwhich uses a polynomial secant minimization of $||F(x)||_2$ or an objective function
420a99ef635SJonas Heinzmannif set, and `SNESLINESEARCHCP`, which minimizes $F(x) \cdot Y$ where
4217f296bb3SBarry Smith$Y$ is the search direction. These are both potentially iterative
4227f296bb3SBarry Smithline searches, which may be used to find a better-fitted steplength in
4237f296bb3SBarry Smiththe case where a single secant search is not sufficient. The number of
4247f296bb3SBarry Smithiterations may be set with `-snes_linesearch_max_it`. In addition, the
4257f296bb3SBarry Smithconvergence criteria of the iterative line searches may be set using
4267f296bb3SBarry Smithfunction tolerances `-snes_linesearch_rtol` and
4277f296bb3SBarry Smith`-snes_linesearch_atol`, and steplength tolerance
4287f296bb3SBarry Smith`snes_linesearch_ltol`.
4297f296bb3SBarry Smith
4307f296bb3SBarry SmithFor highly non-linear problems, the bisection line search `SNESLINESEARCHBISECTION`
4317f296bb3SBarry Smithmay prove useful due to its robustness. Similar to the critical point line search
4327f296bb3SBarry Smith`SNESLINESEARCHCP`, it seeks to find the root of $F(x) \cdot Y$.
4337f296bb3SBarry SmithWhile the latter does so through a secant method, the bisection line search
4347f296bb3SBarry Smithdoes so by iteratively bisecting the step length interval.
4357f296bb3SBarry SmithIt works as follows (with $f(\lambda)=F(x-\lambda Y) \cdot Y / ||Y||$ for brevity):
4367f296bb3SBarry Smith
4377f296bb3SBarry Smith1. initialize: $j=1$, $\lambda_0 = \lambda_{\text{left}} = 0.0$, $\lambda_j = \lambda_{\text{right}} = \alpha$, compute $f(\lambda_0)$ and $f(\lambda_j)$
4387f296bb3SBarry Smith
4397f296bb3SBarry Smith2. check whether there is a change of sign in the interval: $f(\lambda_{\text{left}}) f(\lambda_j) \leq 0$; if not accept the full step length $\lambda_1$
4407f296bb3SBarry Smith
4417f296bb3SBarry Smith3. if there is a change of sign, enter iterative bisection procedure
4427f296bb3SBarry Smith
4437f296bb3SBarry Smith   1. check convergence/ exit criteria:
4447f296bb3SBarry Smith
4457f296bb3SBarry Smith      - absolute tolerance $f(\lambda_j) < \mathtt{atol}$
4467f296bb3SBarry Smith      - relative tolerance $f(\lambda_j) < \mathtt{rtol} \cdot f(\lambda_0)$
4477f296bb3SBarry Smith      - change of step length $\lambda_j - \lambda_{j-1} < \mathtt{ltol}$
4487f296bb3SBarry Smith      - number of iterations $j < \mathtt{max\_it}$
4497f296bb3SBarry Smith
4507f296bb3SBarry Smith   2. if $j > 1$, determine direction of bisection
4517f296bb3SBarry Smith
4527f296bb3SBarry Smith   $$
4537f296bb3SBarry Smith   \begin{aligned}\lambda_{\text{left}} &= \begin{cases}\lambda_{\text{left}} &f(\lambda_{\text{left}}) f(\lambda_j) \leq 0\\\lambda_{j} &\text{else}\\ \end{cases}\\ \lambda_{\text{right}} &= \begin{cases} \lambda_j &f(\lambda_{\text{left}}) f(\lambda_j) \leq 0\\\lambda_{\text{right}} &\text{else}\\ \end{cases}\\\end{aligned}
4547f296bb3SBarry Smith   $$
4557f296bb3SBarry Smith
4567f296bb3SBarry Smith   3. bisect the interval: $\lambda_{j+1} = (\lambda_{\text{left}} + \lambda_{\text{right}})/2$, compute $f(\lambda_{j+1})$
4577f296bb3SBarry Smith   4. update variables for the next iteration: $\lambda_j \gets \lambda_{j+1}$, $f(\lambda_j) \gets f(\lambda_{j+1})$, $j \gets j+1$
4587f296bb3SBarry Smith
4597f296bb3SBarry SmithCustom line search types may either be defined using
4607f296bb3SBarry Smith`SNESLineSearchShell`, or by creating a custom user line search type
4617f296bb3SBarry Smithin the model of the preexisting ones and register it using
4627f296bb3SBarry Smith
4637f296bb3SBarry Smith```
4647f296bb3SBarry SmithSNESLineSearchRegister(const char sname[], PetscErrorCode (*function)(SNESLineSearch));.
4657f296bb3SBarry Smith```
4667f296bb3SBarry Smith
4677f296bb3SBarry Smith### Trust Region Methods
4687f296bb3SBarry Smith
4697f296bb3SBarry SmithThe trust region method in `SNES` for solving systems of nonlinear
4707f296bb3SBarry Smithequations, `SNESNEWTONTR` (`-snes_type newtontr`), is similar to the one developed in the
4717f296bb3SBarry SmithMINPACK project {cite}`more84`. Several parameters can be
4727f296bb3SBarry Smithset to control the variation of the trust region size during the
4737f296bb3SBarry Smithsolution process. In particular, the user can control the initial trust
4747f296bb3SBarry Smithregion radius, computed by
4757f296bb3SBarry Smith
4767f296bb3SBarry Smith$$
4777f296bb3SBarry Smith\Delta = \Delta_0 \| F_0 \|_2,
4787f296bb3SBarry Smith$$
4797f296bb3SBarry Smith
4807f296bb3SBarry Smithby setting $\Delta_0$ via the option `-snes_tr_delta0 <delta0>`.
4817f296bb3SBarry Smith
4827f296bb3SBarry Smith### Newton with Arc Length Continuation
4837f296bb3SBarry Smith
4847f296bb3SBarry SmithThe Newton method with arc length continuation reformulates the linearized system
4857f296bb3SBarry Smith$K\delta \mathbf x = -\mathbf F(\mathbf x)$ by introducing the load parameter
4867f296bb3SBarry Smith$\lambda$ and splitting the residual into two components, commonly
4877f296bb3SBarry Smithcorresponding to internal and external forces:
4887f296bb3SBarry Smith
4897f296bb3SBarry Smith$$
4907f296bb3SBarry Smith\mathbf F(x, \lambda) = \mathbf F^{\mathrm{int}}(\mathbf x) - \mathbf F^{\mathrm{ext}}(\mathbf x, \lambda)
4917f296bb3SBarry Smith$$
4927f296bb3SBarry Smith
4937f296bb3SBarry SmithOften, $\mathbf F^{\mathrm{ext}}(\mathbf x, \lambda)$ is linear in $\lambda$,
4947f296bb3SBarry Smithwhich can be thought of as applying the external force in proportional load
4957f296bb3SBarry Smithincrements. By default, this is how the right-hand side vector is handled in the
4967f296bb3SBarry Smithimplemented method. Generally, however, $\mathbf F^{\mathrm{ext}}(\mathbf x, \lambda)$
4977f296bb3SBarry Smithmay depend non-linearly on $\lambda$ or $\mathbf x$, or both.
4987f296bb3SBarry SmithTo accommodate this possibility, we provide the `SNESNewtonALGetLoadParameter()`
4997f296bb3SBarry Smithfunction, which allows for the current value of $\lambda$ to be queried in the
5007f296bb3SBarry Smithfunctions provided to `SNESSetFunction()` and `SNESSetJacobian()`.
5017f296bb3SBarry Smith
5027f296bb3SBarry SmithAdditionally, we split the solution update into two components:
5037f296bb3SBarry Smith
5047f296bb3SBarry Smith$$
5057f296bb3SBarry Smith\delta \mathbf x = \delta s\delta\mathbf x^F + \delta\lambda\delta\mathbf x^Q,
5067f296bb3SBarry Smith$$
5077f296bb3SBarry Smith
5087f296bb3SBarry Smithwhere $\delta s = 1$ unless partial corrections are used (discussed more
5097f296bb3SBarry Smithbelow). Each of $\delta \mathbf x^F$ and $\delta \mathbf x^Q$ are found via
5107f296bb3SBarry Smithsolving a linear system with the Jacobian $K$:
5117f296bb3SBarry Smith
5127f296bb3SBarry Smith- $\delta \mathbf x^F$ is the full Newton step for a given value of $\lambda$: $K \delta \mathbf x^F = -\mathbf F(\mathbf x, \lambda)$
5137f296bb3SBarry Smith- $\delta \mathbf x^Q$ is the variation in $\mathbf x$ with respect to $\lambda$, computed by $K \delta\mathbf x^Q = \mathbf Q(\mathbf x, \lambda)$, where $\mathbf Q(\mathbf x, \lambda) = -\partial \mathbf F (\mathbf x, \lambda) / \partial \lambda$ is the tangent load vector.
5147f296bb3SBarry Smith
5157f296bb3SBarry SmithOften, the tangent load vector $\mathbf Q$ is constant within a load increment,
5167f296bb3SBarry Smithwhich corresponds to the case of proportional loading discussed above. By default,
5177f296bb3SBarry Smith$\mathbf Q$ is the full right-hand-side vector, if one was provided.
5187f296bb3SBarry SmithThe user can also provide a function which computes $\mathbf Q$ to
5197f296bb3SBarry Smith`SNESNewtonALSetFunction()`. This function should have the same signature as for
5207f296bb3SBarry Smith`SNESSetFunction`, and the user should use `SNESNewtonALGetLoadParameter()` to get
5217f296bb3SBarry Smith$\lambda$ if it is needed.
5227f296bb3SBarry Smith
5237f296bb3SBarry Smith**The Constraint Surface.** Considering the $n+1$ dimensional space of
5247f296bb3SBarry Smith$\mathbf x$ and $\lambda$, we define the linearized equilibrium line to be
5257f296bb3SBarry Smiththe set of points for which the linearized equilibrium equations are satisfied.
5267f296bb3SBarry SmithGiven the previous iterative solution
5277f296bb3SBarry Smith$\mathbf t^{(j-1)} = [\mathbf x^{(j-1)}, \lambda^{(j-1)}]$,
5287f296bb3SBarry Smiththis line is defined by the point $\mathbf t^{(j-1)} + [\delta\mathbf x^F, 0]$ and
5297f296bb3SBarry Smiththe vector $\mathbf t^Q [\delta\mathbf x^Q, 1]$.
5307f296bb3SBarry SmithThe arc length method seeks the intersection of this linearized equilibrium line
5317f296bb3SBarry Smithwith a quadratic constraint surface, defined by
5327f296bb3SBarry Smith
5337f296bb3SBarry Smith% math::L^2 = \|\Delta x\|^2 + \psi^2 (\Delta\lambda)^2,
5347f296bb3SBarry Smith
5357f296bb3SBarry Smithwhere $L$ is a user-provided step size corresponding to the radius of the
5367f296bb3SBarry Smithconstraint surface, $\Delta\mathbf x$ and $\Delta\lambda$ are the
5377f296bb3SBarry Smithaccumulated updates over the current load step, and $\psi^2$ is a
5387f296bb3SBarry Smithuser-provided consistency parameter determining the shape of the constraint surface.
5397f296bb3SBarry SmithGenerally, $\psi^2 > 0$ leads to a hyper-sphere constraint surface, while
5407f296bb3SBarry Smith$\psi^2 = 0$ leads to a hyper-cylinder constraint surface.
5417f296bb3SBarry Smith
5427f296bb3SBarry SmithSince the solution will always fall on the constraint surface, the method will often
5437f296bb3SBarry Smithrequire multiple incremental steps to fully solve the non-linear problem.
5447f296bb3SBarry SmithThis is necessary to accurately trace the equilibrium path.
5457f296bb3SBarry SmithImportantly, this is fundamentally different from time stepping.
5467f296bb3SBarry SmithWhile a similar process could be implemented as a `TS`, this method is
5477f296bb3SBarry Smithparticularly designed to be used as a SNES, either standalone or within a `TS`.
5487f296bb3SBarry Smith
5497f296bb3SBarry SmithTo this end, by default, the load parameter is used such that the full external
5507f296bb3SBarry Smithforces are applied at $\lambda = 1$, although we allow for the user to specify
5517f296bb3SBarry Smitha different value via `-snes_newtonal_lambda_max`.
5527f296bb3SBarry SmithTo ensure that the solution corresponds exactly to the external force prescribed by
5537f296bb3SBarry Smiththe user, i.e. that the load parameter is exactly $\lambda_{max}$ at the end
5547f296bb3SBarry Smithof the SNES solve, we clamp the value before computing the solution update.
5557f296bb3SBarry SmithAs such, the final increment will likely be a hybrid of arc length continuation and
5567f296bb3SBarry Smithnormal Newton iterations.
5577f296bb3SBarry Smith
5587f296bb3SBarry Smith**Choosing the Continuation Step.** For the first iteration from an equilibrium
5597f296bb3SBarry Smithpoint, there is a single correct way to choose $\delta\lambda$, which follows
5607f296bb3SBarry Smithfrom the constraint equations. Specifically the constraint equations yield the
5617f296bb3SBarry Smithquadratic equation $a\delta\lambda^2 + b\delta\lambda + c = 0$, where
5627f296bb3SBarry Smith
5637f296bb3SBarry Smith$$
5647f296bb3SBarry Smith\begin{aligned}
5657f296bb3SBarry Smitha &= \|\delta\mathbf x^Q\|^2 + \psi^2,\\
5667f296bb3SBarry Smithb &= 2\delta\mathbf x^Q\cdot (\Delta\mathbf x + \delta s\delta\mathbf x^F) + 2\psi^2 \Delta\lambda,\\
5677f296bb3SBarry Smithc &= \|\Delta\mathbf x + \delta s\delta\mathbf x^F\|^2 + \psi^2 \Delta\lambda^2 - L^2.
5687f296bb3SBarry Smith\end{aligned}
5697f296bb3SBarry Smith$$
5707f296bb3SBarry Smith
5717f296bb3SBarry SmithSince in the first iteration, $\Delta\mathbf x = \delta\mathbf x^F = \mathbf 0$ and
5727f296bb3SBarry Smith$\Delta\lambda = 0$, $b = 0$ and the equation simplifies to a pair of
5737f296bb3SBarry Smithreal roots:
5747f296bb3SBarry Smith
5757f296bb3SBarry Smith$$
5767f296bb3SBarry Smith\delta\lambda = \pm\frac{L}{\sqrt{\|\delta\mathbf x^Q\|^2 + \psi^2}},
5777f296bb3SBarry Smith$$
5787f296bb3SBarry Smith
5797f296bb3SBarry Smithwhere the sign is positive for the first increment and is determined by the previous
5807f296bb3SBarry Smithincrement otherwise as
5817f296bb3SBarry Smith
5827f296bb3SBarry Smith$$
5837f296bb3SBarry Smith\text{sign}(\delta\lambda) = \text{sign}\big(\delta\mathbf x^Q \cdot (\Delta\mathbf x)_{i-1} + \psi^2(\Delta\lambda)_{i-1}\big),
5847f296bb3SBarry Smith$$
5857f296bb3SBarry Smith
5867f296bb3SBarry Smithwhere $(\Delta\mathbf x)_{i-1}$ and $(\Delta\lambda)_{i-1}$ are the
5877f296bb3SBarry Smithaccumulated updates over the previous load step.
5887f296bb3SBarry Smith
5897f296bb3SBarry SmithIn subsequent iterations, there are different approaches to selecting
5907f296bb3SBarry Smith$\delta\lambda$, all of which have trade-offs.
5917f296bb3SBarry SmithThe main difference is whether the iterative solution falls on the constraint
5927f296bb3SBarry Smithsurface at every iteration, or only when fully converged.
59310999371SStefano ZampiniPETSc implements two approaches, set via
5947f296bb3SBarry Smith`SNESNewtonALSetCorrectionType()` or
5957f296bb3SBarry Smith`-snes_newtonal_correction_type <normal|exact>` on the command line.
5967f296bb3SBarry Smith
5977f296bb3SBarry Smith**Corrections in the Normal Hyperplane.** The `SNES_NEWTONAL_CORRECTION_NORMAL`
5987f296bb3SBarry Smithoption is simpler and computationally less expensive, but may fail to converge, as
5997f296bb3SBarry Smiththe constraint equation is not satisfied at every iteration.
6007f296bb3SBarry SmithThe update $\delta \lambda$ is chosen such that the update is within the
6017f296bb3SBarry Smithnormal hyper-surface to the quadratic constraint surface.
6027f296bb3SBarry SmithMathematically, that is
6037f296bb3SBarry Smith
6047f296bb3SBarry Smith$$
6057f296bb3SBarry Smith\delta \lambda = -\frac{\Delta \mathbf x \cdot \delta \mathbf x^F}{\Delta\mathbf x \cdot \delta\mathbf x^Q + \psi^2 \Delta\lambda}.
6067f296bb3SBarry Smith$$
6077f296bb3SBarry Smith
6087f296bb3SBarry SmithThis implementation is based on {cite}`LeonPaulinoPereiraMenezesLages_2011`.
6097f296bb3SBarry Smith
6107f296bb3SBarry Smith**Exact Corrections.** The `SNES_NEWTONAL_CORRECTION_EXACT` option is far more
6117f296bb3SBarry Smithcomplex, but ensures that the constraint is exactly satisfied at every Newton
6127f296bb3SBarry Smithiteration. As such, it is generally more robust.
6137f296bb3SBarry SmithBy evaluating the intersection of constraint surface and equilibrium line at each
6147f296bb3SBarry Smithiteration, $\delta\lambda$ is chosen as one of the roots of the above
6157f296bb3SBarry Smithquadratic equation $a\delta\lambda^2 + b\delta\lambda + c = 0$.
6167f296bb3SBarry SmithThis method encounters issues, however, if the linearized equilibrium line and
6177f296bb3SBarry Smithconstraint surface do not intersect due to particularly large linearized error.
6187f296bb3SBarry SmithIn this case, the roots are complex.
6197f296bb3SBarry SmithTo continue progressing toward a solution, this method uses a partial correction by
6207f296bb3SBarry Smithchoosing $\delta s$ such that the quadratic equation has a single real root.
6217f296bb3SBarry SmithGeometrically, this is selecting the point on the constraint surface closest to the
6227f296bb3SBarry Smithlinearized equilibrium line. See the code or {cite}`Ritto-CorreaCamotim2008` for a
6237f296bb3SBarry Smithmathematical description of these partial corrections.
6247f296bb3SBarry Smith
6257f296bb3SBarry Smith### Nonlinear Krylov Methods
6267f296bb3SBarry Smith
6277f296bb3SBarry SmithA number of nonlinear Krylov methods are provided, including Nonlinear
6287f296bb3SBarry SmithRichardson (`SNESNRICHARDSON`), nonlinear conjugate gradient (`SNESNCG`), nonlinear GMRES (`SNESNGMRES`), and Anderson Mixing (`SNESANDERSON`). These
6297f296bb3SBarry Smithmethods are described individually below. They are all instrumental to
6307f296bb3SBarry SmithPETSc’s nonlinear preconditioning.
6317f296bb3SBarry Smith
6327f296bb3SBarry Smith**Nonlinear Richardson.** The nonlinear Richardson iteration, `SNESNRICHARDSON`, merely
6337f296bb3SBarry Smithtakes the form of a line search-damped fixed-point iteration of the form
6347f296bb3SBarry Smith
6357f296bb3SBarry Smith$$
6367f296bb3SBarry Smith\mathbf{x}_{k+1} = \mathbf{x}_k - \lambda \mathbf{F}(\mathbf{x}_k), \;\; k=0,1, \ldots,
6377f296bb3SBarry Smith$$
6387f296bb3SBarry Smith
639a99ef635SJonas Heinzmannwhere the default linesearch is `SNESLINESEARCHSECANT`. This simple solver
6407f296bb3SBarry Smithis mostly useful as a nonlinear smoother, or to provide line search
6417f296bb3SBarry Smithstabilization to an inner method.
6427f296bb3SBarry Smith
6437f296bb3SBarry Smith**Nonlinear Conjugate Gradients.** Nonlinear CG, `SNESNCG`, is equivalent to linear
6447f296bb3SBarry SmithCG, but with the steplength determined by line search
6457f296bb3SBarry Smith(`SNESLINESEARCHCP` by default). Five variants (Fletcher-Reed,
6467f296bb3SBarry SmithHestenes-Steifel, Polak-Ribiere-Polyak, Dai-Yuan, and Conjugate Descent)
6477f296bb3SBarry Smithare implemented in PETSc and may be chosen using
6487f296bb3SBarry Smith
6497f296bb3SBarry Smith```
6507f296bb3SBarry SmithSNESNCGSetType(SNES snes, SNESNCGType btype);
6517f296bb3SBarry Smith```
6527f296bb3SBarry Smith
6537f296bb3SBarry Smith**Anderson Mixing and Nonlinear GMRES Methods.** Nonlinear GMRES (`SNESNGMRES`), and
6547f296bb3SBarry SmithAnderson Mixing (`SNESANDERSON`) methods combine the last $m$ iterates, plus a new
6557f296bb3SBarry Smithfixed-point iteration iterate, into an approximate residual-minimizing new iterate.
6567f296bb3SBarry Smith
6577f296bb3SBarry SmithAll of the above methods have support for using a nonlinear preconditioner to compute the preliminary update step, rather than the default
6587f296bb3SBarry Smithwhich is the nonlinear function's residual, \$ mathbf\{F}(mathbf\{x}\_k)\$. The different update is obtained by solving a nonlinear preconditioner nonlinear problem, which has its own
6597f296bb3SBarry Smith`SNES` object that may be obtained with `SNESGetNPC()`.
6607f296bb3SBarry SmithQuasi-Newton Methods
6617f296bb3SBarry Smith^^^^^^^^^^^^^^^^^^^^
6627f296bb3SBarry Smith
6637f296bb3SBarry SmithQuasi-Newton methods store iterative rank-one updates to the Jacobian
6647f296bb3SBarry Smithinstead of computing the Jacobian directly. Three limited-memory quasi-Newton
6657f296bb3SBarry Smithmethods are provided, L-BFGS, which are described in
6667f296bb3SBarry SmithTable {any}`tab-qndefaults`. These all are encapsulated under
6677f296bb3SBarry Smith`-snes_type qn` and may be changed with `snes_qn_type`. The default
6687f296bb3SBarry Smithis L-BFGS, which provides symmetric updates to an approximate Jacobian.
6697f296bb3SBarry SmithThis iteration is similar to the line search Newton methods.
6707f296bb3SBarry Smith
6717f296bb3SBarry SmithThe quasi-Newton methods support the use of a nonlinear preconditioner that can be obtained with `SNESGetNPC()` and then configured; or that can be configured with
6727f296bb3SBarry Smith`SNES`, `KSP`, and `PC` options using the options database prefix `-npc_`.
6737f296bb3SBarry Smith
6747f296bb3SBarry Smith```{eval-rst}
6757f296bb3SBarry Smith.. list-table:: PETSc quasi-Newton solvers
6767f296bb3SBarry Smith   :name: tab-qndefaults
6777f296bb3SBarry Smith   :header-rows: 1
6787f296bb3SBarry Smith
6797f296bb3SBarry Smith   * - QN Method
6807f296bb3SBarry Smith     - ``SNESQNType``
6817f296bb3SBarry Smith     - Options Name
6827f296bb3SBarry Smith     - Default Line Search
6837f296bb3SBarry Smith   * - L-BFGS
6847f296bb3SBarry Smith     - ``SNES_QN_LBFGS``
6857f296bb3SBarry Smith     - ``lbfgs``
6867f296bb3SBarry Smith     - ``SNESLINESEARCHCP``
6877f296bb3SBarry Smith   * - “Good” Broyden
6887f296bb3SBarry Smith     - ``SNES_QN_BROYDEN``
6897f296bb3SBarry Smith     - ``broyden``
6907f296bb3SBarry Smith     - ``SNESLINESEARCHBASIC`` (or equivalently ``SNESLINESEARCHNONE``
6917f296bb3SBarry Smith   * - “Bad” Broyden
6927f296bb3SBarry Smith     - ``SNES_QN_BADBROYDEN``
6937f296bb3SBarry Smith     - ``badbroyden``
694a99ef635SJonas Heinzmann     - ``SNESLINESEARCHSECANT``
6957f296bb3SBarry Smith```
6967f296bb3SBarry Smith
6977f296bb3SBarry SmithOne may also control the form of the initial Jacobian approximation with
6987f296bb3SBarry Smith
6997f296bb3SBarry Smith```
7007f296bb3SBarry SmithSNESQNSetScaleType(SNES snes, SNESQNScaleType stype);
7017f296bb3SBarry Smith```
7027f296bb3SBarry Smith
7037f296bb3SBarry Smithand the restart type with
7047f296bb3SBarry Smith
7057f296bb3SBarry Smith```
7067f296bb3SBarry SmithSNESQNSetRestartType(SNES snes, SNESQNRestartType rtype);
7077f296bb3SBarry Smith```
7087f296bb3SBarry Smith
7097f296bb3SBarry Smith### The Full Approximation Scheme
7107f296bb3SBarry Smith
7117f296bb3SBarry SmithThe Nonlinear Full Approximation Scheme (FAS) `SNESFAS`, is a nonlinear multigrid method. At
7127f296bb3SBarry Smitheach level, there is a recursive cycle control `SNES` instance, and
7137f296bb3SBarry Smitheither one or two nonlinear solvers that act as smoothers (up and down). Problems
7147f296bb3SBarry Smithset up using the `SNES` `DMDA` interface are automatically
7157f296bb3SBarry Smithcoarsened. FAS, `SNESFAS`, differs slightly from linear multigrid `PCMG`, in that the hierarchy is
7167f296bb3SBarry Smithconstructed recursively. However, much of the interface is a one-to-one
7177f296bb3SBarry Smithmap. We describe the “get” operations here, and it can be assumed that
7187f296bb3SBarry Smitheach has a corresponding “set” operation. For instance, the number of
7197f296bb3SBarry Smithlevels in the hierarchy may be retrieved using
7207f296bb3SBarry Smith
7217f296bb3SBarry Smith```
7227f296bb3SBarry SmithSNESFASGetLevels(SNES snes, PetscInt *levels);
7237f296bb3SBarry Smith```
7247f296bb3SBarry Smith
7257f296bb3SBarry SmithThere are four `SNESFAS` cycle types, `SNES_FAS_MULTIPLICATIVE`,
7267f296bb3SBarry Smith`SNES_FAS_ADDITIVE`, `SNES_FAS_FULL`, and `SNES_FAS_KASKADE`. The
7277f296bb3SBarry Smithtype may be set with
7287f296bb3SBarry Smith
7297f296bb3SBarry Smith```
7307f296bb3SBarry SmithSNESFASSetType(SNES snes, SNESFASType fastype);.
7317f296bb3SBarry Smith```
7327f296bb3SBarry Smith
7337f296bb3SBarry Smithand the cycle type, 1 for V, 2 for W, may be set with
7347f296bb3SBarry Smith
7357f296bb3SBarry Smith```
7367f296bb3SBarry SmithSNESFASSetCycles(SNES snes, PetscInt cycles);.
7377f296bb3SBarry Smith```
7387f296bb3SBarry Smith
7397f296bb3SBarry SmithMuch like the interface to `PCMG` described in {any}`sec_mg`, there are interfaces to recover the
7407f296bb3SBarry Smithvarious levels’ cycles and smoothers. The level smoothers may be
7417f296bb3SBarry Smithaccessed with
7427f296bb3SBarry Smith
7437f296bb3SBarry Smith```
7447f296bb3SBarry SmithSNESFASGetSmoother(SNES snes, PetscInt level, SNES *smooth);
7457f296bb3SBarry SmithSNESFASGetSmootherUp(SNES snes, PetscInt level, SNES *smooth);
7467f296bb3SBarry SmithSNESFASGetSmootherDown(SNES snes, PetscInt level, SNES *smooth);
7477f296bb3SBarry Smith```
7487f296bb3SBarry Smith
7497f296bb3SBarry Smithand the level cycles with
7507f296bb3SBarry Smith
7517f296bb3SBarry Smith```
7527f296bb3SBarry SmithSNESFASGetCycleSNES(SNES snes, PetscInt level, SNES *lsnes);.
7537f296bb3SBarry Smith```
7547f296bb3SBarry Smith
7557f296bb3SBarry SmithAlso akin to `PCMG`, the restriction and prolongation at a level may
7567f296bb3SBarry Smithbe acquired with
7577f296bb3SBarry Smith
7587f296bb3SBarry Smith```
7597f296bb3SBarry SmithSNESFASGetInterpolation(SNES snes, PetscInt level, Mat *mat);
7607f296bb3SBarry SmithSNESFASGetRestriction(SNES snes, PetscInt level, Mat *mat);
7617f296bb3SBarry Smith```
7627f296bb3SBarry Smith
7637f296bb3SBarry SmithIn addition, FAS requires special restriction for solution-like
7647f296bb3SBarry Smithvariables, called injection. This may be set with
7657f296bb3SBarry Smith
7667f296bb3SBarry Smith```
7677f296bb3SBarry SmithSNESFASGetInjection(SNES snes, PetscInt level, Mat *mat);.
7687f296bb3SBarry Smith```
7697f296bb3SBarry Smith
7707f296bb3SBarry SmithThe coarse solve context may be acquired with
7717f296bb3SBarry Smith
7727f296bb3SBarry Smith```
7737f296bb3SBarry SmithSNESFASGetCoarseSolve(SNES snes, SNES *smooth);
7747f296bb3SBarry Smith```
7757f296bb3SBarry Smith
7767f296bb3SBarry Smith### Nonlinear Additive Schwarz
7777f296bb3SBarry Smith
7787f296bb3SBarry SmithNonlinear Additive Schwarz methods (NASM) take a number of local
7797f296bb3SBarry Smithnonlinear subproblems, solves them independently in parallel, and
7807f296bb3SBarry Smithcombines those solutions into a new approximate solution.
7817f296bb3SBarry Smith
7827f296bb3SBarry Smith```
7837f296bb3SBarry SmithSNESNASMSetSubdomains(SNES snes, PetscInt n, SNES subsnes[], VecScatter iscatter[], VecScatter oscatter[], VecScatter gscatter[]);
7847f296bb3SBarry Smith```
7857f296bb3SBarry Smith
7867f296bb3SBarry Smithallows for the user to create these local subdomains. Problems set up
7877f296bb3SBarry Smithusing the `SNES` `DMDA` interface are automatically decomposed. To
7887f296bb3SBarry Smithbegin, the type of subdomain updates to the whole solution are limited
7897f296bb3SBarry Smithto two types borrowed from `PCASM`: `PC_ASM_BASIC`, in which the
7907f296bb3SBarry Smithoverlapping updates added. `PC_ASM_RESTRICT` updates in a
7917f296bb3SBarry Smithnonoverlapping fashion. This may be set with
7927f296bb3SBarry Smith
7937f296bb3SBarry Smith```
7947f296bb3SBarry SmithSNESNASMSetType(SNES snes, PCASMType type);.
7957f296bb3SBarry Smith```
7967f296bb3SBarry Smith
7977f296bb3SBarry Smith`SNESASPIN` is a helper `SNES` type that sets up a nonlinearly
7987f296bb3SBarry Smithpreconditioned Newton’s method using NASM as the preconditioner.
7997f296bb3SBarry Smith
8007f296bb3SBarry Smith## General Options
8017f296bb3SBarry Smith
8027f296bb3SBarry SmithThis section discusses options and routines that apply to all `SNES`
8037f296bb3SBarry Smithsolvers and problem classes. In particular, we focus on convergence
8047f296bb3SBarry Smithtests, monitoring routines, and tools for checking derivative
8057f296bb3SBarry Smithcomputations.
8067f296bb3SBarry Smith
8077f296bb3SBarry Smith(sec_snesconvergence)=
8087f296bb3SBarry Smith
8097f296bb3SBarry Smith### Convergence Tests
8107f296bb3SBarry Smith
8117f296bb3SBarry SmithConvergence of the nonlinear solvers can be detected in a variety of
8127f296bb3SBarry Smithways; the user can even specify a customized test, as discussed below.
8137f296bb3SBarry SmithMost of the nonlinear solvers use `SNESConvergenceTestDefault()`,
8147f296bb3SBarry Smithhowever, `SNESNEWTONTR` uses a method-specific additional convergence
8157f296bb3SBarry Smithtest as well. The convergence tests involves several parameters, which
8167f296bb3SBarry Smithare set by default to values that should be reasonable for a wide range
8177f296bb3SBarry Smithof problems. The user can customize the parameters to the problem at
8187f296bb3SBarry Smithhand by using some of the following routines and options.
8197f296bb3SBarry Smith
8207f296bb3SBarry SmithOne method of convergence testing is to declare convergence when the
8217f296bb3SBarry Smithnorm of the change in the solution between successive iterations is less
8227f296bb3SBarry Smiththan some tolerance, `stol`. Convergence can also be determined based
8237f296bb3SBarry Smithon the norm of the function. Such a test can use either the absolute
8247f296bb3SBarry Smithsize of the norm, `atol`, or its relative decrease, `rtol`, from an
8257f296bb3SBarry Smithinitial guess. The following routine sets these parameters, which are
8267f296bb3SBarry Smithused in many of the default `SNES` convergence tests:
8277f296bb3SBarry Smith
8287f296bb3SBarry Smith```
8297f296bb3SBarry SmithSNESSetTolerances(SNES snes, PetscReal atol, PetscReal rtol, PetscReal stol, PetscInt its, PetscInt fcts);
8307f296bb3SBarry Smith```
8317f296bb3SBarry Smith
8327f296bb3SBarry SmithThis routine also sets the maximum numbers of allowable nonlinear
8337f296bb3SBarry Smithiterations, `its`, and function evaluations, `fcts`. The
8347f296bb3SBarry Smithcorresponding options database commands for setting these parameters are:
8357f296bb3SBarry Smith
8367f296bb3SBarry Smith- `-snes_atol <atol>`
8377f296bb3SBarry Smith- `-snes_rtol <rtol>`
8387f296bb3SBarry Smith- `-snes_stol <stol>`
8397f296bb3SBarry Smith- `-snes_max_it <its>`
8407f296bb3SBarry Smith- `-snes_max_funcs <fcts>` (use `unlimited` for no maximum)
8417f296bb3SBarry Smith
8427f296bb3SBarry SmithA related routine is `SNESGetTolerances()`. `PETSC_CURRENT` may be used
8437f296bb3SBarry Smithfor any parameter to indicate the current value should be retained; use `PETSC_DETERMINE` to restore to the default value from when the object was created.
8447f296bb3SBarry Smith
8457f296bb3SBarry SmithUsers can set their own customized convergence tests in `SNES` by
8467f296bb3SBarry Smithusing the command
8477f296bb3SBarry Smith
8487f296bb3SBarry Smith```
849*2a8381b2SBarry SmithSNESSetConvergenceTest(SNES snes, PetscErrorCode (*test)(SNES snes, PetscInt it, PetscReal xnorm, PetscReal gnorm, PetscReal f, SNESConvergedReason reason, PetscCtx cctx), PetscCtx cctx, PetscCtxDestroyFn *destroy);
8507f296bb3SBarry Smith```
8517f296bb3SBarry Smith
8527f296bb3SBarry SmithThe final argument of the convergence test routine, `cctx`, denotes an
8537f296bb3SBarry Smithoptional user-defined context for private data. When solving systems of
8547f296bb3SBarry Smithnonlinear equations, the arguments `xnorm`, `gnorm`, and `f` are
8557f296bb3SBarry Smiththe current iterate norm, current step norm, and function norm,
8567f296bb3SBarry Smithrespectively. `SNESConvergedReason` should be set positive for
8577f296bb3SBarry Smithconvergence and negative for divergence. See `include/petscsnes.h` for
8587f296bb3SBarry Smitha list of values for `SNESConvergedReason`.
8597f296bb3SBarry Smith
8607f296bb3SBarry Smith(sec_snesmonitor)=
8617f296bb3SBarry Smith
8627f296bb3SBarry Smith### Convergence Monitoring
8637f296bb3SBarry Smith
8647f296bb3SBarry SmithBy default the `SNES` solvers run silently without displaying
8657f296bb3SBarry Smithinformation about the iterations. The user can initiate monitoring with
8667f296bb3SBarry Smiththe command
8677f296bb3SBarry Smith
8687f296bb3SBarry Smith```
869*2a8381b2SBarry SmithSNESMonitorSet(SNES snes, PetscErrorCode (*mon)(SNES snes, PetscInt its, PetscReal norm, PetscCtx mctx), PetscCtx mctx, (PetscCtxDestroyFn *)*monitordestroy);
8707f296bb3SBarry Smith```
8717f296bb3SBarry Smith
8727f296bb3SBarry SmithThe routine, `mon`, indicates a user-defined monitoring routine, where
8737f296bb3SBarry Smith`its` and `mctx` respectively denote the iteration number and an
8747f296bb3SBarry Smithoptional user-defined context for private data for the monitor routine.
8757f296bb3SBarry SmithThe argument `norm` is the function norm.
8767f296bb3SBarry Smith
8777f296bb3SBarry SmithThe routine set by `SNESMonitorSet()` is called once after every
8787f296bb3SBarry Smithsuccessful step computation within the nonlinear solver. Hence, the user
8797f296bb3SBarry Smithcan employ this routine for any application-specific computations that
8807f296bb3SBarry Smithshould be done after the solution update. The option `-snes_monitor`
8817f296bb3SBarry Smithactivates the default `SNES` monitor routine,
8827f296bb3SBarry Smith`SNESMonitorDefault()`, while `-snes_monitor_lg_residualnorm` draws
8837f296bb3SBarry Smitha simple line graph of the residual norm’s convergence.
8847f296bb3SBarry Smith
8857f296bb3SBarry SmithOne can cancel hardwired monitoring routines for `SNES` at runtime
8867f296bb3SBarry Smithwith `-snes_monitor_cancel`.
8877f296bb3SBarry Smith
8887f296bb3SBarry SmithAs the Newton method converges so that the residual norm is small, say
8897f296bb3SBarry Smith$10^{-10}$, many of the final digits printed with the
8907f296bb3SBarry Smith`-snes_monitor` option are meaningless. Worse, they are different on
8917f296bb3SBarry Smithdifferent machines; due to different round-off rules used by, say, the
8927f296bb3SBarry SmithIBM RS6000 and the Sun SPARC. This makes testing between different
8937f296bb3SBarry Smithmachines difficult. The option `-snes_monitor_short` causes PETSc to
8947f296bb3SBarry Smithprint fewer of the digits of the residual norm as it gets smaller; thus
8957f296bb3SBarry Smithon most of the machines it will always print the same numbers making
8967f296bb3SBarry Smithcross-process testing easier.
8977f296bb3SBarry Smith
8987f296bb3SBarry SmithThe routines
8997f296bb3SBarry Smith
9007f296bb3SBarry Smith```
9017f296bb3SBarry SmithSNESGetSolution(SNES snes, Vec *x);
902*2a8381b2SBarry SmithSNESGetFunction(SNES snes, Vec *r, PetscCtxRt ctx, int(**func)(SNES, Vec, Vec, PetscCtx));
9037f296bb3SBarry Smith```
9047f296bb3SBarry Smith
9057f296bb3SBarry Smithreturn the solution vector and function vector from a `SNES` context.
9067f296bb3SBarry SmithThese routines are useful, for instance, if the convergence test
9077f296bb3SBarry Smithrequires some property of the solution or function other than those
9087f296bb3SBarry Smithpassed with routine arguments.
9097f296bb3SBarry Smith
9107f296bb3SBarry Smith(sec_snesderivs)=
9117f296bb3SBarry Smith
9127f296bb3SBarry Smith### Checking Accuracy of Derivatives
9137f296bb3SBarry Smith
9147f296bb3SBarry SmithSince hand-coding routines for Jacobian matrix evaluation can be error
9157f296bb3SBarry Smithprone, `SNES` provides easy-to-use support for checking these matrices
9167f296bb3SBarry Smithagainst finite difference versions. In the simplest form of comparison,
9177f296bb3SBarry Smithusers can employ the option `-snes_test_jacobian` to compare the
9187f296bb3SBarry Smithmatrices at several points. Although not exhaustive, this test will
9197f296bb3SBarry Smithgenerally catch obvious problems. One can compare the elements of the
9207f296bb3SBarry Smithtwo matrices by using the option `-snes_test_jacobian_view` , which
9217f296bb3SBarry Smithcauses the two matrices to be printed to the screen.
9227f296bb3SBarry Smith
9237f296bb3SBarry SmithAnother means for verifying the correctness of a code for Jacobian
9247f296bb3SBarry Smithcomputation is running the problem with either the finite difference or
9257f296bb3SBarry Smithmatrix-free variant, `-snes_fd` or `-snes_mf`; see {any}`sec_fdmatrix` or {any}`sec_nlmatrixfree`.
9267f296bb3SBarry SmithIf a
9277f296bb3SBarry Smithproblem converges well with these matrix approximations but not with a
9287f296bb3SBarry Smithuser-provided routine, the problem probably lies with the hand-coded
9297f296bb3SBarry Smithmatrix. See the note in {any}`sec_snesjacobian` about
9307f296bb3SBarry Smithassembling your Jabobian in the "preconditioner" slot `Pmat`.
9317f296bb3SBarry Smith
9327f296bb3SBarry SmithThe correctness of user provided `MATSHELL` Jacobians in general can be
9337f296bb3SBarry Smithchecked with `MatShellTestMultTranspose()` and `MatShellTestMult()`.
9347f296bb3SBarry Smith
9357f296bb3SBarry SmithThe correctness of user provided `MATSHELL` Jacobians via `TSSetRHSJacobian()`
9367f296bb3SBarry Smithcan be checked with `TSRHSJacobianTestTranspose()` and `TSRHSJacobianTest()`
9377f296bb3SBarry Smiththat check the correction of the matrix-transpose vector product and the
9387f296bb3SBarry Smithmatrix-product. From the command line, these can be checked with
9397f296bb3SBarry Smith
9407f296bb3SBarry Smith- `-ts_rhs_jacobian_test_mult_transpose`
9417f296bb3SBarry Smith- `-mat_shell_test_mult_transpose_view`
9427f296bb3SBarry Smith- `-ts_rhs_jacobian_test_mult`
9437f296bb3SBarry Smith- `-mat_shell_test_mult_view`
9447f296bb3SBarry Smith
9457f296bb3SBarry Smith## Inexact Newton-like Methods
9467f296bb3SBarry Smith
9477f296bb3SBarry SmithSince exact solution of the linear Newton systems within {math:numref}`newton`
9487f296bb3SBarry Smithat each iteration can be costly, modifications
9497f296bb3SBarry Smithare often introduced that significantly reduce these expenses and yet
9507f296bb3SBarry Smithretain the rapid convergence of Newton’s method. Inexact or truncated
9517f296bb3SBarry SmithNewton techniques approximately solve the linear systems using an
9527f296bb3SBarry Smithiterative scheme. In comparison with using direct methods for solving
9537f296bb3SBarry Smiththe Newton systems, iterative methods have the virtue of requiring
9547f296bb3SBarry Smithlittle space for matrix storage and potentially saving significant
9557f296bb3SBarry Smithcomputational work. Within the class of inexact Newton methods, of
9567f296bb3SBarry Smithparticular interest are Newton-Krylov methods, where the subsidiary
9577f296bb3SBarry Smithiterative technique for solving the Newton system is chosen from the
9587f296bb3SBarry Smithclass of Krylov subspace projection methods. Note that at runtime the
9597f296bb3SBarry Smithuser can set any of the linear solver options discussed in {any}`ch_ksp`,
9607f296bb3SBarry Smithsuch as `-ksp_type <ksp_method>` and
9617f296bb3SBarry Smith`-pc_type <pc_method>`, to set the Krylov subspace and preconditioner
9627f296bb3SBarry Smithmethods.
9637f296bb3SBarry Smith
9647f296bb3SBarry SmithTwo levels of iterations occur for the inexact techniques, where during
9657f296bb3SBarry Smitheach global or outer Newton iteration a sequence of subsidiary inner
9667f296bb3SBarry Smithiterations of a linear solver is performed. Appropriate control of the
9677f296bb3SBarry Smithaccuracy to which the subsidiary iterative method solves the Newton
9687f296bb3SBarry Smithsystem at each global iteration is critical, since these inner
9697f296bb3SBarry Smithiterations determine the asymptotic convergence rate for inexact Newton
9707f296bb3SBarry Smithtechniques. While the Newton systems must be solved well enough to
9717f296bb3SBarry Smithretain fast local convergence of the Newton’s iterates, use of excessive
9727f296bb3SBarry Smithinner iterations, particularly when $\| \mathbf{x}_k - \mathbf{x}_* \|$ is large,
9737f296bb3SBarry Smithis neither necessary nor economical. Thus, the number of required inner
9747f296bb3SBarry Smithiterations typically increases as the Newton process progresses, so that
9757f296bb3SBarry Smiththe truncated iterates approach the true Newton iterates.
9767f296bb3SBarry Smith
9777f296bb3SBarry SmithA sequence of nonnegative numbers $\{\eta_k\}$ can be used to
9787f296bb3SBarry Smithindicate the variable convergence criterion. In this case, when solving
9797f296bb3SBarry Smitha system of nonlinear equations, the update step of the Newton process
9807f296bb3SBarry Smithremains unchanged, and direct solution of the linear system is replaced
9817f296bb3SBarry Smithby iteration on the system until the residuals
9827f296bb3SBarry Smith
9837f296bb3SBarry Smith$$
9847f296bb3SBarry Smith\mathbf{r}_k^{(i)} =  \mathbf{F}'(\mathbf{x}_k) \Delta \mathbf{x}_k + \mathbf{F}(\mathbf{x}_k)
9857f296bb3SBarry Smith$$
9867f296bb3SBarry Smith
9877f296bb3SBarry Smithsatisfy
9887f296bb3SBarry Smith
9897f296bb3SBarry Smith$$
9907f296bb3SBarry Smith\frac{ \| \mathbf{r}_k^{(i)} \| }{ \| \mathbf{F}(\mathbf{x}_k) \| } \leq \eta_k \leq \eta < 1.
9917f296bb3SBarry Smith$$
9927f296bb3SBarry Smith
9937f296bb3SBarry SmithHere $\mathbf{x}_0$ is an initial approximation of the solution, and
9947f296bb3SBarry Smith$\| \cdot \|$ denotes an arbitrary norm in $\Re^n$ .
9957f296bb3SBarry Smith
9967f296bb3SBarry SmithBy default a constant relative convergence tolerance is used for solving
9977f296bb3SBarry Smiththe subsidiary linear systems within the Newton-like methods of
9987f296bb3SBarry Smith`SNES`. When solving a system of nonlinear equations, one can instead
9997f296bb3SBarry Smithemploy the techniques of Eisenstat and Walker {cite}`ew96`
10007f296bb3SBarry Smithto compute $\eta_k$ at each step of the nonlinear solver by using
10017f296bb3SBarry Smiththe option `-snes_ksp_ew` . In addition, by adding one’s own
10027f296bb3SBarry Smith`KSP` convergence test (see {any}`sec_convergencetests`), one can easily create one’s own,
10037f296bb3SBarry Smithproblem-dependent, inner convergence tests.
10047f296bb3SBarry Smith
10057f296bb3SBarry Smith(sec_nlmatrixfree)=
10067f296bb3SBarry Smith
10077f296bb3SBarry Smith## Matrix-Free Methods
10087f296bb3SBarry Smith
10097f296bb3SBarry SmithThe `SNES` class fully supports matrix-free methods. The matrices
10107f296bb3SBarry Smithspecified in the Jacobian evaluation routine need not be conventional
10117f296bb3SBarry Smithmatrices; instead, they can point to the data required to implement a
10127f296bb3SBarry Smithparticular matrix-free method. The matrix-free variant is allowed *only*
10137f296bb3SBarry Smithwhen the linear systems are solved by an iterative method in combination
10147f296bb3SBarry Smithwith no preconditioning (`PCNONE` or `-pc_type` `none`), a
10157addb90fSBarry Smithuser-provided matrix from which to construct the preconditioner, or a user-provided preconditioner
10167f296bb3SBarry Smithshell (`PCSHELL`, discussed in {any}`sec_pc`); that
10177f296bb3SBarry Smithis, obviously matrix-free methods cannot be used with a direct solver,
10187f296bb3SBarry Smithapproximate factorization, or other preconditioner which requires access
10197f296bb3SBarry Smithto explicit matrix entries.
10207f296bb3SBarry Smith
10217f296bb3SBarry SmithThe user can create a matrix-free context for use within `SNES` with
10227f296bb3SBarry Smiththe routine
10237f296bb3SBarry Smith
10247f296bb3SBarry Smith```
10257f296bb3SBarry SmithMatCreateSNESMF(SNES snes, Mat *mat);
10267f296bb3SBarry Smith```
10277f296bb3SBarry Smith
10287f296bb3SBarry SmithThis routine creates the data structures needed for the matrix-vector
10297f296bb3SBarry Smithproducts that arise within Krylov space iterative
10307f296bb3SBarry Smithmethods {cite}`brownsaad:90`.
10317f296bb3SBarry SmithThe default `SNES`
10327f296bb3SBarry Smithmatrix-free approximations can also be invoked with the command
10337f296bb3SBarry Smith`-snes_mf`. Or, one can retain the user-provided Jacobian
10347f296bb3SBarry Smithpreconditioner, but replace the user-provided Jacobian matrix with the
10357f296bb3SBarry Smithdefault matrix-free variant with the option `-snes_mf_operator`.
10367f296bb3SBarry Smith
10377f296bb3SBarry Smith`MatCreateSNESMF()` uses
10387f296bb3SBarry Smith
10397f296bb3SBarry Smith```
10407f296bb3SBarry SmithMatCreateMFFD(Vec x, Mat *mat);
10417f296bb3SBarry Smith```
10427f296bb3SBarry Smith
10437f296bb3SBarry Smithwhich can also be used directly for users who need a matrix-free matrix but are not using `SNES`.
10447f296bb3SBarry Smith
10457f296bb3SBarry SmithThe user can set one parameter to control the Jacobian-vector product
10467f296bb3SBarry Smithapproximation with the command
10477f296bb3SBarry Smith
10487f296bb3SBarry Smith```
10497f296bb3SBarry SmithMatMFFDSetFunctionError(Mat mat, PetscReal rerror);
10507f296bb3SBarry Smith```
10517f296bb3SBarry Smith
10527f296bb3SBarry SmithThe parameter `rerror` should be set to the square root of the
10537f296bb3SBarry Smithrelative error in the function evaluations, $e_{rel}$; the default
10547f296bb3SBarry Smithis the square root of machine epsilon (about $10^{-8}$ in double
10557f296bb3SBarry Smithprecision), which assumes that the functions are evaluated to full
10567f296bb3SBarry Smithfloating-point precision accuracy. This parameter can also be set from
10577f296bb3SBarry Smiththe options database with `-mat_mffd_err <err>`
10587f296bb3SBarry Smith
10597f296bb3SBarry SmithIn addition, PETSc provides ways to register new routines to compute
10607f296bb3SBarry Smiththe differencing parameter ($h$); see the manual page for
10617f296bb3SBarry Smith`MatMFFDSetType()` and `MatMFFDRegister()`. We currently provide two
10627f296bb3SBarry Smithdefault routines accessible via `-mat_mffd_type <ds or wp>`. For
10637f296bb3SBarry Smiththe default approach there is one “tuning” parameter, set with
10647f296bb3SBarry Smith
10657f296bb3SBarry Smith```
10667f296bb3SBarry SmithMatMFFDDSSetUmin(Mat mat, PetscReal umin);
10677f296bb3SBarry Smith```
10687f296bb3SBarry Smith
10697f296bb3SBarry SmithThis parameter, `umin` (or $u_{min}$), is a bit involved; its
10707f296bb3SBarry Smithdefault is $10^{-6}$ . Its command line form is `-mat_mffd_umin <umin>`.
10717f296bb3SBarry Smith
10727f296bb3SBarry SmithThe Jacobian-vector product is approximated
10737f296bb3SBarry Smithvia the formula
10747f296bb3SBarry Smith
10757f296bb3SBarry Smith$$
10767f296bb3SBarry SmithF'(u) a \approx \frac{F(u + h*a) - F(u)}{h}
10777f296bb3SBarry Smith$$
10787f296bb3SBarry Smith
10797f296bb3SBarry Smithwhere $h$ is computed via
10807f296bb3SBarry Smith
10817f296bb3SBarry Smith$$
10827f296bb3SBarry Smithh = e_{\text{rel}} \cdot \begin{cases}
10837f296bb3SBarry Smithu^{T}a/\lVert a \rVert^2_2                                 & \text{if $|u^T a| > u_{\min} \lVert a \rVert_{1}$} \\
10847f296bb3SBarry Smithu_{\min} \operatorname{sign}(u^{T}a) \lVert a \rVert_{1}/\lVert a\rVert^2_2  & \text{otherwise}.
10857f296bb3SBarry Smith\end{cases}
10867f296bb3SBarry Smith$$
10877f296bb3SBarry Smith
10887f296bb3SBarry SmithThis approach is taken from Brown and Saad
10897f296bb3SBarry Smith{cite}`brownsaad:90`. The second approach, taken from Walker and Pernice,
10907f296bb3SBarry Smith{cite}`pw98`, computes $h$ via
10917f296bb3SBarry Smith
10927f296bb3SBarry Smith$$
10937f296bb3SBarry Smith\begin{aligned}
10947f296bb3SBarry Smith        h = \frac{\sqrt{1 + ||u||}e_{rel}}{||a||}\end{aligned}
10957f296bb3SBarry Smith$$
10967f296bb3SBarry Smith
10977f296bb3SBarry SmithThis has no tunable parameters, but note that inside the nonlinear solve
10987f296bb3SBarry Smithfor the entire *linear* iterative process $u$ does not change
10997f296bb3SBarry Smithhence $\sqrt{1 + ||u||}$ need be computed only once. This
11007f296bb3SBarry Smithinformation may be set with the options
11017f296bb3SBarry Smith
11027f296bb3SBarry Smith```
11034558fef0SBarry SmithMatMFFDWPSetComputeNormU(Mat, PetscBool);
11047f296bb3SBarry Smith```
11057f296bb3SBarry Smith
11067f296bb3SBarry Smithor `-mat_mffd_compute_normu <true or false>`. This information is used
11077f296bb3SBarry Smithto eliminate the redundant computation of these parameters, therefore
11087f296bb3SBarry Smithreducing the number of collective operations and improving the
11097f296bb3SBarry Smithefficiency of the application code. This takes place automatically for the PETSc GMRES solver with left preconditioning.
11107f296bb3SBarry Smith
11117f296bb3SBarry SmithIt is also possible to monitor the differencing parameters h that are
11127f296bb3SBarry Smithcomputed via the routines
11137f296bb3SBarry Smith
11147f296bb3SBarry Smith```
11157f296bb3SBarry SmithMatMFFDSetHHistory(Mat, PetscScalar *, int);
11167f296bb3SBarry SmithMatMFFDResetHHistory(Mat, PetscScalar *, int);
11177f296bb3SBarry SmithMatMFFDGetH(Mat, PetscScalar *);
11187f296bb3SBarry Smith```
11197f296bb3SBarry Smith
11207f296bb3SBarry SmithWe include an explicit example of using matrix-free methods in {any}`ex3.c <snes_ex3>`.
11217f296bb3SBarry SmithNote that by using the option `-snes_mf` one can
11227f296bb3SBarry Smitheasily convert any `SNES` code to use a matrix-free Newton-Krylov
11237f296bb3SBarry Smithmethod without a preconditioner. As shown in this example,
11247f296bb3SBarry Smith`SNESSetFromOptions()` must be called *after* `SNESSetJacobian()` to
11257f296bb3SBarry Smithenable runtime switching between the user-specified Jacobian and the
11267f296bb3SBarry Smithdefault `SNES` matrix-free form.
11277f296bb3SBarry Smith
11287f296bb3SBarry Smith(snes_ex3)=
11297f296bb3SBarry Smith
11307f296bb3SBarry Smith:::{admonition} Listing: `src/snes/tutorials/ex3.c`
11317f296bb3SBarry Smith```{literalinclude} /../src/snes/tutorials/ex3.c
11327f296bb3SBarry Smith:end-before: /*TEST
11337f296bb3SBarry Smith```
11347f296bb3SBarry Smith:::
11357f296bb3SBarry Smith
11367f296bb3SBarry SmithTable {any}`tab-jacobians` summarizes the various matrix situations
11377f296bb3SBarry Smiththat `SNES` supports. In particular, different linear system matrices
11387f296bb3SBarry Smithand preconditioning matrices are allowed, as well as both matrix-free
11397f296bb3SBarry Smithand application-provided preconditioners. If {any}`ex3.c <snes_ex3>` is run with
11407f296bb3SBarry Smiththe options `-snes_mf` and `-user_precond` then it uses a
11417f296bb3SBarry Smithmatrix-free application of the matrix-vector multiple and a user
11427f296bb3SBarry Smithprovided matrix-free Jacobian.
11437f296bb3SBarry Smith
11447f296bb3SBarry Smith```{eval-rst}
11457f296bb3SBarry Smith.. list-table:: Jacobian Options
11467f296bb3SBarry Smith   :name: tab-jacobians
11477f296bb3SBarry Smith
11487f296bb3SBarry Smith   * - Matrix Use
11497f296bb3SBarry Smith     - Conventional Matrix Formats
11507f296bb3SBarry Smith     - Matrix-free versions
11517f296bb3SBarry Smith   * - Jacobian Matrix
11527f296bb3SBarry Smith     - Create matrix with ``MatCreate()``:math:`^*`.  Assemble matrix with user-defined routine :math:`^\dagger`
11537f296bb3SBarry Smith     - Create matrix with ``MatCreateShell()``.  Use ``MatShellSetOperation()`` to set various matrix actions, or use ``MatCreateMFFD()`` or ``MatCreateSNESMF()``.
11547addb90fSBarry Smith   * - Matrix used to construct the preconditioner
11557f296bb3SBarry Smith     - Create matrix with ``MatCreate()``:math:`^*`.  Assemble matrix with user-defined routine :math:`^\dagger`
11567f296bb3SBarry Smith     - Use ``SNESGetKSP()`` and ``KSPGetPC()`` to access the ``PC``, then use ``PCSetType(pc, PCSHELL)`` followed by ``PCShellSetApply()``.
11577f296bb3SBarry Smith```
11587f296bb3SBarry Smith
11597f296bb3SBarry Smith$^*$ Use either the generic `MatCreate()` or a format-specific variant such as `MatCreateAIJ()`.
11607f296bb3SBarry Smith
11617f296bb3SBarry Smith$^\dagger$ Set user-defined matrix formation routine with `SNESSetJacobian()` or with a `DM` variant such as `DMDASNESSetJacobianLocal()`
11627f296bb3SBarry Smith
11637f296bb3SBarry SmithSNES also provides some less well-integrated code to apply matrix-free finite differencing using an automatically computed measurement of the
11647f296bb3SBarry Smithnoise of the functions. This can be selected with `-snes_mf_version 2`; it does not use `MatCreateMFFD()` but has similar options that start with
11657f296bb3SBarry Smith`-snes_mf_` instead of `-mat_mffd_`. Note that this alternative prefix **only** works for version 2 differencing.
11667f296bb3SBarry Smith
11677f296bb3SBarry Smith(sec_fdmatrix)=
11687f296bb3SBarry Smith
11697f296bb3SBarry Smith## Finite Difference Jacobian Approximations
11707f296bb3SBarry Smith
11717f296bb3SBarry SmithPETSc provides some tools to help approximate the Jacobian matrices
11727f296bb3SBarry Smithefficiently via finite differences. These tools are intended for use in
11737f296bb3SBarry Smithcertain situations where one is unable to compute Jacobian matrices
11747f296bb3SBarry Smithanalytically, and matrix-free methods do not work well without a
11757f296bb3SBarry Smithpreconditioner, due to very poor conditioning. The approximation
11767f296bb3SBarry Smithrequires several steps:
11777f296bb3SBarry Smith
11787f296bb3SBarry Smith- First, one colors the columns of the (not yet built) Jacobian matrix,
11797f296bb3SBarry Smith  so that columns of the same color do not share any common rows.
11807f296bb3SBarry Smith- Next, one creates a `MatFDColoring` data structure that will be
11817f296bb3SBarry Smith  used later in actually computing the Jacobian.
11827f296bb3SBarry Smith- Finally, one tells the nonlinear solvers of `SNES` to use the
11837f296bb3SBarry Smith  `SNESComputeJacobianDefaultColor()` routine to compute the
11847f296bb3SBarry Smith  Jacobians.
11857f296bb3SBarry Smith
11867f296bb3SBarry SmithA code fragment that demonstrates this process is given below.
11877f296bb3SBarry Smith
11887f296bb3SBarry Smith```
11897f296bb3SBarry SmithISColoring    iscoloring;
11907f296bb3SBarry SmithMatFDColoring fdcoloring;
11917f296bb3SBarry SmithMatColoring   coloring;
11927f296bb3SBarry Smith
11937f296bb3SBarry Smith/*
11947f296bb3SBarry Smith  This initializes the nonzero structure of the Jacobian. This is artificial
11957f296bb3SBarry Smith  because clearly if we had a routine to compute the Jacobian we wouldn't
11967f296bb3SBarry Smith  need to use finite differences.
11977f296bb3SBarry Smith*/
11987f296bb3SBarry SmithFormJacobian(snes, x, &J, &J, &user);
11997f296bb3SBarry Smith
12007f296bb3SBarry Smith/*
12017f296bb3SBarry Smith   Color the matrix, i.e. determine groups of columns that share no common
12027f296bb3SBarry Smith  rows. These columns in the Jacobian can all be computed simultaneously.
12037f296bb3SBarry Smith*/
12047f296bb3SBarry SmithMatColoringCreate(J, &coloring);
12057f296bb3SBarry SmithMatColoringSetType(coloring, MATCOLORINGSL);
12067f296bb3SBarry SmithMatColoringSetFromOptions(coloring);
12077f296bb3SBarry SmithMatColoringApply(coloring, &iscoloring);
12087f296bb3SBarry SmithMatColoringDestroy(&coloring);
12097f296bb3SBarry Smith/*
12107f296bb3SBarry Smith   Create the data structure that SNESComputeJacobianDefaultColor() uses
12117f296bb3SBarry Smith   to compute the actual Jacobians via finite differences.
12127f296bb3SBarry Smith*/
12137f296bb3SBarry SmithMatFDColoringCreate(J, iscoloring, &fdcoloring);
12147f296bb3SBarry SmithISColoringDestroy(&iscoloring);
12152ba42892SBarry SmithMatFDColoringSetFunction(fdcoloring, (MatFDColoringFn *)FormFunction, &user);
12167f296bb3SBarry SmithMatFDColoringSetFromOptions(fdcoloring);
12177f296bb3SBarry Smith
12187f296bb3SBarry Smith/*
12197f296bb3SBarry Smith  Tell SNES to use the routine SNESComputeJacobianDefaultColor()
12207f296bb3SBarry Smith  to compute Jacobians.
12217f296bb3SBarry Smith*/
12227f296bb3SBarry SmithSNESSetJacobian(snes, J, J, SNESComputeJacobianDefaultColor, fdcoloring);
12237f296bb3SBarry Smith```
12247f296bb3SBarry Smith
12257f296bb3SBarry SmithOf course, we are cheating a bit. If we do not have an analytic formula
12267f296bb3SBarry Smithfor computing the Jacobian, then how do we know what its nonzero
12277f296bb3SBarry Smithstructure is so that it may be colored? Determining the structure is
12287f296bb3SBarry Smithproblem dependent, but fortunately, for most structured grid problems
12297f296bb3SBarry Smith(the class of problems for which PETSc was originally designed) if one
12307f296bb3SBarry Smithknows the stencil used for the nonlinear function one can usually fairly
12317f296bb3SBarry Smitheasily obtain an estimate of the location of nonzeros in the matrix.
12327f296bb3SBarry SmithThis is harder in the unstructured case, but one typically knows where the nonzero entries are from the mesh topology and distribution of degrees of freedom.
12337f296bb3SBarry SmithIf using `DMPlex` ({any}`ch_unstructured`) for unstructured meshes, the nonzero locations will be identified in `DMCreateMatrix()` and the procedure above can be used.
12347f296bb3SBarry SmithMost external packages for unstructured meshes have similar functionality.
12357f296bb3SBarry Smith
12367f296bb3SBarry SmithOne need not necessarily use a `MatColoring` object to determine a
12377f296bb3SBarry Smithcoloring. For example, if a grid can be colored directly (without using
12387f296bb3SBarry Smiththe associated matrix), then that coloring can be provided to
12397f296bb3SBarry Smith`MatFDColoringCreate()`. Note that the user must always preset the
12407f296bb3SBarry Smithnonzero structure in the matrix regardless of which coloring routine is
12417f296bb3SBarry Smithused.
12427f296bb3SBarry Smith
12437f296bb3SBarry SmithPETSc provides the following coloring algorithms, which can be selected using `MatColoringSetType()` or via the command line argument `-mat_coloring_type`.
12447f296bb3SBarry Smith
12457f296bb3SBarry Smith```{eval-rst}
12467f296bb3SBarry Smith.. list-table::
12477f296bb3SBarry Smith   :header-rows: 1
12487f296bb3SBarry Smith
12497f296bb3SBarry Smith   * - Algorithm
12507f296bb3SBarry Smith     - ``MatColoringType``
12517f296bb3SBarry Smith     - ``-mat_coloring_type``
12527f296bb3SBarry Smith     - Parallel
12537f296bb3SBarry Smith   * - smallest-last :cite:`more84`
12547f296bb3SBarry Smith     - ``MATCOLORINGSL``
12557f296bb3SBarry Smith     - ``sl``
12567f296bb3SBarry Smith     - No
12577f296bb3SBarry Smith   * - largest-first :cite:`more84`
12587f296bb3SBarry Smith     - ``MATCOLORINGLF``
12597f296bb3SBarry Smith     - ``lf``
12607f296bb3SBarry Smith     - No
12617f296bb3SBarry Smith   * - incidence-degree :cite:`more84`
12627f296bb3SBarry Smith     - ``MATCOLORINGID``
12637f296bb3SBarry Smith     - ``id``
12647f296bb3SBarry Smith     - No
12657f296bb3SBarry Smith   * - Jones-Plassmann :cite:`jp:pcolor`
12667f296bb3SBarry Smith     - ``MATCOLORINGJP``
12677f296bb3SBarry Smith     - ``jp``
12687f296bb3SBarry Smith     - Yes
12697f296bb3SBarry Smith   * - Greedy
12707f296bb3SBarry Smith     - ``MATCOLORINGGREEDY``
12717f296bb3SBarry Smith     - ``greedy``
12727f296bb3SBarry Smith     - Yes
12737f296bb3SBarry Smith   * - Natural (1 color per column)
12747f296bb3SBarry Smith     - ``MATCOLORINGNATURAL``
12757f296bb3SBarry Smith     - ``natural``
12767f296bb3SBarry Smith     - Yes
12777f296bb3SBarry Smith   * - Power (:math:`A^k` followed by 1-coloring)
12787f296bb3SBarry Smith     - ``MATCOLORINGPOWER``
12797f296bb3SBarry Smith     - ``power``
12807f296bb3SBarry Smith     - Yes
12817f296bb3SBarry Smith```
12827f296bb3SBarry Smith
12837f296bb3SBarry SmithAs for the matrix-free computation of Jacobians ({any}`sec_nlmatrixfree`), two parameters affect the accuracy of the
12847f296bb3SBarry Smithfinite difference Jacobian approximation. These are set with the command
12857f296bb3SBarry Smith
12867f296bb3SBarry Smith```
12877f296bb3SBarry SmithMatFDColoringSetParameters(MatFDColoring fdcoloring, PetscReal rerror, PetscReal umin);
12887f296bb3SBarry Smith```
12897f296bb3SBarry Smith
12907f296bb3SBarry SmithThe parameter `rerror` is the square root of the relative error in the
12917f296bb3SBarry Smithfunction evaluations, $e_{rel}$; the default is the square root of
12927f296bb3SBarry Smithmachine epsilon (about $10^{-8}$ in double precision), which
12937f296bb3SBarry Smithassumes that the functions are evaluated approximately to floating-point
12947f296bb3SBarry Smithprecision accuracy. The second parameter, `umin`, is a bit more
12953ce01cb7SJose E. Romaninvolved; its default is $10^{-6}$. Column $i$ of the
12967f296bb3SBarry SmithJacobian matrix (denoted by $F_{:i}$) is approximated by the
12977f296bb3SBarry Smithformula
12987f296bb3SBarry Smith
12997f296bb3SBarry Smith$$
13007f296bb3SBarry SmithF'_{:i} \approx \frac{F(u + h*dx_{i}) - F(u)}{h}
13017f296bb3SBarry Smith$$
13027f296bb3SBarry Smith
13037f296bb3SBarry Smithwhere $h$ is computed via:
13047f296bb3SBarry Smith
13057f296bb3SBarry Smith$$
13067f296bb3SBarry Smithh = e_{\text{rel}} \cdot \begin{cases}
13077f296bb3SBarry Smithu_{i}             &    \text{if $|u_{i}| > u_{\min}$} \\
13087f296bb3SBarry Smithu_{\min} \cdot \operatorname{sign}(u_{i})  & \text{otherwise}.
13097f296bb3SBarry Smith\end{cases}
13107f296bb3SBarry Smith$$
13117f296bb3SBarry Smith
13127f296bb3SBarry Smithfor `MATMFFD_DS` or:
13137f296bb3SBarry Smith
13147f296bb3SBarry Smith$$
13153ce01cb7SJose E. Romanh = e_{\text{rel}} \sqrt{\|u\|}
13167f296bb3SBarry Smith$$
13177f296bb3SBarry Smith
13187f296bb3SBarry Smithfor `MATMFFD_WP` (default). These parameters may be set from the options
13197f296bb3SBarry Smithdatabase with
13207f296bb3SBarry Smith
13217f296bb3SBarry Smith```
13227f296bb3SBarry Smith-mat_fd_coloring_err <err>
13237f296bb3SBarry Smith-mat_fd_coloring_umin <umin>
13247f296bb3SBarry Smith-mat_fd_type <htype>
13257f296bb3SBarry Smith```
13267f296bb3SBarry Smith
13277f296bb3SBarry SmithNote that `MatColoring` type `MATCOLORINGSL`, `MATCOLORINGLF`, and
13287f296bb3SBarry Smith`MATCOLORINGID` are sequential algorithms. `MATCOLORINGJP` and
13297f296bb3SBarry Smith`MATCOLORINGGREEDY` are parallel algorithms, although in practice they
13307f296bb3SBarry Smithmay create more colors than the sequential algorithms. If one computes
13317f296bb3SBarry Smiththe coloring `iscoloring` reasonably with a parallel algorithm or by
13327f296bb3SBarry Smithknowledge of the discretization, the routine `MatFDColoringCreate()`
13337f296bb3SBarry Smithis scalable. An example of this for 2D distributed arrays is given below
13347f296bb3SBarry Smiththat uses the utility routine `DMCreateColoring()`.
13357f296bb3SBarry Smith
13367f296bb3SBarry Smith```
1337b5ef2b50SBarry SmithDMCreateColoring(dm, IS_COLORING_GHOSTED, &iscoloring);
13387f296bb3SBarry SmithMatFDColoringCreate(J, iscoloring, &fdcoloring);
13397f296bb3SBarry SmithMatFDColoringSetFromOptions(fdcoloring);
13407f296bb3SBarry SmithISColoringDestroy(&iscoloring);
13417f296bb3SBarry Smith```
13427f296bb3SBarry Smith
13437f296bb3SBarry SmithNote that the routine `MatFDColoringCreate()` currently is only
13447f296bb3SBarry Smithsupported for the AIJ and BAIJ matrix formats.
13457f296bb3SBarry Smith
13467f296bb3SBarry Smith(sec_vi)=
13477f296bb3SBarry Smith
13487f296bb3SBarry Smith## Variational Inequalities
13497f296bb3SBarry Smith
13507f296bb3SBarry Smith`SNES` can also solve (differential) variational inequalities with box (bound) constraints.
13517f296bb3SBarry SmithThese are nonlinear algebraic systems with additional inequality
13527f296bb3SBarry Smithconstraints on some or all of the variables:
13537f296bb3SBarry Smith$L_i \le u_i \le H_i$. For example, the pressure variable cannot be negative.
13547f296bb3SBarry SmithSome, or all, of the lower bounds may be
13557f296bb3SBarry Smithnegative infinity (indicated to PETSc with `SNES_VI_NINF`) and some, or
13567f296bb3SBarry Smithall, of the upper bounds may be infinity (indicated by `SNES_VI_INF`).
13577f296bb3SBarry SmithThe commands
13587f296bb3SBarry Smith
13597f296bb3SBarry Smith```
13604558fef0SBarry SmithSNESVISetVariableBounds(SNES snes, Vec L, Vec H);
13617f296bb3SBarry SmithSNESVISetComputeVariableBounds(SNES snes, PetscErrorCode (*compute)(SNES, Vec, Vec))
13627f296bb3SBarry Smith```
13637f296bb3SBarry Smith
13647f296bb3SBarry Smithare used to indicate that one is solving a variational inequality. Problems with box constraints can be solved with
13657f296bb3SBarry Smiththe reduced space, `SNESVINEWTONRSLS`, and semi-smooth `SNESVINEWTONSSLS` solvers.
13667f296bb3SBarry Smith
13677f296bb3SBarry SmithThe
13687f296bb3SBarry Smithoption `-snes_vi_monitor` turns on extra monitoring of the active set
13697f296bb3SBarry Smithassociated with the bounds and `-snes_vi_type` allows selecting from
13707f296bb3SBarry Smithseveral VI solvers, the default is preferred.
13717f296bb3SBarry Smith
13727f296bb3SBarry Smith`SNESLineSearchSetPreCheck()` and `SNESLineSearchSetPostCheck()` can also be used to control properties
13737f296bb3SBarry Smithof the steps selected by `SNES`.
13747f296bb3SBarry Smith
13757f296bb3SBarry Smith(sec_snespc)=
13767f296bb3SBarry Smith
13777f296bb3SBarry Smith## Nonlinear Preconditioning
13787f296bb3SBarry Smith
13797f296bb3SBarry SmithThe mathematical framework of nonlinear preconditioning is explained in detail in {cite}`bruneknepleysmithtu15`.
13807f296bb3SBarry SmithNonlinear preconditioning in PETSc involves the use of an inner `SNES`
13817f296bb3SBarry Smithinstance to define the step for an outer `SNES` instance. The inner
13827f296bb3SBarry Smithinstance may be extracted using
13837f296bb3SBarry Smith
13847f296bb3SBarry Smith```
13857f296bb3SBarry SmithSNESGetNPC(SNES snes, SNES *npc);
13867f296bb3SBarry Smith```
13877f296bb3SBarry Smith
13887f296bb3SBarry Smithand passed run-time options using the `-npc_` prefix. Nonlinear
13897f296bb3SBarry Smithpreconditioning comes in two flavors: left and right. The side may be
13907f296bb3SBarry Smithchanged using `-snes_npc_side` or `SNESSetNPCSide()`. Left nonlinear
13917f296bb3SBarry Smithpreconditioning redefines the nonlinear function as the action of the
13927f296bb3SBarry Smithnonlinear preconditioner $\mathbf{M}$;
13937f296bb3SBarry Smith
13947f296bb3SBarry Smith$$
13957f296bb3SBarry Smith\mathbf{F}_{M}(x) = \mathbf{M}(\mathbf{x},\mathbf{b}) - \mathbf{x}.
13967f296bb3SBarry Smith$$
13977f296bb3SBarry Smith
13987f296bb3SBarry SmithRight nonlinear preconditioning redefines the nonlinear function as the
13997f296bb3SBarry Smithfunction on the action of the nonlinear preconditioner;
14007f296bb3SBarry Smith
14017f296bb3SBarry Smith$$
14027f296bb3SBarry Smith\mathbf{F}(\mathbf{M}(\mathbf{x},\mathbf{b})) = \mathbf{b},
14037f296bb3SBarry Smith$$
14047f296bb3SBarry Smith
14057f296bb3SBarry Smithwhich can be interpreted as putting the preconditioner into “striking
14067f296bb3SBarry Smithdistance” of the solution by outer acceleration.
14077f296bb3SBarry Smith
14087f296bb3SBarry SmithIn addition, basic patterns of solver composition are available with the
14097f296bb3SBarry Smith`SNESType` `SNESCOMPOSITE`. This allows for two or more `SNES`
14107f296bb3SBarry Smithinstances to be combined additively or multiplicatively. By command
14117f296bb3SBarry Smithline, a set of `SNES` types may be given by comma separated list
14127f296bb3SBarry Smithargument to `-snes_composite_sneses`. There are additive
14137f296bb3SBarry Smith(`SNES_COMPOSITE_ADDITIVE`), additive with optimal damping
14147f296bb3SBarry Smith(`SNES_COMPOSITE_ADDITIVEOPTIMAL`), and multiplicative
14157f296bb3SBarry Smith(`SNES_COMPOSITE_MULTIPLICATIVE`) variants which may be set with
14167f296bb3SBarry Smith
14177f296bb3SBarry Smith```
14187f296bb3SBarry SmithSNESCompositeSetType(SNES, SNESCompositeType);
14197f296bb3SBarry Smith```
14207f296bb3SBarry Smith
14217f296bb3SBarry SmithNew subsolvers may be added to the composite solver with
14227f296bb3SBarry Smith
14237f296bb3SBarry Smith```
14247f296bb3SBarry SmithSNESCompositeAddSNES(SNES, SNESType);
14257f296bb3SBarry Smith```
14267f296bb3SBarry Smith
14277f296bb3SBarry Smithand accessed with
14287f296bb3SBarry Smith
14297f296bb3SBarry Smith```
14307f296bb3SBarry SmithSNESCompositeGetSNES(SNES, PetscInt, SNES *);
14317f296bb3SBarry Smith```
14327f296bb3SBarry Smith
14337f296bb3SBarry Smith```{eval-rst}
14347f296bb3SBarry Smith.. bibliography:: /petsc.bib
14357f296bb3SBarry Smith   :filter: docname in docnames
14367f296bb3SBarry Smith```
1437