Actual source code: snes.c

petsc-master 2020-10-23
Report Typos and Errors
  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_NPCSolve, SNES_ObjectiveEval;

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

 18:    Logically Collective on SNES

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

 24:    Options database keys:
 25: .  -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false)

 27:    Level: intermediate

 29:    Notes:
 30:     Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve()
 31:     to determine if it has converged.

 33: .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
 34: @*/
 35: PetscErrorCode  SNESSetErrorIfNotConverged(SNES snes,PetscBool flg)
 36: {
 40:   snes->errorifnotconverged = flg;
 41:   return(0);
 42: }

 44: /*@
 45:    SNESGetErrorIfNotConverged - Will SNESSolve() 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:  SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
 58: @*/
 59: PetscErrorCode  SNESGetErrorIfNotConverged(SNES snes,PetscBool  *flag)
 60: {
 64:   *flag = snes->errorifnotconverged;
 65:   return(0);
 66: }

 68: /*@
 69:     SNESSetAlwaysComputesFinalResidual - does the SNES always compute the residual at the final solution?

 71:    Logically Collective on SNES

 73:     Input Parameters:
 74: +   snes - the shell SNES
 75: -   flg - is the residual computed?

 77:    Level: advanced

 79: .seealso: SNESGetAlwaysComputesFinalResidual()
 80: @*/
 81: PetscErrorCode  SNESSetAlwaysComputesFinalResidual(SNES snes, PetscBool flg)
 82: {
 85:   snes->alwayscomputesfinalresidual = flg;
 86:   return(0);
 87: }

 89: /*@
 90:     SNESGetAlwaysComputesFinalResidual - does the SNES always compute the residual at the final solution?

 92:    Logically Collective on SNES

 94:     Input Parameter:
 95: .   snes - the shell SNES

 97:     Output Parameter:
 98: .   flg - is the residual computed?

100:    Level: advanced

102: .seealso: SNESSetAlwaysComputesFinalResidual()
103: @*/
104: PetscErrorCode  SNESGetAlwaysComputesFinalResidual(SNES snes, PetscBool *flg)
105: {
108:   *flg = snes->alwayscomputesfinalresidual;
109:   return(0);
110: }

112: /*@
113:    SNESSetFunctionDomainError - tells SNES that the input vector to your SNESFunction is not
114:      in the functions domain. For example, negative pressure.

116:    Logically Collective on SNES

118:    Input Parameters:
119: .  snes - the SNES context

121:    Level: advanced

123: .seealso: SNESCreate(), SNESSetFunction(), SNESFunction
124: @*/
125: PetscErrorCode  SNESSetFunctionDomainError(SNES snes)
126: {
129:   if (snes->errorifnotconverged) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"User code indicates input vector is not in the function domain");
130:   snes->domainerror = PETSC_TRUE;
131:   return(0);
132: }

134: /*@
135:    SNESSetJacobianDomainError - tells SNES that computeJacobian does not make sense any more. For example there is a negative element transformation.

137:    Logically Collective on SNES

139:    Input Parameters:
140: .  snes - the SNES context

142:    Level: advanced

144: .seealso: SNESCreate(), SNESSetFunction(), SNESFunction(), SNESSetFunctionDomainError()
145: @*/
146: PetscErrorCode SNESSetJacobianDomainError(SNES snes)
147: {
150:   if (snes->errorifnotconverged) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"User code indicates computeJacobian does not make sense");
151:   snes->jacobiandomainerror = PETSC_TRUE;
152:   return(0);
153: }

155: /*@
156:    SNESSetCheckJacobianDomainError - if or not to check jacobian domain error after each Jacobian evaluation. By default, we check Jacobian domain error
157:    in the debug mode, and do not check it in the optimized mode.

159:    Logically Collective on SNES

161:    Input Parameters:
162: +  snes - the SNES context
163: -  flg  - indicates if or not to check jacobian domain error after each Jacobian evaluation

165:    Level: advanced

167: .seealso: SNESCreate(), SNESSetFunction(), SNESFunction(), SNESSetFunctionDomainError(), SNESGetCheckJacobianDomainError()
168: @*/
169: PetscErrorCode SNESSetCheckJacobianDomainError(SNES snes, PetscBool flg)
170: {
173:   snes->checkjacdomainerror = flg;
174:   return(0);
175: }

177: /*@
178:    SNESGetCheckJacobianDomainError - Get an indicator whether or not we are checking Jacobian domain errors after each Jacobian evaluation.

180:    Logically Collective on SNES

182:    Input Parameters:
183: .  snes - the SNES context

185:    Output Parameters:
186: .  flg  - PETSC_FALSE indicates that we don't check jacobian domain errors after each Jacobian evaluation

188:    Level: advanced

190: .seealso: SNESCreate(), SNESSetFunction(), SNESFunction(), SNESSetFunctionDomainError(), SNESSetCheckJacobianDomainError()
191: @*/
192: PetscErrorCode SNESGetCheckJacobianDomainError(SNES snes, PetscBool *flg)
193: {
197:   *flg = snes->checkjacdomainerror;
198:   return(0);
199: }

201: /*@
202:    SNESGetFunctionDomainError - Gets the status of the domain error after a call to SNESComputeFunction;

204:    Logically Collective on SNES

206:    Input Parameters:
207: .  snes - the SNES context

209:    Output Parameters:
210: .  domainerror - Set to PETSC_TRUE if there's a domain error; PETSC_FALSE otherwise.

212:    Level: advanced

214: .seealso: SNESSetFunctionDomainError(), SNESComputeFunction()
215: @*/
216: PetscErrorCode  SNESGetFunctionDomainError(SNES snes, PetscBool *domainerror)
217: {
221:   *domainerror = snes->domainerror;
222:   return(0);
223: }

225: /*@
226:    SNESGetJacobianDomainError - Gets the status of the Jacobian domain error after a call to SNESComputeJacobian;

228:    Logically Collective on SNES

230:    Input Parameters:
231: .  snes - the SNES context

233:    Output Parameters:
234: .  domainerror - Set to PETSC_TRUE if there's a jacobian domain error; PETSC_FALSE otherwise.

236:    Level: advanced

238: .seealso: SNESSetFunctionDomainError(), SNESComputeFunction(),SNESGetFunctionDomainError()
239: @*/
240: PetscErrorCode SNESGetJacobianDomainError(SNES snes, PetscBool *domainerror)
241: {
245:   *domainerror = snes->jacobiandomainerror;
246:   return(0);
247: }

249: /*@C
250:   SNESLoad - Loads a SNES that has been stored in binary  with SNESView().

252:   Collective on PetscViewer

254:   Input Parameters:
255: + newdm - the newly loaded SNES, this needs to have been created with SNESCreate() or
256:            some related function before a call to SNESLoad().
257: - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()

259:    Level: intermediate

261:   Notes:
262:    The type is determined by the data in the file, any type set into the SNES before this call is ignored.

264:   Notes for advanced users:
265:   Most users should not need to know the details of the binary storage
266:   format, since SNESLoad() and TSView() completely hide these details.
267:   But for anyone who's interested, the standard binary matrix storage
268:   format is
269: .vb
270:      has not yet been determined
271: .ve

273: .seealso: PetscViewerBinaryOpen(), SNESView(), MatLoad(), VecLoad()
274: @*/
275: PetscErrorCode  SNESLoad(SNES snes, PetscViewer viewer)
276: {
278:   PetscBool      isbinary;
279:   PetscInt       classid;
280:   char           type[256];
281:   KSP            ksp;
282:   DM             dm;
283:   DMSNES         dmsnes;

288:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
289:   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");

291:   PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);
292:   if (classid != SNES_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONG,"Not SNES next in file");
293:   PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);
294:   SNESSetType(snes, type);
295:   if (snes->ops->load) {
296:     (*snes->ops->load)(snes,viewer);
297:   }
298:   SNESGetDM(snes,&dm);
299:   DMGetDMSNES(dm,&dmsnes);
300:   DMSNESLoad(dmsnes,viewer);
301:   SNESGetKSP(snes,&ksp);
302:   KSPLoad(ksp,viewer);
303:   return(0);
304: }

306: #include <petscdraw.h>
307: #if defined(PETSC_HAVE_SAWS)
308: #include <petscviewersaws.h>
309: #endif

311: /*@C
312:    SNESViewFromOptions - View from Options

314:    Collective on SNES

316:    Input Parameters:
317: +  A - the application ordering context
318: .  obj - Optional object
319: -  name - command line option

321:    Level: intermediate
322: .seealso:  SNES, SNESView, PetscObjectViewFromOptions(), SNESCreate()
323: @*/
324: PetscErrorCode  SNESViewFromOptions(SNES A,PetscObject obj,const char name[])
325: {

330:   PetscObjectViewFromOptions((PetscObject)A,obj,name);
331:   return(0);
332: }

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

336: /*@C
337:    SNESView - Prints the SNES data structure.

339:    Collective on SNES

341:    Input Parameters:
342: +  SNES - the SNES context
343: -  viewer - visualization context

345:    Options Database Key:
346: .  -snes_view - Calls SNESView() at end of SNESSolve()

348:    Notes:
349:    The available visualization contexts include
350: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
351: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
352:          output where only the first processor opens
353:          the file.  All other processors send their
354:          data to the first processor to print.

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

359:    Level: beginner

361: .seealso: PetscViewerASCIIOpen()
362: @*/
363: PetscErrorCode  SNESView(SNES snes,PetscViewer viewer)
364: {
365:   SNESKSPEW      *kctx;
367:   KSP            ksp;
368:   SNESLineSearch linesearch;
369:   PetscBool      iascii,isstring,isbinary,isdraw;
370:   DMSNES         dmsnes;
371: #if defined(PETSC_HAVE_SAWS)
372:   PetscBool      issaws;
373: #endif

377:   if (!viewer) {
378:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&viewer);
379:   }

383:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
384:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
385:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
386:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
387: #if defined(PETSC_HAVE_SAWS)
388:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
389: #endif
390:   if (iascii) {
391:     SNESNormSchedule normschedule;
392:     DM               dm;
393:     PetscErrorCode   (*cJ)(SNES,Vec,Mat,Mat,void*);
394:     void             *ctx;
395:     const char       *pre = "";

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

468:     PetscObjectGetComm((PetscObject)snes,&comm);
469:     MPI_Comm_rank(comm,&rank);
470:     if (!rank) {
471:       PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);
472:       PetscStrncpy(type,((PetscObject)snes)->type_name,sizeof(type));
473:       PetscViewerBinaryWrite(viewer,type,sizeof(type),PETSC_CHAR);
474:     }
475:     if (snes->ops->view) {
476:       (*snes->ops->view)(snes,viewer);
477:     }
478:   } else if (isdraw) {
479:     PetscDraw draw;
480:     char      str[36];
481:     PetscReal x,y,bottom,h;

483:     PetscViewerDrawGetDraw(viewer,0,&draw);
484:     PetscDrawGetCurrentPoint(draw,&x,&y);
485:     PetscStrncpy(str,"SNES: ",sizeof(str));
486:     PetscStrlcat(str,((PetscObject)snes)->type_name,sizeof(str));
487:     PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLUE,PETSC_DRAW_BLACK,str,NULL,&h);
488:     bottom = y - h;
489:     PetscDrawPushCurrentPoint(draw,x,bottom);
490:     if (snes->ops->view) {
491:       (*snes->ops->view)(snes,viewer);
492:     }
493: #if defined(PETSC_HAVE_SAWS)
494:   } else if (issaws) {
495:     PetscMPIInt rank;
496:     const char *name;

498:     PetscObjectGetName((PetscObject)snes,&name);
499:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
500:     if (!((PetscObject)snes)->amsmem && !rank) {
501:       char       dir[1024];

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

543: /*
544:   We retain a list of functions that also take SNES command
545:   line options. These are called at the end SNESSetFromOptions()
546: */
547: #define MAXSETFROMOPTIONS 5
548: static PetscInt numberofsetfromoptions;
549: static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);

551: /*@C
552:   SNESAddOptionsChecker - Adds an additional function to check for SNES options.

554:   Not Collective

556:   Input Parameter:
557: . snescheck - function that checks for options

559:   Level: developer

561: .seealso: SNESSetFromOptions()
562: @*/
563: PetscErrorCode  SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
564: {
566:   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
567:   othersetfromoptions[numberofsetfromoptions++] = snescheck;
568:   return(0);
569: }

571: PETSC_INTERN PetscErrorCode SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);

573: static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool hasOperator, PetscInt version)
574: {
575:   Mat            J;
577:   MatNullSpace   nullsp;


582:   if (!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
583:     Mat A = snes->jacobian, B = snes->jacobian_pre;
584:     MatCreateVecs(A ? A : B, NULL,&snes->vec_func);
585:   }

587:   if (version == 1) {
588:     MatCreateSNESMF(snes,&J);
589:     MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);
590:     MatSetFromOptions(J);
591:   } else if (version == 2) {
592:     if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
593: #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) && !defined(PETSC_USE_REAL___FP16)
594:     SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);
595: #else
596:     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator routines (version 2)");
597: #endif
598:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator routines, only version 1 and 2");

600:   /* attach any user provided null space that was on Amat to the newly created matrix free matrix */
601:   if (snes->jacobian) {
602:     MatGetNullSpace(snes->jacobian,&nullsp);
603:     if (nullsp) {
604:       MatSetNullSpace(J,nullsp);
605:     }
606:   }

608:   PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);
609:   if (hasOperator) {

611:     /* This version replaces the user provided Jacobian matrix with a
612:        matrix-free version but still employs the user-provided preconditioner matrix. */
613:     SNESSetJacobian(snes,J,NULL,NULL,NULL);
614:   } else {
615:     /* This version replaces both the user-provided Jacobian and the user-
616:      provided preconditioner Jacobian with the default matrix free version. */
617:     if ((snes->npcside== PC_LEFT) && snes->npc) {
618:       if (!snes->jacobian){SNESSetJacobian(snes,J,NULL,NULL,NULL);}
619:     } else {
620:       KSP       ksp;
621:       PC        pc;
622:       PetscBool match;

624:       SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,NULL);
625:       /* Force no preconditioner */
626:       SNESGetKSP(snes,&ksp);
627:       KSPGetPC(ksp,&pc);
628:       PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&match);
629:       if (!match) {
630:         PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");
631:         PCSetType(pc,PCNONE);
632:       }
633:     }
634:   }
635:   MatDestroy(&J);
636:   return(0);
637: }

639: static PetscErrorCode DMRestrictHook_SNESVecSol(DM dmfine,Mat Restrict,Vec Rscale,Mat Inject,DM dmcoarse,void *ctx)
640: {
641:   SNES           snes = (SNES)ctx;
643:   Vec            Xfine,Xfine_named = NULL,Xcoarse;

646:   if (PetscLogPrintInfo) {
647:     PetscInt finelevel,coarselevel,fineclevel,coarseclevel;
648:     DMGetRefineLevel(dmfine,&finelevel);
649:     DMGetCoarsenLevel(dmfine,&fineclevel);
650:     DMGetRefineLevel(dmcoarse,&coarselevel);
651:     DMGetCoarsenLevel(dmcoarse,&coarseclevel);
652:     PetscInfo4(dmfine,"Restricting SNES solution vector from level %D-%D to level %D-%D\n",finelevel,fineclevel,coarselevel,coarseclevel);
653:   }
654:   if (dmfine == snes->dm) Xfine = snes->vec_sol;
655:   else {
656:     DMGetNamedGlobalVector(dmfine,"SNESVecSol",&Xfine_named);
657:     Xfine = Xfine_named;
658:   }
659:   DMGetNamedGlobalVector(dmcoarse,"SNESVecSol",&Xcoarse);
660:   if (Inject) {
661:     MatRestrict(Inject,Xfine,Xcoarse);
662:   } else {
663:     MatRestrict(Restrict,Xfine,Xcoarse);
664:     VecPointwiseMult(Xcoarse,Xcoarse,Rscale);
665:   }
666:   DMRestoreNamedGlobalVector(dmcoarse,"SNESVecSol",&Xcoarse);
667:   if (Xfine_named) {DMRestoreNamedGlobalVector(dmfine,"SNESVecSol",&Xfine_named);}
668:   return(0);
669: }

671: static PetscErrorCode DMCoarsenHook_SNESVecSol(DM dm,DM dmc,void *ctx)
672: {

676:   DMCoarsenHookAdd(dmc,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,ctx);
677:   return(0);
678: }

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

692:   dmsave = snes->dm;
693:   KSPGetDM(ksp,&snes->dm);
694:   if (dmsave == snes->dm) X = snes->vec_sol; /* We are on the finest level */
695:   else {                                     /* We are on a coarser level, this vec was initialized using a DM restrict hook */
696:     DMGetNamedGlobalVector(snes->dm,"SNESVecSol",&Xnamed);
697:     X    = Xnamed;
698:     SNESGetJacobian(snes,NULL,NULL,&jac,&ctxsave);
699:     /* If the DM's don't match up, the MatFDColoring context needed for the jacobian won't match up either -- fixit. */
700:     if (jac == SNESComputeJacobianDefaultColor) {
701:       SNESSetJacobian(snes,NULL,NULL,SNESComputeJacobianDefaultColor,NULL);
702:     }
703:   }
704:   /* Make sure KSP DM has the Jacobian computation routine */
705:   {
706:     DMSNES sdm;

708:     DMGetDMSNES(snes->dm, &sdm);
709:     if (!sdm->ops->computejacobian) {
710:       DMCopyDMSNES(dmsave, snes->dm);
711:     }
712:   }
713:   /* Compute the operators */
714:   SNESComputeJacobian(snes,X,A,B);
715:   /* Put the previous context back */
716:   if (snes->dm != dmsave && jac == SNESComputeJacobianDefaultColor) {
717:     SNESSetJacobian(snes,NULL,NULL,jac,ctxsave);
718:   }

720:   if (Xnamed) {DMRestoreNamedGlobalVector(snes->dm,"SNESVecSol",&Xnamed);}
721:   snes->dm = dmsave;
722:   return(0);
723: }

725: /*@
726:    SNESSetUpMatrices - ensures that matrices are available for SNES, to be called by SNESSetUp_XXX()

728:    Collective

730:    Input Arguments:
731: .  snes - snes to configure

733:    Level: developer

735: .seealso: SNESSetUp()
736: @*/
737: PetscErrorCode SNESSetUpMatrices(SNES snes)
738: {
740:   DM             dm;
741:   DMSNES         sdm;

744:   SNESGetDM(snes,&dm);
745:   DMGetDMSNES(dm,&sdm);
746:   if (!snes->jacobian && snes->mf) {
747:     Mat  J;
748:     void *functx;
749:     MatCreateSNESMF(snes,&J);
750:     MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);
751:     MatSetFromOptions(J);
752:     SNESGetFunction(snes,NULL,NULL,&functx);
753:     SNESSetJacobian(snes,J,J,NULL,NULL);
754:     MatDestroy(&J);
755:   } else if (snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) {
756:     Mat J,B;
757:     MatCreateSNESMF(snes,&J);
758:     MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);
759:     MatSetFromOptions(J);
760:     DMCreateMatrix(snes->dm,&B);
761:     /* sdm->computejacobian was already set to reach here */
762:     SNESSetJacobian(snes,J,B,NULL,NULL);
763:     MatDestroy(&J);
764:     MatDestroy(&B);
765:   } else if (!snes->jacobian_pre) {
766:     PetscDS   prob;
767:     Mat       J, B;
768:     PetscBool hasPrec   = PETSC_FALSE;

770:     J    = snes->jacobian;
771:     DMGetDS(dm, &prob);
772:     if (prob) {PetscDSHasJacobianPreconditioner(prob, &hasPrec);}
773:     if (J)            {PetscObjectReference((PetscObject) J);}
774:     else if (hasPrec) {DMCreateMatrix(snes->dm, &J);}
775:     DMCreateMatrix(snes->dm, &B);
776:     SNESSetJacobian(snes, J ? J : B, B, NULL, NULL);
777:     MatDestroy(&J);
778:     MatDestroy(&B);
779:   }
780:   {
781:     KSP ksp;
782:     SNESGetKSP(snes,&ksp);
783:     KSPSetComputeOperators(ksp,KSPComputeOperators_SNES,snes);
784:     DMCoarsenHookAdd(snes->dm,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,snes);
785:   }
786:   return(0);
787: }

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

792:    Collective on SNES

794:    Input Parameters:
795: +  snes - SNES object you wish to monitor
796: .  name - the monitor type one is seeking
797: .  help - message indicating what monitoring is done
798: .  manual - manual page for the monitor
799: .  monitor - the monitor function
800: -  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

802:    Level: developer

