Date: 2025-03-28T21:11:07+00:00

Actual source code: snes.c

      1: #include <petsc/private/snesimpl.h>
      2: #include <petscdmshell.h>
      3: #include <petscdraw.h>
      4: #include <petscds.h>
      5: #include <petscdmadaptor.h>
      6: #include <petscconvest.h>

      8: PetscBool         SNESRegisterAllCalled = PETSC_FALSE;
      9: PetscFunctionList SNESList              = NULL;

     11: /* Logging support */
     12: PetscClassId  SNES_CLASSID, DMSNES_CLASSID;
     13: PetscLogEvent SNES_Solve, SNES_SetUp, SNES_FunctionEval, SNES_JacobianEval, SNES_NGSEval, SNES_NGSFuncEval, SNES_NewtonALEval, SNES_NPCSolve, SNES_ObjectiveEval;

     15: /*@
     16:   SNESSetErrorIfNotConverged - Causes `SNESSolve()` to generate an error immediately if the solver has not converged.

     18:   Logically Collective

     20:   Input Parameters:
     21: + snes - iterative context obtained from `SNESCreate()`
     22: - flg  - `PETSC_TRUE` indicates you want the error generated

     24:   Options Database Key:
     25: . -snes_error_if_not_converged <true,false> - cause an immediate error condition and stop the program if the solver does not converge

     27:   Level: intermediate

     29:   Note:
     30:   Normally PETSc continues if a solver fails to converge, you can call `SNESGetConvergedReason()` after a `SNESSolve()`
     31:   to determine if it has converged. Otherwise the solution may be inaccurate or wrong

     33: .seealso: [](ch_snes), `SNES`, `SNESGetErrorIfNotConverged()`, `KSPGetErrorIfNotConverged()`, `KSPSetErrorIfNotConverged()`
     34: @*/
     35: PetscErrorCode SNESSetErrorIfNotConverged(SNES snes, PetscBool flg)
     36: {
     37:   PetscFunctionBegin;
     40:   snes->errorifnotconverged = flg;
     41:   PetscFunctionReturn(PETSC_SUCCESS);
     42: }

     44: /*@
     45:   SNESGetErrorIfNotConverged - Indicates if `SNESSolve()` will generate an error if the solver does not converge?

     47:   Not Collective

     49:   Input Parameter:
     50: . snes - iterative context obtained from `SNESCreate()`

     52:   Output Parameter:
     53: . flag - `PETSC_TRUE` if it will generate an error, else `PETSC_FALSE`

     55:   Level: intermediate

     57: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESSetErrorIfNotConverged()`, `KSPGetErrorIfNotConverged()`, `KSPSetErrorIfNotConverged()`
     58: @*/
     59: PetscErrorCode SNESGetErrorIfNotConverged(SNES snes, PetscBool *flag)
     60: {
     61:   PetscFunctionBegin;
     63:   PetscAssertPointer(flag, 2);
     64:   *flag = snes->errorifnotconverged;
     65:   PetscFunctionReturn(PETSC_SUCCESS);
     66: }

     68: /*@
     69:   SNESSetAlwaysComputesFinalResidual - tells the `SNES` to always compute the residual (nonlinear function value) at the final solution

     71:   Logically Collective

     73:   Input Parameters:
     74: + snes - the shell `SNES`
     75: - flg  - `PETSC_TRUE` to always compute the residual

     77:   Level: advanced

     79:   Note:
     80:   Some solvers (such as smoothers in a `SNESFAS`) do not need the residual computed at the final solution so skip computing it
     81:   to save time.

     83: .seealso: [](ch_snes), `SNES`, `SNESFAS`, `SNESSolve()`, `SNESGetAlwaysComputesFinalResidual()`
     84: @*/
     85: PetscErrorCode SNESSetAlwaysComputesFinalResidual(SNES snes, PetscBool flg)
     86: {
     87:   PetscFunctionBegin;
     89:   snes->alwayscomputesfinalresidual = flg;
     90:   PetscFunctionReturn(PETSC_SUCCESS);
     91: }

     93: /*@
     94:   SNESGetAlwaysComputesFinalResidual - checks if the `SNES` always computes the residual at the final solution

     96:   Logically Collective

     98:   Input Parameter:
     99: . snes - the `SNES` context

    101:   Output Parameter:
    102: . flg - `PETSC_TRUE` if the residual is computed

    104:   Level: advanced

    106: .seealso: [](ch_snes), `SNES`, `SNESFAS`, `SNESSolve()`, `SNESSetAlwaysComputesFinalResidual()`
    107: @*/
    108: PetscErrorCode SNESGetAlwaysComputesFinalResidual(SNES snes, PetscBool *flg)
    109: {
    110:   PetscFunctionBegin;
    112:   *flg = snes->alwayscomputesfinalresidual;
    113:   PetscFunctionReturn(PETSC_SUCCESS);
    114: }

    116: /*@
    117:   SNESSetFunctionDomainError - tells `SNES` that the input vector, a proposed new solution, to your function you provided to `SNESSetFunction()` is not
    118:   in the functions domain. For example, a step with negative pressure.

    120:   Logically Collective

    122:   Input Parameter:
    123: . snes - the `SNES` context

    125:   Level: advanced

    127:   Notes:
    128:   If this is called the `SNESSolve()` stops iterating and returns with a `SNESConvergedReason` of `SNES_DIVERGED_FUNCTION_DOMAIN`

    130:   You should always call `SNESGetConvergedReason()` after each `SNESSolve()` and verify if the iteration converged (positive result) or diverged (negative result).

    132:   You can direct `SNES` to avoid certain steps by using `SNESVISetVariableBounds()`, `SNESVISetComputeVariableBounds()` or
    133:   `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()`

    135: .seealso: [](ch_snes), `SNESCreate()`, `SNESSetFunction()`, `SNESFunctionFn`, `SNESSetJacobianDomainError()`, `SNESVISetVariableBounds()`,
    136:           `SNESVISetComputeVariableBounds()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()`, `SNESConvergedReason`, `SNESGetConvergedReason()`
    137: @*/
    138: PetscErrorCode SNESSetFunctionDomainError(SNES snes)
    139: {
    140:   PetscFunctionBegin;
    142:   PetscCheck(!snes->errorifnotconverged, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "User code indicates input vector is not in the function domain");
    143:   snes->domainerror = PETSC_TRUE;
    144:   PetscFunctionReturn(PETSC_SUCCESS);
    145: }

    147: /*@
    148:   SNESSetJacobianDomainError - tells `SNES` that the function you provided to `SNESSetJacobian()` at the proposed step. For example there is a negative element transformation.

    150:   Logically Collective

    152:   Input Parameter:
    153: . snes - the `SNES` context

    155:   Level: advanced

    157:   Notes:
    158:   If this is called the `SNESSolve()` stops iterating and returns with a `SNESConvergedReason` of `SNES_DIVERGED_FUNCTION_DOMAIN`

    160:   You should always call `SNESGetConvergedReason()` after each `SNESSolve()` and verify if the iteration converged (positive result) or diverged (negative result).

    162:   You can direct `SNES` to avoid certain steps by using `SNESVISetVariableBounds()`, `SNESVISetComputeVariableBounds()` or
    163:   `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()`

    165: .seealso: [](ch_snes), `SNESCreate()`, `SNESSetFunction()`, `SNESFunctionFn`, `SNESSetFunctionDomainError()`, `SNESVISetVariableBounds()`,
    166:           `SNESVISetComputeVariableBounds()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()`, `SNESConvergedReason`, `SNESGetConvergedReason()`
    167: @*/
    168: PetscErrorCode SNESSetJacobianDomainError(SNES snes)
    169: {
    170:   PetscFunctionBegin;
    172:   PetscCheck(!snes->errorifnotconverged, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "User code indicates computeJacobian does not make sense");
    173:   snes->jacobiandomainerror = PETSC_TRUE;
    174:   PetscFunctionReturn(PETSC_SUCCESS);
    175: }

    177: /*@
    178:   SNESSetCheckJacobianDomainError - tells `SNESSolve()` whether to check if the user called `SNESSetJacobianDomainError()` Jacobian domain error after
    179:   each Jacobian evaluation. By default, it checks for the Jacobian domain error in the debug mode, and does not check it in the optimized mode.

    181:   Logically Collective

    183:   Input Parameters:
    184: + snes - the `SNES` context
    185: - flg  - indicates if or not to check Jacobian domain error after each Jacobian evaluation

    187:   Level: advanced

    189:   Note:
    190:   Checks require one extra parallel synchronization for each Jacobian evaluation

    192: .seealso: [](ch_snes), `SNES`, `SNESConvergedReason`, `SNESCreate()`, `SNESSetFunction()`, `SNESFunctionFn`, `SNESSetFunctionDomainError()`, `SNESGetCheckJacobianDomainError()`
    193: @*/
    194: PetscErrorCode SNESSetCheckJacobianDomainError(SNES snes, PetscBool flg)
    195: {
    196:   PetscFunctionBegin;
    198:   snes->checkjacdomainerror = flg;
    199:   PetscFunctionReturn(PETSC_SUCCESS);
    200: }

    202: /*@
    203:   SNESGetCheckJacobianDomainError - Get an indicator whether or not `SNES` is checking Jacobian domain errors after each Jacobian evaluation.

    205:   Logically Collective

    207:   Input Parameter:
    208: . snes - the `SNES` context

    210:   Output Parameter:
    211: . flg - `PETSC_FALSE` indicates that it is not checking Jacobian domain errors after each Jacobian evaluation

    213:   Level: advanced

    215: .seealso: [](ch_snes), `SNES`, `SNESCreate()`, `SNESSetFunction()`, `SNESFunctionFn`, `SNESSetFunctionDomainError()`, `SNESSetCheckJacobianDomainError()`
    216: @*/
    217: PetscErrorCode SNESGetCheckJacobianDomainError(SNES snes, PetscBool *flg)
    218: {
    219:   PetscFunctionBegin;
    221:   PetscAssertPointer(flg, 2);
    222:   *flg = snes->checkjacdomainerror;
    223:   PetscFunctionReturn(PETSC_SUCCESS);
    224: }

    226: /*@
    227:   SNESGetFunctionDomainError - Gets the status of the domain error after a call to `SNESComputeFunction()`

    229:   Logically Collective

    231:   Input Parameter:
    232: . snes - the `SNES` context

    234:   Output Parameter:
    235: . domainerror - Set to `PETSC_TRUE` if there's a domain error; `PETSC_FALSE` otherwise.

    237:   Level: developer

    239: .seealso: [](ch_snes), `SNES`, `SNESSetFunctionDomainError()`, `SNESComputeFunction()`
    240: @*/
    241: PetscErrorCode SNESGetFunctionDomainError(SNES snes, PetscBool *domainerror)
    242: {
    243:   PetscFunctionBegin;
    245:   PetscAssertPointer(domainerror, 2);
    246:   *domainerror = snes->domainerror;
    247:   PetscFunctionReturn(PETSC_SUCCESS);
    248: }

    250: /*@
    251:   SNESGetJacobianDomainError - Gets the status of the Jacobian domain error after a call to `SNESComputeJacobian()`

    253:   Logically Collective

    255:   Input Parameter:
    256: . snes - the `SNES` context

    258:   Output Parameter:
    259: . domainerror - Set to `PETSC_TRUE` if there's a Jacobian domain error; `PETSC_FALSE` otherwise.

    261:   Level: advanced

    263: .seealso: [](ch_snes), `SNES`, `SNESSetFunctionDomainError()`, `SNESComputeFunction()`, `SNESGetFunctionDomainError()`
    264: @*/
    265: PetscErrorCode SNESGetJacobianDomainError(SNES snes, PetscBool *domainerror)
    266: {
    267:   PetscFunctionBegin;
    269:   PetscAssertPointer(domainerror, 2);
    270:   *domainerror = snes->jacobiandomainerror;
    271:   PetscFunctionReturn(PETSC_SUCCESS);
    272: }

    274: /*@
    275:   SNESLoad - Loads a `SNES` that has been stored in `PETSCVIEWERBINARY` with `SNESView()`.

    277:   Collective

    279:   Input Parameters:
    280: + snes   - the newly loaded `SNES`, this needs to have been created with `SNESCreate()` or
    281:            some related function before a call to `SNESLoad()`.
    282: - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()`

    284:   Level: intermediate

    286:   Note:
    287:   The `SNESType` is determined by the data in the file, any type set into the `SNES` before this call is ignored.

    289: .seealso: [](ch_snes), `SNES`, `PetscViewer`, `SNESCreate()`, `SNESType`, `PetscViewerBinaryOpen()`, `SNESView()`, `MatLoad()`, `VecLoad()`
    290: @*/
    291: PetscErrorCode SNESLoad(SNES snes, PetscViewer viewer)
    292: {
    293:   PetscBool isbinary;
    294:   PetscInt  classid;
    295:   char      type[256];
    296:   KSP       ksp;
    297:   DM        dm;
    298:   DMSNES    dmsnes;

    300:   PetscFunctionBegin;
    303:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
    304:   PetscCheck(isbinary, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()");

    306:   PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
    307:   PetscCheck(classid == SNES_FILE_CLASSID, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_WRONG, "Not SNES next in file");
    308:   PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
    309:   PetscCall(SNESSetType(snes, type));
    310:   PetscTryTypeMethod(snes, load, viewer);
    311:   PetscCall(SNESGetDM(snes, &dm));
    312:   PetscCall(DMGetDMSNES(dm, &dmsnes));
    313:   PetscCall(DMSNESLoad(dmsnes, viewer));
    314:   PetscCall(SNESGetKSP(snes, &ksp));
    315:   PetscCall(KSPLoad(ksp, viewer));
    316:   PetscFunctionReturn(PETSC_SUCCESS);
    317: }

    319: #include <petscdraw.h>
    320: #if defined(PETSC_HAVE_SAWS)
    321: #include <petscviewersaws.h>
    322: #endif

    324: /*@
    325:   SNESViewFromOptions - View a `SNES` based on values in the options database

    327:   Collective

    329:   Input Parameters:
    330: + A    - the `SNES` context
    331: . obj  - Optional object that provides the options prefix for the checks
    332: - name - command line option

    334:   Level: intermediate

    336: .seealso: [](ch_snes), `SNES`, `SNESView`, `PetscObjectViewFromOptions()`, `SNESCreate()`
    337: @*/
    338: PetscErrorCode SNESViewFromOptions(SNES A, PetscObject obj, const char name[])
    339: {
    340:   PetscFunctionBegin;
    342:   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
    343:   PetscFunctionReturn(PETSC_SUCCESS);
    344: }

    346: PETSC_EXTERN PetscErrorCode SNESComputeJacobian_DMDA(SNES, Vec, Mat, Mat, void *);

    348: /*@
    349:   SNESView - Prints or visualizes the `SNES` data structure.

    351:   Collective

    353:   Input Parameters:
    354: + snes   - the `SNES` context
    355: - viewer - the `PetscViewer`

    357:   Options Database Key:
    358: . -snes_view - Calls `SNESView()` at end of `SNESSolve()`

    360:   Level: beginner

    362:   Notes:
    363:   The available visualization contexts include
    364: +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
    365: -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
    366:   output where only the first processor opens
    367:   the file.  All other processors send their
    368:   data to the first processor to print.

    370:   The available formats include
    371: +     `PETSC_VIEWER_DEFAULT` - standard output (default)
    372: -     `PETSC_VIEWER_ASCII_INFO_DETAIL` - more verbose output for `SNESNASM`

    374:   The user can open an alternative visualization context with
    375:   `PetscViewerASCIIOpen()` - output to a specified file.

    377:   In the debugger you can do "call `SNESView`(snes,0)" to display the `SNES` solver. (The same holds for any PETSc object viewer).

    379: .seealso: [](ch_snes), `SNES`, `SNESLoad()`, `SNESCreate()`, `PetscViewerASCIIOpen()`
    380: @*/
    381: PetscErrorCode SNESView(SNES snes, PetscViewer viewer)
    382: {
    383:   SNESKSPEW     *kctx;
    384:   KSP            ksp;
    385:   SNESLineSearch linesearch;
    386:   PetscBool      iascii, isstring, isbinary, isdraw;
    387:   DMSNES         dmsnes;
    388: #if defined(PETSC_HAVE_SAWS)
    389:   PetscBool issaws;
    390: #endif

    392:   PetscFunctionBegin;
    394:   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes), &viewer));
    396:   PetscCheckSameComm(snes, 1, viewer, 2);

    398:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
    399:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
    400:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
    401:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
    402: #if defined(PETSC_HAVE_SAWS)
    403:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
    404: #endif
    405:   if (iascii) {
    406:     SNESNormSchedule normschedule;
    407:     DM               dm;
    408:     SNESJacobianFn  *cJ;
    409:     void            *ctx;
    410:     const char      *pre = "";

    412:     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)snes, viewer));
    413:     if (!snes->setupcalled) PetscCall(PetscViewerASCIIPrintf(viewer, "  SNES has not been set up so information may be incomplete\n"));
    414:     if (snes->ops->view) {
    415:       PetscCall(PetscViewerASCIIPushTab(viewer));
    416:       PetscUseTypeMethod(snes, view, viewer);
    417:       PetscCall(PetscViewerASCIIPopTab(viewer));
    418:     }
    419:     if (snes->max_funcs == PETSC_UNLIMITED) {
    420:       PetscCall(PetscViewerASCIIPrintf(viewer, "  maximum iterations=%" PetscInt_FMT ", maximum function evaluations=unlimited\n", snes->max_its));
    421:     } else {
    422:       PetscCall(PetscViewerASCIIPrintf(viewer, "  maximum iterations=%" PetscInt_FMT ", maximum function evaluations=%" PetscInt_FMT "\n", snes->max_its, snes->max_funcs));
    423:     }
    424:     PetscCall(PetscViewerASCIIPrintf(viewer, "  tolerances: relative=%g, absolute=%g, solution=%g\n", (double)snes->rtol, (double)snes->abstol, (double)snes->stol));
    425:     if (snes->usesksp) PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of linear solver iterations=%" PetscInt_FMT "\n", snes->linear_its));
    426:     PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of function evaluations=%" PetscInt_FMT "\n", snes->nfuncs));
    427:     PetscCall(SNESGetNormSchedule(snes, &normschedule));
    428:     if (normschedule > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "  norm schedule %s\n", SNESNormSchedules[normschedule]));
    429:     if (snes->gridsequence) PetscCall(PetscViewerASCIIPrintf(viewer, "  total number of grid sequence refinements=%" PetscInt_FMT "\n", snes->gridsequence));
    430:     if (snes->ksp_ewconv) {
    431:       kctx = (SNESKSPEW *)snes->kspconvctx;
    432:       if (kctx) {
    433:         PetscCall(PetscViewerASCIIPrintf(viewer, "  Eisenstat-Walker computation of KSP relative tolerance (version %" PetscInt_FMT ")\n", kctx->version));
    434:         PetscCall(PetscViewerASCIIPrintf(viewer, "    rtol_0=%g, rtol_max=%g, threshold=%g\n", (double)kctx->rtol_0, (double)kctx->rtol_max, (double)kctx->threshold));
    435:         PetscCall(PetscViewerASCIIPrintf(viewer, "    gamma=%g, alpha=%g, alpha2=%g\n", (double)kctx->gamma, (double)kctx->alpha, (double)kctx->alpha2));
    436:       }
    437:     }
    438:     if (snes->lagpreconditioner == -1) {
    439:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Preconditioned is never rebuilt\n"));
    440:     } else if (snes->lagpreconditioner > 1) {
    441:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Preconditioned is rebuilt every %" PetscInt_FMT " new Jacobians\n", snes->lagpreconditioner));
    442:     }
    443:     if (snes->lagjacobian == -1) {
    444:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Jacobian is never rebuilt\n"));
    445:     } else if (snes->lagjacobian > 1) {
    446:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Jacobian is rebuilt every %" PetscInt_FMT " SNES iterations\n", snes->lagjacobian));
    447:     }
    448:     PetscCall(SNESGetDM(snes, &dm));
    449:     PetscCall(DMSNESGetJacobian(dm, &cJ, &ctx));
    450:     if (snes->mf_operator) {
    451:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Jacobian is applied matrix-free with differencing\n"));
    452:       pre = "Preconditioning ";
    453:     }
    454:     if (cJ == SNESComputeJacobianDefault) {
    455:       PetscCall(PetscViewerASCIIPrintf(viewer, "  %sJacobian is built using finite differences one column at a time\n", pre));
    456:     } else if (cJ == SNESComputeJacobianDefaultColor) {
    457:       PetscCall(PetscViewerASCIIPrintf(viewer, "  %sJacobian is built using finite differences with coloring\n", pre));
    458:       /* it slightly breaks data encapsulation for access the DMDA information directly */
    459:     } else if (cJ == SNESComputeJacobian_DMDA) {
    460:       MatFDColoring fdcoloring;
    461:       PetscCall(PetscObjectQuery((PetscObject)dm, "DMDASNES_FDCOLORING", (PetscObject *)&fdcoloring));
    462:       if (fdcoloring) {
    463:         PetscCall(PetscViewerASCIIPrintf(viewer, "  %sJacobian is built using colored finite differences on a DMDA\n", pre));
    464:       } else {
    465:         PetscCall(PetscViewerASCIIPrintf(viewer, "  %sJacobian is built using a DMDA local Jacobian\n", pre));
    466:       }
    467:     } else if (snes->mf && !snes->mf_operator) {
    468:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Jacobian is applied matrix-free with differencing, no explicit Jacobian\n"));
    469:     }
    470:   } else if (isstring) {
    471:     const char *type;
    472:     PetscCall(SNESGetType(snes, &type));
    473:     PetscCall(PetscViewerStringSPrintf(viewer, " SNESType: %-7.7s", type));
    474:     PetscTryTypeMethod(snes, view, viewer);
    475:   } else if (isbinary) {
    476:     PetscInt    classid = SNES_FILE_CLASSID;
    477:     MPI_Comm    comm;
    478:     PetscMPIInt rank;
    479:     char        type[256];

    481:     PetscCall(PetscObjectGetComm((PetscObject)snes, &comm));
    482:     PetscCallMPI(MPI_Comm_rank(comm, &rank));
    483:     if (rank == 0) {
    484:       PetscCall(PetscViewerBinaryWrite(viewer, &classid, 1, PETSC_INT));
    485:       PetscCall(PetscStrncpy(type, ((PetscObject)snes)->type_name, sizeof(type)));
    486:       PetscCall(PetscViewerBinaryWrite(viewer, type, sizeof(type), PETSC_CHAR));
    487:     }
    488:     PetscTryTypeMethod(snes, view, viewer);
    489:   } else if (isdraw) {
    490:     PetscDraw draw;
    491:     char      str[36];
    492:     PetscReal x, y, bottom, h;

    494:     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
    495:     PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
    496:     PetscCall(PetscStrncpy(str, "SNES: ", sizeof(str)));
    497:     PetscCall(PetscStrlcat(str, ((PetscObject)snes)->type_name, sizeof(str)));
    498:     PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_BLUE, PETSC_DRAW_BLACK, str, NULL, &h));
    499:     bottom = y - h;
    500:     PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom));
    501:     PetscTryTypeMethod(snes, view, viewer);
    502: #if defined(PETSC_HAVE_SAWS)
    503:   } else if (issaws) {
    504:     PetscMPIInt rank;
    505:     const char *name;

    507:     PetscCall(PetscObjectGetName((PetscObject)snes, &name));
    508:     PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
    509:     if (!((PetscObject)snes)->amsmem && rank == 0) {
    510:       char dir[1024];

    512:       PetscCall(PetscObjectViewSAWs((PetscObject)snes, viewer));
    513:       PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/its", name));
    514:       PetscCallSAWs(SAWs_Register, (dir, &snes->iter, 1, SAWs_READ, SAWs_INT));
    515:       if (!snes->conv_hist) PetscCall(SNESSetConvergenceHistory(snes, NULL, NULL, PETSC_DECIDE, PETSC_TRUE));
    516:       PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/conv_hist", name));
    517:       PetscCallSAWs(SAWs_Register, (dir, snes->conv_hist, 10, SAWs_READ, SAWs_DOUBLE));
    518:     }
    519: #endif
    520:   }
    521:   if (snes->linesearch) {
    522:     PetscCall(SNESGetLineSearch(snes, &linesearch));
    523:     PetscCall(PetscViewerASCIIPushTab(viewer));
    524:     PetscCall(SNESLineSearchView(linesearch, viewer));
    525:     PetscCall(PetscViewerASCIIPopTab(viewer));
    526:   }
    527:   if (snes->npc && snes->usesnpc) {
    528:     PetscCall(PetscViewerASCIIPushTab(viewer));
    529:     PetscCall(SNESView(snes->npc, viewer));
    530:     PetscCall(PetscViewerASCIIPopTab(viewer));
    531:   }
    532:   PetscCall(PetscViewerASCIIPushTab(viewer));
    533:   PetscCall(DMGetDMSNES(snes->dm, &dmsnes));
    534:   PetscCall(DMSNESView(dmsnes, viewer));
    535:   PetscCall(PetscViewerASCIIPopTab(viewer));
    536:   if (snes->usesksp) {
    537:     PetscCall(SNESGetKSP(snes, &ksp));
    538:     PetscCall(PetscViewerASCIIPushTab(viewer));
    539:     PetscCall(KSPView(ksp, viewer));
    540:     PetscCall(PetscViewerASCIIPopTab(viewer));
    541:   }
    542:   if (isdraw) {
    543:     PetscDraw draw;
    544:     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
    545:     PetscCall(PetscDrawPopCurrentPoint(draw));
    546:   }
    547:   PetscFunctionReturn(PETSC_SUCCESS);
    548: }

    550: /*
    551:   We retain a list of functions that also take SNES command
    552:   line options. These are called at the end SNESSetFromOptions()
    553: */
    554: #define MAXSETFROMOPTIONS 5
    555: static PetscInt numberofsetfromoptions;
    556: static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);

    558: /*@C
    559:   SNESAddOptionsChecker - Adds an additional function to check for `SNES` options.

    561:   Not Collective

    563:   Input Parameter:
    564: . snescheck - function that checks for options

    566:   Calling sequence of `snescheck`:
    567: . snes - the `SNES` object for which it is checking options

    569:   Level: developer

    571: .seealso: [](ch_snes), `SNES`, `SNESSetFromOptions()`
    572: @*/
    573: PetscErrorCode SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES snes))
    574: {
    575:   PetscFunctionBegin;
    576:   PetscCheck(numberofsetfromoptions < MAXSETFROMOPTIONS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %d allowed", MAXSETFROMOPTIONS);
    577:   othersetfromoptions[numberofsetfromoptions++] = snescheck;
    578:   PetscFunctionReturn(PETSC_SUCCESS);
    579: }

    581: static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool hasOperator, PetscInt version)
    582: {
    583:   Mat          J;
    584:   MatNullSpace nullsp;

    586:   PetscFunctionBegin;

    589:   if (!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
    590:     Mat A = snes->jacobian, B = snes->jacobian_pre;
    591:     PetscCall(MatCreateVecs(A ? A : B, NULL, &snes->vec_func));
    592:   }

    594:   PetscCheck(version == 1 || version == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator routines, only version 1 and 2");
    595:   if (version == 1) {
    596:     PetscCall(MatCreateSNESMF(snes, &J));
    597:     PetscCall(MatMFFDSetOptionsPrefix(J, ((PetscObject)snes)->prefix));
    598:     PetscCall(MatSetFromOptions(J));
    599:     /* TODO: the version 2 code should be merged into the MatCreateSNESMF() and MatCreateMFFD() infrastructure and then removed */
    600:   } else /* if (version == 2) */ {
    601:     PetscCheck(snes->vec_func, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "SNESSetFunction() must be called first");
    602: #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) && !defined(PETSC_USE_REAL___FP16)
    603:     PetscCall(MatCreateSNESMFMore(snes, snes->vec_func, &J));
    604: #else
    605:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "matrix-free operator routines (version 2)");
    606: #endif
    607:   }

    609:   /* attach any user provided null space that was on Amat to the newly created matrix-free matrix */
    610:   if (snes->jacobian) {
    611:     PetscCall(MatGetNullSpace(snes->jacobian, &nullsp));
    612:     if (nullsp) PetscCall(MatSetNullSpace(J, nullsp));
    613:   }

    615:   PetscCall(PetscInfo(snes, "Setting default matrix-free operator routines (version %" PetscInt_FMT ")\n", version));
    616:   if (hasOperator) {
    617:     /* This version replaces the user provided Jacobian matrix with a
    618:        matrix-free version but still employs the user-provided preconditioner matrix. */
    619:     PetscCall(SNESSetJacobian(snes, J, NULL, NULL, NULL));
    620:   } else {
    621:     /* This version replaces both the user-provided Jacobian and the user-
    622:      provided preconditioner Jacobian with the default matrix-free version. */
    623:     if (snes->npcside == PC_LEFT && snes->npc) {
    624:       if (!snes->jacobian) PetscCall(SNESSetJacobian(snes, J, NULL, NULL, NULL));
    625:     } else {
    626:       KSP       ksp;
    627:       PC        pc;
    628:       PetscBool match;

    630:       PetscCall(SNESSetJacobian(snes, J, J, MatMFFDComputeJacobian, NULL));
    631:       /* Force no preconditioner */
    632:       PetscCall(SNESGetKSP(snes, &ksp));
    633:       PetscCall(KSPGetPC(ksp, &pc));
    634:       PetscCall(PetscObjectTypeCompareAny((PetscObject)pc, &match, PCSHELL, PCH2OPUS, ""));
    635:       if (!match) {
    636:         PetscCall(PetscInfo(snes, "Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n"));
    637:         PetscCall(PCSetType(pc, PCNONE));
    638:       }
    639:     }
    640:   }
    641:   PetscCall(MatDestroy(&J));
    642:   PetscFunctionReturn(PETSC_SUCCESS);
    643: }

    645: static PetscErrorCode DMRestrictHook_SNESVecSol(DM dmfine, Mat Restrict, Vec Rscale, Mat Inject, DM dmcoarse, void *ctx)
    646: {
    647:   SNES snes = (SNES)ctx;
    648:   Vec  Xfine, Xfine_named = NULL, Xcoarse;

    650:   PetscFunctionBegin;
    651:   if (PetscLogPrintInfo) {
    652:     PetscInt finelevel, coarselevel, fineclevel, coarseclevel;
    653:     PetscCall(DMGetRefineLevel(dmfine, &finelevel));
    654:     PetscCall(DMGetCoarsenLevel(dmfine, &fineclevel));
    655:     PetscCall(DMGetRefineLevel(dmcoarse, &coarselevel));
    656:     PetscCall(DMGetCoarsenLevel(dmcoarse, &coarseclevel));
    657:     PetscCall(PetscInfo(dmfine, "Restricting SNES solution vector from level %" PetscInt_FMT "-%" PetscInt_FMT " to level %" PetscInt_FMT "-%" PetscInt_FMT "\n", finelevel, fineclevel, coarselevel, coarseclevel));
    658:   }
    659:   if (dmfine == snes->dm) Xfine = snes->vec_sol;
    660:   else {
    661:     PetscCall(DMGetNamedGlobalVector(dmfine, "SNESVecSol", &Xfine_named));
    662:     Xfine = Xfine_named;
    663:   }
    664:   PetscCall(DMGetNamedGlobalVector(dmcoarse, "SNESVecSol", &Xcoarse));
    665:   if (Inject) {
    666:     PetscCall(MatRestrict(Inject, Xfine, Xcoarse));
    667:   } else {
    668:     PetscCall(MatRestrict(Restrict, Xfine, Xcoarse));
    669:     PetscCall(VecPointwiseMult(Xcoarse, Xcoarse, Rscale));
    670:   }
    671:   PetscCall(DMRestoreNamedGlobalVector(dmcoarse, "SNESVecSol", &Xcoarse));
    672:   if (Xfine_named) PetscCall(DMRestoreNamedGlobalVector(dmfine, "SNESVecSol", &Xfine_named));
    673:   PetscFunctionReturn(PETSC_SUCCESS);
    674: }

    676: static PetscErrorCode DMCoarsenHook_SNESVecSol(DM dm, DM dmc, void *ctx)
    677: {
    678:   PetscFunctionBegin;
    679:   PetscCall(DMCoarsenHookAdd(dmc, DMCoarsenHook_SNESVecSol, DMRestrictHook_SNESVecSol, ctx));
    680:   PetscFunctionReturn(PETSC_SUCCESS);
    681: }

    683: /* This may be called to rediscretize the operator on levels of linear multigrid. The DM shuffle is so the user can
    684:  * safely call SNESGetDM() in their residual evaluation routine. */
    685: static PetscErrorCode KSPComputeOperators_SNES(KSP ksp, Mat A, Mat B, void *ctx)
    686: {
    687:   SNES            snes = (SNES)ctx;
    688:   DMSNES          sdm;
    689:   Vec             X, Xnamed = NULL;
    690:   DM              dmsave;
    691:   void           *ctxsave;
    692:   SNESJacobianFn *jac = NULL;

    694:   PetscFunctionBegin;
    695:   dmsave = snes->dm;
    696:   PetscCall(KSPGetDM(ksp, &snes->dm));
    697:   if (dmsave == snes->dm) X = snes->vec_sol; /* We are on the finest level */
    698:   else {
    699:     PetscBool has;

    701:     /* We are on a coarser level, this vec was initialized using a DM restrict hook */
    702:     PetscCall(DMHasNamedGlobalVector(snes->dm, "SNESVecSol", &has));
    703:     PetscCheck(has, PetscObjectComm((PetscObject)snes->dm), PETSC_ERR_PLIB, "Missing SNESVecSol");
    704:     PetscCall(DMGetNamedGlobalVector(snes->dm, "SNESVecSol", &Xnamed));
    705:     X = Xnamed;
    706:     PetscCall(SNESGetJacobian(snes, NULL, NULL, &jac, &ctxsave));
    707:     /* If the DM's don't match up, the MatFDColoring context needed for the jacobian won't match up either -- fixit. */
    708:     if (jac == SNESComputeJacobianDefaultColor) PetscCall(SNESSetJacobian(snes, NULL, NULL, SNESComputeJacobianDefaultColor, NULL));
    709:   }

    711:   /* Compute the operators */
    712:   PetscCall(DMGetDMSNES(snes->dm, &sdm));
    713:   if (Xnamed && sdm->ops->computefunction) {
    714:     /* The SNES contract with the user is that ComputeFunction is always called before ComputeJacobian.
    715:        We make sure of this here. Disable affine shift since it is for the finest level */
    716:     Vec F, saverhs = snes->vec_rhs;

    718:     snes->vec_rhs = NULL;
    719:     PetscCall(DMGetGlobalVector(snes->dm, &F));
    720:     PetscCall(SNESComputeFunction(snes, X, F));
    721:     PetscCall(DMRestoreGlobalVector(snes->dm, &F));
    722:     snes->vec_rhs = saverhs;
    723:     snes->nfuncs--; /* Do not log coarser level evaluations */
    724:   }
    725:   /* Make sure KSP DM has the Jacobian computation routine */
    726:   if (!sdm->ops->computejacobian) PetscCall(DMCopyDMSNES(dmsave, snes->dm));
    727:   PetscCall(SNESComputeJacobian(snes, X, A, B));

    729:   /* Put the previous context back */
    730:   if (snes->dm != dmsave && jac == SNESComputeJacobianDefaultColor) PetscCall(SNESSetJacobian(snes, NULL, NULL, jac, ctxsave));

    732:   if (Xnamed) PetscCall(DMRestoreNamedGlobalVector(snes->dm, "SNESVecSol", &Xnamed));
    733:   snes->dm = dmsave;
    734:   PetscFunctionReturn(PETSC_SUCCESS);
    735: }

    737: /*@
    738:   SNESSetUpMatrices - ensures that matrices are available for `SNES` Newton-like methods, this is called by `SNESSetUp_XXX()`

    740:   Collective

    742:   Input Parameter:
    743: . snes - `SNES` object to configure

    745:   Level: developer

    747:   Note:
    748:   If the matrices do not yet exist it attempts to create them based on options previously set for the `SNES` such as `-snes_mf`

    750:   Developer Note:
    751:   The functionality of this routine overlaps in a confusing way with the functionality of `SNESSetUpMatrixFree_Private()` which is called by
    752:   `SNESSetUp()` but sometimes `SNESSetUpMatrices()` is called without `SNESSetUp()` being called. A refactorization to simplify the
    753:   logic that handles the matrix-free case is desirable.

    755: .seealso: [](ch_snes), `SNES`, `SNESSetUp()`
    756: @*/
    757: PetscErrorCode SNESSetUpMatrices(SNES snes)
    758: {
    759:   DM     dm;
    760:   DMSNES sdm;

    762:   PetscFunctionBegin;
    763:   PetscCall(SNESGetDM(snes, &dm));
    764:   PetscCall(DMGetDMSNES(dm, &sdm));
    765:   if (!snes->jacobian && snes->mf && !snes->mf_operator && !snes->jacobian_pre) {
    766:     Mat   J;
    767:     void *functx;
    768:     PetscCall(MatCreateSNESMF(snes, &J));
    769:     PetscCall(MatMFFDSetOptionsPrefix(J, ((PetscObject)snes)->prefix));
    770:     PetscCall(MatSetFromOptions(J));
    771:     PetscCall(SNESGetFunction(snes, NULL, NULL, &functx));
    772:     PetscCall(SNESSetJacobian(snes, J, J, NULL, NULL));
    773:     PetscCall(MatDestroy(&J));
    774:   } else if (snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) {
    775:     Mat J, B;
    776:     PetscCall(MatCreateSNESMF(snes, &J));
    777:     PetscCall(MatMFFDSetOptionsPrefix(J, ((PetscObject)snes)->prefix));
    778:     PetscCall(MatSetFromOptions(J));
    779:     PetscCall(DMCreateMatrix(snes->dm, &B));
    780:     /* sdm->computejacobian was already set to reach here */
    781:     PetscCall(SNESSetJacobian(snes, J, B, NULL, NULL));
    782:     PetscCall(MatDestroy(&J));
    783:     PetscCall(MatDestroy(&B));
    784:   } else if (!snes->jacobian_pre) {
    785:     PetscDS   prob;
    786:     Mat       J, B;
    787:     PetscBool hasPrec = PETSC_FALSE;

    789:     J = snes->jacobian;
    790:     PetscCall(DMGetDS(dm, &prob));
    791:     if (prob) PetscCall(PetscDSHasJacobianPreconditioner(prob, &hasPrec));
    792:     if (J) PetscCall(PetscObjectReference((PetscObject)J));
    793:     else if (hasPrec) PetscCall(DMCreateMatrix(snes->dm, &J));
    794:     PetscCall(DMCreateMatrix(snes->dm, &B));
    795:     PetscCall(SNESSetJacobian(snes, J ? J : B, B, NULL, NULL));
    796:     PetscCall(MatDestroy(&J));
    797:     PetscCall(MatDestroy(&B));
    798:   }
    799:   {
    800:     KSP ksp;
    801:     PetscCall(SNESGetKSP(snes, &ksp));
    802:     PetscCall(KSPSetComputeOperators(ksp, KSPComputeOperators_SNES, snes));
    803:     PetscCall(DMCoarsenHookAdd(snes->dm, DMCoarsenHook_SNESVecSol, DMRestrictHook_SNESVecSol, snes));
    804:   }
    805:   PetscFunctionReturn(PETSC_SUCCESS);
    806: }

    808: static PetscErrorCode SNESMonitorPauseFinal_Internal(SNES snes)
    809: {
    810:   PetscInt i;

    812:   PetscFunctionBegin;
    813:   if (!snes->pauseFinal) PetscFunctionReturn(PETSC_SUCCESS);
    814:   for (i = 0; i < snes->numbermonitors; ++i) {
    815:     PetscViewerAndFormat *vf = (PetscViewerAndFormat *)snes->monitorcontext[i];
    816:     PetscDraw             draw;
    817:     PetscReal             lpause;

    819:     if (!vf) continue;
    820:     if (vf->lg) {
    821:       if (!PetscCheckPointer(vf->lg, PETSC_OBJECT)) continue;
    822:       if (((PetscObject)vf->lg)->classid != PETSC_DRAWLG_CLASSID) continue;
    823:       PetscCall(PetscDrawLGGetDraw(vf->lg, &draw));
    824:       PetscCall(PetscDrawGetPause(draw, &lpause));
    825:       PetscCall(PetscDrawSetPause(draw, -1.0));
    826:       PetscCall(PetscDrawPause(draw));
    827:       PetscCall(PetscDrawSetPause(draw, lpause));
    828:     } else {
    829:       PetscBool isdraw;

    831:       if (!PetscCheckPointer(vf->viewer, PETSC_OBJECT)) continue;
    832:       if (((PetscObject)vf->viewer)->classid != PETSC_VIEWER_CLASSID) continue;
    833:       PetscCall(PetscObjectTypeCompare((PetscObject)vf->viewer, PETSCVIEWERDRAW, &isdraw));
    834:       if (!isdraw) continue;
    835:       PetscCall(PetscViewerDrawGetDraw(vf->viewer, 0, &draw));
    836:       PetscCall(PetscDrawGetPause(draw, &lpause));
    837:       PetscCall(PetscDrawSetPause(draw, -1.0));
    838:       PetscCall(PetscDrawPause(draw));
    839:       PetscCall(PetscDrawSetPause(draw, lpause));
    840:     }
    841:   }
    842:   PetscFunctionReturn(PETSC_SUCCESS);
    843: }

    845: /*@C
    846:   SNESMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user

    848:   Collective

    850:   Input Parameters:
    851: + snes         - `SNES` object you wish to monitor
    852: . name         - the monitor type one is seeking
    853: . help         - message indicating what monitoring is done
    854: . manual       - manual page for the monitor
    855: . monitor      - the monitor function
    856: - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the `SNES` or `PetscViewer` objects

    858:   Calling sequence of `monitor`:
    859: + snes - the nonlinear solver context
    860: . it   - the current iteration
    861: . r    - the current function norm
    862: - vf   - a `PetscViewerAndFormat` struct that contains the `PetscViewer` and `PetscViewerFormat` to use

    864:   Calling sequence of `monitorsetup`:
    865: + snes - the nonlinear solver context
    866: - vf   - a `PetscViewerAndFormat` struct that contains the `PetscViewer` and `PetscViewerFormat` to use

    868:   Options Database Key:
    869: . -name - trigger the use of this monitor in `SNESSetFromOptions()`

    871:   Level: advanced

    873: .seealso: [](ch_snes), `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
    874:           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
    875:           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
    876:           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
    877:           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
    878:           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
    879:           `PetscOptionsFList()`, `PetscOptionsEList()`
    880: @*/
    881: PetscErrorCode SNESMonitorSetFromOptions(SNES snes, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(SNES snes, PetscInt it, PetscReal r, PetscViewerAndFormat *vf), PetscErrorCode (*monitorsetup)(SNES snes, PetscViewerAndFormat *vf))
    882: {
    883:   PetscViewer       viewer;
    884:   PetscViewerFormat format;
    885:   PetscBool         flg;

    887:   PetscFunctionBegin;
    888:   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, name, &viewer, &format, &flg));
    889:   if (flg) {
    890:     PetscViewerAndFormat *vf;
    891:     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
    892:     PetscCall(PetscViewerDestroy(&viewer));
    893:     if (monitorsetup) PetscCall((*monitorsetup)(snes, vf));
    894:     PetscCall(SNESMonitorSet(snes, (PetscErrorCode (*)(SNES, PetscInt, PetscReal, void *))monitor, vf, (PetscErrorCode (*)(void **))PetscViewerAndFormatDestroy));
    895:   }
    896:   PetscFunctionReturn(PETSC_SUCCESS);
    897: }

    899: PetscErrorCode SNESEWSetFromOptions_Private(SNESKSPEW *kctx, PetscBool print_api, MPI_Comm comm, const char *prefix)
    900: {
    901:   const char *api = print_api ? "SNESKSPSetParametersEW" : NULL;

    903:   PetscFunctionBegin;
    904:   PetscOptionsBegin(comm, prefix, "Eisenstat and Walker type forcing options", "KSP");
    905:   PetscCall(PetscOptionsInt("-ksp_ew_version", "Version 1, 2 or 3", api, kctx->version, &kctx->version, NULL));
    906:   PetscCall(PetscOptionsReal("-ksp_ew_rtol0", "0 <= rtol0 < 1", api, kctx->rtol_0, &kctx->rtol_0, NULL));
    907:   kctx->rtol_max = PetscMax(kctx->rtol_0, kctx->rtol_max);
    908:   PetscCall(PetscOptionsReal("-ksp_ew_rtolmax", "0 <= rtolmax < 1", api, kctx->rtol_max, &kctx->rtol_max, NULL));
    909:   PetscCall(PetscOptionsReal("-ksp_ew_gamma", "0 <= gamma <= 1", api, kctx->gamma, &kctx->gamma, NULL));
    910:   PetscCall(PetscOptionsReal("-ksp_ew_alpha", "1 < alpha <= 2", api, kctx->alpha, &kctx->alpha, NULL));
    911:   PetscCall(PetscOptionsReal("-ksp_ew_alpha2", "alpha2", NULL, kctx->alpha2, &kctx->alpha2, NULL));
    912:   PetscCall(PetscOptionsReal("-ksp_ew_threshold", "0 < threshold < 1", api, kctx->threshold, &kctx->threshold, NULL));
    913:   PetscCall(PetscOptionsReal("-ksp_ew_v4_p1", "p1", NULL, kctx->v4_p1, &kctx->v4_p1, NULL));
    914:   PetscCall(PetscOptionsReal("-ksp_ew_v4_p2", "p2", NULL, kctx->v4_p2, &kctx->v4_p2, NULL));
    915:   PetscCall(PetscOptionsReal("-ksp_ew_v4_p3", "p3", NULL, kctx->v4_p3, &kctx->v4_p3, NULL));
    916:   PetscCall(PetscOptionsReal("-ksp_ew_v4_m1", "Scaling when rk-1 in [p2,p3)", NULL, kctx->v4_m1, &kctx->v4_m1, NULL));
    917:   PetscCall(PetscOptionsReal("-ksp_ew_v4_m2", "Scaling when rk-1 in [p3,+infty)", NULL, kctx->v4_m2, &kctx->v4_m2, NULL));
    918:   PetscCall(PetscOptionsReal("-ksp_ew_v4_m3", "Threshold for successive rtol (0.1 in Eq.7)", NULL, kctx->v4_m3, &kctx->v4_m3, NULL));
    919:   PetscCall(PetscOptionsReal("-ksp_ew_v4_m4", "Adaptation scaling (0.5 in Eq.7)", NULL, kctx->v4_m4, &kctx->v4_m4, NULL));
    920:   PetscOptionsEnd();
    921:   PetscFunctionReturn(PETSC_SUCCESS);
    922: }

    924: /*@
    925:   SNESSetFromOptions - Sets various `SNES` and `KSP` parameters from user options.

    927:   Collective

    929:   Input Parameter:
    930: . snes - the `SNES` context

    932:   Options Database Keys:
    933: + -snes_type <type>                                                            - newtonls, newtontr, ngmres, ncg, nrichardson, qn, vi, fas, `SNESType` for complete list
    934: . -snes_rtol <rtol>                                                            - relative decrease in tolerance norm from initial
    935: . -snes_atol <abstol>                                                          - absolute tolerance of residual norm
    936: . -snes_stol <stol>                                                            - convergence tolerance in terms of the norm of the change in the solution between steps
    937: . -snes_divergence_tolerance <divtol>                                          - if the residual goes above divtol*rnorm0, exit with divergence
    938: . -snes_max_it <max_it>                                                        - maximum number of iterations
    939: . -snes_max_funcs <max_funcs>                                                  - maximum number of function evaluations
    940: . -snes_force_iteration <force>                                                - force `SNESSolve()` to take at least one iteration
    941: . -snes_max_fail <max_fail>                                                    - maximum number of line search failures allowed before stopping, default is none
    942: . -snes_max_linear_solve_fail                                                  - number of linear solver failures before SNESSolve() stops
    943: . -snes_lag_preconditioner <lag>                                               - how often preconditioner is rebuilt (use -1 to never rebuild)
    944: . -snes_lag_preconditioner_persists <true,false>                               - retains the -snes_lag_preconditioner information across multiple SNESSolve()
    945: . -snes_lag_jacobian <lag>                                                     - how often Jacobian is rebuilt (use -1 to never rebuild)
    946: . -snes_lag_jacobian_persists <true,false>                                     - retains the -snes_lag_jacobian information across multiple SNESSolve()
    947: . -snes_convergence_test <default,skip,correct_pressure>                       - convergence test in nonlinear solver. default `SNESConvergedDefault()`. skip `SNESConvergedSkip()` means continue iterating until max_it or some other criterion is reached, saving expense of convergence test. correct_pressure `SNESConvergedCorrectPressure()` has special handling of a pressure null space.
    948: . -snes_monitor [ascii][:filename][:viewer format]                             - prints residual norm at each iteration. if no filename given prints to stdout
    949: . -snes_monitor_solution [ascii binary draw][:filename][:viewer format]        - plots solution at each iteration
    950: . -snes_monitor_residual [ascii binary draw][:filename][:viewer format]        - plots residual (not its norm) at each iteration
    951: . -snes_monitor_solution_update [ascii binary draw][:filename][:viewer format] - plots update to solution at each iteration
    952: . -snes_monitor_lg_residualnorm                                                - plots residual norm at each iteration
    953: . -snes_monitor_lg_range                                                       - plots residual norm at each iteration
    954: . -snes_monitor_pause_final                                                    - Pauses all monitor drawing after the solver ends
    955: . -snes_fd                                                                     - use finite differences to compute Jacobian; very slow, only for testing
    956: . -snes_fd_color                                                               - use finite differences with coloring to compute Jacobian
    957: . -snes_mf_ksp_monitor                                                         - if using matrix-free multiply then print h at each `KSP` iteration
    958: . -snes_converged_reason                                                       - print the reason for convergence/divergence after each solve
    959: . -npc_snes_type <type>                                                        - the `SNES` type to use as a nonlinear preconditioner
    960: . -snes_test_jacobian <optional threshold>                                     - compare the user provided Jacobian with one computed via finite differences to check for errors.  If a threshold is given, display only those entries whose difference is greater than the threshold.
    961: - -snes_test_jacobian_view                                                     - display the user provided Jacobian, the finite difference Jacobian and the difference between them to help users detect the location of errors in the user provided Jacobian.

    963:   Options Database Keys for Eisenstat-Walker method:
    964: + -snes_ksp_ew                       - use Eisenstat-Walker method for determining linear system convergence
    965: . -snes_ksp_ew_version ver           - version of  Eisenstat-Walker method
    966: . -snes_ksp_ew_rtol0 <rtol0>         - Sets rtol0
    967: . -snes_ksp_ew_rtolmax <rtolmax>     - Sets rtolmax
    968: . -snes_ksp_ew_gamma <gamma>         - Sets gamma
    969: . -snes_ksp_ew_alpha <alpha>         - Sets alpha
    970: . -snes_ksp_ew_alpha2 <alpha2>       - Sets alpha2
    971: - -snes_ksp_ew_threshold <threshold> - Sets threshold

    973:   Level: beginner

    975:   Notes:
    976:   To see all options, run your program with the -help option or consult the users manual

    978:   `SNES` supports three approaches for computing (approximate) Jacobians: user provided via `SNESSetJacobian()`, matrix-free using `MatCreateSNESMF()`,
    979:   and computing explicitly with
    980:   finite differences and coloring using `MatFDColoring`. It is also possible to use automatic differentiation and the `MatFDColoring` object.

    982: .seealso: [](ch_snes), `SNESType`, `SNESSetOptionsPrefix()`, `SNESResetFromOptions()`, `SNES`, `SNESCreate()`, `MatCreateSNESMF()`, `MatFDColoring`
    983: @*/
    984: PetscErrorCode SNESSetFromOptions(SNES snes)
    985: {
    986:   PetscBool   flg, pcset, persist, set;
    987:   PetscInt    i, indx, lag, grids, max_its, max_funcs;
    988:   const char *deft        = SNESNEWTONLS;
    989:   const char *convtests[] = {"default", "skip", "correct_pressure"};
    990:   SNESKSPEW  *kctx        = NULL;
    991:   char        type[256], monfilename[PETSC_MAX_PATH_LEN], ewprefix[256];
    992:   PCSide      pcside;
    993:   const char *optionsprefix;
    994:   PetscReal   rtol, abstol, stol;

    996:   PetscFunctionBegin;
    998:   PetscCall(SNESRegisterAll());
    999:   PetscObjectOptionsBegin((PetscObject)snes);
    1000:   if (((PetscObject)snes)->type_name) deft = ((PetscObject)snes)->type_name;
    1001:   PetscCall(PetscOptionsFList("-snes_type", "Nonlinear solver method", "SNESSetType", SNESList, deft, type, 256, &flg));
    1002:   if (flg) {
    1003:     PetscCall(SNESSetType(snes, type));
    1004:   } else if (!((PetscObject)snes)->type_name) {
    1005:     PetscCall(SNESSetType(snes, deft));
    1006:   }

    1008:   abstol    = snes->abstol;
    1009:   rtol      = snes->rtol;
    1010:   stol      = snes->stol;
    1011:   max_its   = snes->max_its;
    1012:   max_funcs = snes->max_funcs;
    1013:   PetscCall(PetscOptionsReal("-snes_rtol", "Stop if decrease in function norm less than", "SNESSetTolerances", snes->rtol, &rtol, NULL));
    1014:   PetscCall(PetscOptionsReal("-snes_atol", "Stop if function norm less than", "SNESSetTolerances", snes->abstol, &abstol, NULL));
    1015:   PetscCall(PetscOptionsReal("-snes_stol", "Stop if step length less than", "SNESSetTolerances", snes->stol, &stol, NULL));
    1016:   PetscCall(PetscOptionsInt("-snes_max_it", "Maximum iterations", "SNESSetTolerances", snes->max_its, &max_its, NULL));
    1017:   PetscCall(PetscOptionsInt("-snes_max_funcs", "Maximum function evaluations", "SNESSetTolerances", snes->max_funcs, &max_funcs, NULL));
    1018:   PetscCall(SNESSetTolerances(snes, abstol, rtol, stol, max_its, max_funcs));

    1020:   PetscCall(PetscOptionsReal("-snes_divergence_tolerance", "Stop if residual norm increases by this factor", "SNESSetDivergenceTolerance", snes->divtol, &snes->divtol, &flg));
    1021:   if (flg) PetscCall(SNESSetDivergenceTolerance(snes, snes->divtol));

    1023:   PetscCall(PetscOptionsInt("-snes_max_fail", "Maximum nonlinear step failures", "SNESSetMaxNonlinearStepFailures", snes->maxFailures, &snes->maxFailures, &flg));
    1024:   if (flg) PetscCall(SNESSetMaxNonlinearStepFailures(snes, snes->maxFailures));

    1026:   PetscCall(PetscOptionsInt("-snes_max_linear_solve_fail", "Maximum failures in linear solves allowed", "SNESSetMaxLinearSolveFailures", snes->maxLinearSolveFailures, &snes->maxLinearSolveFailures, &flg));
    1027:   if (flg) PetscCall(SNESSetMaxLinearSolveFailures(snes, snes->maxLinearSolveFailures));

    1029:   PetscCall(PetscOptionsBool("-snes_error_if_not_converged", "Generate error if solver does not converge", "SNESSetErrorIfNotConverged", snes->errorifnotconverged, &snes->errorifnotconverged, NULL));
    1030:   PetscCall(PetscOptionsBool("-snes_force_iteration", "Force SNESSolve() to take at least one iteration", "SNESSetForceIteration", snes->forceiteration, &snes->forceiteration, NULL));
    1031:   PetscCall(PetscOptionsBool("-snes_check_jacobian_domain_error", "Check Jacobian domain error after Jacobian evaluation", "SNESCheckJacobianDomainError", snes->checkjacdomainerror, &snes->checkjacdomainerror, NULL));

    1033:   PetscCall(PetscOptionsInt("-snes_lag_preconditioner", "How often to rebuild preconditioner", "SNESSetLagPreconditioner", snes->lagpreconditioner, &lag, &flg));
    1034:   if (flg) {
    1035:     PetscCheck(lag != -1, PetscObjectComm((PetscObject)snes), PETSC_ERR_USER, "Cannot set the lag to -1 from the command line since the preconditioner must be built as least once, perhaps you mean -2");
    1036:     PetscCall(SNESSetLagPreconditioner(snes, lag));
    1037:   }
    1038:   PetscCall(PetscOptionsBool("-snes_lag_preconditioner_persists", "Preconditioner lagging through multiple SNES solves", "SNESSetLagPreconditionerPersists", snes->lagjac_persist, &persist, &flg));
    1039:   if (flg) PetscCall(SNESSetLagPreconditionerPersists(snes, persist));
    1040:   PetscCall(PetscOptionsInt("-snes_lag_jacobian", "How often to rebuild Jacobian", "SNESSetLagJacobian", snes->lagjacobian, &lag, &flg));
    1041:   if (flg) {
    1042:     PetscCheck(lag != -1, PetscObjectComm((PetscObject)snes), PETSC_ERR_USER, "Cannot set the lag to -1 from the command line since the Jacobian must be built as least once, perhaps you mean -2");
    1043:     PetscCall(SNESSetLagJacobian(snes, lag));
    1044:   }
    1045:   PetscCall(PetscOptionsBool("-snes_lag_jacobian_persists", "Jacobian lagging through multiple SNES solves", "SNESSetLagJacobianPersists", snes->lagjac_persist, &persist, &flg));
    1046:   if (flg) PetscCall(SNESSetLagJacobianPersists(snes, persist));

    1048:   PetscCall(PetscOptionsInt("-snes_grid_sequence", "Use grid sequencing to generate initial guess", "SNESSetGridSequence", snes->gridsequence, &grids, &flg));
    1049:   if (flg) PetscCall(SNESSetGridSequence(snes, grids));

    1051:   PetscCall(PetscOptionsEList("-snes_convergence_test", "Convergence test", "SNESSetConvergenceTest", convtests, PETSC_STATIC_ARRAY_LENGTH(convtests), "default", &indx, &flg));
    1052:   if (flg) {
    1053:     switch (indx) {
    1054:     case 0:
    1055:       PetscCall(SNESSetConvergenceTest(snes, SNESConvergedDefault, NULL, NULL));
    1056:       break;
    1057:     case 1:
    1058:       PetscCall(SNESSetConvergenceTest(snes, SNESConvergedSkip, NULL, NULL));
    1059:       break;
    1060:     case 2:
    1061:       PetscCall(SNESSetConvergenceTest(snes, SNESConvergedCorrectPressure, NULL, NULL));
    1062:       break;
    1063:     }
    1064:   }

    1066:   PetscCall(PetscOptionsEList("-snes_norm_schedule", "SNES Norm schedule", "SNESSetNormSchedule", SNESNormSchedules, 5, "function", &indx, &flg));
    1067:   if (flg) PetscCall(SNESSetNormSchedule(snes, (SNESNormSchedule)indx));

    1069:   PetscCall(PetscOptionsEList("-snes_function_type", "SNES Norm schedule", "SNESSetFunctionType", SNESFunctionTypes, 2, "unpreconditioned", &indx, &flg));
    1070:   if (flg) PetscCall(SNESSetFunctionType(snes, (SNESFunctionType)indx));

    1072:   kctx = (SNESKSPEW *)snes->kspconvctx;

    1074:   PetscCall(PetscOptionsBool("-snes_ksp_ew", "Use Eisentat-Walker linear system convergence test", "SNESKSPSetUseEW", snes->ksp_ewconv, &snes->ksp_ewconv, NULL));

    1076:   PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix));
    1077:   PetscCall(PetscSNPrintf(ewprefix, sizeof(ewprefix), "%s%s", optionsprefix ? optionsprefix : "", "snes_"));
    1078:   PetscCall(SNESEWSetFromOptions_Private(kctx, PETSC_TRUE, PetscObjectComm((PetscObject)snes), ewprefix));

    1080:   flg = PETSC_FALSE;
    1081:   PetscCall(PetscOptionsBool("-snes_monitor_cancel", "Remove all monitors", "SNESMonitorCancel", flg, &flg, &set));
    1082:   if (set && flg) PetscCall(SNESMonitorCancel(snes));

    1084:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor", "Monitor norm of function", "SNESMonitorDefault", SNESMonitorDefault, SNESMonitorDefaultSetUp));
    1085:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_short", "Monitor norm of function with fewer digits", "SNESMonitorDefaultShort", SNESMonitorDefaultShort, NULL));
    1086:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_range", "Monitor range of elements of function", "SNESMonitorRange", SNESMonitorRange, NULL));

    1088:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_ratio", "Monitor ratios of the norm of function for consecutive steps", "SNESMonitorRatio", SNESMonitorRatio, SNESMonitorRatioSetUp));
    1089:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_field", "Monitor norm of function (split into fields)", "SNESMonitorDefaultField", SNESMonitorDefaultField, NULL));
    1090:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_solution", "View solution at each iteration", "SNESMonitorSolution", SNESMonitorSolution, NULL));
    1091:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_solution_update", "View correction at each iteration", "SNESMonitorSolutionUpdate", SNESMonitorSolutionUpdate, NULL));
    1092:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_residual", "View residual at each iteration", "SNESMonitorResidual", SNESMonitorResidual, NULL));
    1093:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_jacupdate_spectrum", "Print the change in the spectrum of the Jacobian", "SNESMonitorJacUpdateSpectrum", SNESMonitorJacUpdateSpectrum, NULL));
    1094:   PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_fields", "Monitor norm of function per field", "SNESMonitorSet", SNESMonitorFields, NULL));
    1095:   PetscCall(PetscOptionsBool("-snes_monitor_pause_final", "Pauses all draw monitors at the final iterate", "SNESMonitorPauseFinal_Internal", PETSC_FALSE, &snes->pauseFinal, NULL));

    1097:   PetscCall(PetscOptionsString("-snes_monitor_python", "Use Python function", "SNESMonitorSet", NULL, monfilename, sizeof(monfilename), &flg));
    1098:   if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)snes, monfilename));

    1100:   flg = PETSC_FALSE;
    1101:   PetscCall(PetscOptionsBool("-snes_monitor_lg_range", "Plot function range at each iteration", "SNESMonitorLGRange", flg, &flg, NULL));
    1102:   if (flg) {
    1103:     PetscViewer ctx;

    1105:     PetscCall(PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, &ctx));
    1106:     PetscCall(SNESMonitorSet(snes, SNESMonitorLGRange, ctx, (PetscErrorCode (*)(void **))PetscViewerDestroy));
    1107:   }

    1109:   PetscCall(PetscViewerDestroy(&snes->convergedreasonviewer));
    1110:   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_converged_reason", &snes->convergedreasonviewer, &snes->convergedreasonformat, NULL));
    1111:   flg = PETSC_FALSE;
    1112:   PetscCall(PetscOptionsBool("-snes_converged_reason_view_cancel", "Remove all converged reason viewers", "SNESConvergedReasonViewCancel", flg, &flg, &set));
    1113:   if (set && flg) PetscCall(SNESConvergedReasonViewCancel(snes));

    1115:   flg = PETSC_FALSE;
    1116:   PetscCall(PetscOptionsBool("-snes_fd", "Use finite differences (slow) to compute Jacobian", "SNESComputeJacobianDefault", flg, &flg, NULL));
    1117:   if (flg) {
    1118:     void *functx;
    1119:     DM    dm;
    1120:     PetscCall(SNESGetDM(snes, &dm));
    1121:     PetscCall(DMSNESUnsetJacobianContext_Internal(dm));
    1122:     PetscCall(SNESGetFunction(snes, NULL, NULL, &functx));
    1123:     PetscCall(SNESSetJacobian(snes, snes->jacobian, snes->jacobian_pre, SNESComputeJacobianDefault, functx));
    1124:     PetscCall(PetscInfo(snes, "Setting default finite difference Jacobian matrix\n"));
    1125:   }

    1127:   flg = PETSC_FALSE;
    1128:   PetscCall(PetscOptionsBool("-snes_fd_function", "Use finite differences (slow) to compute function from user objective", "SNESObjectiveComputeFunctionDefaultFD", flg, &flg, NULL));
    1129:   if (flg) PetscCall(SNESSetFunction(snes, NULL, SNESObjectiveComputeFunctionDefaultFD, NULL));

    1131:   flg = PETSC_FALSE;
    1132:   PetscCall(PetscOptionsBool("-snes_fd_color", "Use finite differences with coloring to compute Jacobian", "SNESComputeJacobianDefaultColor", flg, &flg, NULL));
    1133:   if (flg) {
    1134:     DM dm;
    1135:     PetscCall(SNESGetDM(snes, &dm));
    1136:     PetscCall(DMSNESUnsetJacobianContext_Internal(dm));
    1137:     PetscCall(SNESSetJacobian(snes, snes->jacobian, snes->jacobian_pre, SNESComputeJacobianDefaultColor, NULL));
    1138:     PetscCall(PetscInfo(snes, "Setting default finite difference coloring Jacobian matrix\n"));
    1139:   }

    1141:   flg = PETSC_FALSE;
    1142:   PetscCall(PetscOptionsBool("-snes_mf_operator", "Use a Matrix-Free Jacobian with user-provided preconditioner matrix", "SNESSetUseMatrixFree", PETSC_FALSE, &snes->mf_operator, &flg));
    1143:   if (flg && snes->mf_operator) {
    1144:     snes->mf_operator = PETSC_TRUE;
    1145:     snes->mf          = PETSC_TRUE;
    1146:   }
    1147:   flg = PETSC_FALSE;
    1148:   PetscCall(PetscOptionsBool("-snes_mf", "Use a Matrix-Free Jacobian with no preconditioner matrix", "SNESSetUseMatrixFree", PETSC_FALSE, &snes->mf, &flg));
    1149:   if (!flg && snes->mf_operator) snes->mf = PETSC_TRUE;
    1150:   PetscCall(PetscOptionsInt("-snes_mf_version", "Matrix-Free routines version 1 or 2", "None", snes->mf_version, &snes->mf_version, NULL));

    1152:   flg = PETSC_FALSE;
    1153:   PetscCall(SNESGetNPCSide(snes, &pcside));
    1154:   PetscCall(PetscOptionsEnum("-snes_npc_side", "SNES nonlinear preconditioner side", "SNESSetNPCSide", PCSides, (PetscEnum)pcside, (PetscEnum *)&pcside, &flg));
    1155:   if (flg) PetscCall(SNESSetNPCSide(snes, pcside));

    1157: #if defined(PETSC_HAVE_SAWS)
    1158:   /*
    1159:     Publish convergence information using SAWs
    1160:   */
    1161:   flg = PETSC_FALSE;
    1162:   PetscCall(PetscOptionsBool("-snes_monitor_saws", "Publish SNES progress using SAWs", "SNESMonitorSet", flg, &flg, NULL));
    1163:   if (flg) {
    1164:     void *ctx;
    1165:     PetscCall(SNESMonitorSAWsCreate(snes, &ctx));
    1166:     PetscCall(SNESMonitorSet(snes, SNESMonitorSAWs, ctx, SNESMonitorSAWsDestroy));
    1167:   }
    1168: #endif
    1169: #if defined(PETSC_HAVE_SAWS)
    1170:   {
    1171:     PetscBool set;
    1172:     flg = PETSC_FALSE;
    1173:     PetscCall(PetscOptionsBool("-snes_saws_block", "Block for SAWs at end of SNESSolve", "PetscObjectSAWsBlock", ((PetscObject)snes)->amspublishblock, &flg, &set));
    1174:     if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)snes, flg));
    1175:   }
    1176: #endif

    1178:   for (i = 0; i < numberofsetfromoptions; i++) PetscCall((*othersetfromoptions[i])(snes));

    1180:   PetscTryTypeMethod(snes, setfromoptions, PetscOptionsObject);

    1182:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
    1183:   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)snes, PetscOptionsObject));
    1184:   PetscOptionsEnd();

    1186:   if (snes->linesearch) {
    1187:     PetscCall(SNESGetLineSearch(snes, &snes->linesearch));
    1188:     PetscCall(SNESLineSearchSetFromOptions(snes->linesearch));
    1189:   }

    1191:   if (snes->usesksp) {
    1192:     if (!snes->ksp) PetscCall(SNESGetKSP(snes, &snes->ksp));
    1193:     PetscCall(KSPSetOperators(snes->ksp, snes->jacobian, snes->jacobian_pre));
    1194:     PetscCall(KSPSetFromOptions(snes->ksp));
    1195:   }

    1197:   /* if user has set the SNES NPC type via options database, create it. */
    1198:   PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix));
    1199:   PetscCall(PetscOptionsHasName(((PetscObject)snes)->options, optionsprefix, "-npc_snes_type", &pcset));
    1200:   if (pcset && (!snes->npc)) PetscCall(SNESGetNPC(snes, &snes->npc));
    1201:   if (snes->npc) PetscCall(SNESSetFromOptions(snes->npc));
    1202:   snes->setfromoptionscalled++;
    1203:   PetscFunctionReturn(PETSC_SUCCESS);
    1204: }

    1206: /*@
    1207:   SNESResetFromOptions - Sets various `SNES` and `KSP` parameters from user options ONLY if the `SNESSetFromOptions()` was previously called

    1209:   Collective

    1211:   Input Parameter:
    1212: . snes - the `SNES` context

    1214:   Level: advanced

    1216: .seealso: [](ch_snes), `SNES`, `SNESSetFromOptions()`, `SNESSetOptionsPrefix()`
    1217: @*/
    1218: PetscErrorCode SNESResetFromOptions(SNES snes)
    1219: {
    1220:   PetscFunctionBegin;
    1221:   if (snes->setfromoptionscalled) PetscCall(SNESSetFromOptions(snes));
    1222:   PetscFunctionReturn(PETSC_SUCCESS);
    1223: }

    1225: /*@C
    1226:   SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for
    1227:   the nonlinear solvers.

    1229:   Logically Collective; No Fortran Support

    1231:   Input Parameters:
    1232: + snes    - the `SNES` context
    1233: . compute - function to compute the context
    1234: - destroy - function to destroy the context

    1236:   Calling sequence of `compute`:
    1237: + snes - the `SNES` context
    1238: - ctx  - context to be computed

    1240:   Calling sequence of `destroy`:
    1241: . ctx - context to be computed by `compute()`

    1243:   Level: intermediate

    1245:   Note:
    1246:   This routine is useful if you are performing grid sequencing or using `SNESFAS` and need the appropriate context generated for each level.

    1248:   Use `SNESSetApplicationContext()` to see the context immediately

    1250: .seealso: [](ch_snes), `SNESGetApplicationContext()`, `SNESSetApplicationContext()`
    1251: @*/
    1252: PetscErrorCode SNESSetComputeApplicationContext(SNES snes, PetscErrorCode (*compute)(SNES snes, void **ctx), PetscErrorCode (*destroy)(void **ctx))
    1253: {
    1254:   PetscFunctionBegin;
    1256:   snes->ops->usercompute = compute;
    1257:   snes->ops->userdestroy = destroy;
    1258:   PetscFunctionReturn(PETSC_SUCCESS);
    1259: }

    1261: /*@
    1262:   SNESSetApplicationContext - Sets the optional user-defined context for the nonlinear solvers.

    1264:   Logically Collective

    1266:   Input Parameters:
    1267: + snes - the `SNES` context
    1268: - usrP - optional user context

    1270:   Level: intermediate

    1272:   Notes:
    1273:   Users can provide a context when constructing the `SNES` options and then access it inside their function, Jacobian, or other evaluation function
    1274:   with `SNESGetApplicationContext()`

    1276:   To provide a function that computes the context for you use `SNESSetComputeApplicationContext()`

    1278:   Fortran Note:
    1279:   You must write a Fortran interface definition for this
    1280:   function that tells Fortran the Fortran derived data type that you are passing in as the `usrP` argument.

    1282: .seealso: [](ch_snes), `SNES`, `SNESSetComputeApplicationContext()`, `SNESGetApplicationContext()`
    1283: @*/
    1284: PetscErrorCode SNESSetApplicationContext(SNES snes, void *usrP)
    1285: {
    1286:   KSP ksp;

    1288:   PetscFunctionBegin;
    1290:   PetscCall(SNESGetKSP(snes, &ksp));
    1291:   PetscCall(KSPSetApplicationContext(ksp, usrP));
    1292:   snes->user = usrP;
    1293:   PetscFunctionReturn(PETSC_SUCCESS);
    1294: }

    1296: /*@
    1297:   SNESGetApplicationContext - Gets the user-defined context for the
    1298:   nonlinear solvers set with `SNESGetApplicationContext()` or `SNESSetComputeApplicationContext()`

    1300:   Not Collective

    1302:   Input Parameter:
    1303: . snes - `SNES` context

    1305:   Output Parameter:
    1306: . usrP - user context

    1308:   Level: intermediate

    1310:   Fortran Note:
    1311:   You must write a Fortran interface definition for this
    1312:   function that tells Fortran the Fortran derived data type that you are passing in as the `usrP` argument.

    1314: .seealso: [](ch_snes), `SNESSetApplicationContext()`, `SNESSetComputeApplicationContext()`
    1315: @*/
    1316: PetscErrorCode SNESGetApplicationContext(SNES snes, void *usrP)
    1317: {
    1318:   PetscFunctionBegin;
    1320:   *(void **)usrP = snes->user;
    1321:   PetscFunctionReturn(PETSC_SUCCESS);
    1322: }

    1324: /*@
    1325:   SNESSetUseMatrixFree - indicates that `SNES` should use matrix-free finite difference matrix-vector products to apply the Jacobian.

    1327:   Logically Collective

    1329:   Input Parameters:
    1330: + snes        - `SNES` context
    1331: . mf_operator - use matrix-free only for the Amat used by `SNESSetJacobian()`, this means the user provided Pmat will continue to be used
    1332: - mf          - use matrix-free for both the Amat and Pmat used by `SNESSetJacobian()`, both the Amat and Pmat set in `SNESSetJacobian()` will be ignored. With
    1333:                 this option no matrix-element based preconditioners can be used in the linear solve since the matrix won't be explicitly available

    1335:   Options Database Keys:
    1336: + -snes_mf_operator - use matrix-free only for the mat operator
    1337: . -snes_mf          - use matrix-free for both the mat and pmat operator
    1338: . -snes_fd_color    - compute the Jacobian via coloring and finite differences.
    1339: - -snes_fd          - compute the Jacobian via finite differences (slow)

    1341:   Level: intermediate

    1343:   Note:
    1344:   `SNES` supports three approaches for computing (approximate) Jacobians: user provided via `SNESSetJacobian()`, matrix-free using `MatCreateSNESMF()`,
    1345:   and computing explicitly with
    1346:   finite differences and coloring using `MatFDColoring`. It is also possible to use automatic differentiation and the `MatFDColoring` object.

    1348: .seealso: [](ch_snes), `SNES`, `SNESGetUseMatrixFree()`, `MatCreateSNESMF()`, `SNESComputeJacobianDefaultColor()`, `MatFDColoring`
    1349: @*/
    1350: PetscErrorCode SNESSetUseMatrixFree(SNES snes, PetscBool mf_operator, PetscBool mf)
    1351: {
    1352:   PetscFunctionBegin;
    1356:   snes->mf          = mf_operator ? PETSC_TRUE : mf;
    1357:   snes->mf_operator = mf_operator;
    1358:   PetscFunctionReturn(PETSC_SUCCESS);
    1359: }

    1361: /*@
    1362:   SNESGetUseMatrixFree - indicates if the `SNES` uses matrix-free finite difference matrix vector products to apply the Jacobian.

    1364:   Not Collective, but the resulting flags will be the same on all MPI processes

    1366:   Input Parameter:
    1367: . snes - `SNES` context

    1369:   Output Parameters:
    1370: + mf_operator - use matrix-free only for the Amat used by `SNESSetJacobian()`, this means the user provided Pmat will continue to be used
    1371: - mf          - use matrix-free for both the Amat and Pmat used by `SNESSetJacobian()`, both the Amat and Pmat set in `SNESSetJacobian()` will be ignored

    1373:   Level: intermediate

    1375: .seealso: [](ch_snes), `SNES`, `SNESSetUseMatrixFree()`, `MatCreateSNESMF()`
    1376: @*/
    1377: PetscErrorCode SNESGetUseMatrixFree(SNES snes, PetscBool *mf_operator, PetscBool *mf)
    1378: {
    1379:   PetscFunctionBegin;
    1381:   if (mf) *mf = snes->mf;
    1382:   if (mf_operator) *mf_operator = snes->mf_operator;
    1383:   PetscFunctionReturn(PETSC_SUCCESS);
    1384: }

    1386: /*@
    1387:   SNESGetIterationNumber - Gets the number of nonlinear iterations completed in the current or most recent `SNESSolve()`

    1389:   Not Collective

    1391:   Input Parameter:
    1392: . snes - `SNES` context

    1394:   Output Parameter:
    1395: . iter - iteration number

    1397:   Level: intermediate

    1399:   Notes:
    1400:   For example, during the computation of iteration 2 this would return 1.

    1402:   This is useful for using lagged Jacobians (where one does not recompute the
    1403:   Jacobian at each `SNES` iteration). For example, the code
    1404: .vb
    1405:       ierr = SNESGetIterationNumber(snes,&it);
    1406:       if (!(it % 2)) {
    1407:         [compute Jacobian here]
    1408:       }
    1409: .ve
    1410:   can be used in your function that computes the Jacobian to cause the Jacobian to be
    1411:   recomputed every second `SNES` iteration. See also `SNESSetLagJacobian()`

    1413:   After the `SNES` solve is complete this will return the number of nonlinear iterations used.

    1415: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESSetLagJacobian()`, `SNESGetLinearSolveIterations()`, `SNESSetMonitor()`
    1416: @*/
    1417: PetscErrorCode SNESGetIterationNumber(SNES snes, PetscInt *iter)
    1418: {
    1419:   PetscFunctionBegin;
    1421:   PetscAssertPointer(iter, 2);
    1422:   *iter = snes->iter;
    1423:   PetscFunctionReturn(PETSC_SUCCESS);
    1424: }

    1426: /*@
    1427:   SNESSetIterationNumber - Sets the current iteration number.

    1429:   Not Collective

    1431:   Input Parameters:
    1432: + snes - `SNES` context
    1433: - iter - iteration number

    1435:   Level: developer

    1437:   Note:
    1438:   This should only be called inside a `SNES` nonlinear solver.

    1440: .seealso: [](ch_snes), `SNESGetLinearSolveIterations()`
    1441: @*/
    1442: PetscErrorCode SNESSetIterationNumber(SNES snes, PetscInt iter)
    1443: {
    1444:   PetscFunctionBegin;
    1446:   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
    1447:   snes->iter = iter;
    1448:   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
    1449:   PetscFunctionReturn(PETSC_SUCCESS);
    1450: }

    1452: /*@
    1453:   SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
    1454:   attempted by the nonlinear solver in the current or most recent `SNESSolve()` .

    1456:   Not Collective

    1458:   Input Parameter:
    1459: . snes - `SNES` context

    1461:   Output Parameter:
    1462: . nfails - number of unsuccessful steps attempted

    1464:   Level: intermediate

    1466:   Note:
    1467:   This counter is reset to zero for each successive call to `SNESSolve()`.

    1469: .seealso: [](ch_snes), `SNES`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`,
    1470:           `SNESSetMaxNonlinearStepFailures()`, `SNESGetMaxNonlinearStepFailures()`
    1471: @*/
    1472: PetscErrorCode SNESGetNonlinearStepFailures(SNES snes, PetscInt *nfails)
    1473: {
    1474:   PetscFunctionBegin;
    1476:   PetscAssertPointer(nfails, 2);
    1477:   *nfails = snes->numFailures;
    1478:   PetscFunctionReturn(PETSC_SUCCESS);
    1479: }

    1481: /*@
    1482:   SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
    1483:   attempted by the nonlinear solver before it gives up and returns unconverged or generates an error

    1485:   Not Collective

    1487:   Input Parameters:
    1488: + snes     - `SNES` context
    1489: - maxFails - maximum of unsuccessful steps allowed, use `PETSC_UNLIMITED` to have no limit on the number of failures

    1491:   Options Database Key:
    1492: . -snes_max_fail <n> - maximum number of unsuccessful steps allowed

    1494:   Level: intermediate

    1496:   Developer Note:
    1497:   The options database key is wrong for this function name

    1499: .seealso: [](ch_snes), `SNESSetErrorIfNotConverged()`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`,
    1500:           `SNESGetMaxNonlinearStepFailures()`, `SNESGetNonlinearStepFailures()`
    1501: @*/
    1502: PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
    1503: {
    1504:   PetscFunctionBegin;

    1507:   if (maxFails == PETSC_UNLIMITED) {
    1508:     snes->maxFailures = PETSC_INT_MAX;
    1509:   } else {
    1510:     PetscCheck(maxFails >= 0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Cannot have a negative maximum number of failures");
    1511:     snes->maxFailures = maxFails;
    1512:   }
    1513:   PetscFunctionReturn(PETSC_SUCCESS);
    1514: }

    1516: /*@
    1517:   SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
    1518:   attempted by the nonlinear solver before it gives up and returns unconverged or generates an error

    1520:   Not Collective

    1522:   Input Parameter:
    1523: . snes - `SNES` context

    1525:   Output Parameter:
    1526: . maxFails - maximum of unsuccessful steps

    1528:   Level: intermediate

    1530: .seealso: [](ch_snes), `SNESSetErrorIfNotConverged()`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`,
    1531:           `SNESSetMaxNonlinearStepFailures()`, `SNESGetNonlinearStepFailures()`
    1532: @*/
    1533: PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
    1534: {
    1535:   PetscFunctionBegin;
    1537:   PetscAssertPointer(maxFails, 2);
    1538:   *maxFails = snes->maxFailures;
    1539:   PetscFunctionReturn(PETSC_SUCCESS);
    1540: }

    1542: /*@
    1543:   SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
    1544:   done by the `SNES` object in the current or most recent `SNESSolve()`

    1546:   Not Collective

    1548:   Input Parameter:
    1549: . snes - `SNES` context

    1551:   Output Parameter:
    1552: . nfuncs - number of evaluations

    1554:   Level: intermediate

    1556:   Note:
    1557:   Reset every time `SNESSolve()` is called unless `SNESSetCountersReset()` is used.

    1559: .seealso: [](ch_snes), `SNES`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`, `SNESSetCountersReset()`
    1560: @*/
    1561: PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
    1562: {
    1563:   PetscFunctionBegin;
    1565:   PetscAssertPointer(nfuncs, 2);
    1566:   *nfuncs = snes->nfuncs;
    1567:   PetscFunctionReturn(PETSC_SUCCESS);
    1568: }

    1570: /*@
    1571:   SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
    1572:   linear solvers in the current or most recent `SNESSolve()`

    1574:   Not Collective

    1576:   Input Parameter:
    1577: . snes - `SNES` context

    1579:   Output Parameter:
    1580: . nfails - number of failed solves

    1582:   Options Database Key:
    1583: . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated

    1585:   Level: intermediate

    1587:   Note:
    1588:   This counter is reset to zero for each successive call to `SNESSolve()`.

    1590: .seealso: [](ch_snes), `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`
    1591: @*/
    1592: PetscErrorCode SNESGetLinearSolveFailures(SNES snes, PetscInt *nfails)
    1593: {
    1594:   PetscFunctionBegin;
    1596:   PetscAssertPointer(nfails, 2);
    1597:   *nfails = snes->numLinearSolveFailures;
    1598:   PetscFunctionReturn(PETSC_SUCCESS);
    1599: }

    1601: /*@
    1602:   SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
    1603:   allowed before `SNES` returns with a diverged reason of `SNES_DIVERGED_LINEAR_SOLVE`

    1605:   Logically Collective

    1607:   Input Parameters:
    1608: + snes     - `SNES` context
    1609: - maxFails - maximum allowed linear solve failures, use `PETSC_UNLIMITED` to have no limit on the number of failures

    1611:   Options Database Key:
    1612: . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated

    1614:   Level: intermediate

    1616:   Note:
    1617:   By default this is 0; that is `SNES` returns on the first failed linear solve

    1619:   Developer Note:
    1620:   The options database key is wrong for this function name

    1622: .seealso: [](ch_snes), `SNESSetErrorIfNotConverged()`, `SNESGetLinearSolveFailures()`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`
    1623: @*/
    1624: PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
    1625: {
    1626:   PetscFunctionBegin;

    1630:   if (maxFails == PETSC_UNLIMITED) {
    1631:     snes->maxLinearSolveFailures = PETSC_INT_MAX;
    1632:   } else {
    1633:     PetscCheck(maxFails >= 0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Cannot have a negative maximum number of failures");
    1634:     snes->maxLinearSolveFailures = maxFails;
    1635:   }
    1636:   PetscFunctionReturn(PETSC_SUCCESS);
    1637: }

    1639: /*@
    1640:   SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
    1641:   are allowed before `SNES` returns as unsuccessful

    1643:   Not Collective

    1645:   Input Parameter:
    1646: . snes - `SNES` context

    1648:   Output Parameter:
    1649: . maxFails - maximum of unsuccessful solves allowed

    1651:   Level: intermediate

    1653:   Note:
    1654:   By default this is 1; that is `SNES` returns on the first failed linear solve

    1656: .seealso: [](ch_snes), `SNESSetErrorIfNotConverged()`, `SNESGetLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`,
    1657: @*/
    1658: PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
    1659: {
    1660:   PetscFunctionBegin;
    1662:   PetscAssertPointer(maxFails, 2);
    1663:   *maxFails = snes->maxLinearSolveFailures;
    1664:   PetscFunctionReturn(PETSC_SUCCESS);
    1665: }

    1667: /*@
    1668:   SNESGetLinearSolveIterations - Gets the total number of linear iterations
    1669:   used by the nonlinear solver in the most recent `SNESSolve()`

    1671:   Not Collective

    1673:   Input Parameter:
    1674: . snes - `SNES` context

    1676:   Output Parameter:
    1677: . lits - number of linear iterations

    1679:   Level: intermediate

    1681:   Notes:
    1682:   This counter is reset to zero for each successive call to `SNESSolve()` unless `SNESSetCountersReset()` is used.

    1684:   If the linear solver fails inside the `SNESSolve()` the iterations for that call to the linear solver are not included. If you wish to count them
    1685:   then call `KSPGetIterationNumber()` after the failed solve.

    1687: .seealso: [](ch_snes), `SNES`, `SNESGetIterationNumber()`, `SNESGetLinearSolveFailures()`, `SNESGetMaxLinearSolveFailures()`, `SNESSetCountersReset()`
    1688: @*/
    1689: PetscErrorCode SNESGetLinearSolveIterations(SNES snes, PetscInt *lits)
    1690: {
    1691:   PetscFunctionBegin;
    1693:   PetscAssertPointer(lits, 2);
    1694:   *lits = snes->linear_its;
    1695:   PetscFunctionReturn(PETSC_SUCCESS);
    1696: }

    1698: /*@
    1699:   SNESSetCountersReset - Sets whether or not the counters for linear iterations and function evaluations
    1700:   are reset every time `SNESSolve()` is called.

    1702:   Logically Collective

    1704:   Input Parameters:
    1705: + snes  - `SNES` context
    1706: - reset - whether to reset the counters or not, defaults to `PETSC_TRUE`

    1708:   Level: developer

    1710: .seealso: [](ch_snes), `SNESGetNumberFunctionEvals()`, `SNESGetLinearSolveIterations()`, `SNESGetNPC()`
    1711: @*/
    1712: PetscErrorCode SNESSetCountersReset(SNES snes, PetscBool reset)
    1713: {
    1714:   PetscFunctionBegin;
    1717:   snes->counters_reset = reset;
    1718:   PetscFunctionReturn(PETSC_SUCCESS);
    1719: }

    1721: /*@
    1722:   SNESResetCounters - Reset counters for linear iterations and function evaluations.

    1724:   Logically Collective

    1726:   Input Parameters:
    1727: . snes - `SNES` context

    1729:   Level: developer

    1731:   Note:
    1732:   It honors the flag set with `SNESSetCountersReset()`

    1734: .seealso: [](ch_snes), `SNESGetNumberFunctionEvals()`, `SNESGetLinearSolveIterations()`, `SNESGetNPC()`
    1735: @*/
    1736: PetscErrorCode SNESResetCounters(SNES snes)
    1737: {
    1738:   PetscFunctionBegin;
    1740:   if (snes->counters_reset) {
    1741:     snes->nfuncs      = 0;
    1742:     snes->linear_its  = 0;
    1743:     snes->numFailures = 0;
    1744:   }
    1745:   PetscFunctionReturn(PETSC_SUCCESS);
    1746: }

    1748: /*@
    1749:   SNESSetKSP - Sets a `KSP` context for the `SNES` object to use

    1751:   Not Collective, but the `SNES` and `KSP` objects must live on the same `MPI_Comm`

    1753:   Input Parameters:
    1754: + snes - the `SNES` context
    1755: - ksp  - the `KSP` context

    1757:   Level: developer

    1759:   Notes:
    1760:   The `SNES` object already has its `KSP` object, you can obtain with `SNESGetKSP()`
    1761:   so this routine is rarely needed.

    1763:   The `KSP` object that is already in the `SNES` object has its reference count
    1764:   decreased by one when this is called.

    1766: .seealso: [](ch_snes), `SNES`, `KSP`, `KSPGetPC()`, `SNESCreate()`, `KSPCreate()`
    1767: @*/
    1768: PetscErrorCode SNESSetKSP(SNES snes, KSP ksp)
    1769: {
    1770:   PetscFunctionBegin;
    1773:   PetscCheckSameComm(snes, 1, ksp, 2);
    1774:   PetscCall(PetscObjectReference((PetscObject)ksp));
    1775:   if (snes->ksp) PetscCall(PetscObjectDereference((PetscObject)snes->ksp));
    1776:   snes->ksp = ksp;
    1777:   PetscFunctionReturn(PETSC_SUCCESS);
    1778: }

    1780: /*@
    1781:   SNESParametersInitialize - Sets all the parameters in `snes` to their default value (when `SNESCreate()` was called) if they
    1782:   currently contain default values

    1784:   Collective

    1786:   Input Parameter:
    1787: . snes - the `SNES` object

    1789:   Level: developer

    1791:   Developer Note:
    1792:   This is called by all the `SNESCreate_XXX()` routines.

    1794: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESDestroy()`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobian()`,
    1795:           `PetscObjectParameterSetDefault()`
    1796: @*/
    1797: PetscErrorCode SNESParametersInitialize(SNES snes)
    1798: {
    1799:   PetscObjectParameterSetDefault(snes, max_its, 50);
    1800:   PetscObjectParameterSetDefault(snes, max_funcs, 10000);
    1801:   PetscObjectParameterSetDefault(snes, rtol, PetscDefined(USE_REAL_SINGLE) ? 1.e-5 : 1.e-8);
    1802:   PetscObjectParameterSetDefault(snes, abstol, PetscDefined(USE_REAL_SINGLE) ? 1.e-25 : 1.e-50);
    1803:   PetscObjectParameterSetDefault(snes, stol, PetscDefined(USE_REAL_SINGLE) ? 1.e-5 : 1.e-8);
    1804:   PetscObjectParameterSetDefault(snes, divtol, 1.e4);
    1805:   return PETSC_SUCCESS;
    1806: }

    1808: /*@
    1809:   SNESCreate - Creates a nonlinear solver context used to manage a set of nonlinear solves

    1811:   Collective

    1813:   Input Parameter:
    1814: . comm - MPI communicator

    1816:   Output Parameter:
    1817: . outsnes - the new `SNES` context

    1819:   Options Database Keys:
    1820: + -snes_mf          - Activates default matrix-free Jacobian-vector products, and no preconditioning matrix
    1821: . -snes_mf_operator - Activates default matrix-free Jacobian-vector products, and a user-provided preconditioning matrix
    1822:                       as set by `SNESSetJacobian()`
    1823: . -snes_fd_coloring - uses a relative fast computation of the Jacobian using finite differences and a graph coloring
    1824: - -snes_fd          - Uses (slow!) finite differences to compute Jacobian

    1826:   Level: beginner

    1828:   Developer Notes:
    1829:   `SNES` always creates a `KSP` object even though many `SNES` methods do not use it. This is
    1830:   unfortunate and should be fixed at some point. The flag snes->usesksp indicates if the
    1831:   particular method does use `KSP` and regulates if the information about the `KSP` is printed
    1832:   in `SNESView()`.

    1834:   `TSSetFromOptions()` does call `SNESSetFromOptions()` which can lead to users being confused
    1835:   by help messages about meaningless `SNES` options.

    1837:   `SNES` always creates the snes->kspconvctx even though it is used by only one type. This should be fixed.

    1839: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESDestroy()`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobian()`
    1840: @*/
    1841: PetscErrorCode SNESCreate(MPI_Comm comm, SNES *outsnes)
    1842: {
    1843:   SNES       snes;
    1844:   SNESKSPEW *kctx;

    1846:   PetscFunctionBegin;
    1847:   PetscAssertPointer(outsnes, 2);
    1848:   PetscCall(SNESInitializePackage());

    1850:   PetscCall(PetscHeaderCreate(snes, SNES_CLASSID, "SNES", "Nonlinear solver", "SNES", comm, SNESDestroy, SNESView));
    1851:   snes->ops->converged = SNESConvergedDefault;
    1852:   snes->usesksp        = PETSC_TRUE;
    1853:   snes->norm           = 0.0;
    1854:   snes->xnorm          = 0.0;
    1855:   snes->ynorm          = 0.0;
    1856:   snes->normschedule   = SNES_NORM_ALWAYS;
    1857:   snes->functype       = SNES_FUNCTION_DEFAULT;
    1858:   snes->ttol           = 0.0;

    1860:   snes->rnorm0               = 0;
    1861:   snes->nfuncs               = 0;
    1862:   snes->numFailures          = 0;
    1863:   snes->maxFailures          = 1;
    1864:   snes->linear_its           = 0;
    1865:   snes->lagjacobian          = 1;
    1866:   snes->jac_iter             = 0;
    1867:   snes->lagjac_persist       = PETSC_FALSE;
    1868:   snes->lagpreconditioner    = 1;
    1869:   snes->pre_iter             = 0;
    1870:   snes->lagpre_persist       = PETSC_FALSE;
    1871:   snes->numbermonitors       = 0;
    1872:   snes->numberreasonviews    = 0;
    1873:   snes->data                 = NULL;
    1874:   snes->setupcalled          = PETSC_FALSE;
    1875:   snes->ksp_ewconv           = PETSC_FALSE;
    1876:   snes->nwork                = 0;
    1877:   snes->work                 = NULL;
    1878:   snes->nvwork               = 0;
    1879:   snes->vwork                = NULL;
    1880:   snes->conv_hist_len        = 0;
    1881:   snes->conv_hist_max        = 0;
    1882:   snes->conv_hist            = NULL;
    1883:   snes->conv_hist_its        = NULL;
    1884:   snes->conv_hist_reset      = PETSC_TRUE;
    1885:   snes->counters_reset       = PETSC_TRUE;
    1886:   snes->vec_func_init_set    = PETSC_FALSE;
    1887:   snes->reason               = SNES_CONVERGED_ITERATING;
    1888:   snes->npcside              = PC_RIGHT;
    1889:   snes->setfromoptionscalled = 0;

    1891:   snes->mf          = PETSC_FALSE;
    1892:   snes->mf_operator = PETSC_FALSE;
    1893:   snes->mf_version  = 1;

    1895:   snes->numLinearSolveFailures = 0;
    1896:   snes->maxLinearSolveFailures = 1;

    1898:   snes->vizerotolerance     = 1.e-8;
    1899:   snes->checkjacdomainerror = PetscDefined(USE_DEBUG) ? PETSC_TRUE : PETSC_FALSE;

    1901:   /* Set this to true if the implementation of SNESSolve_XXX does compute the residual at the final solution. */
    1902:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

    1904:   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
    1905:   PetscCall(PetscNew(&kctx));

    1907:   snes->kspconvctx  = (void *)kctx;
    1908:   kctx->version     = 2;
    1909:   kctx->rtol_0      = 0.3; /* Eisenstat and Walker suggest rtol_0=.5, but
    1910:                              this was too large for some test cases */
    1911:   kctx->rtol_last   = 0.0;
    1912:   kctx->rtol_max    = 0.9;
    1913:   kctx->gamma       = 1.0;
    1914:   kctx->alpha       = 0.5 * (1.0 + PetscSqrtReal(5.0));
    1915:   kctx->alpha2      = kctx->alpha;
    1916:   kctx->threshold   = 0.1;
    1917:   kctx->lresid_last = 0.0;
    1918:   kctx->norm_last   = 0.0;

    1920:   kctx->rk_last     = 0.0;
    1921:   kctx->rk_last_2   = 0.0;
    1922:   kctx->rtol_last_2 = 0.0;
    1923:   kctx->v4_p1       = 0.1;
    1924:   kctx->v4_p2       = 0.4;
    1925:   kctx->v4_p3       = 0.7;
    1926:   kctx->v4_m1       = 0.8;
    1927:   kctx->v4_m2       = 0.5;
    1928:   kctx->v4_m3       = 0.1;
    1929:   kctx->v4_m4       = 0.5;

    1931:   PetscCall(SNESParametersInitialize(snes));
    1932:   *outsnes = snes;
    1933:   PetscFunctionReturn(PETSC_SUCCESS);
    1934: }

    1936: /*@C
    1937:   SNESSetFunction - Sets the function evaluation routine and function
    1938:   vector for use by the `SNES` routines in solving systems of nonlinear
    1939:   equations.

    1941:   Logically Collective

    1943:   Input Parameters:
    1944: + snes - the `SNES` context
    1945: . r    - vector to store function values, may be `NULL`
    1946: . f    - function evaluation routine;  for calling sequence see `SNESFunctionFn`
    1947: - ctx  - [optional] user-defined context for private data for the
    1948:          function evaluation routine (may be `NULL`)

    1950:   Level: beginner

    1952: .seealso: [](ch_snes), `SNES`, `SNESGetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESSetPicard()`, `SNESFunctionFn`
    1953: @*/
    1954: PetscErrorCode SNESSetFunction(SNES snes, Vec r, SNESFunctionFn *f, void *ctx)
    1955: {
    1956:   DM dm;

    1958:   PetscFunctionBegin;
    1960:   if (r) {
    1962:     PetscCheckSameComm(snes, 1, r, 2);
    1963:     PetscCall(PetscObjectReference((PetscObject)r));
    1964:     PetscCall(VecDestroy(&snes->vec_func));
    1965:     snes->vec_func = r;
    1966:   }
    1967:   PetscCall(SNESGetDM(snes, &dm));
    1968:   PetscCall(DMSNESSetFunction(dm, f, ctx));
    1969:   if (f == SNESPicardComputeFunction) PetscCall(DMSNESSetMFFunction(dm, SNESPicardComputeMFFunction, ctx));
    1970:   PetscFunctionReturn(PETSC_SUCCESS);
    1971: }

    1973: /*@C
    1974:   SNESSetInitialFunction - Set an already computed function evaluation at the initial guess to be reused by `SNESSolve()`.

    1976:   Logically Collective

    1978:   Input Parameters:
    1979: + snes - the `SNES` context
    1980: - f    - vector to store function value

    1982:   Level: developer

    1984:   Notes:
    1985:   This should not be modified during the solution procedure.

    1987:   This is used extensively in the `SNESFAS` hierarchy and in nonlinear preconditioning.

    1989: .seealso: [](ch_snes), `SNES`, `SNESFAS`, `SNESSetFunction()`, `SNESComputeFunction()`, `SNESSetInitialFunctionNorm()`
    1990: @*/
    1991: PetscErrorCode SNESSetInitialFunction(SNES snes, Vec f)
    1992: {
    1993:   Vec vec_func;

    1995:   PetscFunctionBegin;
    1998:   PetscCheckSameComm(snes, 1, f, 2);
    1999:   if (snes->npcside == PC_LEFT && snes->functype == SNES_FUNCTION_PRECONDITIONED) {
    2000:     snes->vec_func_init_set = PETSC_FALSE;
    2001:     PetscFunctionReturn(PETSC_SUCCESS);
    2002:   }
    2003:   PetscCall(SNESGetFunction(snes, &vec_func, NULL, NULL));
    2004:   PetscCall(VecCopy(f, vec_func));

    2006:   snes->vec_func_init_set = PETSC_TRUE;
    2007:   PetscFunctionReturn(PETSC_SUCCESS);
    2008: }

    2010: /*@
    2011:   SNESSetNormSchedule - Sets the `SNESNormSchedule` used in convergence and monitoring
    2012:   of the `SNES` method, when norms are computed in the solving process

    2014:   Logically Collective

    2016:   Input Parameters:
    2017: + snes         - the `SNES` context
    2018: - normschedule - the frequency of norm computation

    2020:   Options Database Key:
    2021: . -snes_norm_schedule <none, always, initialonly, finalonly, initialfinalonly> - set the schedule

    2023:   Level: advanced

    2025:   Notes:
    2026:   Only certain `SNES` methods support certain `SNESNormSchedules`.  Most require evaluation
    2027:   of the nonlinear function and the taking of its norm at every iteration to
    2028:   even ensure convergence at all.  However, methods such as custom Gauss-Seidel methods
    2029:   `SNESNGS` and the like do not require the norm of the function to be computed, and therefore
    2030:   may either be monitored for convergence or not.  As these are often used as nonlinear
    2031:   preconditioners, monitoring the norm of their error is not a useful enterprise within
    2032:   their solution.

    2034: .seealso: [](ch_snes), `SNESNormSchedule`, `SNESGetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`
    2035: @*/
    2036: PetscErrorCode SNESSetNormSchedule(SNES snes, SNESNormSchedule normschedule)
    2037: {
    2038:   PetscFunctionBegin;
    2040:   snes->normschedule = normschedule;
    2041:   PetscFunctionReturn(PETSC_SUCCESS);
    2042: }

    2044: /*@
    2045:   SNESGetNormSchedule - Gets the `SNESNormSchedule` used in convergence and monitoring
    2046:   of the `SNES` method.

    2048:   Logically Collective

    2050:   Input Parameters:
    2051: + snes         - the `SNES` context
    2052: - normschedule - the type of the norm used

    2054:   Level: advanced

    2056: .seealso: [](ch_snes), `SNES`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule`
    2057: @*/
    2058: PetscErrorCode SNESGetNormSchedule(SNES snes, SNESNormSchedule *normschedule)
    2059: {
    2060:   PetscFunctionBegin;
    2062:   *normschedule = snes->normschedule;
    2063:   PetscFunctionReturn(PETSC_SUCCESS);
    2064: }

    2066: /*@
    2067:   SNESSetFunctionNorm - Sets the last computed residual norm.

    2069:   Logically Collective

    2071:   Input Parameters:
    2072: + snes - the `SNES` context
    2073: - norm - the value of the norm

    2075:   Level: developer

    2077: .seealso: [](ch_snes), `SNES`, `SNESGetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule`
    2078: @*/
    2079: PetscErrorCode SNESSetFunctionNorm(SNES snes, PetscReal norm)
    2080: {
    2081:   PetscFunctionBegin;
    2083:   snes->norm = norm;
    2084:   PetscFunctionReturn(PETSC_SUCCESS);
    2085: }

    2087: /*@
    2088:   SNESGetFunctionNorm - Gets the last computed norm of the residual

    2090:   Not Collective

    2092:   Input Parameter:
    2093: . snes - the `SNES` context

    2095:   Output Parameter:
    2096: . norm - the last computed residual norm

    2098:   Level: developer

    2100: .seealso: [](ch_snes), `SNES`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule`
    2101: @*/
    2102: PetscErrorCode SNESGetFunctionNorm(SNES snes, PetscReal *norm)
    2103: {
    2104:   PetscFunctionBegin;
    2106:   PetscAssertPointer(norm, 2);
    2107:   *norm = snes->norm;
    2108:   PetscFunctionReturn(PETSC_SUCCESS);
    2109: }

    2111: /*@
    2112:   SNESGetUpdateNorm - Gets the last computed norm of the solution update

    2114:   Not Collective

    2116:   Input Parameter:
    2117: . snes - the `SNES` context

    2119:   Output Parameter:
    2120: . ynorm - the last computed update norm

    2122:   Level: developer

    2124:   Note:
    2125:   The new solution is the current solution plus the update, so this norm is an indication of the size of the update

    2127: .seealso: [](ch_snes), `SNES`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `SNESGetFunctionNorm()`
    2128: @*/
    2129: PetscErrorCode SNESGetUpdateNorm(SNES snes, PetscReal *ynorm)
    2130: {
    2131:   PetscFunctionBegin;
    2133:   PetscAssertPointer(ynorm, 2);
    2134:   *ynorm = snes->ynorm;
    2135:   PetscFunctionReturn(PETSC_SUCCESS);
    2136: }

    2138: /*@
    2139:   SNESGetSolutionNorm - Gets the last computed norm of the solution

    2141:   Not Collective

    2143:   Input Parameter:
    2144: . snes - the `SNES` context

    2146:   Output Parameter:
    2147: . xnorm - the last computed solution norm

    2149:   Level: developer

    2151: .seealso: [](ch_snes), `SNES`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `SNESGetFunctionNorm()`, `SNESGetUpdateNorm()`
    2152: @*/
    2153: PetscErrorCode SNESGetSolutionNorm(SNES snes, PetscReal *xnorm)
    2154: {
    2155:   PetscFunctionBegin;
    2157:   PetscAssertPointer(xnorm, 2);
    2158:   *xnorm = snes->xnorm;
    2159:   PetscFunctionReturn(PETSC_SUCCESS);
    2160: }

    2162: /*@
    2163:   SNESSetFunctionType - Sets the `SNESFunctionType`
    2164:   of the `SNES` method.

    2166:   Logically Collective

    2168:   Input Parameters:
    2169: + snes - the `SNES` context
    2170: - type - the function type

    2172:   Level: developer

    2174:   Values of the function type\:
    2175: +  `SNES_FUNCTION_DEFAULT`          - the default for the given `SNESType`
    2176: .  `SNES_FUNCTION_UNPRECONDITIONED` - an unpreconditioned function evaluation (this is the function provided with `SNESSetFunction()`
    2177: -  `SNES_FUNCTION_PRECONDITIONED`   - a transformation of the function provided with `SNESSetFunction()`

    2179:   Note:
    2180:   Different `SNESType`s use this value in different ways

    2182: .seealso: [](ch_snes), `SNES`, `SNESFunctionType`, `SNESGetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule`
    2183: @*/
    2184: PetscErrorCode SNESSetFunctionType(SNES snes, SNESFunctionType type)
    2185: {
    2186:   PetscFunctionBegin;
    2188:   snes->functype = type;
    2189:   PetscFunctionReturn(PETSC_SUCCESS);
    2190: }

    2192: /*@
    2193:   SNESGetFunctionType - Gets the `SNESFunctionType` used in convergence and monitoring set with `SNESSetFunctionType()`
    2194:   of the SNES method.

    2196:   Logically Collective

    2198:   Input Parameters:
    2199: + snes - the `SNES` context
    2200: - type - the type of the function evaluation, see `SNESSetFunctionType()`

    2202:   Level: advanced

    2204: .seealso: [](ch_snes), `SNESSetFunctionType()`, `SNESFunctionType`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule`
    2205: @*/
    2206: PetscErrorCode SNESGetFunctionType(SNES snes, SNESFunctionType *type)
    2207: {
    2208:   PetscFunctionBegin;
    2210:   *type = snes->functype;
    2211:   PetscFunctionReturn(PETSC_SUCCESS);
    2212: }

    2214: /*@C
    2215:   SNESSetNGS - Sets the user nonlinear Gauss-Seidel routine for
    2216:   use with composed nonlinear solvers.

    2218:   Input Parameters:
    2219: + snes - the `SNES` context, usually of the `SNESType` `SNESNGS`
    2220: . f    - function evaluation routine to apply Gauss-Seidel, see `SNESNGSFn` for calling sequence
    2221: - ctx  - [optional] user-defined context for private data for the smoother evaluation routine (may be `NULL`)

    2223:   Level: intermediate

    2225:   Note:
    2226:   The `SNESNGS` routines are used by the composed nonlinear solver to generate
    2227:   a problem appropriate update to the solution, particularly `SNESFAS`.

    2229: .seealso: [](ch_snes), `SNESNGS`, `SNESGetNGS()`, `SNESNCG`, `SNESGetFunction()`, `SNESComputeNGS()`, `SNESNGSFn`
    2230: @*/
    2231: PetscErrorCode SNESSetNGS(SNES snes, SNESNGSFn *f, void *ctx)
    2232: {
    2233:   DM dm;

    2235:   PetscFunctionBegin;
    2237:   PetscCall(SNESGetDM(snes, &dm));
    2238:   PetscCall(DMSNESSetNGS(dm, f, ctx));
    2239:   PetscFunctionReturn(PETSC_SUCCESS);
    2240: }

    2242: /*
    2243:      This is used for -snes_mf_operator; it uses a duplicate of snes->jacobian_pre because snes->jacobian_pre cannot be
    2244:    changed during the KSPSolve()
    2245: */
    2246: PetscErrorCode SNESPicardComputeMFFunction(SNES snes, Vec x, Vec f, void *ctx)
    2247: {
    2248:   DM     dm;
    2249:   DMSNES sdm;

    2251:   PetscFunctionBegin;
    2252:   PetscCall(SNESGetDM(snes, &dm));
    2253:   PetscCall(DMGetDMSNES(dm, &sdm));
    2254:   /*  A(x)*x - b(x) */
    2255:   if (sdm->ops->computepfunction) {
    2256:     PetscCallBack("SNES Picard callback function", (*sdm->ops->computepfunction)(snes, x, f, sdm->pctx));
    2257:     PetscCall(VecScale(f, -1.0));
    2258:     /* Cannot share nonzero pattern because of the possible use of SNESComputeJacobianDefault() */
    2259:     if (!snes->picard) PetscCall(MatDuplicate(snes->jacobian_pre, MAT_DO_NOT_COPY_VALUES, &snes->picard));
    2260:     PetscCallBack("SNES Picard callback Jacobian", (*sdm->ops->computepjacobian)(snes, x, snes->picard, snes->picard, sdm->pctx));
    2261:     PetscCall(MatMultAdd(snes->picard, x, f, f));
    2262:   } else {
    2263:     PetscCallBack("SNES Picard callback Jacobian", (*sdm->ops->computepjacobian)(snes, x, snes->picard, snes->picard, sdm->pctx));
    2264:     PetscCall(MatMult(snes->picard, x, f));
    2265:   }
    2266:   PetscFunctionReturn(PETSC_SUCCESS);
    2267: }

    2269: PetscErrorCode SNESPicardComputeFunction(SNES snes, Vec x, Vec f, void *ctx)
    2270: {
    2271:   DM     dm;
    2272:   DMSNES sdm;

    2274:   PetscFunctionBegin;
    2275:   PetscCall(SNESGetDM(snes, &dm));
    2276:   PetscCall(DMGetDMSNES(dm, &sdm));
    2277:   /*  A(x)*x - b(x) */
    2278:   if (sdm->ops->computepfunction) {
    2279:     PetscCallBack("SNES Picard callback function", (*sdm->ops->computepfunction)(snes, x, f, sdm->pctx));
    2280:     PetscCall(VecScale(f, -1.0));
    2281:     PetscCallBack("SNES Picard callback Jacobian", (*sdm->ops->computepjacobian)(snes, x, snes->jacobian, snes->jacobian_pre, sdm->pctx));
    2282:     PetscCall(MatMultAdd(snes->jacobian_pre, x, f, f));
    2283:   } else {
    2284:     PetscCallBack("SNES Picard callback Jacobian", (*sdm->ops->computepjacobian)(snes, x, snes->jacobian, snes->jacobian_pre, sdm->pctx));
    2285:     PetscCall(MatMult(snes->jacobian_pre, x, f));
    2286:   }
    2287:   PetscFunctionReturn(PETSC_SUCCESS);
    2288: }

    2290: PetscErrorCode SNESPicardComputeJacobian(SNES snes, Vec x1, Mat J, Mat B, void *ctx)
    2291: {
    2292:   PetscFunctionBegin;
    2293:   /* the jacobian matrix should be pre-filled in SNESPicardComputeFunction */
    2294:   /* must assembly if matrix-free to get the last SNES solution */
    2295:   PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
    2296:   PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
    2297:   PetscFunctionReturn(PETSC_SUCCESS);
    2298: }

    2300: /*@C
    2301:   SNESSetPicard - Use `SNES` to solve the system $A(x) x = bp(x) + b $ via a Picard type iteration (Picard linearization)

    2303:   Logically Collective

    2305:   Input Parameters:
    2306: + snes - the `SNES` context
    2307: . r    - vector to store function values, may be `NULL`
    2308: . bp   - function evaluation routine, may be `NULL`, for the calling sequence see `SNESFunctionFn`
    2309: . Amat - matrix with which A(x) x - bp(x) - b is to be computed
    2310: . Pmat - matrix from which preconditioner is computed (usually the same as `Amat`)
    2311: . J    - function to compute matrix values, for the calling sequence see `SNESJacobianFn`
    2312: - ctx  - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)

    2314:   Level: intermediate

    2316:   Notes:
    2317:   It is often better to provide the nonlinear function F() and some approximation to its Jacobian directly and use
    2318:   an approximate Newton solver. This interface is provided to allow porting/testing a previous Picard based code in PETSc before converting it to approximate Newton.

    2320:   One can call `SNESSetPicard()` or `SNESSetFunction()` (and possibly `SNESSetJacobian()`) but cannot call both

    2322:   Solves the equation $A(x) x = bp(x) - b$ via the defect correction algorithm $A(x^{n}) (x^{n+1} - x^{n}) = bp(x^{n}) + b - A(x^{n})x^{n}$.
    2323:   When an exact solver is used this corresponds to the "classic" Picard $A(x^{n}) x^{n+1} = bp(x^{n}) + b$ iteration.

    2325:   Run with `-snes_mf_operator` to solve the system with Newton's method using A(x^{n}) to construct the preconditioner.

    2327:   We implement the defect correction form of the Picard iteration because it converges much more generally when inexact linear solvers are used then
    2328:   the direct Picard iteration $A(x^n) x^{n+1} = bp(x^n) + b$

    2330:   There is some controversity over the definition of a Picard iteration for nonlinear systems but almost everyone agrees that it involves a linear solve and some
    2331:   believe it is the iteration  $A(x^{n}) x^{n+1} = b(x^{n})$ hence we use the name Picard. If anyone has an authoritative  reference that defines the Picard iteration
    2332:   different please contact us at petsc-dev@mcs.anl.gov and we'll have an entirely new argument \:-).

    2334:   When used with `-snes_mf_operator` this will run matrix-free Newton's method where the matrix-vector product is of the true Jacobian of $A(x)x - bp(x) - b$ and
    2335:   A(x^{n}) is used to build the preconditioner

    2337:   When used with `-snes_fd` this will compute the true Jacobian (very slowly one column at a time) and thus represent Newton's method.

    2339:   When used with `-snes_fd_coloring` this will compute the Jacobian via coloring and thus represent a faster implementation of Newton's method. But the
    2340:   the nonzero structure of the Jacobian is, in general larger than that of the Picard matrix A so you must provide in A the needed nonzero structure for the correct
    2341:   coloring. When using `DMDA` this may mean creating the matrix A with `DMCreateMatrix()` using a wider stencil than strictly needed for A or with a `DMDA_STENCIL_BOX`.
    2342:   See the comment in src/snes/tutorials/ex15.c.

    2344: .seealso: [](ch_snes), `SNES`, `SNESGetFunction()`, `SNESSetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESGetPicard()`, `SNESLineSearchPreCheckPicard()`,
    2345:           `SNESFunctionFn`, `SNESJacobianFn`
    2346: @*/
    2347: PetscErrorCode SNESSetPicard(SNES snes, Vec r, SNESFunctionFn *bp, Mat Amat, Mat Pmat, SNESJacobianFn *J, void *ctx)
    2348: {
    2349:   DM dm;

    2351:   PetscFunctionBegin;
    2353:   PetscCall(SNESGetDM(snes, &dm));
    2354:   PetscCall(DMSNESSetPicard(dm, bp, J, ctx));
    2355:   PetscCall(DMSNESSetMFFunction(dm, SNESPicardComputeMFFunction, ctx));
    2356:   PetscCall(SNESSetFunction(snes, r, SNESPicardComputeFunction, ctx));
    2357:   PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESPicardComputeJacobian, ctx));
    2358:   PetscFunctionReturn(PETSC_SUCCESS);
    2359: }

    2361: /*@C
    2362:   SNESGetPicard - Returns the context for the Picard iteration

    2364:   Not Collective, but `Vec` is parallel if `SNES` is parallel. Collective if `Vec` is requested, but has not been created yet.

    2366:   Input Parameter:
    2367: . snes - the `SNES` context

    2369:   Output Parameters:
    2370: + r    - the function (or `NULL`)
    2371: . f    - the function (or `NULL`);  for calling sequence see `SNESFunctionFn`
    2372: . Amat - the matrix used to defined the operation A(x) x - b(x) (or `NULL`)
    2373: . Pmat - the matrix from which the preconditioner will be constructed (or `NULL`)
    2374: . J    - the function for matrix evaluation (or `NULL`);  for calling sequence see `SNESJacobianFn`
    2375: - ctx  - the function context (or `NULL`)

    2377:   Level: advanced

    2379: .seealso: [](ch_snes), `SNESSetFunction()`, `SNESSetPicard()`, `SNESGetFunction()`, `SNESGetJacobian()`, `SNESGetDM()`, `SNESFunctionFn`, `SNESJacobianFn`
    2380: @*/
    2381: PetscErrorCode SNESGetPicard(SNES snes, Vec *r, SNESFunctionFn **f, Mat *Amat, Mat *Pmat, SNESJacobianFn **J, void **ctx)
    2382: {
    2383:   DM dm;

    2385:   PetscFunctionBegin;
    2387:   PetscCall(SNESGetFunction(snes, r, NULL, NULL));
    2388:   PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL));
    2389:   PetscCall(SNESGetDM(snes, &dm));
    2390:   PetscCall(DMSNESGetPicard(dm, f, J, ctx));
    2391:   PetscFunctionReturn(PETSC_SUCCESS);
    2392: }

    2394: /*@C
    2395:   SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the nonlinear problem

    2397:   Logically Collective

    2399:   Input Parameters:
    2400: + snes - the `SNES` context
    2401: . func - function evaluation routine, see `SNESInitialGuessFn` for the calling sequence
    2402: - ctx  - [optional] user-defined context for private data for the
    2403:          function evaluation routine (may be `NULL`)

    2405:   Level: intermediate

    2407: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESSetFunction()`, `SNESGetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESInitialGuessFn`
    2408: @*/
    2409: PetscErrorCode SNESSetComputeInitialGuess(SNES snes, SNESInitialGuessFn *func, void *ctx)
    2410: {
    2411:   PetscFunctionBegin;
    2413:   if (func) snes->ops->computeinitialguess = func;
    2414:   if (ctx) snes->initialguessP = ctx;
    2415:   PetscFunctionReturn(PETSC_SUCCESS);
    2416: }

    2418: /*@C
    2419:   SNESGetRhs - Gets the vector for solving F(x) = `rhs`. If `rhs` is not set
    2420:   it assumes a zero right-hand side.

    2422:   Logically Collective

    2424:   Input Parameter:
    2425: . snes - the `SNES` context

    2427:   Output Parameter:
    2428: . rhs - the right-hand side vector or `NULL` if there is no right-hand side vector

    2430:   Level: intermediate

    2432: .seealso: [](ch_snes), `SNES`, `SNESGetSolution()`, `SNESGetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESSetFunction()`
    2433: @*/
    2434: PetscErrorCode SNESGetRhs(SNES snes, Vec *rhs)
    2435: {
    2436:   PetscFunctionBegin;
    2438:   PetscAssertPointer(rhs, 2);
    2439:   *rhs = snes->vec_rhs;
    2440:   PetscFunctionReturn(PETSC_SUCCESS);
    2441: }

    2443: /*@
    2444:   SNESComputeFunction - Calls the function that has been set with `SNESSetFunction()`.

    2446:   Collective

    2448:   Input Parameters:
    2449: + snes - the `SNES` context
    2450: - x    - input vector

    2452:   Output Parameter:
    2453: . y - function vector, as set by `SNESSetFunction()`

    2455:   Level: developer

    2457:   Notes:
    2458:   `SNESComputeFunction()` is typically used within nonlinear solvers
    2459:   implementations, so users would not generally call this routine themselves.

    2461:   When solving for $F(x) = b$, this routine computes $y = F(x) - b$.

    2463: .seealso: [](ch_snes), `SNES`, `SNESSetFunction()`, `SNESGetFunction()`, `SNESComputeMFFunction()`
    2464: @*/
    2465: PetscErrorCode SNESComputeFunction(SNES snes, Vec x, Vec y)
    2466: {
    2467:   DM     dm;
    2468:   DMSNES sdm;

    2470:   PetscFunctionBegin;
    2474:   PetscCheckSameComm(snes, 1, x, 2);
    2475:   PetscCheckSameComm(snes, 1, y, 3);
    2476:   PetscCall(VecValidValues_Internal(x, 2, PETSC_TRUE));

    2478:   PetscCall(SNESGetDM(snes, &dm));
    2479:   PetscCall(DMGetDMSNES(dm, &sdm));
    2480:   PetscCheck(sdm->ops->computefunction || snes->vec_rhs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve().");
    2481:   if (sdm->ops->computefunction) {
    2482:     if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) PetscCall(PetscLogEventBegin(SNES_FunctionEval, snes, x, y, 0));
    2483:     PetscCall(VecLockReadPush(x));
    2484:     /* ensure domainerror is false prior to computefunction evaluation (may not have been reset) */
    2485:     snes->domainerror = PETSC_FALSE;
    2486:     {
    2487:       void           *ctx;
    2488:       SNESFunctionFn *computefunction;
    2489:       PetscCall(DMSNESGetFunction(dm, &computefunction, &ctx));
    2490:       PetscCallBack("SNES callback function", (*computefunction)(snes, x, y, ctx));
    2491:     }
    2492:     PetscCall(VecLockReadPop(x));
    2493:     if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) PetscCall(PetscLogEventEnd(SNES_FunctionEval, snes, x, y, 0));
    2494:   } else /* if (snes->vec_rhs) */ {
    2495:     PetscCall(MatMult(snes->jacobian, x, y));
    2496:   }
    2497:   if (snes->vec_rhs) PetscCall(VecAXPY(y, -1.0, snes->vec_rhs));
    2498:   snes->nfuncs++;
    2499:   /*
    2500:      domainerror might not be set on all processes; so we tag vector locally with Inf and the next inner product or norm will
    2501:      propagate the value to all processes
    2502:   */
    2503:   PetscCall(VecFlag(y, snes->domainerror));
    2504:   PetscFunctionReturn(PETSC_SUCCESS);
    2505: }

    2507: /*@
    2508:   SNESComputeMFFunction - Calls the function that has been set with `DMSNESSetMFFunction()`.

    2510:   Collective

    2512:   Input Parameters:
    2513: + snes - the `SNES` context
    2514: - x    - input vector

    2516:   Output Parameter:
    2517: . y - output vector

    2519:   Level: developer

    2521:   Notes:
    2522:   `SNESComputeMFFunction()` is used within the matrix-vector products called by the matrix created with `MatCreateSNESMF()`
    2523:   so users would not generally call this routine themselves.

    2525:   Since this function is intended for use with finite differencing it does not subtract the right-hand side vector provided with `SNESSolve()`
    2526:   while `SNESComputeFunction()` does. As such, this routine cannot be used with  `MatMFFDSetBase()` with a provided F function value even if it applies the
    2527:   same function as `SNESComputeFunction()` if a `SNESSolve()` right-hand side vector is use because the two functions difference would include this right hand side function.

    2529: .seealso: [](ch_snes), `SNES`, `SNESSetFunction()`, `SNESGetFunction()`, `SNESComputeFunction()`, `MatCreateSNESMF()`, `DMSNESSetMFFunction()`
    2530: @*/
    2531: PetscErrorCode SNESComputeMFFunction(SNES snes, Vec x, Vec y)
    2532: {
    2533:   DM     dm;
    2534:   DMSNES sdm;

    2536:   PetscFunctionBegin;
    2540:   PetscCheckSameComm(snes, 1, x, 2);
    2541:   PetscCheckSameComm(snes, 1, y, 3);
    2542:   PetscCall(VecValidValues_Internal(x, 2, PETSC_TRUE));

    2544:   PetscCall(SNESGetDM(snes, &dm));
    2545:   PetscCall(DMGetDMSNES(dm, &sdm));
    2546:   PetscCall(PetscLogEventBegin(SNES_FunctionEval, snes, x, y, 0));
    2547:   PetscCall(VecLockReadPush(x));
    2548:   /* ensure domainerror is false prior to computefunction evaluation (may not have been reset) */
    2549:   snes->domainerror = PETSC_FALSE;
    2550:   PetscCallBack("SNES callback function", (*sdm->ops->computemffunction)(snes, x, y, sdm->mffunctionctx));
    2551:   PetscCall(VecLockReadPop(x));
    2552:   PetscCall(PetscLogEventEnd(SNES_FunctionEval, snes, x, y, 0));
    2553:   snes->nfuncs++;
    2554:   /*
    2555:      domainerror might not be set on all processes; so we tag vector locally with Inf and the next inner product or norm will
    2556:      propagate the value to all processes
    2557:   */
    2558:   PetscCall(VecFlag(y, snes->domainerror));
    2559:   PetscFunctionReturn(PETSC_SUCCESS);
    2560: }

    2562: /*@
    2563:   SNESComputeNGS - Calls the Gauss-Seidel function that has been set with `SNESSetNGS()`.

    2565:   Collective

    2567:   Input Parameters:
    2568: + snes - the `SNES` context
    2569: . x    - input vector
    2570: - b    - rhs vector

    2572:   Output Parameter:
    2573: . x - new solution vector

    2575:   Level: developer

    2577:   Note:
    2578:   `SNESComputeNGS()` is typically used within composed nonlinear solver
    2579:   implementations, so most users would not generally call this routine
    2580:   themselves.

    2582: .seealso: [](ch_snes), `SNESNGSFn`, `SNESSetNGS()`, `SNESComputeFunction()`, `SNESNGS`
    2583: @*/
    2584: PetscErrorCode SNESComputeNGS(SNES snes, Vec b, Vec x)
    2585: {
    2586:   DM     dm;
    2587:   DMSNES sdm;

    2589:   PetscFunctionBegin;
    2593:   PetscCheckSameComm(snes, 1, x, 3);
    2594:   if (b) PetscCheckSameComm(snes, 1, b, 2);
    2595:   if (b) PetscCall(VecValidValues_Internal(b, 2, PETSC_TRUE));
    2596:   PetscCall(PetscLogEventBegin(SNES_NGSEval, snes, x, b, 0));
    2597:   PetscCall(SNESGetDM(snes, &dm));
    2598:   PetscCall(DMGetDMSNES(dm, &sdm));
    2599:   PetscCheck(sdm->ops->computegs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetNGS() before SNESComputeNGS(), likely called from SNESSolve().");
    2600:   if (b) PetscCall(VecLockReadPush(b));
    2601:   PetscCallBack("SNES callback NGS", (*sdm->ops->computegs)(snes, x, b, sdm->gsctx));
    2602:   if (b) PetscCall(VecLockReadPop(b));
    2603:   PetscCall(PetscLogEventEnd(SNES_NGSEval, snes, x, b, 0));
    2604:   PetscFunctionReturn(PETSC_SUCCESS);
    2605: }

    2607: static PetscErrorCode SNESComputeFunction_FD(SNES snes, Vec Xin, Vec G)
    2608: {
    2609:   Vec          X;
    2610:   PetscScalar *g;
    2611:   PetscReal    f, f2;
    2612:   PetscInt     low, high, N, i;
    2613:   PetscBool    flg;
    2614:   PetscReal    h = .5 * PETSC_SQRT_MACHINE_EPSILON;

    2616:   PetscFunctionBegin;
    2617:   PetscCall(PetscOptionsGetReal(((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_fd_delta", &h, &flg));
    2618:   PetscCall(VecDuplicate(Xin, &X));
    2619:   PetscCall(VecCopy(Xin, X));
    2620:   PetscCall(VecGetSize(X, &N));
    2621:   PetscCall(VecGetOwnershipRange(X, &low, &high));
    2622:   PetscCall(VecSetOption(X, VEC_IGNORE_OFF_PROC_ENTRIES, PETSC_TRUE));
    2623:   PetscCall(VecGetArray(G, &g));
    2624:   for (i = 0; i < N; i++) {
    2625:     PetscCall(VecSetValue(X, i, -h, ADD_VALUES));
    2626:     PetscCall(VecAssemblyBegin(X));
    2627:     PetscCall(VecAssemblyEnd(X));
    2628:     PetscCall(SNESComputeObjective(snes, X, &f));
    2629:     PetscCall(VecSetValue(X, i, 2.0 * h, ADD_VALUES));
    2630:     PetscCall(VecAssemblyBegin(X));
    2631:     PetscCall(VecAssemblyEnd(X));
    2632:     PetscCall(SNESComputeObjective(snes, X, &f2));
    2633:     PetscCall(VecSetValue(X, i, -h, ADD_VALUES));
    2634:     PetscCall(VecAssemblyBegin(X));
    2635:     PetscCall(VecAssemblyEnd(X));
    2636:     if (i >= low && i < high) g[i - low] = (f2 - f) / (2.0 * h);
    2637:   }
    2638:   PetscCall(VecRestoreArray(G, &g));
    2639:   PetscCall(VecDestroy(&X));
    2640:   PetscFunctionReturn(PETSC_SUCCESS);
    2641: }

    2643: PetscErrorCode SNESTestFunction(SNES snes)
    2644: {
    2645:   Vec               x, g1, g2, g3;
    2646:   PetscBool         complete_print = PETSC_FALSE, test = PETSC_FALSE;
    2647:   PetscReal         hcnorm, fdnorm, hcmax, fdmax, diffmax, diffnorm;
    2648:   PetscScalar       dot;
    2649:   MPI_Comm          comm;
    2650:   PetscViewer       viewer, mviewer;
    2651:   PetscViewerFormat format;
    2652:   PetscInt          tabs;
    2653:   static PetscBool  directionsprinted = PETSC_FALSE;
    2654:   SNESObjectiveFn  *objective;

    2656:   PetscFunctionBegin;
    2657:   PetscCall(SNESGetObjective(snes, &objective, NULL));
    2658:   if (!objective) PetscFunctionReturn(PETSC_SUCCESS);

    2660:   PetscObjectOptionsBegin((PetscObject)snes);
    2661:   PetscCall(PetscOptionsName("-snes_test_function", "Compare hand-coded and finite difference function", "None", &test));
    2662:   PetscCall(PetscOptionsViewer("-snes_test_function_view", "View difference between hand-coded and finite difference function element entries", "None", &mviewer, &format, &complete_print));
    2663:   PetscOptionsEnd();
    2664:   if (!test) {
    2665:     if (complete_print) PetscCall(PetscViewerDestroy(&mviewer));
    2666:     PetscFunctionReturn(PETSC_SUCCESS);
    2667:   }

    2669:   PetscCall(PetscObjectGetComm((PetscObject)snes, &comm));
    2670:   PetscCall(PetscViewerASCIIGetStdout(comm, &viewer));
    2671:   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
    2672:   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)snes)->tablevel));
    2673:   PetscCall(PetscViewerASCIIPrintf(viewer, "  ---------- Testing Function -------------\n"));
    2674:   if (!complete_print && !directionsprinted) {
    2675:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Run with -snes_test_function_view and optionally -snes_test_function <threshold> to show difference\n"));
    2676:     PetscCall(PetscViewerASCIIPrintf(viewer, "    of hand-coded and finite difference function entries greater than <threshold>.\n"));
    2677:   }
    2678:   if (!directionsprinted) {
    2679:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Testing hand-coded Function, if (for double precision runs) ||F - Ffd||/||F|| is\n"));
    2680:     PetscCall(PetscViewerASCIIPrintf(viewer, "    O(1.e-8), the hand-coded Function is probably correct.\n"));
    2681:     directionsprinted = PETSC_TRUE;
    2682:   }
    2683:   if (complete_print) PetscCall(PetscViewerPushFormat(mviewer, format));

    2685:   PetscCall(SNESGetSolution(snes, &x));
    2686:   PetscCall(VecDuplicate(x, &g1));
    2687:   PetscCall(VecDuplicate(x, &g2));
    2688:   PetscCall(VecDuplicate(x, &g3));
    2689:   PetscCall(SNESComputeFunction(snes, x, g1));
    2690:   PetscCall(SNESComputeFunction_FD(snes, x, g2));

    2692:   PetscCall(VecNorm(g2, NORM_2, &fdnorm));
    2693:   PetscCall(VecNorm(g1, NORM_2, &hcnorm));
    2694:   PetscCall(VecNorm(g2, NORM_INFINITY, &fdmax));
    2695:   PetscCall(VecNorm(g1, NORM_INFINITY, &hcmax));
    2696:   PetscCall(VecDot(g1, g2, &dot));
    2697:   PetscCall(VecCopy(g1, g3));
    2698:   PetscCall(VecAXPY(g3, -1.0, g2));
    2699:   PetscCall(VecNorm(g3, NORM_2, &diffnorm));
    2700:   PetscCall(VecNorm(g3, NORM_INFINITY, &diffmax));
    2701:   PetscCall(PetscViewerASCIIPrintf(viewer, "  ||Ffd|| %g, ||F|| = %g, angle cosine = (Ffd'F)/||Ffd||||F|| = %g\n", (double)fdnorm, (double)hcnorm, (double)(PetscRealPart(dot) / (fdnorm * hcnorm))));
    2702:   PetscCall(PetscViewerASCIIPrintf(viewer, "  2-norm ||F - Ffd||/||F|| = %g, ||F - Ffd|| = %g\n", (double)(diffnorm / PetscMax(hcnorm, fdnorm)), (double)diffnorm));
    2703:   PetscCall(PetscViewerASCIIPrintf(viewer, "  max-norm ||F - Ffd||/||F|| = %g, ||F - Ffd|| = %g\n", (double)(diffmax / PetscMax(hcmax, fdmax)), (double)diffmax));

    2705:   if (complete_print) {
    2706:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Hand-coded function ----------\n"));
    2707:     PetscCall(VecView(g1, mviewer));
    2708:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Finite difference function ----------\n"));
    2709:     PetscCall(VecView(g2, mviewer));
    2710:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Hand-coded minus finite-difference function ----------\n"));
    2711:     PetscCall(VecView(g3, mviewer));
    2712:   }
    2713:   PetscCall(VecDestroy(&g1));
    2714:   PetscCall(VecDestroy(&g2));
    2715:   PetscCall(VecDestroy(&g3));

    2717:   if (complete_print) {
    2718:     PetscCall(PetscViewerPopFormat(mviewer));
    2719:     PetscCall(PetscViewerDestroy(&mviewer));
    2720:   }
    2721:   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
    2722:   PetscFunctionReturn(PETSC_SUCCESS);
    2723: }

    2725: PetscErrorCode SNESTestJacobian(SNES snes)
    2726: {
    2727:   Mat               A, B, C, D, jacobian;
    2728:   Vec               x = snes->vec_sol, f;
    2729:   PetscReal         nrm, gnorm;
    2730:   PetscReal         threshold = 1.e-5;
    2731:   MatType           mattype;
    2732:   PetscInt          m, n, M, N;
    2733:   void             *functx;
    2734:   PetscBool         complete_print = PETSC_FALSE, threshold_print = PETSC_FALSE, test = PETSC_FALSE, flg, istranspose;
    2735:   PetscViewer       viewer, mviewer;
    2736:   MPI_Comm          comm;
    2737:   PetscInt          tabs;
    2738:   static PetscBool  directionsprinted = PETSC_FALSE;
    2739:   PetscViewerFormat format;

    2741:   PetscFunctionBegin;
    2742:   PetscObjectOptionsBegin((PetscObject)snes);
    2743:   PetscCall(PetscOptionsName("-snes_test_jacobian", "Compare hand-coded and finite difference Jacobians", "None", &test));
    2744:   PetscCall(PetscOptionsReal("-snes_test_jacobian", "Threshold for element difference between hand-coded and finite difference being meaningful", "None", threshold, &threshold, NULL));
    2745:   PetscCall(PetscOptionsDeprecated("-snes_test_jacobian_display", "-snes_test_jacobian_view", "3.13", NULL));
    2746:   PetscCall(PetscOptionsViewer("-snes_test_jacobian_view", "View difference between hand-coded and finite difference Jacobians element entries", "None", &mviewer, &format, &complete_print));
    2747:   PetscCall(PetscOptionsDeprecated("-snes_test_jacobian_display_threshold", "-snes_test_jacobian", "3.13", "-snes_test_jacobian accepts an optional threshold (since v3.10)"));
    2748:   PetscCall(PetscOptionsReal("-snes_test_jacobian_display_threshold", "Display difference between hand-coded and finite difference Jacobians which exceed input threshold", "None", threshold, &threshold, &threshold_print));
    2749:   PetscOptionsEnd();
    2750:   if (!test) PetscFunctionReturn(PETSC_SUCCESS);

    2752:   PetscCall(PetscObjectGetComm((PetscObject)snes, &comm));
    2753:   PetscCall(PetscViewerASCIIGetStdout(comm, &viewer));
    2754:   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
    2755:   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)snes)->tablevel));
    2756:   PetscCall(PetscViewerASCIIPrintf(viewer, "  ---------- Testing Jacobian -------------\n"));
    2757:   if (!complete_print && !directionsprinted) {
    2758:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Run with -snes_test_jacobian_view and optionally -snes_test_jacobian <threshold> to show difference\n"));
    2759:     PetscCall(PetscViewerASCIIPrintf(viewer, "    of hand-coded and finite difference Jacobian entries greater than <threshold>.\n"));
    2760:   }
    2761:   if (!directionsprinted) {
    2762:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Testing hand-coded Jacobian, if (for double precision runs) ||J - Jfd||_F/||J||_F is\n"));
    2763:     PetscCall(PetscViewerASCIIPrintf(viewer, "    O(1.e-8), the hand-coded Jacobian is probably correct.\n"));
    2764:     directionsprinted = PETSC_TRUE;
    2765:   }
    2766:   if (complete_print) PetscCall(PetscViewerPushFormat(mviewer, format));

    2768:   PetscCall(PetscObjectTypeCompare((PetscObject)snes->jacobian, MATMFFD, &flg));
    2769:   if (!flg) jacobian = snes->jacobian;
    2770:   else jacobian = snes->jacobian_pre;

    2772:   if (!x) PetscCall(MatCreateVecs(jacobian, &x, NULL));
    2773:   else PetscCall(PetscObjectReference((PetscObject)x));
    2774:   PetscCall(VecDuplicate(x, &f));

    2776:   /* evaluate the function at this point because SNESComputeJacobianDefault() assumes that the function has been evaluated and put into snes->vec_func */
    2777:   PetscCall(SNESComputeFunction(snes, x, f));
    2778:   PetscCall(VecDestroy(&f));
    2779:   PetscCall(PetscObjectTypeCompare((PetscObject)snes, SNESKSPTRANSPOSEONLY, &istranspose));
    2780:   while (jacobian) {
    2781:     Mat JT = NULL, Jsave = NULL;

    2783:     if (istranspose) {
    2784:       PetscCall(MatCreateTranspose(jacobian, &JT));
    2785:       Jsave    = jacobian;
    2786:       jacobian = JT;
    2787:     }
    2788:     PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)jacobian, &flg, MATSEQAIJ, MATMPIAIJ, MATSEQDENSE, MATMPIDENSE, MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ, ""));
    2789:     if (flg) {
    2790:       A = jacobian;
    2791:       PetscCall(PetscObjectReference((PetscObject)A));
    2792:     } else {
    2793:       PetscCall(MatComputeOperator(jacobian, MATAIJ, &A));
    2794:     }

    2796:     PetscCall(MatGetType(A, &mattype));
    2797:     PetscCall(MatGetSize(A, &M, &N));
    2798:     PetscCall(MatGetLocalSize(A, &m, &n));
    2799:     PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
    2800:     PetscCall(MatSetType(B, mattype));
    2801:     PetscCall(MatSetSizes(B, m, n, M, N));
    2802:     PetscCall(MatSetBlockSizesFromMats(B, A, A));
    2803:     PetscCall(MatSetUp(B));
    2804:     PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));

    2806:     PetscCall(SNESGetFunction(snes, NULL, NULL, &functx));
    2807:     PetscCall(SNESComputeJacobianDefault(snes, x, B, B, functx));

    2809:     PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &D));
    2810:     PetscCall(MatAYPX(D, -1.0, A, DIFFERENT_NONZERO_PATTERN));
    2811:     PetscCall(MatNorm(D, NORM_FROBENIUS, &nrm));
    2812:     PetscCall(MatNorm(A, NORM_FROBENIUS, &gnorm));
    2813:     PetscCall(MatDestroy(&D));
    2814:     if (!gnorm) gnorm = 1; /* just in case */
    2815:     PetscCall(PetscViewerASCIIPrintf(viewer, "  ||J - Jfd||_F/||J||_F = %g, ||J - Jfd||_F = %g\n", (double)(nrm / gnorm), (double)nrm));

    2817:     if (complete_print) {
    2818:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Hand-coded Jacobian ----------\n"));
    2819:       PetscCall(MatView(A, mviewer));
    2820:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Finite difference Jacobian ----------\n"));
    2821:       PetscCall(MatView(B, mviewer));
    2822:     }

    2824:     if (threshold_print || complete_print) {
    2825:       PetscInt           Istart, Iend, *ccols, bncols, cncols, j, row;
    2826:       PetscScalar       *cvals;
    2827:       const PetscInt    *bcols;
    2828:       const PetscScalar *bvals;

    2830:       PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
    2831:       PetscCall(MatSetType(C, mattype));
    2832:       PetscCall(MatSetSizes(C, m, n, M, N));
    2833:       PetscCall(MatSetBlockSizesFromMats(C, A, A));
    2834:       PetscCall(MatSetUp(C));
    2835:       PetscCall(MatSetOption(C, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));

    2837:       PetscCall(MatAYPX(B, -1.0, A, DIFFERENT_NONZERO_PATTERN));
    2838:       PetscCall(MatGetOwnershipRange(B, &Istart, &Iend));

    2840:       for (row = Istart; row < Iend; row++) {
    2841:         PetscCall(MatGetRow(B, row, &bncols, &bcols, &bvals));
    2842:         PetscCall(PetscMalloc2(bncols, &ccols, bncols, &cvals));
    2843:         for (j = 0, cncols = 0; j < bncols; j++) {
    2844:           if (PetscAbsScalar(bvals[j]) > threshold) {
    2845:             ccols[cncols] = bcols[j];
    2846:             cvals[cncols] = bvals[j];
    2847:             cncols += 1;
    2848:           }
    2849:         }
    2850:         if (cncols) PetscCall(MatSetValues(C, 1, &row, cncols, ccols, cvals, INSERT_VALUES));
    2851:         PetscCall(MatRestoreRow(B, row, &bncols, &bcols, &bvals));
    2852:         PetscCall(PetscFree2(ccols, cvals));
    2853:       }
    2854:       PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
    2855:       PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
    2856:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Hand-coded minus finite-difference Jacobian with tolerance %g ----------\n", (double)threshold));
    2857:       PetscCall(MatView(C, complete_print ? mviewer : viewer));
    2858:       PetscCall(MatDestroy(&C));
    2859:     }
    2860:     PetscCall(MatDestroy(&A));
    2861:     PetscCall(MatDestroy(&B));
    2862:     PetscCall(MatDestroy(&JT));
    2863:     if (Jsave) jacobian = Jsave;
    2864:     if (jacobian != snes->jacobian_pre) {
    2865:       jacobian = snes->jacobian_pre;
    2866:       PetscCall(PetscViewerASCIIPrintf(viewer, "  ---------- Testing Jacobian for preconditioner -------------\n"));
    2867:     } else jacobian = NULL;
    2868:   }
    2869:   PetscCall(VecDestroy(&x));
    2870:   if (complete_print) PetscCall(PetscViewerPopFormat(mviewer));
    2871:   if (mviewer) PetscCall(PetscViewerDestroy(&mviewer));
    2872:   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
    2873:   PetscFunctionReturn(PETSC_SUCCESS);
    2874: }

    2876: /*@
    2877:   SNESComputeJacobian - Computes the Jacobian matrix that has been set with `SNESSetJacobian()`.

    2879:   Collective

    2881:   Input Parameters:
    2882: + snes - the `SNES` context
    2883: - X    - input vector

    2885:   Output Parameters:
    2886: + A - Jacobian matrix
    2887: - B - optional matrix for building the preconditioner, usually the same as `A`

    2889:   Options Database Keys:
    2890: + -snes_lag_preconditioner <lag>           - how often to rebuild preconditioner
    2891: . -snes_lag_jacobian <lag>                 - how often to rebuild Jacobian
    2892: . -snes_test_jacobian <optional threshold> - compare the user provided Jacobian with one compute via finite differences to check for errors.  If a threshold is given, display only those entries whose difference is greater than the threshold.
    2893: . -snes_test_jacobian_view                 - display the user provided Jacobian, the finite difference Jacobian and the difference between them to help users detect the location of errors in the user provided Jacobian
    2894: . -snes_compare_explicit                   - Compare the computed Jacobian to the finite difference Jacobian and output the differences
    2895: . -snes_compare_explicit_draw              - Compare the computed Jacobian to the finite difference Jacobian and draw the result
    2896: . -snes_compare_explicit_contour           - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result
    2897: . -snes_compare_operator                   - Make the comparison options above use the operator instead of the preconditioning matrix
    2898: . -snes_compare_coloring                   - Compute the finite difference Jacobian using coloring and display norms of difference
    2899: . -snes_compare_coloring_display           - Compute the finite difference Jacobian using coloring and display verbose differences
    2900: . -snes_compare_coloring_threshold         - Display only those matrix entries that differ by more than a given threshold
    2901: . -snes_compare_coloring_threshold_atol    - Absolute tolerance for difference in matrix entries to be displayed by `-snes_compare_coloring_threshold`
    2902: . -snes_compare_coloring_threshold_rtol    - Relative tolerance for difference in matrix entries to be displayed by `-snes_compare_coloring_threshold`
    2903: . -snes_compare_coloring_draw              - Compute the finite difference Jacobian using coloring and draw differences
    2904: - -snes_compare_coloring_draw_contour      - Compute the finite difference Jacobian using coloring and show contours of matrices and differences

    2906:   Level: developer

    2908:   Note:
    2909:   Most users should not need to explicitly call this routine, as it
    2910:   is used internally within the nonlinear solvers.

    2912:   Developer Note:
    2913:   This has duplicative ways of checking the accuracy of the user provided Jacobian (see the options above). This is for historical reasons, the routine `SNESTestJacobian()` use to used
    2914:   with the `SNESType` of test that has been removed.

    2916: .seealso: [](ch_snes), `SNESSetJacobian()`, `KSPSetOperators()`, `MatStructure`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobian()`
    2917: @*/
    2918: PetscErrorCode SNESComputeJacobian(SNES snes, Vec X, Mat A, Mat B)
    2919: {
    2920:   PetscBool flag;
    2921:   DM        dm;
    2922:   DMSNES    sdm;
    2923:   KSP       ksp;

    2925:   PetscFunctionBegin;
    2928:   PetscCheckSameComm(snes, 1, X, 2);
    2929:   PetscCall(VecValidValues_Internal(X, 2, PETSC_TRUE));
    2930:   PetscCall(SNESGetDM(snes, &dm));
    2931:   PetscCall(DMGetDMSNES(dm, &sdm));

    2933:   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix-free */
    2934:   if (snes->lagjacobian == -2) {
    2935:     snes->lagjacobian = -1;

    2937:     PetscCall(PetscInfo(snes, "Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n"));
    2938:   } else if (snes->lagjacobian == -1) {
    2939:     PetscCall(PetscInfo(snes, "Reusing Jacobian/preconditioner because lag is -1\n"));
    2940:     PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flag));
    2941:     if (flag) {
    2942:       PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
    2943:       PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
    2944:     }
    2945:     PetscFunctionReturn(PETSC_SUCCESS);
    2946:   } else if (snes->lagjacobian > 1 && (snes->iter + snes->jac_iter) % snes->lagjacobian) {
    2947:     PetscCall(PetscInfo(snes, "Reusing Jacobian/preconditioner because lag is %" PetscInt_FMT " and SNES iteration is %" PetscInt_FMT "\n", snes->lagjacobian, snes->iter));
    2948:     PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flag));
    2949:     if (flag) {
    2950:       PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
    2951:       PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
    2952:     }
    2953:     PetscFunctionReturn(PETSC_SUCCESS);
    2954:   }
    2955:   if (snes->npc && snes->npcside == PC_LEFT) {
    2956:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
    2957:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
    2958:     PetscFunctionReturn(PETSC_SUCCESS);
    2959:   }

    2961:   PetscCall(PetscLogEventBegin(SNES_JacobianEval, snes, X, A, B));
    2962:   PetscCall(VecLockReadPush(X));
    2963:   {
    2964:     void           *ctx;
    2965:     SNESJacobianFn *J;
    2966:     PetscCall(DMSNESGetJacobian(dm, &J, &ctx));
    2967:     PetscCallBack("SNES callback Jacobian", (*J)(snes, X, A, B, ctx));
    2968:   }
    2969:   PetscCall(VecLockReadPop(X));
    2970:   PetscCall(PetscLogEventEnd(SNES_JacobianEval, snes, X, A, B));

    2972:   /* attach latest linearization point to the preconditioning matrix */
    2973:   PetscCall(PetscObjectCompose((PetscObject)B, "__SNES_latest_X", (PetscObject)X));

    2975:   /* the next line ensures that snes->ksp exists */
    2976:   PetscCall(SNESGetKSP(snes, &ksp));
    2977:   if (snes->lagpreconditioner == -2) {
    2978:     PetscCall(PetscInfo(snes, "Rebuilding preconditioner exactly once since lag is -2\n"));
    2979:     PetscCall(KSPSetReusePreconditioner(snes->ksp, PETSC_FALSE));
    2980:     snes->lagpreconditioner = -1;
    2981:   } else if (snes->lagpreconditioner == -1) {
    2982:     PetscCall(PetscInfo(snes, "Reusing preconditioner because lag is -1\n"));
    2983:     PetscCall(KSPSetReusePreconditioner(snes->ksp, PETSC_TRUE));
    2984:   } else if (snes->lagpreconditioner > 1 && (snes->iter + snes->pre_iter) % snes->lagpreconditioner) {
    2985:     PetscCall(PetscInfo(snes, "Reusing preconditioner because lag is %" PetscInt_FMT " and SNES iteration is %" PetscInt_FMT "\n", snes->lagpreconditioner, snes->iter));
    2986:     PetscCall(KSPSetReusePreconditioner(snes->ksp, PETSC_TRUE));
    2987:   } else {
    2988:     PetscCall(PetscInfo(snes, "Rebuilding preconditioner\n"));
    2989:     PetscCall(KSPSetReusePreconditioner(snes->ksp, PETSC_FALSE));
    2990:   }

    2992:   /* monkey business to allow testing Jacobians in multilevel solvers.
    2993:      This is needed because the SNESTestXXX interface does not accept vectors and matrices */
    2994:   {
    2995:     Vec xsave            = snes->vec_sol;
    2996:     Mat jacobiansave     = snes->jacobian;
    2997:     Mat jacobian_presave = snes->jacobian_pre;

    2999:     snes->vec_sol      = X;
    3000:     snes->jacobian     = A;
    3001:     snes->jacobian_pre = B;
    3002:     PetscCall(SNESTestFunction(snes));
    3003:     PetscCall(SNESTestJacobian(snes));

    3005:     snes->vec_sol      = xsave;
    3006:     snes->jacobian     = jacobiansave;
    3007:     snes->jacobian_pre = jacobian_presave;
    3008:   }

    3010:   {
    3011:     PetscBool flag = PETSC_FALSE, flag_draw = PETSC_FALSE, flag_contour = PETSC_FALSE, flag_operator = PETSC_FALSE;
    3012:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_explicit", NULL, NULL, &flag));
    3013:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_explicit_draw", NULL, NULL, &flag_draw));
    3014:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_explicit_draw_contour", NULL, NULL, &flag_contour));
    3015:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_operator", NULL, NULL, &flag_operator));
    3016:     if (flag || flag_draw || flag_contour) {
    3017:       Mat         Bexp_mine = NULL, Bexp, FDexp;
    3018:       PetscViewer vdraw, vstdout;
    3019:       PetscBool   flg;
    3020:       if (flag_operator) {
    3021:         PetscCall(MatComputeOperator(A, MATAIJ, &Bexp_mine));
    3022:         Bexp = Bexp_mine;
    3023:       } else {
    3024:         /* See if the preconditioning matrix can be viewed and added directly */
    3025:         PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)B, &flg, MATSEQAIJ, MATMPIAIJ, MATSEQDENSE, MATMPIDENSE, MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPIBAIJ, ""));
    3026:         if (flg) Bexp = B;
    3027:         else {
    3028:           /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */
    3029:           PetscCall(MatComputeOperator(B, MATAIJ, &Bexp_mine));
    3030:           Bexp = Bexp_mine;
    3031:         }
    3032:       }
    3033:       PetscCall(MatConvert(Bexp, MATSAME, MAT_INITIAL_MATRIX, &FDexp));
    3034:       PetscCall(SNESComputeJacobianDefault(snes, X, FDexp, FDexp, NULL));
    3035:       PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes), &vstdout));
    3036:       if (flag_draw || flag_contour) {
    3037:         PetscCall(PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes), NULL, "Explicit Jacobians", PETSC_DECIDE, PETSC_DECIDE, 300, 300, &vdraw));
    3038:         if (flag_contour) PetscCall(PetscViewerPushFormat(vdraw, PETSC_VIEWER_DRAW_CONTOUR));
    3039:       } else vdraw = NULL;
    3040:       PetscCall(PetscViewerASCIIPrintf(vstdout, "Explicit %s\n", flag_operator ? "Jacobian" : "preconditioning Jacobian"));
    3041:       if (flag) PetscCall(MatView(Bexp, vstdout));
    3042:       if (vdraw) PetscCall(MatView(Bexp, vdraw));
    3043:       PetscCall(PetscViewerASCIIPrintf(vstdout, "Finite difference Jacobian\n"));
    3044:       if (flag) PetscCall(MatView(FDexp, vstdout));
    3045:       if (vdraw) PetscCall(MatView(FDexp, vdraw));
    3046:       PetscCall(MatAYPX(FDexp, -1.0, Bexp, SAME_NONZERO_PATTERN));
    3047:       PetscCall(PetscViewerASCIIPrintf(vstdout, "User-provided matrix minus finite difference Jacobian\n"));
    3048:       if (flag) PetscCall(MatView(FDexp, vstdout));
    3049:       if (vdraw) { /* Always use contour for the difference */
    3050:         PetscCall(PetscViewerPushFormat(vdraw, PETSC_VIEWER_DRAW_CONTOUR));
    3051:         PetscCall(MatView(FDexp, vdraw));
    3052:         PetscCall(PetscViewerPopFormat(vdraw));
    3053:       }
    3054:       if (flag_contour) PetscCall(PetscViewerPopFormat(vdraw));
    3055:       PetscCall(PetscViewerDestroy(&vdraw));
    3056:       PetscCall(MatDestroy(&Bexp_mine));
    3057:       PetscCall(MatDestroy(&FDexp));
    3058:     }
    3059:   }
    3060:   {
    3061:     PetscBool flag = PETSC_FALSE, flag_display = PETSC_FALSE, flag_draw = PETSC_FALSE, flag_contour = PETSC_FALSE, flag_threshold = PETSC_FALSE;
    3062:     PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON, threshold_rtol = 10 * PETSC_SQRT_MACHINE_EPSILON;
    3063:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring", NULL, NULL, &flag));
    3064:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_display", NULL, NULL, &flag_display));
    3065:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_draw", NULL, NULL, &flag_draw));
    3066:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_draw_contour", NULL, NULL, &flag_contour));
    3067:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_threshold", NULL, NULL, &flag_threshold));
    3068:     if (flag_threshold) {
    3069:       PetscCall(PetscOptionsGetReal(((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_threshold_rtol", &threshold_rtol, NULL));
    3070:       PetscCall(PetscOptionsGetReal(((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_threshold_atol", &threshold_atol, NULL));
    3071:     }
    3072:     if (flag || flag_display || flag_draw || flag_contour || flag_threshold) {
    3073:       Mat             Bfd;
    3074:       PetscViewer     vdraw, vstdout;
    3075:       MatColoring     coloring;
    3076:       ISColoring      iscoloring;
    3077:       MatFDColoring   matfdcoloring;
    3078:       SNESFunctionFn *func;
    3079:       void           *funcctx;
    3080:       PetscReal       norm1, norm2, normmax;

    3082:       PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &Bfd));
    3083:       PetscCall(MatColoringCreate(Bfd, &coloring));
    3084:       PetscCall(MatColoringSetType(coloring, MATCOLORINGSL));
    3085:       PetscCall(MatColoringSetFromOptions(coloring));
    3086:       PetscCall(MatColoringApply(coloring, &iscoloring));
    3087:       PetscCall(MatColoringDestroy(&coloring));
    3088:       PetscCall(MatFDColoringCreate(Bfd, iscoloring, &matfdcoloring));
    3089:       PetscCall(MatFDColoringSetFromOptions(matfdcoloring));
    3090:       PetscCall(MatFDColoringSetUp(Bfd, iscoloring, matfdcoloring));
    3091:       PetscCall(ISColoringDestroy(&iscoloring));

    3093:       /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */
    3094:       PetscCall(SNESGetFunction(snes, NULL, &func, &funcctx));
    3095:       PetscCall(MatFDColoringSetFunction(matfdcoloring, (PetscErrorCode (*)(void))func, funcctx));
    3096:       PetscCall(PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring, ((PetscObject)snes)->prefix));
    3097:       PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring, "coloring_"));
    3098:       PetscCall(MatFDColoringSetFromOptions(matfdcoloring));
    3099:       PetscCall(MatFDColoringApply(Bfd, matfdcoloring, X, snes));
    3100:       PetscCall(MatFDColoringDestroy(&matfdcoloring));

    3102:       PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes), &vstdout));
    3103:       if (flag_draw || flag_contour) {
    3104:         PetscCall(PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes), NULL, "Colored Jacobians", PETSC_DECIDE, PETSC_DECIDE, 300, 300, &vdraw));
    3105:         if (flag_contour) PetscCall(PetscViewerPushFormat(vdraw, PETSC_VIEWER_DRAW_CONTOUR));
    3106:       } else vdraw = NULL;
    3107:       PetscCall(PetscViewerASCIIPrintf(vstdout, "Explicit preconditioning Jacobian\n"));
    3108:       if (flag_display) PetscCall(MatView(B, vstdout));
    3109:       if (vdraw) PetscCall(MatView(B, vdraw));
    3110:       PetscCall(PetscViewerASCIIPrintf(vstdout, "Colored Finite difference Jacobian\n"));
    3111:       if (flag_display) PetscCall(MatView(Bfd, vstdout));
    3112:       if (vdraw) PetscCall(MatView(Bfd, vdraw));
    3113:       PetscCall(MatAYPX(Bfd, -1.0, B, SAME_NONZERO_PATTERN));
    3114:       PetscCall(MatNorm(Bfd, NORM_1, &norm1));
    3115:       PetscCall(MatNorm(Bfd, NORM_FROBENIUS, &norm2));
    3116:       PetscCall(MatNorm(Bfd, NORM_MAX, &normmax));
    3117:       PetscCall(PetscViewerASCIIPrintf(vstdout, "User-provided matrix minus finite difference Jacobian, norm1=%g normFrob=%g normmax=%g\n", (double)norm1, (double)norm2, (double)normmax));
    3118:       if (flag_display) PetscCall(MatView(Bfd, vstdout));
    3119:       if (vdraw) { /* Always use contour for the difference */
    3120:         PetscCall(PetscViewerPushFormat(vdraw, PETSC_VIEWER_DRAW_CONTOUR));
    3121:         PetscCall(MatView(Bfd, vdraw));
    3122:         PetscCall(PetscViewerPopFormat(vdraw));
    3123:       }
    3124:       if (flag_contour) PetscCall(PetscViewerPopFormat(vdraw));

    3126:       if (flag_threshold) {
    3127:         PetscInt bs, rstart, rend, i;
    3128:         PetscCall(MatGetBlockSize(B, &bs));
    3129:         PetscCall(MatGetOwnershipRange(B, &rstart, &rend));
    3130:         for (i = rstart; i < rend; i++) {
    3131:           const PetscScalar *ba, *ca;
    3132:           const PetscInt    *bj, *cj;
    3133:           PetscInt           bn, cn, j, maxentrycol = -1, maxdiffcol = -1, maxrdiffcol = -1;
    3134:           PetscReal          maxentry = 0, maxdiff = 0, maxrdiff = 0;
    3135:           PetscCall(MatGetRow(B, i, &bn, &bj, &ba));
    3136:           PetscCall(MatGetRow(Bfd, i, &cn, &cj, &ca));
    3137:           PetscCheck(bn == cn, ((PetscObject)A)->comm, PETSC_ERR_PLIB, "Unexpected different nonzero pattern in -snes_compare_coloring_threshold");
    3138:           for (j = 0; j < bn; j++) {
    3139:             PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol * PetscAbsScalar(ba[j]));
    3140:             if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) {
    3141:               maxentrycol = bj[j];
    3142:               maxentry    = PetscRealPart(ba[j]);
    3143:             }
    3144:             if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) {
    3145:               maxdiffcol = bj[j];
    3146:               maxdiff    = PetscRealPart(ca[j]);
    3147:             }
    3148:             if (rdiff > maxrdiff) {
    3149:               maxrdiffcol = bj[j];
    3150:               maxrdiff    = rdiff;
    3151:             }
    3152:           }
    3153:           if (maxrdiff > 1) {
    3154:             PetscCall(PetscViewerASCIIPrintf(vstdout, "row %" PetscInt_FMT " (maxentry=%g at %" PetscInt_FMT ", maxdiff=%g at %" PetscInt_FMT ", maxrdiff=%g at %" PetscInt_FMT "):", i, (double)maxentry, maxentrycol, (double)maxdiff, maxdiffcol, (double)maxrdiff, maxrdiffcol));
    3155:             for (j = 0; j < bn; j++) {
    3156:               PetscReal rdiff;
    3157:               rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol * PetscAbsScalar(ba[j]));
    3158:               if (rdiff > 1) PetscCall(PetscViewerASCIIPrintf(vstdout, " (%" PetscInt_FMT ",%g:%g)", bj[j], (double)PetscRealPart(ba[j]), (double)PetscRealPart(ca[j])));
    3159:             }
    3160:             PetscCall(PetscViewerASCIIPrintf(vstdout, "\n"));
    3161:           }
    3162:           PetscCall(MatRestoreRow(B, i, &bn, &bj, &ba));
    3163:           PetscCall(MatRestoreRow(Bfd, i, &cn, &cj, &ca));
    3164:         }
    3165:       }
    3166:       PetscCall(PetscViewerDestroy(&vdraw));
    3167:       PetscCall(MatDestroy(&Bfd));
    3168:     }
    3169:   }
    3170:   PetscFunctionReturn(PETSC_SUCCESS);
    3171: }

    3173: /*@C
    3174:   SNESSetJacobian - Sets the function to compute Jacobian as well as the
    3175:   location to store the matrix.

    3177:   Logically Collective

    3179:   Input Parameters:
    3180: + snes - the `SNES` context
    3181: . Amat - the matrix that defines the (approximate) Jacobian
    3182: . Pmat - the matrix to be used in constructing the preconditioner, usually the same as `Amat`.
    3183: . J    - Jacobian evaluation routine (if `NULL` then `SNES` retains any previously set value), see `SNESJacobianFn` for details
    3184: - ctx  - [optional] user-defined context for private data for the
    3185:          Jacobian evaluation routine (may be `NULL`) (if `NULL` then `SNES` retains any previously set value)

    3187:   Level: beginner

    3189:   Notes:
    3190:   If the `Amat` matrix and `Pmat` matrix are different you must call `MatAssemblyBegin()`/`MatAssemblyEnd()` on
    3191:   each matrix.

    3193:   If you know the operator `Amat` has a null space you can use `MatSetNullSpace()` and `MatSetTransposeNullSpace()` to supply the null
    3194:   space to `Amat` and the `KSP` solvers will automatically use that null space as needed during the solution process.

    3196:   If using `SNESComputeJacobianDefaultColor()` to assemble a Jacobian, the `ctx` argument
    3197:   must be a `MatFDColoring`.

    3199:   Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian.  One common
    3200:   example is to use the "Picard linearization" which only differentiates through the highest order parts of each term using `SNESSetPicard()`

    3202: .seealso: [](ch_snes), `SNES`, `KSPSetOperators()`, `SNESSetFunction()`, `MatMFFDComputeJacobian()`, `SNESComputeJacobianDefaultColor()`, `MatStructure`,
    3203:           `SNESSetPicard()`, `SNESJacobianFn`, `SNESFunctionFn`
    3204: @*/
    3205: PetscErrorCode SNESSetJacobian(SNES snes, Mat Amat, Mat Pmat, SNESJacobianFn *J, void *ctx)
    3206: {
    3207:   DM dm;

    3209:   PetscFunctionBegin;
    3213:   if (Amat) PetscCheckSameComm(snes, 1, Amat, 2);
    3214:   if (Pmat) PetscCheckSameComm(snes, 1, Pmat, 3);
    3215:   PetscCall(SNESGetDM(snes, &dm));
    3216:   PetscCall(DMSNESSetJacobian(dm, J, ctx));
    3217:   if (Amat) {
    3218:     PetscCall(PetscObjectReference((PetscObject)Amat));
    3219:     PetscCall(MatDestroy(&snes->jacobian));

    3221:     snes->jacobian = Amat;
    3222:   }
    3223:   if (Pmat) {
    3224:     PetscCall(PetscObjectReference((PetscObject)Pmat));
    3225:     PetscCall(MatDestroy(&snes->jacobian_pre));

    3227:     snes->jacobian_pre = Pmat;
    3228:   }
    3229:   PetscFunctionReturn(PETSC_SUCCESS);
    3230: }

    3232: /*@C
    3233:   SNESGetJacobian - Returns the Jacobian matrix and optionally the user
    3234:   provided context for evaluating the Jacobian.

    3236:   Not Collective, but `Mat` object will be parallel if `SNES` is

    3238:   Input Parameter:
    3239: . snes - the nonlinear solver context

    3241:   Output Parameters:
    3242: + Amat - location to stash (approximate) Jacobian matrix (or `NULL`)
    3243: . Pmat - location to stash matrix used to compute the preconditioner (or `NULL`)
    3244: . J    - location to put Jacobian function (or `NULL`), for calling sequence see `SNESJacobianFn`
    3245: - ctx  - location to stash Jacobian ctx (or `NULL`)

    3247:   Level: advanced

    3249: .seealso: [](ch_snes), `SNES`, `Mat`, `SNESSetJacobian()`, `SNESComputeJacobian()`, `SNESJacobianFn`, `SNESGetFunction()`
    3250: @*/
    3251: PetscErrorCode SNESGetJacobian(SNES snes, Mat *Amat, Mat *Pmat, SNESJacobianFn **J, void **ctx)
    3252: {
    3253:   DM dm;

    3255:   PetscFunctionBegin;
    3257:   if (Amat) *Amat = snes->jacobian;
    3258:   if (Pmat) *Pmat = snes->jacobian_pre;
    3259:   PetscCall(SNESGetDM(snes, &dm));
    3260:   PetscCall(DMSNESGetJacobian(dm, J, ctx));
    3261:   PetscFunctionReturn(PETSC_SUCCESS);
    3262: }

    3264: static PetscErrorCode SNESSetDefaultComputeJacobian(SNES snes)
    3265: {
    3266:   DM     dm;
    3267:   DMSNES sdm;

    3269:   PetscFunctionBegin;
    3270:   PetscCall(SNESGetDM(snes, &dm));
    3271:   PetscCall(DMGetDMSNES(dm, &sdm));
    3272:   if (!sdm->ops->computejacobian && snes->jacobian_pre) {
    3273:     DM        dm;
    3274:     PetscBool isdense, ismf;

    3276:     PetscCall(SNESGetDM(snes, &dm));
    3277:     PetscCall(PetscObjectTypeCompareAny((PetscObject)snes->jacobian_pre, &isdense, MATSEQDENSE, MATMPIDENSE, MATDENSE, NULL));
    3278:     PetscCall(PetscObjectTypeCompareAny((PetscObject)snes->jacobian_pre, &ismf, MATMFFD, MATSHELL, NULL));
    3279:     if (isdense) {
    3280:       PetscCall(DMSNESSetJacobian(dm, SNESComputeJacobianDefault, NULL));
    3281:     } else if (!ismf) {
    3282:       PetscCall(DMSNESSetJacobian(dm, SNESComputeJacobianDefaultColor, NULL));
    3283:     }
    3284:   }
    3285:   PetscFunctionReturn(PETSC_SUCCESS);
    3286: }

    3288: /*@
    3289:   SNESSetUp - Sets up the internal data structures for the later use
    3290:   of a nonlinear solver `SNESSolve()`.

    3292:   Collective

    3294:   Input Parameter:
    3295: . snes - the `SNES` context

    3297:   Level: advanced

    3299:   Note:
    3300:   For basic use of the `SNES` solvers the user does not need to explicitly call
    3301:   `SNESSetUp()`, since these actions will automatically occur during
    3302:   the call to `SNESSolve()`.  However, if one wishes to control this
    3303:   phase separately, `SNESSetUp()` should be called after `SNESCreate()`
    3304:   and optional routines of the form SNESSetXXX(), but before `SNESSolve()`.

    3306: .seealso: [](ch_snes), `SNES`, `SNESCreate()`, `SNESSolve()`, `SNESDestroy()`, `SNESSetFromOptions()`
    3307: @*/
    3308: PetscErrorCode SNESSetUp(SNES snes)
    3309: {
    3310:   DM             dm;
    3311:   DMSNES         sdm;
    3312:   SNESLineSearch linesearch, pclinesearch;
    3313:   void          *lsprectx, *lspostctx;
    3314:   PetscBool      mf_operator, mf;
    3315:   Vec            f, fpc;
    3316:   void          *funcctx;
    3317:   void          *jacctx, *appctx;
    3318:   Mat            j, jpre;
    3319:   PetscErrorCode (*precheck)(SNESLineSearch, Vec, Vec, PetscBool *, void *);
    3320:   PetscErrorCode (*postcheck)(SNESLineSearch, Vec, Vec, Vec, PetscBool *, PetscBool *, void *);
    3321:   SNESFunctionFn *func;
    3322:   SNESJacobianFn *jac;

    3324:   PetscFunctionBegin;
    3326:   if (snes->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
    3327:   PetscCall(PetscLogEventBegin(SNES_SetUp, snes, 0, 0, 0));

    3329:   if (!((PetscObject)snes)->type_name) PetscCall(SNESSetType(snes, SNESNEWTONLS));

    3331:   PetscCall(SNESGetFunction(snes, &snes->vec_func, NULL, NULL));

    3333:   PetscCall(SNESGetDM(snes, &dm));
    3334:   PetscCall(DMGetDMSNES(dm, &sdm));
    3335:   PetscCall(SNESSetDefaultComputeJacobian(snes));

    3337:   if (!snes->vec_func) PetscCall(DMCreateGlobalVector(dm, &snes->vec_func));

    3339:   if (!snes->ksp) PetscCall(SNESGetKSP(snes, &snes->ksp));

    3341:   if (snes->linesearch) {
    3342:     PetscCall(SNESGetLineSearch(snes, &snes->linesearch));
    3343:     PetscCall(SNESLineSearchSetFunction(snes->linesearch, SNESComputeFunction));
    3344:   }

    3346:   PetscCall(SNESGetUseMatrixFree(snes, &mf_operator, &mf));
    3347:   if (snes->npc && snes->npcside == PC_LEFT) {
    3348:     snes->mf          = PETSC_TRUE;
    3349:     snes->mf_operator = PETSC_FALSE;
    3350:   }

    3352:   if (snes->npc) {
    3353:     /* copy the DM over */
    3354:     PetscCall(SNESGetDM(snes, &dm));
    3355:     PetscCall(SNESSetDM(snes->npc, dm));

    3357:     PetscCall(SNESGetFunction(snes, &f, &func, &funcctx));
    3358:     PetscCall(VecDuplicate(f, &fpc));
    3359:     PetscCall(SNESSetFunction(snes->npc, fpc, func, funcctx));
    3360:     PetscCall(SNESGetJacobian(snes, &j, &jpre, &jac, &jacctx));
    3361:     PetscCall(SNESSetJacobian(snes->npc, j, jpre, jac, jacctx));
    3362:     PetscCall(SNESGetApplicationContext(snes, &appctx));
    3363:     PetscCall(SNESSetApplicationContext(snes->npc, appctx));
    3364:     PetscCall(SNESSetUseMatrixFree(snes->npc, mf_operator, mf));
    3365:     PetscCall(VecDestroy(&fpc));

    3367:     /* copy the function pointers over */
    3368:     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)snes, (PetscObject)snes->npc));

    3370:     /* default to 1 iteration */
    3371:     PetscCall(SNESSetTolerances(snes->npc, 0.0, 0.0, 0.0, 1, snes->npc->max_funcs));
    3372:     if (snes->npcside == PC_RIGHT) {
    3373:       PetscCall(SNESSetNormSchedule(snes->npc, SNES_NORM_FINAL_ONLY));
    3374:     } else {
    3375:       PetscCall(SNESSetNormSchedule(snes->npc, SNES_NORM_NONE));
    3376:     }
    3377:     PetscCall(SNESSetFromOptions(snes->npc));

    3379:     /* copy the line search context over */
    3380:     if (snes->linesearch && snes->npc->linesearch) {
    3381:       PetscCall(SNESGetLineSearch(snes, &linesearch));
    3382:       PetscCall(SNESGetLineSearch(snes->npc, &pclinesearch));
    3383:       PetscCall(SNESLineSearchGetPreCheck(linesearch, &precheck, &lsprectx));
    3384:       PetscCall(SNESLineSearchGetPostCheck(linesearch, &postcheck, &lspostctx));
    3385:       PetscCall(SNESLineSearchSetPreCheck(pclinesearch, precheck, lsprectx));
    3386:       PetscCall(SNESLineSearchSetPostCheck(pclinesearch, postcheck, lspostctx));
    3387:       PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)pclinesearch));
    3388:     }
    3389:   }
    3390:   if (snes->mf) PetscCall(SNESSetUpMatrixFree_Private(snes, snes->mf_operator, snes->mf_version));
    3391:   if (snes->ops->usercompute && !snes->user) PetscCallBack("SNES callback compute application context", (*snes->ops->usercompute)(snes, (void **)&snes->user));

    3393:   snes->jac_iter = 0;
    3394:   snes->pre_iter = 0;

    3396:   PetscTryTypeMethod(snes, setup);

    3398:   PetscCall(SNESSetDefaultComputeJacobian(snes));

    3400:   if (snes->npc && snes->npcside == PC_LEFT) {
    3401:     if (snes->functype == SNES_FUNCTION_PRECONDITIONED) {
    3402:       if (snes->linesearch) {
    3403:         PetscCall(SNESGetLineSearch(snes, &linesearch));
    3404:         PetscCall(SNESLineSearchSetFunction(linesearch, SNESComputeFunctionDefaultNPC));
    3405:       }
    3406:     }
    3407:   }
    3408:   PetscCall(PetscLogEventEnd(SNES_SetUp, snes, 0, 0, 0));
    3409:   snes->setupcalled = PETSC_TRUE;
    3410:   PetscFunctionReturn(PETSC_SUCCESS);
    3411: }

    3413: /*@
    3414:   SNESReset - Resets a `SNES` context to the state it was in before `SNESSetUp()` was called and removes any allocated `Vec` and `Mat` from its data structures

    3416:   Collective

    3418:   Input Parameter:
    3419: . snes - the nonlinear iterative solver context obtained from `SNESCreate()`

    3421:   Level: intermediate

    3423:   Notes:
    3424:   Any options set on the `SNES` object, including those set with `SNESSetFromOptions()` remain.

    3426:   Call this if you wish to reuse a `SNES` but with different size vectors

    3428:   Also calls the application context destroy routine set with `SNESSetComputeApplicationContext()`

    3430: .seealso: [](ch_snes), `SNES`, `SNESDestroy()`, `SNESCreate()`, `SNESSetUp()`, `SNESSolve()`
    3431: @*/
    3432: PetscErrorCode SNESReset(SNES snes)
    3433: {
    3434:   PetscFunctionBegin;
    3436:   if (snes->ops->userdestroy && snes->user) {
    3437:     PetscCallBack("SNES callback destroy application context", (*snes->ops->userdestroy)((void **)&snes->user));
    3438:     snes->user = NULL;
    3439:   }
    3440:   if (snes->npc) PetscCall(SNESReset(snes->npc));

    3442:   PetscTryTypeMethod(snes, reset);
    3443:   if (snes->ksp) PetscCall(KSPReset(snes->ksp));

    3445:   if (snes->linesearch) PetscCall(SNESLineSearchReset(snes->linesearch));

    3447:   PetscCall(VecDestroy(&snes->vec_rhs));
    3448:   PetscCall(VecDestroy(&snes->vec_sol));
    3449:   PetscCall(VecDestroy(&snes->vec_sol_update));
    3450:   PetscCall(VecDestroy(&snes->vec_func));
    3451:   PetscCall(MatDestroy(&snes->jacobian));
    3452:   PetscCall(MatDestroy(&snes->jacobian_pre));
    3453:   PetscCall(MatDestroy(&snes->picard));
    3454:   PetscCall(VecDestroyVecs(snes->nwork, &snes->work));
    3455:   PetscCall(VecDestroyVecs(snes->nvwork, &snes->vwork));

    3457:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

    3459:   snes->nwork = snes->nvwork = 0;
    3460:   snes->setupcalled          = PETSC_FALSE;
    3461:   PetscFunctionReturn(PETSC_SUCCESS);
    3462: }

    3464: /*@
    3465:   SNESConvergedReasonViewCancel - Clears all the reason view functions for a `SNES` object provided with `SNESConvergedReasonViewSet()` also
    3466:   removes the default viewer.

    3468:   Collective

    3470:   Input Parameter:
    3471: . snes - the nonlinear iterative solver context obtained from `SNESCreate()`

    3473:   Level: intermediate

    3475: .seealso: [](ch_snes), `SNES`, `SNESCreate()`, `SNESDestroy()`, `SNESReset()`, `SNESConvergedReasonViewSet()`
    3476: @*/
    3477: PetscErrorCode SNESConvergedReasonViewCancel(SNES snes)
    3478: {
    3479:   PetscInt i;

    3481:   PetscFunctionBegin;
    3483:   for (i = 0; i < snes->numberreasonviews; i++) {
    3484:     if (snes->reasonviewdestroy[i]) PetscCall((*snes->reasonviewdestroy[i])(&snes->reasonviewcontext[i]));
    3485:   }
    3486:   snes->numberreasonviews = 0;
    3487:   PetscCall(PetscViewerDestroy(&snes->convergedreasonviewer));
    3488:   PetscFunctionReturn(PETSC_SUCCESS);
    3489: }

    3491: /*@
    3492:   SNESDestroy - Destroys the nonlinear solver context that was created
    3493:   with `SNESCreate()`.

    3495:   Collective

    3497:   Input Parameter:
    3498: . snes - the `SNES` context

    3500:   Level: beginner

    3502: .seealso: [](ch_snes), `SNES`, `SNESCreate()`, `SNESSolve()`
    3503: @*/
    3504: PetscErrorCode SNESDestroy(SNES *snes)
    3505: {
    3506:   PetscFunctionBegin;
    3507:   if (!*snes) PetscFunctionReturn(PETSC_SUCCESS);
    3509:   if (--((PetscObject)*snes)->refct > 0) {
    3510:     *snes = NULL;
    3511:     PetscFunctionReturn(PETSC_SUCCESS);
    3512:   }

    3514:   PetscCall(SNESReset(*snes));
    3515:   PetscCall(SNESDestroy(&(*snes)->npc));

    3517:   /* if memory was published with SAWs then destroy it */
    3518:   PetscCall(PetscObjectSAWsViewOff((PetscObject)*snes));
    3519:   PetscTryTypeMethod(*snes, destroy);

    3521:   if ((*snes)->dm) PetscCall(DMCoarsenHookRemove((*snes)->dm, DMCoarsenHook_SNESVecSol, DMRestrictHook_SNESVecSol, *snes));
    3522:   PetscCall(DMDestroy(&(*snes)->dm));
    3523:   PetscCall(KSPDestroy(&(*snes)->ksp));
    3524:   PetscCall(SNESLineSearchDestroy(&(*snes)->linesearch));

    3526:   PetscCall(PetscFree((*snes)->kspconvctx));
    3527:   if ((*snes)->ops->convergeddestroy) PetscCall((*(*snes)->ops->convergeddestroy)((*snes)->cnvP));
    3528:   if ((*snes)->conv_hist_alloc) PetscCall(PetscFree2((*snes)->conv_hist, (*snes)->conv_hist_its));
    3529:   PetscCall(SNESMonitorCancel(*snes));
    3530:   PetscCall(SNESConvergedReasonViewCancel(*snes));
    3531:   PetscCall(PetscHeaderDestroy(snes));
    3532:   PetscFunctionReturn(PETSC_SUCCESS);
    3533: }

    3535: /* ----------- Routines to set solver parameters ---------- */

    3537: /*@
    3538:   SNESSetLagPreconditioner - Sets when the preconditioner is rebuilt in the nonlinear solve `SNESSolve()`.

    3540:   Logically Collective

    3542:   Input Parameters:
    3543: + snes - the `SNES` context
    3544: - lag  - 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
    3545:          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that

    3547:   Options Database Keys:
    3548: + -snes_lag_jacobian_persists <true,false>       - sets the persistence through multiple `SNESSolve()`
    3549: . -snes_lag_jacobian <-2,1,2,...>                - sets the lag
    3550: . -snes_lag_preconditioner_persists <true,false> - sets the persistence through multiple `SNESSolve()`
    3551: - -snes_lag_preconditioner <-2,1,2,...>          - sets the lag

    3553:   Level: intermediate

    3555:   Notes:
    3556:   The default is 1

    3558:   The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 or `SNESSetLagPreconditionerPersists()` was called

    3560:   `SNESSetLagPreconditionerPersists()` allows using the same uniform lagging (for example every second linear solve) across multiple nonlinear solves.

    3562: .seealso: [](ch_snes), `SNESGetLagPreconditioner()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESSetLagPreconditionerPersists()`,
    3563:           `SNESSetLagJacobianPersists()`, `SNES`, `SNESSolve()`
    3564: @*/
    3565: PetscErrorCode SNESSetLagPreconditioner(SNES snes, PetscInt lag)
    3566: {
    3567:   PetscFunctionBegin;
    3569:   PetscCheck(lag >= -2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Lag must be -2, -1, 1 or greater");
    3570:   PetscCheck(lag, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Lag cannot be 0");
    3572:   snes->lagpreconditioner = lag;
    3573:   PetscFunctionReturn(PETSC_SUCCESS);
    3574: }

    3576: /*@
    3577:   SNESSetGridSequence - sets the number of steps of grid sequencing that `SNES` will do

    3579:   Logically Collective

    3581:   Input Parameters:
    3582: + snes  - the `SNES` context
    3583: - steps - the number of refinements to do, defaults to 0

    3585:   Options Database Key:
    3586: . -snes_grid_sequence <steps> - Use grid sequencing to generate initial guess

    3588:   Level: intermediate

    3590:   Notes:
    3591:   Once grid sequencing is turned on `SNESSolve()` will automatically perform the solve on each grid refinement.

    3593:   Use `SNESGetSolution()` to extract the fine grid solution after grid sequencing.

    3595: .seealso: [](ch_snes), `SNES`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESGetGridSequence()`,
    3596:           `SNESSetDM()`, `SNESSolve()`
    3597: @*/
    3598: PetscErrorCode SNESSetGridSequence(SNES snes, PetscInt steps)
    3599: {
    3600:   PetscFunctionBegin;
    3603:   snes->gridsequence = steps;
    3604:   PetscFunctionReturn(PETSC_SUCCESS);
    3605: }

    3607: /*@
    3608:   SNESGetGridSequence - gets the number of steps of grid sequencing that `SNES` will do

    3610:   Logically Collective

    3612:   Input Parameter:
    3613: . snes - the `SNES` context

    3615:   Output Parameter:
    3616: . steps - the number of refinements to do, defaults to 0

    3618:   Level: intermediate

    3620: .seealso: [](ch_snes), `SNESGetLagPreconditioner()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESSetGridSequence()`
    3621: @*/
    3622: PetscErrorCode SNESGetGridSequence(SNES snes, PetscInt *steps)
    3623: {
    3624:   PetscFunctionBegin;
    3626:   *steps = snes->gridsequence;
    3627:   PetscFunctionReturn(PETSC_SUCCESS);
    3628: }

    3630: /*@
    3631:   SNESGetLagPreconditioner - Return how often the preconditioner is rebuilt

    3633:   Not Collective

    3635:   Input Parameter:
    3636: . snes - the `SNES` context

    3638:   Output Parameter:
    3639: . lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
    3640:          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that

    3642:   Level: intermediate

    3644:   Notes:
    3645:   The default is 1

    3647:   The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1

    3649: .seealso: [](ch_snes), `SNES`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobianPersists()`, `SNESSetLagPreconditionerPersists()`
    3650: @*/
    3651: PetscErrorCode SNESGetLagPreconditioner(SNES snes, PetscInt *lag)
    3652: {
    3653:   PetscFunctionBegin;
    3655:   *lag = snes->lagpreconditioner;
    3656:   PetscFunctionReturn(PETSC_SUCCESS);
    3657: }

    3659: /*@
    3660:   SNESSetLagJacobian - Set when the Jacobian is rebuilt in the nonlinear solve. See `SNESSetLagPreconditioner()` for determining how
    3661:   often the preconditioner is rebuilt.

    3663:   Logically Collective

    3665:   Input Parameters:
    3666: + snes - the `SNES` context
    3667: - lag  - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
    3668:          the Jacobian is built etc. -2 means rebuild at next chance but then never again

    3670:   Options Database Keys:
    3671: + -snes_lag_jacobian_persists <true,false>       - sets the persistence through multiple SNES solves
    3672: . -snes_lag_jacobian <-2,1,2,...>                - sets the lag
    3673: . -snes_lag_preconditioner_persists <true,false> - sets the persistence through multiple SNES solves
    3674: - -snes_lag_preconditioner <-2,1,2,...>          - sets the lag.

    3676:   Level: intermediate

    3678:   Notes:
    3679:   The default is 1

    3681:   The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1

    3683:   If  -1 is used before the very first nonlinear solve the CODE WILL FAIL! because no Jacobian is used, use -2 to indicate you want it recomputed
    3684:   at the next Newton step but never again (unless it is reset to another value)

    3686: .seealso: [](ch_snes), `SNES`, `SNESGetLagPreconditioner()`, `SNESSetLagPreconditioner()`, `SNESGetLagJacobianPersists()`, `SNESSetLagPreconditionerPersists()`
    3687: @*/
    3688: PetscErrorCode SNESSetLagJacobian(SNES snes, PetscInt lag)
    3689: {
    3690:   PetscFunctionBegin;
    3692:   PetscCheck(lag >= -2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Lag must be -2, -1, 1 or greater");
    3693:   PetscCheck(lag, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Lag cannot be 0");
    3695:   snes->lagjacobian = lag;
    3696:   PetscFunctionReturn(PETSC_SUCCESS);
    3697: }

    3699: /*@
    3700:   SNESGetLagJacobian - Get how often the Jacobian is rebuilt. See `SNESGetLagPreconditioner()` to determine when the preconditioner is rebuilt

    3702:   Not Collective

    3704:   Input Parameter:
    3705: . snes - the `SNES` context

    3707:   Output Parameter:
    3708: . lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
    3709:          the Jacobian is built etc.

    3711:   Level: intermediate

    3713:   Notes:
    3714:   The default is 1

    3716:   The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 or `SNESSetLagJacobianPersists()` was called.

    3718: .seealso: [](ch_snes), `SNES`, `SNESSetLagJacobian()`, `SNESSetLagPreconditioner()`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobianPersists()`, `SNESSetLagPreconditionerPersists()`

    3720: @*/
    3721: PetscErrorCode SNESGetLagJacobian(SNES snes, PetscInt *lag)
    3722: {
    3723:   PetscFunctionBegin;
    3725:   *lag = snes->lagjacobian;
    3726:   PetscFunctionReturn(PETSC_SUCCESS);
    3727: }

    3729: /*@
    3730:   SNESSetLagJacobianPersists - Set whether or not the Jacobian lagging persists through multiple nonlinear solves

    3732:   Logically collective

    3734:   Input Parameters:
    3735: + snes - the `SNES` context
    3736: - flg  - jacobian lagging persists if true

    3738:   Options Database Keys:
    3739: + -snes_lag_jacobian_persists <true,false>       - sets the persistence through multiple SNES solves
    3740: . -snes_lag_jacobian <-2,1,2,...>                - sets the lag
    3741: . -snes_lag_preconditioner_persists <true,false> - sets the persistence through multiple SNES solves
    3742: - -snes_lag_preconditioner <-2,1,2,...>          - sets the lag

    3744:   Level: advanced

    3746:   Notes:
    3747:   Normally when `SNESSetLagJacobian()` is used, the Jacobian is always rebuilt at the beginning of each new nonlinear solve, this removes that behavior

    3749:   This is useful both for nonlinear preconditioning, where it's appropriate to have the Jacobian be stale by
    3750:   several solves, and for implicit time-stepping, where Jacobian lagging in the inner nonlinear solve over several
    3751:   timesteps may present huge efficiency gains.

    3753: .seealso: [](ch_snes), `SNES`, `SNESSetLagPreconditionerPersists()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESGetNPC()`
    3754: @*/
    3755: PetscErrorCode SNESSetLagJacobianPersists(SNES snes, PetscBool flg)
    3756: {
    3757:   PetscFunctionBegin;
    3760:   snes->lagjac_persist = flg;
    3761:   PetscFunctionReturn(PETSC_SUCCESS);
    3762: }

    3764: /*@
    3765:   SNESSetLagPreconditionerPersists - Set whether or not the preconditioner lagging persists through multiple nonlinear solves

    3767:   Logically Collective

    3769:   Input Parameters:
    3770: + snes - the `SNES` context
    3771: - flg  - preconditioner lagging persists if true

    3773:   Options Database Keys:
    3774: + -snes_lag_jacobian_persists <true,false>       - sets the persistence through multiple SNES solves
    3775: . -snes_lag_jacobian <-2,1,2,...>                - sets the lag
    3776: . -snes_lag_preconditioner_persists <true,false> - sets the persistence through multiple SNES solves
    3777: - -snes_lag_preconditioner <-2,1,2,...>          - sets the lag

    3779:   Level: developer

    3781:   Notes:
    3782:   Normally when `SNESSetLagPreconditioner()` is used, the preconditioner is always rebuilt at the beginning of each new nonlinear solve, this removes that behavior

    3784:   This is useful both for nonlinear preconditioning, where it's appropriate to have the preconditioner be stale
    3785:   by several solves, and for implicit time-stepping, where preconditioner lagging in the inner nonlinear solve over
    3786:   several timesteps may present huge efficiency gains.

    3788: .seealso: [](ch_snes), `SNES`, `SNESSetLagJacobianPersists()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESGetNPC()`, `SNESSetLagPreconditioner()`
    3789: @*/
    3790: PetscErrorCode SNESSetLagPreconditionerPersists(SNES snes, PetscBool flg)
    3791: {
    3792:   PetscFunctionBegin;
    3795:   snes->lagpre_persist = flg;
    3796:   PetscFunctionReturn(PETSC_SUCCESS);
    3797: }

    3799: /*@
    3800:   SNESSetForceIteration - force `SNESSolve()` to take at least one iteration regardless of the initial residual norm

    3802:   Logically Collective

    3804:   Input Parameters:
    3805: + snes  - the `SNES` context
    3806: - force - `PETSC_TRUE` require at least one iteration

    3808:   Options Database Key:
    3809: . -snes_force_iteration <force> - Sets forcing an iteration

    3811:   Level: intermediate

    3813:   Note:
    3814:   This is used sometimes with `TS` to prevent `TS` from detecting a false steady state solution

    3816: .seealso: [](ch_snes), `SNES`, `TS`, `SNESSetDivergenceTolerance()`
    3817: @*/
    3818: PetscErrorCode SNESSetForceIteration(SNES snes, PetscBool force)
    3819: {
    3820:   PetscFunctionBegin;
    3822:   snes->forceiteration = force;
    3823:   PetscFunctionReturn(PETSC_SUCCESS);
    3824: }

    3826: /*@
    3827:   SNESGetForceIteration - Check whether or not `SNESSolve()` take at least one iteration regardless of the initial residual norm

    3829:   Logically Collective

    3831:   Input Parameter:
    3832: . snes - the `SNES` context

    3834:   Output Parameter:
    3835: . force - `PETSC_TRUE` requires at least one iteration.

    3837:   Level: intermediate

    3839: .seealso: [](ch_snes), `SNES`, `SNESSetForceIteration()`, `SNESSetDivergenceTolerance()`
    3840: @*/
    3841: PetscErrorCode SNESGetForceIteration(SNES snes, PetscBool *force)
    3842: {
    3843:   PetscFunctionBegin;
    3845:   *force = snes->forceiteration;
    3846:   PetscFunctionReturn(PETSC_SUCCESS);
    3847: }

    3849: /*@
    3850:   SNESSetTolerances - Sets various parameters used in `SNES` convergence tests.

    3852:   Logically Collective

    3854:   Input Parameters:
    3855: + snes   - the `SNES` context
    3856: . abstol - the absolute convergence tolerance, $ F(x^n) \le abstol $
    3857: . rtol   - the relative convergence tolerance, $ F(x^n) \le reltol * F(x^0) $
    3858: . stol   - convergence tolerance in terms of the norm of the change in the solution between steps,  || delta x || < stol*|| x ||
    3859: . maxit  - the maximum number of iterations allowed in the solver, default 50.
    3860: - maxf   - the maximum number of function evaluations allowed in the solver (use `PETSC_UNLIMITED` indicates no limit), default 10,000

    3862:   Options Database Keys:
    3863: + -snes_atol <abstol>    - Sets `abstol`
    3864: . -snes_rtol <rtol>      - Sets `rtol`
    3865: . -snes_stol <stol>      - Sets `stol`
    3866: . -snes_max_it <maxit>   - Sets `maxit`
    3867: - -snes_max_funcs <maxf> - Sets `maxf` (use `unlimited` to have no maximum)

    3869:   Level: intermediate

    3871:   Note:
    3872:   All parameters must be non-negative

    3874:   Use `PETSC_CURRENT` to retain the current value of any parameter and `PETSC_DETERMINE` to use the default value for the given `SNES`.
    3875:   The default value is the value in the object when its type is set.

    3877:   Use `PETSC_UNLIMITED` on `maxit` or `maxf` to indicate there is no bound on the number of iterations or number of function evaluations.

    3879:   Fortran Note:
    3880:   Use `PETSC_CURRENT_INTEGER`, `PETSC_CURRENT_REAL`, `PETSC_UNLIMITED_INTEGER`, `PETSC_DETERMINE_INTEGER`, or `PETSC_DETERMINE_REAL`

    3882: .seealso: [](ch_snes), `SNESSolve()`, `SNES`, `SNESSetDivergenceTolerance()`, `SNESSetForceIteration()`
    3883: @*/
    3884: PetscErrorCode SNESSetTolerances(SNES snes, PetscReal abstol, PetscReal rtol, PetscReal stol, PetscInt maxit, PetscInt maxf)
    3885: {
    3886:   PetscFunctionBegin;

    3894:   if (abstol == (PetscReal)PETSC_DETERMINE) {
    3895:     snes->abstol = snes->default_abstol;
    3896:   } else if (abstol != (PetscReal)PETSC_CURRENT) {
    3897:     PetscCheck(abstol >= 0.0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Absolute tolerance %g must be non-negative", (double)abstol);
    3898:     snes->abstol = abstol;
    3899:   }

    3901:   if (rtol == (PetscReal)PETSC_DETERMINE) {
    3902:     snes->rtol = snes->default_rtol;
    3903:   } else if (rtol != (PetscReal)PETSC_CURRENT) {
    3904:     PetscCheck(rtol >= 0.0 && 1.0 > rtol, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Relative tolerance %g must be non-negative and less than 1.0", (double)rtol);
    3905:     snes->rtol = rtol;
    3906:   }

    3908:   if (stol == (PetscReal)PETSC_DETERMINE) {
    3909:     snes->stol = snes->default_stol;
    3910:   } else if (stol != (PetscReal)PETSC_CURRENT) {
    3911:     PetscCheck(stol >= 0.0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Step tolerance %g must be non-negative", (double)stol);
    3912:     snes->stol = stol;
    3913:   }

    3915:   if (maxit == (PetscInt)PETSC_DETERMINE) {
    3916:     snes->max_its = snes->default_max_its;
    3917:   } else if (maxit == (PetscInt)PETSC_UNLIMITED) {
    3918:     snes->max_its = PETSC_INT_MAX;
    3919:   } else if (maxit != PETSC_CURRENT) {
    3920:     PetscCheck(maxit >= 0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of iterations %" PetscInt_FMT " must be non-negative", maxit);
    3921:     snes->max_its = maxit;
    3922:   }

    3924:   if (maxf == (PetscInt)PETSC_DETERMINE) {
    3925:     snes->max_funcs = snes->default_max_funcs;
    3926:   } else if (maxf == (PetscInt)PETSC_UNLIMITED || maxf == -1) {
    3927:     snes->max_funcs = PETSC_UNLIMITED;
    3928:   } else if (maxf != PETSC_CURRENT) {
    3929:     PetscCheck(maxf >= 0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of function evaluations %" PetscInt_FMT " must be nonnegative", maxf);
    3930:     snes->max_funcs = maxf;
    3931:   }
    3932:   PetscFunctionReturn(PETSC_SUCCESS);
    3933: }

    3935: /*@
    3936:   SNESSetDivergenceTolerance - Sets the divergence tolerance used for the `SNES` divergence test.

    3938:   Logically Collective

    3940:   Input Parameters:
    3941: + snes   - the `SNES` context
    3942: - divtol - the divergence tolerance. Use `PETSC_UNLIMITED` to deactivate the test. If the residual norm $ F(x^n) \ge divtol * F(x^0) $ the solver
    3943:            is stopped due to divergence.

    3945:   Options Database Key:
    3946: . -snes_divergence_tolerance <divtol> - Sets `divtol`

    3948:   Level: intermediate

    3950:   Notes:
    3951:   Use `PETSC_DETERMINE` to use the default value from when the object's type was set.

    3953:   Fortran Note:
    3954:   Use ``PETSC_DETERMINE_REAL` or `PETSC_UNLIMITED_REAL`

    3956: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESSetTolerances()`, `SNESGetDivergenceTolerance()`
    3957: @*/
    3958: PetscErrorCode SNESSetDivergenceTolerance(SNES snes, PetscReal divtol)
    3959: {
    3960:   PetscFunctionBegin;

    3964:   if (divtol == (PetscReal)PETSC_DETERMINE) {
    3965:     snes->divtol = snes->default_divtol;
    3966:   } else if (divtol == (PetscReal)PETSC_UNLIMITED || divtol == -1) {
    3967:     snes->divtol = PETSC_UNLIMITED;
    3968:   } else if (divtol != (PetscReal)PETSC_CURRENT) {
    3969:     PetscCheck(divtol >= 1.0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Divergence tolerance %g must be greater than 1.0", (double)divtol);
    3970:     snes->divtol = divtol;
    3971:   }
    3972:   PetscFunctionReturn(PETSC_SUCCESS);
    3973: }

    3975: /*@
    3976:   SNESGetTolerances - Gets various parameters used in `SNES` convergence tests.

    3978:   Not Collective

    3980:   Input Parameter:
    3981: . snes - the `SNES` context

    3983:   Output Parameters:
    3984: + atol  - the absolute convergence tolerance
    3985: . rtol  - the relative convergence tolerance
    3986: . stol  - convergence tolerance in terms of the norm of the change in the solution between steps
    3987: . maxit - the maximum number of iterations allowed
    3988: - maxf  - the maximum number of function evaluations allowed, `PETSC_UNLIMITED` indicates no bound

    3990:   Level: intermediate

    3992:   Notes:
    3993:   See `SNESSetTolerances()` for details on the parameters.

    3995:   The user can specify `NULL` for any parameter that is not needed.

    3997: .seealso: [](ch_snes), `SNES`, `SNESSetTolerances()`
    3998: @*/
    3999: PetscErrorCode SNESGetTolerances(SNES snes, PetscReal *atol, PetscReal *rtol, PetscReal *stol, PetscInt *maxit, PetscInt *maxf)
    4000: {
    4001:   PetscFunctionBegin;
    4003:   if (atol) *atol = snes->abstol;
    4004:   if (rtol) *rtol = snes->rtol;
    4005:   if (stol) *stol = snes->stol;
    4006:   if (maxit) *maxit = snes->max_its;
    4007:   if (maxf) *maxf = snes->max_funcs;
    4008:   PetscFunctionReturn(PETSC_SUCCESS);
    4009: }

    4011: /*@
    4012:   SNESGetDivergenceTolerance - Gets divergence tolerance used in divergence test.

    4014:   Not Collective

    4016:   Input Parameters:
    4017: + snes   - the `SNES` context
    4018: - divtol - divergence tolerance

    4020:   Level: intermediate

    4022: .seealso: [](ch_snes), `SNES`, `SNESSetDivergenceTolerance()`
    4023: @*/
    4024: PetscErrorCode SNESGetDivergenceTolerance(SNES snes, PetscReal *divtol)
    4025: {
    4026:   PetscFunctionBegin;
    4028:   if (divtol) *divtol = snes->divtol;
    4029:   PetscFunctionReturn(PETSC_SUCCESS);
    4030: }

    4032: PETSC_INTERN PetscErrorCode SNESMonitorRange_Private(SNES, PetscInt, PetscReal *);

    4034: PetscErrorCode SNESMonitorLGRange(SNES snes, PetscInt n, PetscReal rnorm, void *monctx)
    4035: {
    4036:   PetscDrawLG      lg;
    4037:   PetscReal        x, y, per;
    4038:   PetscViewer      v = (PetscViewer)monctx;
    4039:   static PetscReal prev; /* should be in the context */
    4040:   PetscDraw        draw;

    4042:   PetscFunctionBegin;
    4044:   PetscCall(PetscViewerDrawGetDrawLG(v, 0, &lg));
    4045:   if (!n) PetscCall(PetscDrawLGReset(lg));
    4046:   PetscCall(PetscDrawLGGetDraw(lg, &draw));
    4047:   PetscCall(PetscDrawSetTitle(draw, "Residual norm"));
    4048:   x = (PetscReal)n;
    4049:   if (rnorm > 0.0) y = PetscLog10Real(rnorm);
    4050:   else y = -15.0;
    4051:   PetscCall(PetscDrawLGAddPoint(lg, &x, &y));
    4052:   if (n < 20 || !(n % 5) || snes->reason) {
    4053:     PetscCall(PetscDrawLGDraw(lg));
    4054:     PetscCall(PetscDrawLGSave(lg));
    4055:   }

    4057:   PetscCall(PetscViewerDrawGetDrawLG(v, 1, &lg));
    4058:   if (!n) PetscCall(PetscDrawLGReset(lg));
    4059:   PetscCall(PetscDrawLGGetDraw(lg, &draw));
    4060:   PetscCall(PetscDrawSetTitle(draw, "% elements > .2*max element"));
    4061:   PetscCall(SNESMonitorRange_Private(snes, n, &per));
    4062:   x = (PetscReal)n;
    4063:   y = 100.0 * per;
    4064:   PetscCall(PetscDrawLGAddPoint(lg, &x, &y));
    4065:   if (n < 20 || !(n % 5) || snes->reason) {
    4066:     PetscCall(PetscDrawLGDraw(lg));
    4067:     PetscCall(PetscDrawLGSave(lg));
    4068:   }

    4070:   PetscCall(PetscViewerDrawGetDrawLG(v, 2, &lg));
    4071:   if (!n) {
    4072:     prev = rnorm;
    4073:     PetscCall(PetscDrawLGReset(lg));
    4074:   }
    4075:   PetscCall(PetscDrawLGGetDraw(lg, &draw));
    4076:   PetscCall(PetscDrawSetTitle(draw, "(norm -oldnorm)/oldnorm"));
    4077:   x = (PetscReal)n;
    4078:   y = (prev - rnorm) / prev;
    4079:   PetscCall(PetscDrawLGAddPoint(lg, &x, &y));
    4080:   if (n < 20 || !(n % 5) || snes->reason) {
    4081:     PetscCall(PetscDrawLGDraw(lg));
    4082:     PetscCall(PetscDrawLGSave(lg));
    4083:   }

    4085:   PetscCall(PetscViewerDrawGetDrawLG(v, 3, &lg));
    4086:   if (!n) PetscCall(PetscDrawLGReset(lg));
    4087:   PetscCall(PetscDrawLGGetDraw(lg, &draw));
    4088:   PetscCall(PetscDrawSetTitle(draw, "(norm -oldnorm)/oldnorm*(% > .2 max)"));
    4089:   x = (PetscReal)n;
    4090:   y = (prev - rnorm) / (prev * per);
    4091:   if (n > 2) { /*skip initial crazy value */
    4092:     PetscCall(PetscDrawLGAddPoint(lg, &x, &y));
    4093:   }
    4094:   if (n < 20 || !(n % 5) || snes->reason) {
    4095:     PetscCall(PetscDrawLGDraw(lg));
    4096:     PetscCall(PetscDrawLGSave(lg));
    4097:   }
    4098:   prev = rnorm;
    4099:   PetscFunctionReturn(PETSC_SUCCESS);
    4100: }

    4102: /*@
    4103:   SNESConverged - Run the convergence test and update the `SNESConvergedReason`.

    4105:   Collective

    4107:   Input Parameters:
    4108: + snes  - the `SNES` context
    4109: . it    - current iteration
    4110: . xnorm - 2-norm of current iterate
    4111: . snorm - 2-norm of current step
    4112: - fnorm - 2-norm of function

    4114:   Level: developer

    4116:   Note:
    4117:   This routine is called by the `SNESSolve()` implementations.
    4118:   It does not typically need to be called by the user.

    4120: .seealso: [](ch_snes), `SNES`, `SNESSolve`, `SNESSetConvergenceTest()`, `SNESGetConvergenceTest()`
    4121: @*/
    4122: PetscErrorCode SNESConverged(SNES snes, PetscInt it, PetscReal xnorm, PetscReal snorm, PetscReal fnorm)
    4123: {
    4124:   PetscFunctionBegin;
    4125:   if (!snes->reason) {
    4126:     if (snes->normschedule == SNES_NORM_ALWAYS) PetscUseTypeMethod(snes, converged, it, xnorm, snorm, fnorm, &snes->reason, snes->cnvP);
    4127:     if (it == snes->max_its && !snes->reason) {
    4128:       if (snes->normschedule == SNES_NORM_ALWAYS) {
    4129:         PetscCall(PetscInfo(snes, "Maximum number of iterations has been reached: %" PetscInt_FMT "\n", snes->max_its));
    4130:         snes->reason = SNES_DIVERGED_MAX_IT;
    4131:       } else snes->reason = SNES_CONVERGED_ITS;
    4132:     }
    4133:   }
    4134:   PetscFunctionReturn(PETSC_SUCCESS);
    4135: }

    4137: /*@
    4138:   SNESMonitor - runs any `SNES` monitor routines provided with `SNESMonitor()` or the options database

    4140:   Collective

    4142:   Input Parameters:
    4143: + snes  - nonlinear solver context obtained from `SNESCreate()`
    4144: . iter  - current iteration number
    4145: - rnorm - current relative norm of the residual

    4147:   Level: developer

    4149:   Note:
    4150:   This routine is called by the `SNESSolve()` implementations.
    4151:   It does not typically need to be called by the user.

    4153: .seealso: [](ch_snes), `SNES`, `SNESMonitorSet()`
    4154: @*/
    4155: PetscErrorCode SNESMonitor(SNES snes, PetscInt iter, PetscReal rnorm)
    4156: {
    4157:   PetscInt i, n = snes->numbermonitors;

    4159:   PetscFunctionBegin;
    4160:   if (n > 0) SNESCheckFunctionNorm(snes, rnorm);
    4161:   PetscCall(VecLockReadPush(snes->vec_sol));
    4162:   for (i = 0; i < n; i++) PetscCall((*snes->monitor[i])(snes, iter, rnorm, snes->monitorcontext[i]));
    4163:   PetscCall(VecLockReadPop(snes->vec_sol));
    4164:   PetscFunctionReturn(PETSC_SUCCESS);
    4165: }

    4167: /* ------------ Routines to set performance monitoring options ----------- */

    4169: /*MC
    4170:     SNESMonitorFunction - functional form passed to `SNESMonitorSet()` to monitor convergence of nonlinear solver

    4172:      Synopsis:
    4173: #include <petscsnes.h>
    4174:     PetscErrorCode SNESMonitorFunction(SNES snes, PetscInt its, PetscReal norm, void *mctx)

    4176:      Collective

    4178:     Input Parameters:
    4179: +    snes - the `SNES` context
    4180: .    its - iteration number
    4181: .    norm - 2-norm function value (may be estimated)
    4182: -    mctx - [optional] monitoring context

    4184:    Level: advanced

    4186: .seealso: [](ch_snes), `SNESMonitorSet()`, `SNESMonitorSet()`, `SNESMonitorGet()`
    4187: M*/

    4189: /*@C
    4190:   SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
    4191:   iteration of the `SNES` nonlinear solver to display the iteration's
    4192:   progress.

    4194:   Logically Collective

    4196:   Input Parameters:
    4197: + snes           - the `SNES` context
    4198: . f              - the monitor function,  for the calling sequence see `SNESMonitorFunction`
    4199: . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
    4200: - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)

    4202:   Options Database Keys:
    4203: + -snes_monitor               - sets `SNESMonitorDefault()`
    4204: . -snes_monitor draw::draw_lg - sets line graph monitor,
    4205: - -snes_monitor_cancel        - cancels all monitors that have been hardwired into a code by calls to `SNESMonitorSet()`, but does not cancel those set via
    4206:                                 the options database.

    4208:   Level: intermediate

    4210:   Note:
    4211:   Several different monitoring routines may be set by calling
    4212:   `SNESMonitorSet()` multiple times; all will be called in the
    4213:   order in which they were set.

    4215:   Fortran Note:
    4216:   Only a single monitor function can be set for each `SNES` object

    4218: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESMonitorDefault()`, `SNESMonitorCancel()`, `SNESMonitorFunction`
    4219: @*/
    4220: PetscErrorCode SNESMonitorSet(SNES snes, PetscErrorCode (*f)(SNES, PetscInt, PetscReal, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
    4221: {
    4222:   PetscInt  i;
    4223:   PetscBool identical;

    4225:   PetscFunctionBegin;
    4227:   for (i = 0; i < snes->numbermonitors; i++) {
    4228:     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))f, mctx, monitordestroy, (PetscErrorCode (*)(void))snes->monitor[i], snes->monitorcontext[i], snes->monitordestroy[i], &identical));
    4229:     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
    4230:   }
    4231:   PetscCheck(snes->numbermonitors < MAXSNESMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
    4232:   snes->monitor[snes->numbermonitors]          = f;
    4233:   snes->monitordestroy[snes->numbermonitors]   = monitordestroy;
    4234:   snes->monitorcontext[snes->numbermonitors++] = (void *)mctx;
    4235:   PetscFunctionReturn(PETSC_SUCCESS);
    4236: }

    4238: /*@
    4239:   SNESMonitorCancel - Clears all the monitor functions for a `SNES` object.

    4241:   Logically Collective

    4243:   Input Parameter:
    4244: . snes - the `SNES` context

    4246:   Options Database Key:
    4247: . -snes_monitor_cancel - cancels all monitors that have been hardwired
    4248:                          into a code by calls to `SNESMonitorSet()`, but does not cancel those
    4249:                          set via the options database

    4251:   Level: intermediate

    4253:   Note:
    4254:   There is no way to clear one specific monitor from a `SNES` object.

    4256: .seealso: [](ch_snes), `SNES`, `SNESMonitorGet()`, `SNESMonitorDefault()`, `SNESMonitorSet()`
    4257: @*/
    4258: PetscErrorCode SNESMonitorCancel(SNES snes)
    4259: {
    4260:   PetscInt i;

    4262:   PetscFunctionBegin;
    4264:   for (i = 0; i < snes->numbermonitors; i++) {
    4265:     if (snes->monitordestroy[i]) PetscCall((*snes->monitordestroy[i])(&snes->monitorcontext[i]));
    4266:   }
    4267:   snes->numbermonitors = 0;
    4268:   PetscFunctionReturn(PETSC_SUCCESS);
    4269: }

    4271: /*MC
    4272:     SNESConvergenceTestFunction - functional form used for testing of convergence of nonlinear solver

    4274:      Synopsis:
    4275: #include <petscsnes.h>
    4276:      PetscErrorCode SNESConvergenceTest(SNES snes, PetscInt it, PetscReal xnorm, PetscReal gnorm, PetscReal f, SNESConvergedReason *reason, void *cctx)

    4278:      Collective

    4280:     Input Parameters:
    4281: +    snes - the `SNES` context
    4282: .    it - current iteration (0 is the first and is before any Newton step)
    4283: .    xnorm - 2-norm of current iterate
    4284: .    gnorm - 2-norm of current step
    4285: .    f - 2-norm of function
    4286: -    cctx - [optional] convergence context

    4288:     Output Parameter:
    4289: .    reason - reason for convergence/divergence, only needs to be set when convergence or divergence is detected

    4291:    Level: intermediate

    4293: .seealso: [](ch_snes), `SNES`, `SNESSolve`, `SNESSetConvergenceTest()`, `SNESGetConvergenceTest()`
    4294: M*/

    4296: /*@C
    4297:   SNESSetConvergenceTest - Sets the function that is to be used
    4298:   to test for convergence of the nonlinear iterative solution.

    4300:   Logically Collective

    4302:   Input Parameters:
    4303: + snes                        - the `SNES` context
    4304: . SNESConvergenceTestFunction - routine to test for convergence
    4305: . cctx                        - [optional] context for private data for the convergence routine  (may be `NULL`)
    4306: - destroy                     - [optional] destructor for the context (may be `NULL`; `PETSC_NULL_FUNCTION` in Fortran)

    4308:   Level: advanced

    4310: .seealso: [](ch_snes), `SNES`, `SNESConvergedDefault()`, `SNESConvergedSkip()`, `SNESConvergenceTestFunction`
    4311: @*/
    4312: PetscErrorCode SNESSetConvergenceTest(SNES snes, PetscErrorCode (*SNESConvergenceTestFunction)(SNES, PetscInt, PetscReal, PetscReal, PetscReal, SNESConvergedReason *, void *), void *cctx, PetscErrorCode (*destroy)(void *))
    4313: {
    4314:   PetscFunctionBegin;
    4316:   if (!SNESConvergenceTestFunction) SNESConvergenceTestFunction = SNESConvergedSkip;
    4317:   if (snes->ops->convergeddestroy) PetscCall((*snes->ops->convergeddestroy)(snes->cnvP));
    4318:   snes->ops->converged        = SNESConvergenceTestFunction;
    4319:   snes->ops->convergeddestroy = destroy;
    4320:   snes->cnvP                  = cctx;
    4321:   PetscFunctionReturn(PETSC_SUCCESS);
    4322: }

    4324: /*@
    4325:   SNESGetConvergedReason - Gets the reason the `SNES` iteration was stopped, which may be due to convergence, divergence, or stagnation

    4327:   Not Collective

    4329:   Input Parameter:
    4330: . snes - the `SNES` context

    4332:   Output Parameter:
    4333: . reason - negative value indicates diverged, positive value converged, see `SNESConvergedReason` for the individual convergence tests for complete lists

    4335:   Options Database Key:
    4336: . -snes_converged_reason - prints the reason to standard out

    4338:   Level: intermediate

    4340:   Note:
    4341:   Should only be called after the call the `SNESSolve()` is complete, if it is called earlier it returns the value `SNES__CONVERGED_ITERATING`.

    4343: .seealso: [](ch_snes), `SNESSolve()`, `SNESSetConvergenceTest()`, `SNESSetConvergedReason()`, `SNESConvergedReason`, `SNESGetConvergedReasonString()`
    4344: @*/
    4345: PetscErrorCode SNESGetConvergedReason(SNES snes, SNESConvergedReason *reason)
    4346: {
    4347:   PetscFunctionBegin;
    4349:   PetscAssertPointer(reason, 2);
    4350:   *reason = snes->reason;
    4351:   PetscFunctionReturn(PETSC_SUCCESS);
    4352: }

    4354: /*@C
    4355:   SNESGetConvergedReasonString - Return a human readable string for `SNESConvergedReason`

    4357:   Not Collective

    4359:   Input Parameter:
    4360: . snes - the `SNES` context

    4362:   Output Parameter:
    4363: . strreason - a human readable string that describes `SNES` converged reason

    4365:   Level: beginner

    4367: .seealso: [](ch_snes), `SNES`, `SNESGetConvergedReason()`
    4368: @*/
    4369: PetscErrorCode SNESGetConvergedReasonString(SNES snes, const char **strreason)
    4370: {
    4371:   PetscFunctionBegin;
    4373:   PetscAssertPointer(strreason, 2);
    4374:   *strreason = SNESConvergedReasons[snes->reason];
    4375:   PetscFunctionReturn(PETSC_SUCCESS);
    4376: }

    4378: /*@
    4379:   SNESSetConvergedReason - Sets the reason the `SNES` iteration was stopped.

    4381:   Not Collective

    4383:   Input Parameters:
    4384: + snes   - the `SNES` context
    4385: - reason - negative value indicates diverged, positive value converged, see `SNESConvergedReason` or the
    4386:             manual pages for the individual convergence tests for complete lists

    4388:   Level: developer

    4390:   Developer Note:
    4391:   Called inside the various `SNESSolve()` implementations

    4393: .seealso: [](ch_snes), `SNESGetConvergedReason()`, `SNESSetConvergenceTest()`, `SNESConvergedReason`
    4394: @*/
    4395: PetscErrorCode SNESSetConvergedReason(SNES snes, SNESConvergedReason reason)
    4396: {
    4397:   PetscFunctionBegin;
    4399:   PetscCheck(!snes->errorifnotconverged || reason > 0, PetscObjectComm((PetscObject)snes), PETSC_ERR_PLIB, "SNES code should have previously errored due to negative reason");
    4400:   snes->reason = reason;
    4401:   PetscFunctionReturn(PETSC_SUCCESS);
    4402: }

    4404: /*@
    4405:   SNESSetConvergenceHistory - Sets the arrays used to hold the convergence history.

    4407:   Logically Collective

    4409:   Input Parameters:
    4410: + snes  - iterative context obtained from `SNESCreate()`
    4411: . a     - array to hold history, this array will contain the function norms computed at each step
    4412: . its   - integer array holds the number of linear iterations for each solve.
    4413: . na    - size of `a` and `its`
    4414: - reset - `PETSC_TRUE` indicates each new nonlinear solve resets the history counter to zero,
    4415:           else it continues storing new values for new nonlinear solves after the old ones

    4417:   Level: intermediate

    4419:   Notes:
    4420:   If 'a' and 'its' are `NULL` then space is allocated for the history. If 'na' is `PETSC_DECIDE` then a
    4421:   default array of length 1,000 is allocated.

    4423:   This routine is useful, e.g., when running a code for purposes
    4424:   of accurate performance monitoring, when no I/O should be done
    4425:   during the section of code that is being timed.

    4427:   If the arrays run out of space after a number of iterations then the later values are not saved in the history

    4429: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESGetConvergenceHistory()`
    4430: @*/
    4431: PetscErrorCode SNESSetConvergenceHistory(SNES snes, PetscReal a[], PetscInt its[], PetscInt na, PetscBool reset)
    4432: {
    4433:   PetscFunctionBegin;
    4435:   if (a) PetscAssertPointer(a, 2);
    4436:   if (its) PetscAssertPointer(its, 3);
    4437:   if (!a) {
    4438:     if (na == PETSC_DECIDE) na = 1000;
    4439:     PetscCall(PetscCalloc2(na, &a, na, &its));
    4440:     snes->conv_hist_alloc = PETSC_TRUE;
    4441:   }
    4442:   snes->conv_hist       = a;
    4443:   snes->conv_hist_its   = its;
    4444:   snes->conv_hist_max   = (size_t)na;
    4445:   snes->conv_hist_len   = 0;
    4446:   snes->conv_hist_reset = reset;
    4447:   PetscFunctionReturn(PETSC_SUCCESS);
    4448: }

    4450: #if defined(PETSC_HAVE_MATLAB)
    4451:   #include <engine.h> /* MATLAB include file */
    4452:   #include <mex.h>    /* MATLAB include file */

    4454: PETSC_EXTERN mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
    4455: {
    4456:   mxArray   *mat;
    4457:   PetscInt   i;
    4458:   PetscReal *ar;

    4460:   mat = mxCreateDoubleMatrix(snes->conv_hist_len, 1, mxREAL);
    4461:   ar  = (PetscReal *)mxGetData(mat);
    4462:   for (i = 0; i < snes->conv_hist_len; i++) ar[i] = snes->conv_hist[i];
    4463:   return mat;
    4464: }
    4465: #endif

    4467: /*@C
    4468:   SNESGetConvergenceHistory - Gets the arrays used to hold the convergence history.

    4470:   Not Collective

    4472:   Input Parameter:
    4473: . snes - iterative context obtained from `SNESCreate()`

    4475:   Output Parameters:
    4476: + a   - array to hold history, usually was set with `SNESSetConvergenceHistory()`
    4477: . its - integer array holds the number of linear iterations (or
    4478:          negative if not converged) for each solve.
    4479: - na  - size of `a` and `its`

    4481:   Level: intermediate

    4483:   Note:
    4484:   This routine is useful, e.g., when running a code for purposes
    4485:   of accurate performance monitoring, when no I/O should be done
    4486:   during the section of code that is being timed.

    4488:   Fortran Note:
    4489:   The calling sequence for this routine in Fortran is
    4490: .vb
    4491:     call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
    4492: .ve

    4494: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESSetConvergenceHistory()`
    4495: @*/
    4496: PetscErrorCode SNESGetConvergenceHistory(SNES snes, PetscReal *a[], PetscInt *its[], PetscInt *na)
    4497: {
    4498:   PetscFunctionBegin;
    4500:   if (a) *a = snes->conv_hist;
    4501:   if (its) *its = snes->conv_hist_its;
    4502:   if (na) *na = (PetscInt)snes->conv_hist_len;
    4503:   PetscFunctionReturn(PETSC_SUCCESS);
    4504: }

    4506: /*@C
    4507:   SNESSetUpdate - Sets the general-purpose update function called
    4508:   at the beginning of every iteration of the nonlinear solve. Specifically
    4509:   it is called just before the Jacobian is "evaluated" and after the function
    4510:   evaluation.

    4512:   Logically Collective

    4514:   Input Parameters:
    4515: + snes - The nonlinear solver context
    4516: - func - The update function; for calling sequence see `SNESUpdateFn`

    4518:   Level: advanced

    4520:   Notes:
    4521:   This is NOT what one uses to update the ghost points before a function evaluation, that should be done at the beginning of your function provided
    4522:   to `SNESSetFunction()`, or `SNESSetPicard()`
    4523:   This is not used by most users, and it is intended to provide a general hook that is run
    4524:   right before the direction step is computed.
    4525:   Users are free to modify the current residual vector,
    4526:   the current linearization point, or any other vector associated to the specific solver used.
    4527:   If such modifications take place, it is the user responsibility to update all the relevant
    4528:   vectors.

    4530:   There are a variety of function hooks one many set that are called at different stages of the nonlinear solution process, see the functions listed below.

    4532: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESSetJacobian()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()`, `SNESNewtonTRSetPreCheck()`, `SNESNewtonTRSetPostCheck()`,
    4533:          `SNESMonitorSet()`
    4534: @*/
    4535: PetscErrorCode SNESSetUpdate(SNES snes, SNESUpdateFn *func)
    4536: {
    4537:   PetscFunctionBegin;
    4539:   snes->ops->update = func;
    4540:   PetscFunctionReturn(PETSC_SUCCESS);
    4541: }

    4543: /*@
    4544:   SNESConvergedReasonView - Displays the reason a `SNES` solve converged or diverged to a viewer

    4546:   Collective

    4548:   Input Parameters:
    4549: + snes   - iterative context obtained from `SNESCreate()`
    4550: - viewer - the viewer to display the reason

    4552:   Options Database Keys:
    4553: + -snes_converged_reason          - print reason for converged or diverged, also prints number of iterations
    4554: - -snes_converged_reason ::failed - only print reason and number of iterations when diverged

    4556:   Level: beginner

    4558:   Note:
    4559:   To change the format of the output call `PetscViewerPushFormat`(viewer,format) before this call. Use `PETSC_VIEWER_DEFAULT` for the default,
    4560:   use `PETSC_VIEWER_FAILED` to only display a reason if it fails.

    4562: .seealso: [](ch_snes), `SNESConvergedReason`, `PetscViewer`, `SNES`,
    4563:           `SNESCreate()`, `SNESSetUp()`, `SNESDestroy()`, `SNESSetTolerances()`, `SNESConvergedDefault()`, `SNESGetConvergedReason()`,
    4564:           `SNESConvergedReasonViewFromOptions()`,
    4565:           `PetscViewerPushFormat()`, `PetscViewerPopFormat()`
    4566: @*/
    4567: PetscErrorCode SNESConvergedReasonView(SNES snes, PetscViewer viewer)
    4568: {
    4569:   PetscViewerFormat format;
    4570:   PetscBool         isAscii;

    4572:   PetscFunctionBegin;
    4573:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
    4574:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isAscii));
    4575:   if (isAscii) {
    4576:     PetscCall(PetscViewerGetFormat(viewer, &format));
    4577:     PetscCall(PetscViewerASCIIAddTab(viewer, ((PetscObject)snes)->tablevel + 1));
    4578:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
    4579:       DM       dm;
    4580:       Vec      u;
    4581:       PetscDS  prob;
    4582:       PetscInt Nf, f;
    4583:       PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
    4584:       void    **exactCtx;
    4585:       PetscReal error;

    4587:       PetscCall(SNESGetDM(snes, &dm));
    4588:       PetscCall(SNESGetSolution(snes, &u));
    4589:       PetscCall(DMGetDS(dm, &prob));
    4590:       PetscCall(PetscDSGetNumFields(prob, &Nf));
    4591:       PetscCall(PetscMalloc2(Nf, &exactSol, Nf, &exactCtx));
    4592:       for (f = 0; f < Nf; ++f) PetscCall(PetscDSGetExactSolution(prob, f, &exactSol[f], &exactCtx[f]));
    4593:       PetscCall(DMComputeL2Diff(dm, 0.0, exactSol, exactCtx, u, &error));
    4594:       PetscCall(PetscFree2(exactSol, exactCtx));
    4595:       if (error < 1.0e-11) PetscCall(PetscViewerASCIIPrintf(viewer, "L_2 Error: < 1.0e-11\n"));
    4596:       else PetscCall(PetscViewerASCIIPrintf(viewer, "L_2 Error: %g\n", (double)error));
    4597:     }
    4598:     if (snes->reason > 0 && format != PETSC_VIEWER_FAILED) {
    4599:       if (((PetscObject)snes)->prefix) {
    4600:         PetscCall(PetscViewerASCIIPrintf(viewer, "Nonlinear %s solve converged due to %s iterations %" PetscInt_FMT "\n", ((PetscObject)snes)->prefix, SNESConvergedReasons[snes->reason], snes->iter));
    4601:       } else {
    4602:         PetscCall(PetscViewerASCIIPrintf(viewer, "Nonlinear solve converged due to %s iterations %" PetscInt_FMT "\n", SNESConvergedReasons[snes->reason], snes->iter));
    4603:       }
    4604:     } else if (snes->reason <= 0) {
    4605:       if (((PetscObject)snes)->prefix) {
    4606:         PetscCall(PetscViewerASCIIPrintf(viewer, "Nonlinear %s solve did not converge due to %s iterations %" PetscInt_FMT "\n", ((PetscObject)snes)->prefix, SNESConvergedReasons[snes->reason], snes->iter));
    4607:       } else {
    4608:         PetscCall(PetscViewerASCIIPrintf(viewer, "Nonlinear solve did not converge due to %s iterations %" PetscInt_FMT "\n", SNESConvergedReasons[snes->reason], snes->iter));
    4609:       }
    4610:     }
    4611:     PetscCall(PetscViewerASCIISubtractTab(viewer, ((PetscObject)snes)->tablevel + 1));
    4612:   }
    4613:   PetscFunctionReturn(PETSC_SUCCESS);
    4614: }

    4616: /*@C
    4617:   SNESConvergedReasonViewSet - Sets an ADDITIONAL function that is to be used at the
    4618:   end of the nonlinear solver to display the convergence reason of the nonlinear solver.

    4620:   Logically Collective

    4622:   Input Parameters:
    4623: + snes              - the `SNES` context
    4624: . f                 - the `SNESConvergedReason` view function
    4625: . vctx              - [optional] user-defined context for private data for the `SNESConvergedReason` view function (use `NULL` if no context is desired)
    4626: - reasonviewdestroy - [optional] routine that frees the context (may be `NULL`)

    4628:   Calling sequence of `f`:
    4629: + snes - the `SNES` context
    4630: - vctx - [optional] user-defined context for private data for the function

    4632:   Calling sequence of `reasonviewerdestroy`:
    4633: . vctx - [optional] user-defined context for private data for the function

    4635:   Options Database Keys:
    4636: + -snes_converged_reason             - sets a default `SNESConvergedReasonView()`
    4637: - -snes_converged_reason_view_cancel - cancels all converged reason viewers that have been hardwired into a code by
    4638:                                        calls to `SNESConvergedReasonViewSet()`, but does not cancel those set via the options database.

    4640:   Level: intermediate

    4642:   Note:
    4643:   Several different converged reason view routines may be set by calling
    4644:   `SNESConvergedReasonViewSet()` multiple times; all will be called in the
    4645:   order in which they were set.

    4647: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESConvergedReason`, `SNESGetConvergedReason()`, `SNESConvergedReasonView()`, `SNESConvergedReasonViewCancel()`
    4648: @*/
    4649: PetscErrorCode SNESConvergedReasonViewSet(SNES snes, PetscErrorCode (*f)(SNES snes, void *vctx), void *vctx, PetscErrorCode (*reasonviewdestroy)(void **vctx))
    4650: {
    4651:   PetscInt  i;
    4652:   PetscBool identical;

    4654:   PetscFunctionBegin;
    4656:   for (i = 0; i < snes->numberreasonviews; i++) {
    4657:     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))f, vctx, reasonviewdestroy, (PetscErrorCode (*)(void))snes->reasonview[i], snes->reasonviewcontext[i], snes->reasonviewdestroy[i], &identical));
    4658:     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
    4659:   }
    4660:   PetscCheck(snes->numberreasonviews < MAXSNESREASONVIEWS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many SNES reasonview set");
    4661:   snes->reasonview[snes->numberreasonviews]          = f;
    4662:   snes->reasonviewdestroy[snes->numberreasonviews]   = reasonviewdestroy;
    4663:   snes->reasonviewcontext[snes->numberreasonviews++] = (void *)vctx;
    4664:   PetscFunctionReturn(PETSC_SUCCESS);
    4665: }

    4667: /*@
    4668:   SNESConvergedReasonViewFromOptions - Processes command line options to determine if/how a `SNESConvergedReason` is to be viewed at the end of `SNESSolve()`
    4669:   All the user-provided viewer routines set with `SNESConvergedReasonViewSet()` will be called, if they exist.

    4671:   Collective

    4673:   Input Parameter:
    4674: . snes - the `SNES` object

    4676:   Level: advanced

    4678: .seealso: [](ch_snes), `SNES`, `SNESConvergedReason`, `SNESConvergedReasonViewSet()`, `SNESCreate()`, `SNESSetUp()`, `SNESDestroy()`,
    4679:           `SNESSetTolerances()`, `SNESConvergedDefault()`, `SNESGetConvergedReason()`, `SNESConvergedReasonView()`
    4680: @*/
    4681: PetscErrorCode SNESConvergedReasonViewFromOptions(SNES snes)
    4682: {
    4683:   static PetscBool incall = PETSC_FALSE;

    4685:   PetscFunctionBegin;
    4686:   if (incall) PetscFunctionReturn(PETSC_SUCCESS);
    4687:   incall = PETSC_TRUE;

    4689:   /* All user-provided viewers are called first, if they exist. */
    4690:   for (PetscInt i = 0; i < snes->numberreasonviews; i++) PetscCall((*snes->reasonview[i])(snes, snes->reasonviewcontext[i]));

    4692:   /* Call PETSc default routine if users ask for it */
    4693:   if (snes->convergedreasonviewer) {
    4694:     PetscCall(PetscViewerPushFormat(snes->convergedreasonviewer, snes->convergedreasonformat));
    4695:     PetscCall(SNESConvergedReasonView(snes, snes->convergedreasonviewer));
    4696:     PetscCall(PetscViewerPopFormat(snes->convergedreasonviewer));
    4697:   }
    4698:   incall = PETSC_FALSE;
    4699:   PetscFunctionReturn(PETSC_SUCCESS);
    4700: }

    4702: /*@
    4703:   SNESSolve - Solves a nonlinear system $F(x) = b $ associated with a `SNES` object

    4705:   Collective

    4707:   Input Parameters:
    4708: + snes - the `SNES` context
    4709: . b    - the constant part of the equation $F(x) = b$, or `NULL` to use zero.
    4710: - x    - the solution vector.

    4712:   Level: beginner

    4714:   Note:
    4715:   The user should initialize the vector, `x`, with the initial guess
    4716:   for the nonlinear solve prior to calling `SNESSolve()` .

    4718: .seealso: [](ch_snes), `SNES`, `SNESCreate()`, `SNESDestroy()`, `SNESSetFunction()`, `SNESSetJacobian()`, `SNESSetGridSequence()`, `SNESGetSolution()`,
    4719:           `SNESNewtonTRSetPreCheck()`, `SNESNewtonTRGetPreCheck()`, `SNESNewtonTRSetPostCheck()`, `SNESNewtonTRGetPostCheck()`,
    4720:           `SNESLineSearchSetPostCheck()`, `SNESLineSearchGetPostCheck()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchGetPreCheck()`
    4721: @*/
    4722: PetscErrorCode SNESSolve(SNES snes, Vec b, Vec x)
    4723: {
    4724:   PetscBool flg;
    4725:   PetscInt  grid;
    4726:   Vec       xcreated = NULL;
    4727:   DM        dm;

    4729:   PetscFunctionBegin;
    4732:   if (x) PetscCheckSameComm(snes, 1, x, 3);
    4734:   if (b) PetscCheckSameComm(snes, 1, b, 2);

    4736:   /* High level operations using the nonlinear solver */
    4737:   {
    4738:     PetscViewer       viewer;
    4739:     PetscViewerFormat format;
    4740:     PetscInt          num;
    4741:     PetscBool         flg;
    4742:     static PetscBool  incall = PETSC_FALSE;

    4744:     if (!incall) {
    4745:       /* Estimate the convergence rate of the discretization */
    4746:       PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_convergence_estimate", &viewer, &format, &flg));
    4747:       if (flg) {
    4748:         PetscConvEst conv;
    4749:         DM           dm;
    4750:         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
    4751:         PetscInt     Nf;

    4753:         incall = PETSC_TRUE;
    4754:         PetscCall(SNESGetDM(snes, &dm));
    4755:         PetscCall(DMGetNumFields(dm, &Nf));
    4756:         PetscCall(PetscCalloc1(Nf, &alpha));
    4757:         PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject)snes), &conv));
    4758:         PetscCall(PetscConvEstSetSolver(conv, (PetscObject)snes));
    4759:         PetscCall(PetscConvEstSetFromOptions(conv));
    4760:         PetscCall(PetscConvEstSetUp(conv));
    4761:         PetscCall(PetscConvEstGetConvRate(conv, alpha));
    4762:         PetscCall(PetscViewerPushFormat(viewer, format));
    4763:         PetscCall(PetscConvEstRateView(conv, alpha, viewer));
    4764:         PetscCall(PetscViewerPopFormat(viewer));
    4765:         PetscCall(PetscViewerDestroy(&viewer));
    4766:         PetscCall(PetscConvEstDestroy(&conv));
    4767:         PetscCall(PetscFree(alpha));
    4768:         incall = PETSC_FALSE;
    4769:       }
    4770:       /* Adaptively refine the initial grid */
    4771:       num = 1;
    4772:       PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)snes)->prefix, "-snes_adapt_initial", &num, &flg));
    4773:       if (flg) {
    4774:         DMAdaptor adaptor;

    4776:         incall = PETSC_TRUE;
    4777:         PetscCall(DMAdaptorCreate(PetscObjectComm((PetscObject)snes), &adaptor));
    4778:         PetscCall(DMAdaptorSetSolver(adaptor, snes));
    4779:         PetscCall(DMAdaptorSetSequenceLength(adaptor, num));
    4780:         PetscCall(DMAdaptorSetFromOptions(adaptor));
    4781:         PetscCall(DMAdaptorSetUp(adaptor));
    4782:         PetscCall(DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_INITIAL, &dm, &x));
    4783:         PetscCall(DMAdaptorDestroy(&adaptor));
    4784:         incall = PETSC_FALSE;
    4785:       }
    4786:       /* Use grid sequencing to adapt */
    4787:       num = 0;
    4788:       PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)snes)->prefix, "-snes_adapt_sequence", &num, NULL));
    4789:       if (num) {
    4790:         DMAdaptor   adaptor;
    4791:         const char *prefix;

    4793:         incall = PETSC_TRUE;
    4794:         PetscCall(DMAdaptorCreate(PetscObjectComm((PetscObject)snes), &adaptor));
    4795:         PetscCall(SNESGetOptionsPrefix(snes, &prefix));
    4796:         PetscCall(DMAdaptorSetOptionsPrefix(adaptor, prefix));
    4797:         PetscCall(DMAdaptorSetSolver(adaptor, snes));
    4798:         PetscCall(DMAdaptorSetSequenceLength(adaptor, num));
    4799:         PetscCall(DMAdaptorSetFromOptions(adaptor));
    4800:         PetscCall(DMAdaptorSetUp(adaptor));
    4801:         PetscCall(PetscObjectViewFromOptions((PetscObject)adaptor, NULL, "-snes_adapt_view"));
    4802:         PetscCall(DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_SEQUENTIAL, &dm, &x));
    4803:         PetscCall(DMAdaptorDestroy(&adaptor));
    4804:         incall = PETSC_FALSE;
    4805:       }
    4806:     }
    4807:   }
    4808:   if (!x) x = snes->vec_sol;
    4809:   if (!x) {
    4810:     PetscCall(SNESGetDM(snes, &dm));
    4811:     PetscCall(DMCreateGlobalVector(dm, &xcreated));
    4812:     x = xcreated;
    4813:   }
    4814:   PetscCall(SNESViewFromOptions(snes, NULL, "-snes_view_pre"));

    4816:   for (grid = 0; grid < snes->gridsequence; grid++) PetscCall(PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes))));
    4817:   for (grid = 0; grid < snes->gridsequence + 1; grid++) {
    4818:     /* set solution vector */
    4819:     if (!grid) PetscCall(PetscObjectReference((PetscObject)x));
    4820:     PetscCall(VecDestroy(&snes->vec_sol));
    4821:     snes->vec_sol = x;
    4822:     PetscCall(SNESGetDM(snes, &dm));

    4824:     /* set affine vector if provided */
    4825:     if (b) PetscCall(PetscObjectReference((PetscObject)b));
    4826:     PetscCall(VecDestroy(&snes->vec_rhs));
    4827:     snes->vec_rhs = b;

    4829:     if (snes->vec_rhs) PetscCheck(snes->vec_func != snes->vec_rhs, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "Right hand side vector cannot be function vector");
    4830:     PetscCheck(snes->vec_func != snes->vec_sol, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "Solution vector cannot be function vector");
    4831:     PetscCheck(snes->vec_rhs != snes->vec_sol, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "Solution vector cannot be right-hand side vector");
    4832:     if (!snes->vec_sol_update /* && snes->vec_sol */) PetscCall(VecDuplicate(snes->vec_sol, &snes->vec_sol_update));
    4833:     PetscCall(DMShellSetGlobalVector(dm, snes->vec_sol));
    4834:     PetscCall(SNESSetUp(snes));

    4836:     if (!grid) {
    4837:       if (snes->ops->computeinitialguess) PetscCallBack("SNES callback compute initial guess", (*snes->ops->computeinitialguess)(snes, snes->vec_sol, snes->initialguessP));
    4838:     }

    4840:     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
    4841:     PetscCall(SNESResetCounters(snes));
    4842:     snes->reason = SNES_CONVERGED_ITERATING;
    4843:     PetscCall(PetscLogEventBegin(SNES_Solve, snes, 0, 0, 0));
    4844:     PetscUseTypeMethod(snes, solve);
    4845:     PetscCall(PetscLogEventEnd(SNES_Solve, snes, 0, 0, 0));
    4846:     PetscCheck(snes->reason, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Internal error, solver %s returned without setting converged reason", ((PetscObject)snes)->type_name);
    4847:     snes->domainerror = PETSC_FALSE; /* clear the flag if it has been set */

    4849:     if (snes->lagjac_persist) snes->jac_iter += snes->iter;
    4850:     if (snes->lagpre_persist) snes->pre_iter += snes->iter;

    4852:     PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_test_local_min", NULL, NULL, &flg));
    4853:     if (flg && !PetscPreLoadingOn) PetscCall(SNESTestLocalMin(snes));
    4854:     /* Call converged reason views. This may involve user-provided viewers as well */
    4855:     PetscCall(SNESConvergedReasonViewFromOptions(snes));

    4857:     if (snes->errorifnotconverged) PetscCheck(snes->reason >= 0, PetscObjectComm((PetscObject)snes), PETSC_ERR_NOT_CONVERGED, "SNESSolve has not converged");
    4858:     if (snes->reason < 0) break;
    4859:     if (grid < snes->gridsequence) {
    4860:       DM  fine;
    4861:       Vec xnew;
    4862:       Mat interp;

    4864:       PetscCall(DMRefine(snes->dm, PetscObjectComm((PetscObject)snes), &fine));
    4865:       PetscCheck(fine, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_INCOMP, "DMRefine() did not perform any refinement, cannot continue grid sequencing");
    4866:       PetscCall(DMCreateInterpolation(snes->dm, fine, &interp, NULL));
    4867:       PetscCall(DMCreateGlobalVector(fine, &xnew));
    4868:       PetscCall(MatInterpolate(interp, x, xnew));
    4869:       PetscCall(DMInterpolate(snes->dm, interp, fine));
    4870:       PetscCall(MatDestroy(&interp));
    4871:       x = xnew;

    4873:       PetscCall(SNESReset(snes));
    4874:       PetscCall(SNESSetDM(snes, fine));
    4875:       PetscCall(SNESResetFromOptions(snes));
    4876:       PetscCall(DMDestroy(&fine));
    4877:       PetscCall(PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes))));
    4878:     }
    4879:   }
    4880:   PetscCall(SNESViewFromOptions(snes, NULL, "-snes_view"));
    4881:   PetscCall(VecViewFromOptions(snes->vec_sol, (PetscObject)snes, "-snes_view_solution"));
    4882:   PetscCall(DMMonitor(snes->dm));
    4883:   PetscCall(SNESMonitorPauseFinal_Internal(snes));

    4885:   PetscCall(VecDestroy(&xcreated));
    4886:   PetscCall(PetscObjectSAWsBlock((PetscObject)snes));
    4887:   PetscFunctionReturn(PETSC_SUCCESS);
    4888: }

    4890: /* --------- Internal routines for SNES Package --------- */

    4892: /*@
    4893:   SNESSetType - Sets the algorithm/method to be used to solve the nonlinear system with the given `SNES`

    4895:   Collective

    4897:   Input Parameters:
    4898: + snes - the `SNES` context
    4899: - type - a known method

    4901:   Options Database Key:
    4902: . -snes_type <type> - Sets the method; use -help for a list
    4903:    of available methods (for instance, newtonls or newtontr)

    4905:   Level: intermediate

    4907:   Notes:
    4908:   See `SNESType` for available methods (for instance)
    4909: +    `SNESNEWTONLS` - Newton's method with line search
    4910:   (systems of nonlinear equations)
    4911: -    `SNESNEWTONTR` - Newton's method with trust region
    4912:   (systems of nonlinear equations)

    4914:   Normally, it is best to use the `SNESSetFromOptions()` command and then
    4915:   set the `SNES` solver type from the options database rather than by using
    4916:   this routine.  Using the options database provides the user with
    4917:   maximum flexibility in evaluating the many nonlinear solvers.
    4918:   The `SNESSetType()` routine is provided for those situations where it
    4919:   is necessary to set the nonlinear solver independently of the command
    4920:   line or options database.  This might be the case, for example, when
    4921:   the choice of solver changes during the execution of the program,
    4922:   and the user's application is taking responsibility for choosing the
    4923:   appropriate method.

    4925:   Developer Note:
    4926:   `SNESRegister()` adds a constructor for a new `SNESType` to `SNESList`, `SNESSetType()` locates
    4927:   the constructor in that list and calls it to create the specific object.

    4929: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESType`, `SNESCreate()`, `SNESDestroy()`, `SNESGetType()`, `SNESSetFromOptions()`
    4930: @*/
    4931: PetscErrorCode SNESSetType(SNES snes, SNESType type)
    4932: {
    4933:   PetscBool match;
    4934:   PetscErrorCode (*r)(SNES);

    4936:   PetscFunctionBegin;
    4938:   PetscAssertPointer(type, 2);

    4940:   PetscCall(PetscObjectTypeCompare((PetscObject)snes, type, &match));
    4941:   if (match) PetscFunctionReturn(PETSC_SUCCESS);

    4943:   PetscCall(PetscFunctionListFind(SNESList, type, &r));
    4944:   PetscCheck(r, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested SNES type %s", type);
    4945:   /* Destroy the previous private SNES context */
    4946:   PetscTryTypeMethod(snes, destroy);
    4947:   /* Reinitialize function pointers in SNESOps structure */
    4948:   snes->ops->setup          = NULL;
    4949:   snes->ops->solve          = NULL;
    4950:   snes->ops->view           = NULL;
    4951:   snes->ops->setfromoptions = NULL;
    4952:   snes->ops->destroy        = NULL;

    4954:   /* It may happen the user has customized the line search before calling SNESSetType */
    4955:   if (((PetscObject)snes)->type_name) PetscCall(SNESLineSearchDestroy(&snes->linesearch));

    4957:   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
    4958:   snes->setupcalled = PETSC_FALSE;

    4960:   PetscCall(PetscObjectChangeTypeName((PetscObject)snes, type));
    4961:   PetscCall((*r)(snes));
    4962:   PetscFunctionReturn(PETSC_SUCCESS);
    4963: }

    4965: /*@
    4966:   SNESGetType - Gets the `SNES` method type and name (as a string).

    4968:   Not Collective

    4970:   Input Parameter:
    4971: . snes - nonlinear solver context

    4973:   Output Parameter:
    4974: . type - `SNES` method (a character string)

    4976:   Level: intermediate

    4978: .seealso: [](ch_snes), `SNESSetType()`, `SNESType`, `SNESSetFromOptions()`, `SNES`
    4979: @*/
    4980: PetscErrorCode SNESGetType(SNES snes, SNESType *type)
    4981: {
    4982:   PetscFunctionBegin;
    4984:   PetscAssertPointer(type, 2);
    4985:   *type = ((PetscObject)snes)->type_name;
    4986:   PetscFunctionReturn(PETSC_SUCCESS);
    4987: }

    4989: /*@
    4990:   SNESSetSolution - Sets the solution vector for use by the `SNES` routines.

    4992:   Logically Collective

    4994:   Input Parameters:
    4995: + snes - the `SNES` context obtained from `SNESCreate()`
    4996: - u    - the solution vector

    4998:   Level: beginner

    5000: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESGetSolution()`, `Vec`
    5001: @*/
    5002: PetscErrorCode SNESSetSolution(SNES snes, Vec u)
    5003: {
    5004:   DM dm;

    5006:   PetscFunctionBegin;
    5009:   PetscCall(PetscObjectReference((PetscObject)u));
    5010:   PetscCall(VecDestroy(&snes->vec_sol));

    5012:   snes->vec_sol = u;

    5014:   PetscCall(SNESGetDM(snes, &dm));
    5015:   PetscCall(DMShellSetGlobalVector(dm, u));
    5016:   PetscFunctionReturn(PETSC_SUCCESS);
    5017: }

    5019: /*@
    5020:   SNESGetSolution - Returns the vector where the approximate solution is
    5021:   stored. This is the fine grid solution when using `SNESSetGridSequence()`.

    5023:   Not Collective, but `x` is parallel if `snes` is parallel

    5025:   Input Parameter:
    5026: . snes - the `SNES` context

    5028:   Output Parameter:
    5029: . x - the solution

    5031:   Level: intermediate

    5033: .seealso: [](ch_snes), `SNESSetSolution()`, `SNESSolve()`, `SNES`, `SNESGetSolutionUpdate()`, `SNESGetFunction()`
    5034: @*/
    5035: PetscErrorCode SNESGetSolution(SNES snes, Vec *x)
    5036: {
    5037:   PetscFunctionBegin;
    5039:   PetscAssertPointer(x, 2);
    5040:   *x = snes->vec_sol;
    5041:   PetscFunctionReturn(PETSC_SUCCESS);
    5042: }

    5044: /*@
    5045:   SNESGetSolutionUpdate - Returns the vector where the solution update is
    5046:   stored.

    5048:   Not Collective, but `x` is parallel if `snes` is parallel

    5050:   Input Parameter:
    5051: . snes - the `SNES` context

    5053:   Output Parameter:
    5054: . x - the solution update

    5056:   Level: advanced

    5058: .seealso: [](ch_snes), `SNES`, `SNESGetSolution()`, `SNESGetFunction()`
    5059: @*/
    5060: PetscErrorCode SNESGetSolutionUpdate(SNES snes, Vec *x)
    5061: {
    5062:   PetscFunctionBegin;
    5064:   PetscAssertPointer(x, 2);
    5065:   *x = snes->vec_sol_update;
    5066:   PetscFunctionReturn(PETSC_SUCCESS);
    5067: }

    5069: /*@C
    5070:   SNESGetFunction - Returns the function that defines the nonlinear system set with `SNESSetFunction()`

    5072:   Not Collective, but `r` is parallel if `snes` is parallel. Collective if `r` is requested, but has not been created yet.

    5074:   Input Parameter:
    5075: . snes - the `SNES` context

    5077:   Output Parameters:
    5078: + r   - the vector that is used to store residuals (or `NULL` if you don't want it)
    5079: . f   - the function (or `NULL` if you don't want it);  for calling sequence see `SNESFunctionFn`
    5080: - ctx - the function context (or `NULL` if you don't want it)

    5082:   Level: advanced

    5084:   Note:
    5085:   The vector `r` DOES NOT, in general, contain the current value of the `SNES` nonlinear function

    5087: .seealso: [](ch_snes), `SNES`, `SNESSolve()`, `SNESSetFunction()`, `SNESGetSolution()`, `SNESFunctionFn`
    5088: @*/
    5089: PetscErrorCode SNESGetFunction(SNES snes, Vec *r, SNESFunctionFn **f, void **ctx)
    5090: {
    5091:   DM dm;

    5093:   PetscFunctionBegin;
    5095:   if (r) {
    5096:     if (!snes->vec_func) {
    5097:       if (snes->vec_rhs) {
    5098:         PetscCall(VecDuplicate(snes->vec_rhs, &snes->vec_func));
    5099:       } else if (snes->vec_sol) {
    5100:         PetscCall(VecDuplicate(snes->vec_sol, &snes->vec_func));
    5101:       } else if (snes->dm) {
    5102:         PetscCall(DMCreateGlobalVector(snes->dm, &snes->vec_func));
    5103:       }
    5104:     }
    5105:     *r = snes->vec_func;
    5106:   }
    5107:   PetscCall(SNESGetDM(snes, &dm));
    5108:   PetscCall(DMSNESGetFunction(dm, f, ctx));
    5109:   PetscFunctionReturn(PETSC_SUCCESS);
    5110: }

    5112: /*@C
    5113:   SNESGetNGS - Returns the function and context set with `SNESSetNGS()`

    5115:   Input Parameter:
    5116: . snes - the `SNES` context

    5118:   Output Parameters:
    5119: + f   - the function (or `NULL`) see `SNESNGSFn` for calling sequence
    5120: - ctx - the function context (or `NULL`)

    5122:   Level: advanced

    5124: .seealso: [](ch_snes), `SNESSetNGS()`, `SNESGetFunction()`, `SNESNGSFn`
    5125: @*/
    5126: PetscErrorCode SNESGetNGS(SNES snes, SNESNGSFn **f, void **ctx)
    5127: {
    5128:   DM dm;

    5130:   PetscFunctionBegin;
    5132:   PetscCall(SNESGetDM(snes, &dm));
    5133:   PetscCall(DMSNESGetNGS(dm, f, ctx));
    5134:   PetscFunctionReturn(PETSC_SUCCESS);
    5135: }

    5137: /*@
    5138:   SNESSetOptionsPrefix - Sets the prefix used for searching for all
    5139:   `SNES` options in the database.

    5141:   Logically Collective

    5143:   Input Parameters:
    5144: + snes   - the `SNES` context
    5145: - prefix - the prefix to prepend to all option names

    5147:   Level: advanced

    5149:   Note:
    5150:   A hyphen (-) must NOT be given at the beginning of the prefix name.
    5151:   The first character of all runtime options is AUTOMATICALLY the hyphen.

    5153: .seealso: [](ch_snes), `SNES`, `SNESSetFromOptions()`, `SNESAppendOptionsPrefix()`
    5154: @*/
    5155: PetscErrorCode SNESSetOptionsPrefix(SNES snes, const char prefix[])
    5156: {
    5157:   PetscFunctionBegin;
    5159:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)snes, prefix));
    5160:   if (!snes->ksp) PetscCall(SNESGetKSP(snes, &snes->ksp));
    5161:   if (snes->linesearch) {
    5162:     PetscCall(SNESGetLineSearch(snes, &snes->linesearch));
    5163:     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)snes->linesearch, prefix));
    5164:   }
    5165:   PetscCall(KSPSetOptionsPrefix(snes->ksp, prefix));
    5166:   PetscFunctionReturn(PETSC_SUCCESS);
    5167: }

    5169: /*@
    5170:   SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
    5171:   `SNES` options in the database.

    5173:   Logically Collective

    5175:   Input Parameters:
    5176: + snes   - the `SNES` context
    5177: - prefix - the prefix to prepend to all option names

    5179:   Level: advanced

    5181:   Note:
    5182:   A hyphen (-) must NOT be given at the beginning of the prefix name.
    5183:   The first character of all runtime options is AUTOMATICALLY the hyphen.

    5185: .seealso: [](ch_snes), `SNESGetOptionsPrefix()`, `SNESSetOptionsPrefix()`
    5186: @*/
    5187: PetscErrorCode SNESAppendOptionsPrefix(SNES snes, const char prefix[])
    5188: {
    5189:   PetscFunctionBegin;
    5191:   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix));
    5192:   if (!snes->ksp) PetscCall(SNESGetKSP(snes, &snes->ksp));
    5193:   if (snes->linesearch) {
    5194:     PetscCall(SNESGetLineSearch(snes, &snes->linesearch));
    5195:     PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)snes->linesearch, prefix));
    5196:   }
    5197:   PetscCall(KSPAppendOptionsPrefix(snes->ksp, prefix));
    5198:   PetscFunctionReturn(PETSC_SUCCESS);
    5199: }

    5201: /*@
    5202:   SNESGetOptionsPrefix - Gets the prefix used for searching for all
    5203:   `SNES` options in the database.

    5205:   Not Collective

    5207:   Input Parameter:
    5208: . snes - the `SNES` context

    5210:   Output Parameter:
    5211: . prefix - pointer to the prefix string used

    5213:   Level: advanced

    5215:   Fortran Note:
    5216:   The user should pass in a string 'prefix' of
    5217:   sufficient length to hold the prefix.

    5219: .seealso: [](ch_snes), `SNES`, `SNESSetOptionsPrefix()`, `SNESAppendOptionsPrefix()`
    5220: @*/
    5221: PetscErrorCode SNESGetOptionsPrefix(SNES snes, const char *prefix[])
    5222: {
    5223:   PetscFunctionBegin;
    5225:   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)snes, prefix));
    5226:   PetscFunctionReturn(PETSC_SUCCESS);
    5227: }

    5229: /*@C
    5230:   SNESRegister - Adds a method to the nonlinear solver package.

    5232:   Not Collective

    5234:   Input Parameters:
    5235: + sname    - name of a new user-defined solver
    5236: - function - routine to create method context

    5238:   Level: advanced

    5240:   Note:
    5241:   `SNESRegister()` may be called multiple times to add several user-defined solvers.

    5243:   Example Usage:
    5244: .vb
    5245:    SNESRegister("my_solver", MySolverCreate);
    5246: .ve

    5248:   Then, your solver can be chosen with the procedural interface via
    5249: $     SNESSetType(snes, "my_solver")
    5250:   or at runtime via the option
    5251: $     -snes_type my_solver

    5253: .seealso: [](ch_snes), `SNESRegisterAll()`, `SNESRegisterDestroy()`
    5254: @*/
    5255: PetscErrorCode SNESRegister(const char sname[], PetscErrorCode (*function)(SNES))
    5256: {
    5257:   PetscFunctionBegin;
    5258:   PetscCall(SNESInitializePackage());
    5259:   PetscCall(PetscFunctionListAdd(&SNESList, sname, function));
    5260:   PetscFunctionReturn(PETSC_SUCCESS);
    5261: }

    5263: PetscErrorCode SNESTestLocalMin(SNES snes)
    5264: {
    5265:   PetscInt    N, i, j;
    5266:   Vec         u, uh, fh;
    5267:   PetscScalar value;
    5268:   PetscReal   norm;

    5270:   PetscFunctionBegin;
    5271:   PetscCall(SNESGetSolution(snes, &u));
    5272:   PetscCall(VecDuplicate(u, &uh));
    5273:   PetscCall(VecDuplicate(u, &fh));

    5275:   /* currently only works for sequential */
    5276:   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)snes), "Testing FormFunction() for local min\n"));
    5277:   PetscCall(VecGetSize(u, &N));
    5278:   for (i = 0; i < N; i++) {
    5279:     PetscCall(VecCopy(u, uh));
    5280:     PetscCall(PetscPrintf(PetscObjectComm((PetscObject)snes), "i = %" PetscInt_FMT "\n", i));
    5281:     for (j = -10; j < 11; j++) {
    5282:       value = PetscSign(j) * PetscExpReal(PetscAbs(j) - 10.0);
    5283:       PetscCall(VecSetValue(uh, i, value, ADD_VALUES));
    5284:       PetscCall(SNESComputeFunction(snes, uh, fh));
    5285:       PetscCall(VecNorm(fh, NORM_2, &norm));
    5286:       PetscCall(PetscPrintf(PetscObjectComm((PetscObject)snes), "       j norm %" PetscInt_FMT " %18.16e\n", j, (double)norm));
    5287:       value = -value;
    5288:       PetscCall(VecSetValue(uh, i, value, ADD_VALUES));
    5289:     }
    5290:   }
    5291:   PetscCall(VecDestroy(&uh));
    5292:   PetscCall(VecDestroy(&fh));
    5293:   PetscFunctionReturn(PETSC_SUCCESS);
    5294: }

    5296: /*@
    5297:   SNESKSPSetUseEW - Sets `SNES` to the use Eisenstat-Walker method for
    5298:   computing relative tolerance for linear solvers within an inexact
    5299:   Newton method.

    5301:   Logically Collective

    5303:   Input Parameters:
    5304: + snes - `SNES` context
    5305: - flag - `PETSC_TRUE` or `PETSC_FALSE`

    5307:   Options Database Keys:
    5308: + -snes_ksp_ew                       - use Eisenstat-Walker method for determining linear system convergence
    5309: . -snes_ksp_ew_version ver           - version of  Eisenstat-Walker method
    5310: . -snes_ksp_ew_rtol0 <rtol0>         - Sets rtol0
    5311: . -snes_ksp_ew_rtolmax <rtolmax>     - Sets rtolmax
    5312: . -snes_ksp_ew_gamma <gamma>         - Sets gamma
    5313: . -snes_ksp_ew_alpha <alpha>         - Sets alpha
    5314: . -snes_ksp_ew_alpha2 <alpha2>       - Sets alpha2
    5315: - -snes_ksp_ew_threshold <threshold> - Sets threshold

    5317:   Level: advanced

    5319:   Note:
    5320:   The default is to use a constant relative tolerance for
    5321:   the inner linear solvers.  Alternatively, one can use the
    5322:   Eisenstat-Walker method {cite}`ew96`, where the relative convergence tolerance
    5323:   is reset at each Newton iteration according progress of the nonlinear
    5324:   solver.

    5326: .seealso: [](ch_snes), `KSP`, `SNES`, `SNESKSPGetUseEW()`, `SNESKSPGetParametersEW()`, `SNESKSPSetParametersEW()`
    5327: @*/
    5328: PetscErrorCode SNESKSPSetUseEW(SNES snes, PetscBool flag)
    5329: {
    5330:   PetscFunctionBegin;
    5333:   snes->ksp_ewconv = flag;
    5334:   PetscFunctionReturn(PETSC_SUCCESS);
    5335: }

    5337: /*@
    5338:   SNESKSPGetUseEW - Gets if `SNES` is using Eisenstat-Walker method
    5339:   for computing relative tolerance for linear solvers within an
    5340:   inexact Newton method.

    5342:   Not Collective

    5344:   Input Parameter:
    5345: . snes - `SNES` context

    5347:   Output Parameter:
    5348: . flag - `PETSC_TRUE` or `PETSC_FALSE`

    5350:   Level: advanced

    5352: .seealso: [](ch_snes), `SNESKSPSetUseEW()`, `SNESKSPGetParametersEW()`, `SNESKSPSetParametersEW()`
    5353: @*/
    5354: PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag)
    5355: {
    5356:   PetscFunctionBegin;
    5358:   PetscAssertPointer(flag, 2);
    5359:   *flag = snes->ksp_ewconv;
    5360:   PetscFunctionReturn(PETSC_SUCCESS);
    5361: }

    5363: /*@
    5364:   SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
    5365:   convergence criteria for the linear solvers within an inexact
    5366:   Newton method.

    5368:   Logically Collective

    5370:   Input Parameters:
    5371: + snes      - `SNES` context
    5372: . version   - version 1, 2 (default is 2), 3 or 4
    5373: . rtol_0    - initial relative tolerance (0 <= rtol_0 < 1)
    5374: . rtol_max  - maximum relative tolerance (0 <= rtol_max < 1)
    5375: . gamma     - multiplicative factor for version 2 rtol computation
    5376:              (0 <= gamma2 <= 1)
    5377: . alpha     - power for version 2 rtol computation (1 < alpha <= 2)
    5378: . alpha2    - power for safeguard
    5379: - threshold - threshold for imposing safeguard (0 < threshold < 1)

    5381:   Level: advanced

    5383:   Notes:
    5384:   Version 3 was contributed by Luis Chacon, June 2006.

    5386:   Use `PETSC_CURRENT` to retain the default for any of the parameters.

    5388: .seealso: [](ch_snes), `SNES`, `SNESKSPSetUseEW()`, `SNESKSPGetUseEW()`, `SNESKSPGetParametersEW()`
    5389: @*/
    5390: PetscErrorCode SNESKSPSetParametersEW(SNES snes, PetscInt version, PetscReal rtol_0, PetscReal rtol_max, PetscReal gamma, PetscReal alpha, PetscReal alpha2, PetscReal threshold)
    5391: {
    5392:   SNESKSPEW *kctx;

    5394:   PetscFunctionBegin;
    5396:   kctx = (SNESKSPEW *)snes->kspconvctx;
    5397:   PetscCheck(kctx, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "No Eisenstat-Walker context existing");

    5406:   if (version != PETSC_CURRENT) kctx->version = version;
    5407:   if (rtol_0 != (PetscReal)PETSC_CURRENT) kctx->rtol_0 = rtol_0;
    5408:   if (rtol_max != (PetscReal)PETSC_CURRENT) kctx->rtol_max = rtol_max;
    5409:   if (gamma != (PetscReal)PETSC_CURRENT) kctx->gamma = gamma;
    5410:   if (alpha != (PetscReal)PETSC_CURRENT) kctx->alpha = alpha;
    5411:   if (alpha2 != (PetscReal)PETSC_CURRENT) kctx->alpha2 = alpha2;
    5412:   if (threshold != (PetscReal)PETSC_CURRENT) kctx->threshold = threshold;

    5414:   PetscCheck(kctx->version >= 1 && kctx->version <= 4, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Only versions 1 to 4 are supported: %" PetscInt_FMT, kctx->version);
    5415:   PetscCheck(kctx->rtol_0 >= 0.0 && kctx->rtol_0 < 1.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "0.0 <= rtol_0 < 1.0: %g", (double)kctx->rtol_0);
    5416:   PetscCheck(kctx->rtol_max >= 0.0 && kctx->rtol_max < 1.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "0.0 <= rtol_max (%g) < 1.0", (double)kctx->rtol_max);
    5417:   PetscCheck(kctx->gamma >= 0.0 && kctx->gamma <= 1.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "0.0 <= gamma (%g) <= 1.0", (double)kctx->gamma);
    5418:   PetscCheck(kctx->alpha > 1.0 && kctx->alpha <= 2.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "1.0 < alpha (%g) <= 2.0", (double)kctx->alpha);
    5419:   PetscCheck(kctx->threshold > 0.0 && kctx->threshold < 1.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "0.0 < threshold (%g) < 1.0", (double)kctx->threshold);
    5420:   PetscFunctionReturn(PETSC_SUCCESS);
    5421: }

    5423: /*@
    5424:   SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
    5425:   convergence criteria for the linear solvers within an inexact
    5426:   Newton method.

    5428:   Not Collective

    5430:   Input Parameter:
    5431: . snes - `SNES` context

    5433:   Output Parameters:
    5434: + version   - version 1, 2 (default is 2), 3 or 4
    5435: . rtol_0    - initial relative tolerance (0 <= rtol_0 < 1)
    5436: . rtol_max  - maximum relative tolerance (0 <= rtol_max < 1)
    5437: . gamma     - multiplicative factor for version 2 rtol computation (0 <= gamma2 <= 1)
    5438: . alpha     - power for version 2 rtol computation (1 < alpha <= 2)
    5439: . alpha2    - power for safeguard
    5440: - threshold - threshold for imposing safeguard (0 < threshold < 1)

    5442:   Level: advanced

    5444: .seealso: [](ch_snes), `SNES`, `SNESKSPSetUseEW()`, `SNESKSPGetUseEW()`, `SNESKSPSetParametersEW()`
    5445: @*/
    5446: PetscErrorCode SNESKSPGetParametersEW(SNES snes, PetscInt *version, PetscReal *rtol_0, PetscReal *rtol_max, PetscReal *gamma, PetscReal *alpha, PetscReal *alpha2, PetscReal *threshold)
    5447: {
    5448:   SNESKSPEW *kctx;

    5450:   PetscFunctionBegin;
    5452:   kctx = (SNESKSPEW *)snes->kspconvctx;
    5453:   PetscCheck(kctx, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "No Eisenstat-Walker context existing");
    5454:   if (version) *version = kctx->version;
    5455:   if (rtol_0) *rtol_0 = kctx->rtol_0;
    5456:   if (rtol_max) *rtol_max = kctx->rtol_max;
    5457:   if (gamma) *gamma = kctx->gamma;
    5458:   if (alpha) *alpha = kctx->alpha;
    5459:   if (alpha2) *alpha2 = kctx->alpha2;
    5460:   if (threshold) *threshold = kctx->threshold;
    5461:   PetscFunctionReturn(PETSC_SUCCESS);
    5462: }

    5464: PetscErrorCode KSPPreSolve_SNESEW(KSP ksp, Vec b, Vec x, void *ctx)
    5465: {
    5466:   SNES       snes = (SNES)ctx;
    5467:   SNESKSPEW *kctx = (SNESKSPEW *)snes->kspconvctx;
    5468:   PetscReal  rtol = PETSC_CURRENT, stol;

    5470:   PetscFunctionBegin;
    5471:   if (!snes->ksp_ewconv) PetscFunctionReturn(PETSC_SUCCESS);
    5472:   if (!snes->iter) {
    5473:     rtol = kctx->rtol_0; /* first time in, so use the original user rtol */
    5474:     PetscCall(VecNorm(snes->vec_func, NORM_2, &kctx->norm_first));
    5475:   } else {
    5476:     PetscCheck(kctx->version >= 1 && kctx->version <= 4, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Only versions 1-4 are supported: %" PetscInt_FMT, kctx->version);
    5477:     if (kctx->version == 1) {
    5478:       rtol = PetscAbsReal(snes->norm - kctx->lresid_last) / kctx->norm_last;
    5479:       stol = PetscPowReal(kctx->rtol_last, kctx->alpha2);
    5480:       if (stol > kctx->threshold) rtol = PetscMax(rtol, stol);
    5481:     } else if (kctx->version == 2) {
    5482:       rtol = kctx->gamma * PetscPowReal(snes->norm / kctx->norm_last, kctx->alpha);
    5483:       stol = kctx->gamma * PetscPowReal(kctx->rtol_last, kctx->alpha);
    5484:       if (stol > kctx->threshold) rtol = PetscMax(rtol, stol);
    5485:     } else if (kctx->version == 3) { /* contributed by Luis Chacon, June 2006. */
    5486:       rtol = kctx->gamma * PetscPowReal(snes->norm / kctx->norm_last, kctx->alpha);
    5487:       /* safeguard: avoid sharp decrease of rtol */
    5488:       stol = kctx->gamma * PetscPowReal(kctx->rtol_last, kctx->alpha);
    5489:       stol = PetscMax(rtol, stol);
    5490:       rtol = PetscMin(kctx->rtol_0, stol);
    5491:       /* safeguard: avoid oversolving */
    5492:       stol = kctx->gamma * (kctx->norm_first * snes->rtol) / snes->norm;
    5493:       stol = PetscMax(rtol, stol);
    5494:       rtol = PetscMin(kctx->rtol_0, stol);
    5495:     } else /* if (kctx->version == 4) */ {
    5496:       /* H.-B. An et al. Journal of Computational and Applied Mathematics 200 (2007) 47-60 */
    5497:       PetscReal ared = PetscAbsReal(kctx->norm_last - snes->norm);
    5498:       PetscReal pred = PetscAbsReal(kctx->norm_last - kctx->lresid_last);
    5499:       PetscReal rk   = ared / pred;
    5500:       if (rk < kctx->v4_p1) rtol = 1. - 2. * kctx->v4_p1;
    5501:       else if (rk < kctx->v4_p2) rtol = kctx->rtol_last;
    5502:       else if (rk < kctx->v4_p3) rtol = kctx->v4_m1 * kctx->rtol_last;
    5503:       else rtol = kctx->v4_m2 * kctx->rtol_last;

    5505:       if (kctx->rtol_last_2 > kctx->v4_m3 && kctx->rtol_last > kctx->v4_m3 && kctx->rk_last_2 < kctx->v4_p1 && kctx->rk_last < kctx->v4_p1) rtol = kctx->v4_m4 * kctx->rtol_last;
    5506:       kctx->rtol_last_2 = kctx->rtol_last;
    5507:       kctx->rk_last_2   = kctx->rk_last;
    5508:       kctx->rk_last     = rk;
    5509:     }
    5510:   }
    5511:   /* safeguard: avoid rtol greater than rtol_max */
    5512:   rtol = PetscMin(rtol, kctx->rtol_max);
    5513:   PetscCall(KSPSetTolerances(ksp, rtol, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT));
    5514:   PetscCall(PetscInfo(snes, "iter %" PetscInt_FMT ", Eisenstat-Walker (version %" PetscInt_FMT ") KSP rtol=%g\n", snes->iter, kctx->version, (double)rtol));
    5515:   PetscFunctionReturn(PETSC_SUCCESS);
    5516: }

    5518: PetscErrorCode KSPPostSolve_SNESEW(KSP ksp, Vec b, Vec x, void *ctx)
    5519: {
    5520:   SNES       snes = (SNES)ctx;
    5521:   SNESKSPEW *kctx = (SNESKSPEW *)snes->kspconvctx;
    5522:   PCSide     pcside;
    5523:   Vec        lres;

    5525:   PetscFunctionBegin;
    5526:   if (!snes->ksp_ewconv) PetscFunctionReturn(PETSC_SUCCESS);
    5527:   PetscCall(KSPGetTolerances(ksp, &kctx->rtol_last, NULL, NULL, NULL));
    5528:   kctx->norm_last = snes->norm;
    5529:   if (kctx->version == 1 || kctx->version == 4) {
    5530:     PC        pc;
    5531:     PetscBool getRes;

    5533:     PetscCall(KSPGetPC(ksp, &pc));
    5534:     PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCNONE, &getRes));
    5535:     if (!getRes) {
    5536:       KSPNormType normtype;

    5538:       PetscCall(KSPGetNormType(ksp, &normtype));
    5539:       getRes = (PetscBool)(normtype == KSP_NORM_UNPRECONDITIONED);
    5540:     }
    5541:     PetscCall(KSPGetPCSide(ksp, &pcside));
    5542:     if (pcside == PC_RIGHT || getRes) { /* KSP residual is true linear residual */
    5543:       PetscCall(KSPGetResidualNorm(ksp, &kctx->lresid_last));
    5544:     } else {
    5545:       /* KSP residual is preconditioned residual */
    5546:       /* compute true linear residual norm */
    5547:       Mat J;
    5548:       PetscCall(KSPGetOperators(ksp, &J, NULL));
    5549:       PetscCall(VecDuplicate(b, &lres));
    5550:       PetscCall(MatMult(J, x, lres));
    5551:       PetscCall(VecAYPX(lres, -1.0, b));
    5552:       PetscCall(VecNorm(lres, NORM_2, &kctx->lresid_last));
    5553:       PetscCall(VecDestroy(&lres));
    5554:     }
    5555:   }
    5556:   PetscFunctionReturn(PETSC_SUCCESS);
    5557: }

    5559: /*@
    5560:   SNESGetKSP - Returns the `KSP` context for a `SNES` solver.

    5562:   Not Collective, but if `snes` is parallel, then `ksp` is parallel

    5564:   Input Parameter:
    5565: . snes - the `SNES` context

    5567:   Output Parameter:
    5568: . ksp - the `KSP` context

    5570:   Level: beginner

    5572:   Notes:
    5573:   The user can then directly manipulate the `KSP` context to set various
    5574:   options, etc.  Likewise, the user can then extract and manipulate the
    5575:   `PC` contexts as well.

    5577:   Some `SNESType`s do not use a `KSP` but a `KSP` is still returned by this function, changes to that `KSP` will have no effect.

    5579: .seealso: [](ch_snes), `SNES`, `KSP`, `PC`, `KSPGetPC()`, `SNESCreate()`, `KSPCreate()`, `SNESSetKSP()`
    5580: @*/
    5581: PetscErrorCode SNESGetKSP(SNES snes, KSP *ksp)
    5582: {
    5583:   PetscFunctionBegin;
    5585:   PetscAssertPointer(ksp, 2);

    5587:   if (!snes->ksp) {
    5588:     PetscCall(KSPCreate(PetscObjectComm((PetscObject)snes), &snes->ksp));
    5589:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)snes->ksp, (PetscObject)snes, 1));

    5591:     PetscCall(KSPSetPreSolve(snes->ksp, KSPPreSolve_SNESEW, snes));
    5592:     PetscCall(KSPSetPostSolve(snes->ksp, KSPPostSolve_SNESEW, snes));

    5594:     PetscCall(KSPMonitorSetFromOptions(snes->ksp, "-snes_monitor_ksp", "snes_preconditioned_residual", snes));
    5595:     PetscCall(PetscObjectSetOptions((PetscObject)snes->ksp, ((PetscObject)snes)->options));
    5596:   }
    5597:   *ksp = snes->ksp;
    5598:   PetscFunctionReturn(PETSC_SUCCESS);
    5599: }

    5601: #include <petsc/private/dmimpl.h>
    5602: /*@
    5603:   SNESSetDM - Sets the `DM` that may be used by some `SNES` nonlinear solvers or their underlying preconditioners

    5605:   Logically Collective

    5607:   Input Parameters:
    5608: + snes - the nonlinear solver context
    5609: - dm   - the `DM`, cannot be `NULL`

    5611:   Level: intermediate

    5613:   Note:
    5614:   A `DM` can only be used for solving one problem at a time because information about the problem is stored on the `DM`,
    5615:   even when not using interfaces like `DMSNESSetFunction()`.  Use `DMClone()` to get a distinct `DM` when solving different
    5616:   problems using the same function space.

    5618: .seealso: [](ch_snes), `DM`, `SNES`, `SNESGetDM()`, `KSPSetDM()`, `KSPGetDM()`
    5619: @*/
    5620: PetscErrorCode SNESSetDM(SNES snes, DM dm)
    5621: {
    5622:   KSP    ksp;
    5623:   DMSNES sdm;

    5625:   PetscFunctionBegin;
    5628:   PetscCall(PetscObjectReference((PetscObject)dm));
    5629:   if (snes->dm) { /* Move the DMSNES context over to the new DM unless the new DM already has one */
    5630:     if (snes->dm->dmsnes && !dm->dmsnes) {
    5631:       PetscCall(DMCopyDMSNES(snes->dm, dm));
    5632:       PetscCall(DMGetDMSNES(snes->dm, &sdm));
    5633:       if (sdm->originaldm == snes->dm) sdm->originaldm = dm; /* Grant write privileges to the replacement DM */
    5634:     }
    5635:     PetscCall(DMCoarsenHookRemove(snes->dm, DMCoarsenHook_SNESVecSol, DMRestrictHook_SNESVecSol, snes));
    5636:     PetscCall(DMDestroy(&snes->dm));
    5637:   }
    5638:   snes->dm     = dm;
    5639:   snes->dmAuto = PETSC_FALSE;

    5641:   PetscCall(SNESGetKSP(snes, &ksp));
    5642:   PetscCall(KSPSetDM(ksp, dm));
    5643:   PetscCall(KSPSetDMActive(ksp, PETSC_FALSE));
    5644:   if (snes->npc) {
    5645:     PetscCall(SNESSetDM(snes->npc, snes->dm));
    5646:     PetscCall(SNESSetNPCSide(snes, snes->npcside));
    5647:   }
    5648:   PetscFunctionReturn(PETSC_SUCCESS);
    5649: }

    5651: /*@
    5652:   SNESGetDM - Gets the `DM` that may be used by some `SNES` nonlinear solvers/preconditioners

    5654:   Not Collective but `dm` obtained is parallel on `snes`

    5656:   Input Parameter:
    5657: . snes - the `SNES` context

    5659:   Output Parameter:
    5660: . dm - the `DM`

    5662:   Level: intermediate

    5664: .seealso: [](ch_snes), `DM`, `SNES`, `SNESSetDM()`, `KSPSetDM()`, `KSPGetDM()`
    5665: @*/
    5666: PetscErrorCode SNESGetDM(SNES snes, DM *dm)
    5667: {
    5668:   PetscFunctionBegin;
    5670:   if (!snes->dm) {
    5671:     PetscCall(DMShellCreate(PetscObjectComm((PetscObject)snes), &snes->dm));
    5672:     snes->dmAuto = PETSC_TRUE;
    5673:   }
    5674:   *dm = snes->dm;
    5675:   PetscFunctionReturn(PETSC_SUCCESS);
    5676: }

    5678: /*@
    5679:   SNESSetNPC - Sets the nonlinear preconditioner to be used.

    5681:   Collective

    5683:   Input Parameters:
    5684: + snes - iterative context obtained from `SNESCreate()`
    5685: - npc  - the `SNES` nonlinear preconditioner object

    5687:   Options Database Key:
    5688: . -npc_snes_type <type> - set the type of the `SNES` to use as the nonlinear preconditioner

    5690:   Level: developer

    5692:   Notes:
    5693:   This is rarely used, rather use `SNESGetNPC()` to retrieve the preconditioner and configure it using the API.

    5695:   Only some `SNESType` can use a nonlinear preconditioner

    5697: .seealso: [](ch_snes), `SNES`, `SNESNGS`, `SNESFAS`, `SNESGetNPC()`, `SNESHasNPC()`
    5698: @*/
    5699: PetscErrorCode SNESSetNPC(SNES snes, SNES npc)
    5700: {
    5701:   PetscFunctionBegin;
    5704:   PetscCheckSameComm(snes, 1, npc, 2);
    5705:   PetscCall(PetscObjectReference((PetscObject)npc));
    5706:   PetscCall(SNESDestroy(&snes->npc));
    5707:   snes->npc = npc;
    5708:   PetscFunctionReturn(PETSC_SUCCESS);
    5709: }

    5711: /*@
    5712:   SNESGetNPC - Gets a nonlinear preconditioning solver SNES` to be used to precondition the original nonlinear solver.

    5714:   Not Collective; but any changes to the obtained the `pc` object must be applied collectively

    5716:   Input Parameter:
    5717: . snes - iterative context obtained from `SNESCreate()`

    5719:   Output Parameter:
    5720: . pc - the `SNES` preconditioner context

    5722:   Options Database Key:
    5723: . -npc_snes_type <type> - set the type of the `SNES` to use as the nonlinear preconditioner

    5725:   Level: advanced

    5727:   Notes:
    5728:   If a `SNES` was previously set with `SNESSetNPC()` then that value is returned, otherwise a new `SNES` object is created that will
    5729:   be used as the nonlinear preconditioner for the current `SNES`.

    5731:   The (preconditioner) `SNES` returned automatically inherits the same nonlinear function and Jacobian supplied to the original
    5732:   `SNES`. These may be overwritten if needed.

    5734:   Use the options database prefixes `-npc_snes`, `-npc_ksp`, etc., to control the configuration of the nonlinear preconditioner

    5736: .seealso: [](ch_snes), `SNESSetNPC()`, `SNESHasNPC()`, `SNES`, `SNESCreate()`
    5737: @*/
    5738: PetscErrorCode SNESGetNPC(SNES snes, SNES *pc)
    5739: {
    5740:   const char *optionsprefix;

    5742:   PetscFunctionBegin;
    5744:   PetscAssertPointer(pc, 2);
    5745:   if (!snes->npc) {
    5746:     void *ctx;

    5748:     PetscCall(SNESCreate(PetscObjectComm((PetscObject)snes), &snes->npc));
    5749:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)snes->npc, (PetscObject)snes, 1));
    5750:     PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix));
    5751:     PetscCall(SNESSetOptionsPrefix(snes->npc, optionsprefix));
    5752:     PetscCall(SNESAppendOptionsPrefix(snes->npc, "npc_"));
    5753:     if (snes->ops->usercompute) {
    5754:       PetscCall(SNESSetComputeApplicationContext(snes, snes->ops->usercompute, snes->ops->userdestroy));
    5755:     } else {
    5756:       PetscCall(SNESGetApplicationContext(snes, &ctx));
    5757:       PetscCall(SNESSetApplicationContext(snes->npc, ctx));
    5758:     }
    5759:     PetscCall(SNESSetCountersReset(snes->npc, PETSC_FALSE));
    5760:   }
    5761:   *pc = snes->npc;
    5762:   PetscFunctionReturn(PETSC_SUCCESS);
    5763: }

    5765: /*@
    5766:   SNESHasNPC - Returns whether a nonlinear preconditioner is associated with the given `SNES`

    5768:   Not Collective

    5770:   Input Parameter:
    5771: . snes - iterative context obtained from `SNESCreate()`

    5773:   Output Parameter:
    5774: . has_npc - whether the `SNES` has a nonlinear preconditioner or not

    5776:   Level: developer

    5778: .seealso: [](ch_snes), `SNESSetNPC()`, `SNESGetNPC()`
    5779: @*/
    5780: PetscErrorCode SNESHasNPC(SNES snes, PetscBool *has_npc)
    5781: {
    5782:   PetscFunctionBegin;
    5784:   *has_npc = (PetscBool)(snes->npc ? PETSC_TRUE : PETSC_FALSE);
    5785:   PetscFunctionReturn(PETSC_SUCCESS);
    5786: }

    5788: /*@
    5789:   SNESSetNPCSide - Sets the nonlinear preconditioning side used by the nonlinear preconditioner inside `SNES`.

    5791:   Logically Collective

    5793:   Input Parameter:
    5794: . snes - iterative context obtained from `SNESCreate()`

    5796:   Output Parameter:
    5797: . side - the preconditioning side, where side is one of
    5798: .vb
    5799:       PC_LEFT  - left preconditioning
    5800:       PC_RIGHT - right preconditioning (default for most nonlinear solvers)
    5801: .ve

    5803:   Options Database Key:
    5804: . -snes_npc_side <right,left> - nonlinear preconditioner side

    5806:   Level: intermediate

    5808:   Note:
    5809:   `SNESNRICHARDSON` and `SNESNCG` only support left preconditioning.

    5811: .seealso: [](ch_snes), `SNES`, `SNESGetNPC()`, `SNESNRICHARDSON`, `SNESNCG`, `SNESType`, `SNESGetNPCSide()`, `KSPSetPCSide()`, `PC_LEFT`, `PC_RIGHT`, `PCSide`
    5812: @*/
    5813: PetscErrorCode SNESSetNPCSide(SNES snes, PCSide side)
    5814: {
    5815:   PetscFunctionBegin;
    5818:   if (side == PC_SIDE_DEFAULT) side = PC_RIGHT;
    5819:   PetscCheck((side == PC_LEFT) || (side == PC_RIGHT), PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_WRONG, "Only PC_LEFT and PC_RIGHT are supported");
    5820:   snes->npcside = side;
    5821:   PetscFunctionReturn(PETSC_SUCCESS);
    5822: }

    5824: /*@
    5825:   SNESGetNPCSide - Gets the preconditioning side used by the nonlinear preconditioner inside `SNES`.

    5827:   Not Collective

    5829:   Input Parameter:
    5830: . snes - iterative context obtained from `SNESCreate()`

    5832:   Output Parameter:
    5833: . side - the preconditioning side, where side is one of
    5834: .vb
    5835:       `PC_LEFT` - left preconditioning
    5836:       `PC_RIGHT` - right preconditioning (default for most nonlinear solvers)
    5837: .ve

    5839:   Level: intermediate

    5841: .seealso: [](ch_snes), `SNES`, `SNESGetNPC()`, `SNESSetNPCSide()`, `KSPGetPCSide()`, `PC_LEFT`, `PC_RIGHT`, `PCSide`
    5842: @*/
    5843: PetscErrorCode SNESGetNPCSide(SNES snes, PCSide *side)
    5844: {
    5845:   PetscFunctionBegin;
    5847:   PetscAssertPointer(side, 2);
    5848:   *side = snes->npcside;
    5849:   PetscFunctionReturn(PETSC_SUCCESS);
    5850: }

    5852: /*@
    5853:   SNESSetLineSearch - Sets the `SNESLineSearch` to be used for a given `SNES`

    5855:   Collective

    5857:   Input Parameters:
    5858: + snes       - iterative context obtained from `SNESCreate()`
    5859: - linesearch - the linesearch object

    5861:   Level: developer

    5863:   Note:
    5864:   This is almost never used, rather one uses `SNESGetLineSearch()` to retrieve the line search and set options on it
    5865:   to configure it using the API).

    5867: .seealso: [](ch_snes), `SNES`, `SNESLineSearch`, `SNESGetLineSearch()`
    5868: @*/
    5869: PetscErrorCode SNESSetLineSearch(SNES snes, SNESLineSearch linesearch)
    5870: {
    5871:   PetscFunctionBegin;
    5874:   PetscCheckSameComm(snes, 1, linesearch, 2);
    5875:   PetscCall(PetscObjectReference((PetscObject)linesearch));
    5876:   PetscCall(SNESLineSearchDestroy(&snes->linesearch));

    5878:   snes->linesearch = linesearch;
    5879:   PetscFunctionReturn(PETSC_SUCCESS);
    5880: }

    5882: /*@
    5883:   SNESGetLineSearch - Returns the line search associated with the `SNES`.

    5885:   Not Collective

    5887:   Input Parameter:
    5888: . snes - iterative context obtained from `SNESCreate()`

    5890:   Output Parameter:
    5891: . linesearch - linesearch context

    5893:   Level: beginner

    5895:   Notes:
    5896:   It creates a default line search instance which can be configured as needed in case it has not been already set with `SNESSetLineSearch()`.

    5898:   You can also use the options database keys `-snes_linesearch_*` to configure the line search. See `SNESLineSearchSetFromOptions()` for the possible options.

    5900: .seealso: [](ch_snes), `SNESLineSearch`, `SNESSetLineSearch()`, `SNESLineSearchCreate()`, `SNESLineSearchSetFromOptions()`
    5901: @*/
    5902: PetscErrorCode SNESGetLineSearch(SNES snes, SNESLineSearch *linesearch)
    5903: {
    5904:   const char *optionsprefix;

    5906:   PetscFunctionBegin;
    5908:   PetscAssertPointer(linesearch, 2);
    5909:   if (!snes->linesearch) {
    5910:     PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix));
    5911:     PetscCall(SNESLineSearchCreate(PetscObjectComm((PetscObject)snes), &snes->linesearch));
    5912:     PetscCall(SNESLineSearchSetSNES(snes->linesearch, snes));
    5913:     PetscCall(SNESLineSearchAppendOptionsPrefix(snes->linesearch, optionsprefix));
    5914:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)snes->linesearch, (PetscObject)snes, 1));
    5915:   }
    5916:   *linesearch = snes->linesearch;
    5917:   PetscFunctionReturn(PETSC_SUCCESS);
    5918: }