1*7f296bb3SBarry Smith(ch_snes)= 2*7f296bb3SBarry Smith 3*7f296bb3SBarry Smith# SNES: Nonlinear Solvers 4*7f296bb3SBarry Smith 5*7f296bb3SBarry SmithThe solution of large-scale nonlinear problems pervades many facets of 6*7f296bb3SBarry Smithcomputational science and demands robust and flexible solution 7*7f296bb3SBarry Smithstrategies. The `SNES` library of PETSc provides a powerful suite of 8*7f296bb3SBarry Smithdata-structure-neutral numerical routines for such problems. Built on 9*7f296bb3SBarry Smithtop of the linear solvers and data structures discussed in preceding 10*7f296bb3SBarry Smithchapters, `SNES` enables the user to easily customize the nonlinear 11*7f296bb3SBarry Smithsolvers according to the application at hand. Also, the `SNES` 12*7f296bb3SBarry Smithinterface is *identical* for the uniprocess and parallel cases; the only 13*7f296bb3SBarry Smithdifference in the parallel version is that each process typically forms 14*7f296bb3SBarry Smithonly its local contribution to various matrices and vectors. 15*7f296bb3SBarry Smith 16*7f296bb3SBarry SmithThe `SNES` class includes methods for solving systems of nonlinear 17*7f296bb3SBarry Smithequations of the form 18*7f296bb3SBarry Smith 19*7f296bb3SBarry Smith$$ 20*7f296bb3SBarry Smith\mathbf{F}(\mathbf{x}) = 0, 21*7f296bb3SBarry Smith$$ (fx0) 22*7f296bb3SBarry Smith 23*7f296bb3SBarry Smithwhere $\mathbf{F}: \, \Re^n \to \Re^n$. Newton-like methods provide the 24*7f296bb3SBarry Smithcore of the package, including both line search and trust region 25*7f296bb3SBarry Smithtechniques. A suite of nonlinear Krylov methods and methods based upon 26*7f296bb3SBarry Smithproblem decomposition are also included. The solvers are discussed 27*7f296bb3SBarry Smithfurther in {any}`sec_nlsolvers`. Following the PETSc design 28*7f296bb3SBarry Smithphilosophy, the interfaces to the various solvers are all virtually 29*7f296bb3SBarry Smithidentical. In addition, the `SNES` software is completely flexible, so 30*7f296bb3SBarry Smiththat the user can at runtime change any facet of the solution process. 31*7f296bb3SBarry Smith 32*7f296bb3SBarry SmithPETSc’s default method for solving the nonlinear equation is Newton’s 33*7f296bb3SBarry Smithmethod with line search, `SNESNEWTONLS`. The general form of the $n$-dimensional Newton’s method 34*7f296bb3SBarry Smithfor solving {math:numref}`fx0` is 35*7f296bb3SBarry Smith 36*7f296bb3SBarry Smith$$ 37*7f296bb3SBarry Smith\mathbf{x}_{k+1} = \mathbf{x}_k - \mathbf{J}(\mathbf{x}_k)^{-1} \mathbf{F}(\mathbf{x}_k), \;\; k=0,1, \ldots, 38*7f296bb3SBarry Smith$$ (newton) 39*7f296bb3SBarry Smith 40*7f296bb3SBarry Smithwhere $\mathbf{x}_0$ is an initial approximation to the solution and 41*7f296bb3SBarry Smith$\mathbf{J}(\mathbf{x}_k) = \mathbf{F}'(\mathbf{x}_k)$, the Jacobian, is nonsingular at each 42*7f296bb3SBarry Smithiteration. In practice, the Newton iteration {math:numref}`newton` is 43*7f296bb3SBarry Smithimplemented by the following two steps: 44*7f296bb3SBarry Smith 45*7f296bb3SBarry Smith$$ 46*7f296bb3SBarry Smith\begin{aligned} 47*7f296bb3SBarry Smith1. & \text{(Approximately) solve} & \mathbf{J}(\mathbf{x}_k) \Delta \mathbf{x}_k &= -\mathbf{F}(\mathbf{x}_k). \\ 48*7f296bb3SBarry Smith2. & \text{Update} & \mathbf{x}_{k+1} &\gets \mathbf{x}_k + \Delta \mathbf{x}_k. 49*7f296bb3SBarry Smith\end{aligned} 50*7f296bb3SBarry Smith$$ 51*7f296bb3SBarry Smith 52*7f296bb3SBarry SmithOther defect-correction algorithms can be implemented by using different 53*7f296bb3SBarry Smithchoices for $J(\mathbf{x}_k)$. 54*7f296bb3SBarry Smith 55*7f296bb3SBarry Smith(sec_snesusage)= 56*7f296bb3SBarry Smith 57*7f296bb3SBarry Smith## Basic SNES Usage 58*7f296bb3SBarry Smith 59*7f296bb3SBarry SmithIn the simplest usage of the nonlinear solvers, the user must merely 60*7f296bb3SBarry Smithprovide a C, C++, Fortran, or Python routine to evaluate the nonlinear function 61*7f296bb3SBarry Smith{math:numref}`fx0`. The corresponding Jacobian matrix 62*7f296bb3SBarry Smithcan be approximated with finite differences. For codes that are 63*7f296bb3SBarry Smithtypically more efficient and accurate, the user can provide a routine to 64*7f296bb3SBarry Smithcompute the Jacobian; details regarding these application-provided 65*7f296bb3SBarry Smithroutines are discussed below. To provide an overview of the use of the 66*7f296bb3SBarry Smithnonlinear solvers, browse the concrete example in {ref}`ex1.c <snes-ex1>` or skip ahead to the discussion. 67*7f296bb3SBarry Smith 68*7f296bb3SBarry Smith(snes_ex1)= 69*7f296bb3SBarry Smith 70*7f296bb3SBarry Smith:::{admonition} Listing: `src/snes/tutorials/ex1.c` 71*7f296bb3SBarry Smith```{literalinclude} /../src/snes/tutorials/ex1.c 72*7f296bb3SBarry Smith:end-before: /*TEST 73*7f296bb3SBarry Smith``` 74*7f296bb3SBarry Smith::: 75*7f296bb3SBarry Smith 76*7f296bb3SBarry SmithTo create a `SNES` solver, one must first call `SNESCreate()` as 77*7f296bb3SBarry Smithfollows: 78*7f296bb3SBarry Smith 79*7f296bb3SBarry Smith``` 80*7f296bb3SBarry SmithSNESCreate(MPI_Comm comm,SNES *snes); 81*7f296bb3SBarry Smith``` 82*7f296bb3SBarry Smith 83*7f296bb3SBarry SmithThe user must then set routines for evaluating the residual function {math:numref}`fx0` 84*7f296bb3SBarry Smithand, *possibly*, its associated Jacobian matrix, as 85*7f296bb3SBarry Smithdiscussed in the following sections. 86*7f296bb3SBarry Smith 87*7f296bb3SBarry SmithTo choose a nonlinear solution method, the user can either call 88*7f296bb3SBarry Smith 89*7f296bb3SBarry Smith``` 90*7f296bb3SBarry SmithSNESSetType(SNES snes,SNESType method); 91*7f296bb3SBarry Smith``` 92*7f296bb3SBarry Smith 93*7f296bb3SBarry Smithor use the option `-snes_type <method>`, where details regarding the 94*7f296bb3SBarry Smithavailable methods are presented in {any}`sec_nlsolvers`. The 95*7f296bb3SBarry Smithapplication code can take complete control of the linear and nonlinear 96*7f296bb3SBarry Smithtechniques used in the Newton-like method by calling 97*7f296bb3SBarry Smith 98*7f296bb3SBarry Smith``` 99*7f296bb3SBarry SmithSNESSetFromOptions(snes); 100*7f296bb3SBarry Smith``` 101*7f296bb3SBarry Smith 102*7f296bb3SBarry SmithThis routine provides an interface to the PETSc options database, so 103*7f296bb3SBarry Smiththat at runtime the user can select a particular nonlinear solver, set 104*7f296bb3SBarry Smithvarious parameters and customized routines (e.g., specialized line 105*7f296bb3SBarry Smithsearch variants), prescribe the convergence tolerance, and set 106*7f296bb3SBarry Smithmonitoring routines. With this routine the user can also control all 107*7f296bb3SBarry Smithlinear solver options in the `KSP`, and `PC` modules, as discussed 108*7f296bb3SBarry Smithin {any}`ch_ksp`. 109*7f296bb3SBarry Smith 110*7f296bb3SBarry SmithAfter having set these routines and options, the user solves the problem 111*7f296bb3SBarry Smithby calling 112*7f296bb3SBarry Smith 113*7f296bb3SBarry Smith``` 114*7f296bb3SBarry SmithSNESSolve(SNES snes,Vec b,Vec x); 115*7f296bb3SBarry Smith``` 116*7f296bb3SBarry Smith 117*7f296bb3SBarry Smithwhere `x` should be initialized to the initial guess before calling and contains the solution on return. 118*7f296bb3SBarry SmithIn particular, to employ an initial guess of 119*7f296bb3SBarry Smithzero, the user should explicitly set this vector to zero by calling 120*7f296bb3SBarry Smith`VecZeroEntries(x)`. Finally, after solving the nonlinear system (or several 121*7f296bb3SBarry Smithsystems), the user should destroy the `SNES` context with 122*7f296bb3SBarry Smith 123*7f296bb3SBarry Smith``` 124*7f296bb3SBarry SmithSNESDestroy(SNES *snes); 125*7f296bb3SBarry Smith``` 126*7f296bb3SBarry Smith 127*7f296bb3SBarry Smith(sec_snesfunction)= 128*7f296bb3SBarry Smith 129*7f296bb3SBarry Smith### Nonlinear Function Evaluation 130*7f296bb3SBarry Smith 131*7f296bb3SBarry SmithWhen solving a system of nonlinear equations, the user must provide a 132*7f296bb3SBarry Smitha residual function {math:numref}`fx0`, which is set using 133*7f296bb3SBarry Smith 134*7f296bb3SBarry Smith``` 135*7f296bb3SBarry SmithSNESSetFunction(SNES snes,Vec f,PetscErrorCode (*FormFunction)(SNES snes,Vec x,Vec f,void *ctx),void *ctx); 136*7f296bb3SBarry Smith``` 137*7f296bb3SBarry Smith 138*7f296bb3SBarry SmithThe argument `f` is an optional vector for storing the solution; pass `NULL` to have the `SNES` allocate it for you. 139*7f296bb3SBarry SmithThe argument `ctx` is an optional user-defined context, which can 140*7f296bb3SBarry Smithstore any private, application-specific data required by the function 141*7f296bb3SBarry Smithevaluation routine; `NULL` should be used if such information is not 142*7f296bb3SBarry Smithneeded. In C and C++, a user-defined context is merely a structure in 143*7f296bb3SBarry Smithwhich various objects can be stashed; in Fortran a user context can be 144*7f296bb3SBarry Smithan integer array that contains both parameters and pointers to PETSc 145*7f296bb3SBarry Smithobjects. 146*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tutorials/ex5.c.html">SNES Tutorial ex5</a> 147*7f296bb3SBarry Smithand 148*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tutorials/ex5f90.F90.html">SNES Tutorial ex5f90</a> 149*7f296bb3SBarry Smithgive examples of user-defined application contexts in C and Fortran, 150*7f296bb3SBarry Smithrespectively. 151*7f296bb3SBarry Smith 152*7f296bb3SBarry Smith(sec_snesjacobian)= 153*7f296bb3SBarry Smith 154*7f296bb3SBarry Smith### Jacobian Evaluation 155*7f296bb3SBarry Smith 156*7f296bb3SBarry SmithThe user may also specify a routine to form some approximation of the 157*7f296bb3SBarry SmithJacobian matrix, `A`, at the current iterate, `x`, as is typically 158*7f296bb3SBarry Smithdone with 159*7f296bb3SBarry Smith 160*7f296bb3SBarry Smith``` 161*7f296bb3SBarry SmithSNESSetJacobian(SNES snes,Mat Amat,Mat Pmat,PetscErrorCode (*FormJacobian)(SNES snes,Vec x,Mat A,Mat B,void *ctx),void *ctx); 162*7f296bb3SBarry Smith``` 163*7f296bb3SBarry Smith 164*7f296bb3SBarry SmithThe arguments of the routine `FormJacobian()` are the current iterate, 165*7f296bb3SBarry Smith`x`; the (approximate) Jacobian matrix, `Amat`; the matrix from 166*7f296bb3SBarry Smithwhich the preconditioner is constructed, `Pmat` (which is usually the 167*7f296bb3SBarry Smithsame as `Amat`); and an optional user-defined Jacobian context, 168*7f296bb3SBarry Smith`ctx`, for application-specific data. The `FormJacobian()` 169*7f296bb3SBarry Smithcallback is only invoked if the solver requires it, always 170*7f296bb3SBarry Smith*after* `FormFunction()` has been called at the current iterate. 171*7f296bb3SBarry Smith 172*7f296bb3SBarry SmithNote that the `SNES` solvers 173*7f296bb3SBarry Smithare all data-structure neutral, so the full range of PETSc matrix 174*7f296bb3SBarry Smithformats (including “matrix-free” methods) can be used. 175*7f296bb3SBarry Smith{any}`ch_matrices` discusses information regarding 176*7f296bb3SBarry Smithavailable matrix formats and options, while {any}`sec_nlmatrixfree` focuses on matrix-free methods in 177*7f296bb3SBarry Smith`SNES`. We briefly touch on a few details of matrix usage that are 178*7f296bb3SBarry Smithparticularly important for efficient use of the nonlinear solvers. 179*7f296bb3SBarry Smith 180*7f296bb3SBarry SmithA common usage paradigm is to assemble the problem Jacobian in the 181*7f296bb3SBarry Smithpreconditioner storage `B`, rather than `A`. In the case where they 182*7f296bb3SBarry Smithare identical, as in many simulations, this makes no difference. 183*7f296bb3SBarry SmithHowever, it allows us to check the analytic Jacobian we construct in 184*7f296bb3SBarry Smith`FormJacobian()` by passing the `-snes_mf_operator` flag. This 185*7f296bb3SBarry Smithcauses PETSc to approximate the Jacobian using finite differencing of 186*7f296bb3SBarry Smiththe function evaluation (discussed in {any}`sec_fdmatrix`), 187*7f296bb3SBarry Smithand the analytic Jacobian becomes merely the preconditioner. Even if the 188*7f296bb3SBarry Smithanalytic Jacobian is incorrect, it is likely that the finite difference 189*7f296bb3SBarry Smithapproximation will converge, and thus this is an excellent method to 190*7f296bb3SBarry Smithverify the analytic Jacobian. Moreover, if the analytic Jacobian is 191*7f296bb3SBarry Smithincomplete (some terms are missing or approximate), 192*7f296bb3SBarry Smith`-snes_mf_operator` may be used to obtain the exact solution, where 193*7f296bb3SBarry Smiththe Jacobian approximation has been transferred to the preconditioner. 194*7f296bb3SBarry Smith 195*7f296bb3SBarry SmithOne such approximate Jacobian comes from “Picard linearization”, use `SNESSetPicard()`, which 196*7f296bb3SBarry Smithwrites the nonlinear system as 197*7f296bb3SBarry Smith 198*7f296bb3SBarry Smith$$ 199*7f296bb3SBarry Smith\mathbf{F}(\mathbf{x}) := \mathbf{A}(\mathbf{x}) \mathbf{x} - \mathbf{b} = 0 200*7f296bb3SBarry Smith$$ 201*7f296bb3SBarry Smith 202*7f296bb3SBarry Smithwhere $\mathbf{A}(\mathbf{x})$ usually contains the lower-derivative parts of the 203*7f296bb3SBarry Smithequation. For example, the nonlinear diffusion problem 204*7f296bb3SBarry Smith 205*7f296bb3SBarry Smith$$ 206*7f296bb3SBarry Smith- \nabla\cdot(\kappa(u) \nabla u) = 0 207*7f296bb3SBarry Smith$$ 208*7f296bb3SBarry Smith 209*7f296bb3SBarry Smithwould be linearized as 210*7f296bb3SBarry Smith 211*7f296bb3SBarry Smith$$ 212*7f296bb3SBarry SmithA(u) v \simeq -\nabla\cdot(\kappa(u) \nabla v). 213*7f296bb3SBarry Smith$$ 214*7f296bb3SBarry Smith 215*7f296bb3SBarry SmithUsually this linearization is simpler to implement than Newton and the 216*7f296bb3SBarry Smithlinear problems are somewhat easier to solve. In addition to using 217*7f296bb3SBarry Smith`-snes_mf_operator` with this approximation to the Jacobian, the 218*7f296bb3SBarry SmithPicard iterative procedure can be performed by defining $\mathbf{J}(\mathbf{x})$ 219*7f296bb3SBarry Smithto be $\mathbf{A}(\mathbf{x})$. Sometimes this iteration exhibits better global 220*7f296bb3SBarry Smithconvergence than Newton linearization. 221*7f296bb3SBarry Smith 222*7f296bb3SBarry SmithDuring successive calls to `FormJacobian()`, the user can either 223*7f296bb3SBarry Smithinsert new matrix contexts or reuse old ones, depending on the 224*7f296bb3SBarry Smithapplication requirements. For many sparse matrix formats, reusing the 225*7f296bb3SBarry Smithold space (and merely changing the matrix elements) is more efficient; 226*7f296bb3SBarry Smithhowever, if the matrix nonzero structure completely changes, creating an 227*7f296bb3SBarry Smithentirely new matrix context may be preferable. Upon subsequent calls to 228*7f296bb3SBarry Smiththe `FormJacobian()` routine, the user may wish to reinitialize the 229*7f296bb3SBarry Smithmatrix entries to zero by calling `MatZeroEntries()`. See 230*7f296bb3SBarry Smith{any}`sec_othermat` for details on the reuse of the matrix 231*7f296bb3SBarry Smithcontext. 232*7f296bb3SBarry Smith 233*7f296bb3SBarry SmithThe directory `$PETSC_DIR/src/snes/tutorials` provides a variety of 234*7f296bb3SBarry Smithexamples. 235*7f296bb3SBarry Smith 236*7f296bb3SBarry SmithSometimes a nonlinear solver may produce a step that is not within the domain 237*7f296bb3SBarry Smithof a given function, for example one with a negative pressure. When this occurs 238*7f296bb3SBarry Smithone can call `SNESSetFunctionDomainError()` or `SNESSetJacobianDomainError()` 239*7f296bb3SBarry Smithto indicate to `SNES` the step is not valid. One must also use `SNESGetConvergedReason()` 240*7f296bb3SBarry Smithand check the reason to confirm if the solver succeeded. See {any}`sec_vi` for how to 241*7f296bb3SBarry Smithprovide `SNES` with bounds on the variables to solve (differential) variational inequalities 242*7f296bb3SBarry Smithand how to control properties of the line step computed. 243*7f296bb3SBarry Smith 244*7f296bb3SBarry Smith(sec_nlsolvers)= 245*7f296bb3SBarry Smith 246*7f296bb3SBarry Smith## The Nonlinear Solvers 247*7f296bb3SBarry Smith 248*7f296bb3SBarry SmithAs summarized in Table {any}`tab-snesdefaults`, `SNES` includes 249*7f296bb3SBarry Smithseveral Newton-like nonlinear solvers based on line search techniques 250*7f296bb3SBarry Smithand trust region methods. Also provided are several nonlinear Krylov 251*7f296bb3SBarry Smithmethods, as well as nonlinear methods involving decompositions of the 252*7f296bb3SBarry Smithproblem. 253*7f296bb3SBarry Smith 254*7f296bb3SBarry SmithEach solver may have associated with it a set of options, which can be 255*7f296bb3SBarry Smithset with routines and options database commands provided for this 256*7f296bb3SBarry Smithpurpose. A complete list can be found by consulting the manual pages or 257*7f296bb3SBarry Smithby running a program with the `-help` option; we discuss just a few in 258*7f296bb3SBarry Smiththe sections below. 259*7f296bb3SBarry Smith 260*7f296bb3SBarry Smith```{eval-rst} 261*7f296bb3SBarry Smith.. list-table:: PETSc Nonlinear Solvers 262*7f296bb3SBarry Smith :name: tab-snesdefaults 263*7f296bb3SBarry Smith :header-rows: 1 264*7f296bb3SBarry Smith 265*7f296bb3SBarry Smith * - Method 266*7f296bb3SBarry Smith - SNESType 267*7f296bb3SBarry Smith - Options Name 268*7f296bb3SBarry Smith - Default Line Search 269*7f296bb3SBarry Smith * - Line Search Newton 270*7f296bb3SBarry Smith - ``SNESNEWTONLS`` 271*7f296bb3SBarry Smith - ``newtonls`` 272*7f296bb3SBarry Smith - ``SNESLINESEARCHBT`` 273*7f296bb3SBarry Smith * - Trust region Newton 274*7f296bb3SBarry Smith - ``SNESNEWTONTR`` 275*7f296bb3SBarry Smith - ``newtontr`` 276*7f296bb3SBarry Smith - — 277*7f296bb3SBarry Smith * - Newton with Arc Length Continuation 278*7f296bb3SBarry Smith - ``SNESNEWTONAL`` 279*7f296bb3SBarry Smith - ``newtonal`` 280*7f296bb3SBarry Smith - — 281*7f296bb3SBarry Smith * - Nonlinear Richardson 282*7f296bb3SBarry Smith - ``SNESNRICHARDSON`` 283*7f296bb3SBarry Smith - ``nrichardson`` 284*7f296bb3SBarry Smith - ``SNESLINESEARCHL2`` 285*7f296bb3SBarry Smith * - Nonlinear CG 286*7f296bb3SBarry Smith - ``SNESNCG`` 287*7f296bb3SBarry Smith - ``ncg`` 288*7f296bb3SBarry Smith - ``SNESLINESEARCHCP`` 289*7f296bb3SBarry Smith * - Nonlinear GMRES 290*7f296bb3SBarry Smith - ``SNESNGMRES`` 291*7f296bb3SBarry Smith - ``ngmres`` 292*7f296bb3SBarry Smith - ``SNESLINESEARCHL2`` 293*7f296bb3SBarry Smith * - Quasi-Newton 294*7f296bb3SBarry Smith - ``SNESQN`` 295*7f296bb3SBarry Smith - ``qn`` 296*7f296bb3SBarry Smith - see :any:`tab-qndefaults` 297*7f296bb3SBarry Smith * - Full Approximation Scheme 298*7f296bb3SBarry Smith - ``SNESFAS`` 299*7f296bb3SBarry Smith - ``fas`` 300*7f296bb3SBarry Smith - — 301*7f296bb3SBarry Smith * - Nonlinear ASM 302*7f296bb3SBarry Smith - ``SNESNASM`` 303*7f296bb3SBarry Smith - ``nasm`` 304*7f296bb3SBarry Smith - – 305*7f296bb3SBarry Smith * - ASPIN 306*7f296bb3SBarry Smith - ``SNESASPIN`` 307*7f296bb3SBarry Smith - ``aspin`` 308*7f296bb3SBarry Smith - ``SNESLINESEARCHBT`` 309*7f296bb3SBarry Smith * - Nonlinear Gauss-Seidel 310*7f296bb3SBarry Smith - ``SNESNGS`` 311*7f296bb3SBarry Smith - ``ngs`` 312*7f296bb3SBarry Smith - – 313*7f296bb3SBarry Smith * - Anderson Mixing 314*7f296bb3SBarry Smith - ``SNESANDERSON`` 315*7f296bb3SBarry Smith - ``anderson`` 316*7f296bb3SBarry Smith - – 317*7f296bb3SBarry Smith * - Newton with constraints (1) 318*7f296bb3SBarry Smith - ``SNESVINEWTONRSLS`` 319*7f296bb3SBarry Smith - ``vinewtonrsls`` 320*7f296bb3SBarry Smith - ``SNESLINESEARCHBT`` 321*7f296bb3SBarry Smith * - Newton with constraints (2) 322*7f296bb3SBarry Smith - ``SNESVINEWTONSSLS`` 323*7f296bb3SBarry Smith - ``vinewtonssls`` 324*7f296bb3SBarry Smith - ``SNESLINESEARCHBT`` 325*7f296bb3SBarry Smith * - Multi-stage Smoothers 326*7f296bb3SBarry Smith - ``SNESMS`` 327*7f296bb3SBarry Smith - ``ms`` 328*7f296bb3SBarry Smith - – 329*7f296bb3SBarry Smith * - Composite 330*7f296bb3SBarry Smith - ``SNESCOMPOSITE`` 331*7f296bb3SBarry Smith - ``composite`` 332*7f296bb3SBarry Smith - – 333*7f296bb3SBarry Smith * - Linear solve only 334*7f296bb3SBarry Smith - ``SNESKSPONLY`` 335*7f296bb3SBarry Smith - ``ksponly`` 336*7f296bb3SBarry Smith - – 337*7f296bb3SBarry Smith * - Python Shell 338*7f296bb3SBarry Smith - ``SNESPYTHON`` 339*7f296bb3SBarry Smith - ``python`` 340*7f296bb3SBarry Smith - – 341*7f296bb3SBarry Smith * - Shell (user-defined) 342*7f296bb3SBarry Smith - ``SNESSHELL`` 343*7f296bb3SBarry Smith - ``shell`` 344*7f296bb3SBarry Smith - – 345*7f296bb3SBarry Smith 346*7f296bb3SBarry Smith``` 347*7f296bb3SBarry Smith 348*7f296bb3SBarry Smith### Line Search Newton 349*7f296bb3SBarry Smith 350*7f296bb3SBarry SmithThe method `SNESNEWTONLS` (`-snes_type newtonls`) provides a 351*7f296bb3SBarry Smithline search Newton method for solving systems of nonlinear equations. By 352*7f296bb3SBarry Smithdefault, this technique employs cubic backtracking 353*7f296bb3SBarry Smith{cite}`dennis:83`. Alternative line search techniques are 354*7f296bb3SBarry Smithlisted in Table {any}`tab-linesearches`. 355*7f296bb3SBarry Smith 356*7f296bb3SBarry Smith```{eval-rst} 357*7f296bb3SBarry Smith.. table:: PETSc Line Search Methods 358*7f296bb3SBarry Smith :name: tab-linesearches 359*7f296bb3SBarry Smith 360*7f296bb3SBarry Smith ==================== =========================== ================ 361*7f296bb3SBarry Smith **Line Search** **SNESLineSearchType** **Options Name** 362*7f296bb3SBarry Smith ==================== =========================== ================ 363*7f296bb3SBarry Smith Backtracking ``SNESLINESEARCHBT`` ``bt`` 364*7f296bb3SBarry Smith (damped) step ``SNESLINESEARCHBASIC`` ``basic`` 365*7f296bb3SBarry Smith identical to above ``SNESLINESEARCHNONE`` ``none`` 366*7f296bb3SBarry Smith L2-norm Minimization ``SNESLINESEARCHL2`` ``l2`` 367*7f296bb3SBarry Smith Critical point ``SNESLINESEARCHCP`` ``cp`` 368*7f296bb3SBarry Smith Bisection ``SNESLINESEARCHBISECTION`` ``bisection`` 369*7f296bb3SBarry Smith Shell ``SNESLINESEARCHSHELL`` ``shell`` 370*7f296bb3SBarry Smith ==================== =========================== ================ 371*7f296bb3SBarry Smith``` 372*7f296bb3SBarry Smith 373*7f296bb3SBarry SmithEvery `SNES` has a line search context of type `SNESLineSearch` that 374*7f296bb3SBarry Smithmay be retrieved using 375*7f296bb3SBarry Smith 376*7f296bb3SBarry Smith``` 377*7f296bb3SBarry SmithSNESGetLineSearch(SNES snes,SNESLineSearch *ls);. 378*7f296bb3SBarry Smith``` 379*7f296bb3SBarry Smith 380*7f296bb3SBarry SmithThere are several default options for the line searches. The order of 381*7f296bb3SBarry Smithpolynomial approximation may be set with `-snes_linesearch_order` or 382*7f296bb3SBarry Smith 383*7f296bb3SBarry Smith``` 384*7f296bb3SBarry SmithSNESLineSearchSetOrder(SNESLineSearch ls, PetscInt order); 385*7f296bb3SBarry Smith``` 386*7f296bb3SBarry Smith 387*7f296bb3SBarry Smithfor instance, 2 for quadratic or 3 for cubic. Sometimes, it may not be 388*7f296bb3SBarry Smithnecessary to monitor the progress of the nonlinear iteration. In this 389*7f296bb3SBarry Smithcase, `-snes_linesearch_norms` or 390*7f296bb3SBarry Smith 391*7f296bb3SBarry Smith``` 392*7f296bb3SBarry SmithSNESLineSearchSetComputeNorms(SNESLineSearch ls,PetscBool norms); 393*7f296bb3SBarry Smith``` 394*7f296bb3SBarry Smith 395*7f296bb3SBarry Smithmay be used to turn off function, step, and solution norm computation at 396*7f296bb3SBarry Smiththe end of the linesearch. 397*7f296bb3SBarry Smith 398*7f296bb3SBarry SmithThe default line search for the line search Newton method, 399*7f296bb3SBarry Smith`SNESLINESEARCHBT` involves several parameters, which are set to 400*7f296bb3SBarry Smithdefaults that are reasonable for many applications. The user can 401*7f296bb3SBarry Smithoverride the defaults by using the following options: 402*7f296bb3SBarry Smith 403*7f296bb3SBarry Smith- `-snes_linesearch_alpha <alpha>` 404*7f296bb3SBarry Smith- `-snes_linesearch_maxstep <max>` 405*7f296bb3SBarry Smith- `-snes_linesearch_minlambda <tol>` 406*7f296bb3SBarry Smith 407*7f296bb3SBarry SmithBesides the backtracking linesearch, there are `SNESLINESEARCHL2`, 408*7f296bb3SBarry Smithwhich uses a polynomial secant minimization of $||F(x)||_2$, and 409*7f296bb3SBarry Smith`SNESLINESEARCHCP`, which minimizes $F(x) \cdot Y$ where 410*7f296bb3SBarry Smith$Y$ is the search direction. These are both potentially iterative 411*7f296bb3SBarry Smithline searches, which may be used to find a better-fitted steplength in 412*7f296bb3SBarry Smiththe case where a single secant search is not sufficient. The number of 413*7f296bb3SBarry Smithiterations may be set with `-snes_linesearch_max_it`. In addition, the 414*7f296bb3SBarry Smithconvergence criteria of the iterative line searches may be set using 415*7f296bb3SBarry Smithfunction tolerances `-snes_linesearch_rtol` and 416*7f296bb3SBarry Smith`-snes_linesearch_atol`, and steplength tolerance 417*7f296bb3SBarry Smith`snes_linesearch_ltol`. 418*7f296bb3SBarry Smith 419*7f296bb3SBarry SmithFor highly non-linear problems, the bisection line search `SNESLINESEARCHBISECTION` 420*7f296bb3SBarry Smithmay prove useful due to its robustness. Similar to the critical point line search 421*7f296bb3SBarry Smith`SNESLINESEARCHCP`, it seeks to find the root of $F(x) \cdot Y$. 422*7f296bb3SBarry SmithWhile the latter does so through a secant method, the bisection line search 423*7f296bb3SBarry Smithdoes so by iteratively bisecting the step length interval. 424*7f296bb3SBarry SmithIt works as follows (with $f(\lambda)=F(x-\lambda Y) \cdot Y / ||Y||$ for brevity): 425*7f296bb3SBarry Smith 426*7f296bb3SBarry 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)$ 427*7f296bb3SBarry Smith 428*7f296bb3SBarry 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$ 429*7f296bb3SBarry Smith 430*7f296bb3SBarry Smith3. if there is a change of sign, enter iterative bisection procedure 431*7f296bb3SBarry Smith 432*7f296bb3SBarry Smith 1. check convergence/ exit criteria: 433*7f296bb3SBarry Smith 434*7f296bb3SBarry Smith - absolute tolerance $f(\lambda_j) < \mathtt{atol}$ 435*7f296bb3SBarry Smith - relative tolerance $f(\lambda_j) < \mathtt{rtol} \cdot f(\lambda_0)$ 436*7f296bb3SBarry Smith - change of step length $\lambda_j - \lambda_{j-1} < \mathtt{ltol}$ 437*7f296bb3SBarry Smith - number of iterations $j < \mathtt{max\_it}$ 438*7f296bb3SBarry Smith 439*7f296bb3SBarry Smith 2. if $j > 1$, determine direction of bisection 440*7f296bb3SBarry Smith 441*7f296bb3SBarry Smith $$ 442*7f296bb3SBarry 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} 443*7f296bb3SBarry Smith $$ 444*7f296bb3SBarry Smith 445*7f296bb3SBarry Smith 3. bisect the interval: $\lambda_{j+1} = (\lambda_{\text{left}} + \lambda_{\text{right}})/2$, compute $f(\lambda_{j+1})$ 446*7f296bb3SBarry 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$ 447*7f296bb3SBarry Smith 448*7f296bb3SBarry SmithCustom line search types may either be defined using 449*7f296bb3SBarry Smith`SNESLineSearchShell`, or by creating a custom user line search type 450*7f296bb3SBarry Smithin the model of the preexisting ones and register it using 451*7f296bb3SBarry Smith 452*7f296bb3SBarry Smith``` 453*7f296bb3SBarry SmithSNESLineSearchRegister(const char sname[],PetscErrorCode (*function)(SNESLineSearch));. 454*7f296bb3SBarry Smith``` 455*7f296bb3SBarry Smith 456*7f296bb3SBarry Smith### Trust Region Methods 457*7f296bb3SBarry Smith 458*7f296bb3SBarry SmithThe trust region method in `SNES` for solving systems of nonlinear 459*7f296bb3SBarry Smithequations, `SNESNEWTONTR` (`-snes_type newtontr`), is similar to the one developed in the 460*7f296bb3SBarry SmithMINPACK project {cite}`more84`. Several parameters can be 461*7f296bb3SBarry Smithset to control the variation of the trust region size during the 462*7f296bb3SBarry Smithsolution process. In particular, the user can control the initial trust 463*7f296bb3SBarry Smithregion radius, computed by 464*7f296bb3SBarry Smith 465*7f296bb3SBarry Smith$$ 466*7f296bb3SBarry Smith\Delta = \Delta_0 \| F_0 \|_2, 467*7f296bb3SBarry Smith$$ 468*7f296bb3SBarry Smith 469*7f296bb3SBarry Smithby setting $\Delta_0$ via the option `-snes_tr_delta0 <delta0>`. 470*7f296bb3SBarry Smith 471*7f296bb3SBarry Smith### Newton with Arc Length Continuation 472*7f296bb3SBarry Smith 473*7f296bb3SBarry SmithThe Newton method with arc length continuation reformulates the linearized system 474*7f296bb3SBarry Smith$K\delta \mathbf x = -\mathbf F(\mathbf x)$ by introducing the load parameter 475*7f296bb3SBarry Smith$\lambda$ and splitting the residual into two components, commonly 476*7f296bb3SBarry Smithcorresponding to internal and external forces: 477*7f296bb3SBarry Smith 478*7f296bb3SBarry Smith$$ 479*7f296bb3SBarry Smith\mathbf F(x, \lambda) = \mathbf F^{\mathrm{int}}(\mathbf x) - \mathbf F^{\mathrm{ext}}(\mathbf x, \lambda) 480*7f296bb3SBarry Smith$$ 481*7f296bb3SBarry Smith 482*7f296bb3SBarry SmithOften, $\mathbf F^{\mathrm{ext}}(\mathbf x, \lambda)$ is linear in $\lambda$, 483*7f296bb3SBarry Smithwhich can be thought of as applying the external force in proportional load 484*7f296bb3SBarry Smithincrements. By default, this is how the right-hand side vector is handled in the 485*7f296bb3SBarry Smithimplemented method. Generally, however, $\mathbf F^{\mathrm{ext}}(\mathbf x, \lambda)$ 486*7f296bb3SBarry Smithmay depend non-linearly on $\lambda$ or $\mathbf x$, or both. 487*7f296bb3SBarry SmithTo accommodate this possibility, we provide the `SNESNewtonALGetLoadParameter()` 488*7f296bb3SBarry Smithfunction, which allows for the current value of $\lambda$ to be queried in the 489*7f296bb3SBarry Smithfunctions provided to `SNESSetFunction()` and `SNESSetJacobian()`. 490*7f296bb3SBarry Smith 491*7f296bb3SBarry SmithAdditionally, we split the solution update into two components: 492*7f296bb3SBarry Smith 493*7f296bb3SBarry Smith$$ 494*7f296bb3SBarry Smith\delta \mathbf x = \delta s\delta\mathbf x^F + \delta\lambda\delta\mathbf x^Q, 495*7f296bb3SBarry Smith$$ 496*7f296bb3SBarry Smith 497*7f296bb3SBarry Smithwhere $\delta s = 1$ unless partial corrections are used (discussed more 498*7f296bb3SBarry Smithbelow). Each of $\delta \mathbf x^F$ and $\delta \mathbf x^Q$ are found via 499*7f296bb3SBarry Smithsolving a linear system with the Jacobian $K$: 500*7f296bb3SBarry Smith 501*7f296bb3SBarry 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)$ 502*7f296bb3SBarry 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. 503*7f296bb3SBarry Smith 504*7f296bb3SBarry SmithOften, the tangent load vector $\mathbf Q$ is constant within a load increment, 505*7f296bb3SBarry Smithwhich corresponds to the case of proportional loading discussed above. By default, 506*7f296bb3SBarry Smith$\mathbf Q$ is the full right-hand-side vector, if one was provided. 507*7f296bb3SBarry SmithThe user can also provide a function which computes $\mathbf Q$ to 508*7f296bb3SBarry Smith`SNESNewtonALSetFunction()`. This function should have the same signature as for 509*7f296bb3SBarry Smith`SNESSetFunction`, and the user should use `SNESNewtonALGetLoadParameter()` to get 510*7f296bb3SBarry Smith$\lambda$ if it is needed. 511*7f296bb3SBarry Smith 512*7f296bb3SBarry Smith**The Constraint Surface.** Considering the $n+1$ dimensional space of 513*7f296bb3SBarry Smith$\mathbf x$ and $\lambda$, we define the linearized equilibrium line to be 514*7f296bb3SBarry Smiththe set of points for which the linearized equilibrium equations are satisfied. 515*7f296bb3SBarry SmithGiven the previous iterative solution 516*7f296bb3SBarry Smith$\mathbf t^{(j-1)} = [\mathbf x^{(j-1)}, \lambda^{(j-1)}]$, 517*7f296bb3SBarry Smiththis line is defined by the point $\mathbf t^{(j-1)} + [\delta\mathbf x^F, 0]$ and 518*7f296bb3SBarry Smiththe vector $\mathbf t^Q [\delta\mathbf x^Q, 1]$. 519*7f296bb3SBarry SmithThe arc length method seeks the intersection of this linearized equilibrium line 520*7f296bb3SBarry Smithwith a quadratic constraint surface, defined by 521*7f296bb3SBarry Smith 522*7f296bb3SBarry Smith% math::L^2 = \|\Delta x\|^2 + \psi^2 (\Delta\lambda)^2, 523*7f296bb3SBarry Smith 524*7f296bb3SBarry Smithwhere $L$ is a user-provided step size corresponding to the radius of the 525*7f296bb3SBarry Smithconstraint surface, $\Delta\mathbf x$ and $\Delta\lambda$ are the 526*7f296bb3SBarry Smithaccumulated updates over the current load step, and $\psi^2$ is a 527*7f296bb3SBarry Smithuser-provided consistency parameter determining the shape of the constraint surface. 528*7f296bb3SBarry SmithGenerally, $\psi^2 > 0$ leads to a hyper-sphere constraint surface, while 529*7f296bb3SBarry Smith$\psi^2 = 0$ leads to a hyper-cylinder constraint surface. 530*7f296bb3SBarry Smith 531*7f296bb3SBarry SmithSince the solution will always fall on the constraint surface, the method will often 532*7f296bb3SBarry Smithrequire multiple incremental steps to fully solve the non-linear problem. 533*7f296bb3SBarry SmithThis is necessary to accurately trace the equilibrium path. 534*7f296bb3SBarry SmithImportantly, this is fundamentally different from time stepping. 535*7f296bb3SBarry SmithWhile a similar process could be implemented as a `TS`, this method is 536*7f296bb3SBarry Smithparticularly designed to be used as a SNES, either standalone or within a `TS`. 537*7f296bb3SBarry Smith 538*7f296bb3SBarry SmithTo this end, by default, the load parameter is used such that the full external 539*7f296bb3SBarry Smithforces are applied at $\lambda = 1$, although we allow for the user to specify 540*7f296bb3SBarry Smitha different value via `-snes_newtonal_lambda_max`. 541*7f296bb3SBarry SmithTo ensure that the solution corresponds exactly to the external force prescribed by 542*7f296bb3SBarry Smiththe user, i.e. that the load parameter is exactly $\lambda_{max}$ at the end 543*7f296bb3SBarry Smithof the SNES solve, we clamp the value before computing the solution update. 544*7f296bb3SBarry SmithAs such, the final increment will likely be a hybrid of arc length continuation and 545*7f296bb3SBarry Smithnormal Newton iterations. 546*7f296bb3SBarry Smith 547*7f296bb3SBarry Smith**Choosing the Continuation Step.** For the first iteration from an equilibrium 548*7f296bb3SBarry Smithpoint, there is a single correct way to choose $\delta\lambda$, which follows 549*7f296bb3SBarry Smithfrom the constraint equations. Specifically the constraint equations yield the 550*7f296bb3SBarry Smithquadratic equation $a\delta\lambda^2 + b\delta\lambda + c = 0$, where 551*7f296bb3SBarry Smith 552*7f296bb3SBarry Smith$$ 553*7f296bb3SBarry Smith\begin{aligned} 554*7f296bb3SBarry Smitha &= \|\delta\mathbf x^Q\|^2 + \psi^2,\\ 555*7f296bb3SBarry Smithb &= 2\delta\mathbf x^Q\cdot (\Delta\mathbf x + \delta s\delta\mathbf x^F) + 2\psi^2 \Delta\lambda,\\ 556*7f296bb3SBarry Smithc &= \|\Delta\mathbf x + \delta s\delta\mathbf x^F\|^2 + \psi^2 \Delta\lambda^2 - L^2. 557*7f296bb3SBarry Smith\end{aligned} 558*7f296bb3SBarry Smith$$ 559*7f296bb3SBarry Smith 560*7f296bb3SBarry SmithSince in the first iteration, $\Delta\mathbf x = \delta\mathbf x^F = \mathbf 0$ and 561*7f296bb3SBarry Smith$\Delta\lambda = 0$, $b = 0$ and the equation simplifies to a pair of 562*7f296bb3SBarry Smithreal roots: 563*7f296bb3SBarry Smith 564*7f296bb3SBarry Smith$$ 565*7f296bb3SBarry Smith\delta\lambda = \pm\frac{L}{\sqrt{\|\delta\mathbf x^Q\|^2 + \psi^2}}, 566*7f296bb3SBarry Smith$$ 567*7f296bb3SBarry Smith 568*7f296bb3SBarry Smithwhere the sign is positive for the first increment and is determined by the previous 569*7f296bb3SBarry Smithincrement otherwise as 570*7f296bb3SBarry Smith 571*7f296bb3SBarry Smith$$ 572*7f296bb3SBarry 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), 573*7f296bb3SBarry Smith$$ 574*7f296bb3SBarry Smith 575*7f296bb3SBarry Smithwhere $(\Delta\mathbf x)_{i-1}$ and $(\Delta\lambda)_{i-1}$ are the 576*7f296bb3SBarry Smithaccumulated updates over the previous load step. 577*7f296bb3SBarry Smith 578*7f296bb3SBarry SmithIn subsequent iterations, there are different approaches to selecting 579*7f296bb3SBarry Smith$\delta\lambda$, all of which have trade-offs. 580*7f296bb3SBarry SmithThe main difference is whether the iterative solution falls on the constraint 581*7f296bb3SBarry Smithsurface at every iteration, or only when fully converged. 582*7f296bb3SBarry SmithThis MR implements one of each of these approaches, set via 583*7f296bb3SBarry Smith`SNESNewtonALSetCorrectionType()` or 584*7f296bb3SBarry Smith`-snes_newtonal_correction_type <normal|exact>` on the command line. 585*7f296bb3SBarry Smith 586*7f296bb3SBarry Smith**Corrections in the Normal Hyperplane.** The `SNES_NEWTONAL_CORRECTION_NORMAL` 587*7f296bb3SBarry Smithoption is simpler and computationally less expensive, but may fail to converge, as 588*7f296bb3SBarry Smiththe constraint equation is not satisfied at every iteration. 589*7f296bb3SBarry SmithThe update $\delta \lambda$ is chosen such that the update is within the 590*7f296bb3SBarry Smithnormal hyper-surface to the quadratic constraint surface. 591*7f296bb3SBarry SmithMathematically, that is 592*7f296bb3SBarry Smith 593*7f296bb3SBarry Smith$$ 594*7f296bb3SBarry Smith\delta \lambda = -\frac{\Delta \mathbf x \cdot \delta \mathbf x^F}{\Delta\mathbf x \cdot \delta\mathbf x^Q + \psi^2 \Delta\lambda}. 595*7f296bb3SBarry Smith$$ 596*7f296bb3SBarry Smith 597*7f296bb3SBarry SmithThis implementation is based on {cite}`LeonPaulinoPereiraMenezesLages_2011`. 598*7f296bb3SBarry Smith 599*7f296bb3SBarry Smith**Exact Corrections.** The `SNES_NEWTONAL_CORRECTION_EXACT` option is far more 600*7f296bb3SBarry Smithcomplex, but ensures that the constraint is exactly satisfied at every Newton 601*7f296bb3SBarry Smithiteration. As such, it is generally more robust. 602*7f296bb3SBarry SmithBy evaluating the intersection of constraint surface and equilibrium line at each 603*7f296bb3SBarry Smithiteration, $\delta\lambda$ is chosen as one of the roots of the above 604*7f296bb3SBarry Smithquadratic equation $a\delta\lambda^2 + b\delta\lambda + c = 0$. 605*7f296bb3SBarry SmithThis method encounters issues, however, if the linearized equilibrium line and 606*7f296bb3SBarry Smithconstraint surface do not intersect due to particularly large linearized error. 607*7f296bb3SBarry SmithIn this case, the roots are complex. 608*7f296bb3SBarry SmithTo continue progressing toward a solution, this method uses a partial correction by 609*7f296bb3SBarry Smithchoosing $\delta s$ such that the quadratic equation has a single real root. 610*7f296bb3SBarry SmithGeometrically, this is selecting the point on the constraint surface closest to the 611*7f296bb3SBarry Smithlinearized equilibrium line. See the code or {cite}`Ritto-CorreaCamotim2008` for a 612*7f296bb3SBarry Smithmathematical description of these partial corrections. 613*7f296bb3SBarry Smith 614*7f296bb3SBarry Smith### Nonlinear Krylov Methods 615*7f296bb3SBarry Smith 616*7f296bb3SBarry SmithA number of nonlinear Krylov methods are provided, including Nonlinear 617*7f296bb3SBarry SmithRichardson (`SNESNRICHARDSON`), nonlinear conjugate gradient (`SNESNCG`), nonlinear GMRES (`SNESNGMRES`), and Anderson Mixing (`SNESANDERSON`). These 618*7f296bb3SBarry Smithmethods are described individually below. They are all instrumental to 619*7f296bb3SBarry SmithPETSc’s nonlinear preconditioning. 620*7f296bb3SBarry Smith 621*7f296bb3SBarry Smith**Nonlinear Richardson.** The nonlinear Richardson iteration, `SNESNRICHARDSON`, merely 622*7f296bb3SBarry Smithtakes the form of a line search-damped fixed-point iteration of the form 623*7f296bb3SBarry Smith 624*7f296bb3SBarry Smith$$ 625*7f296bb3SBarry Smith\mathbf{x}_{k+1} = \mathbf{x}_k - \lambda \mathbf{F}(\mathbf{x}_k), \;\; k=0,1, \ldots, 626*7f296bb3SBarry Smith$$ 627*7f296bb3SBarry Smith 628*7f296bb3SBarry Smithwhere the default linesearch is `SNESLINESEARCHL2`. This simple solver 629*7f296bb3SBarry Smithis mostly useful as a nonlinear smoother, or to provide line search 630*7f296bb3SBarry Smithstabilization to an inner method. 631*7f296bb3SBarry Smith 632*7f296bb3SBarry Smith**Nonlinear Conjugate Gradients.** Nonlinear CG, `SNESNCG`, is equivalent to linear 633*7f296bb3SBarry SmithCG, but with the steplength determined by line search 634*7f296bb3SBarry Smith(`SNESLINESEARCHCP` by default). Five variants (Fletcher-Reed, 635*7f296bb3SBarry SmithHestenes-Steifel, Polak-Ribiere-Polyak, Dai-Yuan, and Conjugate Descent) 636*7f296bb3SBarry Smithare implemented in PETSc and may be chosen using 637*7f296bb3SBarry Smith 638*7f296bb3SBarry Smith``` 639*7f296bb3SBarry SmithSNESNCGSetType(SNES snes, SNESNCGType btype); 640*7f296bb3SBarry Smith``` 641*7f296bb3SBarry Smith 642*7f296bb3SBarry Smith**Anderson Mixing and Nonlinear GMRES Methods.** Nonlinear GMRES (`SNESNGMRES`), and 643*7f296bb3SBarry SmithAnderson Mixing (`SNESANDERSON`) methods combine the last $m$ iterates, plus a new 644*7f296bb3SBarry Smithfixed-point iteration iterate, into an approximate residual-minimizing new iterate. 645*7f296bb3SBarry Smith 646*7f296bb3SBarry SmithAll of the above methods have support for using a nonlinear preconditioner to compute the preliminary update step, rather than the default 647*7f296bb3SBarry 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 648*7f296bb3SBarry Smith`SNES` object that may be obtained with `SNESGetNPC()`. 649*7f296bb3SBarry SmithQuasi-Newton Methods 650*7f296bb3SBarry Smith^^^^^^^^^^^^^^^^^^^^ 651*7f296bb3SBarry Smith 652*7f296bb3SBarry SmithQuasi-Newton methods store iterative rank-one updates to the Jacobian 653*7f296bb3SBarry Smithinstead of computing the Jacobian directly. Three limited-memory quasi-Newton 654*7f296bb3SBarry Smithmethods are provided, L-BFGS, which are described in 655*7f296bb3SBarry SmithTable {any}`tab-qndefaults`. These all are encapsulated under 656*7f296bb3SBarry Smith`-snes_type qn` and may be changed with `snes_qn_type`. The default 657*7f296bb3SBarry Smithis L-BFGS, which provides symmetric updates to an approximate Jacobian. 658*7f296bb3SBarry SmithThis iteration is similar to the line search Newton methods. 659*7f296bb3SBarry Smith 660*7f296bb3SBarry 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 661*7f296bb3SBarry Smith`SNES`, `KSP`, and `PC` options using the options database prefix `-npc_`. 662*7f296bb3SBarry Smith 663*7f296bb3SBarry Smith```{eval-rst} 664*7f296bb3SBarry Smith.. list-table:: PETSc quasi-Newton solvers 665*7f296bb3SBarry Smith :name: tab-qndefaults 666*7f296bb3SBarry Smith :header-rows: 1 667*7f296bb3SBarry Smith 668*7f296bb3SBarry Smith * - QN Method 669*7f296bb3SBarry Smith - ``SNESQNType`` 670*7f296bb3SBarry Smith - Options Name 671*7f296bb3SBarry Smith - Default Line Search 672*7f296bb3SBarry Smith * - L-BFGS 673*7f296bb3SBarry Smith - ``SNES_QN_LBFGS`` 674*7f296bb3SBarry Smith - ``lbfgs`` 675*7f296bb3SBarry Smith - ``SNESLINESEARCHCP`` 676*7f296bb3SBarry Smith * - “Good” Broyden 677*7f296bb3SBarry Smith - ``SNES_QN_BROYDEN`` 678*7f296bb3SBarry Smith - ``broyden`` 679*7f296bb3SBarry Smith - ``SNESLINESEARCHBASIC`` (or equivalently ``SNESLINESEARCHNONE`` 680*7f296bb3SBarry Smith * - “Bad” Broyden 681*7f296bb3SBarry Smith - ``SNES_QN_BADBROYDEN`` 682*7f296bb3SBarry Smith - ``badbroyden`` 683*7f296bb3SBarry Smith - ``SNESLINESEARCHL2`` 684*7f296bb3SBarry Smith``` 685*7f296bb3SBarry Smith 686*7f296bb3SBarry SmithOne may also control the form of the initial Jacobian approximation with 687*7f296bb3SBarry Smith 688*7f296bb3SBarry Smith``` 689*7f296bb3SBarry SmithSNESQNSetScaleType(SNES snes, SNESQNScaleType stype); 690*7f296bb3SBarry Smith``` 691*7f296bb3SBarry Smith 692*7f296bb3SBarry Smithand the restart type with 693*7f296bb3SBarry Smith 694*7f296bb3SBarry Smith``` 695*7f296bb3SBarry SmithSNESQNSetRestartType(SNES snes, SNESQNRestartType rtype); 696*7f296bb3SBarry Smith``` 697*7f296bb3SBarry Smith 698*7f296bb3SBarry Smith### The Full Approximation Scheme 699*7f296bb3SBarry Smith 700*7f296bb3SBarry SmithThe Nonlinear Full Approximation Scheme (FAS) `SNESFAS`, is a nonlinear multigrid method. At 701*7f296bb3SBarry Smitheach level, there is a recursive cycle control `SNES` instance, and 702*7f296bb3SBarry Smitheither one or two nonlinear solvers that act as smoothers (up and down). Problems 703*7f296bb3SBarry Smithset up using the `SNES` `DMDA` interface are automatically 704*7f296bb3SBarry Smithcoarsened. FAS, `SNESFAS`, differs slightly from linear multigrid `PCMG`, in that the hierarchy is 705*7f296bb3SBarry Smithconstructed recursively. However, much of the interface is a one-to-one 706*7f296bb3SBarry Smithmap. We describe the “get” operations here, and it can be assumed that 707*7f296bb3SBarry Smitheach has a corresponding “set” operation. For instance, the number of 708*7f296bb3SBarry Smithlevels in the hierarchy may be retrieved using 709*7f296bb3SBarry Smith 710*7f296bb3SBarry Smith``` 711*7f296bb3SBarry SmithSNESFASGetLevels(SNES snes, PetscInt *levels); 712*7f296bb3SBarry Smith``` 713*7f296bb3SBarry Smith 714*7f296bb3SBarry SmithThere are four `SNESFAS` cycle types, `SNES_FAS_MULTIPLICATIVE`, 715*7f296bb3SBarry Smith`SNES_FAS_ADDITIVE`, `SNES_FAS_FULL`, and `SNES_FAS_KASKADE`. The 716*7f296bb3SBarry Smithtype may be set with 717*7f296bb3SBarry Smith 718*7f296bb3SBarry Smith``` 719*7f296bb3SBarry SmithSNESFASSetType(SNES snes,SNESFASType fastype);. 720*7f296bb3SBarry Smith``` 721*7f296bb3SBarry Smith 722*7f296bb3SBarry Smithand the cycle type, 1 for V, 2 for W, may be set with 723*7f296bb3SBarry Smith 724*7f296bb3SBarry Smith``` 725*7f296bb3SBarry SmithSNESFASSetCycles(SNES snes, PetscInt cycles);. 726*7f296bb3SBarry Smith``` 727*7f296bb3SBarry Smith 728*7f296bb3SBarry SmithMuch like the interface to `PCMG` described in {any}`sec_mg`, there are interfaces to recover the 729*7f296bb3SBarry Smithvarious levels’ cycles and smoothers. The level smoothers may be 730*7f296bb3SBarry Smithaccessed with 731*7f296bb3SBarry Smith 732*7f296bb3SBarry Smith``` 733*7f296bb3SBarry SmithSNESFASGetSmoother(SNES snes, PetscInt level, SNES *smooth); 734*7f296bb3SBarry SmithSNESFASGetSmootherUp(SNES snes, PetscInt level, SNES *smooth); 735*7f296bb3SBarry SmithSNESFASGetSmootherDown(SNES snes, PetscInt level, SNES *smooth); 736*7f296bb3SBarry Smith``` 737*7f296bb3SBarry Smith 738*7f296bb3SBarry Smithand the level cycles with 739*7f296bb3SBarry Smith 740*7f296bb3SBarry Smith``` 741*7f296bb3SBarry SmithSNESFASGetCycleSNES(SNES snes,PetscInt level,SNES *lsnes);. 742*7f296bb3SBarry Smith``` 743*7f296bb3SBarry Smith 744*7f296bb3SBarry SmithAlso akin to `PCMG`, the restriction and prolongation at a level may 745*7f296bb3SBarry Smithbe acquired with 746*7f296bb3SBarry Smith 747*7f296bb3SBarry Smith``` 748*7f296bb3SBarry SmithSNESFASGetInterpolation(SNES snes, PetscInt level, Mat *mat); 749*7f296bb3SBarry SmithSNESFASGetRestriction(SNES snes, PetscInt level, Mat *mat); 750*7f296bb3SBarry Smith``` 751*7f296bb3SBarry Smith 752*7f296bb3SBarry SmithIn addition, FAS requires special restriction for solution-like 753*7f296bb3SBarry Smithvariables, called injection. This may be set with 754*7f296bb3SBarry Smith 755*7f296bb3SBarry Smith``` 756*7f296bb3SBarry SmithSNESFASGetInjection(SNES snes, PetscInt level, Mat *mat);. 757*7f296bb3SBarry Smith``` 758*7f296bb3SBarry Smith 759*7f296bb3SBarry SmithThe coarse solve context may be acquired with 760*7f296bb3SBarry Smith 761*7f296bb3SBarry Smith``` 762*7f296bb3SBarry SmithSNESFASGetCoarseSolve(SNES snes, SNES *smooth); 763*7f296bb3SBarry Smith``` 764*7f296bb3SBarry Smith 765*7f296bb3SBarry Smith### Nonlinear Additive Schwarz 766*7f296bb3SBarry Smith 767*7f296bb3SBarry SmithNonlinear Additive Schwarz methods (NASM) take a number of local 768*7f296bb3SBarry Smithnonlinear subproblems, solves them independently in parallel, and 769*7f296bb3SBarry Smithcombines those solutions into a new approximate solution. 770*7f296bb3SBarry Smith 771*7f296bb3SBarry Smith``` 772*7f296bb3SBarry SmithSNESNASMSetSubdomains(SNES snes,PetscInt n,SNES subsnes[],VecScatter iscatter[],VecScatter oscatter[],VecScatter gscatter[]); 773*7f296bb3SBarry Smith``` 774*7f296bb3SBarry Smith 775*7f296bb3SBarry Smithallows for the user to create these local subdomains. Problems set up 776*7f296bb3SBarry Smithusing the `SNES` `DMDA` interface are automatically decomposed. To 777*7f296bb3SBarry Smithbegin, the type of subdomain updates to the whole solution are limited 778*7f296bb3SBarry Smithto two types borrowed from `PCASM`: `PC_ASM_BASIC`, in which the 779*7f296bb3SBarry Smithoverlapping updates added. `PC_ASM_RESTRICT` updates in a 780*7f296bb3SBarry Smithnonoverlapping fashion. This may be set with 781*7f296bb3SBarry Smith 782*7f296bb3SBarry Smith``` 783*7f296bb3SBarry SmithSNESNASMSetType(SNES snes,PCASMType type);. 784*7f296bb3SBarry Smith``` 785*7f296bb3SBarry Smith 786*7f296bb3SBarry Smith`SNESASPIN` is a helper `SNES` type that sets up a nonlinearly 787*7f296bb3SBarry Smithpreconditioned Newton’s method using NASM as the preconditioner. 788*7f296bb3SBarry Smith 789*7f296bb3SBarry Smith## General Options 790*7f296bb3SBarry Smith 791*7f296bb3SBarry SmithThis section discusses options and routines that apply to all `SNES` 792*7f296bb3SBarry Smithsolvers and problem classes. In particular, we focus on convergence 793*7f296bb3SBarry Smithtests, monitoring routines, and tools for checking derivative 794*7f296bb3SBarry Smithcomputations. 795*7f296bb3SBarry Smith 796*7f296bb3SBarry Smith(sec_snesconvergence)= 797*7f296bb3SBarry Smith 798*7f296bb3SBarry Smith### Convergence Tests 799*7f296bb3SBarry Smith 800*7f296bb3SBarry SmithConvergence of the nonlinear solvers can be detected in a variety of 801*7f296bb3SBarry Smithways; the user can even specify a customized test, as discussed below. 802*7f296bb3SBarry SmithMost of the nonlinear solvers use `SNESConvergenceTestDefault()`, 803*7f296bb3SBarry Smithhowever, `SNESNEWTONTR` uses a method-specific additional convergence 804*7f296bb3SBarry Smithtest as well. The convergence tests involves several parameters, which 805*7f296bb3SBarry Smithare set by default to values that should be reasonable for a wide range 806*7f296bb3SBarry Smithof problems. The user can customize the parameters to the problem at 807*7f296bb3SBarry Smithhand by using some of the following routines and options. 808*7f296bb3SBarry Smith 809*7f296bb3SBarry SmithOne method of convergence testing is to declare convergence when the 810*7f296bb3SBarry Smithnorm of the change in the solution between successive iterations is less 811*7f296bb3SBarry Smiththan some tolerance, `stol`. Convergence can also be determined based 812*7f296bb3SBarry Smithon the norm of the function. Such a test can use either the absolute 813*7f296bb3SBarry Smithsize of the norm, `atol`, or its relative decrease, `rtol`, from an 814*7f296bb3SBarry Smithinitial guess. The following routine sets these parameters, which are 815*7f296bb3SBarry Smithused in many of the default `SNES` convergence tests: 816*7f296bb3SBarry Smith 817*7f296bb3SBarry Smith``` 818*7f296bb3SBarry SmithSNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol, PetscInt its,PetscInt fcts); 819*7f296bb3SBarry Smith``` 820*7f296bb3SBarry Smith 821*7f296bb3SBarry SmithThis routine also sets the maximum numbers of allowable nonlinear 822*7f296bb3SBarry Smithiterations, `its`, and function evaluations, `fcts`. The 823*7f296bb3SBarry Smithcorresponding options database commands for setting these parameters are: 824*7f296bb3SBarry Smith 825*7f296bb3SBarry Smith- `-snes_atol <atol>` 826*7f296bb3SBarry Smith- `-snes_rtol <rtol>` 827*7f296bb3SBarry Smith- `-snes_stol <stol>` 828*7f296bb3SBarry Smith- `-snes_max_it <its>` 829*7f296bb3SBarry Smith- `-snes_max_funcs <fcts>` (use `unlimited` for no maximum) 830*7f296bb3SBarry Smith 831*7f296bb3SBarry SmithA related routine is `SNESGetTolerances()`. `PETSC_CURRENT` may be used 832*7f296bb3SBarry 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. 833*7f296bb3SBarry Smith 834*7f296bb3SBarry SmithUsers can set their own customized convergence tests in `SNES` by 835*7f296bb3SBarry Smithusing the command 836*7f296bb3SBarry Smith 837*7f296bb3SBarry Smith``` 838*7f296bb3SBarry SmithSNESSetConvergenceTest(SNES snes,PetscErrorCode (*test)(SNES snes,PetscInt it,PetscReal xnorm, PetscReal gnorm,PetscReal f,SNESConvergedReason reason, void *cctx),void *cctx,PetscErrorCode (*destroy)(void *cctx)); 839*7f296bb3SBarry Smith``` 840*7f296bb3SBarry Smith 841*7f296bb3SBarry SmithThe final argument of the convergence test routine, `cctx`, denotes an 842*7f296bb3SBarry Smithoptional user-defined context for private data. When solving systems of 843*7f296bb3SBarry Smithnonlinear equations, the arguments `xnorm`, `gnorm`, and `f` are 844*7f296bb3SBarry Smiththe current iterate norm, current step norm, and function norm, 845*7f296bb3SBarry Smithrespectively. `SNESConvergedReason` should be set positive for 846*7f296bb3SBarry Smithconvergence and negative for divergence. See `include/petscsnes.h` for 847*7f296bb3SBarry Smitha list of values for `SNESConvergedReason`. 848*7f296bb3SBarry Smith 849*7f296bb3SBarry Smith(sec_snesmonitor)= 850*7f296bb3SBarry Smith 851*7f296bb3SBarry Smith### Convergence Monitoring 852*7f296bb3SBarry Smith 853*7f296bb3SBarry SmithBy default the `SNES` solvers run silently without displaying 854*7f296bb3SBarry Smithinformation about the iterations. The user can initiate monitoring with 855*7f296bb3SBarry Smiththe command 856*7f296bb3SBarry Smith 857*7f296bb3SBarry Smith``` 858*7f296bb3SBarry SmithSNESMonitorSet(SNES snes, PetscErrorCode (*mon)(SNES snes, PetscInt its, PetscReal norm, void* mctx), void *mctx, (PetscCtxDestroyFn *)*monitordestroy); 859*7f296bb3SBarry Smith``` 860*7f296bb3SBarry Smith 861*7f296bb3SBarry SmithThe routine, `mon`, indicates a user-defined monitoring routine, where 862*7f296bb3SBarry Smith`its` and `mctx` respectively denote the iteration number and an 863*7f296bb3SBarry Smithoptional user-defined context for private data for the monitor routine. 864*7f296bb3SBarry SmithThe argument `norm` is the function norm. 865*7f296bb3SBarry Smith 866*7f296bb3SBarry SmithThe routine set by `SNESMonitorSet()` is called once after every 867*7f296bb3SBarry Smithsuccessful step computation within the nonlinear solver. Hence, the user 868*7f296bb3SBarry Smithcan employ this routine for any application-specific computations that 869*7f296bb3SBarry Smithshould be done after the solution update. The option `-snes_monitor` 870*7f296bb3SBarry Smithactivates the default `SNES` monitor routine, 871*7f296bb3SBarry Smith`SNESMonitorDefault()`, while `-snes_monitor_lg_residualnorm` draws 872*7f296bb3SBarry Smitha simple line graph of the residual norm’s convergence. 873*7f296bb3SBarry Smith 874*7f296bb3SBarry SmithOne can cancel hardwired monitoring routines for `SNES` at runtime 875*7f296bb3SBarry Smithwith `-snes_monitor_cancel`. 876*7f296bb3SBarry Smith 877*7f296bb3SBarry SmithAs the Newton method converges so that the residual norm is small, say 878*7f296bb3SBarry Smith$10^{-10}$, many of the final digits printed with the 879*7f296bb3SBarry Smith`-snes_monitor` option are meaningless. Worse, they are different on 880*7f296bb3SBarry Smithdifferent machines; due to different round-off rules used by, say, the 881*7f296bb3SBarry SmithIBM RS6000 and the Sun SPARC. This makes testing between different 882*7f296bb3SBarry Smithmachines difficult. The option `-snes_monitor_short` causes PETSc to 883*7f296bb3SBarry Smithprint fewer of the digits of the residual norm as it gets smaller; thus 884*7f296bb3SBarry Smithon most of the machines it will always print the same numbers making 885*7f296bb3SBarry Smithcross-process testing easier. 886*7f296bb3SBarry Smith 887*7f296bb3SBarry SmithThe routines 888*7f296bb3SBarry Smith 889*7f296bb3SBarry Smith``` 890*7f296bb3SBarry SmithSNESGetSolution(SNES snes,Vec *x); 891*7f296bb3SBarry SmithSNESGetFunction(SNES snes,Vec *r,void *ctx,int(**func)(SNES,Vec,Vec,void*)); 892*7f296bb3SBarry Smith``` 893*7f296bb3SBarry Smith 894*7f296bb3SBarry Smithreturn the solution vector and function vector from a `SNES` context. 895*7f296bb3SBarry SmithThese routines are useful, for instance, if the convergence test 896*7f296bb3SBarry Smithrequires some property of the solution or function other than those 897*7f296bb3SBarry Smithpassed with routine arguments. 898*7f296bb3SBarry Smith 899*7f296bb3SBarry Smith(sec_snesderivs)= 900*7f296bb3SBarry Smith 901*7f296bb3SBarry Smith### Checking Accuracy of Derivatives 902*7f296bb3SBarry Smith 903*7f296bb3SBarry SmithSince hand-coding routines for Jacobian matrix evaluation can be error 904*7f296bb3SBarry Smithprone, `SNES` provides easy-to-use support for checking these matrices 905*7f296bb3SBarry Smithagainst finite difference versions. In the simplest form of comparison, 906*7f296bb3SBarry Smithusers can employ the option `-snes_test_jacobian` to compare the 907*7f296bb3SBarry Smithmatrices at several points. Although not exhaustive, this test will 908*7f296bb3SBarry Smithgenerally catch obvious problems. One can compare the elements of the 909*7f296bb3SBarry Smithtwo matrices by using the option `-snes_test_jacobian_view` , which 910*7f296bb3SBarry Smithcauses the two matrices to be printed to the screen. 911*7f296bb3SBarry Smith 912*7f296bb3SBarry SmithAnother means for verifying the correctness of a code for Jacobian 913*7f296bb3SBarry Smithcomputation is running the problem with either the finite difference or 914*7f296bb3SBarry Smithmatrix-free variant, `-snes_fd` or `-snes_mf`; see {any}`sec_fdmatrix` or {any}`sec_nlmatrixfree`. 915*7f296bb3SBarry SmithIf a 916*7f296bb3SBarry Smithproblem converges well with these matrix approximations but not with a 917*7f296bb3SBarry Smithuser-provided routine, the problem probably lies with the hand-coded 918*7f296bb3SBarry Smithmatrix. See the note in {any}`sec_snesjacobian` about 919*7f296bb3SBarry Smithassembling your Jabobian in the "preconditioner" slot `Pmat`. 920*7f296bb3SBarry Smith 921*7f296bb3SBarry SmithThe correctness of user provided `MATSHELL` Jacobians in general can be 922*7f296bb3SBarry Smithchecked with `MatShellTestMultTranspose()` and `MatShellTestMult()`. 923*7f296bb3SBarry Smith 924*7f296bb3SBarry SmithThe correctness of user provided `MATSHELL` Jacobians via `TSSetRHSJacobian()` 925*7f296bb3SBarry Smithcan be checked with `TSRHSJacobianTestTranspose()` and `TSRHSJacobianTest()` 926*7f296bb3SBarry Smiththat check the correction of the matrix-transpose vector product and the 927*7f296bb3SBarry Smithmatrix-product. From the command line, these can be checked with 928*7f296bb3SBarry Smith 929*7f296bb3SBarry Smith- `-ts_rhs_jacobian_test_mult_transpose` 930*7f296bb3SBarry Smith- `-mat_shell_test_mult_transpose_view` 931*7f296bb3SBarry Smith- `-ts_rhs_jacobian_test_mult` 932*7f296bb3SBarry Smith- `-mat_shell_test_mult_view` 933*7f296bb3SBarry Smith 934*7f296bb3SBarry Smith## Inexact Newton-like Methods 935*7f296bb3SBarry Smith 936*7f296bb3SBarry SmithSince exact solution of the linear Newton systems within {math:numref}`newton` 937*7f296bb3SBarry Smithat each iteration can be costly, modifications 938*7f296bb3SBarry Smithare often introduced that significantly reduce these expenses and yet 939*7f296bb3SBarry Smithretain the rapid convergence of Newton’s method. Inexact or truncated 940*7f296bb3SBarry SmithNewton techniques approximately solve the linear systems using an 941*7f296bb3SBarry Smithiterative scheme. In comparison with using direct methods for solving 942*7f296bb3SBarry Smiththe Newton systems, iterative methods have the virtue of requiring 943*7f296bb3SBarry Smithlittle space for matrix storage and potentially saving significant 944*7f296bb3SBarry Smithcomputational work. Within the class of inexact Newton methods, of 945*7f296bb3SBarry Smithparticular interest are Newton-Krylov methods, where the subsidiary 946*7f296bb3SBarry Smithiterative technique for solving the Newton system is chosen from the 947*7f296bb3SBarry Smithclass of Krylov subspace projection methods. Note that at runtime the 948*7f296bb3SBarry Smithuser can set any of the linear solver options discussed in {any}`ch_ksp`, 949*7f296bb3SBarry Smithsuch as `-ksp_type <ksp_method>` and 950*7f296bb3SBarry Smith`-pc_type <pc_method>`, to set the Krylov subspace and preconditioner 951*7f296bb3SBarry Smithmethods. 952*7f296bb3SBarry Smith 953*7f296bb3SBarry SmithTwo levels of iterations occur for the inexact techniques, where during 954*7f296bb3SBarry Smitheach global or outer Newton iteration a sequence of subsidiary inner 955*7f296bb3SBarry Smithiterations of a linear solver is performed. Appropriate control of the 956*7f296bb3SBarry Smithaccuracy to which the subsidiary iterative method solves the Newton 957*7f296bb3SBarry Smithsystem at each global iteration is critical, since these inner 958*7f296bb3SBarry Smithiterations determine the asymptotic convergence rate for inexact Newton 959*7f296bb3SBarry Smithtechniques. While the Newton systems must be solved well enough to 960*7f296bb3SBarry Smithretain fast local convergence of the Newton’s iterates, use of excessive 961*7f296bb3SBarry Smithinner iterations, particularly when $\| \mathbf{x}_k - \mathbf{x}_* \|$ is large, 962*7f296bb3SBarry Smithis neither necessary nor economical. Thus, the number of required inner 963*7f296bb3SBarry Smithiterations typically increases as the Newton process progresses, so that 964*7f296bb3SBarry Smiththe truncated iterates approach the true Newton iterates. 965*7f296bb3SBarry Smith 966*7f296bb3SBarry SmithA sequence of nonnegative numbers $\{\eta_k\}$ can be used to 967*7f296bb3SBarry Smithindicate the variable convergence criterion. In this case, when solving 968*7f296bb3SBarry Smitha system of nonlinear equations, the update step of the Newton process 969*7f296bb3SBarry Smithremains unchanged, and direct solution of the linear system is replaced 970*7f296bb3SBarry Smithby iteration on the system until the residuals 971*7f296bb3SBarry Smith 972*7f296bb3SBarry Smith$$ 973*7f296bb3SBarry Smith\mathbf{r}_k^{(i)} = \mathbf{F}'(\mathbf{x}_k) \Delta \mathbf{x}_k + \mathbf{F}(\mathbf{x}_k) 974*7f296bb3SBarry Smith$$ 975*7f296bb3SBarry Smith 976*7f296bb3SBarry Smithsatisfy 977*7f296bb3SBarry Smith 978*7f296bb3SBarry Smith$$ 979*7f296bb3SBarry Smith\frac{ \| \mathbf{r}_k^{(i)} \| }{ \| \mathbf{F}(\mathbf{x}_k) \| } \leq \eta_k \leq \eta < 1. 980*7f296bb3SBarry Smith$$ 981*7f296bb3SBarry Smith 982*7f296bb3SBarry SmithHere $\mathbf{x}_0$ is an initial approximation of the solution, and 983*7f296bb3SBarry Smith$\| \cdot \|$ denotes an arbitrary norm in $\Re^n$ . 984*7f296bb3SBarry Smith 985*7f296bb3SBarry SmithBy default a constant relative convergence tolerance is used for solving 986*7f296bb3SBarry Smiththe subsidiary linear systems within the Newton-like methods of 987*7f296bb3SBarry Smith`SNES`. When solving a system of nonlinear equations, one can instead 988*7f296bb3SBarry Smithemploy the techniques of Eisenstat and Walker {cite}`ew96` 989*7f296bb3SBarry Smithto compute $\eta_k$ at each step of the nonlinear solver by using 990*7f296bb3SBarry Smiththe option `-snes_ksp_ew` . In addition, by adding one’s own 991*7f296bb3SBarry Smith`KSP` convergence test (see {any}`sec_convergencetests`), one can easily create one’s own, 992*7f296bb3SBarry Smithproblem-dependent, inner convergence tests. 993*7f296bb3SBarry Smith 994*7f296bb3SBarry Smith(sec_nlmatrixfree)= 995*7f296bb3SBarry Smith 996*7f296bb3SBarry Smith## Matrix-Free Methods 997*7f296bb3SBarry Smith 998*7f296bb3SBarry SmithThe `SNES` class fully supports matrix-free methods. The matrices 999*7f296bb3SBarry Smithspecified in the Jacobian evaluation routine need not be conventional 1000*7f296bb3SBarry Smithmatrices; instead, they can point to the data required to implement a 1001*7f296bb3SBarry Smithparticular matrix-free method. The matrix-free variant is allowed *only* 1002*7f296bb3SBarry Smithwhen the linear systems are solved by an iterative method in combination 1003*7f296bb3SBarry Smithwith no preconditioning (`PCNONE` or `-pc_type` `none`), a 1004*7f296bb3SBarry Smithuser-provided preconditioner matrix, or a user-provided preconditioner 1005*7f296bb3SBarry Smithshell (`PCSHELL`, discussed in {any}`sec_pc`); that 1006*7f296bb3SBarry Smithis, obviously matrix-free methods cannot be used with a direct solver, 1007*7f296bb3SBarry Smithapproximate factorization, or other preconditioner which requires access 1008*7f296bb3SBarry Smithto explicit matrix entries. 1009*7f296bb3SBarry Smith 1010*7f296bb3SBarry SmithThe user can create a matrix-free context for use within `SNES` with 1011*7f296bb3SBarry Smiththe routine 1012*7f296bb3SBarry Smith 1013*7f296bb3SBarry Smith``` 1014*7f296bb3SBarry SmithMatCreateSNESMF(SNES snes,Mat *mat); 1015*7f296bb3SBarry Smith``` 1016*7f296bb3SBarry Smith 1017*7f296bb3SBarry SmithThis routine creates the data structures needed for the matrix-vector 1018*7f296bb3SBarry Smithproducts that arise within Krylov space iterative 1019*7f296bb3SBarry Smithmethods {cite}`brownsaad:90`. 1020*7f296bb3SBarry SmithThe default `SNES` 1021*7f296bb3SBarry Smithmatrix-free approximations can also be invoked with the command 1022*7f296bb3SBarry Smith`-snes_mf`. Or, one can retain the user-provided Jacobian 1023*7f296bb3SBarry Smithpreconditioner, but replace the user-provided Jacobian matrix with the 1024*7f296bb3SBarry Smithdefault matrix-free variant with the option `-snes_mf_operator`. 1025*7f296bb3SBarry Smith 1026*7f296bb3SBarry Smith`MatCreateSNESMF()` uses 1027*7f296bb3SBarry Smith 1028*7f296bb3SBarry Smith``` 1029*7f296bb3SBarry SmithMatCreateMFFD(Vec x, Mat *mat); 1030*7f296bb3SBarry Smith``` 1031*7f296bb3SBarry Smith 1032*7f296bb3SBarry Smithwhich can also be used directly for users who need a matrix-free matrix but are not using `SNES`. 1033*7f296bb3SBarry Smith 1034*7f296bb3SBarry SmithThe user can set one parameter to control the Jacobian-vector product 1035*7f296bb3SBarry Smithapproximation with the command 1036*7f296bb3SBarry Smith 1037*7f296bb3SBarry Smith``` 1038*7f296bb3SBarry SmithMatMFFDSetFunctionError(Mat mat,PetscReal rerror); 1039*7f296bb3SBarry Smith``` 1040*7f296bb3SBarry Smith 1041*7f296bb3SBarry SmithThe parameter `rerror` should be set to the square root of the 1042*7f296bb3SBarry Smithrelative error in the function evaluations, $e_{rel}$; the default 1043*7f296bb3SBarry Smithis the square root of machine epsilon (about $10^{-8}$ in double 1044*7f296bb3SBarry Smithprecision), which assumes that the functions are evaluated to full 1045*7f296bb3SBarry Smithfloating-point precision accuracy. This parameter can also be set from 1046*7f296bb3SBarry Smiththe options database with `-mat_mffd_err <err>` 1047*7f296bb3SBarry Smith 1048*7f296bb3SBarry SmithIn addition, PETSc provides ways to register new routines to compute 1049*7f296bb3SBarry Smiththe differencing parameter ($h$); see the manual page for 1050*7f296bb3SBarry Smith`MatMFFDSetType()` and `MatMFFDRegister()`. We currently provide two 1051*7f296bb3SBarry Smithdefault routines accessible via `-mat_mffd_type <ds or wp>`. For 1052*7f296bb3SBarry Smiththe default approach there is one “tuning” parameter, set with 1053*7f296bb3SBarry Smith 1054*7f296bb3SBarry Smith``` 1055*7f296bb3SBarry SmithMatMFFDDSSetUmin(Mat mat,PetscReal umin); 1056*7f296bb3SBarry Smith``` 1057*7f296bb3SBarry Smith 1058*7f296bb3SBarry SmithThis parameter, `umin` (or $u_{min}$), is a bit involved; its 1059*7f296bb3SBarry Smithdefault is $10^{-6}$ . Its command line form is `-mat_mffd_umin <umin>`. 1060*7f296bb3SBarry Smith 1061*7f296bb3SBarry SmithThe Jacobian-vector product is approximated 1062*7f296bb3SBarry Smithvia the formula 1063*7f296bb3SBarry Smith 1064*7f296bb3SBarry Smith$$ 1065*7f296bb3SBarry SmithF'(u) a \approx \frac{F(u + h*a) - F(u)}{h} 1066*7f296bb3SBarry Smith$$ 1067*7f296bb3SBarry Smith 1068*7f296bb3SBarry Smithwhere $h$ is computed via 1069*7f296bb3SBarry Smith 1070*7f296bb3SBarry Smith$$ 1071*7f296bb3SBarry Smithh = e_{\text{rel}} \cdot \begin{cases} 1072*7f296bb3SBarry Smithu^{T}a/\lVert a \rVert^2_2 & \text{if $|u^T a| > u_{\min} \lVert a \rVert_{1}$} \\ 1073*7f296bb3SBarry Smithu_{\min} \operatorname{sign}(u^{T}a) \lVert a \rVert_{1}/\lVert a\rVert^2_2 & \text{otherwise}. 1074*7f296bb3SBarry Smith\end{cases} 1075*7f296bb3SBarry Smith$$ 1076*7f296bb3SBarry Smith 1077*7f296bb3SBarry SmithThis approach is taken from Brown and Saad 1078*7f296bb3SBarry Smith{cite}`brownsaad:90`. The second approach, taken from Walker and Pernice, 1079*7f296bb3SBarry Smith{cite}`pw98`, computes $h$ via 1080*7f296bb3SBarry Smith 1081*7f296bb3SBarry Smith$$ 1082*7f296bb3SBarry Smith\begin{aligned} 1083*7f296bb3SBarry Smith h = \frac{\sqrt{1 + ||u||}e_{rel}}{||a||}\end{aligned} 1084*7f296bb3SBarry Smith$$ 1085*7f296bb3SBarry Smith 1086*7f296bb3SBarry SmithThis has no tunable parameters, but note that inside the nonlinear solve 1087*7f296bb3SBarry Smithfor the entire *linear* iterative process $u$ does not change 1088*7f296bb3SBarry Smithhence $\sqrt{1 + ||u||}$ need be computed only once. This 1089*7f296bb3SBarry Smithinformation may be set with the options 1090*7f296bb3SBarry Smith 1091*7f296bb3SBarry Smith``` 1092*7f296bb3SBarry SmithMatMFFDWPSetComputeNormU(Mat mat,PetscBool ); 1093*7f296bb3SBarry Smith``` 1094*7f296bb3SBarry Smith 1095*7f296bb3SBarry Smithor `-mat_mffd_compute_normu <true or false>`. This information is used 1096*7f296bb3SBarry Smithto eliminate the redundant computation of these parameters, therefore 1097*7f296bb3SBarry Smithreducing the number of collective operations and improving the 1098*7f296bb3SBarry Smithefficiency of the application code. This takes place automatically for the PETSc GMRES solver with left preconditioning. 1099*7f296bb3SBarry Smith 1100*7f296bb3SBarry SmithIt is also possible to monitor the differencing parameters h that are 1101*7f296bb3SBarry Smithcomputed via the routines 1102*7f296bb3SBarry Smith 1103*7f296bb3SBarry Smith``` 1104*7f296bb3SBarry SmithMatMFFDSetHHistory(Mat,PetscScalar *,int); 1105*7f296bb3SBarry SmithMatMFFDResetHHistory(Mat,PetscScalar *,int); 1106*7f296bb3SBarry SmithMatMFFDGetH(Mat,PetscScalar *); 1107*7f296bb3SBarry Smith``` 1108*7f296bb3SBarry Smith 1109*7f296bb3SBarry SmithWe include an explicit example of using matrix-free methods in {any}`ex3.c <snes_ex3>`. 1110*7f296bb3SBarry SmithNote that by using the option `-snes_mf` one can 1111*7f296bb3SBarry Smitheasily convert any `SNES` code to use a matrix-free Newton-Krylov 1112*7f296bb3SBarry Smithmethod without a preconditioner. As shown in this example, 1113*7f296bb3SBarry Smith`SNESSetFromOptions()` must be called *after* `SNESSetJacobian()` to 1114*7f296bb3SBarry Smithenable runtime switching between the user-specified Jacobian and the 1115*7f296bb3SBarry Smithdefault `SNES` matrix-free form. 1116*7f296bb3SBarry Smith 1117*7f296bb3SBarry Smith(snes_ex3)= 1118*7f296bb3SBarry Smith 1119*7f296bb3SBarry Smith:::{admonition} Listing: `src/snes/tutorials/ex3.c` 1120*7f296bb3SBarry Smith```{literalinclude} /../src/snes/tutorials/ex3.c 1121*7f296bb3SBarry Smith:end-before: /*TEST 1122*7f296bb3SBarry Smith``` 1123*7f296bb3SBarry Smith::: 1124*7f296bb3SBarry Smith 1125*7f296bb3SBarry SmithTable {any}`tab-jacobians` summarizes the various matrix situations 1126*7f296bb3SBarry Smiththat `SNES` supports. In particular, different linear system matrices 1127*7f296bb3SBarry Smithand preconditioning matrices are allowed, as well as both matrix-free 1128*7f296bb3SBarry Smithand application-provided preconditioners. If {any}`ex3.c <snes_ex3>` is run with 1129*7f296bb3SBarry Smiththe options `-snes_mf` and `-user_precond` then it uses a 1130*7f296bb3SBarry Smithmatrix-free application of the matrix-vector multiple and a user 1131*7f296bb3SBarry Smithprovided matrix-free Jacobian. 1132*7f296bb3SBarry Smith 1133*7f296bb3SBarry Smith```{eval-rst} 1134*7f296bb3SBarry Smith.. list-table:: Jacobian Options 1135*7f296bb3SBarry Smith :name: tab-jacobians 1136*7f296bb3SBarry Smith 1137*7f296bb3SBarry Smith * - Matrix Use 1138*7f296bb3SBarry Smith - Conventional Matrix Formats 1139*7f296bb3SBarry Smith - Matrix-free versions 1140*7f296bb3SBarry Smith * - Jacobian Matrix 1141*7f296bb3SBarry Smith - Create matrix with ``MatCreate()``:math:`^*`. Assemble matrix with user-defined routine :math:`^\dagger` 1142*7f296bb3SBarry Smith - Create matrix with ``MatCreateShell()``. Use ``MatShellSetOperation()`` to set various matrix actions, or use ``MatCreateMFFD()`` or ``MatCreateSNESMF()``. 1143*7f296bb3SBarry Smith * - Preconditioning Matrix 1144*7f296bb3SBarry Smith - Create matrix with ``MatCreate()``:math:`^*`. Assemble matrix with user-defined routine :math:`^\dagger` 1145*7f296bb3SBarry Smith - Use ``SNESGetKSP()`` and ``KSPGetPC()`` to access the ``PC``, then use ``PCSetType(pc, PCSHELL)`` followed by ``PCShellSetApply()``. 1146*7f296bb3SBarry Smith``` 1147*7f296bb3SBarry Smith 1148*7f296bb3SBarry Smith$^*$ Use either the generic `MatCreate()` or a format-specific variant such as `MatCreateAIJ()`. 1149*7f296bb3SBarry Smith 1150*7f296bb3SBarry Smith$^\dagger$ Set user-defined matrix formation routine with `SNESSetJacobian()` or with a `DM` variant such as `DMDASNESSetJacobianLocal()` 1151*7f296bb3SBarry Smith 1152*7f296bb3SBarry SmithSNES also provides some less well-integrated code to apply matrix-free finite differencing using an automatically computed measurement of the 1153*7f296bb3SBarry 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 1154*7f296bb3SBarry Smith`-snes_mf_` instead of `-mat_mffd_`. Note that this alternative prefix **only** works for version 2 differencing. 1155*7f296bb3SBarry Smith 1156*7f296bb3SBarry Smith(sec_fdmatrix)= 1157*7f296bb3SBarry Smith 1158*7f296bb3SBarry Smith## Finite Difference Jacobian Approximations 1159*7f296bb3SBarry Smith 1160*7f296bb3SBarry SmithPETSc provides some tools to help approximate the Jacobian matrices 1161*7f296bb3SBarry Smithefficiently via finite differences. These tools are intended for use in 1162*7f296bb3SBarry Smithcertain situations where one is unable to compute Jacobian matrices 1163*7f296bb3SBarry Smithanalytically, and matrix-free methods do not work well without a 1164*7f296bb3SBarry Smithpreconditioner, due to very poor conditioning. The approximation 1165*7f296bb3SBarry Smithrequires several steps: 1166*7f296bb3SBarry Smith 1167*7f296bb3SBarry Smith- First, one colors the columns of the (not yet built) Jacobian matrix, 1168*7f296bb3SBarry Smith so that columns of the same color do not share any common rows. 1169*7f296bb3SBarry Smith- Next, one creates a `MatFDColoring` data structure that will be 1170*7f296bb3SBarry Smith used later in actually computing the Jacobian. 1171*7f296bb3SBarry Smith- Finally, one tells the nonlinear solvers of `SNES` to use the 1172*7f296bb3SBarry Smith `SNESComputeJacobianDefaultColor()` routine to compute the 1173*7f296bb3SBarry Smith Jacobians. 1174*7f296bb3SBarry Smith 1175*7f296bb3SBarry SmithA code fragment that demonstrates this process is given below. 1176*7f296bb3SBarry Smith 1177*7f296bb3SBarry Smith``` 1178*7f296bb3SBarry SmithISColoring iscoloring; 1179*7f296bb3SBarry SmithMatFDColoring fdcoloring; 1180*7f296bb3SBarry SmithMatColoring coloring; 1181*7f296bb3SBarry Smith 1182*7f296bb3SBarry Smith/* 1183*7f296bb3SBarry Smith This initializes the nonzero structure of the Jacobian. This is artificial 1184*7f296bb3SBarry Smith because clearly if we had a routine to compute the Jacobian we wouldn't 1185*7f296bb3SBarry Smith need to use finite differences. 1186*7f296bb3SBarry Smith*/ 1187*7f296bb3SBarry SmithFormJacobian(snes,x, &J, &J, &user); 1188*7f296bb3SBarry Smith 1189*7f296bb3SBarry Smith/* 1190*7f296bb3SBarry Smith Color the matrix, i.e. determine groups of columns that share no common 1191*7f296bb3SBarry Smith rows. These columns in the Jacobian can all be computed simultaneously. 1192*7f296bb3SBarry Smith*/ 1193*7f296bb3SBarry SmithMatColoringCreate(J, &coloring); 1194*7f296bb3SBarry SmithMatColoringSetType(coloring,MATCOLORINGSL); 1195*7f296bb3SBarry SmithMatColoringSetFromOptions(coloring); 1196*7f296bb3SBarry SmithMatColoringApply(coloring, &iscoloring); 1197*7f296bb3SBarry SmithMatColoringDestroy(&coloring); 1198*7f296bb3SBarry Smith/* 1199*7f296bb3SBarry Smith Create the data structure that SNESComputeJacobianDefaultColor() uses 1200*7f296bb3SBarry Smith to compute the actual Jacobians via finite differences. 1201*7f296bb3SBarry Smith*/ 1202*7f296bb3SBarry SmithMatFDColoringCreate(J,iscoloring, &fdcoloring); 1203*7f296bb3SBarry SmithISColoringDestroy(&iscoloring); 1204*7f296bb3SBarry SmithMatFDColoringSetFunction(fdcoloring,(PetscErrorCode (*)(void))FormFunction, &user); 1205*7f296bb3SBarry SmithMatFDColoringSetFromOptions(fdcoloring); 1206*7f296bb3SBarry Smith 1207*7f296bb3SBarry Smith/* 1208*7f296bb3SBarry Smith Tell SNES to use the routine SNESComputeJacobianDefaultColor() 1209*7f296bb3SBarry Smith to compute Jacobians. 1210*7f296bb3SBarry Smith*/ 1211*7f296bb3SBarry SmithSNESSetJacobian(snes,J,J,SNESComputeJacobianDefaultColor,fdcoloring); 1212*7f296bb3SBarry Smith``` 1213*7f296bb3SBarry Smith 1214*7f296bb3SBarry SmithOf course, we are cheating a bit. If we do not have an analytic formula 1215*7f296bb3SBarry Smithfor computing the Jacobian, then how do we know what its nonzero 1216*7f296bb3SBarry Smithstructure is so that it may be colored? Determining the structure is 1217*7f296bb3SBarry Smithproblem dependent, but fortunately, for most structured grid problems 1218*7f296bb3SBarry Smith(the class of problems for which PETSc was originally designed) if one 1219*7f296bb3SBarry Smithknows the stencil used for the nonlinear function one can usually fairly 1220*7f296bb3SBarry Smitheasily obtain an estimate of the location of nonzeros in the matrix. 1221*7f296bb3SBarry 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. 1222*7f296bb3SBarry SmithIf using `DMPlex` ({any}`ch_unstructured`) for unstructured meshes, the nonzero locations will be identified in `DMCreateMatrix()` and the procedure above can be used. 1223*7f296bb3SBarry SmithMost external packages for unstructured meshes have similar functionality. 1224*7f296bb3SBarry Smith 1225*7f296bb3SBarry SmithOne need not necessarily use a `MatColoring` object to determine a 1226*7f296bb3SBarry Smithcoloring. For example, if a grid can be colored directly (without using 1227*7f296bb3SBarry Smiththe associated matrix), then that coloring can be provided to 1228*7f296bb3SBarry Smith`MatFDColoringCreate()`. Note that the user must always preset the 1229*7f296bb3SBarry Smithnonzero structure in the matrix regardless of which coloring routine is 1230*7f296bb3SBarry Smithused. 1231*7f296bb3SBarry Smith 1232*7f296bb3SBarry SmithPETSc provides the following coloring algorithms, which can be selected using `MatColoringSetType()` or via the command line argument `-mat_coloring_type`. 1233*7f296bb3SBarry Smith 1234*7f296bb3SBarry Smith```{eval-rst} 1235*7f296bb3SBarry Smith.. list-table:: 1236*7f296bb3SBarry Smith :header-rows: 1 1237*7f296bb3SBarry Smith 1238*7f296bb3SBarry Smith * - Algorithm 1239*7f296bb3SBarry Smith - ``MatColoringType`` 1240*7f296bb3SBarry Smith - ``-mat_coloring_type`` 1241*7f296bb3SBarry Smith - Parallel 1242*7f296bb3SBarry Smith * - smallest-last :cite:`more84` 1243*7f296bb3SBarry Smith - ``MATCOLORINGSL`` 1244*7f296bb3SBarry Smith - ``sl`` 1245*7f296bb3SBarry Smith - No 1246*7f296bb3SBarry Smith * - largest-first :cite:`more84` 1247*7f296bb3SBarry Smith - ``MATCOLORINGLF`` 1248*7f296bb3SBarry Smith - ``lf`` 1249*7f296bb3SBarry Smith - No 1250*7f296bb3SBarry Smith * - incidence-degree :cite:`more84` 1251*7f296bb3SBarry Smith - ``MATCOLORINGID`` 1252*7f296bb3SBarry Smith - ``id`` 1253*7f296bb3SBarry Smith - No 1254*7f296bb3SBarry Smith * - Jones-Plassmann :cite:`jp:pcolor` 1255*7f296bb3SBarry Smith - ``MATCOLORINGJP`` 1256*7f296bb3SBarry Smith - ``jp`` 1257*7f296bb3SBarry Smith - Yes 1258*7f296bb3SBarry Smith * - Greedy 1259*7f296bb3SBarry Smith - ``MATCOLORINGGREEDY`` 1260*7f296bb3SBarry Smith - ``greedy`` 1261*7f296bb3SBarry Smith - Yes 1262*7f296bb3SBarry Smith * - Natural (1 color per column) 1263*7f296bb3SBarry Smith - ``MATCOLORINGNATURAL`` 1264*7f296bb3SBarry Smith - ``natural`` 1265*7f296bb3SBarry Smith - Yes 1266*7f296bb3SBarry Smith * - Power (:math:`A^k` followed by 1-coloring) 1267*7f296bb3SBarry Smith - ``MATCOLORINGPOWER`` 1268*7f296bb3SBarry Smith - ``power`` 1269*7f296bb3SBarry Smith - Yes 1270*7f296bb3SBarry Smith``` 1271*7f296bb3SBarry Smith 1272*7f296bb3SBarry SmithAs for the matrix-free computation of Jacobians ({any}`sec_nlmatrixfree`), two parameters affect the accuracy of the 1273*7f296bb3SBarry Smithfinite difference Jacobian approximation. These are set with the command 1274*7f296bb3SBarry Smith 1275*7f296bb3SBarry Smith``` 1276*7f296bb3SBarry SmithMatFDColoringSetParameters(MatFDColoring fdcoloring,PetscReal rerror,PetscReal umin); 1277*7f296bb3SBarry Smith``` 1278*7f296bb3SBarry Smith 1279*7f296bb3SBarry SmithThe parameter `rerror` is the square root of the relative error in the 1280*7f296bb3SBarry Smithfunction evaluations, $e_{rel}$; the default is the square root of 1281*7f296bb3SBarry Smithmachine epsilon (about $10^{-8}$ in double precision), which 1282*7f296bb3SBarry Smithassumes that the functions are evaluated approximately to floating-point 1283*7f296bb3SBarry Smithprecision accuracy. The second parameter, `umin`, is a bit more 1284*7f296bb3SBarry Smithinvolved; its default is $10e^{-6}$ . Column $i$ of the 1285*7f296bb3SBarry SmithJacobian matrix (denoted by $F_{:i}$) is approximated by the 1286*7f296bb3SBarry Smithformula 1287*7f296bb3SBarry Smith 1288*7f296bb3SBarry Smith$$ 1289*7f296bb3SBarry SmithF'_{:i} \approx \frac{F(u + h*dx_{i}) - F(u)}{h} 1290*7f296bb3SBarry Smith$$ 1291*7f296bb3SBarry Smith 1292*7f296bb3SBarry Smithwhere $h$ is computed via: 1293*7f296bb3SBarry Smith 1294*7f296bb3SBarry Smith$$ 1295*7f296bb3SBarry Smithh = e_{\text{rel}} \cdot \begin{cases} 1296*7f296bb3SBarry Smithu_{i} & \text{if $|u_{i}| > u_{\min}$} \\ 1297*7f296bb3SBarry Smithu_{\min} \cdot \operatorname{sign}(u_{i}) & \text{otherwise}. 1298*7f296bb3SBarry Smith\end{cases} 1299*7f296bb3SBarry Smith$$ 1300*7f296bb3SBarry Smith 1301*7f296bb3SBarry Smithfor `MATMFFD_DS` or: 1302*7f296bb3SBarry Smith 1303*7f296bb3SBarry Smith$$ 1304*7f296bb3SBarry Smithh = e_{\text{rel}} \sqrt(\|u\|) 1305*7f296bb3SBarry Smith$$ 1306*7f296bb3SBarry Smith 1307*7f296bb3SBarry Smithfor `MATMFFD_WP` (default). These parameters may be set from the options 1308*7f296bb3SBarry Smithdatabase with 1309*7f296bb3SBarry Smith 1310*7f296bb3SBarry Smith``` 1311*7f296bb3SBarry Smith-mat_fd_coloring_err <err> 1312*7f296bb3SBarry Smith-mat_fd_coloring_umin <umin> 1313*7f296bb3SBarry Smith-mat_fd_type <htype> 1314*7f296bb3SBarry Smith``` 1315*7f296bb3SBarry Smith 1316*7f296bb3SBarry SmithNote that `MatColoring` type `MATCOLORINGSL`, `MATCOLORINGLF`, and 1317*7f296bb3SBarry Smith`MATCOLORINGID` are sequential algorithms. `MATCOLORINGJP` and 1318*7f296bb3SBarry Smith`MATCOLORINGGREEDY` are parallel algorithms, although in practice they 1319*7f296bb3SBarry Smithmay create more colors than the sequential algorithms. If one computes 1320*7f296bb3SBarry Smiththe coloring `iscoloring` reasonably with a parallel algorithm or by 1321*7f296bb3SBarry Smithknowledge of the discretization, the routine `MatFDColoringCreate()` 1322*7f296bb3SBarry Smithis scalable. An example of this for 2D distributed arrays is given below 1323*7f296bb3SBarry Smiththat uses the utility routine `DMCreateColoring()`. 1324*7f296bb3SBarry Smith 1325*7f296bb3SBarry Smith``` 1326*7f296bb3SBarry SmithDMCreateColoring(da,IS_COLORING_GHOSTED, &iscoloring); 1327*7f296bb3SBarry SmithMatFDColoringCreate(J,iscoloring, &fdcoloring); 1328*7f296bb3SBarry SmithMatFDColoringSetFromOptions(fdcoloring); 1329*7f296bb3SBarry SmithISColoringDestroy( &iscoloring); 1330*7f296bb3SBarry Smith``` 1331*7f296bb3SBarry Smith 1332*7f296bb3SBarry SmithNote that the routine `MatFDColoringCreate()` currently is only 1333*7f296bb3SBarry Smithsupported for the AIJ and BAIJ matrix formats. 1334*7f296bb3SBarry Smith 1335*7f296bb3SBarry Smith(sec_vi)= 1336*7f296bb3SBarry Smith 1337*7f296bb3SBarry Smith## Variational Inequalities 1338*7f296bb3SBarry Smith 1339*7f296bb3SBarry Smith`SNES` can also solve (differential) variational inequalities with box (bound) constraints. 1340*7f296bb3SBarry SmithThese are nonlinear algebraic systems with additional inequality 1341*7f296bb3SBarry Smithconstraints on some or all of the variables: 1342*7f296bb3SBarry Smith$L_i \le u_i \le H_i$. For example, the pressure variable cannot be negative. 1343*7f296bb3SBarry SmithSome, or all, of the lower bounds may be 1344*7f296bb3SBarry Smithnegative infinity (indicated to PETSc with `SNES_VI_NINF`) and some, or 1345*7f296bb3SBarry Smithall, of the upper bounds may be infinity (indicated by `SNES_VI_INF`). 1346*7f296bb3SBarry SmithThe commands 1347*7f296bb3SBarry Smith 1348*7f296bb3SBarry Smith``` 1349*7f296bb3SBarry SmithSNESVISetVariableBounds(SNES,Vec L,Vec H); 1350*7f296bb3SBarry SmithSNESVISetComputeVariableBounds(SNES snes, PetscErrorCode (*compute)(SNES,Vec,Vec)) 1351*7f296bb3SBarry Smith``` 1352*7f296bb3SBarry Smith 1353*7f296bb3SBarry Smithare used to indicate that one is solving a variational inequality. Problems with box constraints can be solved with 1354*7f296bb3SBarry Smiththe reduced space, `SNESVINEWTONRSLS`, and semi-smooth `SNESVINEWTONSSLS` solvers. 1355*7f296bb3SBarry Smith 1356*7f296bb3SBarry SmithThe 1357*7f296bb3SBarry Smithoption `-snes_vi_monitor` turns on extra monitoring of the active set 1358*7f296bb3SBarry Smithassociated with the bounds and `-snes_vi_type` allows selecting from 1359*7f296bb3SBarry Smithseveral VI solvers, the default is preferred. 1360*7f296bb3SBarry Smith 1361*7f296bb3SBarry Smith`SNESLineSearchSetPreCheck()` and `SNESLineSearchSetPostCheck()` can also be used to control properties 1362*7f296bb3SBarry Smithof the steps selected by `SNES`. 1363*7f296bb3SBarry Smith 1364*7f296bb3SBarry Smith(sec_snespc)= 1365*7f296bb3SBarry Smith 1366*7f296bb3SBarry Smith## Nonlinear Preconditioning 1367*7f296bb3SBarry Smith 1368*7f296bb3SBarry SmithThe mathematical framework of nonlinear preconditioning is explained in detail in {cite}`bruneknepleysmithtu15`. 1369*7f296bb3SBarry SmithNonlinear preconditioning in PETSc involves the use of an inner `SNES` 1370*7f296bb3SBarry Smithinstance to define the step for an outer `SNES` instance. The inner 1371*7f296bb3SBarry Smithinstance may be extracted using 1372*7f296bb3SBarry Smith 1373*7f296bb3SBarry Smith``` 1374*7f296bb3SBarry SmithSNESGetNPC(SNES snes,SNES *npc); 1375*7f296bb3SBarry Smith``` 1376*7f296bb3SBarry Smith 1377*7f296bb3SBarry Smithand passed run-time options using the `-npc_` prefix. Nonlinear 1378*7f296bb3SBarry Smithpreconditioning comes in two flavors: left and right. The side may be 1379*7f296bb3SBarry Smithchanged using `-snes_npc_side` or `SNESSetNPCSide()`. Left nonlinear 1380*7f296bb3SBarry Smithpreconditioning redefines the nonlinear function as the action of the 1381*7f296bb3SBarry Smithnonlinear preconditioner $\mathbf{M}$; 1382*7f296bb3SBarry Smith 1383*7f296bb3SBarry Smith$$ 1384*7f296bb3SBarry Smith\mathbf{F}_{M}(x) = \mathbf{M}(\mathbf{x},\mathbf{b}) - \mathbf{x}. 1385*7f296bb3SBarry Smith$$ 1386*7f296bb3SBarry Smith 1387*7f296bb3SBarry SmithRight nonlinear preconditioning redefines the nonlinear function as the 1388*7f296bb3SBarry Smithfunction on the action of the nonlinear preconditioner; 1389*7f296bb3SBarry Smith 1390*7f296bb3SBarry Smith$$ 1391*7f296bb3SBarry Smith\mathbf{F}(\mathbf{M}(\mathbf{x},\mathbf{b})) = \mathbf{b}, 1392*7f296bb3SBarry Smith$$ 1393*7f296bb3SBarry Smith 1394*7f296bb3SBarry Smithwhich can be interpreted as putting the preconditioner into “striking 1395*7f296bb3SBarry Smithdistance” of the solution by outer acceleration. 1396*7f296bb3SBarry Smith 1397*7f296bb3SBarry SmithIn addition, basic patterns of solver composition are available with the 1398*7f296bb3SBarry Smith`SNESType` `SNESCOMPOSITE`. This allows for two or more `SNES` 1399*7f296bb3SBarry Smithinstances to be combined additively or multiplicatively. By command 1400*7f296bb3SBarry Smithline, a set of `SNES` types may be given by comma separated list 1401*7f296bb3SBarry Smithargument to `-snes_composite_sneses`. There are additive 1402*7f296bb3SBarry Smith(`SNES_COMPOSITE_ADDITIVE`), additive with optimal damping 1403*7f296bb3SBarry Smith(`SNES_COMPOSITE_ADDITIVEOPTIMAL`), and multiplicative 1404*7f296bb3SBarry Smith(`SNES_COMPOSITE_MULTIPLICATIVE`) variants which may be set with 1405*7f296bb3SBarry Smith 1406*7f296bb3SBarry Smith``` 1407*7f296bb3SBarry SmithSNESCompositeSetType(SNES,SNESCompositeType); 1408*7f296bb3SBarry Smith``` 1409*7f296bb3SBarry Smith 1410*7f296bb3SBarry SmithNew subsolvers may be added to the composite solver with 1411*7f296bb3SBarry Smith 1412*7f296bb3SBarry Smith``` 1413*7f296bb3SBarry SmithSNESCompositeAddSNES(SNES,SNESType); 1414*7f296bb3SBarry Smith``` 1415*7f296bb3SBarry Smith 1416*7f296bb3SBarry Smithand accessed with 1417*7f296bb3SBarry Smith 1418*7f296bb3SBarry Smith``` 1419*7f296bb3SBarry SmithSNESCompositeGetSNES(SNES,PetscInt,SNES *); 1420*7f296bb3SBarry Smith``` 1421*7f296bb3SBarry Smith 1422*7f296bb3SBarry Smith```{eval-rst} 1423*7f296bb3SBarry Smith.. bibliography:: /petsc.bib 1424*7f296bb3SBarry Smith :filter: docname in docnames 1425*7f296bb3SBarry Smith``` 1426