804: .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
805:           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
806:           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
807:           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
808:           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
809:           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
810:           PetscOptionsFList(), PetscOptionsEList()
811: @*/
812: PetscErrorCode  SNESMonitorSetFromOptions(SNES snes,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(SNES,PetscViewerAndFormat*))
813: {
814:   PetscErrorCode    ierr;
815:   PetscViewer       viewer;
816:   PetscViewerFormat format;
817:   PetscBool         flg;

820:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,name,&viewer,&format,&flg);
821:   if (flg) {
822:     PetscViewerAndFormat *vf;
823:     PetscViewerAndFormatCreate(viewer,format,&vf);
824:     PetscObjectDereference((PetscObject)viewer);
825:     if (monitorsetup) {
826:       (*monitorsetup)(snes,vf);
827:     }
828:     SNESMonitorSet(snes,(PetscErrorCode (*)(SNES,PetscInt,PetscReal,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);
829:   }
830:   return(0);
831: }

833: /*@
834:    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.

836:    Collective on SNES

838:    Input Parameter:
839: .  snes - the SNES context

841:    Options Database Keys:
842: +  -snes_type <type> - newtonls, newtontr, ngmres, ncg, nrichardson, qn, vi, fas, SNESType for complete list
843: .  -snes_stol - convergence tolerance in terms of the norm
844:                 of the change in the solution between steps
845: .  -snes_atol <abstol> - absolute tolerance of residual norm
846: .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
847: .  -snes_divergence_tolerance <divtol> - if the residual goes above divtol*rnorm0, exit with divergence
848: .  -snes_force_iteration <force> - force SNESSolve() to take at least one iteration
849: .  -snes_max_it <max_it> - maximum number of iterations
850: .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
851: .  -snes_max_fail <max_fail> - maximum number of line search failures allowed before stopping, default is none
852: .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
853: .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
854: .  -snes_lag_preconditioner_persists <true,false> - retains the -snes_lag_preconditioner information across multiple SNESSolve()
855: .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
856: .  -snes_lag_jacobian_persists <true,false> - retains the -snes_lag_jacobian information across multiple SNESSolve()
857: .  -snes_trtol <trtol> - trust region tolerance
858: .  -snes_no_convergence_test - skip convergence test in nonlinear
859:                                solver; hence iterations will continue until max_it
860:                                or some other criterion is reached. Saves expense
861:                                of convergence test
862: .  -snes_monitor [ascii][:filename][:viewer format] - prints residual norm at each iteration. if no filename given prints to stdout
863: .  -snes_monitor_solution [ascii binary draw][:filename][:viewer format] - plots solution at each iteration
864: .  -snes_monitor_residual [ascii binary draw][:filename][:viewer format] - plots residual (not its norm) at each iteration
865: .  -snes_monitor_solution_update [ascii binary draw][:filename][:viewer format] - plots update to solution at each iteration
866: .  -snes_monitor_lg_residualnorm - plots residual norm at each iteration
867: .  -snes_monitor_lg_range - plots residual norm at each iteration
868: .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
869: .  -snes_fd_color - use finite differences with coloring to compute Jacobian
870: .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
871: .  -snes_converged_reason - print the reason for convergence/divergence after each solve
872: -  -npc_snes_type <type> - the SNES type to use as a nonlinear preconditioner

874:     Options Database for Eisenstat-Walker method:
875: +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
876: .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
877: .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
878: .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
879: .  -snes_ksp_ew_gamma <gamma> - Sets gamma
880: .  -snes_ksp_ew_alpha <alpha> - Sets alpha
881: .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
882: -  -snes_ksp_ew_threshold <threshold> - Sets threshold

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

887:    Notes:
888:       SNES supports three approaches for computing (approximate) Jacobians: user provided via SNESSetJacobian(), matrix free, and computing explictly with
889:       finite differences and coloring using MatFDColoring. It is also possible to use automatic differentiation and the MatFDColoring object.

891:    Level: beginner

893: .seealso: SNESSetOptionsPrefix(), SNESResetFromOptions(), SNES, SNESCreate()
894: @*/
895: PetscErrorCode  SNESSetFromOptions(SNES snes)
896: {
897:   PetscBool      flg,pcset,persist,set;
898:   PetscInt       i,indx,lag,grids;
899:   const char     *deft        = SNESNEWTONLS;
900:   const char     *convtests[] = {"default","skip","correct_pressure"};
901:   SNESKSPEW      *kctx        = NULL;
902:   char           type[256], monfilename[PETSC_MAX_PATH_LEN];
904:   PCSide         pcside;
905:   const char     *optionsprefix;

909:   SNESRegisterAll();
910:   PetscObjectOptionsBegin((PetscObject)snes);
911:   if (((PetscObject)snes)->type_name) deft = ((PetscObject)snes)->type_name;
912:   PetscOptionsFList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);
913:   if (flg) {
914:     SNESSetType(snes,type);
915:   } else if (!((PetscObject)snes)->type_name) {
916:     SNESSetType(snes,deft);
917:   }
918:   PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->stol,&snes->stol,NULL);
919:   PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,NULL);

921:   PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,NULL);
922:   PetscOptionsReal("-snes_divergence_tolerance","Stop if residual norm increases by this factor","SNESSetDivergenceTolerance",snes->divtol,&snes->divtol,NULL);
923:   PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,NULL);
924:   PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,NULL);
925:   PetscOptionsInt("-snes_max_fail","Maximum nonlinear step failures","SNESSetMaxNonlinearStepFailures",snes->maxFailures,&snes->maxFailures,NULL);
926:   PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,NULL);
927:   PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,NULL);
928:   PetscOptionsBool("-snes_force_iteration","Force SNESSolve() to take at least one iteration","SNESSetForceIteration",snes->forceiteration,&snes->forceiteration,NULL);
929:   PetscOptionsBool("-snes_check_jacobian_domain_error","Check Jacobian domain error after Jacobian evaluation","SNESCheckJacobianDomainError",snes->checkjacdomainerror,&snes->checkjacdomainerror,NULL);

931:   PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);
932:   if (flg) {
933:     if (lag == -1) SETERRQ(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");
934:     SNESSetLagPreconditioner(snes,lag);
935:   }
936:   PetscOptionsBool("-snes_lag_preconditioner_persists","Preconditioner lagging through multiple SNES solves","SNESSetLagPreconditionerPersists",snes->lagjac_persist,&persist,&flg);
937:   if (flg) {
938:     SNESSetLagPreconditionerPersists(snes,persist);
939:   }
940:   PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);
941:   if (flg) {
942:     if (lag == -1) SETERRQ(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");
943:     SNESSetLagJacobian(snes,lag);
944:   }
945:   PetscOptionsBool("-snes_lag_jacobian_persists","Jacobian lagging through multiple SNES solves","SNESSetLagJacobianPersists",snes->lagjac_persist,&persist,&flg);
946:   if (flg) {
947:     SNESSetLagJacobianPersists(snes,persist);
948:   }

950:   PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);
951:   if (flg) {
952:     SNESSetGridSequence(snes,grids);
953:   }

955:   PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,sizeof(convtests)/sizeof(char*),"default",&indx,&flg);
956:   if (flg) {
957:     switch (indx) {
958:     case 0: SNESSetConvergenceTest(snes,SNESConvergedDefault,NULL,NULL); break;
959:     case 1: SNESSetConvergenceTest(snes,SNESConvergedSkip,NULL,NULL); break;
960:     case 2: SNESSetConvergenceTest(snes,SNESConvergedCorrectPressure,NULL,NULL); break;
961:     }
962:   }

964:   PetscOptionsEList("-snes_norm_schedule","SNES Norm schedule","SNESSetNormSchedule",SNESNormSchedules,5,"function",&indx,&flg);
965:   if (flg) { SNESSetNormSchedule(snes,(SNESNormSchedule)indx); }

967:   PetscOptionsEList("-snes_function_type","SNES Norm schedule","SNESSetFunctionType",SNESFunctionTypes,2,"unpreconditioned",&indx,&flg);
968:   if (flg) { SNESSetFunctionType(snes,(SNESFunctionType)indx); }

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

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

974:   PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,NULL);
975:   PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,NULL);
976:   PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,NULL);
977:   PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,NULL);
978:   PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,NULL);
979:   PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,NULL);
980:   PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,NULL);

982:   flg  = PETSC_FALSE;
983:   PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,&set);
984:   if (set && flg) {SNESMonitorCancel(snes);}

986:   SNESMonitorSetFromOptions(snes,"-snes_monitor","Monitor norm of function","SNESMonitorDefault",SNESMonitorDefault,NULL);
987:   SNESMonitorSetFromOptions(snes,"-snes_monitor_short","Monitor norm of function with fewer digits","SNESMonitorDefaultShort",SNESMonitorDefaultShort,NULL);
988:   SNESMonitorSetFromOptions(snes,"-snes_monitor_range","Monitor range of elements of function","SNESMonitorRange",SNESMonitorRange,NULL);

990:   SNESMonitorSetFromOptions(snes,"-snes_monitor_ratio","Monitor ratios of the norm of function for consecutive steps","SNESMonitorRatio",SNESMonitorRatio,SNESMonitorRatioSetUp);
991:   SNESMonitorSetFromOptions(snes,"-snes_monitor_field","Monitor norm of function (split into fields)","SNESMonitorDefaultField",SNESMonitorDefaultField,NULL);
992:   SNESMonitorSetFromOptions(snes,"-snes_monitor_solution","View solution at each iteration","SNESMonitorSolution",SNESMonitorSolution,NULL);
993:   SNESMonitorSetFromOptions(snes,"-snes_monitor_solution_update","View correction at each iteration","SNESMonitorSolutionUpdate",SNESMonitorSolutionUpdate,NULL);
994:   SNESMonitorSetFromOptions(snes,"-snes_monitor_residual","View residual at each iteration","SNESMonitorResidual",SNESMonitorResidual,NULL);
995:   SNESMonitorSetFromOptions(snes,"-snes_monitor_jacupdate_spectrum","Print the change in the spectrum of the Jacobian","SNESMonitorJacUpdateSpectrum",SNESMonitorJacUpdateSpectrum,NULL);
996:   SNESMonitorSetFromOptions(snes,"-snes_monitor_fields","Monitor norm of function per field","SNESMonitorSet",SNESMonitorFields,NULL);

998:   PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",NULL,monfilename,sizeof(monfilename),&flg);
999:   if (flg) {PetscPythonMonitorSet((PetscObject)snes,monfilename);}

1001:   flg  = PETSC_FALSE;
1002:   PetscOptionsBool("-snes_monitor_lg_residualnorm","Plot function norm at each iteration","SNESMonitorLGResidualNorm",flg,&flg,NULL);
1003:   if (flg) {
1004:     PetscDrawLG ctx;

1006:     SNESMonitorLGCreate(PetscObjectComm((PetscObject)snes),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&ctx);
1007:     SNESMonitorSet(snes,SNESMonitorLGResidualNorm,ctx,(PetscErrorCode (*)(void**))PetscDrawLGDestroy);
1008:   }
1009:   flg  = PETSC_FALSE;
1010:   PetscOptionsBool("-snes_monitor_lg_range","Plot function range at each iteration","SNESMonitorLGRange",flg,&flg,NULL);
1011:   if (flg) {
1012:     PetscViewer ctx;

1014:     PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&ctx);
1015:     SNESMonitorSet(snes,SNESMonitorLGRange,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);
1016:   }

1018:   flg  = PETSC_FALSE;
1019:   PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESComputeJacobianDefault",flg,&flg,NULL);
1020:   if (flg) {
1021:     void    *functx;
1022:     DM      dm;
1023:     DMSNES  sdm;
1024:     SNESGetDM(snes,&dm);
1025:     DMGetDMSNES(dm,&sdm);
1026:     sdm->jacobianctx = NULL;
1027:     SNESGetFunction(snes,NULL,NULL,&functx);
1028:     SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESComputeJacobianDefault,functx);
1029:     PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");
1030:   }

1032:   flg  = PETSC_FALSE;
1033:   PetscOptionsBool("-snes_fd_function","Use finite differences (slow) to compute function from user objective","SNESObjectiveComputeFunctionDefaultFD",flg,&flg,NULL);
1034:   if (flg) {
1035:     SNESSetFunction(snes,NULL,SNESObjectiveComputeFunctionDefaultFD,NULL);
1036:   }

1038:   flg  = PETSC_FALSE;
1039:   PetscOptionsBool("-snes_fd_color","Use finite differences with coloring to compute Jacobian","SNESComputeJacobianDefaultColor",flg,&flg,NULL);
1040:   if (flg) {
1041:     DM             dm;
1042:     DMSNES         sdm;
1043:     SNESGetDM(snes,&dm);
1044:     DMGetDMSNES(dm,&sdm);
1045:     sdm->jacobianctx = NULL;
1046:     SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESComputeJacobianDefaultColor,NULL);
1047:     PetscInfo(snes,"Setting default finite difference coloring Jacobian matrix\n");
1048:   }

1050:   flg  = PETSC_FALSE;
1051:   PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","SNESSetUseMatrixFree",PETSC_FALSE,&snes->mf_operator,&flg);
1052:   if (flg && snes->mf_operator) {
1053:     snes->mf_operator = PETSC_TRUE;
1054:     snes->mf          = PETSC_TRUE;
1055:   }
1056:   flg  = PETSC_FALSE;
1057:   PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","SNESSetUseMatrixFree",PETSC_FALSE,&snes->mf,&flg);
1058:   if (!flg && snes->mf_operator) snes->mf = PETSC_TRUE;
1059:   PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",snes->mf_version,&snes->mf_version,NULL);

1061:   flg  = PETSC_FALSE;
1062:   SNESGetNPCSide(snes,&pcside);
1063:   PetscOptionsEnum("-snes_npc_side","SNES nonlinear preconditioner side","SNESSetNPCSide",PCSides,(PetscEnum)pcside,(PetscEnum*)&pcside,&flg);
1064:   if (flg) {SNESSetNPCSide(snes,pcside);}

1066: #if defined(PETSC_HAVE_SAWS)
1067:   /*
1068:     Publish convergence information using SAWs
1069:   */
1070:   flg  = PETSC_FALSE;
1071:   PetscOptionsBool("-snes_monitor_saws","Publish SNES progress using SAWs","SNESMonitorSet",flg,&flg,NULL);
1072:   if (flg) {
1073:     void *ctx;
1074:     SNESMonitorSAWsCreate(snes,&ctx);
1075:     SNESMonitorSet(snes,SNESMonitorSAWs,ctx,SNESMonitorSAWsDestroy);
1076:   }
1077: #endif
1078: #if defined(PETSC_HAVE_SAWS)
1079:   {
1080:   PetscBool set;
1081:   flg  = PETSC_FALSE;
1082:   PetscOptionsBool("-snes_saws_block","Block for SAWs at end of SNESSolve","PetscObjectSAWsBlock",((PetscObject)snes)->amspublishblock,&flg,&set);
1083:   if (set) {
1084:     PetscObjectSAWsSetBlock((PetscObject)snes,flg);
1085:   }
1086:   }
1087: #endif

1089:   for (i = 0; i < numberofsetfromoptions; i++) {
1090:     (*othersetfromoptions[i])(snes);
1091:   }

1093:   if (snes->ops->setfromoptions) {
1094:     (*snes->ops->setfromoptions)(PetscOptionsObject,snes);
1095:   }

1097:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
1098:   PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)snes);
1099:   PetscOptionsEnd();

1101:   if (snes->linesearch) {
1102:     SNESGetLineSearch(snes, &snes->linesearch);
1103:     SNESLineSearchSetFromOptions(snes->linesearch);
1104:   }

1106:   if (snes->usesksp) {
1107:     if (!snes->ksp) {SNESGetKSP(snes,&snes->ksp);}
1108:     KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre);
1109:     KSPSetFromOptions(snes->ksp);
1110:   }

1112:   /* if user has set the SNES NPC type via options database, create it. */
1113:   SNESGetOptionsPrefix(snes, &optionsprefix);
1114:   PetscOptionsHasName(((PetscObject)snes)->options,optionsprefix, "-npc_snes_type", &pcset);
1115:   if (pcset && (!snes->npc)) {
1116:     SNESGetNPC(snes, &snes->npc);
1117:   }
1118:   if (snes->npc) {
1119:     SNESSetFromOptions(snes->npc);
1120:   }
1121:   snes->setfromoptionscalled++;
1122:   return(0);
1123: }

1125: /*@
1126:    SNESResetFromOptions - Sets various SNES and KSP parameters from user options ONLY if the SNES was previously set from options

1128:    Collective on SNES

1130:    Input Parameter:
1131: .  snes - the SNES context

1133:    Level: beginner

1135: .seealso: SNESSetFromOptions(), SNESSetOptionsPrefix()
1136: @*/
1137: PetscErrorCode SNESResetFromOptions(SNES snes)
1138: {

1142:   if (snes->setfromoptionscalled) {SNESSetFromOptions(snes);}
1143:   return(0);
1144: }

1146: /*@C
1147:    SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for
1148:    the nonlinear solvers.

1150:    Logically Collective on SNES

1152:    Input Parameters:
1153: +  snes - the SNES context
1154: .  compute - function to compute the context
1155: -  destroy - function to destroy the context

1157:    Level: intermediate

1159:    Notes:
1160:    This function is currently not available from Fortran.

1162: .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext()
1163: @*/
1164: PetscErrorCode  SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**))
1165: {
1168:   snes->ops->usercompute = compute;
1169:   snes->ops->userdestroy = destroy;
1170:   return(0);
1171: }

1173: /*@
1174:    SNESSetApplicationContext - Sets the optional user-defined context for
1175:    the nonlinear solvers.

1177:    Logically Collective on SNES

1179:    Input Parameters:
1180: +  snes - the SNES context
1181: -  usrP - optional user context

1183:    Level: intermediate

1185:    Fortran Notes:
1186:     To use this from Fortran you must write a Fortran interface definition for this
1187:     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.

1189: .seealso: SNESGetApplicationContext()
1190: @*/
1191: PetscErrorCode  SNESSetApplicationContext(SNES snes,void *usrP)
1192: {
1194:   KSP            ksp;

1198:   SNESGetKSP(snes,&ksp);
1199:   KSPSetApplicationContext(ksp,usrP);
1200:   snes->user = usrP;
1201:   return(0);
1202: }

1204: /*@
1205:    SNESGetApplicationContext - Gets the user-defined context for the
1206:    nonlinear solvers.

1208:    Not Collective

1210:    Input Parameter:
1211: .  snes - SNES context

1213:    Output Parameter:
1214: .  usrP - user context

1216:    Fortran Notes:
1217:     To use this from Fortran you must write a Fortran interface definition for this
1218:     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.

1220:    Level: intermediate

1222: .seealso: SNESSetApplicationContext()
1223: @*/
1224: PetscErrorCode  SNESGetApplicationContext(SNES snes,void *usrP)
1225: {
1228:   *(void**)usrP = snes->user;
1229:   return(0);
1230: }

1232: /*@
1233:    SNESSetUseMatrixFree - indicates that SNES should use matrix free finite difference matrix vector products internally to apply the Jacobian.

1235:    Collective on SNES

1237:    Input Parameters:
1238: +  snes - SNES context
1239: .  mf_operator - use matrix-free only for the Amat used by SNESSetJacobian(), this means the user provided Pmat will continue to be used
1240: -  mf - use matrix-free for both the Amat and Pmat used by SNESSetJacobian(), both the Amat and Pmat set in SNESSetJacobian() will be ignored

1242:    Options Database:
1243: + -snes_mf - use matrix free for both the mat and pmat operator
1244: . -snes_mf_operator - use matrix free only for the mat operator
1245: . -snes_fd_color - compute the Jacobian via coloring and finite differences.
1246: - -snes_fd - compute the Jacobian via finite differences (slow)

1248:    Level: intermediate

1250:    Notes:
1251:       SNES supports three approaches for computing (approximate) Jacobians: user provided via SNESSetJacobian(), matrix free, and computing explictly with
1252:       finite differences and coloring using MatFDColoring. It is also possible to use automatic differentiation and the MatFDColoring object.

1254: .seealso:   SNESGetUseMatrixFree(), MatCreateSNESMF(), SNESComputeJacobianDefaultColor()
1255: @*/
1256: PetscErrorCode  SNESSetUseMatrixFree(SNES snes,PetscBool mf_operator,PetscBool mf)
1257: {
1262:   snes->mf          = mf_operator ? PETSC_TRUE : mf;
1263:   snes->mf_operator = mf_operator;
1264:   return(0);
1265: }

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

1270:    Collective on SNES

1272:    Input Parameter:
1273: .  snes - SNES context

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

1279:    Options Database:
1280: + -snes_mf - use matrix free for both the mat and pmat operator
1281: - -snes_mf_operator - use matrix free only for the mat operator

1283:    Level: intermediate

1285: .seealso:   SNESSetUseMatrixFree(), MatCreateSNESMF()
1286: @*/
1287: PetscErrorCode  SNESGetUseMatrixFree(SNES snes,PetscBool *mf_operator,PetscBool *mf)
1288: {
1291:   if (mf)          *mf          = snes->mf;
1292:   if (mf_operator) *mf_operator = snes->mf_operator;
1293:   return(0);
1294: }

1296: /*@
1297:    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
1298:    at this time.

1300:    Not Collective

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

1305:    Output Parameter:
1306: .  iter - iteration number

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

1311:    This is useful for using lagged Jacobians (where one does not recompute the
1312:    Jacobian at each SNES iteration). For example, the code
1313: .vb
1314:       SNESGetIterationNumber(snes,&it);
1315:       if (!(it % 2)) {
1316:         [compute Jacobian here]
1317:       }
1318: .ve
1319:    can be used in your ComputeJacobian() function to cause the Jacobian to be
1320:    recomputed every second SNES iteration.

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

1324:    Level: intermediate

1326: .seealso:   SNESGetLinearSolveIterations()
1327: @*/
1328: PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt *iter)
1329: {
1333:   *iter = snes->iter;
1334:   return(0);
1335: }

1337: /*@
1338:    SNESSetIterationNumber - Sets the current iteration number.

1340:    Not Collective

1342:    Input Parameter:
1343: +  snes - SNES context
1344: -  iter - iteration number

1346:    Level: developer

1348: .seealso:   SNESGetLinearSolveIterations()
1349: @*/
1350: PetscErrorCode  SNESSetIterationNumber(SNES snes,PetscInt iter)
1351: {

1356:   PetscObjectSAWsTakeAccess((PetscObject)snes);
1357:   snes->iter = iter;
1358:   PetscObjectSAWsGrantAccess((PetscObject)snes);
1359:   return(0);
1360: }

1362: /*@
1363:    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
1364:    attempted by the nonlinear solver.

1366:    Not Collective

1368:    Input Parameter:
1369: .  snes - SNES context

1371:    Output Parameter:
1372: .  nfails - number of unsuccessful steps attempted

1374:    Notes:
1375:    This counter is reset to zero for each successive call to SNESSolve().

1377:    Level: intermediate

1379: .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
1380:           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
1381: @*/
1382: PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt *nfails)
1383: {
1387:   *nfails = snes->numFailures;
1388:   return(0);
1389: }

1391: /*@
1392:    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
1393:    attempted by the nonlinear solver before it gives up.

1395:    Not Collective

1397:    Input Parameters:
1398: +  snes     - SNES context
1399: -  maxFails - maximum of unsuccessful steps

1401:    Level: intermediate

1403: .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
1404:           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
1405: @*/
1406: PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
1407: {
1410:   snes->maxFailures = maxFails;
1411:   return(0);
1412: }

1414: /*@
1415:    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
1416:    attempted by the nonlinear solver before it gives up.

1418:    Not Collective

1420:    Input Parameter:
1421: .  snes     - SNES context

1423:    Output Parameter:
1424: .  maxFails - maximum of unsuccessful steps

1426:    Level: intermediate

1428: .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
1429:           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()

1431: @*/
1432: PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
1433: {
1437:   *maxFails = snes->maxFailures;
1438:   return(0);
1439: }

1441: /*@
1442:    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
1443:      done by SNES.

1445:    Not Collective

1447:    Input Parameter:
1448: .  snes     - SNES context

1450:    Output Parameter:
1451: .  nfuncs - number of evaluations

1453:    Level: intermediate

1455:    Notes:
1456:     Reset every time SNESSolve is called unless SNESSetCountersReset() is used.

1458: .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), SNESSetCountersReset()
1459: @*/
1460: PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
1461: {
1465:   *nfuncs = snes->nfuncs;
1466:   return(0);
1467: }

1469: /*@
1470:    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
1471:    linear solvers.

1473:    Not Collective

1475:    Input Parameter:
1476: .  snes - SNES context

1478:    Output Parameter:
1479: .  nfails - number of failed solves

1481:    Level: intermediate

1483:    Options Database Keys:
1484: . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated

1486:    Notes:
1487:    This counter is reset to zero for each successive call to SNESSolve().

1489: .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
1490: @*/
1491: PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt *nfails)
1492: {
1496:   *nfails = snes->numLinearSolveFailures;
1497:   return(0);
1498: }

1500: /*@
1501:    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
1502:    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE

1504:    Logically Collective on SNES

1506:    Input Parameters:
1507: +  snes     - SNES context
1508: -  maxFails - maximum allowed linear solve failures

1510:    Level: intermediate

1512:    Options Database Keys:
1513: . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated

1515:    Notes:
1516:     By default this is 0; that is SNES returns on the first failed linear solve

1518: .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
1519: @*/
1520: PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
1521: {
1525:   snes->maxLinearSolveFailures = maxFails;
1526:   return(0);
1527: }

