xref: /petsc/src/snes/interface/snes.c (revision a63bb30eff8b1dff6ddce5531d6230e04f6fd10b)
19b94acceSBarry Smith 
2c6db04a5SJed Brown #include <private/snesimpl.h>      /*I "petscsnes.h"  I*/
39b94acceSBarry Smith 
4ace3abfcSBarry Smith PetscBool  SNESRegisterAllCalled = PETSC_FALSE;
58ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
68ba1e511SMatthew Knepley 
78ba1e511SMatthew Knepley /* Logging support */
87087cfbeSBarry Smith PetscClassId  SNES_CLASSID;
9701cf23dSPeter Brune PetscLogEvent  SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval, SNES_GSEval;
10a09944afSBarry Smith 
11a09944afSBarry Smith #undef __FUNCT__
12cab2e9ccSBarry Smith #define __FUNCT__ "SNESDMComputeJacobian"
13cab2e9ccSBarry Smith /*
14cab2e9ccSBarry Smith     Translates from a SNES call to a DM call in computing a Jacobian
15cab2e9ccSBarry Smith */
16cab2e9ccSBarry Smith PetscErrorCode SNESDMComputeJacobian(SNES snes,Vec X,Mat *J,Mat *B,MatStructure *flag,void *ptr)
17cab2e9ccSBarry Smith {
18cab2e9ccSBarry Smith   PetscErrorCode ierr;
19cab2e9ccSBarry Smith   DM             dm;
20cab2e9ccSBarry Smith 
21cab2e9ccSBarry Smith   PetscFunctionBegin;
22cab2e9ccSBarry Smith   ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr);
23cab2e9ccSBarry Smith   ierr = DMComputeJacobian(dm,X,*J,*B,flag);CHKERRQ(ierr);
24cab2e9ccSBarry Smith   PetscFunctionReturn(0);
25cab2e9ccSBarry Smith }
26cab2e9ccSBarry Smith 
27cab2e9ccSBarry Smith #undef __FUNCT__
28e113a28aSBarry Smith #define __FUNCT__ "SNESSetErrorIfNotConverged"
29e113a28aSBarry Smith /*@
30e113a28aSBarry Smith    SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged.
31e113a28aSBarry Smith 
323f9fe445SBarry Smith    Logically Collective on SNES
33e113a28aSBarry Smith 
34e113a28aSBarry Smith    Input Parameters:
35e113a28aSBarry Smith +  snes - iterative context obtained from SNESCreate()
36e113a28aSBarry Smith -  flg - PETSC_TRUE indicates you want the error generated
37e113a28aSBarry Smith 
38e113a28aSBarry Smith    Options database keys:
39e113a28aSBarry Smith .  -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false)
40e113a28aSBarry Smith 
41e113a28aSBarry Smith    Level: intermediate
42e113a28aSBarry Smith 
43e113a28aSBarry Smith    Notes:
44e113a28aSBarry Smith     Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve()
45e113a28aSBarry Smith     to determine if it has converged.
46e113a28aSBarry Smith 
47e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
48e113a28aSBarry Smith 
49e113a28aSBarry Smith .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
50e113a28aSBarry Smith @*/
517087cfbeSBarry Smith PetscErrorCode  SNESSetErrorIfNotConverged(SNES snes,PetscBool  flg)
52e113a28aSBarry Smith {
53e113a28aSBarry Smith   PetscFunctionBegin;
54e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
55acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flg,2);
56e113a28aSBarry Smith   snes->errorifnotconverged = flg;
57dd568438SSatish Balay 
58e113a28aSBarry Smith   PetscFunctionReturn(0);
59e113a28aSBarry Smith }
60e113a28aSBarry Smith 
61e113a28aSBarry Smith #undef __FUNCT__
62e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged"
63e113a28aSBarry Smith /*@
64e113a28aSBarry Smith    SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge?
65e113a28aSBarry Smith 
66e113a28aSBarry Smith    Not Collective
67e113a28aSBarry Smith 
68e113a28aSBarry Smith    Input Parameter:
69e113a28aSBarry Smith .  snes - iterative context obtained from SNESCreate()
70e113a28aSBarry Smith 
71e113a28aSBarry Smith    Output Parameter:
72e113a28aSBarry Smith .  flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE
73e113a28aSBarry Smith 
74e113a28aSBarry Smith    Level: intermediate
75e113a28aSBarry Smith 
76e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
77e113a28aSBarry Smith 
78e113a28aSBarry Smith .seealso:  SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
79e113a28aSBarry Smith @*/
807087cfbeSBarry Smith PetscErrorCode  SNESGetErrorIfNotConverged(SNES snes,PetscBool  *flag)
81e113a28aSBarry Smith {
82e113a28aSBarry Smith   PetscFunctionBegin;
83e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
84e113a28aSBarry Smith   PetscValidPointer(flag,2);
85e113a28aSBarry Smith   *flag = snes->errorifnotconverged;
86e113a28aSBarry Smith   PetscFunctionReturn(0);
87e113a28aSBarry Smith }
88e113a28aSBarry Smith 
89e113a28aSBarry Smith #undef __FUNCT__
904936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
91e725d27bSBarry Smith /*@
924936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
934936397dSBarry Smith      in the functions domain. For example, negative pressure.
944936397dSBarry Smith 
953f9fe445SBarry Smith    Logically Collective on SNES
964936397dSBarry Smith 
974936397dSBarry Smith    Input Parameters:
984936397dSBarry Smith .  SNES - the SNES context
994936397dSBarry Smith 
10028529972SSatish Balay    Level: advanced
1014936397dSBarry Smith 
1024936397dSBarry Smith .keywords: SNES, view
1034936397dSBarry Smith 
1044936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
1054936397dSBarry Smith @*/
1067087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionDomainError(SNES snes)
1074936397dSBarry Smith {
1084936397dSBarry Smith   PetscFunctionBegin;
1090700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1104936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
1114936397dSBarry Smith   PetscFunctionReturn(0);
1124936397dSBarry Smith }
1134936397dSBarry Smith 
1144936397dSBarry Smith #undef __FUNCT__
1154a2ae208SSatish Balay #define __FUNCT__ "SNESView"
1167e2c5f70SBarry Smith /*@C
1179b94acceSBarry Smith    SNESView - Prints the SNES data structure.
1189b94acceSBarry Smith 
1194c49b128SBarry Smith    Collective on SNES
120fee21e36SBarry Smith 
121c7afd0dbSLois Curfman McInnes    Input Parameters:
122c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
123c7afd0dbSLois Curfman McInnes -  viewer - visualization context
124c7afd0dbSLois Curfman McInnes 
1259b94acceSBarry Smith    Options Database Key:
126c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
1279b94acceSBarry Smith 
1289b94acceSBarry Smith    Notes:
1299b94acceSBarry Smith    The available visualization contexts include
130b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
131b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
132c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
133c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
134c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
1359b94acceSBarry Smith 
1363e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
137b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
1389b94acceSBarry Smith 
13936851e7fSLois Curfman McInnes    Level: beginner
14036851e7fSLois Curfman McInnes 
1419b94acceSBarry Smith .keywords: SNES, view
1429b94acceSBarry Smith 
143b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1449b94acceSBarry Smith @*/
1457087cfbeSBarry Smith PetscErrorCode  SNESView(SNES snes,PetscViewer viewer)
1469b94acceSBarry Smith {
147fa9f3622SBarry Smith   SNESKSPEW           *kctx;
148dfbe8321SBarry Smith   PetscErrorCode      ierr;
14994b7f48cSBarry Smith   KSP                 ksp;
150ace3abfcSBarry Smith   PetscBool           iascii,isstring;
1519b94acceSBarry Smith 
1523a40ed3dSBarry Smith   PetscFunctionBegin;
1530700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1543050cee2SBarry Smith   if (!viewer) {
1557adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
1563050cee2SBarry Smith   }
1570700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
158c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
15974679c65SBarry Smith 
1602692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1612692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
16232077d6dSBarry Smith   if (iascii) {
163317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer,"SNES Object");CHKERRQ(ierr);
164e7788613SBarry Smith     if (snes->ops->view) {
165b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
166e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
167b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1680ef38995SBarry Smith     }
16977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
170a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
17170441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
17277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
17377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1749b94acceSBarry Smith     if (snes->ksp_ewconv) {
175fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1769b94acceSBarry Smith       if (kctx) {
17777431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
178a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
179a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1809b94acceSBarry Smith       }
1819b94acceSBarry Smith     }
182eb1f6c34SBarry Smith     if (snes->lagpreconditioner == -1) {
183eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is never rebuilt\n");CHKERRQ(ierr);
184eb1f6c34SBarry Smith     } else if (snes->lagpreconditioner > 1) {
185eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr);
186eb1f6c34SBarry Smith     }
187eb1f6c34SBarry Smith     if (snes->lagjacobian == -1) {
188eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is never rebuilt\n");CHKERRQ(ierr);
189eb1f6c34SBarry Smith     } else if (snes->lagjacobian > 1) {
19042f4f86dSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is rebuilt every %D SNES iterations\n",snes->lagjacobian);CHKERRQ(ierr);
191eb1f6c34SBarry Smith     }
1920f5bd95cSBarry Smith   } else if (isstring) {
193317d6ea6SBarry Smith     const char *type;
194454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
195b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
19619bcc07fSBarry Smith   }
19742f4f86dSBarry Smith   if (snes->pc && snes->usespc) {
1984a0c5b0cSMatthew G Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1994a0c5b0cSMatthew G Knepley     ierr = SNESView(snes->pc, viewer);CHKERRQ(ierr);
2004a0c5b0cSMatthew G Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2014a0c5b0cSMatthew G Knepley   }
2022c155ee1SBarry Smith   if (snes->usesksp) {
2032c155ee1SBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
204b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
20594b7f48cSBarry Smith     ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
206b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2072c155ee1SBarry Smith   }
2083a40ed3dSBarry Smith   PetscFunctionReturn(0);
2099b94acceSBarry Smith }
2109b94acceSBarry Smith 
21176b2cf59SMatthew Knepley /*
21276b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
21376b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
21476b2cf59SMatthew Knepley */
21576b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
216a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
2176849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
21876b2cf59SMatthew Knepley 
219e74ef692SMatthew Knepley #undef __FUNCT__
220e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
221ac226902SBarry Smith /*@C
22276b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
22376b2cf59SMatthew Knepley 
22476b2cf59SMatthew Knepley   Not Collective
22576b2cf59SMatthew Knepley 
22676b2cf59SMatthew Knepley   Input Parameter:
22776b2cf59SMatthew Knepley . snescheck - function that checks for options
22876b2cf59SMatthew Knepley 
22976b2cf59SMatthew Knepley   Level: developer
23076b2cf59SMatthew Knepley 
23176b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
23276b2cf59SMatthew Knepley @*/
2337087cfbeSBarry Smith PetscErrorCode  SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
23476b2cf59SMatthew Knepley {
23576b2cf59SMatthew Knepley   PetscFunctionBegin;
23676b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
237e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
23876b2cf59SMatthew Knepley   }
23976b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
24076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
24176b2cf59SMatthew Knepley }
24276b2cf59SMatthew Knepley 
2437087cfbeSBarry Smith extern PetscErrorCode  SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
244aa3661deSLisandro Dalcin 
245aa3661deSLisandro Dalcin #undef __FUNCT__
246aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private"
247ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool  hasOperator, PetscInt version)
248aa3661deSLisandro Dalcin {
249aa3661deSLisandro Dalcin   Mat            J;
250aa3661deSLisandro Dalcin   KSP            ksp;
251aa3661deSLisandro Dalcin   PC             pc;
252ace3abfcSBarry Smith   PetscBool      match;
253aa3661deSLisandro Dalcin   PetscErrorCode ierr;
254aa3661deSLisandro Dalcin 
255aa3661deSLisandro Dalcin   PetscFunctionBegin;
2560700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
257aa3661deSLisandro Dalcin 
25898613b67SLisandro Dalcin   if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
25998613b67SLisandro Dalcin     Mat A = snes->jacobian, B = snes->jacobian_pre;
26098613b67SLisandro Dalcin     ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr);
26198613b67SLisandro Dalcin   }
26298613b67SLisandro Dalcin 
263aa3661deSLisandro Dalcin   if (version == 1) {
264aa3661deSLisandro Dalcin     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
26598613b67SLisandro Dalcin     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
2669c6ac3b3SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
267aa3661deSLisandro Dalcin   } else if (version == 2) {
268e32f2f54SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
26982a7e548SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
270aa3661deSLisandro Dalcin     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr);
271aa3661deSLisandro Dalcin #else
272e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
273aa3661deSLisandro Dalcin #endif
274a8248277SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2");
275aa3661deSLisandro Dalcin 
276aa3661deSLisandro Dalcin   ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr);
277d3462f78SMatthew Knepley   if (hasOperator) {
278aa3661deSLisandro Dalcin     /* This version replaces the user provided Jacobian matrix with a
279aa3661deSLisandro Dalcin        matrix-free version but still employs the user-provided preconditioner matrix. */
280aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
281aa3661deSLisandro Dalcin   } else {
282aa3661deSLisandro Dalcin     /* This version replaces both the user-provided Jacobian and the user-
283aa3661deSLisandro Dalcin        provided preconditioner matrix with the default matrix free version. */
284aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
285aa3661deSLisandro Dalcin     /* Force no preconditioner */
286aa3661deSLisandro Dalcin     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
287aa3661deSLisandro Dalcin     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
288aa3661deSLisandro Dalcin     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr);
289aa3661deSLisandro Dalcin     if (!match) {
290aa3661deSLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
291aa3661deSLisandro Dalcin       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
292aa3661deSLisandro Dalcin     }
293aa3661deSLisandro Dalcin   }
2946bf464f9SBarry Smith   ierr = MatDestroy(&J);CHKERRQ(ierr);
295aa3661deSLisandro Dalcin   PetscFunctionReturn(0);
296aa3661deSLisandro Dalcin }
297aa3661deSLisandro Dalcin 
2984a2ae208SSatish Balay #undef __FUNCT__
2994a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
3009b94acceSBarry Smith /*@
30194b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
3029b94acceSBarry Smith 
303c7afd0dbSLois Curfman McInnes    Collective on SNES
304c7afd0dbSLois Curfman McInnes 
3059b94acceSBarry Smith    Input Parameter:
3069b94acceSBarry Smith .  snes - the SNES context
3079b94acceSBarry Smith 
30836851e7fSLois Curfman McInnes    Options Database Keys:
309ea630c6eSPeter Brune +  -snes_type <type> - ls, tr, ngmres, ncg, richardson, qn, vi, fas
31082738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
31182738288SBarry Smith                 of the change in the solution between steps
31270441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
313b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
314b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
315b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
316*4839bfe8SBarry Smith .  -snes_max_fail <max_fail> - maximum number of line search failures allowed before stopping, default is none
317ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
318a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
319e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
320b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
3212492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
32282738288SBarry Smith                                solver; hence iterations will continue until max_it
3231fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
32482738288SBarry Smith                                of convergence test
325e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
326e8105e01SRichard Katz                                        filename given prints to stdout
327a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
328a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
329a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
330a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
331e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
3325968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
333fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
33482738288SBarry Smith 
33582738288SBarry Smith     Options Database for Eisenstat-Walker method:
336fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
3374b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
33836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
33936851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
34036851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
34136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
34236851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
34336851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
34482738288SBarry Smith 
34511ca99fdSLois Curfman McInnes    Notes:
34611ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
3470598bfebSBarry Smith    the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A>.
34883e2fdc7SBarry Smith 
34936851e7fSLois Curfman McInnes    Level: beginner
35036851e7fSLois Curfman McInnes 
3519b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
3529b94acceSBarry Smith 
35369ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
3549b94acceSBarry Smith @*/
3557087cfbeSBarry Smith PetscErrorCode  SNESSetFromOptions(SNES snes)
3569b94acceSBarry Smith {
357ea630c6eSPeter Brune   PetscBool               flg,set,mf,mf_operator,pcset;
358efd51863SBarry Smith   PetscInt                i,indx,lag,mf_version,grids;
359aa3661deSLisandro Dalcin   MatStructure            matflag;
36085385478SLisandro Dalcin   const char              *deft = SNESLS;
36185385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
36285385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
363e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
36451e86f29SPeter Brune   const char              *optionsprefix;
365649052a6SBarry Smith   PetscViewer             monviewer;
36685385478SLisandro Dalcin   PetscErrorCode          ierr;
3679b94acceSBarry Smith 
3683a40ed3dSBarry Smith   PetscFunctionBegin;
3690700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
370ca161407SBarry Smith 
371186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
3723194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)snes);CHKERRQ(ierr);
3737adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
374b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
375d64ed03dSBarry Smith     if (flg) {
376186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
3777adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
378186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
379d64ed03dSBarry Smith     }
38090d69ab7SBarry Smith     /* not used here, but called so will go into help messaage */
381909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
38293c39befSBarry Smith 
38357034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
38457034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
385186905e3SBarry Smith 
38657034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
387b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
388b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
38950ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
390ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
391acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr);
39285385478SLisandro Dalcin 
393a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
394a8054027SBarry Smith     if (flg) {
395a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
396a8054027SBarry Smith     }
397e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
398e35cf81dSBarry Smith     if (flg) {
399e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
400e35cf81dSBarry Smith     }
401efd51863SBarry Smith     ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr);
402efd51863SBarry Smith     if (flg) {
403efd51863SBarry Smith       ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr);
404efd51863SBarry Smith     }
405a8054027SBarry Smith 
40685385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
40785385478SLisandro Dalcin     if (flg) {
40885385478SLisandro Dalcin       switch (indx) {
4097f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
4107f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
41185385478SLisandro Dalcin       }
41285385478SLisandro Dalcin     }
41385385478SLisandro Dalcin 
414acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
415186905e3SBarry Smith 
41685385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
41785385478SLisandro Dalcin 
418acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
419186905e3SBarry Smith 
420fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
421fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
422fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
423fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
424fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
425fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
426fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
427186905e3SBarry Smith 
42890d69ab7SBarry Smith     flg  = PETSC_FALSE;
429acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
430a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
431eabae89aSBarry Smith 
432a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
433e8105e01SRichard Katz     if (flg) {
434649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
435649052a6SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
436e8105e01SRichard Katz     }
437eabae89aSBarry Smith 
438b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
439b271bb04SBarry Smith     if (flg) {
440b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
441b271bb04SBarry Smith     }
442b271bb04SBarry Smith 
443a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
444eabae89aSBarry Smith     if (flg) {
445649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
446f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
447e8105e01SRichard Katz     }
448eabae89aSBarry Smith 
449a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
450eabae89aSBarry Smith     if (flg) {
451649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
452649052a6SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
453eabae89aSBarry Smith     }
454eabae89aSBarry Smith 
4555180491cSLisandro Dalcin     ierr = PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
4565180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)snes,monfilename);CHKERRQ(ierr);}
4575180491cSLisandro Dalcin 
45890d69ab7SBarry Smith     flg  = PETSC_FALSE;
459acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
460a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
46190d69ab7SBarry Smith     flg  = PETSC_FALSE;
462acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
463a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
46490d69ab7SBarry Smith     flg  = PETSC_FALSE;
465acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
466a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
46790d69ab7SBarry Smith     flg  = PETSC_FALSE;
468acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
469a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
47090d69ab7SBarry Smith     flg  = PETSC_FALSE;
471acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
472b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
473e24b481bSBarry Smith 
47490d69ab7SBarry Smith     flg  = PETSC_FALSE;
475acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
4764b27c08aSLois Curfman McInnes     if (flg) {
477186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
478ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
4799b94acceSBarry Smith     }
480639f9d9dSBarry Smith 
481aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
482aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
483acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
484a8248277SBarry Smith     if (flg && mf_operator) {
485a8248277SBarry Smith       snes->mf_operator = PETSC_TRUE;
486a8248277SBarry Smith       mf = PETSC_TRUE;
487a8248277SBarry Smith     }
488aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
489acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
490aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
491aa3661deSLisandro Dalcin     mf_version = 1;
492aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
493aa3661deSLisandro Dalcin 
494d28543b3SPeter Brune 
495d28543b3SPeter Brune     ierr = PetscOptionsBool("-snes_use_gs","Use user-provided GS routine","SNESSetUseGS",snes->usegs,&snes->usegs,PETSC_NULL);CHKERRQ(ierr);
496d28543b3SPeter Brune 
497ea630c6eSPeter Brune     /* line search options */
498ea630c6eSPeter Brune     ierr = PetscOptionsReal("-snes_ls_alpha","Constant function norm must decrease by","None",snes->ls_alpha,&snes->ls_alpha,0);CHKERRQ(ierr);
499ea630c6eSPeter Brune     ierr = PetscOptionsReal("-snes_ls_maxstep","Step must be less than","None",snes->maxstep,&snes->maxstep,0);CHKERRQ(ierr);
500ea630c6eSPeter Brune     ierr = PetscOptionsReal("-snes_ls_steptol","Minimum lambda allowed","None",snes->steptol,&snes->steptol,0);CHKERRQ(ierr);
501ea630c6eSPeter Brune     ierr = PetscOptionsReal("-snes_ls_damping","Damping parameter","SNES",snes->damping,&snes->damping,&flg);CHKERRQ(ierr);
502af60355fSPeter Brune     ierr = PetscOptionsInt("-snes_ls_it"      ,"Line search iterations","SNES",snes->ls_its,&snes->ls_its,&flg);CHKERRQ(ierr);
503ea630c6eSPeter Brune     ierr = PetscOptionsBool("-snes_ls_monitor","Print progress of line searches","SNESLineSearchSetMonitor",snes->ls_monitor ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr);
504ea630c6eSPeter Brune     if (set) {ierr = SNESLineSearchSetMonitor(snes,flg);CHKERRQ(ierr);}
50515f5eeeaSPeter Brune     ierr = PetscOptionsEnum("-snes_ls","Line search used","SNESLineSearchSet",SNESLineSearchTypes,(PetscEnum)snes->ls_type,(PetscEnum*)&indx,&flg);CHKERRQ(ierr);
50615f5eeeaSPeter Brune     if (flg) {
507ea630c6eSPeter Brune       ierr = SNESLineSearchSetType(snes,(SNESLineSearchType)indx);CHKERRQ(ierr);
50815f5eeeaSPeter Brune     }
5098e3fc8c0SJed Brown     flg = snes->ops->precheckstep == SNESLineSearchPreCheckPicard ? PETSC_TRUE : PETSC_FALSE;
5108e3fc8c0SJed Brown     ierr = PetscOptionsBool("-snes_ls_precheck_picard","Use a correction that sometimes improves convergence of Picard iteration","SNESLineSearchPreCheckPicard",flg,&flg,&set);CHKERRQ(ierr);
5118e3fc8c0SJed Brown     if (set) {
5128e3fc8c0SJed Brown       if (flg) {
5138e3fc8c0SJed Brown         snes->precheck_picard_angle = 10.; /* correction only active if angle is less than 10 degrees */
5148e3fc8c0SJed Brown         ierr = PetscOptionsReal("-snes_ls_precheck_picard_angle","Maximum angle at which to activate the correction","none",snes->precheck_picard_angle,&snes->precheck_picard_angle,PETSC_NULL);CHKERRQ(ierr);
5158e3fc8c0SJed Brown         ierr = SNESLineSearchSetPreCheck(snes,SNESLineSearchPreCheckPicard,&snes->precheck_picard_angle);CHKERRQ(ierr);
5168e3fc8c0SJed Brown       } else {
5178e3fc8c0SJed Brown         ierr = SNESLineSearchSetPreCheck(snes,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
5188e3fc8c0SJed Brown       }
5198e3fc8c0SJed Brown     }
5208e3fc8c0SJed Brown 
52176b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
52276b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
52376b2cf59SMatthew Knepley     }
52476b2cf59SMatthew Knepley 
525e7788613SBarry Smith     if (snes->ops->setfromoptions) {
526e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
527639f9d9dSBarry Smith     }
5285d973c19SBarry Smith 
5295d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
5305d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr);
531b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
5324bbc92c1SBarry Smith 
533aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
5341cee3971SBarry Smith 
5351cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
536aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
537aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
53885385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
53993993e2dSLois Curfman McInnes 
54051e86f29SPeter Brune   /* if someone has set the SNES PC type, create it. */
54151e86f29SPeter Brune   ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr);
54251e86f29SPeter Brune   ierr = PetscOptionsHasName(optionsprefix, "-npc_snes_type", &pcset);CHKERRQ(ierr);
54351e86f29SPeter Brune   if (pcset && (!snes->pc)) {
54451e86f29SPeter Brune     ierr = SNESGetPC(snes, &snes->pc);CHKERRQ(ierr);
54551e86f29SPeter Brune   }
5464a0c5b0cSMatthew G Knepley   if (snes->pc) {
5474a0c5b0cSMatthew G Knepley     ierr = SNESSetOptionsPrefix(snes->pc, "npc_");CHKERRQ(ierr);
5484a0c5b0cSMatthew G Knepley     ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr);
549646217ecSPeter Brune     ierr = SNESSetGS(snes->pc, snes->ops->computegs, snes->gsP);CHKERRQ(ierr);
5504a0c5b0cSMatthew G Knepley     /* Should we make a duplicate vector and matrix? Leave the DM to make it? */
5514a0c5b0cSMatthew G Knepley     ierr = SNESSetFunction(snes->pc, snes->vec_func, snes->ops->computefunction, snes->funP);CHKERRQ(ierr);
5524a0c5b0cSMatthew G Knepley     ierr = SNESSetJacobian(snes->pc, snes->jacobian, snes->jacobian_pre, snes->ops->computejacobian, snes->jacP);CHKERRQ(ierr);
5534a0c5b0cSMatthew G Knepley     ierr = SNESSetFromOptions(snes->pc);CHKERRQ(ierr);
5544a0c5b0cSMatthew G Knepley   }
5553a40ed3dSBarry Smith   PetscFunctionReturn(0);
5569b94acceSBarry Smith }
5579b94acceSBarry Smith 
558d25893d9SBarry Smith #undef __FUNCT__
559d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext"
560d25893d9SBarry Smith /*@
561d25893d9SBarry Smith    SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for
562d25893d9SBarry Smith    the nonlinear solvers.
563d25893d9SBarry Smith 
564d25893d9SBarry Smith    Logically Collective on SNES
565d25893d9SBarry Smith 
566d25893d9SBarry Smith    Input Parameters:
567d25893d9SBarry Smith +  snes - the SNES context
568d25893d9SBarry Smith .  compute - function to compute the context
569d25893d9SBarry Smith -  destroy - function to destroy the context
570d25893d9SBarry Smith 
571d25893d9SBarry Smith    Level: intermediate
572d25893d9SBarry Smith 
573d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context
574d25893d9SBarry Smith 
575d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext()
576d25893d9SBarry Smith @*/
577d25893d9SBarry Smith PetscErrorCode  SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**))
578d25893d9SBarry Smith {
579d25893d9SBarry Smith   PetscFunctionBegin;
580d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
581d25893d9SBarry Smith   snes->ops->usercompute = compute;
582d25893d9SBarry Smith   snes->ops->userdestroy = destroy;
583d25893d9SBarry Smith   PetscFunctionReturn(0);
584d25893d9SBarry Smith }
585a847f771SSatish Balay 
5864a2ae208SSatish Balay #undef __FUNCT__
5874a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
588b07ff414SBarry Smith /*@
5899b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
5909b94acceSBarry Smith    the nonlinear solvers.
5919b94acceSBarry Smith 
5923f9fe445SBarry Smith    Logically Collective on SNES
593fee21e36SBarry Smith 
594c7afd0dbSLois Curfman McInnes    Input Parameters:
595c7afd0dbSLois Curfman McInnes +  snes - the SNES context
596c7afd0dbSLois Curfman McInnes -  usrP - optional user context
597c7afd0dbSLois Curfman McInnes 
59836851e7fSLois Curfman McInnes    Level: intermediate
59936851e7fSLois Curfman McInnes 
6009b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
6019b94acceSBarry Smith 
602d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext()
6039b94acceSBarry Smith @*/
6047087cfbeSBarry Smith PetscErrorCode  SNESSetApplicationContext(SNES snes,void *usrP)
6059b94acceSBarry Smith {
6061b2093e4SBarry Smith   PetscErrorCode ierr;
607b07ff414SBarry Smith   KSP            ksp;
6081b2093e4SBarry Smith 
6093a40ed3dSBarry Smith   PetscFunctionBegin;
6100700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
611b07ff414SBarry Smith   ierr       = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
612b07ff414SBarry Smith   ierr       = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr);
6139b94acceSBarry Smith   snes->user = usrP;
6143a40ed3dSBarry Smith   PetscFunctionReturn(0);
6159b94acceSBarry Smith }
61674679c65SBarry Smith 
6174a2ae208SSatish Balay #undef __FUNCT__
6184a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
619b07ff414SBarry Smith /*@
6209b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
6219b94acceSBarry Smith    nonlinear solvers.
6229b94acceSBarry Smith 
623c7afd0dbSLois Curfman McInnes    Not Collective
624c7afd0dbSLois Curfman McInnes 
6259b94acceSBarry Smith    Input Parameter:
6269b94acceSBarry Smith .  snes - SNES context
6279b94acceSBarry Smith 
6289b94acceSBarry Smith    Output Parameter:
6299b94acceSBarry Smith .  usrP - user context
6309b94acceSBarry Smith 
63136851e7fSLois Curfman McInnes    Level: intermediate
63236851e7fSLois Curfman McInnes 
6339b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
6349b94acceSBarry Smith 
6359b94acceSBarry Smith .seealso: SNESSetApplicationContext()
6369b94acceSBarry Smith @*/
637e71120c6SJed Brown PetscErrorCode  SNESGetApplicationContext(SNES snes,void *usrP)
6389b94acceSBarry Smith {
6393a40ed3dSBarry Smith   PetscFunctionBegin;
6400700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
641e71120c6SJed Brown   *(void**)usrP = snes->user;
6423a40ed3dSBarry Smith   PetscFunctionReturn(0);
6439b94acceSBarry Smith }
64474679c65SBarry Smith 
6454a2ae208SSatish Balay #undef __FUNCT__
6464a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
6479b94acceSBarry Smith /*@
648c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
649c8228a4eSBarry Smith    at this time.
6509b94acceSBarry Smith 
651c7afd0dbSLois Curfman McInnes    Not Collective
652c7afd0dbSLois Curfman McInnes 
6539b94acceSBarry Smith    Input Parameter:
6549b94acceSBarry Smith .  snes - SNES context
6559b94acceSBarry Smith 
6569b94acceSBarry Smith    Output Parameter:
6579b94acceSBarry Smith .  iter - iteration number
6589b94acceSBarry Smith 
659c8228a4eSBarry Smith    Notes:
660c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
661c8228a4eSBarry Smith 
662c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
66308405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
66408405cd6SLois Curfman McInnes .vb
66508405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
66608405cd6SLois Curfman McInnes       if (!(it % 2)) {
66708405cd6SLois Curfman McInnes         [compute Jacobian here]
66808405cd6SLois Curfman McInnes       }
66908405cd6SLois Curfman McInnes .ve
670c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
67108405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
672c8228a4eSBarry Smith 
67336851e7fSLois Curfman McInnes    Level: intermediate
67436851e7fSLois Curfman McInnes 
6752b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
6762b668275SBarry Smith 
677b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
6789b94acceSBarry Smith @*/
6797087cfbeSBarry Smith PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt* iter)
6809b94acceSBarry Smith {
6813a40ed3dSBarry Smith   PetscFunctionBegin;
6820700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6834482741eSBarry Smith   PetscValidIntPointer(iter,2);
6849b94acceSBarry Smith   *iter = snes->iter;
6853a40ed3dSBarry Smith   PetscFunctionReturn(0);
6869b94acceSBarry Smith }
68774679c65SBarry Smith 
6884a2ae208SSatish Balay #undef __FUNCT__
6894a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
6909b94acceSBarry Smith /*@
6919b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
6929b94acceSBarry Smith    with SNESSSetFunction().
6939b94acceSBarry Smith 
694c7afd0dbSLois Curfman McInnes    Collective on SNES
695c7afd0dbSLois Curfman McInnes 
6969b94acceSBarry Smith    Input Parameter:
6979b94acceSBarry Smith .  snes - SNES context
6989b94acceSBarry Smith 
6999b94acceSBarry Smith    Output Parameter:
7009b94acceSBarry Smith .  fnorm - 2-norm of function
7019b94acceSBarry Smith 
70236851e7fSLois Curfman McInnes    Level: intermediate
70336851e7fSLois Curfman McInnes 
7049b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
705a86d99e1SLois Curfman McInnes 
706b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
7079b94acceSBarry Smith @*/
7087087cfbeSBarry Smith PetscErrorCode  SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
7099b94acceSBarry Smith {
7103a40ed3dSBarry Smith   PetscFunctionBegin;
7110700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7124482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
7139b94acceSBarry Smith   *fnorm = snes->norm;
7143a40ed3dSBarry Smith   PetscFunctionReturn(0);
7159b94acceSBarry Smith }
71674679c65SBarry Smith 
7174a2ae208SSatish Balay #undef __FUNCT__
718b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
7199b94acceSBarry Smith /*@
720b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
7219b94acceSBarry Smith    attempted by the nonlinear solver.
7229b94acceSBarry Smith 
723c7afd0dbSLois Curfman McInnes    Not Collective
724c7afd0dbSLois Curfman McInnes 
7259b94acceSBarry Smith    Input Parameter:
7269b94acceSBarry Smith .  snes - SNES context
7279b94acceSBarry Smith 
7289b94acceSBarry Smith    Output Parameter:
7299b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
7309b94acceSBarry Smith 
731c96a6f78SLois Curfman McInnes    Notes:
732c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
733c96a6f78SLois Curfman McInnes 
73436851e7fSLois Curfman McInnes    Level: intermediate
73536851e7fSLois Curfman McInnes 
7369b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
73758ebbce7SBarry Smith 
738e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
73958ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
7409b94acceSBarry Smith @*/
7417087cfbeSBarry Smith PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
7429b94acceSBarry Smith {
7433a40ed3dSBarry Smith   PetscFunctionBegin;
7440700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7454482741eSBarry Smith   PetscValidIntPointer(nfails,2);
74650ffb88aSMatthew Knepley   *nfails = snes->numFailures;
74750ffb88aSMatthew Knepley   PetscFunctionReturn(0);
74850ffb88aSMatthew Knepley }
74950ffb88aSMatthew Knepley 
75050ffb88aSMatthew Knepley #undef __FUNCT__
751b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
75250ffb88aSMatthew Knepley /*@
753b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
75450ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
75550ffb88aSMatthew Knepley 
75650ffb88aSMatthew Knepley    Not Collective
75750ffb88aSMatthew Knepley 
75850ffb88aSMatthew Knepley    Input Parameters:
75950ffb88aSMatthew Knepley +  snes     - SNES context
76050ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
76150ffb88aSMatthew Knepley 
76250ffb88aSMatthew Knepley    Level: intermediate
76350ffb88aSMatthew Knepley 
76450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
76558ebbce7SBarry Smith 
766e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
76758ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
76850ffb88aSMatthew Knepley @*/
7697087cfbeSBarry Smith PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
77050ffb88aSMatthew Knepley {
77150ffb88aSMatthew Knepley   PetscFunctionBegin;
7720700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
77350ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
77450ffb88aSMatthew Knepley   PetscFunctionReturn(0);
77550ffb88aSMatthew Knepley }
77650ffb88aSMatthew Knepley 
77750ffb88aSMatthew Knepley #undef __FUNCT__
778b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
77950ffb88aSMatthew Knepley /*@
780b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
78150ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
78250ffb88aSMatthew Knepley 
78350ffb88aSMatthew Knepley    Not Collective
78450ffb88aSMatthew Knepley 
78550ffb88aSMatthew Knepley    Input Parameter:
78650ffb88aSMatthew Knepley .  snes     - SNES context
78750ffb88aSMatthew Knepley 
78850ffb88aSMatthew Knepley    Output Parameter:
78950ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
79050ffb88aSMatthew Knepley 
79150ffb88aSMatthew Knepley    Level: intermediate
79250ffb88aSMatthew Knepley 
79350ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
79458ebbce7SBarry Smith 
795e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
79658ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
79758ebbce7SBarry Smith 
79850ffb88aSMatthew Knepley @*/
7997087cfbeSBarry Smith PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
80050ffb88aSMatthew Knepley {
80150ffb88aSMatthew Knepley   PetscFunctionBegin;
8020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8034482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
80450ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
8053a40ed3dSBarry Smith   PetscFunctionReturn(0);
8069b94acceSBarry Smith }
807a847f771SSatish Balay 
8084a2ae208SSatish Balay #undef __FUNCT__
8092541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
8102541af92SBarry Smith /*@
8112541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
8122541af92SBarry Smith      done by SNES.
8132541af92SBarry Smith 
8142541af92SBarry Smith    Not Collective
8152541af92SBarry Smith 
8162541af92SBarry Smith    Input Parameter:
8172541af92SBarry Smith .  snes     - SNES context
8182541af92SBarry Smith 
8192541af92SBarry Smith    Output Parameter:
8202541af92SBarry Smith .  nfuncs - number of evaluations
8212541af92SBarry Smith 
8222541af92SBarry Smith    Level: intermediate
8232541af92SBarry Smith 
8242541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
82558ebbce7SBarry Smith 
826e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
8272541af92SBarry Smith @*/
8287087cfbeSBarry Smith PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
8292541af92SBarry Smith {
8302541af92SBarry Smith   PetscFunctionBegin;
8310700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8322541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
8332541af92SBarry Smith   *nfuncs = snes->nfuncs;
8342541af92SBarry Smith   PetscFunctionReturn(0);
8352541af92SBarry Smith }
8362541af92SBarry Smith 
8372541af92SBarry Smith #undef __FUNCT__
8383d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
8393d4c4710SBarry Smith /*@
8403d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
8413d4c4710SBarry Smith    linear solvers.
8423d4c4710SBarry Smith 
8433d4c4710SBarry Smith    Not Collective
8443d4c4710SBarry Smith 
8453d4c4710SBarry Smith    Input Parameter:
8463d4c4710SBarry Smith .  snes - SNES context
8473d4c4710SBarry Smith 
8483d4c4710SBarry Smith    Output Parameter:
8493d4c4710SBarry Smith .  nfails - number of failed solves
8503d4c4710SBarry Smith 
8513d4c4710SBarry Smith    Notes:
8523d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
8533d4c4710SBarry Smith 
8543d4c4710SBarry Smith    Level: intermediate
8553d4c4710SBarry Smith 
8563d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
85758ebbce7SBarry Smith 
858e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
8593d4c4710SBarry Smith @*/
8607087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
8613d4c4710SBarry Smith {
8623d4c4710SBarry Smith   PetscFunctionBegin;
8630700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8643d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
8653d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
8663d4c4710SBarry Smith   PetscFunctionReturn(0);
8673d4c4710SBarry Smith }
8683d4c4710SBarry Smith 
8693d4c4710SBarry Smith #undef __FUNCT__
8703d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
8713d4c4710SBarry Smith /*@
8723d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
8733d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
8743d4c4710SBarry Smith 
8753f9fe445SBarry Smith    Logically Collective on SNES
8763d4c4710SBarry Smith 
8773d4c4710SBarry Smith    Input Parameters:
8783d4c4710SBarry Smith +  snes     - SNES context
8793d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
8803d4c4710SBarry Smith 
8813d4c4710SBarry Smith    Level: intermediate
8823d4c4710SBarry Smith 
883a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
8843d4c4710SBarry Smith 
8853d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
8863d4c4710SBarry Smith 
88758ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
8883d4c4710SBarry Smith @*/
8897087cfbeSBarry Smith PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
8903d4c4710SBarry Smith {
8913d4c4710SBarry Smith   PetscFunctionBegin;
8920700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
893c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
8943d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
8953d4c4710SBarry Smith   PetscFunctionReturn(0);
8963d4c4710SBarry Smith }
8973d4c4710SBarry Smith 
8983d4c4710SBarry Smith #undef __FUNCT__
8993d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
9003d4c4710SBarry Smith /*@
9013d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
9023d4c4710SBarry Smith      are allowed before SNES terminates
9033d4c4710SBarry Smith 
9043d4c4710SBarry Smith    Not Collective
9053d4c4710SBarry Smith 
9063d4c4710SBarry Smith    Input Parameter:
9073d4c4710SBarry Smith .  snes     - SNES context
9083d4c4710SBarry Smith 
9093d4c4710SBarry Smith    Output Parameter:
9103d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
9113d4c4710SBarry Smith 
9123d4c4710SBarry Smith    Level: intermediate
9133d4c4710SBarry Smith 
9143d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
9153d4c4710SBarry Smith 
9163d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
9173d4c4710SBarry Smith 
918e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
9193d4c4710SBarry Smith @*/
9207087cfbeSBarry Smith PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
9213d4c4710SBarry Smith {
9223d4c4710SBarry Smith   PetscFunctionBegin;
9230700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9243d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
9253d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
9263d4c4710SBarry Smith   PetscFunctionReturn(0);
9273d4c4710SBarry Smith }
9283d4c4710SBarry Smith 
9293d4c4710SBarry Smith #undef __FUNCT__
930b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
931c96a6f78SLois Curfman McInnes /*@
932b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
933c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
934c96a6f78SLois Curfman McInnes 
935c7afd0dbSLois Curfman McInnes    Not Collective
936c7afd0dbSLois Curfman McInnes 
937c96a6f78SLois Curfman McInnes    Input Parameter:
938c96a6f78SLois Curfman McInnes .  snes - SNES context
939c96a6f78SLois Curfman McInnes 
940c96a6f78SLois Curfman McInnes    Output Parameter:
941c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
942c96a6f78SLois Curfman McInnes 
943c96a6f78SLois Curfman McInnes    Notes:
944c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
945c96a6f78SLois Curfman McInnes 
94636851e7fSLois Curfman McInnes    Level: intermediate
94736851e7fSLois Curfman McInnes 
948c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
9492b668275SBarry Smith 
9508c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
951c96a6f78SLois Curfman McInnes @*/
9527087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
953c96a6f78SLois Curfman McInnes {
9543a40ed3dSBarry Smith   PetscFunctionBegin;
9550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9564482741eSBarry Smith   PetscValidIntPointer(lits,2);
957c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
9583a40ed3dSBarry Smith   PetscFunctionReturn(0);
959c96a6f78SLois Curfman McInnes }
960c96a6f78SLois Curfman McInnes 
9614a2ae208SSatish Balay #undef __FUNCT__
96294b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
96352baeb72SSatish Balay /*@
96494b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
9659b94acceSBarry Smith 
96694b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
967c7afd0dbSLois Curfman McInnes 
9689b94acceSBarry Smith    Input Parameter:
9699b94acceSBarry Smith .  snes - the SNES context
9709b94acceSBarry Smith 
9719b94acceSBarry Smith    Output Parameter:
97294b7f48cSBarry Smith .  ksp - the KSP context
9739b94acceSBarry Smith 
9749b94acceSBarry Smith    Notes:
97594b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
9769b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
9772999313aSBarry Smith    PC contexts as well.
9789b94acceSBarry Smith 
97936851e7fSLois Curfman McInnes    Level: beginner
98036851e7fSLois Curfman McInnes 
98194b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9829b94acceSBarry Smith 
9832999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9849b94acceSBarry Smith @*/
9857087cfbeSBarry Smith PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
9869b94acceSBarry Smith {
9871cee3971SBarry Smith   PetscErrorCode ierr;
9881cee3971SBarry Smith 
9893a40ed3dSBarry Smith   PetscFunctionBegin;
9900700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9914482741eSBarry Smith   PetscValidPointer(ksp,2);
9921cee3971SBarry Smith 
9931cee3971SBarry Smith   if (!snes->ksp) {
9941cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
9951cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
9961cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
9971cee3971SBarry Smith   }
99894b7f48cSBarry Smith   *ksp = snes->ksp;
9993a40ed3dSBarry Smith   PetscFunctionReturn(0);
10009b94acceSBarry Smith }
100182bf6240SBarry Smith 
10024a2ae208SSatish Balay #undef __FUNCT__
10032999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
10042999313aSBarry Smith /*@
10052999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
10062999313aSBarry Smith 
10072999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
10082999313aSBarry Smith 
10092999313aSBarry Smith    Input Parameters:
10102999313aSBarry Smith +  snes - the SNES context
10112999313aSBarry Smith -  ksp - the KSP context
10122999313aSBarry Smith 
10132999313aSBarry Smith    Notes:
10142999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
10152999313aSBarry Smith    so this routine is rarely needed.
10162999313aSBarry Smith 
10172999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
10182999313aSBarry Smith    decreased by one.
10192999313aSBarry Smith 
10202999313aSBarry Smith    Level: developer
10212999313aSBarry Smith 
10222999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
10232999313aSBarry Smith 
10242999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
10252999313aSBarry Smith @*/
10267087cfbeSBarry Smith PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
10272999313aSBarry Smith {
10282999313aSBarry Smith   PetscErrorCode ierr;
10292999313aSBarry Smith 
10302999313aSBarry Smith   PetscFunctionBegin;
10310700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
10320700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
10332999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
10347dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
1035906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
10362999313aSBarry Smith   snes->ksp = ksp;
10372999313aSBarry Smith   PetscFunctionReturn(0);
10382999313aSBarry Smith }
10392999313aSBarry Smith 
10407adad957SLisandro Dalcin #if 0
10412999313aSBarry Smith #undef __FUNCT__
10424a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
10436849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
1044e24b481bSBarry Smith {
1045e24b481bSBarry Smith   PetscFunctionBegin;
1046e24b481bSBarry Smith   PetscFunctionReturn(0);
1047e24b481bSBarry Smith }
10487adad957SLisandro Dalcin #endif
1049e24b481bSBarry Smith 
10509b94acceSBarry Smith /* -----------------------------------------------------------*/
10514a2ae208SSatish Balay #undef __FUNCT__
10524a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
105352baeb72SSatish Balay /*@
10549b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
10559b94acceSBarry Smith 
1056c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
1057c7afd0dbSLois Curfman McInnes 
1058c7afd0dbSLois Curfman McInnes    Input Parameters:
1059906ed7ccSBarry Smith .  comm - MPI communicator
10609b94acceSBarry Smith 
10619b94acceSBarry Smith    Output Parameter:
10629b94acceSBarry Smith .  outsnes - the new SNES context
10639b94acceSBarry Smith 
1064c7afd0dbSLois Curfman McInnes    Options Database Keys:
1065c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
1066c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
1067c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
1068c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
1069c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
1070c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
1071c1f60f51SBarry Smith 
107236851e7fSLois Curfman McInnes    Level: beginner
107336851e7fSLois Curfman McInnes 
10749b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
10759b94acceSBarry Smith 
1076a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
1077a8054027SBarry Smith 
10789b94acceSBarry Smith @*/
10797087cfbeSBarry Smith PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
10809b94acceSBarry Smith {
1081dfbe8321SBarry Smith   PetscErrorCode      ierr;
10829b94acceSBarry Smith   SNES                snes;
1083fa9f3622SBarry Smith   SNESKSPEW           *kctx;
108437fcc0dbSBarry Smith 
10853a40ed3dSBarry Smith   PetscFunctionBegin;
1086ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
10878ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
10888ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
10898ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
10908ba1e511SMatthew Knepley #endif
10918ba1e511SMatthew Knepley 
10923194b578SJed Brown   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
10937adad957SLisandro Dalcin 
109485385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
10952c155ee1SBarry Smith   snes->usesksp           = PETSC_TRUE;
1096d28543b3SPeter Brune   snes->usegs             = PETSC_FALSE;
10979b94acceSBarry Smith   snes->max_its           = 50;
10989750a799SBarry Smith   snes->max_funcs	  = 10000;
10999b94acceSBarry Smith   snes->norm		  = 0.0;
1100b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
1101b4874afaSBarry Smith   snes->ttol              = 0.0;
110270441072SBarry Smith   snes->abstol		  = 1.e-50;
11039b94acceSBarry Smith   snes->xtol		  = 1.e-8;
11044b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
11059b94acceSBarry Smith   snes->nfuncs            = 0;
110650ffb88aSMatthew Knepley   snes->numFailures       = 0;
110750ffb88aSMatthew Knepley   snes->maxFailures       = 1;
11087a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1109e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1110a8054027SBarry Smith   snes->lagpreconditioner = 1;
1111639f9d9dSBarry Smith   snes->numbermonitors    = 0;
11129b94acceSBarry Smith   snes->data              = 0;
11134dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1114186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
11156f24a144SLois Curfman McInnes   snes->nwork             = 0;
111658c9b817SLisandro Dalcin   snes->work              = 0;
111758c9b817SLisandro Dalcin   snes->nvwork            = 0;
111858c9b817SLisandro Dalcin   snes->vwork             = 0;
1119758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1120758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1121758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1122758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1123758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1124184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
11259b94acceSBarry Smith 
1126ea630c6eSPeter Brune   /* initialize the line search options */
1127ea630c6eSPeter Brune   snes->ls_type           = SNES_LS_BASIC;
1128af60355fSPeter Brune   snes->ls_its            = 1;
1129ea630c6eSPeter Brune   snes->damping           = 1.0;
1130ea630c6eSPeter Brune   snes->maxstep           = 1e8;
1131ea630c6eSPeter Brune   snes->steptol           = 1e-12;
1132ea630c6eSPeter Brune   snes->ls_alpha          = 1e-4;
1133ea630c6eSPeter Brune   snes->ls_monitor        = PETSC_NULL;
1134ea630c6eSPeter Brune 
1135ea630c6eSPeter Brune   snes->ops->linesearch   = PETSC_NULL;
1136ea630c6eSPeter Brune   snes->precheck          = PETSC_NULL;
1137ea630c6eSPeter Brune   snes->ops->precheckstep = PETSC_NULL;
1138ea630c6eSPeter Brune   snes->postcheck         = PETSC_NULL;
1139ea630c6eSPeter Brune   snes->ops->postcheckstep= PETSC_NULL;
1140ea630c6eSPeter Brune 
11413d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
11423d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
11433d4c4710SBarry Smith 
11449b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
114538f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
11469b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
11479b94acceSBarry Smith   kctx->version     = 2;
11489b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
11499b94acceSBarry Smith                              this was too large for some test cases */
115075567043SBarry Smith   kctx->rtol_last   = 0.0;
11519b94acceSBarry Smith   kctx->rtol_max    = .9;
11529b94acceSBarry Smith   kctx->gamma       = 1.0;
115362d1f40fSBarry Smith   kctx->alpha       = .5*(1.0 + PetscSqrtReal(5.0));
115471f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
11559b94acceSBarry Smith   kctx->threshold   = .1;
115675567043SBarry Smith   kctx->lresid_last = 0.0;
115775567043SBarry Smith   kctx->norm_last   = 0.0;
11589b94acceSBarry Smith 
11599b94acceSBarry Smith   *outsnes = snes;
11603a40ed3dSBarry Smith   PetscFunctionReturn(0);
11619b94acceSBarry Smith }
11629b94acceSBarry Smith 
11634a2ae208SSatish Balay #undef __FUNCT__
11644a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
11659b94acceSBarry Smith /*@C
11669b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
11679b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
11689b94acceSBarry Smith    equations.
11699b94acceSBarry Smith 
11703f9fe445SBarry Smith    Logically Collective on SNES
1171fee21e36SBarry Smith 
1172c7afd0dbSLois Curfman McInnes    Input Parameters:
1173c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1174c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1175de044059SHong Zhang .  func - function evaluation routine
1176c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1177c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
11789b94acceSBarry Smith 
1179c7afd0dbSLois Curfman McInnes    Calling sequence of func:
11808d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1181c7afd0dbSLois Curfman McInnes 
1182313e4042SLois Curfman McInnes .  f - function vector
1183c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
11849b94acceSBarry Smith 
11859b94acceSBarry Smith    Notes:
11869b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
11879b94acceSBarry Smith $      f'(x) x = -f(x),
1188c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
11899b94acceSBarry Smith 
119036851e7fSLois Curfman McInnes    Level: beginner
119136851e7fSLois Curfman McInnes 
11929b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
11939b94acceSBarry Smith 
11948b0a5094SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetPicard()
11959b94acceSBarry Smith @*/
11967087cfbeSBarry Smith PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
11979b94acceSBarry Smith {
119885385478SLisandro Dalcin   PetscErrorCode ierr;
11993a40ed3dSBarry Smith   PetscFunctionBegin;
12000700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1201d2a683ecSLisandro Dalcin   if (r) {
1202d2a683ecSLisandro Dalcin     PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1203d2a683ecSLisandro Dalcin     PetscCheckSameComm(snes,1,r,2);
120485385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
12056bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
120685385478SLisandro Dalcin     snes->vec_func = r;
1207d2a683ecSLisandro Dalcin   } else if (!snes->vec_func && snes->dm) {
1208d2a683ecSLisandro Dalcin     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1209d2a683ecSLisandro Dalcin   }
1210d2a683ecSLisandro Dalcin   if (func) snes->ops->computefunction = func;
1211d2a683ecSLisandro Dalcin   if (ctx)  snes->funP                 = ctx;
12123a40ed3dSBarry Smith   PetscFunctionReturn(0);
12139b94acceSBarry Smith }
12149b94acceSBarry Smith 
1215646217ecSPeter Brune 
1216646217ecSPeter Brune #undef __FUNCT__
1217646217ecSPeter Brune #define __FUNCT__ "SNESSetGS"
1218c79ef259SPeter Brune /*@C
1219c79ef259SPeter Brune    SNESSetGS - Sets the user nonlinear Gauss-Seidel routine for
1220c79ef259SPeter Brune    use with composed nonlinear solvers.
1221c79ef259SPeter Brune 
1222c79ef259SPeter Brune    Input Parameters:
1223c79ef259SPeter Brune +  snes   - the SNES context
1224c79ef259SPeter Brune .  gsfunc - function evaluation routine
1225c79ef259SPeter Brune -  ctx    - [optional] user-defined context for private data for the
1226c79ef259SPeter Brune             smoother evaluation routine (may be PETSC_NULL)
1227c79ef259SPeter Brune 
1228c79ef259SPeter Brune    Calling sequence of func:
1229c79ef259SPeter Brune $    func (SNES snes,Vec x,Vec b,void *ctx);
1230c79ef259SPeter Brune 
1231c79ef259SPeter Brune +  X   - solution vector
1232c79ef259SPeter Brune .  B   - RHS vector
1233d28543b3SPeter Brune -  ctx - optional user-defined Gauss-Seidel context
1234c79ef259SPeter Brune 
1235c79ef259SPeter Brune    Notes:
1236c79ef259SPeter Brune    The GS routines are used by the composed nonlinear solver to generate
1237c79ef259SPeter Brune     a problem appropriate update to the solution, particularly FAS.
1238c79ef259SPeter Brune 
1239d28543b3SPeter Brune    Level: intermediate
1240c79ef259SPeter Brune 
1241d28543b3SPeter Brune .keywords: SNES, nonlinear, set, Gauss-Seidel
1242c79ef259SPeter Brune 
1243c79ef259SPeter Brune .seealso: SNESGetFunction(), SNESComputeGS()
1244c79ef259SPeter Brune @*/
1245646217ecSPeter Brune PetscErrorCode SNESSetGS(SNES snes, PetscErrorCode (*gsfunc)(SNES,Vec,Vec,void *), void * ctx) {
1246646217ecSPeter Brune   PetscFunctionBegin;
1247646217ecSPeter Brune   if (gsfunc) snes->ops->computegs = gsfunc;
1248646217ecSPeter Brune   if (ctx) snes->gsP = ctx;
1249646217ecSPeter Brune   PetscFunctionReturn(0);
1250646217ecSPeter Brune }
1251646217ecSPeter Brune 
1252d25893d9SBarry Smith #undef __FUNCT__
1253d28543b3SPeter Brune #define __FUNCT__ "SNESSetUseGS"
1254d28543b3SPeter Brune /*@
1255d28543b3SPeter Brune    SNESSetUseGS - Toggles use of the user-set nonlinear GS solver in
1256d28543b3SPeter Brune    SNES solvers that may use a preconditioning routine.
1257d28543b3SPeter Brune 
1258d28543b3SPeter Brune    Input Parameters:
1259d28543b3SPeter Brune +  snes   - the SNES context
1260d28543b3SPeter Brune -  usegs  - whether to use the nonlinear GS routine or not.
1261d28543b3SPeter Brune 
1262d28543b3SPeter Brune    Notes:
1263d28543b3SPeter Brune    The behavior of the nonlinear GS solvers is that if useGS is set, then
1264d28543b3SPeter Brune    the update is constructed with the user-defined nonlinear GS method.
1265d28543b3SPeter Brune    Otherwise, the update is either formed by the user-customized
1266d28543b3SPeter Brune    preconditioner SNES, or by nonlinear richardson if both of these
1267d28543b3SPeter Brune    are not provided.
1268d28543b3SPeter Brune 
1269d28543b3SPeter Brune    Level: intermediate
1270d28543b3SPeter Brune 
1271d28543b3SPeter Brune .keywords: SNES, nonlinear, set, Gauss-Siedel
1272d28543b3SPeter Brune 
1273d28543b3SPeter Brune .seealso: SNESSetGS(), SNESGetGS(), SNESSetPC(), SNESGetUseGS()
1274d28543b3SPeter Brune @*/
1275d28543b3SPeter Brune PetscErrorCode SNESSetUseGS(SNES snes, PetscBool usegs) {
1276d28543b3SPeter Brune   PetscFunctionBegin;
1277d28543b3SPeter Brune   snes->usegs = usegs;
1278d28543b3SPeter Brune   PetscFunctionReturn(0);
1279d28543b3SPeter Brune }
1280d28543b3SPeter Brune 
1281d28543b3SPeter Brune 
1282d28543b3SPeter Brune #undef __FUNCT__
1283d28543b3SPeter Brune #define __FUNCT__ "SNESGetUseGS"
1284d28543b3SPeter Brune /*@
1285d28543b3SPeter Brune    SNESGetUseGS - Returns whether or not the SNES is using a
1286d28543b3SPeter Brune    provided nonlinear GS routine.
1287d28543b3SPeter Brune 
1288d28543b3SPeter Brune    Input Parameters:
1289d28543b3SPeter Brune +  snes   - the SNES context
1290d28543b3SPeter Brune -  usegs  - flag indicating whether or not GS is being used
1291d28543b3SPeter Brune 
1292d28543b3SPeter Brune    Level: intermediate
1293d28543b3SPeter Brune 
1294d28543b3SPeter Brune .keywords: SNES, nonlinear, set, Gauss-Seidel
1295d28543b3SPeter Brune 
1296d28543b3SPeter Brune .seealso: SNESSetGS(), SNESGetGS(), SNESSetUseGS()
1297d28543b3SPeter Brune @*/
1298d28543b3SPeter Brune 
1299d28543b3SPeter Brune PetscErrorCode SNESGetUseGS(SNES snes, PetscBool * usegs) {
1300d28543b3SPeter Brune   PetscFunctionBegin;
1301d28543b3SPeter Brune 
1302d28543b3SPeter Brune   *usegs = snes->usegs;
1303d28543b3SPeter Brune   PetscFunctionReturn(0);
1304d28543b3SPeter Brune }
1305d28543b3SPeter Brune 
1306d28543b3SPeter Brune #undef __FUNCT__
13078b0a5094SBarry Smith #define __FUNCT__ "SNESPicardComputeFunction"
13088b0a5094SBarry Smith PetscErrorCode  SNESPicardComputeFunction(SNES snes,Vec x,Vec f,void *ctx)
13098b0a5094SBarry Smith {
13108b0a5094SBarry Smith   PetscErrorCode ierr;
13118b0a5094SBarry Smith   PetscFunctionBegin;
13128b0a5094SBarry Smith   /*  A(x)*x - b(x) */
13138b0a5094SBarry Smith   ierr = (*snes->ops->computepjacobian)(snes,x,&snes->jacobian,&snes->jacobian_pre,&snes->matstruct,snes->jacP);CHKERRQ(ierr);
13148b0a5094SBarry Smith   ierr = (*snes->ops->computepfunction)(snes,x,f,snes->funP);CHKERRQ(ierr);
13158b0a5094SBarry Smith   ierr = VecView(x,PETSC_VIEWER_BINARY_WORLD);CHKERRQ(ierr);
13168b0a5094SBarry Smith   ierr = VecView(f,PETSC_VIEWER_BINARY_WORLD);CHKERRQ(ierr);
13178b0a5094SBarry Smith   ierr = VecScale(f,-1.0);CHKERRQ(ierr);
13188b0a5094SBarry Smith   ierr = MatMultAdd(snes->jacobian_pre,x,f,f);CHKERRQ(ierr);
13198b0a5094SBarry Smith   PetscFunctionReturn(0);
13208b0a5094SBarry Smith }
13218b0a5094SBarry Smith 
13228b0a5094SBarry Smith #undef __FUNCT__
13238b0a5094SBarry Smith #define __FUNCT__ "SNESPicardComputeJacobian"
13248b0a5094SBarry Smith PetscErrorCode  SNESPicardComputeJacobian(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx)
13258b0a5094SBarry Smith {
13268b0a5094SBarry Smith   PetscFunctionBegin;
13278b0a5094SBarry Smith   *flag = snes->matstruct;
13288b0a5094SBarry Smith   PetscFunctionReturn(0);
13298b0a5094SBarry Smith }
13308b0a5094SBarry Smith 
13318b0a5094SBarry Smith #undef __FUNCT__
13328b0a5094SBarry Smith #define __FUNCT__ "SNESSetPicard"
13338b0a5094SBarry Smith /*@C
13340d04baf8SBarry Smith    SNESSetPicard - Use SNES to solve the semilinear-system A(x) x = b(x) via a Picard type iteration (Picard linearization)
13358b0a5094SBarry Smith 
13368b0a5094SBarry Smith    Logically Collective on SNES
13378b0a5094SBarry Smith 
13388b0a5094SBarry Smith    Input Parameters:
13398b0a5094SBarry Smith +  snes - the SNES context
13408b0a5094SBarry Smith .  r - vector to store function value
13418b0a5094SBarry Smith .  func - function evaluation routine
13428b0a5094SBarry Smith .  jmat - normally the same as mat but you can pass another matrix for which you compute the Jacobian of A(x) x - b(x) (see jmat below)
13438b0a5094SBarry Smith .  mat - matrix to store A
13448b0a5094SBarry Smith .  mfunc  - function to compute matrix value
13458b0a5094SBarry Smith -  ctx - [optional] user-defined context for private data for the
13468b0a5094SBarry Smith          function evaluation routine (may be PETSC_NULL)
13478b0a5094SBarry Smith 
13488b0a5094SBarry Smith    Calling sequence of func:
13498b0a5094SBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
13508b0a5094SBarry Smith 
13518b0a5094SBarry Smith +  f - function vector
13528b0a5094SBarry Smith -  ctx - optional user-defined function context
13538b0a5094SBarry Smith 
13548b0a5094SBarry Smith    Calling sequence of mfunc:
13558b0a5094SBarry Smith $     mfunc (SNES snes,Vec x,Mat *jmat,Mat *mat,int *flag,void *ctx);
13568b0a5094SBarry Smith 
13578b0a5094SBarry Smith +  x - input vector
13588b0a5094SBarry Smith .  jmat - Form Jacobian matrix of A(x) x - b(x) if available, not there is really no reason to use it in this way since then you can just use SNESSetJacobian(),
13598b0a5094SBarry Smith           normally just pass mat in this location
13608b0a5094SBarry Smith .  mat - form A(x) matrix
13618b0a5094SBarry Smith .  flag - flag indicating information about the preconditioner matrix
13628b0a5094SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
13638b0a5094SBarry Smith -  ctx - [optional] user-defined Jacobian context
13648b0a5094SBarry Smith 
13658b0a5094SBarry Smith    Notes:
13668b0a5094SBarry Smith     One can call SNESSetPicard() or SNESSetFunction() (and possibly SNESSetJacobian()) but cannot call both
13678b0a5094SBarry Smith 
13688b0a5094SBarry Smith $     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}
13698b0a5094SBarry Smith $     Note that when an exact solver is used this corresponds to the "classic" Picard A(x^{n}) x^{n+1} = b(x^{n}) iteration.
13708b0a5094SBarry Smith 
13718b0a5094SBarry Smith      Run with -snes_mf_operator to solve the system with Newton's method using A(x^{n}) to construct the preconditioner.
13728b0a5094SBarry Smith 
13730d04baf8SBarry Smith    We implement the defect correction form of the Picard iteration because it converges much more generally when inexact linear solvers are used then
13740d04baf8SBarry Smith    the direct Picard iteration A(x^n) x^{n+1} = b(x^n)
13758b0a5094SBarry Smith 
13768b0a5094SBarry Smith    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
13778b0a5094SBarry Smith    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
13788b0a5094SBarry Smith    different please contact us at petsc-dev@mcs.anl.gov and we'll have an entirely new argument :-).
13798b0a5094SBarry Smith 
13808b0a5094SBarry Smith    Level: beginner
13818b0a5094SBarry Smith 
13828b0a5094SBarry Smith .keywords: SNES, nonlinear, set, function
13838b0a5094SBarry Smith 
13840d04baf8SBarry Smith .seealso: SNESGetFunction(), SNESSetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESGetPicard(), SNESLineSearchPreCheckPicard()
13858b0a5094SBarry Smith @*/
13868b0a5094SBarry Smith PetscErrorCode  SNESSetPicard(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),Mat jmat, Mat mat, PetscErrorCode (*mfunc)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
13878b0a5094SBarry Smith {
13888b0a5094SBarry Smith   PetscErrorCode ierr;
13898b0a5094SBarry Smith   PetscFunctionBegin;
13908b0a5094SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13918b0a5094SBarry Smith   snes->ops->computepfunction = func;
13928b0a5094SBarry Smith   snes->ops->computepjacobian = mfunc;
13938b0a5094SBarry Smith   ierr = SNESSetFunction(snes,r,SNESPicardComputeFunction,ctx);CHKERRQ(ierr);
13948b0a5094SBarry Smith   ierr = SNESSetJacobian(snes,jmat,mat,SNESPicardComputeJacobian,ctx);CHKERRQ(ierr);
13958b0a5094SBarry Smith   PetscFunctionReturn(0);
13968b0a5094SBarry Smith }
13978b0a5094SBarry Smith 
13988b0a5094SBarry Smith #undef __FUNCT__
1399d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess"
1400d25893d9SBarry Smith /*@C
1401d25893d9SBarry Smith    SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem
1402d25893d9SBarry Smith 
1403d25893d9SBarry Smith    Logically Collective on SNES
1404d25893d9SBarry Smith 
1405d25893d9SBarry Smith    Input Parameters:
1406d25893d9SBarry Smith +  snes - the SNES context
1407d25893d9SBarry Smith .  func - function evaluation routine
1408d25893d9SBarry Smith -  ctx - [optional] user-defined context for private data for the
1409d25893d9SBarry Smith          function evaluation routine (may be PETSC_NULL)
1410d25893d9SBarry Smith 
1411d25893d9SBarry Smith    Calling sequence of func:
1412d25893d9SBarry Smith $    func (SNES snes,Vec x,void *ctx);
1413d25893d9SBarry Smith 
1414d25893d9SBarry Smith .  f - function vector
1415d25893d9SBarry Smith -  ctx - optional user-defined function context
1416d25893d9SBarry Smith 
1417d25893d9SBarry Smith    Level: intermediate
1418d25893d9SBarry Smith 
1419d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function
1420d25893d9SBarry Smith 
1421d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
1422d25893d9SBarry Smith @*/
1423d25893d9SBarry Smith PetscErrorCode  SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx)
1424d25893d9SBarry Smith {
1425d25893d9SBarry Smith   PetscFunctionBegin;
1426d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1427d25893d9SBarry Smith   if (func) snes->ops->computeinitialguess = func;
1428d25893d9SBarry Smith   if (ctx)  snes->initialguessP            = ctx;
1429d25893d9SBarry Smith   PetscFunctionReturn(0);
1430d25893d9SBarry Smith }
1431d25893d9SBarry Smith 
14323ab0aad5SBarry Smith /* --------------------------------------------------------------- */
14333ab0aad5SBarry Smith #undef __FUNCT__
14341096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
14351096aae1SMatthew Knepley /*@C
14361096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
14371096aae1SMatthew Knepley    it assumes a zero right hand side.
14381096aae1SMatthew Knepley 
14393f9fe445SBarry Smith    Logically Collective on SNES
14401096aae1SMatthew Knepley 
14411096aae1SMatthew Knepley    Input Parameter:
14421096aae1SMatthew Knepley .  snes - the SNES context
14431096aae1SMatthew Knepley 
14441096aae1SMatthew Knepley    Output Parameter:
1445bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
14461096aae1SMatthew Knepley 
14471096aae1SMatthew Knepley    Level: intermediate
14481096aae1SMatthew Knepley 
14491096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
14501096aae1SMatthew Knepley 
145185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
14521096aae1SMatthew Knepley @*/
14537087cfbeSBarry Smith PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
14541096aae1SMatthew Knepley {
14551096aae1SMatthew Knepley   PetscFunctionBegin;
14560700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14571096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
145885385478SLisandro Dalcin   *rhs = snes->vec_rhs;
14591096aae1SMatthew Knepley   PetscFunctionReturn(0);
14601096aae1SMatthew Knepley }
14611096aae1SMatthew Knepley 
14621096aae1SMatthew Knepley #undef __FUNCT__
14634a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
14649b94acceSBarry Smith /*@
146536851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
14669b94acceSBarry Smith                          SNESSetFunction().
14679b94acceSBarry Smith 
1468c7afd0dbSLois Curfman McInnes    Collective on SNES
1469c7afd0dbSLois Curfman McInnes 
14709b94acceSBarry Smith    Input Parameters:
1471c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1472c7afd0dbSLois Curfman McInnes -  x - input vector
14739b94acceSBarry Smith 
14749b94acceSBarry Smith    Output Parameter:
14753638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
14769b94acceSBarry Smith 
14771bffabb2SLois Curfman McInnes    Notes:
147836851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
147936851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
148036851e7fSLois Curfman McInnes    themselves.
148136851e7fSLois Curfman McInnes 
148236851e7fSLois Curfman McInnes    Level: developer
148336851e7fSLois Curfman McInnes 
14849b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
14859b94acceSBarry Smith 
1486a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
14879b94acceSBarry Smith @*/
14887087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
14899b94acceSBarry Smith {
1490dfbe8321SBarry Smith   PetscErrorCode ierr;
14919b94acceSBarry Smith 
14923a40ed3dSBarry Smith   PetscFunctionBegin;
14930700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14940700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
14950700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1496c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1497c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1498184914b5SBarry Smith 
1499d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1500e7788613SBarry Smith   if (snes->ops->computefunction) {
1501d64ed03dSBarry Smith     PetscStackPush("SNES user function");
150239d508bbSBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1503d64ed03dSBarry Smith     PetscStackPop;
150473250ac0SBarry Smith   } else if (snes->dm) {
1505644e2e5bSBarry Smith     ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr);
1506c90fad12SPeter Brune   } else if (snes->vec_rhs) {
1507c90fad12SPeter Brune     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
1508644e2e5bSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve().");
150985385478SLisandro Dalcin   if (snes->vec_rhs) {
151085385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
15113ab0aad5SBarry Smith   }
1512ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1513d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
15143a40ed3dSBarry Smith   PetscFunctionReturn(0);
15159b94acceSBarry Smith }
15169b94acceSBarry Smith 
15174a2ae208SSatish Balay #undef __FUNCT__
1518646217ecSPeter Brune #define __FUNCT__ "SNESComputeGS"
1519c79ef259SPeter Brune /*@
1520c79ef259SPeter Brune    SNESComputeGS - Calls the Gauss-Seidel function that has been set with
1521c79ef259SPeter Brune                    SNESSetGS().
1522c79ef259SPeter Brune 
1523c79ef259SPeter Brune    Collective on SNES
1524c79ef259SPeter Brune 
1525c79ef259SPeter Brune    Input Parameters:
1526c79ef259SPeter Brune +  snes - the SNES context
1527c79ef259SPeter Brune .  x - input vector
1528c79ef259SPeter Brune -  b - rhs vector
1529c79ef259SPeter Brune 
1530c79ef259SPeter Brune    Output Parameter:
1531c79ef259SPeter Brune .  x - new solution vector
1532c79ef259SPeter Brune 
1533c79ef259SPeter Brune    Notes:
1534c79ef259SPeter Brune    SNESComputeGS() is typically used within composed nonlinear solver
1535c79ef259SPeter Brune    implementations, so most users would not generally call this routine
1536c79ef259SPeter Brune    themselves.
1537c79ef259SPeter Brune 
1538c79ef259SPeter Brune    Level: developer
1539c79ef259SPeter Brune 
1540c79ef259SPeter Brune .keywords: SNES, nonlinear, compute, function
1541c79ef259SPeter Brune 
1542c79ef259SPeter Brune .seealso: SNESSetGS(), SNESComputeFunction()
1543c79ef259SPeter Brune @*/
1544646217ecSPeter Brune PetscErrorCode  SNESComputeGS(SNES snes,Vec b,Vec x)
1545646217ecSPeter Brune {
1546646217ecSPeter Brune   PetscErrorCode ierr;
1547646217ecSPeter Brune 
1548646217ecSPeter Brune   PetscFunctionBegin;
1549646217ecSPeter Brune   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1550646217ecSPeter Brune   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
1551646217ecSPeter Brune   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,3);
1552646217ecSPeter Brune   PetscCheckSameComm(snes,1,x,2);
1553646217ecSPeter Brune   if(b) PetscCheckSameComm(snes,1,b,3);
1554701cf23dSPeter Brune   ierr = PetscLogEventBegin(SNES_GSEval,snes,x,b,0);CHKERRQ(ierr);
1555646217ecSPeter Brune   if (snes->ops->computegs) {
1556646217ecSPeter Brune     PetscStackPush("SNES user GS");
1557646217ecSPeter Brune     ierr = (*snes->ops->computegs)(snes,x,b,snes->gsP);CHKERRQ(ierr);
1558646217ecSPeter Brune     PetscStackPop;
1559646217ecSPeter Brune   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetGS() before SNESComputeGS(), likely called from SNESSolve().");
1560701cf23dSPeter Brune   ierr = PetscLogEventEnd(SNES_GSEval,snes,x,b,0);CHKERRQ(ierr);
1561646217ecSPeter Brune   PetscFunctionReturn(0);
1562646217ecSPeter Brune }
1563646217ecSPeter Brune 
1564646217ecSPeter Brune 
1565646217ecSPeter Brune #undef __FUNCT__
15664a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
156762fef451SLois Curfman McInnes /*@
156862fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
156962fef451SLois Curfman McInnes    set with SNESSetJacobian().
157062fef451SLois Curfman McInnes 
1571c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1572c7afd0dbSLois Curfman McInnes 
157362fef451SLois Curfman McInnes    Input Parameters:
1574c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1575c7afd0dbSLois Curfman McInnes -  x - input vector
157662fef451SLois Curfman McInnes 
157762fef451SLois Curfman McInnes    Output Parameters:
1578c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
157962fef451SLois Curfman McInnes .  B - optional preconditioning matrix
15802b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1581fee21e36SBarry Smith 
1582e35cf81dSBarry Smith   Options Database Keys:
1583e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1584693365a8SJed Brown .    -snes_lag_jacobian <lag>
1585693365a8SJed Brown .    -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences
1586693365a8SJed Brown .    -snes_compare_explicit_draw  - Compare the computed Jacobian to the finite difference Jacobian and draw the result
1587693365a8SJed Brown .    -snes_compare_explicit_contour  - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result
15884c30e9fbSJed Brown .    -snes_compare_operator  - Make the comparison options above use the operator instead of the preconditioning matrix
1589c01495d3SJed Brown .    -snes_compare_coloring - Compute the finite differece Jacobian using coloring and display norms of difference
1590c01495d3SJed Brown .    -snes_compare_coloring_display - Compute the finite differece Jacobian using coloring and display verbose differences
1591c01495d3SJed Brown .    -snes_compare_coloring_threshold - Display only those matrix entries that differ by more than a given threshold
1592c01495d3SJed Brown .    -snes_compare_coloring_threshold_atol - Absolute tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold
1593c01495d3SJed Brown .    -snes_compare_coloring_threshold_rtol - Relative tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold
15944c30e9fbSJed Brown .    -snes_compare_coloring_draw - Compute the finite differece Jacobian using coloring and draw differences
1595c01495d3SJed Brown -    -snes_compare_coloring_draw_contour - Compute the finite differece Jacobian using coloring and show contours of matrices and differences
1596c01495d3SJed Brown 
1597e35cf81dSBarry Smith 
159862fef451SLois Curfman McInnes    Notes:
159962fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
160062fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
160162fef451SLois Curfman McInnes 
160294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1603dc5a77f8SLois Curfman McInnes    flag parameter.
160462fef451SLois Curfman McInnes 
160536851e7fSLois Curfman McInnes    Level: developer
160636851e7fSLois Curfman McInnes 
160762fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
160862fef451SLois Curfman McInnes 
1609e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
161062fef451SLois Curfman McInnes @*/
16117087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
16129b94acceSBarry Smith {
1613dfbe8321SBarry Smith   PetscErrorCode ierr;
1614ace3abfcSBarry Smith   PetscBool      flag;
16153a40ed3dSBarry Smith 
16163a40ed3dSBarry Smith   PetscFunctionBegin;
16170700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
16180700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
16194482741eSBarry Smith   PetscValidPointer(flg,5);
1620c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1621e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1622ebd3b9afSBarry Smith 
1623ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1624ebd3b9afSBarry Smith 
1625fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1626fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1627fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1628fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1629e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1630e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1631ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1632ebd3b9afSBarry Smith     if (flag) {
1633ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1634ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1635ebd3b9afSBarry Smith     }
1636e35cf81dSBarry Smith     PetscFunctionReturn(0);
1637e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1638e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1639e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1640ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1641ebd3b9afSBarry Smith     if (flag) {
1642ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1643ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1644ebd3b9afSBarry Smith     }
1645e35cf81dSBarry Smith     PetscFunctionReturn(0);
1646e35cf81dSBarry Smith   }
1647e35cf81dSBarry Smith 
1648c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1649e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1650d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1651e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1652d64ed03dSBarry Smith   PetscStackPop;
1653d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1654a8054027SBarry Smith 
16553b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
16563b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
16573b4f5425SBarry Smith     snes->lagpreconditioner = -1;
16583b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1659a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1660a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1661a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1662a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1663a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1664a8054027SBarry Smith   }
1665a8054027SBarry Smith 
16666d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
16670700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
16680700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
1669693365a8SJed Brown   {
1670693365a8SJed Brown     PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE;
1671693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr);
1672693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr);
1673693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr);
1674693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr);
1675693365a8SJed Brown     if (flag || flag_draw || flag_contour) {
1676693365a8SJed Brown       Mat Bexp_mine = PETSC_NULL,Bexp,FDexp;
1677693365a8SJed Brown       MatStructure mstruct;
1678693365a8SJed Brown       PetscViewer vdraw,vstdout;
16796b3a5b13SJed Brown       PetscBool flg;
1680693365a8SJed Brown       if (flag_operator) {
1681693365a8SJed Brown         ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr);
1682693365a8SJed Brown         Bexp = Bexp_mine;
1683693365a8SJed Brown       } else {
1684693365a8SJed Brown         /* See if the preconditioning matrix can be viewed and added directly */
1685693365a8SJed Brown         ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr);
1686693365a8SJed Brown         if (flg) Bexp = *B;
1687693365a8SJed Brown         else {
1688693365a8SJed Brown           /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */
1689693365a8SJed Brown           ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr);
1690693365a8SJed Brown           Bexp = Bexp_mine;
1691693365a8SJed Brown         }
1692693365a8SJed Brown       }
1693693365a8SJed Brown       ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr);
1694693365a8SJed Brown       ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr);
1695693365a8SJed Brown       ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr);
1696693365a8SJed Brown       if (flag_draw || flag_contour) {
1697693365a8SJed Brown         ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr);
1698693365a8SJed Brown         if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);}
1699693365a8SJed Brown       } else vdraw = PETSC_NULL;
1700693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr);
1701693365a8SJed Brown       if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);}
1702693365a8SJed Brown       if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);}
1703693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr);
1704693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1705693365a8SJed Brown       if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);}
1706693365a8SJed Brown       ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
1707693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr);
1708693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1709693365a8SJed Brown       if (vdraw) {              /* Always use contour for the difference */
1710693365a8SJed Brown         ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
1711693365a8SJed Brown         ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);
1712693365a8SJed Brown         ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);
1713693365a8SJed Brown       }
1714693365a8SJed Brown       if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);}
1715693365a8SJed Brown       ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr);
1716693365a8SJed Brown       ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr);
1717693365a8SJed Brown       ierr = MatDestroy(&FDexp);CHKERRQ(ierr);
1718693365a8SJed Brown     }
1719693365a8SJed Brown   }
17204c30e9fbSJed Brown   {
17216719d8e4SJed Brown     PetscBool flag = PETSC_FALSE,flag_display = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_threshold = PETSC_FALSE;
17226719d8e4SJed Brown     PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON,threshold_rtol = 10*PETSC_SQRT_MACHINE_EPSILON;
17234c30e9fbSJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring",&flag,PETSC_NULL);CHKERRQ(ierr);
17246719d8e4SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_display",&flag_display,PETSC_NULL);CHKERRQ(ierr);
17254c30e9fbSJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr);
17264c30e9fbSJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr);
17276719d8e4SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold",&flag_threshold,PETSC_NULL);CHKERRQ(ierr);
17286719d8e4SJed Brown     ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_rtol",&threshold_rtol,PETSC_NULL);CHKERRQ(ierr);
17296719d8e4SJed Brown     ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_atol",&threshold_atol,PETSC_NULL);CHKERRQ(ierr);
17306719d8e4SJed Brown     if (flag || flag_display || flag_draw || flag_contour || flag_threshold) {
17314c30e9fbSJed Brown       Mat Bfd;
17324c30e9fbSJed Brown       MatStructure mstruct;
17334c30e9fbSJed Brown       PetscViewer vdraw,vstdout;
17344c30e9fbSJed Brown       ISColoring iscoloring;
17354c30e9fbSJed Brown       MatFDColoring matfdcoloring;
17364c30e9fbSJed Brown       PetscErrorCode (*func)(SNES,Vec,Vec,void*);
17374c30e9fbSJed Brown       void *funcctx;
17386719d8e4SJed Brown       PetscReal norm1,norm2,normmax;
17394c30e9fbSJed Brown 
17404c30e9fbSJed Brown       ierr = MatDuplicate(*B,MAT_DO_NOT_COPY_VALUES,&Bfd);CHKERRQ(ierr);
17414c30e9fbSJed Brown       ierr = MatGetColoring(Bfd,MATCOLORINGSL,&iscoloring);CHKERRQ(ierr);
17424c30e9fbSJed Brown       ierr = MatFDColoringCreate(Bfd,iscoloring,&matfdcoloring);CHKERRQ(ierr);
17434c30e9fbSJed Brown       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
17444c30e9fbSJed Brown 
17454c30e9fbSJed Brown       /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */
17464c30e9fbSJed Brown       ierr = SNESGetFunction(snes,PETSC_NULL,&func,&funcctx);CHKERRQ(ierr);
17474c30e9fbSJed Brown       ierr = MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode(*)(void))func,funcctx);CHKERRQ(ierr);
17484c30e9fbSJed Brown       ierr = PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring,((PetscObject)snes)->prefix);CHKERRQ(ierr);
17494c30e9fbSJed Brown       ierr = PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring,"coloring_");CHKERRQ(ierr);
17504c30e9fbSJed Brown       ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr);
17514c30e9fbSJed Brown       ierr = MatFDColoringApply(Bfd,matfdcoloring,X,&mstruct,snes);CHKERRQ(ierr);
17524c30e9fbSJed Brown       ierr = MatFDColoringDestroy(&matfdcoloring);CHKERRQ(ierr);
17534c30e9fbSJed Brown 
17544c30e9fbSJed Brown       ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr);
17554c30e9fbSJed Brown       if (flag_draw || flag_contour) {
17564c30e9fbSJed Brown         ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Colored Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr);
17574c30e9fbSJed Brown         if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);}
17584c30e9fbSJed Brown       } else vdraw = PETSC_NULL;
17594c30e9fbSJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Explicit preconditioning Jacobian\n");CHKERRQ(ierr);
17606719d8e4SJed Brown       if (flag_display) {ierr = MatView(*B,vstdout);CHKERRQ(ierr);}
17614c30e9fbSJed Brown       if (vdraw) {ierr = MatView(*B,vdraw);CHKERRQ(ierr);}
17624c30e9fbSJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Colored Finite difference Jacobian\n");CHKERRQ(ierr);
17636719d8e4SJed Brown       if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);}
17644c30e9fbSJed Brown       if (vdraw) {ierr = MatView(Bfd,vdraw);CHKERRQ(ierr);}
17654c30e9fbSJed Brown       ierr = MatAYPX(Bfd,-1.0,*B,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
17664c30e9fbSJed Brown       ierr = MatNorm(Bfd,NORM_1,&norm1);CHKERRQ(ierr);
17676719d8e4SJed Brown       ierr = MatNorm(Bfd,NORM_FROBENIUS,&norm2);CHKERRQ(ierr);
17684c30e9fbSJed Brown       ierr = MatNorm(Bfd,NORM_MAX,&normmax);CHKERRQ(ierr);
17696719d8e4SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian, norm1=%G normFrob=%G normmax=%G\n",norm1,norm2,normmax);CHKERRQ(ierr);
17706719d8e4SJed Brown       if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);}
17714c30e9fbSJed Brown       if (vdraw) {              /* Always use contour for the difference */
17724c30e9fbSJed Brown         ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
17734c30e9fbSJed Brown         ierr = MatView(Bfd,vdraw);CHKERRQ(ierr);
17744c30e9fbSJed Brown         ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);
17754c30e9fbSJed Brown       }
17764c30e9fbSJed Brown       if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);}
17776719d8e4SJed Brown 
17786719d8e4SJed Brown       if (flag_threshold) {
17796719d8e4SJed Brown         PetscInt bs,rstart,rend,i;
17806719d8e4SJed Brown         ierr = MatGetBlockSize(*B,&bs);CHKERRQ(ierr);
17816719d8e4SJed Brown         ierr = MatGetOwnershipRange(*B,&rstart,&rend);CHKERRQ(ierr);
17826719d8e4SJed Brown         for (i=rstart; i<rend; i++) {
17836719d8e4SJed Brown           const PetscScalar *ba,*ca;
17846719d8e4SJed Brown           const PetscInt *bj,*cj;
17856719d8e4SJed Brown           PetscInt bn,cn,j,maxentrycol = -1,maxdiffcol = -1,maxrdiffcol = -1;
17866719d8e4SJed Brown           PetscReal maxentry = 0,maxdiff = 0,maxrdiff = 0;
17876719d8e4SJed Brown           ierr = MatGetRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr);
17886719d8e4SJed Brown           ierr = MatGetRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr);
17896719d8e4SJed Brown           if (bn != cn) SETERRQ(((PetscObject)*A)->comm,PETSC_ERR_PLIB,"Unexpected different nonzero pattern in -snes_compare_coloring_threshold");
17906719d8e4SJed Brown           for (j=0; j<bn; j++) {
17916719d8e4SJed Brown             PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j]));
17926719d8e4SJed Brown             if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) {
17936719d8e4SJed Brown               maxentrycol = bj[j];
17946719d8e4SJed Brown               maxentry = PetscRealPart(ba[j]);
17956719d8e4SJed Brown             }
17966719d8e4SJed Brown             if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) {
17976719d8e4SJed Brown               maxdiffcol = bj[j];
17986719d8e4SJed Brown               maxdiff = PetscRealPart(ca[j]);
17996719d8e4SJed Brown             }
18006719d8e4SJed Brown             if (rdiff > maxrdiff) {
18016719d8e4SJed Brown               maxrdiffcol = bj[j];
18026719d8e4SJed Brown               maxrdiff = rdiff;
18036719d8e4SJed Brown             }
18046719d8e4SJed Brown           }
18056719d8e4SJed Brown           if (maxrdiff > 1) {
18066719d8e4SJed Brown             ierr = PetscViewerASCIIPrintf(vstdout,"row %D (maxentry=%G at %D, maxdiff=%G at %D, maxrdiff=%G at %D):",i,maxentry,maxentrycol,maxdiff,maxdiffcol,maxrdiff,maxrdiffcol);CHKERRQ(ierr);
18076719d8e4SJed Brown             for (j=0; j<bn; j++) {
18086719d8e4SJed Brown               PetscReal rdiff;
18096719d8e4SJed Brown               rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j]));
18106719d8e4SJed Brown               if (rdiff > 1) {
18116719d8e4SJed Brown                 ierr = PetscViewerASCIIPrintf(vstdout," (%D,%G:%G)",bj[j],PetscRealPart(ba[j]),PetscRealPart(ca[j]));CHKERRQ(ierr);
18126719d8e4SJed Brown               }
18136719d8e4SJed Brown             }
18146719d8e4SJed Brown             ierr = PetscViewerASCIIPrintf(vstdout,"\n",i,maxentry,maxdiff,maxrdiff);CHKERRQ(ierr);
18156719d8e4SJed Brown           }
18166719d8e4SJed Brown           ierr = MatRestoreRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr);
18176719d8e4SJed Brown           ierr = MatRestoreRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr);
18186719d8e4SJed Brown         }
18196719d8e4SJed Brown       }
18204c30e9fbSJed Brown       ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr);
18214c30e9fbSJed Brown       ierr = MatDestroy(&Bfd);CHKERRQ(ierr);
18224c30e9fbSJed Brown     }
18234c30e9fbSJed Brown   }
18243a40ed3dSBarry Smith   PetscFunctionReturn(0);
18259b94acceSBarry Smith }
18269b94acceSBarry Smith 
18274a2ae208SSatish Balay #undef __FUNCT__
18284a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
18299b94acceSBarry Smith /*@C
18309b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1831044dda88SLois Curfman McInnes    location to store the matrix.
18329b94acceSBarry Smith 
18333f9fe445SBarry Smith    Logically Collective on SNES and Mat
1834c7afd0dbSLois Curfman McInnes 
18359b94acceSBarry Smith    Input Parameters:
1836c7afd0dbSLois Curfman McInnes +  snes - the SNES context
18379b94acceSBarry Smith .  A - Jacobian matrix
18389b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
1839efd51863SBarry Smith .  func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value)
1840c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1841efd51863SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value)
18429b94acceSBarry Smith 
18439b94acceSBarry Smith    Calling sequence of func:
18448d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
18459b94acceSBarry Smith 
1846c7afd0dbSLois Curfman McInnes +  x - input vector
18479b94acceSBarry Smith .  A - Jacobian matrix
18489b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1849ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
18502b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1851c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
18529b94acceSBarry Smith 
18539b94acceSBarry Smith    Notes:
185494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
18552cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1856ac21db08SLois Curfman McInnes 
1857ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
18589b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
18599b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
18609b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
18619b94acceSBarry Smith    throughout the global iterations.
18629b94acceSBarry Smith 
186316913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
186416913363SBarry Smith    each matrix.
186516913363SBarry Smith 
1866a8a26c1eSJed Brown    If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument
1867a8a26c1eSJed Brown    must be a MatFDColoring.
1868a8a26c1eSJed Brown 
1869c3cc8fd1SJed Brown    Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian.  One common
1870c3cc8fd1SJed Brown    example is to use the "Picard linearization" which only differentiates through the highest order parts of each term.
1871c3cc8fd1SJed Brown 
187236851e7fSLois Curfman McInnes    Level: beginner
187336851e7fSLois Curfman McInnes 
18749b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
18759b94acceSBarry Smith 
18763ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
18779b94acceSBarry Smith @*/
18787087cfbeSBarry Smith PetscErrorCode  SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
18799b94acceSBarry Smith {
1880dfbe8321SBarry Smith   PetscErrorCode ierr;
18813a7fca6bSBarry Smith 
18823a40ed3dSBarry Smith   PetscFunctionBegin;
18830700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
18840700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
18850700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1886c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
188706975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1888e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
18893a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
18903a7fca6bSBarry Smith   if (A) {
18917dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
18926bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
18939b94acceSBarry Smith     snes->jacobian = A;
18943a7fca6bSBarry Smith   }
18953a7fca6bSBarry Smith   if (B) {
18967dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
18976bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
18989b94acceSBarry Smith     snes->jacobian_pre = B;
18993a7fca6bSBarry Smith   }
19003a40ed3dSBarry Smith   PetscFunctionReturn(0);
19019b94acceSBarry Smith }
190262fef451SLois Curfman McInnes 
19034a2ae208SSatish Balay #undef __FUNCT__
19044a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1905c2aafc4cSSatish Balay /*@C
1906b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1907b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1908b4fd4287SBarry Smith 
1909c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1910c7afd0dbSLois Curfman McInnes 
1911b4fd4287SBarry Smith    Input Parameter:
1912b4fd4287SBarry Smith .  snes - the nonlinear solver context
1913b4fd4287SBarry Smith 
1914b4fd4287SBarry Smith    Output Parameters:
1915c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1916b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
191770e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
191870e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1919fee21e36SBarry Smith 
192036851e7fSLois Curfman McInnes    Level: advanced
192136851e7fSLois Curfman McInnes 
1922b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1923b4fd4287SBarry Smith @*/
19247087cfbeSBarry Smith PetscErrorCode  SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1925b4fd4287SBarry Smith {
19263a40ed3dSBarry Smith   PetscFunctionBegin;
19270700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1928b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1929b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1930e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
193170e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
19323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1933b4fd4287SBarry Smith }
1934b4fd4287SBarry Smith 
19359b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
19369b94acceSBarry Smith 
19374a2ae208SSatish Balay #undef __FUNCT__
19384a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
19399b94acceSBarry Smith /*@
19409b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1941272ac6f2SLois Curfman McInnes    of a nonlinear solver.
19429b94acceSBarry Smith 
1943fee21e36SBarry Smith    Collective on SNES
1944fee21e36SBarry Smith 
1945c7afd0dbSLois Curfman McInnes    Input Parameters:
194670e92668SMatthew Knepley .  snes - the SNES context
1947c7afd0dbSLois Curfman McInnes 
1948272ac6f2SLois Curfman McInnes    Notes:
1949272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1950272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1951272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1952272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1953272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1954272ac6f2SLois Curfman McInnes 
195536851e7fSLois Curfman McInnes    Level: advanced
195636851e7fSLois Curfman McInnes 
19579b94acceSBarry Smith .keywords: SNES, nonlinear, setup
19589b94acceSBarry Smith 
19599b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
19609b94acceSBarry Smith @*/
19617087cfbeSBarry Smith PetscErrorCode  SNESSetUp(SNES snes)
19629b94acceSBarry Smith {
1963dfbe8321SBarry Smith   PetscErrorCode ierr;
19643a40ed3dSBarry Smith 
19653a40ed3dSBarry Smith   PetscFunctionBegin;
19660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
19674dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
19689b94acceSBarry Smith 
19697adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
197085385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
197185385478SLisandro Dalcin   }
197285385478SLisandro Dalcin 
1973a63bb30eSJed Brown   ierr = SNESGetFunction(snes,&snes->vec_func,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
197417186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
197558c9b817SLisandro Dalcin   if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
197658c9b817SLisandro Dalcin 
197758c9b817SLisandro Dalcin   if (!snes->vec_sol_update /* && snes->vec_sol */) {
197858c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr);
197958c9b817SLisandro Dalcin     ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr);
198058c9b817SLisandro Dalcin   }
198158c9b817SLisandro Dalcin 
1982ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1983ef8dffc7SBarry Smith     Mat J;
1984950540a4SJed Brown     ierr = DMCreateMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1985cab2e9ccSBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDMComputeJacobian,PETSC_NULL);CHKERRQ(ierr);
1986cab2e9ccSBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
19872ef29e96SBarry Smith   } else if (!snes->jacobian && snes->ops->computejacobian == MatMFFDComputeJacobian) {
1988a8248277SBarry Smith     Mat J;
1989a8248277SBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
1990a8248277SBarry Smith     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
1991a8248277SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
1992a8248277SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
1993a8248277SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1994a8248277SBarry Smith   } else if (snes->dm && snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) {
1995a8248277SBarry Smith     Mat J,B;
1996a8248277SBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
1997a8248277SBarry Smith     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
1998a8248277SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
1999950540a4SJed Brown     ierr = DMCreateMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr);
2000a8248277SBarry Smith     ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,snes->funP);CHKERRQ(ierr);
2001a8248277SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
2002a8248277SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
2003efd51863SBarry Smith   } else if (snes->dm && !snes->jacobian_pre){
2004efd51863SBarry Smith     Mat J;
2005950540a4SJed Brown     ierr = DMCreateMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
20063cbb28f5SBarry Smith     ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
2007efd51863SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
2008ef8dffc7SBarry Smith   }
2009cfaf3a74SBarry Smith   if (!snes->ops->computefunction && !snes->dm) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a residual function with SNESSetFunction() or call SNESSetDM()");
2010c9b9fda1SJed Brown   if (!snes->vec_func) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a vector when calling SNESSetFunction() or call SNESSetDM()");
2011efd51863SBarry Smith 
2012b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
2013b710008aSBarry Smith 
2014d25893d9SBarry Smith   if (snes->ops->usercompute && !snes->user) {
2015d25893d9SBarry Smith     ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr);
2016d25893d9SBarry Smith   }
2017d25893d9SBarry Smith 
2018410397dcSLisandro Dalcin   if (snes->ops->setup) {
2019410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
2020410397dcSLisandro Dalcin   }
202158c9b817SLisandro Dalcin 
20227aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
20233a40ed3dSBarry Smith   PetscFunctionReturn(0);
20249b94acceSBarry Smith }
20259b94acceSBarry Smith 
20264a2ae208SSatish Balay #undef __FUNCT__
202737596af1SLisandro Dalcin #define __FUNCT__ "SNESReset"
202837596af1SLisandro Dalcin /*@
202937596af1SLisandro Dalcin    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats
203037596af1SLisandro Dalcin 
203137596af1SLisandro Dalcin    Collective on SNES
203237596af1SLisandro Dalcin 
203337596af1SLisandro Dalcin    Input Parameter:
203437596af1SLisandro Dalcin .  snes - iterative context obtained from SNESCreate()
203537596af1SLisandro Dalcin 
2036d25893d9SBarry Smith    Level: intermediate
2037d25893d9SBarry Smith 
2038d25893d9SBarry Smith    Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext()
203937596af1SLisandro Dalcin 
204037596af1SLisandro Dalcin .keywords: SNES, destroy
204137596af1SLisandro Dalcin 
204237596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
204337596af1SLisandro Dalcin @*/
204437596af1SLisandro Dalcin PetscErrorCode  SNESReset(SNES snes)
204537596af1SLisandro Dalcin {
204637596af1SLisandro Dalcin   PetscErrorCode ierr;
204737596af1SLisandro Dalcin 
204837596af1SLisandro Dalcin   PetscFunctionBegin;
204937596af1SLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2050d25893d9SBarry Smith   if (snes->ops->userdestroy && snes->user) {
2051d25893d9SBarry Smith     ierr       = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr);
2052d25893d9SBarry Smith     snes->user = PETSC_NULL;
2053d25893d9SBarry Smith   }
20548a23116dSBarry Smith   if (snes->pc) {
20558a23116dSBarry Smith     ierr = SNESReset(snes->pc);CHKERRQ(ierr);
20568a23116dSBarry Smith   }
20578a23116dSBarry Smith 
205837596af1SLisandro Dalcin   if (snes->ops->reset) {
205937596af1SLisandro Dalcin     ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr);
206037596af1SLisandro Dalcin   }
206137596af1SLisandro Dalcin   if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);}
20626bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
20636bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
20646bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr);
20656bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
20666bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
20676bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
206837596af1SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
206937596af1SLisandro Dalcin   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
207037596af1SLisandro Dalcin   snes->nwork = snes->nvwork = 0;
207137596af1SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
207237596af1SLisandro Dalcin   PetscFunctionReturn(0);
207337596af1SLisandro Dalcin }
207437596af1SLisandro Dalcin 
207537596af1SLisandro Dalcin #undef __FUNCT__
20764a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
207752baeb72SSatish Balay /*@
20789b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
20799b94acceSBarry Smith    with SNESCreate().
20809b94acceSBarry Smith 
2081c7afd0dbSLois Curfman McInnes    Collective on SNES
2082c7afd0dbSLois Curfman McInnes 
20839b94acceSBarry Smith    Input Parameter:
20849b94acceSBarry Smith .  snes - the SNES context
20859b94acceSBarry Smith 
208636851e7fSLois Curfman McInnes    Level: beginner
208736851e7fSLois Curfman McInnes 
20889b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
20899b94acceSBarry Smith 
209063a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
20919b94acceSBarry Smith @*/
20926bf464f9SBarry Smith PetscErrorCode  SNESDestroy(SNES *snes)
20939b94acceSBarry Smith {
20946849ba73SBarry Smith   PetscErrorCode ierr;
20953a40ed3dSBarry Smith 
20963a40ed3dSBarry Smith   PetscFunctionBegin;
20976bf464f9SBarry Smith   if (!*snes) PetscFunctionReturn(0);
20986bf464f9SBarry Smith   PetscValidHeaderSpecific((*snes),SNES_CLASSID,1);
20996bf464f9SBarry Smith   if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);}
2100d4bb536fSBarry Smith 
21016bf464f9SBarry Smith   ierr = SNESReset((*snes));CHKERRQ(ierr);
21028a23116dSBarry Smith   ierr = SNESDestroy(&(*snes)->pc);CHKERRQ(ierr);
21036b8b9a38SLisandro Dalcin 
2104be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
21056bf464f9SBarry Smith   ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr);
21066bf464f9SBarry Smith   if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);}
21076d4c513bSLisandro Dalcin 
21086bf464f9SBarry Smith   ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr);
21096bf464f9SBarry Smith   ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr);
21106b8b9a38SLisandro Dalcin 
21116bf464f9SBarry Smith   ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr);
21126bf464f9SBarry Smith   if ((*snes)->ops->convergeddestroy) {
21136bf464f9SBarry Smith     ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr);
21146b8b9a38SLisandro Dalcin   }
21156bf464f9SBarry Smith   if ((*snes)->conv_malloc) {
21166bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr);
21176bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr);
211858c9b817SLisandro Dalcin   }
2119ea630c6eSPeter Brune   ierr = PetscViewerDestroy(&(*snes)->ls_monitor);CHKERRQ(ierr);
21206bf464f9SBarry Smith   ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr);
2121a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
21223a40ed3dSBarry Smith  PetscFunctionReturn(0);
21239b94acceSBarry Smith }
21249b94acceSBarry Smith 
21259b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
21269b94acceSBarry Smith 
21274a2ae208SSatish Balay #undef __FUNCT__
2128a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
2129a8054027SBarry Smith /*@
2130a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
2131a8054027SBarry Smith 
21323f9fe445SBarry Smith    Logically Collective on SNES
2133a8054027SBarry Smith 
2134a8054027SBarry Smith    Input Parameters:
2135a8054027SBarry Smith +  snes - the SNES context
2136a8054027SBarry Smith -  lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
21373b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
2138a8054027SBarry Smith 
2139a8054027SBarry Smith    Options Database Keys:
2140a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
2141a8054027SBarry Smith 
2142a8054027SBarry Smith    Notes:
2143a8054027SBarry Smith    The default is 1
2144a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
2145a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
2146a8054027SBarry Smith 
2147a8054027SBarry Smith    Level: intermediate
2148a8054027SBarry Smith 
2149a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2150a8054027SBarry Smith 
2151e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
2152a8054027SBarry Smith 
2153a8054027SBarry Smith @*/
21547087cfbeSBarry Smith PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
2155a8054027SBarry Smith {
2156a8054027SBarry Smith   PetscFunctionBegin;
21570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2158e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
2159e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
2160c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
2161a8054027SBarry Smith   snes->lagpreconditioner = lag;
2162a8054027SBarry Smith   PetscFunctionReturn(0);
2163a8054027SBarry Smith }
2164a8054027SBarry Smith 
2165a8054027SBarry Smith #undef __FUNCT__
2166efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence"
2167efd51863SBarry Smith /*@
2168efd51863SBarry Smith    SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does
2169efd51863SBarry Smith 
2170efd51863SBarry Smith    Logically Collective on SNES
2171efd51863SBarry Smith 
2172efd51863SBarry Smith    Input Parameters:
2173efd51863SBarry Smith +  snes - the SNES context
2174efd51863SBarry Smith -  steps - the number of refinements to do, defaults to 0
2175efd51863SBarry Smith 
2176efd51863SBarry Smith    Options Database Keys:
2177efd51863SBarry Smith .    -snes_grid_sequence <steps>
2178efd51863SBarry Smith 
2179efd51863SBarry Smith    Level: intermediate
2180efd51863SBarry Smith 
2181c0df2a02SJed Brown    Notes:
2182c0df2a02SJed Brown    Use SNESGetSolution() to extract the fine grid solution after grid sequencing.
2183c0df2a02SJed Brown 
2184efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2185efd51863SBarry Smith 
2186efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
2187efd51863SBarry Smith 
2188efd51863SBarry Smith @*/
2189efd51863SBarry Smith PetscErrorCode  SNESSetGridSequence(SNES snes,PetscInt steps)
2190efd51863SBarry Smith {
2191efd51863SBarry Smith   PetscFunctionBegin;
2192efd51863SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2193efd51863SBarry Smith   PetscValidLogicalCollectiveInt(snes,steps,2);
2194efd51863SBarry Smith   snes->gridsequence = steps;
2195efd51863SBarry Smith   PetscFunctionReturn(0);
2196efd51863SBarry Smith }
2197efd51863SBarry Smith 
2198efd51863SBarry Smith #undef __FUNCT__
2199a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
2200a8054027SBarry Smith /*@
2201a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
2202a8054027SBarry Smith 
22033f9fe445SBarry Smith    Not Collective
2204a8054027SBarry Smith 
2205a8054027SBarry Smith    Input Parameter:
2206a8054027SBarry Smith .  snes - the SNES context
2207a8054027SBarry Smith 
2208a8054027SBarry Smith    Output Parameter:
2209a8054027SBarry Smith .   lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
22103b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
2211a8054027SBarry Smith 
2212a8054027SBarry Smith    Options Database Keys:
2213a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
2214a8054027SBarry Smith 
2215a8054027SBarry Smith    Notes:
2216a8054027SBarry Smith    The default is 1
2217a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
2218a8054027SBarry Smith 
2219a8054027SBarry Smith    Level: intermediate
2220a8054027SBarry Smith 
2221a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2222a8054027SBarry Smith 
2223a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
2224a8054027SBarry Smith 
2225a8054027SBarry Smith @*/
22267087cfbeSBarry Smith PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
2227a8054027SBarry Smith {
2228a8054027SBarry Smith   PetscFunctionBegin;
22290700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2230a8054027SBarry Smith   *lag = snes->lagpreconditioner;
2231a8054027SBarry Smith   PetscFunctionReturn(0);
2232a8054027SBarry Smith }
2233a8054027SBarry Smith 
2234a8054027SBarry Smith #undef __FUNCT__
2235e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
2236e35cf81dSBarry Smith /*@
2237e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
2238e35cf81dSBarry Smith      often the preconditioner is rebuilt.
2239e35cf81dSBarry Smith 
22403f9fe445SBarry Smith    Logically Collective on SNES
2241e35cf81dSBarry Smith 
2242e35cf81dSBarry Smith    Input Parameters:
2243e35cf81dSBarry Smith +  snes - the SNES context
2244e35cf81dSBarry Smith -  lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
2245fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
2246e35cf81dSBarry Smith 
2247e35cf81dSBarry Smith    Options Database Keys:
2248e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
2249e35cf81dSBarry Smith 
2250e35cf81dSBarry Smith    Notes:
2251e35cf81dSBarry Smith    The default is 1
2252e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
2253fe3ffe1eSBarry Smith    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
2254fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
2255e35cf81dSBarry Smith 
2256e35cf81dSBarry Smith    Level: intermediate
2257e35cf81dSBarry Smith 
2258e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2259e35cf81dSBarry Smith 
2260e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
2261e35cf81dSBarry Smith 
2262e35cf81dSBarry Smith @*/
22637087cfbeSBarry Smith PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
2264e35cf81dSBarry Smith {
2265e35cf81dSBarry Smith   PetscFunctionBegin;
22660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2267e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
2268e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
2269c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
2270e35cf81dSBarry Smith   snes->lagjacobian = lag;
2271e35cf81dSBarry Smith   PetscFunctionReturn(0);
2272e35cf81dSBarry Smith }
2273e35cf81dSBarry Smith 
2274e35cf81dSBarry Smith #undef __FUNCT__
2275e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
2276e35cf81dSBarry Smith /*@
2277e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
2278e35cf81dSBarry Smith 
22793f9fe445SBarry Smith    Not Collective
2280e35cf81dSBarry Smith 
2281e35cf81dSBarry Smith    Input Parameter:
2282e35cf81dSBarry Smith .  snes - the SNES context
2283e35cf81dSBarry Smith 
2284e35cf81dSBarry Smith    Output Parameter:
2285e35cf81dSBarry Smith .   lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
2286e35cf81dSBarry Smith          the Jacobian is built etc.
2287e35cf81dSBarry Smith 
2288e35cf81dSBarry Smith    Options Database Keys:
2289e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
2290e35cf81dSBarry Smith 
2291e35cf81dSBarry Smith    Notes:
2292e35cf81dSBarry Smith    The default is 1
2293e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
2294e35cf81dSBarry Smith 
2295e35cf81dSBarry Smith    Level: intermediate
2296e35cf81dSBarry Smith 
2297e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2298e35cf81dSBarry Smith 
2299e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
2300e35cf81dSBarry Smith 
2301e35cf81dSBarry Smith @*/
23027087cfbeSBarry Smith PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
2303e35cf81dSBarry Smith {
2304e35cf81dSBarry Smith   PetscFunctionBegin;
23050700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2306e35cf81dSBarry Smith   *lag = snes->lagjacobian;
2307e35cf81dSBarry Smith   PetscFunctionReturn(0);
2308e35cf81dSBarry Smith }
2309e35cf81dSBarry Smith 
2310e35cf81dSBarry Smith #undef __FUNCT__
23114a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
23129b94acceSBarry Smith /*@
2313d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
23149b94acceSBarry Smith 
23153f9fe445SBarry Smith    Logically Collective on SNES
2316c7afd0dbSLois Curfman McInnes 
23179b94acceSBarry Smith    Input Parameters:
2318c7afd0dbSLois Curfman McInnes +  snes - the SNES context
231970441072SBarry Smith .  abstol - absolute convergence tolerance
232033174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
232133174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
232233174efeSLois Curfman McInnes            of the change in the solution between steps
232333174efeSLois Curfman McInnes .  maxit - maximum number of iterations
2324c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
2325fee21e36SBarry Smith 
232633174efeSLois Curfman McInnes    Options Database Keys:
232770441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
2328c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
2329c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
2330c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
2331c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
23329b94acceSBarry Smith 
2333d7a720efSLois Curfman McInnes    Notes:
23349b94acceSBarry Smith    The default maximum number of iterations is 50.
23359b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
23369b94acceSBarry Smith 
233736851e7fSLois Curfman McInnes    Level: intermediate
233836851e7fSLois Curfman McInnes 
233933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
23409b94acceSBarry Smith 
23412492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
23429b94acceSBarry Smith @*/
23437087cfbeSBarry Smith PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
23449b94acceSBarry Smith {
23453a40ed3dSBarry Smith   PetscFunctionBegin;
23460700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2347c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
2348c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
2349c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
2350c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
2351c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
2352c5eb9154SBarry Smith 
2353ab54825eSJed Brown   if (abstol != PETSC_DEFAULT) {
2354ab54825eSJed Brown     if (abstol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %G must be non-negative",abstol);
2355ab54825eSJed Brown     snes->abstol = abstol;
2356ab54825eSJed Brown   }
2357ab54825eSJed Brown   if (rtol != PETSC_DEFAULT) {
2358ab54825eSJed Brown     if (rtol < 0.0 || 1.0 <= rtol) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Relative tolerance %G must be non-negative and less than 1.0",rtol);
2359ab54825eSJed Brown     snes->rtol = rtol;
2360ab54825eSJed Brown   }
2361ab54825eSJed Brown   if (stol != PETSC_DEFAULT) {
2362ab54825eSJed Brown     if (stol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %G must be non-negative",stol);
2363ab54825eSJed Brown     snes->xtol = stol;
2364ab54825eSJed Brown   }
2365ab54825eSJed Brown   if (maxit != PETSC_DEFAULT) {
2366ab54825eSJed Brown     if (maxit < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit);
2367ab54825eSJed Brown     snes->max_its = maxit;
2368ab54825eSJed Brown   }
2369ab54825eSJed Brown   if (maxf != PETSC_DEFAULT) {
2370ab54825eSJed Brown     if (maxf < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be non-negative",maxf);
2371ab54825eSJed Brown     snes->max_funcs = maxf;
2372ab54825eSJed Brown   }
23733a40ed3dSBarry Smith   PetscFunctionReturn(0);
23749b94acceSBarry Smith }
23759b94acceSBarry Smith 
23764a2ae208SSatish Balay #undef __FUNCT__
23774a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
23789b94acceSBarry Smith /*@
237933174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
238033174efeSLois Curfman McInnes 
2381c7afd0dbSLois Curfman McInnes    Not Collective
2382c7afd0dbSLois Curfman McInnes 
238333174efeSLois Curfman McInnes    Input Parameters:
2384c7afd0dbSLois Curfman McInnes +  snes - the SNES context
238585385478SLisandro Dalcin .  atol - absolute convergence tolerance
238633174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
238733174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
238833174efeSLois Curfman McInnes            of the change in the solution between steps
238933174efeSLois Curfman McInnes .  maxit - maximum number of iterations
2390c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
2391fee21e36SBarry Smith 
239233174efeSLois Curfman McInnes    Notes:
239333174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
239433174efeSLois Curfman McInnes 
239536851e7fSLois Curfman McInnes    Level: intermediate
239636851e7fSLois Curfman McInnes 
239733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
239833174efeSLois Curfman McInnes 
239933174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
240033174efeSLois Curfman McInnes @*/
24017087cfbeSBarry Smith PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
240233174efeSLois Curfman McInnes {
24033a40ed3dSBarry Smith   PetscFunctionBegin;
24040700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
240585385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
240633174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
240733174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
240833174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
240933174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
24103a40ed3dSBarry Smith   PetscFunctionReturn(0);
241133174efeSLois Curfman McInnes }
241233174efeSLois Curfman McInnes 
24134a2ae208SSatish Balay #undef __FUNCT__
24144a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
241533174efeSLois Curfman McInnes /*@
24169b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
24179b94acceSBarry Smith 
24183f9fe445SBarry Smith    Logically Collective on SNES
2419fee21e36SBarry Smith 
2420c7afd0dbSLois Curfman McInnes    Input Parameters:
2421c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2422c7afd0dbSLois Curfman McInnes -  tol - tolerance
2423c7afd0dbSLois Curfman McInnes 
24249b94acceSBarry Smith    Options Database Key:
2425c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
24269b94acceSBarry Smith 
242736851e7fSLois Curfman McInnes    Level: intermediate
242836851e7fSLois Curfman McInnes 
24299b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
24309b94acceSBarry Smith 
24312492ecdbSBarry Smith .seealso: SNESSetTolerances()
24329b94acceSBarry Smith @*/
24337087cfbeSBarry Smith PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
24349b94acceSBarry Smith {
24353a40ed3dSBarry Smith   PetscFunctionBegin;
24360700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2437c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
24389b94acceSBarry Smith   snes->deltatol = tol;
24393a40ed3dSBarry Smith   PetscFunctionReturn(0);
24409b94acceSBarry Smith }
24419b94acceSBarry Smith 
2442df9fa365SBarry Smith /*
2443df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
2444df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
2445df9fa365SBarry Smith    macros instead of functions
2446df9fa365SBarry Smith */
24474a2ae208SSatish Balay #undef __FUNCT__
2448a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
24497087cfbeSBarry Smith PetscErrorCode  SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
2450ce1608b8SBarry Smith {
2451dfbe8321SBarry Smith   PetscErrorCode ierr;
2452ce1608b8SBarry Smith 
2453ce1608b8SBarry Smith   PetscFunctionBegin;
24540700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2455a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
2456ce1608b8SBarry Smith   PetscFunctionReturn(0);
2457ce1608b8SBarry Smith }
2458ce1608b8SBarry Smith 
24594a2ae208SSatish Balay #undef __FUNCT__
2460a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
24617087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2462df9fa365SBarry Smith {
2463dfbe8321SBarry Smith   PetscErrorCode ierr;
2464df9fa365SBarry Smith 
2465df9fa365SBarry Smith   PetscFunctionBegin;
2466a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2467df9fa365SBarry Smith   PetscFunctionReturn(0);
2468df9fa365SBarry Smith }
2469df9fa365SBarry Smith 
24704a2ae208SSatish Balay #undef __FUNCT__
2471a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
24726bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGDestroy(PetscDrawLG *draw)
2473df9fa365SBarry Smith {
2474dfbe8321SBarry Smith   PetscErrorCode ierr;
2475df9fa365SBarry Smith 
2476df9fa365SBarry Smith   PetscFunctionBegin;
2477a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2478df9fa365SBarry Smith   PetscFunctionReturn(0);
2479df9fa365SBarry Smith }
2480df9fa365SBarry Smith 
24817087cfbeSBarry Smith extern PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
2482b271bb04SBarry Smith #undef __FUNCT__
2483b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
24847087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
2485b271bb04SBarry Smith {
2486b271bb04SBarry Smith   PetscDrawLG      lg;
2487b271bb04SBarry Smith   PetscErrorCode   ierr;
2488b271bb04SBarry Smith   PetscReal        x,y,per;
2489b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
2490b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
2491b271bb04SBarry Smith   PetscDraw        draw;
2492b271bb04SBarry Smith   PetscFunctionBegin;
2493b271bb04SBarry Smith   if (!monctx) {
2494b271bb04SBarry Smith     MPI_Comm    comm;
2495b271bb04SBarry Smith 
2496b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
2497b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
2498b271bb04SBarry Smith   }
2499b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
2500b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2501b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2502b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
2503b271bb04SBarry Smith   x = (PetscReal) n;
2504b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
2505b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2506b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2507b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2508b271bb04SBarry Smith   }
2509b271bb04SBarry Smith 
2510b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
2511b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2512b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2513b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
2514b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
2515b271bb04SBarry Smith   x = (PetscReal) n;
2516b271bb04SBarry Smith   y = 100.0*per;
2517b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2518b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2519b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2520b271bb04SBarry Smith   }
2521b271bb04SBarry Smith 
2522b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
2523b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2524b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2525b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
2526b271bb04SBarry Smith   x = (PetscReal) n;
2527b271bb04SBarry Smith   y = (prev - rnorm)/prev;
2528b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2529b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2530b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2531b271bb04SBarry Smith   }
2532b271bb04SBarry Smith 
2533b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
2534b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2535b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2536b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
2537b271bb04SBarry Smith   x = (PetscReal) n;
2538b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
2539b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
2540b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2541b271bb04SBarry Smith   }
2542b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2543b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2544b271bb04SBarry Smith   }
2545b271bb04SBarry Smith   prev = rnorm;
2546b271bb04SBarry Smith   PetscFunctionReturn(0);
2547b271bb04SBarry Smith }
2548b271bb04SBarry Smith 
2549b271bb04SBarry Smith #undef __FUNCT__
2550b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
25517087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2552b271bb04SBarry Smith {
2553b271bb04SBarry Smith   PetscErrorCode ierr;
2554b271bb04SBarry Smith 
2555b271bb04SBarry Smith   PetscFunctionBegin;
2556b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2557b271bb04SBarry Smith   PetscFunctionReturn(0);
2558b271bb04SBarry Smith }
2559b271bb04SBarry Smith 
2560b271bb04SBarry Smith #undef __FUNCT__
2561b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
25626bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGRangeDestroy(PetscDrawLG *draw)
2563b271bb04SBarry Smith {
2564b271bb04SBarry Smith   PetscErrorCode ierr;
2565b271bb04SBarry Smith 
2566b271bb04SBarry Smith   PetscFunctionBegin;
2567b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2568b271bb04SBarry Smith   PetscFunctionReturn(0);
2569b271bb04SBarry Smith }
2570b271bb04SBarry Smith 
25717a03ce2fSLisandro Dalcin #undef __FUNCT__
25727a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor"
2573228d79bcSJed Brown /*@
2574228d79bcSJed Brown    SNESMonitor - runs the user provided monitor routines, if they exist
2575228d79bcSJed Brown 
2576228d79bcSJed Brown    Collective on SNES
2577228d79bcSJed Brown 
2578228d79bcSJed Brown    Input Parameters:
2579228d79bcSJed Brown +  snes - nonlinear solver context obtained from SNESCreate()
2580228d79bcSJed Brown .  iter - iteration number
2581228d79bcSJed Brown -  rnorm - relative norm of the residual
2582228d79bcSJed Brown 
2583228d79bcSJed Brown    Notes:
2584228d79bcSJed Brown    This routine is called by the SNES implementations.
2585228d79bcSJed Brown    It does not typically need to be called by the user.
2586228d79bcSJed Brown 
2587228d79bcSJed Brown    Level: developer
2588228d79bcSJed Brown 
2589228d79bcSJed Brown .seealso: SNESMonitorSet()
2590228d79bcSJed Brown @*/
25917a03ce2fSLisandro Dalcin PetscErrorCode  SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm)
25927a03ce2fSLisandro Dalcin {
25937a03ce2fSLisandro Dalcin   PetscErrorCode ierr;
25947a03ce2fSLisandro Dalcin   PetscInt       i,n = snes->numbermonitors;
25957a03ce2fSLisandro Dalcin 
25967a03ce2fSLisandro Dalcin   PetscFunctionBegin;
25977a03ce2fSLisandro Dalcin   for (i=0; i<n; i++) {
25987a03ce2fSLisandro Dalcin     ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr);
25997a03ce2fSLisandro Dalcin   }
26007a03ce2fSLisandro Dalcin   PetscFunctionReturn(0);
26017a03ce2fSLisandro Dalcin }
26027a03ce2fSLisandro Dalcin 
26039b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
26049b94acceSBarry Smith 
26054a2ae208SSatish Balay #undef __FUNCT__
2606a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
26079b94acceSBarry Smith /*@C
2608a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
26099b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
26109b94acceSBarry Smith    progress.
26119b94acceSBarry Smith 
26123f9fe445SBarry Smith    Logically Collective on SNES
2613fee21e36SBarry Smith 
2614c7afd0dbSLois Curfman McInnes    Input Parameters:
2615c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2616c7afd0dbSLois Curfman McInnes .  func - monitoring routine
2617b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
2618e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
2619b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2620b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
26219b94acceSBarry Smith 
2622c7afd0dbSLois Curfman McInnes    Calling sequence of func:
2623a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
2624c7afd0dbSLois Curfman McInnes 
2625c7afd0dbSLois Curfman McInnes +    snes - the SNES context
2626c7afd0dbSLois Curfman McInnes .    its - iteration number
2627c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
262840a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
26299b94acceSBarry Smith 
26309665c990SLois Curfman McInnes    Options Database Keys:
2631a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
2632a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
2633a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
2634cca6129bSJed Brown -    -snes_monitor_cancel - cancels all monitors that have
2635c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
2636a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
2637c7afd0dbSLois Curfman McInnes                             does not cancel those set via
2638c7afd0dbSLois Curfman McInnes                             the options database.
26399665c990SLois Curfman McInnes 
2640639f9d9dSBarry Smith    Notes:
26416bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
2642a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
26436bc08f3fSLois Curfman McInnes    order in which they were set.
2644639f9d9dSBarry Smith 
2645025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
2646025f1a04SBarry Smith 
264736851e7fSLois Curfman McInnes    Level: intermediate
264836851e7fSLois Curfman McInnes 
26499b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
26509b94acceSBarry Smith 
2651a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
26529b94acceSBarry Smith @*/
2653c2efdce3SBarry Smith PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
26549b94acceSBarry Smith {
2655b90d0a6eSBarry Smith   PetscInt       i;
2656649052a6SBarry Smith   PetscErrorCode ierr;
2657b90d0a6eSBarry Smith 
26583a40ed3dSBarry Smith   PetscFunctionBegin;
26590700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
266017186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2661b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
2662649052a6SBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) {
2663649052a6SBarry Smith       if (monitordestroy) {
2664c2efdce3SBarry Smith         ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr);
2665649052a6SBarry Smith       }
2666b90d0a6eSBarry Smith       PetscFunctionReturn(0);
2667b90d0a6eSBarry Smith     }
2668b90d0a6eSBarry Smith   }
2669b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
2670b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
2671639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
26723a40ed3dSBarry Smith   PetscFunctionReturn(0);
26739b94acceSBarry Smith }
26749b94acceSBarry Smith 
26754a2ae208SSatish Balay #undef __FUNCT__
2676a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
26775cd90555SBarry Smith /*@C
2678a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
26795cd90555SBarry Smith 
26803f9fe445SBarry Smith    Logically Collective on SNES
2681c7afd0dbSLois Curfman McInnes 
26825cd90555SBarry Smith    Input Parameters:
26835cd90555SBarry Smith .  snes - the SNES context
26845cd90555SBarry Smith 
26851a480d89SAdministrator    Options Database Key:
2686a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
2687a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
2688c7afd0dbSLois Curfman McInnes     set via the options database
26895cd90555SBarry Smith 
26905cd90555SBarry Smith    Notes:
26915cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
26925cd90555SBarry Smith 
269336851e7fSLois Curfman McInnes    Level: intermediate
269436851e7fSLois Curfman McInnes 
26955cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
26965cd90555SBarry Smith 
2697a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
26985cd90555SBarry Smith @*/
26997087cfbeSBarry Smith PetscErrorCode  SNESMonitorCancel(SNES snes)
27005cd90555SBarry Smith {
2701d952e501SBarry Smith   PetscErrorCode ierr;
2702d952e501SBarry Smith   PetscInt       i;
2703d952e501SBarry Smith 
27045cd90555SBarry Smith   PetscFunctionBegin;
27050700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2706d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2707d952e501SBarry Smith     if (snes->monitordestroy[i]) {
27083c4aec1bSBarry Smith       ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr);
2709d952e501SBarry Smith     }
2710d952e501SBarry Smith   }
27115cd90555SBarry Smith   snes->numbermonitors = 0;
27125cd90555SBarry Smith   PetscFunctionReturn(0);
27135cd90555SBarry Smith }
27145cd90555SBarry Smith 
27154a2ae208SSatish Balay #undef __FUNCT__
27164a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
27179b94acceSBarry Smith /*@C
27189b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
27199b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
27209b94acceSBarry Smith 
27213f9fe445SBarry Smith    Logically Collective on SNES
2722fee21e36SBarry Smith 
2723c7afd0dbSLois Curfman McInnes    Input Parameters:
2724c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2725c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
27267f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
27277f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
27289b94acceSBarry Smith 
2729c7afd0dbSLois Curfman McInnes    Calling sequence of func:
273006ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2731c7afd0dbSLois Curfman McInnes 
2732c7afd0dbSLois Curfman McInnes +    snes - the SNES context
273306ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2734c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2735184914b5SBarry Smith .    reason - reason for convergence/divergence
2736c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
27374b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
27384b27c08aSLois Curfman McInnes -    f - 2-norm of function
27399b94acceSBarry Smith 
274036851e7fSLois Curfman McInnes    Level: advanced
274136851e7fSLois Curfman McInnes 
27429b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
27439b94acceSBarry Smith 
274485385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
27459b94acceSBarry Smith @*/
27467087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
27479b94acceSBarry Smith {
27487f7931b9SBarry Smith   PetscErrorCode ierr;
27497f7931b9SBarry Smith 
27503a40ed3dSBarry Smith   PetscFunctionBegin;
27510700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
275285385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
27537f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
27547f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
27557f7931b9SBarry Smith   }
275685385478SLisandro Dalcin   snes->ops->converged        = func;
27577f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
275885385478SLisandro Dalcin   snes->cnvP                  = cctx;
27593a40ed3dSBarry Smith   PetscFunctionReturn(0);
27609b94acceSBarry Smith }
27619b94acceSBarry Smith 
27624a2ae208SSatish Balay #undef __FUNCT__
27634a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
276452baeb72SSatish Balay /*@
2765184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2766184914b5SBarry Smith 
2767184914b5SBarry Smith    Not Collective
2768184914b5SBarry Smith 
2769184914b5SBarry Smith    Input Parameter:
2770184914b5SBarry Smith .  snes - the SNES context
2771184914b5SBarry Smith 
2772184914b5SBarry Smith    Output Parameter:
27734d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2774184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2775184914b5SBarry Smith 
2776184914b5SBarry Smith    Level: intermediate
2777184914b5SBarry Smith 
2778184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2779184914b5SBarry Smith 
2780184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2781184914b5SBarry Smith 
278285385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2783184914b5SBarry Smith @*/
27847087cfbeSBarry Smith PetscErrorCode  SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2785184914b5SBarry Smith {
2786184914b5SBarry Smith   PetscFunctionBegin;
27870700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27884482741eSBarry Smith   PetscValidPointer(reason,2);
2789184914b5SBarry Smith   *reason = snes->reason;
2790184914b5SBarry Smith   PetscFunctionReturn(0);
2791184914b5SBarry Smith }
2792184914b5SBarry Smith 
27934a2ae208SSatish Balay #undef __FUNCT__
27944a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2795c9005455SLois Curfman McInnes /*@
2796c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2797c9005455SLois Curfman McInnes 
27983f9fe445SBarry Smith    Logically Collective on SNES
2799fee21e36SBarry Smith 
2800c7afd0dbSLois Curfman McInnes    Input Parameters:
2801c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
28028c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2803cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2804758f92a0SBarry Smith .  na  - size of a and its
280564731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2806758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2807c7afd0dbSLois Curfman McInnes 
2808308dcc3eSBarry Smith    Notes:
2809308dcc3eSBarry Smith    If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2810308dcc3eSBarry Smith    default array of length 10000 is allocated.
2811308dcc3eSBarry Smith 
2812c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2813c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2814c9005455SLois Curfman McInnes    during the section of code that is being timed.
2815c9005455SLois Curfman McInnes 
281636851e7fSLois Curfman McInnes    Level: intermediate
281736851e7fSLois Curfman McInnes 
2818c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2819758f92a0SBarry Smith 
282008405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2821758f92a0SBarry Smith 
2822c9005455SLois Curfman McInnes @*/
28237087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool  reset)
2824c9005455SLois Curfman McInnes {
2825308dcc3eSBarry Smith   PetscErrorCode ierr;
2826308dcc3eSBarry Smith 
28273a40ed3dSBarry Smith   PetscFunctionBegin;
28280700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
28294482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2830a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2831308dcc3eSBarry Smith   if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) {
2832308dcc3eSBarry Smith     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2833308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr);
2834308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr);
2835308dcc3eSBarry Smith     snes->conv_malloc   = PETSC_TRUE;
2836308dcc3eSBarry Smith   }
2837c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2838758f92a0SBarry Smith   snes->conv_hist_its   = its;
2839758f92a0SBarry Smith   snes->conv_hist_max   = na;
2840a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2841758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2842758f92a0SBarry Smith   PetscFunctionReturn(0);
2843758f92a0SBarry Smith }
2844758f92a0SBarry Smith 
2845308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2846c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
2847c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
2848308dcc3eSBarry Smith EXTERN_C_BEGIN
2849308dcc3eSBarry Smith #undef __FUNCT__
2850308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab"
2851308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
2852308dcc3eSBarry Smith {
2853308dcc3eSBarry Smith   mxArray        *mat;
2854308dcc3eSBarry Smith   PetscInt       i;
2855308dcc3eSBarry Smith   PetscReal      *ar;
2856308dcc3eSBarry Smith 
2857308dcc3eSBarry Smith   PetscFunctionBegin;
2858308dcc3eSBarry Smith   mat  = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
2859308dcc3eSBarry Smith   ar   = (PetscReal*) mxGetData(mat);
2860308dcc3eSBarry Smith   for (i=0; i<snes->conv_hist_len; i++) {
2861308dcc3eSBarry Smith     ar[i] = snes->conv_hist[i];
2862308dcc3eSBarry Smith   }
2863308dcc3eSBarry Smith   PetscFunctionReturn(mat);
2864308dcc3eSBarry Smith }
2865308dcc3eSBarry Smith EXTERN_C_END
2866308dcc3eSBarry Smith #endif
2867308dcc3eSBarry Smith 
2868308dcc3eSBarry Smith 
28694a2ae208SSatish Balay #undef __FUNCT__
28704a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
28710c4c9dddSBarry Smith /*@C
2872758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2873758f92a0SBarry Smith 
28743f9fe445SBarry Smith    Not Collective
2875758f92a0SBarry Smith 
2876758f92a0SBarry Smith    Input Parameter:
2877758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2878758f92a0SBarry Smith 
2879758f92a0SBarry Smith    Output Parameters:
2880758f92a0SBarry Smith .  a   - array to hold history
2881758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2882758f92a0SBarry Smith          negative if not converged) for each solve.
2883758f92a0SBarry Smith -  na  - size of a and its
2884758f92a0SBarry Smith 
2885758f92a0SBarry Smith    Notes:
2886758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2887758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2888758f92a0SBarry Smith 
2889758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2890758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2891758f92a0SBarry Smith    during the section of code that is being timed.
2892758f92a0SBarry Smith 
2893758f92a0SBarry Smith    Level: intermediate
2894758f92a0SBarry Smith 
2895758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2896758f92a0SBarry Smith 
2897758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2898758f92a0SBarry Smith 
2899758f92a0SBarry Smith @*/
29007087cfbeSBarry Smith PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2901758f92a0SBarry Smith {
2902758f92a0SBarry Smith   PetscFunctionBegin;
29030700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2904758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2905758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2906758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
29073a40ed3dSBarry Smith   PetscFunctionReturn(0);
2908c9005455SLois Curfman McInnes }
2909c9005455SLois Curfman McInnes 
2910e74ef692SMatthew Knepley #undef __FUNCT__
2911e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2912ac226902SBarry Smith /*@C
291376b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
2914eec8f646SJed Brown   at the beginning of every iteration of the nonlinear solve. Specifically
29157e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
291676b2cf59SMatthew Knepley 
29173f9fe445SBarry Smith   Logically Collective on SNES
291876b2cf59SMatthew Knepley 
291976b2cf59SMatthew Knepley   Input Parameters:
292076b2cf59SMatthew Knepley . snes - The nonlinear solver context
292176b2cf59SMatthew Knepley . func - The function
292276b2cf59SMatthew Knepley 
292376b2cf59SMatthew Knepley   Calling sequence of func:
2924b5d30489SBarry Smith . func (SNES snes, PetscInt step);
292576b2cf59SMatthew Knepley 
292676b2cf59SMatthew Knepley . step - The current step of the iteration
292776b2cf59SMatthew Knepley 
2928fe97e370SBarry Smith   Level: advanced
2929fe97e370SBarry Smith 
2930fe97e370SBarry Smith   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()
2931fe97e370SBarry Smith         This is not used by most users.
293276b2cf59SMatthew Knepley 
293376b2cf59SMatthew Knepley .keywords: SNES, update
2934b5d30489SBarry Smith 
293585385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
293676b2cf59SMatthew Knepley @*/
29377087cfbeSBarry Smith PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
293876b2cf59SMatthew Knepley {
293976b2cf59SMatthew Knepley   PetscFunctionBegin;
29400700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2941e7788613SBarry Smith   snes->ops->update = func;
294276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
294376b2cf59SMatthew Knepley }
294476b2cf59SMatthew Knepley 
2945e74ef692SMatthew Knepley #undef __FUNCT__
2946e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
294776b2cf59SMatthew Knepley /*@
294876b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
294976b2cf59SMatthew Knepley 
295076b2cf59SMatthew Knepley   Not collective
295176b2cf59SMatthew Knepley 
295276b2cf59SMatthew Knepley   Input Parameters:
295376b2cf59SMatthew Knepley . snes - The nonlinear solver context
295476b2cf59SMatthew Knepley . step - The current step of the iteration
295576b2cf59SMatthew Knepley 
2956205452f4SMatthew Knepley   Level: intermediate
2957205452f4SMatthew Knepley 
295876b2cf59SMatthew Knepley .keywords: SNES, update
2959a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
296076b2cf59SMatthew Knepley @*/
29617087cfbeSBarry Smith PetscErrorCode  SNESDefaultUpdate(SNES snes, PetscInt step)
296276b2cf59SMatthew Knepley {
296376b2cf59SMatthew Knepley   PetscFunctionBegin;
296476b2cf59SMatthew Knepley   PetscFunctionReturn(0);
296576b2cf59SMatthew Knepley }
296676b2cf59SMatthew Knepley 
29674a2ae208SSatish Balay #undef __FUNCT__
29684a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
29699b94acceSBarry Smith /*
29709b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
29719b94acceSBarry Smith    positive parameter delta.
29729b94acceSBarry Smith 
29739b94acceSBarry Smith     Input Parameters:
2974c7afd0dbSLois Curfman McInnes +   snes - the SNES context
29759b94acceSBarry Smith .   y - approximate solution of linear system
29769b94acceSBarry Smith .   fnorm - 2-norm of current function
2977c7afd0dbSLois Curfman McInnes -   delta - trust region size
29789b94acceSBarry Smith 
29799b94acceSBarry Smith     Output Parameters:
2980c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
29819b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
29829b94acceSBarry Smith     region, and exceeds zero otherwise.
2983c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
29849b94acceSBarry Smith 
29859b94acceSBarry Smith     Note:
29864b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
29879b94acceSBarry Smith     is set to be the maximum allowable step size.
29889b94acceSBarry Smith 
29899b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
29909b94acceSBarry Smith */
2991dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
29929b94acceSBarry Smith {
2993064f8208SBarry Smith   PetscReal      nrm;
2994ea709b57SSatish Balay   PetscScalar    cnorm;
2995dfbe8321SBarry Smith   PetscErrorCode ierr;
29963a40ed3dSBarry Smith 
29973a40ed3dSBarry Smith   PetscFunctionBegin;
29980700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
29990700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
3000c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
3001184914b5SBarry Smith 
3002064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
3003064f8208SBarry Smith   if (nrm > *delta) {
3004064f8208SBarry Smith      nrm = *delta/nrm;
3005064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
3006064f8208SBarry Smith      cnorm = nrm;
30072dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
30089b94acceSBarry Smith      *ynorm = *delta;
30099b94acceSBarry Smith   } else {
30109b94acceSBarry Smith      *gpnorm = 0.0;
3011064f8208SBarry Smith      *ynorm = nrm;
30129b94acceSBarry Smith   }
30133a40ed3dSBarry Smith   PetscFunctionReturn(0);
30149b94acceSBarry Smith }
30159b94acceSBarry Smith 
30164a2ae208SSatish Balay #undef __FUNCT__
30174a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
30186ce558aeSBarry Smith /*@C
3019f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
3020f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
30219b94acceSBarry Smith 
3022c7afd0dbSLois Curfman McInnes    Collective on SNES
3023c7afd0dbSLois Curfman McInnes 
3024b2002411SLois Curfman McInnes    Input Parameters:
3025c7afd0dbSLois Curfman McInnes +  snes - the SNES context
30263cebf274SJed Brown .  b - the constant part of the equation F(x) = b, or PETSC_NULL to use zero.
302785385478SLisandro Dalcin -  x - the solution vector.
30289b94acceSBarry Smith 
3029b2002411SLois Curfman McInnes    Notes:
30308ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
30318ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
30328ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
30338ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
30348ddd3da0SLois Curfman McInnes 
303536851e7fSLois Curfman McInnes    Level: beginner
303636851e7fSLois Curfman McInnes 
30379b94acceSBarry Smith .keywords: SNES, nonlinear, solve
30389b94acceSBarry Smith 
3039c0df2a02SJed Brown .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetGridSequence(), SNESGetSolution()
30409b94acceSBarry Smith @*/
30417087cfbeSBarry Smith PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
30429b94acceSBarry Smith {
3043dfbe8321SBarry Smith   PetscErrorCode ierr;
3044ace3abfcSBarry Smith   PetscBool      flg;
3045eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
3046eabae89aSBarry Smith   PetscViewer    viewer;
3047efd51863SBarry Smith   PetscInt       grid;
3048a69afd8bSBarry Smith   Vec            xcreated = PETSC_NULL;
3049052efed2SBarry Smith 
30503a40ed3dSBarry Smith   PetscFunctionBegin;
30510700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3052a69afd8bSBarry Smith   if (x) PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3053a69afd8bSBarry Smith   if (x) PetscCheckSameComm(snes,1,x,3);
30540700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
305585385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
305685385478SLisandro Dalcin 
3057a69afd8bSBarry Smith   if (!x && snes->dm) {
3058a69afd8bSBarry Smith     ierr = DMCreateGlobalVector(snes->dm,&xcreated);CHKERRQ(ierr);
3059a69afd8bSBarry Smith     x    = xcreated;
3060a69afd8bSBarry Smith   }
3061a69afd8bSBarry Smith 
3062a8248277SBarry Smith   for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);}
3063efd51863SBarry Smith   for (grid=0; grid<snes->gridsequence+1; grid++) {
3064efd51863SBarry Smith 
306585385478SLisandro Dalcin     /* set solution vector */
3066efd51863SBarry Smith     if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);}
30676bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
306885385478SLisandro Dalcin     snes->vec_sol = x;
306985385478SLisandro Dalcin     /* set afine vector if provided */
307085385478SLisandro Dalcin     if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
30716bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
307285385478SLisandro Dalcin     snes->vec_rhs = b;
307385385478SLisandro Dalcin 
307470e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
30753f149594SLisandro Dalcin 
30767eee914bSBarry Smith     if (!grid) {
30777eee914bSBarry Smith       if (snes->ops->computeinitialguess) {
3078d25893d9SBarry Smith         ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr);
3079dd568438SSatish Balay       } else if (snes->dm) {
3080dd568438SSatish Balay         PetscBool ig;
3081dd568438SSatish Balay         ierr = DMHasInitialGuess(snes->dm,&ig);CHKERRQ(ierr);
3082dd568438SSatish Balay         if (ig) {
30837eee914bSBarry Smith           ierr = DMComputeInitialGuess(snes->dm,snes->vec_sol);CHKERRQ(ierr);
30847eee914bSBarry Smith         }
3085d25893d9SBarry Smith       }
3086dd568438SSatish Balay     }
3087d25893d9SBarry Smith 
3088abc0a331SBarry Smith     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
308950ffb88aSMatthew Knepley     snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
3090d5e45103SBarry Smith 
30913f149594SLisandro Dalcin     ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
30924936397dSBarry Smith     ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
309385385478SLisandro Dalcin     ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
30944936397dSBarry Smith     if (snes->domainerror){
30954936397dSBarry Smith       snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
30964936397dSBarry Smith       snes->domainerror = PETSC_FALSE;
30974936397dSBarry Smith     }
309817186662SBarry Smith     if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
30993f149594SLisandro Dalcin 
31007adad957SLisandro Dalcin     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
3101eabae89aSBarry Smith     if (flg && !PetscPreLoadingOn) {
31027adad957SLisandro Dalcin       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
3103eabae89aSBarry Smith       ierr = SNESView(snes,viewer);CHKERRQ(ierr);
31046bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3105eabae89aSBarry Smith     }
3106eabae89aSBarry Smith 
310790d69ab7SBarry Smith     flg  = PETSC_FALSE;
3108acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
3109da9b6338SBarry Smith     if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
31105968eb51SBarry Smith     if (snes->printreason) {
3111a8248277SBarry Smith       ierr = PetscViewerASCIIAddTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr);
31125968eb51SBarry Smith       if (snes->reason > 0) {
3113a8248277SBarry Smith         ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
31145968eb51SBarry Smith       } else {
3115a8248277SBarry Smith         ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
31165968eb51SBarry Smith       }
3117a8248277SBarry Smith       ierr = PetscViewerASCIISubtractTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr);
31185968eb51SBarry Smith     }
31195968eb51SBarry Smith 
31208501fc72SJed Brown     flg = PETSC_FALSE;
31218501fc72SJed Brown     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view_solution_vtk",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
31228501fc72SJed Brown     if (flg) {
31238501fc72SJed Brown       PetscViewer viewer;
31248501fc72SJed Brown       ierr = PetscViewerCreate(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
31258501fc72SJed Brown       ierr = PetscViewerSetType(viewer,PETSCVIEWERVTK);CHKERRQ(ierr);
31268501fc72SJed Brown       ierr = PetscViewerFileSetName(viewer,filename);CHKERRQ(ierr);
31278501fc72SJed Brown       ierr = VecView(snes->vec_sol,viewer);CHKERRQ(ierr);
31288501fc72SJed Brown       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
31298501fc72SJed Brown     }
31308501fc72SJed Brown 
3131e113a28aSBarry Smith     if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
3132efd51863SBarry Smith     if (grid <  snes->gridsequence) {
3133efd51863SBarry Smith       DM  fine;
3134efd51863SBarry Smith       Vec xnew;
3135efd51863SBarry Smith       Mat interp;
3136efd51863SBarry Smith 
3137efd51863SBarry Smith       ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr);
3138e727c939SJed Brown       ierr = DMCreateInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr);
3139efd51863SBarry Smith       ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr);
3140efd51863SBarry Smith       ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr);
3141efd51863SBarry Smith       ierr = MatDestroy(&interp);CHKERRQ(ierr);
3142efd51863SBarry Smith       x    = xnew;
3143efd51863SBarry Smith 
3144efd51863SBarry Smith       ierr = SNESReset(snes);CHKERRQ(ierr);
3145efd51863SBarry Smith       ierr = SNESSetDM(snes,fine);CHKERRQ(ierr);
3146efd51863SBarry Smith       ierr = DMDestroy(&fine);CHKERRQ(ierr);
3147a8248277SBarry Smith       ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);
3148efd51863SBarry Smith     }
3149efd51863SBarry Smith   }
3150a69afd8bSBarry Smith   ierr = VecDestroy(&xcreated);CHKERRQ(ierr);
31513a40ed3dSBarry Smith   PetscFunctionReturn(0);
31529b94acceSBarry Smith }
31539b94acceSBarry Smith 
31549b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
31559b94acceSBarry Smith 
31564a2ae208SSatish Balay #undef __FUNCT__
31574a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
315882bf6240SBarry Smith /*@C
31594b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
31609b94acceSBarry Smith 
3161fee21e36SBarry Smith    Collective on SNES
3162fee21e36SBarry Smith 
3163c7afd0dbSLois Curfman McInnes    Input Parameters:
3164c7afd0dbSLois Curfman McInnes +  snes - the SNES context
3165454a90a3SBarry Smith -  type - a known method
3166c7afd0dbSLois Curfman McInnes 
3167c7afd0dbSLois Curfman McInnes    Options Database Key:
3168454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
3169c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
3170ae12b187SLois Curfman McInnes 
31719b94acceSBarry Smith    Notes:
3172e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
31734b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
3174c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
31754b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
3176c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
31779b94acceSBarry Smith 
3178ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
3179ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
3180ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
3181ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
3182ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
3183ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
3184ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
3185ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
3186ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
3187b0a32e0cSBarry Smith   appropriate method.
318836851e7fSLois Curfman McInnes 
318936851e7fSLois Curfman McInnes   Level: intermediate
3190a703fe33SLois Curfman McInnes 
3191454a90a3SBarry Smith .keywords: SNES, set, type
3192435da068SBarry Smith 
3193435da068SBarry Smith .seealso: SNESType, SNESCreate()
3194435da068SBarry Smith 
31959b94acceSBarry Smith @*/
31967087cfbeSBarry Smith PetscErrorCode  SNESSetType(SNES snes,const SNESType type)
31979b94acceSBarry Smith {
3198dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
3199ace3abfcSBarry Smith   PetscBool      match;
32003a40ed3dSBarry Smith 
32013a40ed3dSBarry Smith   PetscFunctionBegin;
32020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32034482741eSBarry Smith   PetscValidCharPointer(type,2);
320482bf6240SBarry Smith 
32056831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
32060f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
320792ff6ae8SBarry Smith 
32084b91b6eaSBarry Smith   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
3209e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
321075396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
321175396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
321275396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
321375396ef9SLisandro Dalcin   snes->ops->setup          = 0;
321475396ef9SLisandro Dalcin   snes->ops->solve          = 0;
321575396ef9SLisandro Dalcin   snes->ops->view           = 0;
321675396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
321775396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
321875396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
321975396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
3220454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
322103bfa161SLisandro Dalcin   ierr = (*r)(snes);CHKERRQ(ierr);
32229fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
32239fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
32249fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr);
32259fb22e1aSBarry Smith   }
32269fb22e1aSBarry Smith #endif
32273a40ed3dSBarry Smith   PetscFunctionReturn(0);
32289b94acceSBarry Smith }
32299b94acceSBarry Smith 
3230a847f771SSatish Balay 
32319b94acceSBarry Smith /* --------------------------------------------------------------------- */
32324a2ae208SSatish Balay #undef __FUNCT__
32334a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
323452baeb72SSatish Balay /*@
32359b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
3236f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
32379b94acceSBarry Smith 
3238fee21e36SBarry Smith    Not Collective
3239fee21e36SBarry Smith 
324036851e7fSLois Curfman McInnes    Level: advanced
324136851e7fSLois Curfman McInnes 
32429b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
32439b94acceSBarry Smith 
32449b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
32459b94acceSBarry Smith @*/
32467087cfbeSBarry Smith PetscErrorCode  SNESRegisterDestroy(void)
32479b94acceSBarry Smith {
3248dfbe8321SBarry Smith   PetscErrorCode ierr;
324982bf6240SBarry Smith 
32503a40ed3dSBarry Smith   PetscFunctionBegin;
32511441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
32524c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
32533a40ed3dSBarry Smith   PetscFunctionReturn(0);
32549b94acceSBarry Smith }
32559b94acceSBarry Smith 
32564a2ae208SSatish Balay #undef __FUNCT__
32574a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
32589b94acceSBarry Smith /*@C
32599a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
32609b94acceSBarry Smith 
3261c7afd0dbSLois Curfman McInnes    Not Collective
3262c7afd0dbSLois Curfman McInnes 
32639b94acceSBarry Smith    Input Parameter:
32644b0e389bSBarry Smith .  snes - nonlinear solver context
32659b94acceSBarry Smith 
32669b94acceSBarry Smith    Output Parameter:
32673a7fca6bSBarry Smith .  type - SNES method (a character string)
32689b94acceSBarry Smith 
326936851e7fSLois Curfman McInnes    Level: intermediate
327036851e7fSLois Curfman McInnes 
3271454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
32729b94acceSBarry Smith @*/
32737087cfbeSBarry Smith PetscErrorCode  SNESGetType(SNES snes,const SNESType *type)
32749b94acceSBarry Smith {
32753a40ed3dSBarry Smith   PetscFunctionBegin;
32760700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32774482741eSBarry Smith   PetscValidPointer(type,2);
32787adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
32793a40ed3dSBarry Smith   PetscFunctionReturn(0);
32809b94acceSBarry Smith }
32819b94acceSBarry Smith 
32824a2ae208SSatish Balay #undef __FUNCT__
32834a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
328452baeb72SSatish Balay /*@
32859b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
3286c0df2a02SJed Brown    stored. This is the fine grid solution when using SNESSetGridSequence().
32879b94acceSBarry Smith 
3288c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
3289c7afd0dbSLois Curfman McInnes 
32909b94acceSBarry Smith    Input Parameter:
32919b94acceSBarry Smith .  snes - the SNES context
32929b94acceSBarry Smith 
32939b94acceSBarry Smith    Output Parameter:
32949b94acceSBarry Smith .  x - the solution
32959b94acceSBarry Smith 
329670e92668SMatthew Knepley    Level: intermediate
329736851e7fSLois Curfman McInnes 
32989b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
32999b94acceSBarry Smith 
330085385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
33019b94acceSBarry Smith @*/
33027087cfbeSBarry Smith PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
33039b94acceSBarry Smith {
33043a40ed3dSBarry Smith   PetscFunctionBegin;
33050700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33064482741eSBarry Smith   PetscValidPointer(x,2);
330785385478SLisandro Dalcin   *x = snes->vec_sol;
330870e92668SMatthew Knepley   PetscFunctionReturn(0);
330970e92668SMatthew Knepley }
331070e92668SMatthew Knepley 
331170e92668SMatthew Knepley #undef __FUNCT__
33124a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
331352baeb72SSatish Balay /*@
33149b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
33159b94acceSBarry Smith    stored.
33169b94acceSBarry Smith 
3317c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
3318c7afd0dbSLois Curfman McInnes 
33199b94acceSBarry Smith    Input Parameter:
33209b94acceSBarry Smith .  snes - the SNES context
33219b94acceSBarry Smith 
33229b94acceSBarry Smith    Output Parameter:
33239b94acceSBarry Smith .  x - the solution update
33249b94acceSBarry Smith 
332536851e7fSLois Curfman McInnes    Level: advanced
332636851e7fSLois Curfman McInnes 
33279b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
33289b94acceSBarry Smith 
332985385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
33309b94acceSBarry Smith @*/
33317087cfbeSBarry Smith PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
33329b94acceSBarry Smith {
33333a40ed3dSBarry Smith   PetscFunctionBegin;
33340700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33354482741eSBarry Smith   PetscValidPointer(x,2);
333685385478SLisandro Dalcin   *x = snes->vec_sol_update;
33373a40ed3dSBarry Smith   PetscFunctionReturn(0);
33389b94acceSBarry Smith }
33399b94acceSBarry Smith 
33404a2ae208SSatish Balay #undef __FUNCT__
33414a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
33429b94acceSBarry Smith /*@C
33433638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
33449b94acceSBarry Smith 
3345a63bb30eSJed Brown    Not Collective, but Vec is parallel if SNES is parallel. Collective if Vec is requested, but has not been created yet.
3346c7afd0dbSLois Curfman McInnes 
33479b94acceSBarry Smith    Input Parameter:
33489b94acceSBarry Smith .  snes - the SNES context
33499b94acceSBarry Smith 
33509b94acceSBarry Smith    Output Parameter:
33517bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
335270e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
335370e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
33549b94acceSBarry Smith 
335536851e7fSLois Curfman McInnes    Level: advanced
335636851e7fSLois Curfman McInnes 
3357a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
33589b94acceSBarry Smith 
33594b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
33609b94acceSBarry Smith @*/
33617087cfbeSBarry Smith PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
33629b94acceSBarry Smith {
3363a63bb30eSJed Brown   PetscErrorCode ierr;
3364a63bb30eSJed Brown 
33653a40ed3dSBarry Smith   PetscFunctionBegin;
33660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3367a63bb30eSJed Brown   if (r) {
3368a63bb30eSJed Brown     if (!snes->vec_func) {
3369a63bb30eSJed Brown       if (snes->vec_rhs) {
3370a63bb30eSJed Brown         ierr = VecDuplicate(snes->vec_rhs,&snes->vec_func);CHKERRQ(ierr);
3371a63bb30eSJed Brown       } else if (snes->vec_sol) {
3372a63bb30eSJed Brown         ierr = VecDuplicate(snes->vec_sol,&snes->vec_func);CHKERRQ(ierr);
3373a63bb30eSJed Brown       } else if (snes->dm) {
3374a63bb30eSJed Brown         ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
3375a63bb30eSJed Brown       }
3376a63bb30eSJed Brown     }
3377a63bb30eSJed Brown     *r = snes->vec_func;
3378a63bb30eSJed Brown   }
3379e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
338070e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
33813a40ed3dSBarry Smith   PetscFunctionReturn(0);
33829b94acceSBarry Smith }
33839b94acceSBarry Smith 
3384c79ef259SPeter Brune /*@C
3385c79ef259SPeter Brune    SNESGetGS - Returns the GS function and context.
3386c79ef259SPeter Brune 
3387c79ef259SPeter Brune    Input Parameter:
3388c79ef259SPeter Brune .  snes - the SNES context
3389c79ef259SPeter Brune 
3390c79ef259SPeter Brune    Output Parameter:
3391c79ef259SPeter Brune +  gsfunc - the function (or PETSC_NULL)
3392c79ef259SPeter Brune -  ctx    - the function context (or PETSC_NULL)
3393c79ef259SPeter Brune 
3394c79ef259SPeter Brune    Level: advanced
3395c79ef259SPeter Brune 
3396c79ef259SPeter Brune .keywords: SNES, nonlinear, get, function
3397c79ef259SPeter Brune 
3398c79ef259SPeter Brune .seealso: SNESSetGS(), SNESGetFunction()
3399c79ef259SPeter Brune @*/
3400c79ef259SPeter Brune 
34014a2ae208SSatish Balay #undef __FUNCT__
3402646217ecSPeter Brune #define __FUNCT__ "SNESGetGS"
3403646217ecSPeter Brune PetscErrorCode SNESGetGS (SNES snes, PetscErrorCode(**func)(SNES, Vec, Vec, void*), void ** ctx)
3404646217ecSPeter Brune {
3405646217ecSPeter Brune   PetscFunctionBegin;
3406646217ecSPeter Brune   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3407646217ecSPeter Brune   if (func) *func = snes->ops->computegs;
3408646217ecSPeter Brune   if (ctx)  *ctx  = snes->funP;
3409646217ecSPeter Brune   PetscFunctionReturn(0);
3410646217ecSPeter Brune }
3411646217ecSPeter Brune 
34124a2ae208SSatish Balay #undef __FUNCT__
34134a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
34143c7409f5SSatish Balay /*@C
34153c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
3416d850072dSLois Curfman McInnes    SNES options in the database.
34173c7409f5SSatish Balay 
34183f9fe445SBarry Smith    Logically Collective on SNES
3419fee21e36SBarry Smith 
3420c7afd0dbSLois Curfman McInnes    Input Parameter:
3421c7afd0dbSLois Curfman McInnes +  snes - the SNES context
3422c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
3423c7afd0dbSLois Curfman McInnes 
3424d850072dSLois Curfman McInnes    Notes:
3425a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
3426c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
3427d850072dSLois Curfman McInnes 
342836851e7fSLois Curfman McInnes    Level: advanced
342936851e7fSLois Curfman McInnes 
34303c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
3431a86d99e1SLois Curfman McInnes 
3432a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
34333c7409f5SSatish Balay @*/
34347087cfbeSBarry Smith PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
34353c7409f5SSatish Balay {
3436dfbe8321SBarry Smith   PetscErrorCode ierr;
34373c7409f5SSatish Balay 
34383a40ed3dSBarry Smith   PetscFunctionBegin;
34390700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3440639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
34411cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
344294b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
34433a40ed3dSBarry Smith   PetscFunctionReturn(0);
34443c7409f5SSatish Balay }
34453c7409f5SSatish Balay 
34464a2ae208SSatish Balay #undef __FUNCT__
34474a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
34483c7409f5SSatish Balay /*@C
3449f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
3450d850072dSLois Curfman McInnes    SNES options in the database.
34513c7409f5SSatish Balay 
34523f9fe445SBarry Smith    Logically Collective on SNES
3453fee21e36SBarry Smith 
3454c7afd0dbSLois Curfman McInnes    Input Parameters:
3455c7afd0dbSLois Curfman McInnes +  snes - the SNES context
3456c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
3457c7afd0dbSLois Curfman McInnes 
3458d850072dSLois Curfman McInnes    Notes:
3459a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
3460c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
3461d850072dSLois Curfman McInnes 
346236851e7fSLois Curfman McInnes    Level: advanced
346336851e7fSLois Curfman McInnes 
34643c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
3465a86d99e1SLois Curfman McInnes 
3466a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
34673c7409f5SSatish Balay @*/
34687087cfbeSBarry Smith PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
34693c7409f5SSatish Balay {
3470dfbe8321SBarry Smith   PetscErrorCode ierr;
34713c7409f5SSatish Balay 
34723a40ed3dSBarry Smith   PetscFunctionBegin;
34730700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3474639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
34751cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
347694b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
34773a40ed3dSBarry Smith   PetscFunctionReturn(0);
34783c7409f5SSatish Balay }
34793c7409f5SSatish Balay 
34804a2ae208SSatish Balay #undef __FUNCT__
34814a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
34829ab63eb5SSatish Balay /*@C
34833c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
34843c7409f5SSatish Balay    SNES options in the database.
34853c7409f5SSatish Balay 
3486c7afd0dbSLois Curfman McInnes    Not Collective
3487c7afd0dbSLois Curfman McInnes 
34883c7409f5SSatish Balay    Input Parameter:
34893c7409f5SSatish Balay .  snes - the SNES context
34903c7409f5SSatish Balay 
34913c7409f5SSatish Balay    Output Parameter:
34923c7409f5SSatish Balay .  prefix - pointer to the prefix string used
34933c7409f5SSatish Balay 
34944ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
34959ab63eb5SSatish Balay    sufficient length to hold the prefix.
34969ab63eb5SSatish Balay 
349736851e7fSLois Curfman McInnes    Level: advanced
349836851e7fSLois Curfman McInnes 
34993c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
3500a86d99e1SLois Curfman McInnes 
3501a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
35023c7409f5SSatish Balay @*/
35037087cfbeSBarry Smith PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
35043c7409f5SSatish Balay {
3505dfbe8321SBarry Smith   PetscErrorCode ierr;
35063c7409f5SSatish Balay 
35073a40ed3dSBarry Smith   PetscFunctionBegin;
35080700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3509639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
35103a40ed3dSBarry Smith   PetscFunctionReturn(0);
35113c7409f5SSatish Balay }
35123c7409f5SSatish Balay 
3513b2002411SLois Curfman McInnes 
35144a2ae208SSatish Balay #undef __FUNCT__
35154a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
35163cea93caSBarry Smith /*@C
35173cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
35183cea93caSBarry Smith 
35197f6c08e0SMatthew Knepley   Level: advanced
35203cea93caSBarry Smith @*/
35217087cfbeSBarry Smith PetscErrorCode  SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
3522b2002411SLois Curfman McInnes {
3523e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
3524dfbe8321SBarry Smith   PetscErrorCode ierr;
3525b2002411SLois Curfman McInnes 
3526b2002411SLois Curfman McInnes   PetscFunctionBegin;
3527b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
3528c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
3529b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
3530b2002411SLois Curfman McInnes }
3531da9b6338SBarry Smith 
3532da9b6338SBarry Smith #undef __FUNCT__
3533da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
35347087cfbeSBarry Smith PetscErrorCode  SNESTestLocalMin(SNES snes)
3535da9b6338SBarry Smith {
3536dfbe8321SBarry Smith   PetscErrorCode ierr;
353777431f27SBarry Smith   PetscInt       N,i,j;
3538da9b6338SBarry Smith   Vec            u,uh,fh;
3539da9b6338SBarry Smith   PetscScalar    value;
3540da9b6338SBarry Smith   PetscReal      norm;
3541da9b6338SBarry Smith 
3542da9b6338SBarry Smith   PetscFunctionBegin;
3543da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
3544da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
3545da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
3546da9b6338SBarry Smith 
3547da9b6338SBarry Smith   /* currently only works for sequential */
3548da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
3549da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
3550da9b6338SBarry Smith   for (i=0; i<N; i++) {
3551da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
355277431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
3553da9b6338SBarry Smith     for (j=-10; j<11; j++) {
3554ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
3555da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
35563ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
3557da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
355877431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
3559da9b6338SBarry Smith       value = -value;
3560da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
3561da9b6338SBarry Smith     }
3562da9b6338SBarry Smith   }
35636bf464f9SBarry Smith   ierr = VecDestroy(&uh);CHKERRQ(ierr);
35646bf464f9SBarry Smith   ierr = VecDestroy(&fh);CHKERRQ(ierr);
3565da9b6338SBarry Smith   PetscFunctionReturn(0);
3566da9b6338SBarry Smith }
356771f87433Sdalcinl 
356871f87433Sdalcinl #undef __FUNCT__
3569fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
357071f87433Sdalcinl /*@
3571fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
357271f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
357371f87433Sdalcinl    Newton method.
357471f87433Sdalcinl 
35753f9fe445SBarry Smith    Logically Collective on SNES
357671f87433Sdalcinl 
357771f87433Sdalcinl    Input Parameters:
357871f87433Sdalcinl +  snes - SNES context
357971f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
358071f87433Sdalcinl 
358164ba62caSBarry Smith     Options Database:
358264ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
358364ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
358464ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
358564ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
358664ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
358764ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
358864ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
358964ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
359064ba62caSBarry Smith 
359171f87433Sdalcinl    Notes:
359271f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
359371f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
359471f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
359571f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
359671f87433Sdalcinl    solver.
359771f87433Sdalcinl 
359871f87433Sdalcinl    Level: advanced
359971f87433Sdalcinl 
360071f87433Sdalcinl    Reference:
360171f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
360271f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
360371f87433Sdalcinl 
360471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
360571f87433Sdalcinl 
3606fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
360771f87433Sdalcinl @*/
36087087cfbeSBarry Smith PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool  flag)
360971f87433Sdalcinl {
361071f87433Sdalcinl   PetscFunctionBegin;
36110700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3612acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flag,2);
361371f87433Sdalcinl   snes->ksp_ewconv = flag;
361471f87433Sdalcinl   PetscFunctionReturn(0);
361571f87433Sdalcinl }
361671f87433Sdalcinl 
361771f87433Sdalcinl #undef __FUNCT__
3618fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
361971f87433Sdalcinl /*@
3620fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
362171f87433Sdalcinl    for computing relative tolerance for linear solvers within an
362271f87433Sdalcinl    inexact Newton method.
362371f87433Sdalcinl 
362471f87433Sdalcinl    Not Collective
362571f87433Sdalcinl 
362671f87433Sdalcinl    Input Parameter:
362771f87433Sdalcinl .  snes - SNES context
362871f87433Sdalcinl 
362971f87433Sdalcinl    Output Parameter:
363071f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
363171f87433Sdalcinl 
363271f87433Sdalcinl    Notes:
363371f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
363471f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
363571f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
363671f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
363771f87433Sdalcinl    solver.
363871f87433Sdalcinl 
363971f87433Sdalcinl    Level: advanced
364071f87433Sdalcinl 
364171f87433Sdalcinl    Reference:
364271f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
364371f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
364471f87433Sdalcinl 
364571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
364671f87433Sdalcinl 
3647fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
364871f87433Sdalcinl @*/
36497087cfbeSBarry Smith PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
365071f87433Sdalcinl {
365171f87433Sdalcinl   PetscFunctionBegin;
36520700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
365371f87433Sdalcinl   PetscValidPointer(flag,2);
365471f87433Sdalcinl   *flag = snes->ksp_ewconv;
365571f87433Sdalcinl   PetscFunctionReturn(0);
365671f87433Sdalcinl }
365771f87433Sdalcinl 
365871f87433Sdalcinl #undef __FUNCT__
3659fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
366071f87433Sdalcinl /*@
3661fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
366271f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
366371f87433Sdalcinl    Newton method.
366471f87433Sdalcinl 
36653f9fe445SBarry Smith    Logically Collective on SNES
366671f87433Sdalcinl 
366771f87433Sdalcinl    Input Parameters:
366871f87433Sdalcinl +    snes - SNES context
366971f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
367071f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
367171f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
367271f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
367371f87433Sdalcinl              (0 <= gamma2 <= 1)
367471f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
367571f87433Sdalcinl .    alpha2 - power for safeguard
367671f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
367771f87433Sdalcinl 
367871f87433Sdalcinl    Note:
367971f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
368071f87433Sdalcinl 
368171f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
368271f87433Sdalcinl 
368371f87433Sdalcinl    Level: advanced
368471f87433Sdalcinl 
368571f87433Sdalcinl    Reference:
368671f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
368771f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
368871f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
368971f87433Sdalcinl 
369071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
369171f87433Sdalcinl 
3692fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
369371f87433Sdalcinl @*/
36947087cfbeSBarry Smith PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
369571f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
369671f87433Sdalcinl {
3697fa9f3622SBarry Smith   SNESKSPEW *kctx;
369871f87433Sdalcinl   PetscFunctionBegin;
36990700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3700fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3701e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
3702c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
3703c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
3704c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
3705c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
3706c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
3707c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
3708c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
370971f87433Sdalcinl 
371071f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
371171f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
371271f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
371371f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
371471f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
371571f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
371671f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
371771f87433Sdalcinl 
371871f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
3719e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
372071f87433Sdalcinl   }
372171f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
3722e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
372371f87433Sdalcinl   }
372471f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
3725e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
372671f87433Sdalcinl   }
372771f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
3728e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
372971f87433Sdalcinl   }
373071f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
3731e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
373271f87433Sdalcinl   }
373371f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
3734e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
373571f87433Sdalcinl   }
373671f87433Sdalcinl   PetscFunctionReturn(0);
373771f87433Sdalcinl }
373871f87433Sdalcinl 
373971f87433Sdalcinl #undef __FUNCT__
3740fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
374171f87433Sdalcinl /*@
3742fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
374371f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
374471f87433Sdalcinl    Newton method.
374571f87433Sdalcinl 
374671f87433Sdalcinl    Not Collective
374771f87433Sdalcinl 
374871f87433Sdalcinl    Input Parameters:
374971f87433Sdalcinl      snes - SNES context
375071f87433Sdalcinl 
375171f87433Sdalcinl    Output Parameters:
375271f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
375371f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
375471f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
375571f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
375671f87433Sdalcinl              (0 <= gamma2 <= 1)
375771f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
375871f87433Sdalcinl .    alpha2 - power for safeguard
375971f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
376071f87433Sdalcinl 
376171f87433Sdalcinl    Level: advanced
376271f87433Sdalcinl 
376371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
376471f87433Sdalcinl 
3765fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
376671f87433Sdalcinl @*/
37677087cfbeSBarry Smith PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
376871f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
376971f87433Sdalcinl {
3770fa9f3622SBarry Smith   SNESKSPEW *kctx;
377171f87433Sdalcinl   PetscFunctionBegin;
37720700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3773fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3774e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
377571f87433Sdalcinl   if(version)   *version   = kctx->version;
377671f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
377771f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
377871f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
377971f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
378071f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
378171f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
378271f87433Sdalcinl   PetscFunctionReturn(0);
378371f87433Sdalcinl }
378471f87433Sdalcinl 
378571f87433Sdalcinl #undef __FUNCT__
3786fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
3787fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
378871f87433Sdalcinl {
378971f87433Sdalcinl   PetscErrorCode ierr;
3790fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
379171f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
379271f87433Sdalcinl 
379371f87433Sdalcinl   PetscFunctionBegin;
3794e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
379571f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
379671f87433Sdalcinl     rtol = kctx->rtol_0;
379771f87433Sdalcinl   } else {
379871f87433Sdalcinl     if (kctx->version == 1) {
379971f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
380071f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
380171f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
380271f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
380371f87433Sdalcinl     } else if (kctx->version == 2) {
380471f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
380571f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
380671f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
380771f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
380871f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
380971f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
381071f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
381171f87433Sdalcinl       stol = PetscMax(rtol,stol);
381271f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
381371f87433Sdalcinl       /* safeguard: avoid oversolving */
381471f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
381571f87433Sdalcinl       stol = PetscMax(rtol,stol);
381671f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
3817e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
381871f87433Sdalcinl   }
381971f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
382071f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
382171f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
382271f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
382371f87433Sdalcinl   PetscFunctionReturn(0);
382471f87433Sdalcinl }
382571f87433Sdalcinl 
382671f87433Sdalcinl #undef __FUNCT__
3827fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
3828fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
382971f87433Sdalcinl {
383071f87433Sdalcinl   PetscErrorCode ierr;
3831fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
383271f87433Sdalcinl   PCSide         pcside;
383371f87433Sdalcinl   Vec            lres;
383471f87433Sdalcinl 
383571f87433Sdalcinl   PetscFunctionBegin;
3836e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
383771f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
383871f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
383971f87433Sdalcinl   if (kctx->version == 1) {
3840b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
384171f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
384271f87433Sdalcinl       /* KSP residual is true linear residual */
384371f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
384471f87433Sdalcinl     } else {
384571f87433Sdalcinl       /* KSP residual is preconditioned residual */
384671f87433Sdalcinl       /* compute true linear residual norm */
384771f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
384871f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
384971f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
385071f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
38516bf464f9SBarry Smith       ierr = VecDestroy(&lres);CHKERRQ(ierr);
385271f87433Sdalcinl     }
385371f87433Sdalcinl   }
385471f87433Sdalcinl   PetscFunctionReturn(0);
385571f87433Sdalcinl }
385671f87433Sdalcinl 
385771f87433Sdalcinl #undef __FUNCT__
385871f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
385971f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
386071f87433Sdalcinl {
386171f87433Sdalcinl   PetscErrorCode ierr;
386271f87433Sdalcinl 
386371f87433Sdalcinl   PetscFunctionBegin;
3864fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
386571f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3866fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
386771f87433Sdalcinl   PetscFunctionReturn(0);
386871f87433Sdalcinl }
38696c699258SBarry Smith 
38706c699258SBarry Smith #undef __FUNCT__
38716c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
38726c699258SBarry Smith /*@
38736c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
38746c699258SBarry Smith 
38753f9fe445SBarry Smith    Logically Collective on SNES
38766c699258SBarry Smith 
38776c699258SBarry Smith    Input Parameters:
38786c699258SBarry Smith +  snes - the preconditioner context
38796c699258SBarry Smith -  dm - the dm
38806c699258SBarry Smith 
38816c699258SBarry Smith    Level: intermediate
38826c699258SBarry Smith 
38836c699258SBarry Smith 
38846c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
38856c699258SBarry Smith @*/
38867087cfbeSBarry Smith PetscErrorCode  SNESSetDM(SNES snes,DM dm)
38876c699258SBarry Smith {
38886c699258SBarry Smith   PetscErrorCode ierr;
3889345fed2cSBarry Smith   KSP            ksp;
38906c699258SBarry Smith 
38916c699258SBarry Smith   PetscFunctionBegin;
38920700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3893d0660788SBarry Smith   if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);}
38946bf464f9SBarry Smith   ierr = DMDestroy(&snes->dm);CHKERRQ(ierr);
38956c699258SBarry Smith   snes->dm = dm;
3896345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3897345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3898f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
38992c155ee1SBarry Smith   if (snes->pc) {
39002c155ee1SBarry Smith     ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr);
39012c155ee1SBarry Smith   }
39026c699258SBarry Smith   PetscFunctionReturn(0);
39036c699258SBarry Smith }
39046c699258SBarry Smith 
39056c699258SBarry Smith #undef __FUNCT__
39066c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
39076c699258SBarry Smith /*@
39086c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
39096c699258SBarry Smith 
39103f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
39116c699258SBarry Smith 
39126c699258SBarry Smith    Input Parameter:
39136c699258SBarry Smith . snes - the preconditioner context
39146c699258SBarry Smith 
39156c699258SBarry Smith    Output Parameter:
39166c699258SBarry Smith .  dm - the dm
39176c699258SBarry Smith 
39186c699258SBarry Smith    Level: intermediate
39196c699258SBarry Smith 
39206c699258SBarry Smith 
39216c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
39226c699258SBarry Smith @*/
39237087cfbeSBarry Smith PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
39246c699258SBarry Smith {
39256c699258SBarry Smith   PetscFunctionBegin;
39260700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
39276c699258SBarry Smith   *dm = snes->dm;
39286c699258SBarry Smith   PetscFunctionReturn(0);
39296c699258SBarry Smith }
39300807856dSBarry Smith 
393131823bd8SMatthew G Knepley #undef __FUNCT__
393231823bd8SMatthew G Knepley #define __FUNCT__ "SNESSetPC"
393331823bd8SMatthew G Knepley /*@
3934fd4b34e9SJed Brown   SNESSetPC - Sets the nonlinear preconditioner to be used.
393531823bd8SMatthew G Knepley 
393631823bd8SMatthew G Knepley   Collective on SNES
393731823bd8SMatthew G Knepley 
393831823bd8SMatthew G Knepley   Input Parameters:
393931823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate()
394031823bd8SMatthew G Knepley - pc   - the preconditioner object
394131823bd8SMatthew G Knepley 
394231823bd8SMatthew G Knepley   Notes:
394331823bd8SMatthew G Knepley   Use SNESGetPC() to retrieve the preconditioner context (for example,
394431823bd8SMatthew G Knepley   to configure it using the API).
394531823bd8SMatthew G Knepley 
394631823bd8SMatthew G Knepley   Level: developer
394731823bd8SMatthew G Knepley 
394831823bd8SMatthew G Knepley .keywords: SNES, set, precondition
394931823bd8SMatthew G Knepley .seealso: SNESGetPC()
395031823bd8SMatthew G Knepley @*/
395131823bd8SMatthew G Knepley PetscErrorCode SNESSetPC(SNES snes, SNES pc)
395231823bd8SMatthew G Knepley {
395331823bd8SMatthew G Knepley   PetscErrorCode ierr;
395431823bd8SMatthew G Knepley 
395531823bd8SMatthew G Knepley   PetscFunctionBegin;
395631823bd8SMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
395731823bd8SMatthew G Knepley   PetscValidHeaderSpecific(pc, SNES_CLASSID, 2);
395831823bd8SMatthew G Knepley   PetscCheckSameComm(snes, 1, pc, 2);
395931823bd8SMatthew G Knepley   ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr);
3960bf11d3b6SBarry Smith   ierr = SNESDestroy(&snes->pc);CHKERRQ(ierr);
396131823bd8SMatthew G Knepley   snes->pc = pc;
396231823bd8SMatthew G Knepley   ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr);
396331823bd8SMatthew G Knepley   PetscFunctionReturn(0);
396431823bd8SMatthew G Knepley }
396531823bd8SMatthew G Knepley 
396631823bd8SMatthew G Knepley #undef __FUNCT__
396731823bd8SMatthew G Knepley #define __FUNCT__ "SNESGetPC"
396831823bd8SMatthew G Knepley /*@
3969fd4b34e9SJed Brown   SNESGetPC - Returns a pointer to the nonlinear preconditioning context set with SNESSetPC().
397031823bd8SMatthew G Knepley 
397131823bd8SMatthew G Knepley   Not Collective
397231823bd8SMatthew G Knepley 
397331823bd8SMatthew G Knepley   Input Parameter:
397431823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate()
397531823bd8SMatthew G Knepley 
397631823bd8SMatthew G Knepley   Output Parameter:
397731823bd8SMatthew G Knepley . pc - preconditioner context
397831823bd8SMatthew G Knepley 
397931823bd8SMatthew G Knepley   Level: developer
398031823bd8SMatthew G Knepley 
398131823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner
398231823bd8SMatthew G Knepley .seealso: SNESSetPC()
398331823bd8SMatthew G Knepley @*/
398431823bd8SMatthew G Knepley PetscErrorCode SNESGetPC(SNES snes, SNES *pc)
398531823bd8SMatthew G Knepley {
398631823bd8SMatthew G Knepley   PetscErrorCode ierr;
398731823bd8SMatthew G Knepley 
398831823bd8SMatthew G Knepley   PetscFunctionBegin;
398931823bd8SMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
399031823bd8SMatthew G Knepley   PetscValidPointer(pc, 2);
399131823bd8SMatthew G Knepley   if (!snes->pc) {
399231823bd8SMatthew G Knepley     ierr = SNESCreate(((PetscObject) snes)->comm, &snes->pc);CHKERRQ(ierr);
39934a0c5b0cSMatthew G Knepley     ierr = PetscObjectIncrementTabLevel((PetscObject) snes->pc, (PetscObject) snes, 1);CHKERRQ(ierr);
399431823bd8SMatthew G Knepley     ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr);
399531823bd8SMatthew G Knepley   }
399631823bd8SMatthew G Knepley   *pc = snes->pc;
399731823bd8SMatthew G Knepley   PetscFunctionReturn(0);
399831823bd8SMatthew G Knepley }
399931823bd8SMatthew G Knepley 
400069b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4001c6db04a5SJed Brown #include <mex.h>
400269b4f73cSBarry Smith 
40038f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
40048f6e6473SBarry Smith 
40050807856dSBarry Smith #undef __FUNCT__
40060807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab"
40070807856dSBarry Smith /*
40080807856dSBarry Smith    SNESComputeFunction_Matlab - Calls the function that has been set with
40090807856dSBarry Smith                          SNESSetFunctionMatlab().
40100807856dSBarry Smith 
40110807856dSBarry Smith    Collective on SNES
40120807856dSBarry Smith 
40130807856dSBarry Smith    Input Parameters:
40140807856dSBarry Smith +  snes - the SNES context
40150807856dSBarry Smith -  x - input vector
40160807856dSBarry Smith 
40170807856dSBarry Smith    Output Parameter:
40180807856dSBarry Smith .  y - function vector, as set by SNESSetFunction()
40190807856dSBarry Smith 
40200807856dSBarry Smith    Notes:
40210807856dSBarry Smith    SNESComputeFunction() is typically used within nonlinear solvers
40220807856dSBarry Smith    implementations, so most users would not generally call this routine
40230807856dSBarry Smith    themselves.
40240807856dSBarry Smith 
40250807856dSBarry Smith    Level: developer
40260807856dSBarry Smith 
40270807856dSBarry Smith .keywords: SNES, nonlinear, compute, function
40280807856dSBarry Smith 
40290807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
403061b2408cSBarry Smith */
40317087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx)
40320807856dSBarry Smith {
4033e650e774SBarry Smith   PetscErrorCode    ierr;
40348f6e6473SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
40358f6e6473SBarry Smith   int               nlhs = 1,nrhs = 5;
40368f6e6473SBarry Smith   mxArray	    *plhs[1],*prhs[5];
403791621f2eSBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
4038e650e774SBarry Smith 
40390807856dSBarry Smith   PetscFunctionBegin;
40400807856dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
40410807856dSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
40420807856dSBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
40430807856dSBarry Smith   PetscCheckSameComm(snes,1,x,2);
40440807856dSBarry Smith   PetscCheckSameComm(snes,1,y,3);
40450807856dSBarry Smith 
40460807856dSBarry Smith   /* call Matlab function in ctx with arguments x and y */
4047e650e774SBarry Smith 
404891621f2eSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
4049e650e774SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
4050e650e774SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
405191621f2eSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
405291621f2eSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
405391621f2eSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
40548f6e6473SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
40558f6e6473SBarry Smith   prhs[4] =  sctx->ctx;
4056b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr);
4057e650e774SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4058e650e774SBarry Smith   mxDestroyArray(prhs[0]);
4059e650e774SBarry Smith   mxDestroyArray(prhs[1]);
4060e650e774SBarry Smith   mxDestroyArray(prhs[2]);
40618f6e6473SBarry Smith   mxDestroyArray(prhs[3]);
4062e650e774SBarry Smith   mxDestroyArray(plhs[0]);
40630807856dSBarry Smith   PetscFunctionReturn(0);
40640807856dSBarry Smith }
40650807856dSBarry Smith 
40660807856dSBarry Smith 
40670807856dSBarry Smith #undef __FUNCT__
40680807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab"
406961b2408cSBarry Smith /*
40700807856dSBarry Smith    SNESSetFunctionMatlab - Sets the function evaluation routine and function
40710807856dSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
4072e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
40730807856dSBarry Smith 
40740807856dSBarry Smith    Logically Collective on SNES
40750807856dSBarry Smith 
40760807856dSBarry Smith    Input Parameters:
40770807856dSBarry Smith +  snes - the SNES context
40780807856dSBarry Smith .  r - vector to store function value
40790807856dSBarry Smith -  func - function evaluation routine
40800807856dSBarry Smith 
40810807856dSBarry Smith    Calling sequence of func:
408261b2408cSBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
40830807856dSBarry Smith 
40840807856dSBarry Smith 
40850807856dSBarry Smith    Notes:
40860807856dSBarry Smith    The Newton-like methods typically solve linear systems of the form
40870807856dSBarry Smith $      f'(x) x = -f(x),
40880807856dSBarry Smith    where f'(x) denotes the Jacobian matrix and f(x) is the function.
40890807856dSBarry Smith 
40900807856dSBarry Smith    Level: beginner
40910807856dSBarry Smith 
40920807856dSBarry Smith .keywords: SNES, nonlinear, set, function
40930807856dSBarry Smith 
40940807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
409561b2408cSBarry Smith */
40967087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx)
40970807856dSBarry Smith {
40980807856dSBarry Smith   PetscErrorCode    ierr;
40998f6e6473SBarry Smith   SNESMatlabContext *sctx;
41000807856dSBarry Smith 
41010807856dSBarry Smith   PetscFunctionBegin;
41028f6e6473SBarry Smith   /* currently sctx is memory bleed */
41038f6e6473SBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
41048f6e6473SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
41058f6e6473SBarry Smith   /*
41068f6e6473SBarry Smith      This should work, but it doesn't
41078f6e6473SBarry Smith   sctx->ctx = ctx;
41088f6e6473SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
41098f6e6473SBarry Smith   */
41108f6e6473SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
41118f6e6473SBarry Smith   ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr);
41120807856dSBarry Smith   PetscFunctionReturn(0);
41130807856dSBarry Smith }
411469b4f73cSBarry Smith 
411561b2408cSBarry Smith #undef __FUNCT__
411661b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab"
411761b2408cSBarry Smith /*
411861b2408cSBarry Smith    SNESComputeJacobian_Matlab - Calls the function that has been set with
411961b2408cSBarry Smith                          SNESSetJacobianMatlab().
412061b2408cSBarry Smith 
412161b2408cSBarry Smith    Collective on SNES
412261b2408cSBarry Smith 
412361b2408cSBarry Smith    Input Parameters:
412461b2408cSBarry Smith +  snes - the SNES context
412561b2408cSBarry Smith .  x - input vector
412661b2408cSBarry Smith .  A, B - the matrices
412761b2408cSBarry Smith -  ctx - user context
412861b2408cSBarry Smith 
412961b2408cSBarry Smith    Output Parameter:
413061b2408cSBarry Smith .  flag - structure of the matrix
413161b2408cSBarry Smith 
413261b2408cSBarry Smith    Level: developer
413361b2408cSBarry Smith 
413461b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function
413561b2408cSBarry Smith 
413661b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
413761b2408cSBarry Smith @*/
41387087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx)
413961b2408cSBarry Smith {
414061b2408cSBarry Smith   PetscErrorCode    ierr;
414161b2408cSBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
414261b2408cSBarry Smith   int               nlhs = 2,nrhs = 6;
414361b2408cSBarry Smith   mxArray	    *plhs[2],*prhs[6];
414461b2408cSBarry Smith   long long int     lx = 0,lA = 0,ls = 0, lB = 0;
414561b2408cSBarry Smith 
414661b2408cSBarry Smith   PetscFunctionBegin;
414761b2408cSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
414861b2408cSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
414961b2408cSBarry Smith 
415061b2408cSBarry Smith   /* call Matlab function in ctx with arguments x and y */
415161b2408cSBarry Smith 
415261b2408cSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
415361b2408cSBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
415461b2408cSBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
415561b2408cSBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
415661b2408cSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
415761b2408cSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
415861b2408cSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
415961b2408cSBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
416061b2408cSBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
416161b2408cSBarry Smith   prhs[5] =  sctx->ctx;
4162b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr);
416361b2408cSBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
416461b2408cSBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
416561b2408cSBarry Smith   mxDestroyArray(prhs[0]);
416661b2408cSBarry Smith   mxDestroyArray(prhs[1]);
416761b2408cSBarry Smith   mxDestroyArray(prhs[2]);
416861b2408cSBarry Smith   mxDestroyArray(prhs[3]);
416961b2408cSBarry Smith   mxDestroyArray(prhs[4]);
417061b2408cSBarry Smith   mxDestroyArray(plhs[0]);
417161b2408cSBarry Smith   mxDestroyArray(plhs[1]);
417261b2408cSBarry Smith   PetscFunctionReturn(0);
417361b2408cSBarry Smith }
417461b2408cSBarry Smith 
417561b2408cSBarry Smith 
417661b2408cSBarry Smith #undef __FUNCT__
417761b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab"
417861b2408cSBarry Smith /*
417961b2408cSBarry Smith    SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
418061b2408cSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
4181e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
418261b2408cSBarry Smith 
418361b2408cSBarry Smith    Logically Collective on SNES
418461b2408cSBarry Smith 
418561b2408cSBarry Smith    Input Parameters:
418661b2408cSBarry Smith +  snes - the SNES context
418761b2408cSBarry Smith .  A,B - Jacobian matrices
418861b2408cSBarry Smith .  func - function evaluation routine
418961b2408cSBarry Smith -  ctx - user context
419061b2408cSBarry Smith 
419161b2408cSBarry Smith    Calling sequence of func:
419261b2408cSBarry Smith $    flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx);
419361b2408cSBarry Smith 
419461b2408cSBarry Smith 
419561b2408cSBarry Smith    Level: developer
419661b2408cSBarry Smith 
419761b2408cSBarry Smith .keywords: SNES, nonlinear, set, function
419861b2408cSBarry Smith 
419961b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
420061b2408cSBarry Smith */
42017087cfbeSBarry Smith PetscErrorCode  SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx)
420261b2408cSBarry Smith {
420361b2408cSBarry Smith   PetscErrorCode    ierr;
420461b2408cSBarry Smith   SNESMatlabContext *sctx;
420561b2408cSBarry Smith 
420661b2408cSBarry Smith   PetscFunctionBegin;
420761b2408cSBarry Smith   /* currently sctx is memory bleed */
420861b2408cSBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
420961b2408cSBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
421061b2408cSBarry Smith   /*
421161b2408cSBarry Smith      This should work, but it doesn't
421261b2408cSBarry Smith   sctx->ctx = ctx;
421361b2408cSBarry Smith   mexMakeArrayPersistent(sctx->ctx);
421461b2408cSBarry Smith   */
421561b2408cSBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
421661b2408cSBarry Smith   ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
421761b2408cSBarry Smith   PetscFunctionReturn(0);
421861b2408cSBarry Smith }
421969b4f73cSBarry Smith 
4220f9eb7ae2SShri Abhyankar #undef __FUNCT__
4221f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab"
4222f9eb7ae2SShri Abhyankar /*
4223f9eb7ae2SShri Abhyankar    SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab().
4224f9eb7ae2SShri Abhyankar 
4225f9eb7ae2SShri Abhyankar    Collective on SNES
4226f9eb7ae2SShri Abhyankar 
4227f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction()
4228f9eb7ae2SShri Abhyankar @*/
42297087cfbeSBarry Smith PetscErrorCode  SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx)
4230f9eb7ae2SShri Abhyankar {
4231f9eb7ae2SShri Abhyankar   PetscErrorCode  ierr;
423248f37e70SShri Abhyankar   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
4233f9eb7ae2SShri Abhyankar   int             nlhs = 1,nrhs = 6;
4234f9eb7ae2SShri Abhyankar   mxArray	  *plhs[1],*prhs[6];
4235f9eb7ae2SShri Abhyankar   long long int   lx = 0,ls = 0;
4236f9eb7ae2SShri Abhyankar   Vec             x=snes->vec_sol;
4237f9eb7ae2SShri Abhyankar 
4238f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
4239f9eb7ae2SShri Abhyankar   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
4240f9eb7ae2SShri Abhyankar 
4241f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
4242f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
4243f9eb7ae2SShri Abhyankar   prhs[0] =  mxCreateDoubleScalar((double)ls);
4244f9eb7ae2SShri Abhyankar   prhs[1] =  mxCreateDoubleScalar((double)it);
4245f9eb7ae2SShri Abhyankar   prhs[2] =  mxCreateDoubleScalar((double)fnorm);
4246f9eb7ae2SShri Abhyankar   prhs[3] =  mxCreateDoubleScalar((double)lx);
4247f9eb7ae2SShri Abhyankar   prhs[4] =  mxCreateString(sctx->funcname);
4248f9eb7ae2SShri Abhyankar   prhs[5] =  sctx->ctx;
4249f9eb7ae2SShri Abhyankar   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr);
4250f9eb7ae2SShri Abhyankar   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4251f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[0]);
4252f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[1]);
4253f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[2]);
4254f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[3]);
4255f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[4]);
4256f9eb7ae2SShri Abhyankar   mxDestroyArray(plhs[0]);
4257f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
4258f9eb7ae2SShri Abhyankar }
4259f9eb7ae2SShri Abhyankar 
4260f9eb7ae2SShri Abhyankar 
4261f9eb7ae2SShri Abhyankar #undef __FUNCT__
4262f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab"
4263f9eb7ae2SShri Abhyankar /*
4264e3c5b3baSBarry Smith    SNESMonitorSetMatlab - Sets the monitor function from MATLAB
4265f9eb7ae2SShri Abhyankar 
4266f9eb7ae2SShri Abhyankar    Level: developer
4267f9eb7ae2SShri Abhyankar 
4268f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function
4269f9eb7ae2SShri Abhyankar 
4270f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
4271f9eb7ae2SShri Abhyankar */
42727087cfbeSBarry Smith PetscErrorCode  SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx)
4273f9eb7ae2SShri Abhyankar {
4274f9eb7ae2SShri Abhyankar   PetscErrorCode    ierr;
4275f9eb7ae2SShri Abhyankar   SNESMatlabContext *sctx;
4276f9eb7ae2SShri Abhyankar 
4277f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
4278f9eb7ae2SShri Abhyankar   /* currently sctx is memory bleed */
4279f9eb7ae2SShri Abhyankar   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
4280f9eb7ae2SShri Abhyankar   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4281f9eb7ae2SShri Abhyankar   /*
4282f9eb7ae2SShri Abhyankar      This should work, but it doesn't
4283f9eb7ae2SShri Abhyankar   sctx->ctx = ctx;
4284f9eb7ae2SShri Abhyankar   mexMakeArrayPersistent(sctx->ctx);
4285f9eb7ae2SShri Abhyankar   */
4286f9eb7ae2SShri Abhyankar   sctx->ctx = mxDuplicateArray(ctx);
4287f9eb7ae2SShri Abhyankar   ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4288f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
4289f9eb7ae2SShri Abhyankar }
4290f9eb7ae2SShri Abhyankar 
429169b4f73cSBarry Smith #endif
4292