1529: /*@
1530:    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
1531:      are allowed before SNES terminates

1533:    Not Collective

1535:    Input Parameter:
1536: .  snes     - SNES context

1538:    Output Parameter:
1539: .  maxFails - maximum of unsuccessful solves allowed

1541:    Level: intermediate

1543:    Notes:
1544:     By default this is 1; that is SNES returns on the first failed linear solve

1546: .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
1547: @*/
1548: PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
1549: {
1553:   *maxFails = snes->maxLinearSolveFailures;
1554:   return(0);
1555: }

1557: /*@
1558:    SNESGetLinearSolveIterations - Gets the total number of linear iterations
1559:    used by the nonlinear solver.

1561:    Not Collective

1563:    Input Parameter:
1564: .  snes - SNES context

1566:    Output Parameter:
1567: .  lits - number of linear iterations

1569:    Notes:
1570:    This counter is reset to zero for each successive call to SNESSolve() unless SNESSetCountersReset() is used.

1572:    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
1573:    then call KSPGetIterationNumber() after the failed solve.

1575:    Level: intermediate

1577: .seealso:  SNESGetIterationNumber(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESSetCountersReset()
1578: @*/
1579: PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt *lits)
1580: {
1584:   *lits = snes->linear_its;
1585:   return(0);
1586: }

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

1592:    Logically Collective on SNES

1594:    Input Parameter:
1595: +  snes - SNES context
1596: -  reset - whether to reset the counters or not

1598:    Notes:
1599:    This defaults to PETSC_TRUE

1601:    Level: developer

1603: .seealso:  SNESGetNumberFunctionEvals(), SNESGetLinearSolveIterations(), SNESGetNPC()
1604: @*/
1605: PetscErrorCode  SNESSetCountersReset(SNES snes,PetscBool reset)
1606: {
1610:   snes->counters_reset = reset;
1611:   return(0);
1612: }


1615: /*@
1616:    SNESSetKSP - Sets a KSP context for the SNES object to use

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

1620:    Input Parameters:
1621: +  snes - the SNES context
1622: -  ksp - the KSP context

1624:    Notes:
1625:    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
1626:    so this routine is rarely needed.

1628:    The KSP object that is already in the SNES object has its reference count
1629:    decreased by one.

1631:    Level: developer

1633: .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
1634: @*/
1635: PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
1636: {

1643:   PetscObjectReference((PetscObject)ksp);
1644:   if (snes->ksp) {PetscObjectDereference((PetscObject)snes->ksp);}
1645:   snes->ksp = ksp;
1646:   return(0);
1647: }

1649: /* -----------------------------------------------------------*/
1650: /*@
1651:    SNESCreate - Creates a nonlinear solver context.

1653:    Collective

1655:    Input Parameters:
1656: .  comm - MPI communicator

1658:    Output Parameter:
1659: .  outsnes - the new SNES context

1661:    Options Database Keys:
1662: +   -snes_mf - Activates default matrix-free Jacobian-vector products,
1663:                and no preconditioning matrix
1664: .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
1665:                products, and a user-provided preconditioning matrix
1666:                as set by SNESSetJacobian()
1667: -   -snes_fd - Uses (slow!) finite differences to compute Jacobian

1669:    Level: beginner

1671:    Developer Notes:
1672:     SNES always creates a KSP object even though many SNES methods do not use it. This is
1673:                     unfortunate and should be fixed at some point. The flag snes->usesksp indicates if the
1674:                     particular method does use KSP and regulates if the information about the KSP is printed
1675:                     in SNESView(). TSSetFromOptions() does call SNESSetFromOptions() which can lead to users being confused
1676:                     by help messages about meaningless SNES options.

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

1681: .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner(), SNESSetLagJacobian()

1683: @*/
1684: PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
1685: {
1687:   SNES           snes;
1688:   SNESKSPEW      *kctx;

1692:   *outsnes = NULL;
1693:   SNESInitializePackage();

1695:   PetscHeaderCreate(snes,SNES_CLASSID,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);

1697:   snes->ops->converged    = SNESConvergedDefault;
1698:   snes->usesksp           = PETSC_TRUE;
1699:   snes->tolerancesset     = PETSC_FALSE;
1700:   snes->max_its           = 50;
1701:   snes->max_funcs         = 10000;
1702:   snes->norm              = 0.0;
1703:   snes->xnorm             = 0.0;
1704:   snes->ynorm             = 0.0;
1705:   snes->normschedule      = SNES_NORM_ALWAYS;
1706:   snes->functype          = SNES_FUNCTION_DEFAULT;
1707: #if defined(PETSC_USE_REAL_SINGLE)
1708:   snes->rtol              = 1.e-5;
1709: #else
1710:   snes->rtol              = 1.e-8;
1711: #endif
1712:   snes->ttol              = 0.0;
1713: #if defined(PETSC_USE_REAL_SINGLE)
1714:   snes->abstol            = 1.e-25;
1715: #else
1716:   snes->abstol            = 1.e-50;
1717: #endif
1718: #if defined(PETSC_USE_REAL_SINGLE)
1719:   snes->stol              = 1.e-5;
1720: #else
1721:   snes->stol              = 1.e-8;
1722: #endif
1723: #if defined(PETSC_USE_REAL_SINGLE)
1724:   snes->deltatol          = 1.e-6;
1725: #else
1726:   snes->deltatol          = 1.e-12;
1727: #endif
1728:   snes->divtol            = 1.e4;
1729:   snes->rnorm0            = 0;
1730:   snes->nfuncs            = 0;
1731:   snes->numFailures       = 0;
1732:   snes->maxFailures       = 1;
1733:   snes->linear_its        = 0;
1734:   snes->lagjacobian       = 1;
1735:   snes->jac_iter          = 0;
1736:   snes->lagjac_persist    = PETSC_FALSE;
1737:   snes->lagpreconditioner = 1;
1738:   snes->pre_iter          = 0;
1739:   snes->lagpre_persist    = PETSC_FALSE;
1740:   snes->numbermonitors    = 0;
1741:   snes->data              = NULL;
1742:   snes->setupcalled       = PETSC_FALSE;
1743:   snes->ksp_ewconv        = PETSC_FALSE;
1744:   snes->nwork             = 0;
1745:   snes->work              = NULL;
1746:   snes->nvwork            = 0;
1747:   snes->vwork             = NULL;
1748:   snes->conv_hist_len     = 0;
1749:   snes->conv_hist_max     = 0;
1750:   snes->conv_hist         = NULL;
1751:   snes->conv_hist_its     = NULL;
1752:   snes->conv_hist_reset   = PETSC_TRUE;
1753:   snes->counters_reset    = PETSC_TRUE;
1754:   snes->vec_func_init_set = PETSC_FALSE;
1755:   snes->reason            = SNES_CONVERGED_ITERATING;
1756:   snes->npcside           = PC_RIGHT;
1757:   snes->setfromoptionscalled = 0;

1759:   snes->mf          = PETSC_FALSE;
1760:   snes->mf_operator = PETSC_FALSE;
1761:   snes->mf_version  = 1;

1763:   snes->numLinearSolveFailures = 0;
1764:   snes->maxLinearSolveFailures = 1;

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

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

1772:   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
1773:   PetscNewLog(snes,&kctx);

1775:   snes->kspconvctx  = (void*)kctx;
1776:   kctx->version     = 2;
1777:   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
1778:                              this was too large for some test cases */
1779:   kctx->rtol_last   = 0.0;
1780:   kctx->rtol_max    = .9;
1781:   kctx->gamma       = 1.0;
1782:   kctx->alpha       = .5*(1.0 + PetscSqrtReal(5.0));
1783:   kctx->alpha2      = kctx->alpha;
1784:   kctx->threshold   = .1;
1785:   kctx->lresid_last = 0.0;
1786:   kctx->norm_last   = 0.0;

1788:   *outsnes = snes;
1789:   return(0);
1790: }

1792: /*MC
1793:     SNESFunction - Functional form used to convey the nonlinear function to be solved by SNES

1795:      Synopsis:
1796:      #include "petscsnes.h"
1797:      PetscErrorCode SNESFunction(SNES snes,Vec x,Vec f,void *ctx);

1799:      Collective on snes

1801:      Input Parameters:
1802: +     snes - the SNES context
1803: .     x    - state at which to evaluate residual
1804: -     ctx     - optional user-defined function context, passed in with SNESSetFunction()

1806:      Output Parameter:
1807: .     f  - vector to put residual (function value)

1809:    Level: intermediate

1811: .seealso:   SNESSetFunction(), SNESGetFunction()
1812: M*/

1814: /*@C
1815:    SNESSetFunction - Sets the function evaluation routine and function
1816:    vector for use by the SNES routines in solving systems of nonlinear
1817:    equations.

1819:    Logically Collective on SNES

1821:    Input Parameters:
1822: +  snes - the SNES context
1823: .  r - vector to store function value
1824: .  f - function evaluation routine; see SNESFunction for calling sequence details
1825: -  ctx - [optional] user-defined context for private data for the
1826:          function evaluation routine (may be NULL)

1828:    Notes:
1829:    The Newton-like methods typically solve linear systems of the form
1830: $      f'(x) x = -f(x),
1831:    where f'(x) denotes the Jacobian matrix and f(x) is the function.

1833:    Level: beginner

1835: .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetPicard(), SNESFunction
1836: @*/
1837: PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*f)(SNES,Vec,Vec,void*),void *ctx)
1838: {
1840:   DM             dm;

1844:   if (r) {
1847:     PetscObjectReference((PetscObject)r);
1848:     VecDestroy(&snes->vec_func);

1850:     snes->vec_func = r;
1851:   }
1852:   SNESGetDM(snes,&dm);
1853:   DMSNESSetFunction(dm,f,ctx);
1854:   return(0);
1855: }


1858: /*@C
1859:    SNESSetInitialFunction - Sets the function vector to be used as the
1860:    function norm at the initialization of the method.  In some
1861:    instances, the user has precomputed the function before calling
1862:    SNESSolve.  This function allows one to avoid a redundant call
1863:    to SNESComputeFunction in that case.

1865:    Logically Collective on SNES

1867:    Input Parameters:
1868: +  snes - the SNES context
1869: -  f - vector to store function value

1871:    Notes:
1872:    This should not be modified during the solution procedure.

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

1876:    Level: developer

1878: .seealso: SNESSetFunction(), SNESComputeFunction(), SNESSetInitialFunctionNorm()
1879: @*/
1880: PetscErrorCode  SNESSetInitialFunction(SNES snes, Vec f)
1881: {
1883:   Vec            vec_func;

1889:   if (snes->npcside== PC_LEFT && snes->functype == SNES_FUNCTION_PRECONDITIONED) {
1890:     snes->vec_func_init_set = PETSC_FALSE;
1891:     return(0);
1892:   }
1893:   SNESGetFunction(snes,&vec_func,NULL,NULL);
1894:   VecCopy(f, vec_func);

1896:   snes->vec_func_init_set = PETSC_TRUE;
1897:   return(0);
1898: }

1900: /*@
1901:    SNESSetNormSchedule - Sets the SNESNormSchedule used in covergence and monitoring
1902:    of the SNES method.

1904:    Logically Collective on SNES

1906:    Input Parameters:
1907: +  snes - the SNES context
1908: -  normschedule - the frequency of norm computation

1910:    Options Database Key:
1911: .  -snes_norm_schedule <none, always, initialonly, finalonly, initalfinalonly>

1913:    Notes:
1914:    Only certain SNES methods support certain SNESNormSchedules.  Most require evaluation
1915:    of the nonlinear function and the taking of its norm at every iteration to
1916:    even ensure convergence at all.  However, methods such as custom Gauss-Seidel methods
1917:    (SNESNGS) and the like do not require the norm of the function to be computed, and therfore
1918:    may either be monitored for convergence or not.  As these are often used as nonlinear
1919:    preconditioners, monitoring the norm of their error is not a useful enterprise within
1920:    their solution.

1922:    Level: developer

1924: .seealso: SNESGetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule
1925: @*/
1926: PetscErrorCode  SNESSetNormSchedule(SNES snes, SNESNormSchedule normschedule)
1927: {
1930:   snes->normschedule = normschedule;
1931:   return(0);
1932: }


1935: /*@
1936:    SNESGetNormSchedule - Gets the SNESNormSchedule used in covergence and monitoring
1937:    of the SNES method.

1939:    Logically Collective on SNES

1941:    Input Parameters:
1942: +  snes - the SNES context
1943: -  normschedule - the type of the norm used

1945:    Level: advanced

1947: .seealso: SNESSetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule
1948: @*/
1949: PetscErrorCode  SNESGetNormSchedule(SNES snes, SNESNormSchedule *normschedule)
1950: {
1953:   *normschedule = snes->normschedule;
1954:   return(0);
1955: }


1958: /*@
1959:   SNESSetFunctionNorm - Sets the last computed residual norm.

1961:   Logically Collective on SNES

1963:   Input Parameters:
1964: + snes - the SNES context

1966: - normschedule - the frequency of norm computation

1968:   Level: developer

1970: .seealso: SNESGetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule
1971: @*/
1972: PetscErrorCode SNESSetFunctionNorm(SNES snes, PetscReal norm)
1973: {
1976:   snes->norm = norm;
1977:   return(0);
1978: }

1980: /*@
1981:   SNESGetFunctionNorm - Gets the last computed norm of the residual

1983:   Not Collective

1985:   Input Parameter:
1986: . snes - the SNES context

1988:   Output Parameter:
1989: . norm - the last computed residual norm

1991:   Level: developer

1993: .seealso: SNESSetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule
1994: @*/
1995: PetscErrorCode SNESGetFunctionNorm(SNES snes, PetscReal *norm)
1996: {
2000:   *norm = snes->norm;
2001:   return(0);
2002: }

2004: /*@
2005:   SNESGetUpdateNorm - Gets the last computed norm of the Newton update

2007:   Not Collective

2009:   Input Parameter:
2010: . snes - the SNES context

2012:   Output Parameter:
2013: . ynorm - the last computed update norm

2015:   Level: developer

2017: .seealso: SNESSetNormSchedule(), SNESComputeFunction(), SNESGetFunctionNorm()
2018: @*/
2019: PetscErrorCode SNESGetUpdateNorm(SNES snes, PetscReal *ynorm)
2020: {
2024:   *ynorm = snes->ynorm;
2025:   return(0);
2026: }

2028: /*@
2029:   SNESGetSolutionNorm - Gets the last computed norm of the solution

2031:   Not Collective

2033:   Input Parameter:
2034: . snes - the SNES context

2036:   Output Parameter:
2037: . xnorm - the last computed solution norm

2039:   Level: developer

2041: .seealso: SNESSetNormSchedule(), SNESComputeFunction(), SNESGetFunctionNorm(), SNESGetUpdateNorm()
2042: @*/
2043: PetscErrorCode SNESGetSolutionNorm(SNES snes, PetscReal *xnorm)
2044: {
2048:   *xnorm = snes->xnorm;
2049:   return(0);
2050: }

2052: /*@C
2053:    SNESSetFunctionType - Sets the SNESNormSchedule used in covergence and monitoring
2054:    of the SNES method.

2056:    Logically Collective on SNES

2058:    Input Parameters:
2059: +  snes - the SNES context
2060: -  normschedule - the frequency of norm computation

2062:    Notes:
2063:    Only certain SNES methods support certain SNESNormSchedules.  Most require evaluation
2064:    of the nonlinear function and the taking of its norm at every iteration to
2065:    even ensure convergence at all.  However, methods such as custom Gauss-Seidel methods
2066:    (SNESNGS) and the like do not require the norm of the function to be computed, and therfore
2067:    may either be monitored for convergence or not.  As these are often used as nonlinear
2068:    preconditioners, monitoring the norm of their error is not a useful enterprise within
2069:    their solution.

2071:    Level: developer

2073: .seealso: SNESGetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule
2074: @*/
2075: PetscErrorCode  SNESSetFunctionType(SNES snes, SNESFunctionType type)
2076: {
2079:   snes->functype = type;
2080:   return(0);
2081: }


2084: /*@C
2085:    SNESGetFunctionType - Gets the SNESNormSchedule used in covergence and monitoring
2086:    of the SNES method.

2088:    Logically Collective on SNES

2090:    Input Parameters:
2091: +  snes - the SNES context
2092: -  normschedule - the type of the norm used

2094:    Level: advanced

2096: .seealso: SNESSetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule
2097: @*/
2098: PetscErrorCode  SNESGetFunctionType(SNES snes, SNESFunctionType *type)
2099: {
2102:   *type = snes->functype;
2103:   return(0);
2104: }

2106: /*MC
2107:     SNESNGSFunction - function used to convey a Gauss-Seidel sweep on the nonlinear function

2109:      Synopsis:
2110: #include <petscsnes.h>
2111: $    SNESNGSFunction(SNES snes,Vec x,Vec b,void *ctx);

2113:      Collective on snes

2115:      Input Parameters:
2116: +  X   - solution vector
2117: .  B   - RHS vector
2118: -  ctx - optional user-defined Gauss-Seidel context

2120:      Output Parameter:
2121: .  X   - solution vector

2123:    Level: intermediate

2125: .seealso:   SNESSetNGS(), SNESGetNGS()
2126: M*/

2128: /*@C
2129:    SNESSetNGS - Sets the user nonlinear Gauss-Seidel routine for
2130:    use with composed nonlinear solvers.

2132:    Input Parameters:
2133: +  snes   - the SNES context
2134: .  f - function evaluation routine to apply Gauss-Seidel see SNESNGSFunction
2135: -  ctx    - [optional] user-defined context for private data for the
2136:             smoother evaluation routine (may be NULL)

2138:    Notes:
2139:    The NGS routines are used by the composed nonlinear solver to generate
2140:     a problem appropriate update to the solution, particularly FAS.

2142:    Level: intermediate

2144: .seealso: SNESGetFunction(), SNESComputeNGS()
2145: @*/
2146: PetscErrorCode SNESSetNGS(SNES snes,PetscErrorCode (*f)(SNES,Vec,Vec,void*),void *ctx)
2147: {
2149:   DM             dm;

2153:   SNESGetDM(snes,&dm);
2154:   DMSNESSetNGS(dm,f,ctx);
2155:   return(0);
2156: }

2158: PetscErrorCode SNESPicardComputeFunction(SNES snes,Vec x,Vec f,void *ctx)
2159: {
2161:   DM             dm;
2162:   DMSNES         sdm;

2165:   SNESGetDM(snes,&dm);
2166:   DMGetDMSNES(dm,&sdm);
2167:   if (!sdm->ops->computepfunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetPicard() to provide Picard function.");
2168:   if (!sdm->ops->computepjacobian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetPicard() to provide Picard Jacobian.");
2169:   /*  A(x)*x - b(x) */
2170:   PetscStackPush("SNES Picard user function");
2171:   (*sdm->ops->computepfunction)(snes,x,f,sdm->pctx);
2172:   PetscStackPop;
2173:   PetscStackPush("SNES Picard user Jacobian");
2174:   (*sdm->ops->computepjacobian)(snes,x,snes->jacobian,snes->jacobian_pre,sdm->pctx);
2175:   PetscStackPop;
2176:   VecScale(f,-1.0);
2177:   MatMultAdd(snes->jacobian,x,f,f);
2178:   return(0);
2179: }

2181: PetscErrorCode SNESPicardComputeJacobian(SNES snes,Vec x1,Mat J,Mat B,void *ctx)
2182: {
2184:   /* the jacobian matrix should be pre-filled in SNESPicardComputeFunction */
2185:   return(0);
2186: }

2188: /*@C
2189:    SNESSetPicard - Use SNES to solve the semilinear-system A(x) x = b(x) via a Picard type iteration (Picard linearization)

2191:    Logically Collective on SNES

2193:    Input Parameters:
2194: +  snes - the SNES context
2195: .  r - vector to store function value
2196: .  b - function evaluation routine
2197: .  Amat - matrix with which A(x) x - b(x) is to be computed
2198: .  Pmat - matrix from which preconditioner is computed (usually the same as Amat)
2199: .  J  - function to compute matrix value, see SNESJacobianFunction for details on its calling sequence
2200: -  ctx - [optional] user-defined context for private data for the
2201:          function evaluation routine (may be NULL)

2203:    Notes:
2204:     We do not recomemend using this routine. It is far better to provide the nonlinear function F() and some approximation to the Jacobian and use
2205:     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.

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

2209: $     Solves the equation A(x) x = b(x) via the defect correction algorithm A(x^{n}) (x^{n+1} - x^{n}) = b(x^{n}) - A(x^{n})x^{n}
2210: $     Note that when an exact solver is used this corresponds to the "classic" Picard A(x^{n}) x^{n+1} = b(x^{n}) iteration.

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

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

2217:    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
2218:    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
2219:    different please contact us at petsc-dev@mcs.anl.gov and we'll have an entirely new argument :-).

2221:    Level: intermediate

2223: .seealso: SNESGetFunction(), SNESSetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESGetPicard(), SNESLineSearchPreCheckPicard(), SNESJacobianFunction
2224: @*/
2225: PetscErrorCode  SNESSetPicard(SNES snes,Vec r,PetscErrorCode (*b)(SNES,Vec,Vec,void*),Mat Amat, Mat Pmat, PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx)
2226: {
2228:   DM             dm;

2232:   SNESGetDM(snes, &dm);
2233:   DMSNESSetPicard(dm,b,J,ctx);
2234:   SNESSetFunction(snes,r,SNESPicardComputeFunction,ctx);
2235:   SNESSetJacobian(snes,Amat,Pmat,SNESPicardComputeJacobian,ctx);
2236:   return(0);
2237: }

2239: /*@C
2240:    SNESGetPicard - Returns the context for the Picard iteration

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

2244:    Input Parameter:
2245: .  snes - the SNES context

2247:    Output Parameter:
2248: +  r - the function (or NULL)
2249: .  f - the function (or NULL); see SNESFunction for calling sequence details
2250: .  Amat - the matrix used to defined the operation A(x) x - b(x) (or NULL)
2251: .  Pmat  - the matrix from which the preconditioner will be constructed (or NULL)
2252: .  J - the function for matrix evaluation (or NULL); see SNESJacobianFunction for calling sequence details
2253: -  ctx - the function context (or NULL)

2255:    Level: advanced

2257: .seealso: SNESSetPicard(), SNESGetFunction(), SNESGetJacobian(), SNESGetDM(), SNESFunction, SNESJacobianFunction
2258: @*/
2259: PetscErrorCode  SNESGetPicard(SNES snes,Vec *r,PetscErrorCode (**f)(SNES,Vec,Vec,void*),Mat *Amat, Mat *Pmat, PetscErrorCode (**J)(SNES,Vec,Mat,Mat,void*),void **ctx)
2260: {
2262:   DM             dm;

2266:   SNESGetFunction(snes,r,NULL,NULL);
2267:   SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);
2268:   SNESGetDM(snes,&dm);
2269:   DMSNESGetPicard(dm,f,J,ctx);
2270:   return(0);
2271: }

2273: /*@C
2274:    SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem

2276:    Logically Collective on SNES

2278:    Input Parameters:
2279: +  snes - the SNES context
2280: .  func - function evaluation routine
2281: -  ctx - [optional] user-defined context for private data for the
2282:          function evaluation routine (may be NULL)

2284:    Calling sequence of func:
2285: $    func (SNES snes,Vec x,void *ctx);

2287: .  f - function vector
2288: -  ctx - optional user-defined function context

2290:    Level: intermediate

2292: .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
2293: @*/
2294: PetscErrorCode  SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx)
2295: {
2298:   if (func) snes->ops->computeinitialguess = func;
2299:   if (ctx)  snes->initialguessP            = ctx;
2300:   return(0);
2301: }

2303: /* --------------------------------------------------------------- */
2304: /*@C
2305:    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
2306:    it assumes a zero right hand side.

2308:    Logically Collective on SNES

2310:    Input Parameter:
2311: .  snes - the SNES context

2313:    Output Parameter:
2314: .  rhs - the right hand side vector or NULL if the right hand side vector is null

2316:    Level: intermediate

2318: .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
2319: @*/
2320: PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
2321: {
2325:   *rhs = snes->vec_rhs;
2326:   return(0);
2327: }

2329: /*@
2330:    SNESComputeFunction - Calls the function that has been set with SNESSetFunction().

2332:    Collective on SNES

2334:    Input Parameters:
2335: +  snes - the SNES context
2336: -  x - input vector

2338:    Output Parameter:
2339: .  y - function vector, as set by SNESSetFunction()

2341:    Notes:
2342:    SNESComputeFunction() is typically used within nonlinear solvers
2343:    implementations, so most users would not generally call this routine
2344:    themselves.

2346:    Level: developer

2348: .seealso: SNESSetFunction(), SNESGetFunction()
2349: @*/
2350: PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
2351: {
2353:   DM             dm;
2354:   DMSNES         sdm;

2362:   VecValidValues(x,2,PETSC_TRUE);

2364:   SNESGetDM(snes,&dm);
2365:   DMGetDMSNES(dm,&sdm);
2366:   if (sdm->ops->computefunction) {
2367:     if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) {
2368:       PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);
2369:     }
2370:     VecLockReadPush(x);
2371:     PetscStackPush("SNES user function");
2372:     /* ensure domainerror is false prior to computefunction evaluation (may not have been reset) */
2373:     snes->domainerror = PETSC_FALSE;
2374:     (*sdm->ops->computefunction)(snes,x,y,sdm->functionctx);
2375:     PetscStackPop;
2376:     VecLockReadPop(x);
2377:     if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) {
2378:       PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);
2379:     }
2380:   } else if (snes->vec_rhs) {
2381:     MatMult(snes->jacobian, x, y);
2382:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve().");
2383:   if (snes->vec_rhs) {
2384:     VecAXPY(y,-1.0,snes->vec_rhs);
2385:   }
2386:   snes->nfuncs++;
2387:   /*
2388:      domainerror might not be set on all processes; so we tag vector locally with Inf and the next inner product or norm will
2389:      propagate the value to all processes
2390:   */
2391:   if (snes->domainerror) {
2392:     VecSetInf(y);
2393:   }
2394:   return(0);
2395: }

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

2400:    Collective on SNES

2402:    Input Parameters:
2403: +  snes - the SNES context
2404: .  x - input vector
2405: -  b - rhs vector

2407:    Output Parameter:
2408: .  x - new solution vector

2410:    Notes:
2411:    SNESComputeNGS() is typically used within composed nonlinear solver
2412:    implementations, so most users would not generally call this routine
2413:    themselves.

2415:    Level: developer

2417: .seealso: SNESSetNGS(), SNESComputeFunction()
2418: @*/
2419: PetscErrorCode  SNESComputeNGS(SNES snes,Vec b,Vec x)
2420: {
2422:   DM             dm;
2423:   DMSNES         sdm;

2431:   if (b) {VecValidValues(b,2,PETSC_TRUE);}
2432:   PetscLogEventBegin(SNES_NGSEval,snes,x,b,0);
2433:   SNESGetDM(snes,&dm);
2434:   DMGetDMSNES(dm,&sdm);
2435:   if (sdm->ops->computegs) {
2436:     if (b) {VecLockReadPush(b);}
2437:     PetscStackPush("SNES user NGS");
2438:     (*sdm->ops->computegs)(snes,x,b,sdm->gsctx);
2439:     PetscStackPop;
2440:     if (b) {VecLockReadPop(b);}
2441:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetNGS() before SNESComputeNGS(), likely called from SNESSolve().");
2442:   PetscLogEventEnd(SNES_NGSEval,snes,x,b,0);
2443:   return(0);
2444: }

2446: PetscErrorCode SNESTestJacobian(SNES snes)
2447: {
2448:   Mat               A,B,C,D,jacobian;
2449:   Vec               x = snes->vec_sol,f = snes->vec_func;
2450:   PetscErrorCode    ierr;
2451:   PetscReal         nrm,gnorm;
2452:   PetscReal         threshold = 1.e-5;
2453:   MatType           mattype;
2454:   PetscInt          m,n,M,N;
2455:   void              *functx;
2456:   PetscBool         complete_print = PETSC_FALSE,threshold_print = PETSC_FALSE,test = PETSC_FALSE,flg,istranspose;
2457:   PetscViewer       viewer,mviewer;
2458:   MPI_Comm          comm;
2459:   PetscInt          tabs;
2460:   static PetscBool  directionsprinted = PETSC_FALSE;
2461:   PetscViewerFormat format;

2464:   PetscObjectOptionsBegin((PetscObject)snes);
2465:   PetscOptionsName("-snes_test_jacobian","Compare hand-coded and finite difference Jacobians","None",&test);
2466:   PetscOptionsReal("-snes_test_jacobian", "Threshold for element difference between hand-coded and finite difference being meaningful", "None", threshold, &threshold,NULL);
2467:   PetscOptionsViewer("-snes_test_jacobian_view","View difference between hand-coded and finite difference Jacobians element entries","None",&mviewer,&format,&complete_print);
2468:   if (!complete_print) {
2469:     PetscOptionsDeprecated("-snes_test_jacobian_display","-snes_test_jacobian_view","3.13",NULL);
2470:     PetscOptionsViewer("-snes_test_jacobian_display","Display difference between hand-coded and finite difference Jacobians","None",&mviewer,&format,&complete_print);
2471:   }
2472:   /* for compatibility with PETSc 3.9 and older. */
2473:   PetscOptionsDeprecated("-snes_test_jacobian_display_threshold","-snes_test_jacobian","3.13","-snes_test_jacobian accepts an optional threshold (since v3.10)");
2474:   PetscOptionsReal("-snes_test_jacobian_display_threshold", "Display difference between hand-coded and finite difference Jacobians which exceed input threshold", "None", threshold, &threshold, &threshold_print);
2475:   PetscOptionsEnd();
2476:   if (!test) return(0);

2478:   PetscObjectGetComm((PetscObject)snes,&comm);
2479:   PetscViewerASCIIGetStdout(comm,&viewer);
2480:   PetscViewerASCIIGetTab(viewer, &tabs);
2481:   PetscViewerASCIISetTab(viewer, ((PetscObject)snes)->tablevel);
2482:   PetscViewerASCIIPrintf(viewer,"  ---------- Testing Jacobian -------------\n");
2483:   if (!complete_print && !directionsprinted) {
2484:     PetscViewerASCIIPrintf(viewer,"  Run with -snes_test_jacobian_view and optionally -snes_test_jacobian <threshold> to show difference\n");
2485:     PetscViewerASCIIPrintf(viewer,"    of hand-coded and finite difference Jacobian entries greater than <threshold>.\n");
2486:   }
2487:   if (!directionsprinted) {
2488:     PetscViewerASCIIPrintf(viewer,"  Testing hand-coded Jacobian, if (for double precision runs) ||J - Jfd||_F/||J||_F is\n");
2489:     PetscViewerASCIIPrintf(viewer,"    O(1.e-8), the hand-coded Jacobian is probably correct.\n");
2490:     directionsprinted = PETSC_TRUE;
2491:   }
2492:   if (complete_print) {
2493:     PetscViewerPushFormat(mviewer,format);
2494:   }

2496:   PetscObjectTypeCompare((PetscObject)snes->jacobian,MATMFFD,&flg);
2497:   if (!flg) jacobian = snes->jacobian;
2498:   else jacobian = snes->jacobian_pre;

2500:   if (!x) {
2501:     MatCreateVecs(jacobian, &x, NULL);
2502:   } else {
2503:     PetscObjectReference((PetscObject) x);
2504:   }
2505:   if (!f) {
2506:     VecDuplicate(x, &f);
2507:   } else {
2508:     PetscObjectReference((PetscObject) f);
2509:   }
2510:   /* evaluate the function at this point because SNESComputeJacobianDefault() assumes that the function has been evaluated and put into snes->vec_func */
2511:   SNESComputeFunction(snes,x,f);
2512:   VecDestroy(&f);
2513:   PetscObjectTypeCompare((PetscObject)snes,SNESKSPTRANSPOSEONLY,&istranspose);
2514:   while (jacobian) {
2515:     Mat JT = NULL, Jsave = NULL;

2517:     if (istranspose) {
2518:       MatCreateTranspose(jacobian,&JT);
2519:       Jsave = jacobian;
2520:       jacobian = JT;
2521:     }
2522:     PetscObjectBaseTypeCompareAny((PetscObject)jacobian,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPISBAIJ,"");
2523:     if (flg) {
2524:       A    = jacobian;
2525:       PetscObjectReference((PetscObject)A);
2526:     } else {
2527:       MatComputeOperator(jacobian,MATAIJ,&A);
2528:     }

2530:     MatGetType(A,&mattype);
2531:     MatGetSize(A,&M,&N);
2532:     MatGetLocalSize(A,&m,&n);
2533:     MatCreate(PetscObjectComm((PetscObject)A),&B);
2534:     MatSetType(B,mattype);
2535:     MatSetSizes(B,m,n,M,N);
2536:     MatSetBlockSizesFromMats(B,A,A);
2537:     MatSetUp(B);
2538:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);

2540:     SNESGetFunction(snes,NULL,NULL,&functx);
2541:     SNESComputeJacobianDefault(snes,x,B,B,functx);

2543:     MatDuplicate(B,MAT_COPY_VALUES,&D);
2544:     MatAYPX(D,-1.0,A,DIFFERENT_NONZERO_PATTERN);
2545:     MatNorm(D,NORM_FROBENIUS,&nrm);
2546:     MatNorm(A,NORM_FROBENIUS,&gnorm);
2547:     MatDestroy(&D);
2548:     if (!gnorm) gnorm = 1; /* just in case */
2549:     PetscViewerASCIIPrintf(viewer,"  ||J - Jfd||_F/||J||_F = %g, ||J - Jfd||_F = %g\n",(double)(nrm/gnorm),(double)nrm);

2551:     if (complete_print) {
2552:       PetscViewerASCIIPrintf(viewer,"  Hand-coded Jacobian ----------\n");
2553:       MatView(A,mviewer);
2554:       PetscViewerASCIIPrintf(viewer,"  Finite difference Jacobian ----------\n");
2555:       MatView(B,mviewer);
2556:     }

2558:     if (threshold_print || complete_print) {
2559:       PetscInt          Istart, Iend, *ccols, bncols, cncols, j, row;
2560:       PetscScalar       *cvals;
2561:       const PetscInt    *bcols;
2562:       const PetscScalar *bvals;

2564:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2565:       MatSetType(C,mattype);
2566:       MatSetSizes(C,m,n,M,N);
2567:       MatSetBlockSizesFromMats(C,A,A);
2568:       MatSetUp(C);
2569:       MatSetOption(C,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);

2571:       MatAYPX(B,-1.0,A,DIFFERENT_NONZERO_PATTERN);
2572:       MatGetOwnershipRange(B,&Istart,&Iend);

2574:       for (row = Istart; row < Iend; row++) {
2575:         MatGetRow(B,row,&bncols,&bcols,&bvals);
2576:         PetscMalloc2(bncols,&ccols,bncols,&cvals);
2577:         for (j = 0, cncols = 0; j < bncols; j++) {
2578:           if (PetscAbsScalar(bvals[j]) > threshold) {
2579:             ccols[cncols] = bcols[j];
2580:             cvals[cncols] = bvals[j];
2581:             cncols += 1;
2582:           }
2583:         }
2584:         if (cncols) {
2585:           MatSetValues(C,1,&row,cncols,ccols,cvals,INSERT_VALUES);
2586:         }
2587:         MatRestoreRow(B,row,&bncols,&bcols,&bvals);
2588:         PetscFree2(ccols,cvals);
2589:       }
2590:       MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2591:       MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
2592:       PetscViewerASCIIPrintf(viewer,"  Hand-coded minus finite-difference Jacobian with tolerance %g ----------\n",(double)threshold);
2593:       MatView(C,complete_print ? mviewer : viewer);
2594:       MatDestroy(&C);
2595:     }
2596:     MatDestroy(&A);
2597:     MatDestroy(&B);
2598:     MatDestroy(&JT);
2599:     if (Jsave) jacobian = Jsave;
2600:     if (jacobian != snes->jacobian_pre) {
2601:       jacobian = snes->jacobian_pre;
2602:       PetscViewerASCIIPrintf(viewer,"  ---------- Testing Jacobian for preconditioner -------------\n");
2603:     }
2604:     else jacobian = NULL;
2605:   }
2606:   VecDestroy(&x);
2607:   if (complete_print) {
2608:     PetscViewerPopFormat(mviewer);
2609:   }
2610:   if (mviewer) { PetscViewerDestroy(&mviewer); }
2611:   PetscViewerASCIISetTab(viewer,tabs);
2612:   return(0);
2613: }

2615: /*@
2616:    SNESComputeJacobian - Computes the Jacobian matrix that has been set with SNESSetJacobian().

2618:    Collective on SNES

2620:    Input Parameters:
2621: +  snes - the SNES context
2622: -  x - input vector

2624:    Output Parameters:
2625: +  A - Jacobian matrix
2626: -  B - optional preconditioning matrix

2628:   Options Database Keys:
2629: +    -snes_lag_preconditioner <lag>
2630: .    -snes_lag_jacobian <lag>
2631: .    -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.
2632: .    -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
2633: .    -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences
2634: .    -snes_compare_explicit_draw  - Compare the computed Jacobian to the finite difference Jacobian and draw the result
2635: .    -snes_compare_explicit_contour  - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result
2636: .    -snes_compare_operator  - Make the comparison options above use the operator instead of the preconditioning matrix
2637: .    -snes_compare_coloring - Compute the finite difference Jacobian using coloring and display norms of difference
2638: .    -snes_compare_coloring_display - Compute the finite differece Jacobian using coloring and display verbose differences
2639: .    -snes_compare_coloring_threshold - Display only those matrix entries that differ by more than a given threshold
2640: .    -snes_compare_coloring_threshold_atol - Absolute tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold
2641: .    -snes_compare_coloring_threshold_rtol - Relative tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold
2642: .    -snes_compare_coloring_draw - Compute the finite differece Jacobian using coloring and draw differences
2643: -    -snes_compare_coloring_draw_contour - Compute the finite differece Jacobian using coloring and show contours of matrices and differences


2646:    Notes:
2647:    Most users should not need to explicitly call this routine, as it
2648:    is used internally within the nonlinear solvers.

2650:    Developer Notes:
2651:     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
2652:       for with the SNESType of test that has been removed.

2654:    Level: developer

2656: .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
2657: @*/
2658: PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat A,Mat B)
2659: {
2661:   PetscBool      flag;
2662:   DM             dm;
2663:   DMSNES         sdm;
2664:   KSP            ksp;

2670:   VecValidValues(X,2,PETSC_TRUE);
2671:   SNESGetDM(snes,&dm);
2672:   DMGetDMSNES(dm,&sdm);

2674:   if (!sdm->ops->computejacobian) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_USER,"Must call SNESSetJacobian(), DMSNESSetJacobian(), DMDASNESSetJacobianLocal(), etc");

2676:   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */

2678:   if (snes->lagjacobian == -2) {
2679:     snes->lagjacobian = -1;

2681:     PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");
2682:   } else if (snes->lagjacobian == -1) {
2683:     PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");
2684:     PetscObjectTypeCompare((PetscObject)A,MATMFFD,&flag);
2685:     if (flag) {
2686:       MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2687:       MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2688:     }
2689:     return(0);
2690:   } else if (snes->lagjacobian > 1 && (snes->iter + snes->jac_iter) % snes->lagjacobian) {
2691:     PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);
2692:     PetscObjectTypeCompare((PetscObject)A,MATMFFD,&flag);
2693:     if (flag) {
2694:       MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2695:       MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2696:     }
2697:     return(0);
2698:   }
2699:   if (snes->npc && snes->npcside== PC_LEFT) {
2700:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2701:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2702:     return(0);
2703:   }

2705:   PetscLogEventBegin(SNES_JacobianEval,snes,X,A,B);
2706:   VecLockReadPush(X);
2707:   PetscStackPush("SNES user Jacobian function");
2708:   (*sdm->ops->computejacobian)(snes,X,A,B,sdm->jacobianctx);
2709:   PetscStackPop;
2710:   VecLockReadPop(X);
2711:   PetscLogEventEnd(SNES_JacobianEval,snes,X,A,B);

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

2716:   /* the next line ensures that snes->ksp exists */
2717:   SNESGetKSP(snes,&ksp);
2718:   if (snes->lagpreconditioner == -2) {
2719:     PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");
2720:     KSPSetReusePreconditioner(snes->ksp,PETSC_FALSE);
2721:     snes->lagpreconditioner = -1;
2722:   } else if (snes->lagpreconditioner == -1) {
2723:     PetscInfo(snes,"Reusing preconditioner because lag is -1\n");
2724:     KSPSetReusePreconditioner(snes->ksp,PETSC_TRUE);
2725:   } else if (snes->lagpreconditioner > 1 && (snes->iter + snes->pre_iter) % snes->lagpreconditioner) {
2726:     PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);
2727:     KSPSetReusePreconditioner(snes->ksp,PETSC_TRUE);
2728:   } else {
2729:     PetscInfo(snes,"Rebuilding preconditioner\n");
2730:     KSPSetReusePreconditioner(snes->ksp,PETSC_FALSE);
2731:   }

2733:   SNESTestJacobian(snes);
2734:   /* make sure user returned a correct Jacobian and preconditioner */
2737:   {
2738:     PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE;
2739:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_explicit",NULL,NULL,&flag);
2740:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",NULL,NULL,&flag_draw);
2741:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",NULL,NULL,&flag_contour);
2742:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_operator",NULL,NULL,&flag_operator);
2743:     if (flag || flag_draw || flag_contour) {
2744:       Mat          Bexp_mine = NULL,Bexp,FDexp;
2745:       PetscViewer  vdraw,vstdout;
2746:       PetscBool    flg;
2747:       if (flag_operator) {
2748:         MatComputeOperator(A,MATAIJ,&Bexp_mine);
2749:         Bexp = Bexp_mine;
2750:       } else {
2751:         /* See if the preconditioning matrix can be viewed and added directly */
2752:         PetscObjectBaseTypeCompareAny((PetscObject)B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");
2753:         if (flg) Bexp = B;
2754:         else {
2755:           /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */
2756:           MatComputeOperator(B,MATAIJ,&Bexp_mine);
2757:           Bexp = Bexp_mine;
2758:         }
2759:       }
2760:       MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);
2761:       SNESComputeJacobianDefault(snes,X,FDexp,FDexp,NULL);
2762:       PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&vstdout);
2763:       if (flag_draw || flag_contour) {
2764:         PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes),NULL,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);
2765:         if (flag_contour) {PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);}
2766:       } else vdraw = NULL;
2767:       PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator ? "Jacobian" : "preconditioning Jacobian");
2768:       if (flag) {MatView(Bexp,vstdout);}
2769:       if (vdraw) {MatView(Bexp,vdraw);}
2770:       PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");
2771:       if (flag) {MatView(FDexp,vstdout);}
2772:       if (vdraw) {MatView(FDexp,vdraw);}
2773:       MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);
2774:       PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");
2775:       if (flag) {MatView(FDexp,vstdout);}
2776:       if (vdraw) {              /* Always use contour for the difference */
2777:         PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);
2778:         MatView(FDexp,vdraw);
2779:         PetscViewerPopFormat(vdraw);
2780:       }
2781:       if (flag_contour) {PetscViewerPopFormat(vdraw);}
2782:       PetscViewerDestroy(&vdraw);
2783:       MatDestroy(&Bexp_mine);
2784:       MatDestroy(&FDexp);
2785:     }
2786:   }
2787:   {
2788:     PetscBool flag = PETSC_FALSE,flag_display = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_threshold = PETSC_FALSE;
2789:     PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON,threshold_rtol = 10*PETSC_SQRT_MACHINE_EPSILON;
2790:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring",NULL,NULL,&flag);
2791:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_display",NULL,NULL,&flag_display);
2792:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_draw",NULL,NULL,&flag_draw);
2793:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_draw_contour",NULL,NULL,&flag_contour);
2794:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold",NULL,NULL,&flag_threshold);
2795:     if (flag_threshold) {
2796:       PetscOptionsGetReal(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_rtol",&threshold_rtol,NULL);
2797:       PetscOptionsGetReal(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_atol",&threshold_atol,NULL);
2798:     }
2799:     if (flag || flag_display || flag_draw || flag_contour || flag_threshold) {
2800:       Mat            Bfd;
2801:       PetscViewer    vdraw,vstdout;
2802:       MatColoring    coloring;
2803:       ISColoring     iscoloring;
2804:       MatFDColoring  matfdcoloring;
2805:       PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2806:       void           *funcctx;
2807:       PetscReal      norm1,norm2,normmax;

2809:       MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&Bfd);
2810:       MatColoringCreate(Bfd,&coloring);
2811:       MatColoringSetType(coloring,MATCOLORINGSL);
2812:       MatColoringSetFromOptions(coloring);
2813:       MatColoringApply(coloring,&iscoloring);
2814:       MatColoringDestroy(&coloring);
2815:       MatFDColoringCreate(Bfd,iscoloring,&matfdcoloring);
2816:       MatFDColoringSetFromOptions(matfdcoloring);
2817:       MatFDColoringSetUp(Bfd,iscoloring,matfdcoloring);
2818:       ISColoringDestroy(&iscoloring);

2820:       /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */
2821:       SNESGetFunction(snes,NULL,&func,&funcctx);
2822:       MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode (*)(void))func,funcctx);
2823:       PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring,((PetscObject)snes)->prefix);
2824:       PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring,"coloring_");
2825:       MatFDColoringSetFromOptions(matfdcoloring);
2826:       MatFDColoringApply(Bfd,matfdcoloring,X,snes);
2827:       MatFDColoringDestroy(&matfdcoloring);

2829:       PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&vstdout);
2830:       if (flag_draw || flag_contour) {
2831:         PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes),NULL,"Colored Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);
2832:         if (flag_contour) {PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);}
2833:       } else vdraw = NULL;
2834:       PetscViewerASCIIPrintf(vstdout,"Explicit preconditioning Jacobian\n");
2835:       if (flag_display) {MatView(B,vstdout);}
2836:       if (vdraw) {MatView(B,vdraw);}
2837:       PetscViewerASCIIPrintf(vstdout,"Colored Finite difference Jacobian\n");
2838:       if (flag_display) {MatView(Bfd,vstdout);}
2839:       if (vdraw) {MatView(Bfd,vdraw);}
2840:       MatAYPX(Bfd,-1.0,B,SAME_NONZERO_PATTERN);
2841:       MatNorm(Bfd,NORM_1,&norm1);
2842:       MatNorm(Bfd,NORM_FROBENIUS,&norm2);
2843:       MatNorm(Bfd,NORM_MAX,&normmax);
2844:       PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian, norm1=%g normFrob=%g normmax=%g\n",(double)norm1,(double)norm2,(double)normmax);
2845:       if (flag_display) {MatView(Bfd,vstdout);}
2846:       if (vdraw) {              /* Always use contour for the difference */
2847:         PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);
2848:         MatView(Bfd,vdraw);
2849:         PetscViewerPopFormat(vdraw);
2850:       }
2851:       if (flag_contour) {PetscViewerPopFormat(vdraw);}

2853:       if (flag_threshold) {
2854:         PetscInt bs,rstart,rend,i;
2855:         MatGetBlockSize(B,&bs);
2856:         MatGetOwnershipRange(B,&rstart,&rend);
2857:         for (i=rstart; i<rend; i++) {
2858:           const PetscScalar *ba,*ca;
2859:           const PetscInt    *bj,*cj;
2860:           PetscInt          bn,cn,j,maxentrycol = -1,maxdiffcol = -1,maxrdiffcol = -1;
2861:           PetscReal         maxentry = 0,maxdiff = 0,maxrdiff = 0;
2862:           MatGetRow(B,i,&bn,&bj,&ba);
2863:           MatGetRow(Bfd,i,&cn,&cj,&ca);
2864:           if (bn != cn) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_PLIB,"Unexpected different nonzero pattern in -snes_compare_coloring_threshold");
2865:           for (j=0; j<bn; j++) {
2866:             PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j]));
2867:             if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) {
2868:               maxentrycol = bj[j];
2869:               maxentry    = PetscRealPart(ba[j]);
2870:             }
2871:             if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) {
2872:               maxdiffcol = bj[j];
2873:               maxdiff    = PetscRealPart(ca[j]);
2874:             }
2875:             if (rdiff > maxrdiff) {
2876:               maxrdiffcol = bj[j];
2877:               maxrdiff    = rdiff;
2878:             }
2879:           }
2880:           if (maxrdiff > 1) {
2881:             PetscViewerASCIIPrintf(vstdout,"row %D (maxentry=%g at %D, maxdiff=%g at %D, maxrdiff=%g at %D):",i,(double)maxentry,maxentrycol,(double)maxdiff,maxdiffcol,(double)maxrdiff,maxrdiffcol);
2882:             for (j=0; j<bn; j++) {
2883:               PetscReal rdiff;
2884:               rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j]));
2885:               if (rdiff > 1) {
2886:                 PetscViewerASCIIPrintf(vstdout," (%D,%g:%g)",bj[j],(double)PetscRealPart(ba[j]),(double)PetscRealPart(ca[j]));
2887:               }
2888:             }
2889:             PetscViewerASCIIPrintf(vstdout,"\n",i,maxentry,maxdiff,maxrdiff);
2890:           }
2891:           MatRestoreRow(B,i,&bn,&bj,&ba);
2892:           MatRestoreRow(Bfd,i,&cn,&cj,&ca);
2893:         }
2894:       }
2895:       PetscViewerDestroy(&vdraw);
2896:       MatDestroy(&Bfd);
2897:     }
2898:   }
2899:   return(0);
2900: }

2902: /*MC
2903:     SNESJacobianFunction - Function used to convey the nonlinear Jacobian of the function to be solved by SNES

2905:      Synopsis:
2906:      #include "petscsnes.h"
2907:      PetscErrorCode SNESJacobianFunction(SNES snes,Vec x,Mat Amat,Mat Pmat,void *ctx);

2909:      Collective on snes

2911:     Input Parameters:
2912: +  x - input vector, the Jacobian is to be computed at this value
2913: -  ctx - [optional] user-defined Jacobian context

2915:     Output Parameters:
2916: +  Amat - the matrix that defines the (approximate) Jacobian
2917: -  Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat.

2919:    Level: intermediate

2921: .seealso:   SNESSetFunction(), SNESGetFunction(), SNESSetJacobian(), SNESGetJacobian()
2922: M*/

2924: /*@C
2925:    SNESSetJacobian - Sets the function to compute Jacobian as well as the
2926:    location to store the matrix.

2928:    Logically Collective on SNES

2930:    Input Parameters:
2931: +  snes - the SNES context
2932: .  Amat - the matrix that defines the (approximate) Jacobian
2933: .  Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat.
2934: .  J - Jacobian evaluation routine (if NULL then SNES retains any previously set value), see SNESJacobianFunction for details
2935: -  ctx - [optional] user-defined context for private data for the
2936:          Jacobian evaluation routine (may be NULL) (if NULL then SNES retains any previously set value)

2938:    Notes:
2939:    If the Amat matrix and Pmat matrix are different you must call MatAssemblyBegin/End() on
2940:    each matrix.

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

2945:    If using SNESComputeJacobianDefaultColor() to assemble a Jacobian, the ctx argument
2946:    must be a MatFDColoring.

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

2951:    Level: beginner

2953: .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESComputeJacobianDefaultColor(), MatStructure, J,
2954:           SNESSetPicard(), SNESJacobianFunction
2955: @*/
2956: PetscErrorCode  SNESSetJacobian(SNES snes,Mat Amat,Mat Pmat,PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx)
2957: {
2959:   DM             dm;

2967:   SNESGetDM(snes,&dm);
2968:   DMSNESSetJacobian(dm,J,ctx);
2969:   if (Amat) {
2970:     PetscObjectReference((PetscObject)Amat);
2971:     MatDestroy(&snes->jacobian);

2973:     snes->jacobian = Amat;
2974:   }
2975:   if (Pmat) {
2976:     PetscObjectReference((PetscObject)Pmat);
2977:     MatDestroy(&snes->jacobian_pre);

2979:     snes->jacobian_pre = Pmat;
2980:   }
2981:   return(0);
2982: }

2984: /*@C
2985:    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
2986:    provided context for evaluating the Jacobian.

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

2990:    Input Parameter:
2991: .  snes - the nonlinear solver context

2993:    Output Parameters:
2994: +  Amat - location to stash (approximate) Jacobian matrix (or NULL)
2995: .  Pmat - location to stash matrix used to compute the preconditioner (or NULL)
2996: .  J - location to put Jacobian function (or NULL), see SNESJacobianFunction for details on its calling sequence
2997: -  ctx - location to stash Jacobian ctx (or NULL)

2999:    Level: advanced

3001: .seealso: SNESSetJacobian(), SNESComputeJacobian(), SNESJacobianFunction, SNESGetFunction()
3002: @*/
3003: PetscErrorCode SNESGetJacobian(SNES snes,Mat *Amat,Mat *Pmat,PetscErrorCode (**J)(SNES,Vec,Mat,Mat,void*),void **ctx)
3004: {
3006:   DM             dm;
3007:   DMSNES         sdm;

3011:   if (Amat) *Amat = snes->jacobian;
3012:   if (Pmat) *Pmat = snes->jacobian_pre;
3013:   SNESGetDM(snes,&dm);
3014:   DMGetDMSNES(dm,&sdm);
3015:   if (J) *J = sdm->ops->computejacobian;
3016:   if (ctx) *ctx = sdm->jacobianctx;
3017:   return(0);
3018: }

3020: static PetscErrorCode SNESSetDefaultComputeJacobian(SNES snes)
3021: {
3023:   DM             dm;
3024:   DMSNES         sdm;

3027:   SNESGetDM(snes,&dm);
3028:   DMGetDMSNES(dm,&sdm);
3029:   if (!sdm->ops->computejacobian && snes->jacobian_pre) {
3030:     DM        dm;
3031:     PetscBool isdense,ismf;

3033:     SNESGetDM(snes,&dm);
3034:     PetscObjectTypeCompareAny((PetscObject)snes->jacobian_pre,&isdense,MATSEQDENSE,MATMPIDENSE,MATDENSE,NULL);
3035:     PetscObjectTypeCompareAny((PetscObject)snes->jacobian_pre,&ismf,MATMFFD,MATSHELL,NULL);
3036:     if (isdense) {
3037:       DMSNESSetJacobian(dm,SNESComputeJacobianDefault,NULL);
3038:     } else if (!ismf) {
3039:       DMSNESSetJacobian(dm,SNESComputeJacobianDefaultColor,NULL);
3040:     }
3041:   }
3042:   return(0);
3043: }

3045: /*@
3046:    SNESSetUp - Sets up the internal data structures for the later use
3047:    of a nonlinear solver.

3049:    Collective on SNES

3051:    Input Parameters:
3052: .  snes - the SNES context

3054:    Notes:
3055:    For basic use of the SNES solvers the user need not explicitly call
3056:    SNESSetUp(), since these actions will automatically occur during
3057:    the call to SNESSolve().  However, if one wishes to control this
3058:    phase separately, SNESSetUp() should be called after SNESCreate()
3059:    and optional routines of the form SNESSetXXX(), but before SNESSolve().

3061:    Level: advanced

3063: .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
3064: @*/
3065: PetscErrorCode  SNESSetUp(SNES snes)
3066: {
3068:   DM             dm;
3069:   DMSNES         sdm;
3070:   SNESLineSearch linesearch, pclinesearch;
3071:   void           *lsprectx,*lspostctx;
3072:   PetscErrorCode (*precheck)(SNESLineSearch,Vec,Vec,PetscBool*,void*);
3073:   PetscErrorCode (*postcheck)(SNESLineSearch,Vec,Vec,Vec,PetscBool*,PetscBool*,void*);
3074:   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
3075:   Vec            f,fpc;
3076:   void           *funcctx;
3077:   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
3078:   void           *jacctx,*appctx;
3079:   Mat            j,jpre;

3083:   if (snes->setupcalled) return(0);
3084:   PetscLogEventBegin(SNES_Setup,snes,0,0,0);

3086:   if (!((PetscObject)snes)->type_name) {
3087:     SNESSetType(snes,SNESNEWTONLS);
3088:   }

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

3092:   SNESGetDM(snes,&dm);
3093:   DMGetDMSNES(dm,&sdm);
3094:   if (!sdm->ops->computefunction) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Function never provided to SNES object");
3095:   SNESSetDefaultComputeJacobian(snes);

3097:   if (!snes->vec_func) {
3098:     DMCreateGlobalVector(dm,&snes->vec_func);
3099:   }

3101:   if (!snes->ksp) {
3102:     SNESGetKSP(snes, &snes->ksp);
3103:   }

3105:   if (snes->linesearch) {
3106:     SNESGetLineSearch(snes, &snes->linesearch);
3107:     SNESLineSearchSetFunction(snes->linesearch,SNESComputeFunction);
3108:   }

3110:   if (snes->npc && (snes->npcside== PC_LEFT)) {
3111:     snes->mf          = PETSC_TRUE;
3112:     snes->mf_operator = PETSC_FALSE;
3113:   }

3115:   if (snes->npc) {
3116:     /* copy the DM over */
3117:     SNESGetDM(snes,&dm);
3118:     SNESSetDM(snes->npc,dm);

3120:     SNESGetFunction(snes,&f,&func,&funcctx);
3121:     VecDuplicate(f,&fpc);
3122:     SNESSetFunction(snes->npc,fpc,func,funcctx);
3123:     SNESGetJacobian(snes,&j,&jpre,&jac,&jacctx);
3124:     SNESSetJacobian(snes->npc,j,jpre,jac,jacctx);
3125:     SNESGetApplicationContext(snes,&appctx);
3126:     SNESSetApplicationContext(snes->npc,appctx);
3127:     VecDestroy(&fpc);

3129:     /* copy the function pointers over */
3130:     PetscObjectCopyFortranFunctionPointers((PetscObject)snes,(PetscObject)snes->npc);

3132:     /* default to 1 iteration */
3133:     SNESSetTolerances(snes->npc,0.0,0.0,0.0,1,snes->npc->max_funcs);
3134:     if (snes->npcside==PC_RIGHT) {
3135:       SNESSetNormSchedule(snes->npc,SNES_NORM_FINAL_ONLY);
3136:     } else {
3137:       SNESSetNormSchedule(snes->npc,SNES_NORM_NONE);
3138:     }
3139:     SNESSetFromOptions(snes->npc);

3141:     /* copy the line search context over */
3142:     if (snes->linesearch && snes->npc->linesearch) {
3143:       SNESGetLineSearch(snes,&linesearch);
3144:       SNESGetLineSearch(snes->npc,&pclinesearch);
3145:       SNESLineSearchGetPreCheck(linesearch,&precheck,&lsprectx);
3146:       SNESLineSearchGetPostCheck(linesearch,&postcheck,&lspostctx);
3147:       SNESLineSearchSetPreCheck(pclinesearch,precheck,lsprectx);
3148:       SNESLineSearchSetPostCheck(pclinesearch,postcheck,lspostctx);
3149:       PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)pclinesearch);
3150:     }
3151:   }
3152:   if (snes->mf) {
3153:     SNESSetUpMatrixFree_Private(snes, snes->mf_operator, snes->mf_version);
3154:   }
3155:   if (snes->ops->usercompute && !snes->user) {
3156:     (*snes->ops->usercompute)(snes,(void**)&snes->user);
3157:   }

3159:   snes->jac_iter = 0;
3160:   snes->pre_iter = 0;

3162:   if (snes->ops->setup) {
3163:     (*snes->ops->setup)(snes);
3164:   }

3166:   SNESSetDefaultComputeJacobian(snes);

3168:   if (snes->npc && (snes->npcside== PC_LEFT)) {
3169:     if (snes->functype == SNES_FUNCTION_PRECONDITIONED) {
3170:       if (snes->linesearch){
3171:         SNESGetLineSearch(snes,&linesearch);
3172:         SNESLineSearchSetFunction(linesearch,SNESComputeFunctionDefaultNPC);
3173:       }
3174:     }
3175:   }
3176:   PetscLogEventEnd(SNES_Setup,snes,0,0,0);
3177:   snes->setupcalled = PETSC_TRUE;
3178:   return(0);
3179: }

3181: /*@
3182:    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats

3184:    Collective on SNES

3186:    Input Parameter:
3187: .  snes - iterative context obtained from SNESCreate()

3189:    Level: intermediate

3191:    Notes:
3192:     Also calls the application context destroy routine set with SNESSetComputeApplicationContext()

3194: .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
3195: @*/
3196: PetscErrorCode  SNESReset(SNES snes)
3197: {

3202:   if (snes->ops->userdestroy && snes->user) {
3203:     (*snes->ops->userdestroy)((void**)&snes->user);
3204:     snes->user = NULL;
3205:   }
3206:   if (snes->npc) {
3207:     SNESReset(snes->npc);
3208:   }

3210:   if (snes->ops->reset) {
3211:     (*snes->ops->reset)(snes);
3212:   }
3213:   if (snes->ksp) {
3214:     KSPReset(snes->ksp);
3215:   }

3217:   if (snes->linesearch) {
3218:     SNESLineSearchReset(snes->linesearch);
3219:   }

3221:   VecDestroy(&snes->vec_rhs);
3222:   VecDestroy(&snes->vec_sol);
3223:   VecDestroy(&snes->vec_sol_update);
3224:   VecDestroy(&snes->vec_func);
3225:   MatDestroy(&snes->jacobian);
3226:   MatDestroy(&snes->jacobian_pre);
3227:   VecDestroyVecs(snes->nwork,&snes->work);
3228:   VecDestroyVecs(snes->nvwork,&snes->vwork);

3230:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

3232:   snes->nwork       = snes->nvwork = 0;
3233:   snes->setupcalled = PETSC_FALSE;
3234:   return(0);
3235: }

3237: /*@
3238:    SNESDestroy - Destroys the nonlinear solver context that was created
3239:    with SNESCreate().

3241:    Collective on SNES

3243:    Input Parameter:
3244: .  snes - the SNES context

3246:    Level: beginner

3248: .seealso: SNESCreate(), SNESSolve()
3249: @*/
3250: PetscErrorCode  SNESDestroy(SNES *snes)
3251: {

3255:   if (!*snes) return(0);
3257:   if (--((PetscObject)(*snes))->refct > 0) {*snes = NULL; return(0);}

3259:   SNESReset((*snes));
3260:   SNESDestroy(&(*snes)->npc);

3262:   /* if memory was published with SAWs then destroy it */
3263:   PetscObjectSAWsViewOff((PetscObject)*snes);
3264:   if ((*snes)->ops->destroy) {(*((*snes))->ops->destroy)((*snes));}

3266:   if ((*snes)->dm) {DMCoarsenHookRemove((*snes)->dm,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,*snes);}
3267:   DMDestroy(&(*snes)->dm);
3268:   KSPDestroy(&(*snes)->ksp);
3269:   SNESLineSearchDestroy(&(*snes)->linesearch);

3271:   PetscFree((*snes)->kspconvctx);
3272:   if ((*snes)->ops->convergeddestroy) {
3273:     (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);
3274:   }
3275:   if ((*snes)->conv_hist_alloc) {
3276:     PetscFree2((*snes)->conv_hist,(*snes)->conv_hist_its);
3277:   }
3278:   SNESMonitorCancel((*snes));
3279:   PetscHeaderDestroy(snes);
3280:   return(0);
3281: }

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

3285: /*@
3286:    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.

3288:    Logically Collective on SNES

3290:    Input Parameters:
3291: +  snes - the SNES context
3292: -  lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
3293:          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that

3295:    Options Database Keys:
3296: +    -snes_lag_jacobian_persists <true,false> - sets the persistence
3297: .    -snes_lag_jacobian <-2,1,2,...> - sets the lag
3298: .    -snes_lag_preconditioner_persists <true,false> - sets the persistence
3299: -    -snes_lag_preconditioner <-2,1,2,...> - sets the lag

3301:    Notes:
3302:    The default is 1
3303:    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 or SNESSetLagPreconditionerPersists() was called
3304:    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use

3306:    Level: intermediate

3308: .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian(), SNESSetLagPreconditionerPersists(),
3309:           SNESSetLagJacobianPersists()

3311: @*/
3312: PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
3313: {
3316:   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
3317:   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
3319:   snes->lagpreconditioner = lag;
3320:   return(0);
3321: }

3323: /*@
3324:    SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does

3326:    Logically Collective on SNES

3328:    Input Parameters:
3329: +  snes - the SNES context
3330: -  steps - the number of refinements to do, defaults to 0

3332:    Options Database Keys:
3333: .    -snes_grid_sequence <steps>

3335:    Level: intermediate

3337:    Notes:
3338:    Use SNESGetSolution() to extract the fine grid solution after grid sequencing.

3340: .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian(), SNESGetGridSequence()

3342: @*/
3343: PetscErrorCode  SNESSetGridSequence(SNES snes,PetscInt steps)
3344: {
3348:   snes->gridsequence = steps;
3349:   return(0);
3350: }

3352: /*@
3353:    SNESGetGridSequence - gets the number of steps of grid sequencing that SNES does

3355:    Logically Collective on SNES

3357:    Input Parameter:
3358: .  snes - the SNES context

3360:    Output Parameter:
3361: .  steps - the number of refinements to do, defaults to 0

3363:    Options Database Keys:
3364: .    -snes_grid_sequence <steps>

3366:    Level: intermediate

3368:    Notes:
3369:    Use SNESGetSolution() to extract the fine grid solution after grid sequencing.

3371: .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian(), SNESSetGridSequence()

3373: @*/
3374: PetscErrorCode  SNESGetGridSequence(SNES snes,PetscInt *steps)
3375: {
3378:   *steps = snes->gridsequence;
3379:   return(0);
3380: }

3382: /*@
3383:    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt

3385:    Not Collective

3387:    Input Parameter:
3388: .  snes - the SNES context

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

3394:    Options Database Keys:
3395: +    -snes_lag_jacobian_persists <true,false> - sets the persistence
3396: .    -snes_lag_jacobian <-2,1,2,...> - sets the lag
3397: .    -snes_lag_preconditioner_persists <true,false> - sets the persistence
3398: -    -snes_lag_preconditioner <-2,1,2,...> - sets the lag

3400:    Notes:
3401:    The default is 1
3402:    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1

3404:    Level: intermediate

3406: .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner(), SNESSetLagJacobianPersists(), SNESSetLagPreconditionerPersists()

3408: @*/
3409: PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
3410: {
3413:   *lag = snes->lagpreconditioner;
3414:   return(0);
3415: }

3417: /*@
3418:    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
3419:      often the preconditioner is rebuilt.

3421:    Logically Collective on SNES

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

3428:    Options Database Keys:
3429: +    -snes_lag_jacobian_persists <true,false> - sets the persistence
3430: .    -snes_lag_jacobian <-2,1,2,...> - sets the lag
3431: .    -snes_lag_preconditioner_persists <true,false> - sets the persistence
3432: -    -snes_lag_preconditioner <-2,1,2,...> - sets the lag.

3434:    Notes:
3435:    The default is 1
3436:    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
3437:    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
3438:    at the next Newton step but never again (unless it is reset to another value)

3440:    Level: intermediate

3442: .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobianPersists(), SNESSetLagPreconditionerPersists()

3444: @*/
3445: PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
3446: {
3449:   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
3450:   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
3452:   snes->lagjacobian = lag;
3453:   return(0);
3454: }

3456: /*@
3457:    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt

3459:    Not Collective

3461:    Input Parameter:
3462: .  snes - the SNES context

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

3468:    Notes:
3469:    The default is 1
3470:    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 or SNESSetLagJacobianPersists() was called.

3472:    Level: intermediate

3474: .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner(), SNESSetLagJacobianPersists(), SNESSetLagPreconditionerPersists()

3476: @*/
3477: PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
3478: {
3481:   *lag = snes->lagjacobian;
3482:   return(0);
3483: }

3485: /*@
3486:    SNESSetLagJacobianPersists - Set whether or not the Jacobian lagging persists through multiple solves

3488:    Logically collective on SNES

3490:    Input Parameter:
3491: +  snes - the SNES context
3492: -   flg - jacobian lagging persists if true

3494:    Options Database Keys:
3495: +    -snes_lag_jacobian_persists <true,false> - sets the persistence
3496: .    -snes_lag_jacobian <-2,1,2,...> - sets the lag
3497: .    -snes_lag_preconditioner_persists <true,false> - sets the persistence
3498: -    -snes_lag_preconditioner <-2,1,2,...> - sets the lag


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

3506:    Level: developer

3508: .seealso: SNESSetLagPreconditionerPersists(), SNESSetLagJacobian(), SNESGetLagJacobian(), SNESGetNPC(), SNESSetLagJacobianPersists()

3510: @*/
3511: PetscErrorCode  SNESSetLagJacobianPersists(SNES snes,PetscBool flg)
3512: {
3516:   snes->lagjac_persist = flg;
3517:   return(0);
3518: }

3520: /*@
3521:    SNESSetLagPreconditionerPersists - Set whether or not the preconditioner lagging persists through multiple solves

3523:    Logically Collective on SNES

3525:    Input Parameter:
3526: +  snes - the SNES context
3527: -   flg - preconditioner lagging persists if true

3529:    Options Database Keys:
3530: +    -snes_lag_jacobian_persists <true,false> - sets the persistence
3531: .    -snes_lag_jacobian <-2,1,2,...> - sets the lag
3532: .    -snes_lag_preconditioner_persists <true,false> - sets the persistence
3533: -    -snes_lag_preconditioner <-2,1,2,...> - sets the lag

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

3540:    Level: developer

3542: .seealso: SNESSetLagJacobianPersists(), SNESSetLagJacobian(), SNESGetLagJacobian(), SNESGetNPC(), SNESSetLagPreconditioner()

3544: @*/
3545: PetscErrorCode  SNESSetLagPreconditionerPersists(SNES snes,PetscBool flg)
3546: {
3550:   snes->lagpre_persist = flg;
3551:   return(0);
3552: }

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

3557:    Logically Collective on SNES

3559:    Input Parameters:
3560: +  snes - the SNES context
3561: -  force - PETSC_TRUE require at least one iteration

3563:    Options Database Keys:
3564: .    -snes_force_iteration <force> - Sets forcing an iteration

3566:    Notes:
3567:    This is used sometimes with TS to prevent TS from detecting a false steady state solution

3569:    Level: intermediate

3571: .seealso: SNESSetTrustRegionTolerance(), SNESSetDivergenceTolerance()
3572: @*/
3573: PetscErrorCode  SNESSetForceIteration(SNES snes,PetscBool force)
3574: {
3577:   snes->forceiteration = force;
3578:   return(0);
3579: }

3581: /*@
3582:    SNESGetForceIteration - Whether or not to force SNESSolve() take at least one iteration regardless of the initial residual norm

3584:    Logically Collective on SNES

3586:    Input Parameters:
3587: .  snes - the SNES context

3589:    Output Parameter:
3590: .  force - PETSC_TRUE requires at least one iteration.

3592:    Level: intermediate

3594: .seealso: SNESSetForceIteration(), SNESSetTrustRegionTolerance(), SNESSetDivergenceTolerance()
3595: @*/
3596: PetscErrorCode  SNESGetForceIteration(SNES snes,PetscBool *force)
3597: {
3600:   *force = snes->forceiteration;
3601:   return(0);
3602: }

3604: /*@
3605:    SNESSetTolerances - Sets various parameters used in convergence tests.

3607:    Logically Collective on SNES

3609:    Input Parameters:
3610: +  snes - the SNES context
3611: .  abstol - absolute convergence tolerance
3612: .  rtol - relative convergence tolerance
3613: .  stol -  convergence tolerance in terms of the norm of the change in the solution between steps,  || delta x || < stol*|| x ||
3614: .  maxit - maximum number of iterations
3615: -  maxf - maximum number of function evaluations (-1 indicates no limit)

3617:    Options Database Keys:
3618: +    -snes_atol <abstol> - Sets abstol
3619: .    -snes_rtol <rtol> - Sets rtol
3620: .    -snes_stol <stol> - Sets stol
3621: .    -snes_max_it <maxit> - Sets maxit
3622: -    -snes_max_funcs <maxf> - Sets maxf

3624:    Notes:
3625:    The default maximum number of iterations is 50.
3626:    The default maximum number of function evaluations is 1000.

3628:    Level: intermediate

3630: .seealso: SNESSetTrustRegionTolerance(), SNESSetDivergenceTolerance(), SNESSetForceIteration()
3631: @*/
3632: PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
3633: {

3642:   if (abstol != PETSC_DEFAULT) {
3643:     if (abstol < 0.0) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %g must be non-negative",(double)abstol);
3644:     snes->abstol = abstol;
3645:   }
3646:   if (rtol != PETSC_DEFAULT) {
3647:     if (rtol < 0.0 || 1.0 <= rtol) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Relative tolerance %g must be non-negative and less than 1.0",(double)rtol);
3648:     snes->rtol = rtol;
3649:   }
3650:   if (stol != PETSC_DEFAULT) {
3651:     if (stol < 0.0) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %g must be non-negative",(double)stol);
3652:     snes->stol = stol;
3653:   }
3654:   if (maxit != PETSC_DEFAULT) {
3655:     if (maxit < 0) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit);
3656:     snes->max_its = maxit;
3657:   }
3658:   if (maxf != PETSC_DEFAULT) {
3659:     if (maxf < -1) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be -1 or nonnegative",maxf);
3660:     snes->max_funcs = maxf;
3661:   }
3662:   snes->tolerancesset = PETSC_TRUE;
3663:   return(0);
3664: }

3666: /*@
3667:    SNESSetDivergenceTolerance - Sets the divergence tolerance used for the SNES divergence test.

3669:    Logically Collective on SNES

3671:    Input Parameters:
3672: +  snes - the SNES context
3673: -  divtol - the divergence tolerance. Use -1 to deactivate the test.

3675:    Options Database Keys:
3676: .    -snes_divergence_tolerance <divtol> - Sets divtol

3678:    Notes:
3679:    The default divergence tolerance is 1e4.

3681:    Level: intermediate

3683: .seealso: SNESSetTolerances(), SNESGetDivergenceTolerance
3684: @*/
3685: PetscErrorCode  SNESSetDivergenceTolerance(SNES snes,PetscReal divtol)
3686: {

3691:   if (divtol != PETSC_DEFAULT) {
3692:     snes->divtol = divtol;
3693:   }
3694:   else {
3695:     snes->divtol = 1.0e4;
3696:   }
3697:   return(0);
3698: }

3700: /*@
3701:    SNESGetTolerances - Gets various parameters used in convergence tests.

3703:    Not Collective

3705:    Input Parameters:
3706: +  snes - the SNES context
3707: .  atol - absolute convergence tolerance
3708: .  rtol - relative convergence tolerance
3709: .  stol -  convergence tolerance in terms of the norm
3710:            of the change in the solution between steps
3711: .  maxit - maximum number of iterations
3712: -  maxf - maximum number of function evaluations

3714:    Notes:
3715:    The user can specify NULL for any parameter that is not needed.

3717:    Level: intermediate

3719: .seealso: SNESSetTolerances()
3720: @*/
3721: PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
3722: {
3725:   if (atol)  *atol  = snes->abstol;
3726:   if (rtol)  *rtol  = snes->rtol;
3727:   if (stol)  *stol  = snes->stol;
3728:   if (maxit) *maxit = snes->max_its;
3729:   if (maxf)  *maxf  = snes->max_funcs;
3730:   return(0);
3731: }

3733: /*@
3734:    SNESGetDivergenceTolerance - Gets divergence tolerance used in divergence test.

3736:    Not Collective

3738:    Input Parameters:
3739: +  snes - the SNES context
3740: -  divtol - divergence tolerance

3742:    Level: intermediate

3744: .seealso: SNESSetDivergenceTolerance()
3745: @*/
3746: PetscErrorCode  SNESGetDivergenceTolerance(SNES snes,PetscReal *divtol)
3747: {
3750:   if (divtol) *divtol = snes->divtol;
3751:   return(0);
3752: }

3754: /*@
3755:    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.

3757:    Logically Collective on SNES

3759:    Input Parameters:
3760: +  snes - the SNES context
3761: -  tol - tolerance

3763:    Options Database Key:
3764: .  -snes_trtol <tol> - Sets tol

3766:    Level: intermediate

3768: .seealso: SNESSetTolerances()
3769: @*/
3770: PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
3771: {
3775:   snes->deltatol = tol;
3776:   return(0);
3777: }

3779: /*
3780:    Duplicate the lg monitors for SNES from KSP; for some reason with
3781:    dynamic libraries things don't work under Sun4 if we just use
3782:    macros instead of functions
3783: */
3784: PetscErrorCode  SNESMonitorLGResidualNorm(SNES snes,PetscInt it,PetscReal norm,void *ctx)
3785: {

3790:   KSPMonitorLGResidualNorm((KSP)snes,it,norm,ctx);
3791:   return(0);
3792: }

3794: PetscErrorCode  SNESMonitorLGCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *lgctx)
3795: {

3799:   KSPMonitorLGResidualNormCreate(comm,host,label,x,y,m,n,lgctx);
3800:   return(0);
3801: }

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

3805: PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
3806: {
3807:   PetscDrawLG      lg;
3808:   PetscErrorCode   ierr;
3809:   PetscReal        x,y,per;
3810:   PetscViewer      v = (PetscViewer)monctx;
3811:   static PetscReal prev; /* should be in the context */
3812:   PetscDraw        draw;

3816:   PetscViewerDrawGetDrawLG(v,0,&lg);
3817:   if (!n) {PetscDrawLGReset(lg);}
3818:   PetscDrawLGGetDraw(lg,&draw);
3819:   PetscDrawSetTitle(draw,"Residual norm");
3820:   x    = (PetscReal)n;
3821:   if (rnorm > 0.0) y = PetscLog10Real(rnorm);
3822:   else y = -15.0;
3823:   PetscDrawLGAddPoint(lg,&x,&y);
3824:   if (n < 20 || !(n % 5) || snes->reason) {
3825:     PetscDrawLGDraw(lg);
3826:     PetscDrawLGSave(lg);
3827:   }

3829:   PetscViewerDrawGetDrawLG(v,1,&lg);
3830:   if (!n) {PetscDrawLGReset(lg);}
3831:   PetscDrawLGGetDraw(lg,&draw);
3832:   PetscDrawSetTitle(draw,"% elemts > .2*max elemt");
3833:    SNESMonitorRange_Private(snes,n,&per);
3834:   x    = (PetscReal)n;
3835:   y    = 100.0*per;
3836:   PetscDrawLGAddPoint(lg,&x,&y);
3837:   if (n < 20 || !(n % 5) || snes->reason) {
3838:     PetscDrawLGDraw(lg);
3839:     PetscDrawLGSave(lg);
3840:   }

3842:   PetscViewerDrawGetDrawLG(v,2,&lg);
3843:   if (!n) {prev = rnorm;PetscDrawLGReset(lg);}
3844:   PetscDrawLGGetDraw(lg,&draw);
3845:   PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");
3846:   x    = (PetscReal)n;
3847:   y    = (prev - rnorm)/prev;
3848:   PetscDrawLGAddPoint(lg,&x,&y);
3849:   if (n < 20 || !(n % 5) || snes->reason) {
3850:     PetscDrawLGDraw(lg);
3851:     PetscDrawLGSave(lg);
3852:   }

3854:   PetscViewerDrawGetDrawLG(v,3,&lg);
3855:   if (!n) {PetscDrawLGReset(lg);}
3856:   PetscDrawLGGetDraw(lg,&draw);
3857:   PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");
3858:   x    = (PetscReal)n;
3859:   y    = (prev - rnorm)/(prev*per);
3860:   if (n > 2) { /*skip initial crazy value */
3861:     PetscDrawLGAddPoint(lg,&x,&y);
3862:   }
3863:   if (n < 20 || !(n % 5) || snes->reason) {
3864:     PetscDrawLGDraw(lg);
3865:     PetscDrawLGSave(lg);
3866:   }
3867:   prev = rnorm;
3868:   return(0);
3869: }

3871: /*@
3872:    SNESMonitor - runs the user provided monitor routines, if they exist

3874:    Collective on SNES

3876:    Input Parameters:
3877: +  snes - nonlinear solver context obtained from SNESCreate()
3878: .  iter - iteration number
3879: -  rnorm - relative norm of the residual

3881:    Notes:
3882:    This routine is called by the SNES implementations.
3883:    It does not typically need to be called by the user.

3885:    Level: developer

3887: .seealso: SNESMonitorSet()
3888: @*/
3889: PetscErrorCode  SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm)
3890: {
3892:   PetscInt       i,n = snes->numbermonitors;

3895:   VecLockReadPush(snes->vec_sol);
3896:   for (i=0; i<n; i++) {
3897:     (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);
3898:   }
3899:   VecLockReadPop(snes->vec_sol);
3900:   return(0);
3901: }

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

3905: /*MC
3906:     SNESMonitorFunction - functional form passed to SNESMonitorSet() to monitor convergence of nonlinear solver

3908:      Synopsis:
3909: #include <petscsnes.h>
3910: $    PetscErrorCode SNESMonitorFunction(SNES snes,PetscInt its, PetscReal norm,void *mctx)

3912:      Collective on snes

3914:     Input Parameters:
3915: +    snes - the SNES context
3916: .    its - iteration number
3917: .    norm - 2-norm function value (may be estimated)
3918: -    mctx - [optional] monitoring context

3920:    Level: advanced

3922: .seealso:   SNESMonitorSet(), SNESMonitorGet()
3923: M*/

3925: /*@C
3926:    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
3927:    iteration of the nonlinear solver to display the iteration's
3928:    progress.

3930:    Logically Collective on SNES

3932:    Input Parameters:
3933: +  snes - the SNES context
3934: .  f - the monitor function, see SNESMonitorFunction for the calling sequence
3935: .  mctx - [optional] user-defined context for private data for the
3936:           monitor routine (use NULL if no context is desired)
3937: -  monitordestroy - [optional] routine that frees monitor context
3938:           (may be NULL)

3940:    Options Database Keys:
3941: +    -snes_monitor        - sets SNESMonitorDefault()
3942: .    -snes_monitor_lg_residualnorm    - sets line graph monitor,
3943:                             uses SNESMonitorLGCreate()
3944: -    -snes_monitor_cancel - cancels all monitors that have
3945:                             been hardwired into a code by
3946:                             calls to SNESMonitorSet(), but
3947:                             does not cancel those set via
3948:                             the options database.

3950:    Notes:
3951:    Several different monitoring routines may be set by calling
3952:    SNESMonitorSet() multiple times; all will be called in the
3953:    order in which they were set.

3955:    Fortran Notes:
3956:     Only a single monitor function can be set for each SNES object

3958:    Level: intermediate

3960: .seealso: SNESMonitorDefault(), SNESMonitorCancel(), SNESMonitorFunction
3961: @*/
3962: PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*f)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
3963: {
3964:   PetscInt       i;
3966:   PetscBool      identical;

3970:   for (i=0; i<snes->numbermonitors;i++) {
3971:     PetscMonitorCompare((PetscErrorCode (*)(void))f,mctx,monitordestroy,(PetscErrorCode (*)(void))snes->monitor[i],snes->monitorcontext[i],snes->monitordestroy[i],&identical);
3972:     if (identical) return(0);
3973:   }
3974:   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3975:   snes->monitor[snes->numbermonitors]          = f;
3976:   snes->monitordestroy[snes->numbermonitors]   = monitordestroy;
3977:   snes->monitorcontext[snes->numbermonitors++] = (void*)mctx;
3978:   return(0);
3979: }

3981: /*@
3982:    SNESMonitorCancel - Clears all the monitor functions for a SNES object.

3984:    Logically Collective on SNES

3986:    Input Parameters:
3987: .  snes - the SNES context

3989:    Options Database Key:
3990: .  -snes_monitor_cancel - cancels all monitors that have been hardwired
3991:     into a code by calls to SNESMonitorSet(), but does not cancel those
3992:     set via the options database

3994:    Notes:
3995:    There is no way to clear one specific monitor from a SNES object.

3997:    Level: intermediate

3999: .seealso: SNESMonitorDefault(), SNESMonitorSet()
4000: @*/
4001: PetscErrorCode  SNESMonitorCancel(SNES snes)
4002: {
4004:   PetscInt       i;

4008:   for (i=0; i<snes->numbermonitors; i++) {
4009:     if (snes->monitordestroy[i]) {
4010:       (*snes->monitordestroy[i])(&snes->monitorcontext[i]);
4011:     }
4012:   }
4013:   snes->numbermonitors = 0;
4014:   return(0);
4015: }

4017: /*MC
4018:     SNESConvergenceTestFunction - functional form used for testing of convergence of nonlinear solver

4020:      Synopsis:
4021: #include <petscsnes.h>
4022: $     PetscErrorCode SNESConvergenceTest(SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)

4024:      Collective on snes

4026:     Input Parameters:
4027: +    snes - the SNES context
4028: .    it - current iteration (0 is the first and is before any Newton step)
4029: .    xnorm - 2-norm of current iterate
4030: .    gnorm - 2-norm of current step
4031: .    f - 2-norm of function
4032: -    cctx - [optional] convergence context

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

4037:    Level: intermediate

4039: .seealso:   SNESSetConvergenceTest(), SNESGetConvergenceTest()
4040: M*/

4042: /*@C
4043:    SNESSetConvergenceTest - Sets the function that is to be used
4044:    to test for convergence of the nonlinear iterative solution.

4046:    Logically Collective on SNES

4048:    Input Parameters:
4049: +  snes - the SNES context
4050: .  SNESConvergenceTestFunction - routine to test for convergence
4051: .  cctx - [optional] context for private data for the convergence routine  (may be NULL)
4052: -  destroy - [optional] destructor for the context (may be NULL; PETSC_NULL_FUNCTION in Fortran)

4054:    Level: advanced

4056: .seealso: SNESConvergedDefault(), SNESConvergedSkip(), SNESConvergenceTestFunction
4057: @*/
4058: PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*SNESConvergenceTestFunction)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
4059: {

4064:   if (!SNESConvergenceTestFunction) SNESConvergenceTestFunction = SNESConvergedSkip;
4065:   if (snes->ops->convergeddestroy) {
4066:     (*snes->ops->convergeddestroy)(snes->cnvP);
4067:   }
4068:   snes->ops->converged        = SNESConvergenceTestFunction;
4069:   snes->ops->convergeddestroy = destroy;
4070:   snes->cnvP                  = cctx;
4071:   return(0);
4072: }

4074: /*@
4075:    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.

4077:    Not Collective

4079:    Input Parameter:
4080: .  snes - the SNES context

4082:    Output Parameter:
4083: .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
4084:             manual pages for the individual convergence tests for complete lists

4086:    Options Database:
4087: .   -snes_converged_reason - prints the reason to standard out

4089:    Level: intermediate

4091:    Notes:
4092:     Should only be called after the call the SNESSolve() is complete, if it is called earlier it returns the value SNES__CONVERGED_ITERATING.

4094: .seealso: SNESSetConvergenceTest(), SNESSetConvergedReason(), SNESConvergedReason
4095: @*/
4096: PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
4097: {
4101:   *reason = snes->reason;
4102:   return(0);
4103: }

4105: /*@
4106:    SNESSetConvergedReason - Sets the reason the SNES iteration was stopped.

4108:    Not Collective

4110:    Input Parameters:
4111: +  snes - the SNES context
4112: -  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
4113:             manual pages for the individual convergence tests for complete lists

4115:    Level: intermediate

4117: .seealso: SNESGetConvergedReason(), SNESSetConvergenceTest(), SNESConvergedReason
4118: @*/
4119: PetscErrorCode SNESSetConvergedReason(SNES snes,SNESConvergedReason reason)
4120: {
4123:   snes->reason = reason;
4124:   return(0);
4125: }

4127: /*@
4128:    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.

4130:    Logically Collective on SNES

4132:    Input Parameters:
4133: +  snes - iterative context obtained from SNESCreate()
4134: .  a   - array to hold history, this array will contain the function norms computed at each step
4135: .  its - integer array holds the number of linear iterations for each solve.
4136: .  na  - size of a and its
4137: -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
4138:            else it continues storing new values for new nonlinear solves after the old ones

4140:    Notes:
4141:    If 'a' and 'its' are NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
4142:    default array of length 10000 is allocated.

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

4148:    Level: intermediate

4150: .seealso: SNESGetConvergenceHistory()

4152: @*/
4153: PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset)
4154: {

4161:   if (!a) {
4162:     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
4163:     PetscCalloc2(na,&a,na,&its);
4164:     snes->conv_hist_alloc = PETSC_TRUE;
4165:   }
4166:   snes->conv_hist       = a;
4167:   snes->conv_hist_its   = its;
4168:   snes->conv_hist_max   = na;
4169:   snes->conv_hist_len   = 0;
4170:   snes->conv_hist_reset = reset;
4171:   return(0);
4172: }

4174: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4175: #include <engine.h>   /* MATLAB include file */
4176: #include <mex.h>      /* MATLAB include file */

4178: PETSC_EXTERN mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
4179: {
4180:   mxArray   *mat;
4181:   PetscInt  i;
4182:   PetscReal *ar;

4185:   mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
4186:   ar  = (PetscReal*) mxGetData(mat);
4187:   for (i=0; i<snes->conv_hist_len; i++) ar[i] = snes->conv_hist[i];
4188:   PetscFunctionReturn(mat);
4189: }
4190: #endif

4192: /*@C
4193:    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.

4195:    Not Collective

4197:    Input Parameter:
4198: .  snes - iterative context obtained from SNESCreate()

4200:    Output Parameters:
4201: +  a   - array to hold history
4202: .  its - integer array holds the number of linear iterations (or
4203:          negative if not converged) for each solve.
4204: -  na  - size of a and its

4206:    Notes:
4207:     The calling sequence for this routine in Fortran is
4208: $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)

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

4214:    Level: intermediate

4216: .seealso: SNESSetConvergenceHistory()

4218: @*/
4219: PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
4220: {
4223:   if (a)   *a   = snes->conv_hist;
4224:   if (its) *its = snes->conv_hist_its;
4225:   if (na)  *na  = snes->conv_hist_len;
4226:   return(0);
4227: }

4229: /*@C
4230:   SNESSetUpdate - Sets the general-purpose update function called
4231:   at the beginning of every iteration of the nonlinear solve. Specifically
4232:   it is called just before the Jacobian is "evaluated".

4234:   Logically Collective on SNES

4236:   Input Parameters:
4237: + snes - The nonlinear solver context
4238: - func - The function

4240:   Calling sequence of func:
4241: $ func (SNES snes, PetscInt step);

4243: . step - The current step of the iteration

4245:   Level: advanced

4247:   Note: This is NOT what one uses to update the ghost points before a function evaluation, that should be done at the beginning of your FormFunction()
4248:         This is not used by most users.

4250: .seealso SNESSetJacobian(), SNESSolve()
4251: @*/
4252: PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
4253: {
4256:   snes->ops->update = func;
4257:   return(0);
4258: }

4260: /*
4261:    SNESScaleStep_Private - Scales a step so that its length is less than the
4262:    positive parameter delta.

4264:     Input Parameters:
4265: +   snes - the SNES context
4266: .   y - approximate solution of linear system
4267: .   fnorm - 2-norm of current function
4268: -   delta - trust region size

4270:     Output Parameters:
4271: +   gpnorm - predicted function norm at the new point, assuming local
4272:     linearization.  The value is zero if the step lies within the trust
4273:     region, and exceeds zero otherwise.
4274: -   ynorm - 2-norm of the step

4276:     Note:
4277:     For non-trust region methods such as SNESNEWTONLS, the parameter delta
4278:     is set to be the maximum allowable step size.

4280: */
4281: PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
4282: {
4283:   PetscReal      nrm;
4284:   PetscScalar    cnorm;


4292:   VecNorm(y,NORM_2,&nrm);
4293:   if (nrm > *delta) {
4294:     nrm     = *delta/nrm;
4295:     *gpnorm = (1.0 - nrm)*(*fnorm);
4296:     cnorm   = nrm;
4297:     VecScale(y,cnorm);
4298:     *ynorm  = *delta;
4299:   } else {
4300:     *gpnorm = 0.0;
4301:     *ynorm  = nrm;
4302:   }
4303:   return(0);
4304: }

4306: /*@C
4307:    SNESConvergedReasonView - Displays the reason a SNES solve converged or diverged to a viewer

4309:    Collective on SNES

4311:    Parameter:
4312: +  snes - iterative context obtained from SNESCreate()
4313: -  viewer - the viewer to display the reason


4316:    Options Database Keys:
4317: +  -snes_converged_reason - print reason for converged or diverged, also prints number of iterations
4318: -  -snes_converged_reason ::failed - only print reason and number of iterations when diverged

4320:   Notes:
4321:      To change the format of the output call PetscViewerPushFormat(viewer,format) before this call. Use PETSC_VIEWER_DEFAULT for the default,
4322:      use PETSC_VIEWER_FAILED to only display a reason if it fails.

4324:    Level: beginner

4326: .seealso: SNESCreate(), SNESSetUp(), SNESDestroy(), SNESSetTolerances(), SNESConvergedDefault(), SNESGetConvergedReason(), SNESConvergedReasonViewFromOptions(),
4327:           PetscViewerPushFormat(), PetscViewerPopFormat()

4329: @*/
4330: PetscErrorCode  SNESConvergedReasonView(SNES snes,PetscViewer viewer)
4331: {
4332:   PetscViewerFormat format;
4333:   PetscBool         isAscii;
4334:   PetscErrorCode    ierr;

4337:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
4338:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isAscii);
4339:   if (isAscii) {
4340:     PetscViewerGetFormat(viewer, &format);
4341:     PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);
4342:     if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
4343:       DM              dm;
4344:       Vec             u;
4345:       PetscDS         prob;
4346:       PetscInt        Nf, f;
4347:       PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
4348:       void            **exactCtx;
4349:       PetscReal       error;

4351:       SNESGetDM(snes, &dm);
4352:       SNESGetSolution(snes, &u);
4353:       DMGetDS(dm, &prob);
4354:       PetscDSGetNumFields(prob, &Nf);
4355:       PetscMalloc2(Nf, &exactSol, Nf, &exactCtx);
4356:       for (f = 0; f < Nf; ++f) {PetscDSGetExactSolution(prob, f, &exactSol[f], &exactCtx[f]);}
4357:       DMComputeL2Diff(dm, 0.0, exactSol, exactCtx, u, &error);
4358:       PetscFree2(exactSol, exactCtx);
4359:       if (error < 1.0e-11) {PetscViewerASCIIPrintf(viewer, "L_2 Error: < 1.0e-11\n");}
4360:       else                 {PetscViewerASCIIPrintf(viewer, "L_2 Error: %g\n", error);}
4361:     }
4362:     if (snes->reason > 0 && format != PETSC_VIEWER_FAILED) {
4363:       if (((PetscObject) snes)->prefix) {
4364:         PetscViewerASCIIPrintf(viewer,"Nonlinear %s solve converged due to %s iterations %D\n",((PetscObject) snes)->prefix,SNESConvergedReasons[snes->reason],snes->iter);
4365:       } else {
4366:         PetscViewerASCIIPrintf(viewer,"Nonlinear solve converged due to %s iterations %D\n",SNESConvergedReasons[snes->reason],snes->iter);
4367:       }
4368:     } else if (snes->reason <= 0) {
4369:       if (((PetscObject) snes)->prefix) {
4370:         PetscViewerASCIIPrintf(viewer,"Nonlinear %s solve did not converge due to %s iterations %D\n",((PetscObject) snes)->prefix,SNESConvergedReasons[snes->reason],snes->iter);
4371:       } else {
4372:         PetscViewerASCIIPrintf(viewer,"Nonlinear solve did not converge due to %s iterations %D\n",SNESConvergedReasons[snes->reason],snes->iter);
4373:       }
4374:     }
4375:     PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);
4376:   }
4377:   return(0);
4378: }

4380: /*@
4381:   SNESConvergedReasonViewFromOptions - Processes command line options to determine if/how a SNESReason is to be viewed.

4383:   Collective on SNES

4385:   Input Parameters:
4386: . snes   - the SNES object

4388:   Level: intermediate

4390: .seealso: SNESCreate(), SNESSetUp(), SNESDestroy(), SNESSetTolerances(), SNESConvergedDefault(), SNESGetConvergedReason(), SNESConvergedReasonView()

4392: @*/
4393: PetscErrorCode SNESConvergedReasonViewFromOptions(SNES snes)
4394: {
4395:   PetscErrorCode    ierr;
4396:   PetscViewer       viewer;
4397:   PetscBool         flg;
4398:   static PetscBool  incall = PETSC_FALSE;
4399:   PetscViewerFormat format;

4402:   if (incall) return(0);
4403:   incall = PETSC_TRUE;
4404:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_converged_reason",&viewer,&format,&flg);
4405:   if (flg) {
4406:     PetscViewerPushFormat(viewer,format);
4407:     SNESConvergedReasonView(snes,viewer);
4408:     PetscViewerPopFormat(viewer);
4409:     PetscViewerDestroy(&viewer);
4410:   }
4411:   incall = PETSC_FALSE;
4412:   return(0);
4413: }

4415: /*@
4416:    SNESSolve - Solves a nonlinear system F(x) = b.
4417:    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().

4419:    Collective on SNES

4421:    Input Parameters:
4422: +  snes - the SNES context
4423: .  b - the constant part of the equation F(x) = b, or NULL to use zero.
4424: -  x - the solution vector.

4426:    Notes:
4427:    The user should initialize the vector,x, with the initial guess
4428:    for the nonlinear solve prior to calling SNESSolve.  In particular,
4429:    to employ an initial guess of zero, the user should explicitly set
4430:    this vector to zero by calling VecSet().

4432:    Level: beginner

4434: .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetGridSequence(), SNESGetSolution()
4435: @*/
4436: PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
4437: {
4438:   PetscErrorCode    ierr;
4439:   PetscBool         flg;
4440:   PetscInt          grid;
4441:   Vec               xcreated = NULL;
4442:   DM                dm;


4451:   /* High level operations using the nonlinear solver */
4452:   {
4453:     PetscViewer       viewer;
4454:     PetscViewerFormat format;
4455:     PetscInt          num;
4456:     PetscBool         flg;
4457:     static PetscBool  incall = PETSC_FALSE;

4459:     if (!incall) {
4460:       /* Estimate the convergence rate of the discretization */
4461:       PetscOptionsGetViewer(PetscObjectComm((PetscObject) snes),((PetscObject)snes)->options, ((PetscObject) snes)->prefix, "-snes_convergence_estimate", &viewer, &format, &flg);
4462:       if (flg) {
4463:         PetscConvEst conv;
4464:         DM           dm;
4465:         PetscReal   *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */
4466:         PetscInt     Nf;

4468:         incall = PETSC_TRUE;
4469:         SNESGetDM(snes, &dm);
4470:         DMGetNumFields(dm, &Nf);
4471:         PetscCalloc1(Nf, &alpha);
4472:         PetscConvEstCreate(PetscObjectComm((PetscObject) snes), &conv);
4473:         PetscConvEstSetSolver(conv, (PetscObject) snes);
4474:         PetscConvEstSetFromOptions(conv);
4475:         PetscConvEstSetUp(conv);
4476:         PetscConvEstGetConvRate(conv, alpha);
4477:         PetscViewerPushFormat(viewer, format);
4478:         PetscConvEstRateView(conv, alpha, viewer);
4479:         PetscViewerPopFormat(viewer);
4480:         PetscViewerDestroy(&viewer);
4481:         PetscConvEstDestroy(&conv);
4482:         PetscFree(alpha);
4483:         incall = PETSC_FALSE;
4484:       }
4485:       /* Adaptively refine the initial grid */
4486:       num  = 1;
4487:       PetscOptionsGetInt(NULL, ((PetscObject) snes)->prefix, "-snes_adapt_initial", &num, &flg);
4488:       if (flg) {
4489:         DMAdaptor adaptor;

4491:         incall = PETSC_TRUE;
4492:         DMAdaptorCreate(PetscObjectComm((PetscObject)snes), &adaptor);
4493:         DMAdaptorSetSolver(adaptor, snes);
4494:         DMAdaptorSetSequenceLength(adaptor, num);
4495:         DMAdaptorSetFromOptions(adaptor);
4496:         DMAdaptorSetUp(adaptor);
4497:         DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_INITIAL, &dm, &x);
4498:         DMAdaptorDestroy(&adaptor);
4499:         incall = PETSC_FALSE;
4500:       }
4501:       /* Use grid sequencing to adapt */
4502:       num  = 0;
4503:       PetscOptionsGetInt(NULL, ((PetscObject) snes)->prefix, "-snes_adapt_sequence", &num, NULL);
4504:       if (num) {
4505:         DMAdaptor adaptor;

4507:         incall = PETSC_TRUE;
4508:         DMAdaptorCreate(PetscObjectComm((PetscObject)snes), &adaptor);
4509:         DMAdaptorSetSolver(adaptor, snes);
4510:         DMAdaptorSetSequenceLength(adaptor, num);
4511:         DMAdaptorSetFromOptions(adaptor);
4512:         DMAdaptorSetUp(adaptor);
4513:         DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_SEQUENTIAL, &dm, &x);
4514:         DMAdaptorDestroy(&adaptor);
4515:         incall = PETSC_FALSE;
4516:       }
4517:     }
4518:   }
4519:   if (!x) {
4520:     SNESGetDM(snes,&dm);
4521:     DMCreateGlobalVector(dm,&xcreated);
4522:     x    = xcreated;
4523:   }
4524:   SNESViewFromOptions(snes,NULL,"-snes_view_pre");

4526:   for (grid=0; grid<snes->gridsequence; grid++) {PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)));}
4527:   for (grid=0; grid<snes->gridsequence+1; grid++) {

4529:     /* set solution vector */
4530:     if (!grid) {PetscObjectReference((PetscObject)x);}
4531:     VecDestroy(&snes->vec_sol);
4532:     snes->vec_sol = x;
4533:     SNESGetDM(snes,&dm);

4535:     /* set affine vector if provided */
4536:     if (b) { PetscObjectReference((PetscObject)b); }
4537:     VecDestroy(&snes->vec_rhs);
4538:     snes->vec_rhs = b;

4540:     if (snes->vec_rhs && (snes->vec_func == snes->vec_rhs)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Right hand side vector cannot be function vector");
4541:     if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
4542:     if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
4543:     if (!snes->vec_sol_update /* && snes->vec_sol */) {
4544:       VecDuplicate(snes->vec_sol,&snes->vec_sol_update);
4545:       PetscLogObjectParent((PetscObject)snes,(PetscObject)snes->vec_sol_update);
4546:     }
4547:     DMShellSetGlobalVector(dm,snes->vec_sol);
4548:     SNESSetUp(snes);

4550:     if (!grid) {
4551:       if (snes->ops->computeinitialguess) {
4552:         (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);
4553:       }
4554:     }

4556:     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
4557:     if (snes->counters_reset) {snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;}

4559:     PetscLogEventBegin(SNES_Solve,snes,0,0,0);
4560:     (*snes->ops->solve)(snes);
4561:     PetscLogEventEnd(SNES_Solve,snes,0,0,0);
4562:     if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
4563:     snes->domainerror = PETSC_FALSE; /* clear the flag if it has been set */

4565:     if (snes->lagjac_persist) snes->jac_iter += snes->iter;
4566:     if (snes->lagpre_persist) snes->pre_iter += snes->iter;

4568:     PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_test_local_min",NULL,NULL,&flg);
4569:     if (flg && !PetscPreLoadingOn) { SNESTestLocalMin(snes); }
4570:     SNESConvergedReasonViewFromOptions(snes);

4572:     if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
4573:     if (snes->reason < 0) break;
4574:     if (grid <  snes->gridsequence) {
4575:       DM  fine;
4576:       Vec xnew;
4577:       Mat interp;

4579:       DMRefine(snes->dm,PetscObjectComm((PetscObject)snes),&fine);
4580:       if (!fine) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_INCOMP,"DMRefine() did not perform any refinement, cannot continue grid sequencing");
4581:       DMCreateInterpolation(snes->dm,fine,&interp,NULL);
4582:       DMCreateGlobalVector(fine,&xnew);
4583:       MatInterpolate(interp,x,xnew);
4584:       DMInterpolate(snes->dm,interp,fine);
4585:       MatDestroy(&interp);
4586:       x    = xnew;

4588:       SNESReset(snes);
4589:       SNESSetDM(snes,fine);
4590:       SNESResetFromOptions(snes);
4591:       DMDestroy(&fine);
4592:       PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)));
4593:     }
4594:   }
4595:   SNESViewFromOptions(snes,NULL,"-snes_view");
4596:   VecViewFromOptions(snes->vec_sol,(PetscObject)snes,"-snes_view_solution");
4597:   DMMonitor(snes->dm);

4599:   VecDestroy(&xcreated);
4600:   PetscObjectSAWsBlock((PetscObject)snes);
4601:   return(0);
4602: }

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

4606: /*@C
4607:    SNESSetType - Sets the method for the nonlinear solver.

4609:    Collective on SNES

4611:    Input Parameters:
4612: +  snes - the SNES context
4613: -  type - a known method

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

4619:    Notes:
4620:    See "petsc/include/petscsnes.h" for available methods (for instance)
4621: +    SNESNEWTONLS - Newton's method with line search
4622:      (systems of nonlinear equations)
4623: -    SNESNEWTONTR - Newton's method with trust region
4624:      (systems of nonlinear equations)

4626:   Normally, it is best to use the SNESSetFromOptions() command and then
4627:   set the SNES solver type from the options database rather than by using
4628:   this routine.  Using the options database provides the user with
4629:   maximum flexibility in evaluating the many nonlinear solvers.
4630:   The SNESSetType() routine is provided for those situations where it
4631:   is necessary to set the nonlinear solver independently of the command
4632:   line or options database.  This might be the case, for example, when
4633:   the choice of solver changes during the execution of the program,
4634:   and the user's application is taking responsibility for choosing the
4635:   appropriate method.

4637:     Developer Notes:
4638:     SNESRegister() adds a constructor for a new SNESType to SNESList, SNESSetType() locates
4639:     the constructor in that list and calls it to create the spexific object.

4641:   Level: intermediate

4643: .seealso: SNESType, SNESCreate(), SNESDestroy(), SNESGetType(), SNESSetFromOptions()

4645: @*/
4646: PetscErrorCode  SNESSetType(SNES snes,SNESType type)
4647: {
4648:   PetscErrorCode ierr,(*r)(SNES);
4649:   PetscBool      match;


4655:   PetscObjectTypeCompare((PetscObject)snes,type,&match);
4656:   if (match) return(0);

4658:   PetscFunctionListFind(SNESList,type,&r);
4659:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
4660:   /* Destroy the previous private SNES context */
4661:   if (snes->ops->destroy) {
4662:     (*(snes)->ops->destroy)(snes);
4663:     snes->ops->destroy = NULL;
4664:   }
4665:   /* Reinitialize function pointers in SNESOps structure */
4666:   snes->ops->setup          = NULL;
4667:   snes->ops->solve          = NULL;
4668:   snes->ops->view           = NULL;
4669:   snes->ops->setfromoptions = NULL;
4670:   snes->ops->destroy        = NULL;

4672:   /* It may happen the user has customized the line search before calling SNESSetType */
4673:   if (((PetscObject)snes)->type_name) {
4674:     SNESLineSearchDestroy(&snes->linesearch);
4675:   }

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

4680:   PetscObjectChangeTypeName((PetscObject)snes,type);
4681:   (*r)(snes);
4682:   return(0);
4683: }

4685: /*@C
4686:    SNESGetType - Gets the SNES method type and name (as a string).

4688:    Not Collective

4690:    Input Parameter:
4691: .  snes - nonlinear solver context

4693:    Output Parameter:
4694: .  type - SNES method (a character string)

4696:    Level: intermediate

4698: @*/
4699: PetscErrorCode  SNESGetType(SNES snes,SNESType *type)
4700: {
4704:   *type = ((PetscObject)snes)->type_name;
4705:   return(0);
4706: }

4708: /*@
4709:   SNESSetSolution - Sets the solution vector for use by the SNES routines.

4711:   Logically Collective on SNES

4713:   Input Parameters:
4714: + snes - the SNES context obtained from SNESCreate()
4715: - u    - the solution vector

4717:   Level: beginner

4719: @*/
4720: PetscErrorCode SNESSetSolution(SNES snes, Vec u)
4721: {
4722:   DM             dm;

4728:   PetscObjectReference((PetscObject) u);
4729:   VecDestroy(&snes->vec_sol);

4731:   snes->vec_sol = u;

4733:   SNESGetDM(snes, &dm);
4734:   DMShellSetGlobalVector(dm, u);
4735:   return(0);
4736: }

4738: /*@
4739:    SNESGetSolution - Returns the vector where the approximate solution is
4740:    stored. This is the fine grid solution when using SNESSetGridSequence().

4742:    Not Collective, but Vec is parallel if SNES is parallel

4744:    Input Parameter:
4745: .  snes - the SNES context

4747:    Output Parameter:
4748: .  x - the solution

4750:    Level: intermediate

4752: .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
4753: @*/
4754: PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
4755: {
4759:   *x = snes->vec_sol;
4760:   return(0);
4761: }

4763: /*@
4764:    SNESGetSolutionUpdate - Returns the vector where the solution update is
4765:    stored.

4767:    Not Collective, but Vec is parallel if SNES is parallel

4769:    Input Parameter:
4770: .  snes - the SNES context

4772:    Output Parameter:
4773: .  x - the solution update

4775:    Level: advanced

4777: .seealso: SNESGetSolution(), SNESGetFunction()
4778: @*/
4779: PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
4780: {
4784:   *x = snes->vec_sol_update;
4785:   return(0);
4786: }

4788: /*@C
4789:    SNESGetFunction - Returns the vector where the function is stored.

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

4793:    Input Parameter:
4794: .  snes - the SNES context

4796:    Output Parameter:
4797: +  r - the vector that is used to store residuals (or NULL if you don't want it)
4798: .  f - the function (or NULL if you don't want it); see SNESFunction for calling sequence details
4799: -  ctx - the function context (or NULL if you don't want it)

4801:    Level: advanced

4803:     Notes: The vector r DOES NOT, in general contain the current value of the SNES nonlinear function

4805: .seealso: SNESSetFunction(), SNESGetSolution(), SNESFunction
4806: @*/
4807: PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**f)(SNES,Vec,Vec,void*),void **ctx)
4808: {
4810:   DM             dm;

4814:   if (r) {
4815:     if (!snes->vec_func) {
4816:       if (snes->vec_rhs) {
4817:         VecDuplicate(snes->vec_rhs,&snes->vec_func);
4818:       } else if (snes->vec_sol) {
4819:         VecDuplicate(snes->vec_sol,&snes->vec_func);
4820:       } else if (snes->dm) {
4821:         DMCreateGlobalVector(snes->dm,&snes->vec_func);
4822:       }
4823:     }
4824:     *r = snes->vec_func;
4825:   }
4826:   SNESGetDM(snes,&dm);
4827:   DMSNESGetFunction(dm,f,ctx);
4828:   return(0);
4829: }

4831: /*@C
4832:    SNESGetNGS - Returns the NGS function and context.

4834:    Input Parameter:
4835: .  snes - the SNES context

4837:    Output Parameter:
4838: +  f - the function (or NULL) see SNESNGSFunction for details
4839: -  ctx    - the function context (or NULL)

4841:    Level: advanced

4843: .seealso: SNESSetNGS(), SNESGetFunction()
4844: @*/

4846: PetscErrorCode SNESGetNGS (SNES snes, PetscErrorCode (**f)(SNES, Vec, Vec, void*), void ** ctx)
4847: {
4849:   DM             dm;

4853:   SNESGetDM(snes,&dm);
4854:   DMSNESGetNGS(dm,f,ctx);
4855:   return(0);
4856: }

4858: /*@C
4859:    SNESSetOptionsPrefix - Sets the prefix used for searching for all
4860:    SNES options in the database.

4862:    Logically Collective on SNES

4864:    Input Parameter:
4865: +  snes - the SNES context
4866: -  prefix - the prefix to prepend to all option names

4868:    Notes:
4869:    A hyphen (-) must NOT be given at the beginning of the prefix name.
4870:    The first character of all runtime options is AUTOMATICALLY the hyphen.

4872:    Level: advanced

4874: .seealso: SNESSetFromOptions()
4875: @*/
4876: PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
4877: {

4882:   PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);
4883:   if (!snes->ksp) {SNESGetKSP(snes,&snes->ksp);}
4884:   if (snes->linesearch) {
4885:     SNESGetLineSearch(snes,&snes->linesearch);
4886:     PetscObjectSetOptionsPrefix((PetscObject)snes->linesearch,prefix);
4887:   }
4888:   KSPSetOptionsPrefix(snes->ksp,prefix);
4889:   return(0);
4890: }

4892: /*@C
4893:    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
4894:    SNES options in the database.

4896:    Logically Collective on SNES

4898:    Input Parameters:
4899: +  snes - the SNES context
4900: -  prefix - the prefix to prepend to all option names

4902:    Notes:
4903:    A hyphen (-) must NOT be given at the beginning of the prefix name.
4904:    The first character of all runtime options is AUTOMATICALLY the hyphen.

4906:    Level: advanced

4908: .seealso: SNESGetOptionsPrefix()
4909: @*/
4910: PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
4911: {

4916:   PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);
4917:   if (!snes->ksp) {SNESGetKSP(snes,&snes->ksp);}
4918:   if (snes->linesearch) {
4919:     SNESGetLineSearch(snes,&snes->linesearch);
4920:     PetscObjectAppendOptionsPrefix((PetscObject)snes->linesearch,prefix);
4921:   }
4922:   KSPAppendOptionsPrefix(snes->ksp,prefix);
4923:   return(0);
4924: }

4926: /*@C
4927:    SNESGetOptionsPrefix - Sets the prefix used for searching for all
4928:    SNES options in the database.

4930:    Not Collective

4932:    Input Parameter:
4933: .  snes - the SNES context

4935:    Output Parameter:
4936: .  prefix - pointer to the prefix string used

4938:    Notes:
4939:     On the fortran side, the user should pass in a string 'prefix' of
4940:    sufficient length to hold the prefix.

4942:    Level: advanced

4944: .seealso: SNESAppendOptionsPrefix()
4945: @*/
4946: PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
4947: {

4952:   PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);
4953:   return(0);
4954: }


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

4960:    Not collective

4962:    Input Parameters:
4963: +  name_solver - name of a new user-defined solver
4964: -  routine_create - routine to create method context

4966:    Notes:
4967:    SNESRegister() may be called multiple times to add several user-defined solvers.

4969:    Sample usage:
4970: .vb
4971:    SNESRegister("my_solver",MySolverCreate);
4972: .ve

4974:    Then, your solver can be chosen with the procedural interface via
4975: $     SNESSetType(snes,"my_solver")
4976:    or at runtime via the option
4977: $     -snes_type my_solver

4979:    Level: advanced

4981:     Note: If your function is not being put into a shared library then use SNESRegister() instead

4983: .seealso: SNESRegisterAll(), SNESRegisterDestroy()

4985:   Level: advanced
4986: @*/
4987: PetscErrorCode  SNESRegister(const char sname[],PetscErrorCode (*function)(SNES))
4988: {

4992:   SNESInitializePackage();
4993:   PetscFunctionListAdd(&SNESList,sname,function);
4994:   return(0);
4995: }

4997: PetscErrorCode  SNESTestLocalMin(SNES snes)
4998: {
5000:   PetscInt       N,i,j;
5001:   Vec            u,uh,fh;
5002:   PetscScalar    value;
5003:   PetscReal      norm;

5006:   SNESGetSolution(snes,&u);
5007:   VecDuplicate(u,&uh);
5008:   VecDuplicate(u,&fh);

5010:   /* currently only works for sequential */
5011:   PetscPrintf(PetscObjectComm((PetscObject)snes),"Testing FormFunction() for local min\n");
5012:   VecGetSize(u,&N);
5013:   for (i=0; i<N; i++) {
5014:     VecCopy(u,uh);
5015:     PetscPrintf(PetscObjectComm((PetscObject)snes),"i = %D\n",i);
5016:     for (j=-10; j<11; j++) {
5017:       value = PetscSign(j)*PetscExpReal(PetscAbs(j)-10.0);
5018:       VecSetValue(uh,i,value,ADD_VALUES);
5019:       SNESComputeFunction(snes,uh,fh);
5020:       VecNorm(fh,NORM_2,&norm);
5021:       PetscPrintf(PetscObjectComm((PetscObject)snes),"       j norm %D %18.16e\n",j,norm);
5022:       value = -value;
5023:       VecSetValue(uh,i,value,ADD_VALUES);
5024:     }
5025:   }
5026:   VecDestroy(&uh);
5027:   VecDestroy(&fh);
5028:   return(0);
5029: }

5031: /*@
5032:    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
5033:    computing relative tolerance for linear solvers within an inexact
5034:    Newton method.

5036:    Logically Collective on SNES

5038:    Input Parameters:
5039: +  snes - SNES context
5040: -  flag - PETSC_TRUE or PETSC_FALSE

5042:     Options Database:
5043: +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
5044: .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
5045: .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
5046: .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
5047: .  -snes_ksp_ew_gamma <gamma> - Sets gamma
5048: .  -snes_ksp_ew_alpha <alpha> - Sets alpha
5049: .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
5050: -  -snes_ksp_ew_threshold <threshold> - Sets threshold

5052:    Notes:
5053:    Currently, the default is to use a constant relative tolerance for
5054:    the inner linear solvers.  Alternatively, one can use the
5055:    Eisenstat-Walker method, where the relative convergence tolerance
5056:    is reset at each Newton iteration according progress of the nonlinear
5057:    solver.

5059:    Level: advanced

5061:    Reference:
5062:    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
5063:    inexact Newton method", SISC 17 (1), pp.16-32, 1996.

5065: .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
5066: @*/
5067: PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool flag)
5068: {
5072:   snes->ksp_ewconv = flag;
5073:   return(0);
5074: }

5076: /*@
5077:    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
5078:    for computing relative tolerance for linear solvers within an
5079:    inexact Newton method.

5081:    Not Collective

5083:    Input Parameter:
5084: .  snes - SNES context

5086:    Output Parameter:
5087: .  flag - PETSC_TRUE or PETSC_FALSE

5089:    Notes:
5090:    Currently, the default is to use a constant relative tolerance for
5091:    the inner linear solvers.  Alternatively, one can use the
5092:    Eisenstat-Walker method, where the relative convergence tolerance
5093:    is reset at each Newton iteration according progress of the nonlinear
5094:    solver.

5096:    Level: advanced

5098:    Reference:
5099:    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
5100:    inexact Newton method", SISC 17 (1), pp.16-32, 1996.

5102: .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
5103: @*/
5104: PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
5105: {
5109:   *flag = snes->ksp_ewconv;
5110:   return(0);
5111: }

5113: /*@
5114:    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
5115:    convergence criteria for the linear solvers within an inexact
5116:    Newton method.

5118:    Logically Collective on SNES

5120:    Input Parameters:
5121: +    snes - SNES context
5122: .    version - version 1, 2 (default is 2) or 3
5123: .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
5124: .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
5125: .    gamma - multiplicative factor for version 2 rtol computation
5126:              (0 <= gamma2 <= 1)
5127: .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
5128: .    alpha2 - power for safeguard
5129: -    threshold - threshold for imposing safeguard (0 < threshold < 1)

5131:    Note:
5132:    Version 3 was contributed by Luis Chacon, June 2006.

5134:    Use PETSC_DEFAULT to retain the default for any of the parameters.

5136:    Level: advanced

5138:    Reference:
5139:    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
5140:    inexact Newton method", Utah State University Math. Stat. Dept. Res.
5141:    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.

5143: .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
5144: @*/
5145: PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
5146: {
5147:   SNESKSPEW *kctx;

5151:   kctx = (SNESKSPEW*)snes->kspconvctx;
5152:   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");

5161:   if (version != PETSC_DEFAULT)   kctx->version   = version;
5162:   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
5163:   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
5164:   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
5165:   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
5166:   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
5167:   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;

5169:   if (kctx->version < 1 || kctx->version > 3) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
5170:   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %g",(double)kctx->rtol_0);
5171:   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%g) < 1.0\n",(double)kctx->rtol_max);
5172:   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%g) <= 1.0\n",(double)kctx->gamma);
5173:   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%g) <= 2.0\n",(double)kctx->alpha);
5174:   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%g) < 1.0\n",(double)kctx->threshold);
5175:   return(0);
5176: }

5178: /*@
5179:    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
5180:    convergence criteria for the linear solvers within an inexact
5181:    Newton method.

5183:    Not Collective

5185:    Input Parameters:
5186:      snes - SNES context

5188:    Output Parameters:
5189: +    version - version 1, 2 (default is 2) or 3
5190: .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
5191: .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
5192: .    gamma - multiplicative factor for version 2 rtol computation (0 <= gamma2 <= 1)
5193: .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
5194: .    alpha2 - power for safeguard
5195: -    threshold - threshold for imposing safeguard (0 < threshold < 1)

5197:    Level: advanced

5199: .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
5200: @*/
5201: PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
5202: {
5203:   SNESKSPEW *kctx;

5207:   kctx = (SNESKSPEW*)snes->kspconvctx;
5208:   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
5209:   if (version)   *version   = kctx->version;
5210:   if (rtol_0)    *rtol_0    = kctx->rtol_0;
5211:   if (rtol_max)  *rtol_max  = kctx->rtol_max;
5212:   if (gamma)     *gamma     = kctx->gamma;
5213:   if (alpha)     *alpha     = kctx->alpha;
5214:   if (alpha2)    *alpha2    = kctx->alpha2;
5215:   if (threshold) *threshold = kctx->threshold;
5216:   return(0);
5217: }

5219:  PetscErrorCode KSPPreSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes)
5220: {
5222:   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
5223:   PetscReal      rtol  = PETSC_DEFAULT,stol;

5226:   if (!snes->ksp_ewconv) return(0);
5227:   if (!snes->iter) {
5228:     rtol = kctx->rtol_0; /* first time in, so use the original user rtol */
5229:     VecNorm(snes->vec_func,NORM_2,&kctx->norm_first);
5230:   }
5231:   else {
5232:     if (kctx->version == 1) {
5233:       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
5234:       if (rtol < 0.0) rtol = -rtol;
5235:       stol = PetscPowReal(kctx->rtol_last,kctx->alpha2);
5236:       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
5237:     } else if (kctx->version == 2) {
5238:       rtol = kctx->gamma * PetscPowReal(snes->norm/kctx->norm_last,kctx->alpha);
5239:       stol = kctx->gamma * PetscPowReal(kctx->rtol_last,kctx->alpha);
5240:       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
5241:     } else if (kctx->version == 3) { /* contributed by Luis Chacon, June 2006. */
5242:       rtol = kctx->gamma * PetscPowReal(snes->norm/kctx->norm_last,kctx->alpha);
5243:       /* safeguard: avoid sharp decrease of rtol */
5244:       stol = kctx->gamma*PetscPowReal(kctx->rtol_last,kctx->alpha);
5245:       stol = PetscMax(rtol,stol);
5246:       rtol = PetscMin(kctx->rtol_0,stol);
5247:       /* safeguard: avoid oversolving */
5248:       stol = kctx->gamma*(kctx->norm_first*snes->rtol)/snes->norm;
5249:       stol = PetscMax(rtol,stol);
5250:       rtol = PetscMin(kctx->rtol_0,stol);
5251:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
5252:   }
5253:   /* safeguard: avoid rtol greater than one */
5254:   rtol = PetscMin(rtol,kctx->rtol_max);
5255:   KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
5256:   PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%g\n",snes->iter,kctx->version,(double)rtol);
5257:   return(0);
5258: }

5260: PetscErrorCode KSPPostSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes)
5261: {
5263:   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
5264:   PCSide         pcside;
5265:   Vec            lres;

5268:   if (!snes->ksp_ewconv) return(0);
5269:   KSPGetTolerances(ksp,&kctx->rtol_last,NULL,NULL,NULL);
5270:   kctx->norm_last = snes->norm;
5271:   if (kctx->version == 1) {
5272:     PC        pc;
5273:     PetscBool isNone;

5275:     KSPGetPC(ksp, &pc);
5276:     PetscObjectTypeCompare((PetscObject) pc, PCNONE, &isNone);
5277:     KSPGetPCSide(ksp,&pcside);
5278:      if (pcside == PC_RIGHT || isNone) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
5279:       /* KSP residual is true linear residual */
5280:       KSPGetResidualNorm(ksp,&kctx->lresid_last);
5281:     } else {
5282:       /* KSP residual is preconditioned residual */
5283:       /* compute true linear residual norm */
5284:       VecDuplicate(b,&lres);
5285:       MatMult(snes->jacobian,x,lres);
5286:       VecAYPX(lres,-1.0,b);
5287:       VecNorm(lres,NORM_2,&kctx->lresid_last);
5288:       VecDestroy(&lres);
5289:     }
5290:   }
5291:   return(0);
5292: }

5294: /*@
5295:    SNESGetKSP - Returns the KSP context for a SNES solver.

5297:    Not Collective, but if SNES object is parallel, then KSP object is parallel

5299:    Input Parameter:
5300: .  snes - the SNES context

5302:    Output Parameter:
5303: .  ksp - the KSP context

5305:    Notes:
5306:    The user can then directly manipulate the KSP context to set various
5307:    options, etc.  Likewise, the user can then extract and manipulate the
5308:    PC contexts as well.

5310:    Level: beginner

5312: .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
5313: @*/
5314: PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
5315: {


5322:   if (!snes->ksp) {
5323:     PetscBool monitor = PETSC_FALSE;

5325:     KSPCreate(PetscObjectComm((PetscObject)snes),&snes->ksp);
5326:     PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);
5327:     PetscLogObjectParent((PetscObject)snes,(PetscObject)snes->ksp);

5329:     KSPSetPreSolve(snes->ksp,(PetscErrorCode (*)(KSP,Vec,Vec,void*))KSPPreSolve_SNESEW,snes);
5330:     KSPSetPostSolve(snes->ksp,(PetscErrorCode (*)(KSP,Vec,Vec,void*))KSPPostSolve_SNESEW,snes);

5332:     PetscOptionsGetBool(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-ksp_monitor_snes",&monitor,NULL);
5333:     if (monitor) {
5334:       KSPMonitorSet(snes->ksp,KSPMonitorSNES,snes,NULL);
5335:     }
5336:     monitor = PETSC_FALSE;
5337:     PetscOptionsGetBool(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-ksp_monitor_snes_lg",&monitor,NULL);
5338:     if (monitor) {
5339:       PetscObject *objs;
5340:       KSPMonitorSNESLGResidualNormCreate(PetscObjectComm((PetscObject)snes),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,600,&objs);
5341:       objs[0] = (PetscObject) snes;
5342:       KSPMonitorSet(snes->ksp,(PetscErrorCode (*)(KSP,PetscInt,PetscReal,void*))KSPMonitorSNESLGResidualNorm,objs,(PetscErrorCode (*)(void**))KSPMonitorSNESLGResidualNormDestroy);
5343:     }
5344:     PetscObjectSetOptions((PetscObject)snes->ksp,((PetscObject)snes)->options);
5345:   }
5346:   *ksp = snes->ksp;
5347:   return(0);
5348: }


5351: #include <petsc/private/dmimpl.h>
5352: /*@
5353:    SNESSetDM - Sets the DM that may be used by some nonlinear solvers or their underlying preconditioners

5355:    Logically Collective on SNES

5357:    Input Parameters:
5358: +  snes - the nonlinear solver context
5359: -  dm - the dm, cannot be NULL

5361:    Notes:
5362:    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
5363:    even when not using interfaces like DMSNESSetFunction().  Use DMClone() to get a distinct DM when solving different
5364:    problems using the same function space.

5366:    Level: intermediate

5368: .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
5369: @*/
5370: PetscErrorCode  SNESSetDM(SNES snes,DM dm)
5371: {
5373:   KSP            ksp;
5374:   DMSNES         sdm;

5379:   PetscObjectReference((PetscObject)dm);
5380:   if (snes->dm) {               /* Move the DMSNES context over to the new DM unless the new DM already has one */
5381:     if (snes->dm->dmsnes && !dm->dmsnes) {
5382:       DMCopyDMSNES(snes->dm,dm);
5383:       DMGetDMSNES(snes->dm,&sdm);
5384:       if (sdm->originaldm == snes->dm) sdm->originaldm = dm; /* Grant write privileges to the replacement DM */
5385:     }
5386:     DMCoarsenHookRemove(snes->dm,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,snes);
5387:     DMDestroy(&snes->dm);
5388:   }
5389:   snes->dm     = dm;
5390:   snes->dmAuto = PETSC_FALSE;

5392:   SNESGetKSP(snes,&ksp);
5393:   KSPSetDM(ksp,dm);
5394:   KSPSetDMActive(ksp,PETSC_FALSE);
5395:   if (snes->npc) {
5396:     SNESSetDM(snes->npc, snes->dm);
5397:     SNESSetNPCSide(snes,snes->npcside);
5398:   }
5399:   return(0);
5400: }

5402: /*@
5403:    SNESGetDM - Gets the DM that may be used by some preconditioners

5405:    Not Collective but DM obtained is parallel on SNES

5407:    Input Parameter:
5408: . snes - the preconditioner context

5410:    Output Parameter:
5411: .  dm - the dm

5413:    Level: intermediate

5415: .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
5416: @*/
5417: PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
5418: {

5423:   if (!snes->dm) {
5424:     DMShellCreate(PetscObjectComm((PetscObject)snes),&snes->dm);
5425:     snes->dmAuto = PETSC_TRUE;
5426:   }
5427:   *dm = snes->dm;
5428:   return(0);
5429: }

5431: /*@
5432:   SNESSetNPC - Sets the nonlinear preconditioner to be used.

5434:   Collective on SNES

5436:   Input Parameters:
5437: + snes - iterative context obtained from SNESCreate()
5438: - pc   - the preconditioner object

5440:   Notes:
5441:   Use SNESGetNPC() to retrieve the preconditioner context (for example,
5442:   to configure it using the API).

5444:   Level: developer

5446: .seealso: SNESGetNPC(), SNESHasNPC()
5447: @*/
5448: PetscErrorCode SNESSetNPC(SNES snes, SNES pc)
5449: {

5456:   PetscObjectReference((PetscObject) pc);
5457:   SNESDestroy(&snes->npc);
5458:   snes->npc = pc;
5459:   PetscLogObjectParent((PetscObject)snes, (PetscObject)snes->npc);
5460:   return(0);
5461: }

5463: /*@
5464:   SNESGetNPC - Creates a nonlinear preconditioning solver (SNES) to be used to precondition the nonlinear solver.

5466:   Not Collective; but any changes to the obtained SNES object must be applied collectively

5468:   Input Parameter:
5469: . snes - iterative context obtained from SNESCreate()

5471:   Output Parameter:
5472: . pc - preconditioner context

5474:   Options Database:
5475: . -npc_snes_type <type> - set the type of the SNES to use as the nonlinear preconditioner

5477:   Notes:
5478:     If a SNES was previously set with SNESSetNPC() then that SNES is returned, otherwise a new SNES object is created.

5480:     The (preconditioner) SNES returned automatically inherits the same nonlinear function and Jacobian supplied to the original
5481:     SNES during SNESSetUp()

5483:   Level: developer

5485: .seealso: SNESSetNPC(), SNESHasNPC(), SNES, SNESCreate()
5486: @*/
5487: PetscErrorCode SNESGetNPC(SNES snes, SNES *pc)
5488: {
5490:   const char     *optionsprefix;

5495:   if (!snes->npc) {
5496:     SNESCreate(PetscObjectComm((PetscObject)snes),&snes->npc);
5497:     PetscObjectIncrementTabLevel((PetscObject)snes->npc,(PetscObject)snes,1);
5498:     PetscLogObjectParent((PetscObject)snes,(PetscObject)snes->npc);
5499:     SNESGetOptionsPrefix(snes,&optionsprefix);
5500:     SNESSetOptionsPrefix(snes->npc,optionsprefix);
5501:     SNESAppendOptionsPrefix(snes->npc,"npc_");
5502:     SNESSetCountersReset(snes->npc,PETSC_FALSE);
5503:   }
5504:   *pc = snes->npc;
5505:   return(0);
5506: }

5508: /*@
5509:   SNESHasNPC - Returns whether a nonlinear preconditioner exists

5511:   Not Collective

5513:   Input Parameter:
5514: . snes - iterative context obtained from SNESCreate()

5516:   Output Parameter:
5517: . has_npc - whether the SNES has an NPC or not

5519:   Level: developer

5521: .seealso: SNESSetNPC(), SNESGetNPC()
5522: @*/
5523: PetscErrorCode SNESHasNPC(SNES snes, PetscBool *has_npc)
5524: {
5527:   *has_npc = (PetscBool) (snes->npc ? PETSC_TRUE : PETSC_FALSE);
5528:   return(0);
5529: }

5531: /*@
5532:     SNESSetNPCSide - Sets the preconditioning side.

5534:     Logically Collective on SNES

5536:     Input Parameter:
5537: .   snes - iterative context obtained from SNESCreate()

5539:     Output Parameter:
5540: .   side - the preconditioning side, where side is one of
5541: .vb
5542:       PC_LEFT - left preconditioning
5543:       PC_RIGHT - right preconditioning (default for most nonlinear solvers)
5544: .ve

5546:     Options Database Keys:
5547: .   -snes_pc_side <right,left>

5549:     Notes:
5550:     SNESNRICHARDSON and SNESNCG only support left preconditioning.

5552:     Level: intermediate

5554: .seealso: SNESGetNPCSide(), KSPSetPCSide()
5555: @*/
5556: PetscErrorCode  SNESSetNPCSide(SNES snes,PCSide side)
5557: {
5561:   snes->npcside= side;
5562:   return(0);
5563: }

5565: /*@
5566:     SNESGetNPCSide - Gets the preconditioning side.

5568:     Not Collective

5570:     Input Parameter:
5571: .   snes - iterative context obtained from SNESCreate()

5573:     Output Parameter:
5574: .   side - the preconditioning side, where side is one of
5575: .vb
5576:       PC_LEFT - left preconditioning
5577:       PC_RIGHT - right preconditioning (default for most nonlinear solvers)
5578: .ve

5580:     Level: intermediate

5582: .seealso: SNESSetNPCSide(), KSPGetPCSide()
5583: @*/
5584: PetscErrorCode  SNESGetNPCSide(SNES snes,PCSide *side)
5585: {
5589:   *side = snes->npcside;
5590:   return(0);
5591: }

5593: /*@
5594:   SNESSetLineSearch - Sets the linesearch on the SNES instance.

5596:   Collective on SNES

5598:   Input Parameters:
5599: + snes - iterative context obtained from SNESCreate()
5600: - linesearch   - the linesearch object

5602:   Notes:
5603:   Use SNESGetLineSearch() to retrieve the preconditioner context (for example,
5604:   to configure it using the API).

5606:   Level: developer

5608: .seealso: SNESGetLineSearch()
5609: @*/
5610: PetscErrorCode SNESSetLineSearch(SNES snes, SNESLineSearch linesearch)
5611: {

5618:   PetscObjectReference((PetscObject) linesearch);
5619:   SNESLineSearchDestroy(&snes->linesearch);

5621:   snes->linesearch = linesearch;

5623:   PetscLogObjectParent((PetscObject)snes, (PetscObject)snes->linesearch);
5624:   return(0);
5625: }

5627: /*@
5628:   SNESGetLineSearch - Returns a pointer to the line search context set with SNESSetLineSearch()
5629:   or creates a default line search instance associated with the SNES and returns it.

5631:   Not Collective

5633:   Input Parameter:
5634: . snes - iterative context obtained from SNESCreate()

5636:   Output Parameter:
5637: . linesearch - linesearch context

5639:   Level: beginner

5641: .seealso: SNESSetLineSearch(), SNESLineSearchCreate()
5642: @*/
5643: PetscErrorCode SNESGetLineSearch(SNES snes, SNESLineSearch *linesearch)
5644: {
5646:   const char     *optionsprefix;

5651:   if (!snes->linesearch) {
5652:     SNESGetOptionsPrefix(snes, &optionsprefix);
5653:     SNESLineSearchCreate(PetscObjectComm((PetscObject)snes), &snes->linesearch);
5654:     SNESLineSearchSetSNES(snes->linesearch, snes);
5655:     SNESLineSearchAppendOptionsPrefix(snes->linesearch, optionsprefix);
5656:     PetscObjectIncrementTabLevel((PetscObject) snes->linesearch, (PetscObject) snes, 1);
5657:     PetscLogObjectParent((PetscObject)snes, (PetscObject)snes->linesearch);
5658:   }
5659:   *linesearch = snes->linesearch;
5660:   return(0);
5661: }