xref: /petsc/src/snes/interface/snes.c (revision c79ef2592ddd350a3e361856ed98a4a2544c077c)
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;
9166c7f25SBarry Smith PetscLogEvent  SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval;
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;
57e113a28aSBarry Smith   PetscFunctionReturn(0);
58e113a28aSBarry Smith }
59e113a28aSBarry Smith 
60e113a28aSBarry Smith #undef __FUNCT__
61e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged"
62e113a28aSBarry Smith /*@
63e113a28aSBarry Smith    SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge?
64e113a28aSBarry Smith 
65e113a28aSBarry Smith    Not Collective
66e113a28aSBarry Smith 
67e113a28aSBarry Smith    Input Parameter:
68e113a28aSBarry Smith .  snes - iterative context obtained from SNESCreate()
69e113a28aSBarry Smith 
70e113a28aSBarry Smith    Output Parameter:
71e113a28aSBarry Smith .  flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE
72e113a28aSBarry Smith 
73e113a28aSBarry Smith    Level: intermediate
74e113a28aSBarry Smith 
75e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
76e113a28aSBarry Smith 
77e113a28aSBarry Smith .seealso:  SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
78e113a28aSBarry Smith @*/
797087cfbeSBarry Smith PetscErrorCode  SNESGetErrorIfNotConverged(SNES snes,PetscBool  *flag)
80e113a28aSBarry Smith {
81e113a28aSBarry Smith   PetscFunctionBegin;
82e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
83e113a28aSBarry Smith   PetscValidPointer(flag,2);
84e113a28aSBarry Smith   *flag = snes->errorifnotconverged;
85e113a28aSBarry Smith   PetscFunctionReturn(0);
86e113a28aSBarry Smith }
87e113a28aSBarry Smith 
88e113a28aSBarry Smith #undef __FUNCT__
894936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
90e725d27bSBarry Smith /*@
914936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
924936397dSBarry Smith      in the functions domain. For example, negative pressure.
934936397dSBarry Smith 
943f9fe445SBarry Smith    Logically Collective on SNES
954936397dSBarry Smith 
964936397dSBarry Smith    Input Parameters:
974936397dSBarry Smith .  SNES - the SNES context
984936397dSBarry Smith 
9928529972SSatish Balay    Level: advanced
1004936397dSBarry Smith 
1014936397dSBarry Smith .keywords: SNES, view
1024936397dSBarry Smith 
1034936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
1044936397dSBarry Smith @*/
1057087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionDomainError(SNES snes)
1064936397dSBarry Smith {
1074936397dSBarry Smith   PetscFunctionBegin;
1080700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1094936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
1104936397dSBarry Smith   PetscFunctionReturn(0);
1114936397dSBarry Smith }
1124936397dSBarry Smith 
1134936397dSBarry Smith #undef __FUNCT__
1144a2ae208SSatish Balay #define __FUNCT__ "SNESView"
1157e2c5f70SBarry Smith /*@C
1169b94acceSBarry Smith    SNESView - Prints the SNES data structure.
1179b94acceSBarry Smith 
1184c49b128SBarry Smith    Collective on SNES
119fee21e36SBarry Smith 
120c7afd0dbSLois Curfman McInnes    Input Parameters:
121c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
122c7afd0dbSLois Curfman McInnes -  viewer - visualization context
123c7afd0dbSLois Curfman McInnes 
1249b94acceSBarry Smith    Options Database Key:
125c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
1269b94acceSBarry Smith 
1279b94acceSBarry Smith    Notes:
1289b94acceSBarry Smith    The available visualization contexts include
129b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
130b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
131c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
132c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
133c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
1349b94acceSBarry Smith 
1353e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
136b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
1379b94acceSBarry Smith 
13836851e7fSLois Curfman McInnes    Level: beginner
13936851e7fSLois Curfman McInnes 
1409b94acceSBarry Smith .keywords: SNES, view
1419b94acceSBarry Smith 
142b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1439b94acceSBarry Smith @*/
1447087cfbeSBarry Smith PetscErrorCode  SNESView(SNES snes,PetscViewer viewer)
1459b94acceSBarry Smith {
146fa9f3622SBarry Smith   SNESKSPEW           *kctx;
147dfbe8321SBarry Smith   PetscErrorCode      ierr;
14894b7f48cSBarry Smith   KSP                 ksp;
149ace3abfcSBarry Smith   PetscBool           iascii,isstring;
1509b94acceSBarry Smith 
1513a40ed3dSBarry Smith   PetscFunctionBegin;
1520700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1533050cee2SBarry Smith   if (!viewer) {
1547adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
1553050cee2SBarry Smith   }
1560700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
157c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
15874679c65SBarry Smith 
1592692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1602692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
16132077d6dSBarry Smith   if (iascii) {
162317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer,"SNES Object");CHKERRQ(ierr);
163e7788613SBarry Smith     if (snes->ops->view) {
164b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
165e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
166b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1670ef38995SBarry Smith     }
16877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
169a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
17070441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
17177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
17277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1739b94acceSBarry Smith     if (snes->ksp_ewconv) {
174fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1759b94acceSBarry Smith       if (kctx) {
17677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
177a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
178a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1799b94acceSBarry Smith       }
1809b94acceSBarry Smith     }
181eb1f6c34SBarry Smith     if (snes->lagpreconditioner == -1) {
182eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is never rebuilt\n");CHKERRQ(ierr);
183eb1f6c34SBarry Smith     } else if (snes->lagpreconditioner > 1) {
184eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr);
185eb1f6c34SBarry Smith     }
186eb1f6c34SBarry Smith     if (snes->lagjacobian == -1) {
187eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is never rebuilt\n");CHKERRQ(ierr);
188eb1f6c34SBarry Smith     } else if (snes->lagjacobian > 1) {
18942f4f86dSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is rebuilt every %D SNES iterations\n",snes->lagjacobian);CHKERRQ(ierr);
190eb1f6c34SBarry Smith     }
1910f5bd95cSBarry Smith   } else if (isstring) {
192317d6ea6SBarry Smith     const char *type;
193454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
194b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
19519bcc07fSBarry Smith   }
19642f4f86dSBarry Smith   if (snes->pc && snes->usespc) {
1974a0c5b0cSMatthew G Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1984a0c5b0cSMatthew G Knepley     ierr = SNESView(snes->pc, viewer);CHKERRQ(ierr);
1994a0c5b0cSMatthew G Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2004a0c5b0cSMatthew G Knepley   }
2012c155ee1SBarry Smith   if (snes->usesksp) {
2022c155ee1SBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
203b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
20494b7f48cSBarry Smith     ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
205b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2062c155ee1SBarry Smith   }
2073a40ed3dSBarry Smith   PetscFunctionReturn(0);
2089b94acceSBarry Smith }
2099b94acceSBarry Smith 
21076b2cf59SMatthew Knepley /*
21176b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
21276b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
21376b2cf59SMatthew Knepley */
21476b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
215a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
2166849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
21776b2cf59SMatthew Knepley 
218e74ef692SMatthew Knepley #undef __FUNCT__
219e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
220ac226902SBarry Smith /*@C
22176b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
22276b2cf59SMatthew Knepley 
22376b2cf59SMatthew Knepley   Not Collective
22476b2cf59SMatthew Knepley 
22576b2cf59SMatthew Knepley   Input Parameter:
22676b2cf59SMatthew Knepley . snescheck - function that checks for options
22776b2cf59SMatthew Knepley 
22876b2cf59SMatthew Knepley   Level: developer
22976b2cf59SMatthew Knepley 
23076b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
23176b2cf59SMatthew Knepley @*/
2327087cfbeSBarry Smith PetscErrorCode  SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
23376b2cf59SMatthew Knepley {
23476b2cf59SMatthew Knepley   PetscFunctionBegin;
23576b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
236e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
23776b2cf59SMatthew Knepley   }
23876b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
23976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
24076b2cf59SMatthew Knepley }
24176b2cf59SMatthew Knepley 
2427087cfbeSBarry Smith extern PetscErrorCode  SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
243aa3661deSLisandro Dalcin 
244aa3661deSLisandro Dalcin #undef __FUNCT__
245aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private"
246ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool  hasOperator, PetscInt version)
247aa3661deSLisandro Dalcin {
248aa3661deSLisandro Dalcin   Mat            J;
249aa3661deSLisandro Dalcin   KSP            ksp;
250aa3661deSLisandro Dalcin   PC             pc;
251ace3abfcSBarry Smith   PetscBool      match;
252aa3661deSLisandro Dalcin   PetscErrorCode ierr;
253aa3661deSLisandro Dalcin 
254aa3661deSLisandro Dalcin   PetscFunctionBegin;
2550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
256aa3661deSLisandro Dalcin 
25798613b67SLisandro Dalcin   if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
25898613b67SLisandro Dalcin     Mat A = snes->jacobian, B = snes->jacobian_pre;
25998613b67SLisandro Dalcin     ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr);
26098613b67SLisandro Dalcin   }
26198613b67SLisandro Dalcin 
262aa3661deSLisandro Dalcin   if (version == 1) {
263aa3661deSLisandro Dalcin     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
26498613b67SLisandro Dalcin     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
2659c6ac3b3SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
266aa3661deSLisandro Dalcin   } else if (version == 2) {
267e32f2f54SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
26882a7e548SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
269aa3661deSLisandro Dalcin     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr);
270aa3661deSLisandro Dalcin #else
271e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
272aa3661deSLisandro Dalcin #endif
273a8248277SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2");
274aa3661deSLisandro Dalcin 
275aa3661deSLisandro Dalcin   ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr);
276d3462f78SMatthew Knepley   if (hasOperator) {
277aa3661deSLisandro Dalcin     /* This version replaces the user provided Jacobian matrix with a
278aa3661deSLisandro Dalcin        matrix-free version but still employs the user-provided preconditioner matrix. */
279aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
280aa3661deSLisandro Dalcin   } else {
281aa3661deSLisandro Dalcin     /* This version replaces both the user-provided Jacobian and the user-
282aa3661deSLisandro Dalcin        provided preconditioner matrix with the default matrix free version. */
283aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
284aa3661deSLisandro Dalcin     /* Force no preconditioner */
285aa3661deSLisandro Dalcin     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
286aa3661deSLisandro Dalcin     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
287aa3661deSLisandro Dalcin     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr);
288aa3661deSLisandro Dalcin     if (!match) {
289aa3661deSLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
290aa3661deSLisandro Dalcin       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
291aa3661deSLisandro Dalcin     }
292aa3661deSLisandro Dalcin   }
2936bf464f9SBarry Smith   ierr = MatDestroy(&J);CHKERRQ(ierr);
294aa3661deSLisandro Dalcin   PetscFunctionReturn(0);
295aa3661deSLisandro Dalcin }
296aa3661deSLisandro Dalcin 
2974a2ae208SSatish Balay #undef __FUNCT__
2984a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
2999b94acceSBarry Smith /*@
30094b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
3019b94acceSBarry Smith 
302c7afd0dbSLois Curfman McInnes    Collective on SNES
303c7afd0dbSLois Curfman McInnes 
3049b94acceSBarry Smith    Input Parameter:
3059b94acceSBarry Smith .  snes - the SNES context
3069b94acceSBarry Smith 
30736851e7fSLois Curfman McInnes    Options Database Keys:
308ea630c6eSPeter Brune +  -snes_type <type> - ls, tr, ngmres, ncg, richardson, qn, vi, fas
30982738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
31082738288SBarry Smith                 of the change in the solution between steps
31170441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
312b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
313b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
314b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
315*4839bfe8SBarry Smith .  -snes_max_fail <max_fail> - maximum number of line search failures allowed before stopping, default is none
316ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
317a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
318e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
319b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
3202492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
32182738288SBarry Smith                                solver; hence iterations will continue until max_it
3221fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
32382738288SBarry Smith                                of convergence test
324e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
325e8105e01SRichard Katz                                        filename given prints to stdout
326a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
327a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
328a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
329a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
330e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
3315968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
332fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
33382738288SBarry Smith 
33482738288SBarry Smith     Options Database for Eisenstat-Walker method:
335fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
3364b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
33736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
33836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
33936851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
34036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
34136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
34236851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
34382738288SBarry Smith 
34411ca99fdSLois Curfman McInnes    Notes:
34511ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
3460598bfebSBarry Smith    the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A>.
34783e2fdc7SBarry Smith 
34836851e7fSLois Curfman McInnes    Level: beginner
34936851e7fSLois Curfman McInnes 
3509b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
3519b94acceSBarry Smith 
35269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
3539b94acceSBarry Smith @*/
3547087cfbeSBarry Smith PetscErrorCode  SNESSetFromOptions(SNES snes)
3559b94acceSBarry Smith {
356ea630c6eSPeter Brune   PetscBool               flg,set,mf,mf_operator,pcset;
357efd51863SBarry Smith   PetscInt                i,indx,lag,mf_version,grids;
358aa3661deSLisandro Dalcin   MatStructure            matflag;
35985385478SLisandro Dalcin   const char              *deft = SNESLS;
36085385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
36185385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
362e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
36351e86f29SPeter Brune   const char              *optionsprefix;
364649052a6SBarry Smith   PetscViewer             monviewer;
36585385478SLisandro Dalcin   PetscErrorCode          ierr;
3669b94acceSBarry Smith 
3673a40ed3dSBarry Smith   PetscFunctionBegin;
3680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
369ca161407SBarry Smith 
370186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
3713194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)snes);CHKERRQ(ierr);
3727adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
373b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
374d64ed03dSBarry Smith     if (flg) {
375186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
3767adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
377186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
378d64ed03dSBarry Smith     }
37990d69ab7SBarry Smith     /* not used here, but called so will go into help messaage */
380909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
38193c39befSBarry Smith 
38257034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
38357034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
384186905e3SBarry Smith 
38557034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
386b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
387b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
38850ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
389ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
390acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr);
39185385478SLisandro Dalcin 
392a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
393a8054027SBarry Smith     if (flg) {
394a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
395a8054027SBarry Smith     }
396e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
397e35cf81dSBarry Smith     if (flg) {
398e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
399e35cf81dSBarry Smith     }
400efd51863SBarry Smith     ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr);
401efd51863SBarry Smith     if (flg) {
402efd51863SBarry Smith       ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr);
403efd51863SBarry Smith     }
404a8054027SBarry Smith 
40585385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
40685385478SLisandro Dalcin     if (flg) {
40785385478SLisandro Dalcin       switch (indx) {
4087f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
4097f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
41085385478SLisandro Dalcin       }
41185385478SLisandro Dalcin     }
41285385478SLisandro Dalcin 
413acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
414186905e3SBarry Smith 
41585385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
41685385478SLisandro Dalcin 
417acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
418186905e3SBarry Smith 
419fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
420fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
421fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
422fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
423fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
424fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
425fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
426186905e3SBarry Smith 
42790d69ab7SBarry Smith     flg  = PETSC_FALSE;
428acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
429a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
430eabae89aSBarry Smith 
431a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
432e8105e01SRichard Katz     if (flg) {
433649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
434649052a6SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
435e8105e01SRichard Katz     }
436eabae89aSBarry Smith 
437b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
438b271bb04SBarry Smith     if (flg) {
439b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
440b271bb04SBarry Smith     }
441b271bb04SBarry Smith 
442a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
443eabae89aSBarry Smith     if (flg) {
444649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
445f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
446e8105e01SRichard Katz     }
447eabae89aSBarry Smith 
448a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
449eabae89aSBarry Smith     if (flg) {
450649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
451649052a6SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
452eabae89aSBarry Smith     }
453eabae89aSBarry Smith 
4545180491cSLisandro Dalcin     ierr = PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
4555180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)snes,monfilename);CHKERRQ(ierr);}
4565180491cSLisandro Dalcin 
45790d69ab7SBarry Smith     flg  = PETSC_FALSE;
458acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
459a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
46090d69ab7SBarry Smith     flg  = PETSC_FALSE;
461acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
462a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
46390d69ab7SBarry Smith     flg  = PETSC_FALSE;
464acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
465a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
46690d69ab7SBarry Smith     flg  = PETSC_FALSE;
467acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
468a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
46990d69ab7SBarry Smith     flg  = PETSC_FALSE;
470acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
471b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
472e24b481bSBarry Smith 
47390d69ab7SBarry Smith     flg  = PETSC_FALSE;
474acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
4754b27c08aSLois Curfman McInnes     if (flg) {
476186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
477ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
4789b94acceSBarry Smith     }
479639f9d9dSBarry Smith 
480aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
481aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
482acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
483a8248277SBarry Smith     if (flg && mf_operator) {
484a8248277SBarry Smith       snes->mf_operator = PETSC_TRUE;
485a8248277SBarry Smith       mf = PETSC_TRUE;
486a8248277SBarry Smith     }
487aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
488acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
489aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
490aa3661deSLisandro Dalcin     mf_version = 1;
491aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
492aa3661deSLisandro Dalcin 
493ea630c6eSPeter Brune     /* line search options */
494ea630c6eSPeter Brune     ierr = PetscOptionsReal("-snes_ls_alpha","Constant function norm must decrease by","None",snes->ls_alpha,&snes->ls_alpha,0);CHKERRQ(ierr);
495ea630c6eSPeter Brune     ierr = PetscOptionsReal("-snes_ls_maxstep","Step must be less than","None",snes->maxstep,&snes->maxstep,0);CHKERRQ(ierr);
496ea630c6eSPeter Brune     ierr = PetscOptionsReal("-snes_ls_steptol","Minimum lambda allowed","None",snes->steptol,&snes->steptol,0);CHKERRQ(ierr);
497ea630c6eSPeter Brune     ierr = PetscOptionsReal("-snes_ls_damping","Damping parameter","SNES",snes->damping,&snes->damping,&flg);CHKERRQ(ierr);
498af60355fSPeter Brune     ierr = PetscOptionsInt("-snes_ls_it"      ,"Line search iterations","SNES",snes->ls_its,&snes->ls_its,&flg);CHKERRQ(ierr);
499ea630c6eSPeter Brune     ierr = PetscOptionsBool("-snes_ls_monitor","Print progress of line searches","SNESLineSearchSetMonitor",snes->ls_monitor ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr);
500ea630c6eSPeter Brune     if (set) {ierr = SNESLineSearchSetMonitor(snes,flg);CHKERRQ(ierr);}
50115f5eeeaSPeter Brune     ierr = PetscOptionsEnum("-snes_ls","Line search used","SNESLineSearchSet",SNESLineSearchTypes,(PetscEnum)snes->ls_type,(PetscEnum*)&indx,&flg);CHKERRQ(ierr);
50215f5eeeaSPeter Brune     if (flg) {
503ea630c6eSPeter Brune       ierr = SNESLineSearchSetType(snes,(SNESLineSearchType)indx);CHKERRQ(ierr);
50415f5eeeaSPeter Brune     }
5058e3fc8c0SJed Brown     flg = snes->ops->precheckstep == SNESLineSearchPreCheckPicard ? PETSC_TRUE : PETSC_FALSE;
5068e3fc8c0SJed Brown     ierr = PetscOptionsBool("-snes_ls_precheck_picard","Use a correction that sometimes improves convergence of Picard iteration","SNESLineSearchPreCheckPicard",flg,&flg,&set);CHKERRQ(ierr);
5078e3fc8c0SJed Brown     if (set) {
5088e3fc8c0SJed Brown       if (flg) {
5098e3fc8c0SJed Brown         snes->precheck_picard_angle = 10.; /* correction only active if angle is less than 10 degrees */
5108e3fc8c0SJed 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);
5118e3fc8c0SJed Brown         ierr = SNESLineSearchSetPreCheck(snes,SNESLineSearchPreCheckPicard,&snes->precheck_picard_angle);CHKERRQ(ierr);
5128e3fc8c0SJed Brown       } else {
5138e3fc8c0SJed Brown         ierr = SNESLineSearchSetPreCheck(snes,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
5148e3fc8c0SJed Brown       }
5158e3fc8c0SJed Brown     }
5168e3fc8c0SJed Brown 
51776b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
51876b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
51976b2cf59SMatthew Knepley     }
52076b2cf59SMatthew Knepley 
521e7788613SBarry Smith     if (snes->ops->setfromoptions) {
522e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
523639f9d9dSBarry Smith     }
5245d973c19SBarry Smith 
5255d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
5265d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr);
527b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
5284bbc92c1SBarry Smith 
529aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
5301cee3971SBarry Smith 
5311cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
532aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
533aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
53485385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
53593993e2dSLois Curfman McInnes 
53651e86f29SPeter Brune   /* if someone has set the SNES PC type, create it. */
53751e86f29SPeter Brune   ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr);
53851e86f29SPeter Brune   ierr = PetscOptionsHasName(optionsprefix, "-npc_snes_type", &pcset);CHKERRQ(ierr);
53951e86f29SPeter Brune   if (pcset && (!snes->pc)) {
54051e86f29SPeter Brune     ierr = SNESGetPC(snes, &snes->pc);CHKERRQ(ierr);
54151e86f29SPeter Brune   }
54251e86f29SPeter Brune 
5434a0c5b0cSMatthew G Knepley   if (snes->pc) {
5444a0c5b0cSMatthew G Knepley     ierr = SNESSetOptionsPrefix(snes->pc, "npc_");CHKERRQ(ierr);
5454a0c5b0cSMatthew G Knepley     ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr);
546646217ecSPeter Brune     ierr = SNESSetGS(snes->pc, snes->ops->computegs, snes->gsP);CHKERRQ(ierr);
5474a0c5b0cSMatthew G Knepley     /* Should we make a duplicate vector and matrix? Leave the DM to make it? */
5484a0c5b0cSMatthew G Knepley     ierr = SNESSetFunction(snes->pc, snes->vec_func, snes->ops->computefunction, snes->funP);CHKERRQ(ierr);
5494a0c5b0cSMatthew G Knepley     ierr = SNESSetJacobian(snes->pc, snes->jacobian, snes->jacobian_pre, snes->ops->computejacobian, snes->jacP);CHKERRQ(ierr);
5504a0c5b0cSMatthew G Knepley     ierr = SNESSetFromOptions(snes->pc);CHKERRQ(ierr);
5514a0c5b0cSMatthew G Knepley   }
5523a40ed3dSBarry Smith   PetscFunctionReturn(0);
5539b94acceSBarry Smith }
5549b94acceSBarry Smith 
555d25893d9SBarry Smith #undef __FUNCT__
556d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext"
557d25893d9SBarry Smith /*@
558d25893d9SBarry Smith    SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for
559d25893d9SBarry Smith    the nonlinear solvers.
560d25893d9SBarry Smith 
561d25893d9SBarry Smith    Logically Collective on SNES
562d25893d9SBarry Smith 
563d25893d9SBarry Smith    Input Parameters:
564d25893d9SBarry Smith +  snes - the SNES context
565d25893d9SBarry Smith .  compute - function to compute the context
566d25893d9SBarry Smith -  destroy - function to destroy the context
567d25893d9SBarry Smith 
568d25893d9SBarry Smith    Level: intermediate
569d25893d9SBarry Smith 
570d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context
571d25893d9SBarry Smith 
572d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext()
573d25893d9SBarry Smith @*/
574d25893d9SBarry Smith PetscErrorCode  SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**))
575d25893d9SBarry Smith {
576d25893d9SBarry Smith   PetscFunctionBegin;
577d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
578d25893d9SBarry Smith   snes->ops->usercompute = compute;
579d25893d9SBarry Smith   snes->ops->userdestroy = destroy;
580d25893d9SBarry Smith   PetscFunctionReturn(0);
581d25893d9SBarry Smith }
582a847f771SSatish Balay 
5834a2ae208SSatish Balay #undef __FUNCT__
5844a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
585b07ff414SBarry Smith /*@
5869b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
5879b94acceSBarry Smith    the nonlinear solvers.
5889b94acceSBarry Smith 
5893f9fe445SBarry Smith    Logically Collective on SNES
590fee21e36SBarry Smith 
591c7afd0dbSLois Curfman McInnes    Input Parameters:
592c7afd0dbSLois Curfman McInnes +  snes - the SNES context
593c7afd0dbSLois Curfman McInnes -  usrP - optional user context
594c7afd0dbSLois Curfman McInnes 
59536851e7fSLois Curfman McInnes    Level: intermediate
59636851e7fSLois Curfman McInnes 
5979b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
5989b94acceSBarry Smith 
599d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext()
6009b94acceSBarry Smith @*/
6017087cfbeSBarry Smith PetscErrorCode  SNESSetApplicationContext(SNES snes,void *usrP)
6029b94acceSBarry Smith {
6031b2093e4SBarry Smith   PetscErrorCode ierr;
604b07ff414SBarry Smith   KSP            ksp;
6051b2093e4SBarry Smith 
6063a40ed3dSBarry Smith   PetscFunctionBegin;
6070700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
608b07ff414SBarry Smith   ierr       = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
609b07ff414SBarry Smith   ierr       = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr);
6109b94acceSBarry Smith   snes->user = usrP;
6113a40ed3dSBarry Smith   PetscFunctionReturn(0);
6129b94acceSBarry Smith }
61374679c65SBarry Smith 
6144a2ae208SSatish Balay #undef __FUNCT__
6154a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
616b07ff414SBarry Smith /*@
6179b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
6189b94acceSBarry Smith    nonlinear solvers.
6199b94acceSBarry Smith 
620c7afd0dbSLois Curfman McInnes    Not Collective
621c7afd0dbSLois Curfman McInnes 
6229b94acceSBarry Smith    Input Parameter:
6239b94acceSBarry Smith .  snes - SNES context
6249b94acceSBarry Smith 
6259b94acceSBarry Smith    Output Parameter:
6269b94acceSBarry Smith .  usrP - user context
6279b94acceSBarry Smith 
62836851e7fSLois Curfman McInnes    Level: intermediate
62936851e7fSLois Curfman McInnes 
6309b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
6319b94acceSBarry Smith 
6329b94acceSBarry Smith .seealso: SNESSetApplicationContext()
6339b94acceSBarry Smith @*/
634e71120c6SJed Brown PetscErrorCode  SNESGetApplicationContext(SNES snes,void *usrP)
6359b94acceSBarry Smith {
6363a40ed3dSBarry Smith   PetscFunctionBegin;
6370700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
638e71120c6SJed Brown   *(void**)usrP = snes->user;
6393a40ed3dSBarry Smith   PetscFunctionReturn(0);
6409b94acceSBarry Smith }
64174679c65SBarry Smith 
6424a2ae208SSatish Balay #undef __FUNCT__
6434a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
6449b94acceSBarry Smith /*@
645c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
646c8228a4eSBarry Smith    at this time.
6479b94acceSBarry Smith 
648c7afd0dbSLois Curfman McInnes    Not Collective
649c7afd0dbSLois Curfman McInnes 
6509b94acceSBarry Smith    Input Parameter:
6519b94acceSBarry Smith .  snes - SNES context
6529b94acceSBarry Smith 
6539b94acceSBarry Smith    Output Parameter:
6549b94acceSBarry Smith .  iter - iteration number
6559b94acceSBarry Smith 
656c8228a4eSBarry Smith    Notes:
657c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
658c8228a4eSBarry Smith 
659c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
66008405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
66108405cd6SLois Curfman McInnes .vb
66208405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
66308405cd6SLois Curfman McInnes       if (!(it % 2)) {
66408405cd6SLois Curfman McInnes         [compute Jacobian here]
66508405cd6SLois Curfman McInnes       }
66608405cd6SLois Curfman McInnes .ve
667c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
66808405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
669c8228a4eSBarry Smith 
67036851e7fSLois Curfman McInnes    Level: intermediate
67136851e7fSLois Curfman McInnes 
6722b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
6732b668275SBarry Smith 
674b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
6759b94acceSBarry Smith @*/
6767087cfbeSBarry Smith PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt* iter)
6779b94acceSBarry Smith {
6783a40ed3dSBarry Smith   PetscFunctionBegin;
6790700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6804482741eSBarry Smith   PetscValidIntPointer(iter,2);
6819b94acceSBarry Smith   *iter = snes->iter;
6823a40ed3dSBarry Smith   PetscFunctionReturn(0);
6839b94acceSBarry Smith }
68474679c65SBarry Smith 
6854a2ae208SSatish Balay #undef __FUNCT__
6864a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
6879b94acceSBarry Smith /*@
6889b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
6899b94acceSBarry Smith    with SNESSSetFunction().
6909b94acceSBarry Smith 
691c7afd0dbSLois Curfman McInnes    Collective on SNES
692c7afd0dbSLois Curfman McInnes 
6939b94acceSBarry Smith    Input Parameter:
6949b94acceSBarry Smith .  snes - SNES context
6959b94acceSBarry Smith 
6969b94acceSBarry Smith    Output Parameter:
6979b94acceSBarry Smith .  fnorm - 2-norm of function
6989b94acceSBarry Smith 
69936851e7fSLois Curfman McInnes    Level: intermediate
70036851e7fSLois Curfman McInnes 
7019b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
702a86d99e1SLois Curfman McInnes 
703b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
7049b94acceSBarry Smith @*/
7057087cfbeSBarry Smith PetscErrorCode  SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
7069b94acceSBarry Smith {
7073a40ed3dSBarry Smith   PetscFunctionBegin;
7080700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7094482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
7109b94acceSBarry Smith   *fnorm = snes->norm;
7113a40ed3dSBarry Smith   PetscFunctionReturn(0);
7129b94acceSBarry Smith }
71374679c65SBarry Smith 
7144a2ae208SSatish Balay #undef __FUNCT__
715b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
7169b94acceSBarry Smith /*@
717b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
7189b94acceSBarry Smith    attempted by the nonlinear solver.
7199b94acceSBarry Smith 
720c7afd0dbSLois Curfman McInnes    Not Collective
721c7afd0dbSLois Curfman McInnes 
7229b94acceSBarry Smith    Input Parameter:
7239b94acceSBarry Smith .  snes - SNES context
7249b94acceSBarry Smith 
7259b94acceSBarry Smith    Output Parameter:
7269b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
7279b94acceSBarry Smith 
728c96a6f78SLois Curfman McInnes    Notes:
729c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
730c96a6f78SLois Curfman McInnes 
73136851e7fSLois Curfman McInnes    Level: intermediate
73236851e7fSLois Curfman McInnes 
7339b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
73458ebbce7SBarry Smith 
735e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
73658ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
7379b94acceSBarry Smith @*/
7387087cfbeSBarry Smith PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
7399b94acceSBarry Smith {
7403a40ed3dSBarry Smith   PetscFunctionBegin;
7410700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7424482741eSBarry Smith   PetscValidIntPointer(nfails,2);
74350ffb88aSMatthew Knepley   *nfails = snes->numFailures;
74450ffb88aSMatthew Knepley   PetscFunctionReturn(0);
74550ffb88aSMatthew Knepley }
74650ffb88aSMatthew Knepley 
74750ffb88aSMatthew Knepley #undef __FUNCT__
748b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
74950ffb88aSMatthew Knepley /*@
750b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
75150ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
75250ffb88aSMatthew Knepley 
75350ffb88aSMatthew Knepley    Not Collective
75450ffb88aSMatthew Knepley 
75550ffb88aSMatthew Knepley    Input Parameters:
75650ffb88aSMatthew Knepley +  snes     - SNES context
75750ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
75850ffb88aSMatthew Knepley 
75950ffb88aSMatthew Knepley    Level: intermediate
76050ffb88aSMatthew Knepley 
76150ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
76258ebbce7SBarry Smith 
763e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
76458ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
76550ffb88aSMatthew Knepley @*/
7667087cfbeSBarry Smith PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
76750ffb88aSMatthew Knepley {
76850ffb88aSMatthew Knepley   PetscFunctionBegin;
7690700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
77050ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
77150ffb88aSMatthew Knepley   PetscFunctionReturn(0);
77250ffb88aSMatthew Knepley }
77350ffb88aSMatthew Knepley 
77450ffb88aSMatthew Knepley #undef __FUNCT__
775b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
77650ffb88aSMatthew Knepley /*@
777b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
77850ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
77950ffb88aSMatthew Knepley 
78050ffb88aSMatthew Knepley    Not Collective
78150ffb88aSMatthew Knepley 
78250ffb88aSMatthew Knepley    Input Parameter:
78350ffb88aSMatthew Knepley .  snes     - SNES context
78450ffb88aSMatthew Knepley 
78550ffb88aSMatthew Knepley    Output Parameter:
78650ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
78750ffb88aSMatthew Knepley 
78850ffb88aSMatthew Knepley    Level: intermediate
78950ffb88aSMatthew Knepley 
79050ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
79158ebbce7SBarry Smith 
792e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
79358ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
79458ebbce7SBarry Smith 
79550ffb88aSMatthew Knepley @*/
7967087cfbeSBarry Smith PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
79750ffb88aSMatthew Knepley {
79850ffb88aSMatthew Knepley   PetscFunctionBegin;
7990700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8004482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
80150ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
8023a40ed3dSBarry Smith   PetscFunctionReturn(0);
8039b94acceSBarry Smith }
804a847f771SSatish Balay 
8054a2ae208SSatish Balay #undef __FUNCT__
8062541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
8072541af92SBarry Smith /*@
8082541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
8092541af92SBarry Smith      done by SNES.
8102541af92SBarry Smith 
8112541af92SBarry Smith    Not Collective
8122541af92SBarry Smith 
8132541af92SBarry Smith    Input Parameter:
8142541af92SBarry Smith .  snes     - SNES context
8152541af92SBarry Smith 
8162541af92SBarry Smith    Output Parameter:
8172541af92SBarry Smith .  nfuncs - number of evaluations
8182541af92SBarry Smith 
8192541af92SBarry Smith    Level: intermediate
8202541af92SBarry Smith 
8212541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
82258ebbce7SBarry Smith 
823e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
8242541af92SBarry Smith @*/
8257087cfbeSBarry Smith PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
8262541af92SBarry Smith {
8272541af92SBarry Smith   PetscFunctionBegin;
8280700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8292541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
8302541af92SBarry Smith   *nfuncs = snes->nfuncs;
8312541af92SBarry Smith   PetscFunctionReturn(0);
8322541af92SBarry Smith }
8332541af92SBarry Smith 
8342541af92SBarry Smith #undef __FUNCT__
8353d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
8363d4c4710SBarry Smith /*@
8373d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
8383d4c4710SBarry Smith    linear solvers.
8393d4c4710SBarry Smith 
8403d4c4710SBarry Smith    Not Collective
8413d4c4710SBarry Smith 
8423d4c4710SBarry Smith    Input Parameter:
8433d4c4710SBarry Smith .  snes - SNES context
8443d4c4710SBarry Smith 
8453d4c4710SBarry Smith    Output Parameter:
8463d4c4710SBarry Smith .  nfails - number of failed solves
8473d4c4710SBarry Smith 
8483d4c4710SBarry Smith    Notes:
8493d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
8503d4c4710SBarry Smith 
8513d4c4710SBarry Smith    Level: intermediate
8523d4c4710SBarry Smith 
8533d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
85458ebbce7SBarry Smith 
855e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
8563d4c4710SBarry Smith @*/
8577087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
8583d4c4710SBarry Smith {
8593d4c4710SBarry Smith   PetscFunctionBegin;
8600700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8613d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
8623d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
8633d4c4710SBarry Smith   PetscFunctionReturn(0);
8643d4c4710SBarry Smith }
8653d4c4710SBarry Smith 
8663d4c4710SBarry Smith #undef __FUNCT__
8673d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
8683d4c4710SBarry Smith /*@
8693d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
8703d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
8713d4c4710SBarry Smith 
8723f9fe445SBarry Smith    Logically Collective on SNES
8733d4c4710SBarry Smith 
8743d4c4710SBarry Smith    Input Parameters:
8753d4c4710SBarry Smith +  snes     - SNES context
8763d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
8773d4c4710SBarry Smith 
8783d4c4710SBarry Smith    Level: intermediate
8793d4c4710SBarry Smith 
880a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
8813d4c4710SBarry Smith 
8823d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
8833d4c4710SBarry Smith 
88458ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
8853d4c4710SBarry Smith @*/
8867087cfbeSBarry Smith PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
8873d4c4710SBarry Smith {
8883d4c4710SBarry Smith   PetscFunctionBegin;
8890700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
890c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
8913d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
8923d4c4710SBarry Smith   PetscFunctionReturn(0);
8933d4c4710SBarry Smith }
8943d4c4710SBarry Smith 
8953d4c4710SBarry Smith #undef __FUNCT__
8963d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
8973d4c4710SBarry Smith /*@
8983d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
8993d4c4710SBarry Smith      are allowed before SNES terminates
9003d4c4710SBarry Smith 
9013d4c4710SBarry Smith    Not Collective
9023d4c4710SBarry Smith 
9033d4c4710SBarry Smith    Input Parameter:
9043d4c4710SBarry Smith .  snes     - SNES context
9053d4c4710SBarry Smith 
9063d4c4710SBarry Smith    Output Parameter:
9073d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
9083d4c4710SBarry Smith 
9093d4c4710SBarry Smith    Level: intermediate
9103d4c4710SBarry Smith 
9113d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
9123d4c4710SBarry Smith 
9133d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
9143d4c4710SBarry Smith 
915e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
9163d4c4710SBarry Smith @*/
9177087cfbeSBarry Smith PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
9183d4c4710SBarry Smith {
9193d4c4710SBarry Smith   PetscFunctionBegin;
9200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9213d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
9223d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
9233d4c4710SBarry Smith   PetscFunctionReturn(0);
9243d4c4710SBarry Smith }
9253d4c4710SBarry Smith 
9263d4c4710SBarry Smith #undef __FUNCT__
927b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
928c96a6f78SLois Curfman McInnes /*@
929b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
930c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
931c96a6f78SLois Curfman McInnes 
932c7afd0dbSLois Curfman McInnes    Not Collective
933c7afd0dbSLois Curfman McInnes 
934c96a6f78SLois Curfman McInnes    Input Parameter:
935c96a6f78SLois Curfman McInnes .  snes - SNES context
936c96a6f78SLois Curfman McInnes 
937c96a6f78SLois Curfman McInnes    Output Parameter:
938c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
939c96a6f78SLois Curfman McInnes 
940c96a6f78SLois Curfman McInnes    Notes:
941c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
942c96a6f78SLois Curfman McInnes 
94336851e7fSLois Curfman McInnes    Level: intermediate
94436851e7fSLois Curfman McInnes 
945c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
9462b668275SBarry Smith 
9478c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
948c96a6f78SLois Curfman McInnes @*/
9497087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
950c96a6f78SLois Curfman McInnes {
9513a40ed3dSBarry Smith   PetscFunctionBegin;
9520700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9534482741eSBarry Smith   PetscValidIntPointer(lits,2);
954c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
9553a40ed3dSBarry Smith   PetscFunctionReturn(0);
956c96a6f78SLois Curfman McInnes }
957c96a6f78SLois Curfman McInnes 
9584a2ae208SSatish Balay #undef __FUNCT__
95994b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
96052baeb72SSatish Balay /*@
96194b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
9629b94acceSBarry Smith 
96394b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
964c7afd0dbSLois Curfman McInnes 
9659b94acceSBarry Smith    Input Parameter:
9669b94acceSBarry Smith .  snes - the SNES context
9679b94acceSBarry Smith 
9689b94acceSBarry Smith    Output Parameter:
96994b7f48cSBarry Smith .  ksp - the KSP context
9709b94acceSBarry Smith 
9719b94acceSBarry Smith    Notes:
97294b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
9739b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
9742999313aSBarry Smith    PC contexts as well.
9759b94acceSBarry Smith 
97636851e7fSLois Curfman McInnes    Level: beginner
97736851e7fSLois Curfman McInnes 
97894b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9799b94acceSBarry Smith 
9802999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9819b94acceSBarry Smith @*/
9827087cfbeSBarry Smith PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
9839b94acceSBarry Smith {
9841cee3971SBarry Smith   PetscErrorCode ierr;
9851cee3971SBarry Smith 
9863a40ed3dSBarry Smith   PetscFunctionBegin;
9870700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9884482741eSBarry Smith   PetscValidPointer(ksp,2);
9891cee3971SBarry Smith 
9901cee3971SBarry Smith   if (!snes->ksp) {
9911cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
9921cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
9931cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
9941cee3971SBarry Smith   }
99594b7f48cSBarry Smith   *ksp = snes->ksp;
9963a40ed3dSBarry Smith   PetscFunctionReturn(0);
9979b94acceSBarry Smith }
99882bf6240SBarry Smith 
9994a2ae208SSatish Balay #undef __FUNCT__
10002999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
10012999313aSBarry Smith /*@
10022999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
10032999313aSBarry Smith 
10042999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
10052999313aSBarry Smith 
10062999313aSBarry Smith    Input Parameters:
10072999313aSBarry Smith +  snes - the SNES context
10082999313aSBarry Smith -  ksp - the KSP context
10092999313aSBarry Smith 
10102999313aSBarry Smith    Notes:
10112999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
10122999313aSBarry Smith    so this routine is rarely needed.
10132999313aSBarry Smith 
10142999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
10152999313aSBarry Smith    decreased by one.
10162999313aSBarry Smith 
10172999313aSBarry Smith    Level: developer
10182999313aSBarry Smith 
10192999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
10202999313aSBarry Smith 
10212999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
10222999313aSBarry Smith @*/
10237087cfbeSBarry Smith PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
10242999313aSBarry Smith {
10252999313aSBarry Smith   PetscErrorCode ierr;
10262999313aSBarry Smith 
10272999313aSBarry Smith   PetscFunctionBegin;
10280700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
10290700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
10302999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
10317dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
1032906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
10332999313aSBarry Smith   snes->ksp = ksp;
10342999313aSBarry Smith   PetscFunctionReturn(0);
10352999313aSBarry Smith }
10362999313aSBarry Smith 
10377adad957SLisandro Dalcin #if 0
10382999313aSBarry Smith #undef __FUNCT__
10394a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
10406849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
1041e24b481bSBarry Smith {
1042e24b481bSBarry Smith   PetscFunctionBegin;
1043e24b481bSBarry Smith   PetscFunctionReturn(0);
1044e24b481bSBarry Smith }
10457adad957SLisandro Dalcin #endif
1046e24b481bSBarry Smith 
10479b94acceSBarry Smith /* -----------------------------------------------------------*/
10484a2ae208SSatish Balay #undef __FUNCT__
10494a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
105052baeb72SSatish Balay /*@
10519b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
10529b94acceSBarry Smith 
1053c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
1054c7afd0dbSLois Curfman McInnes 
1055c7afd0dbSLois Curfman McInnes    Input Parameters:
1056906ed7ccSBarry Smith .  comm - MPI communicator
10579b94acceSBarry Smith 
10589b94acceSBarry Smith    Output Parameter:
10599b94acceSBarry Smith .  outsnes - the new SNES context
10609b94acceSBarry Smith 
1061c7afd0dbSLois Curfman McInnes    Options Database Keys:
1062c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
1063c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
1064c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
1065c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
1066c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
1067c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
1068c1f60f51SBarry Smith 
106936851e7fSLois Curfman McInnes    Level: beginner
107036851e7fSLois Curfman McInnes 
10719b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
10729b94acceSBarry Smith 
1073a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
1074a8054027SBarry Smith 
10759b94acceSBarry Smith @*/
10767087cfbeSBarry Smith PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
10779b94acceSBarry Smith {
1078dfbe8321SBarry Smith   PetscErrorCode      ierr;
10799b94acceSBarry Smith   SNES                snes;
1080fa9f3622SBarry Smith   SNESKSPEW           *kctx;
108137fcc0dbSBarry Smith 
10823a40ed3dSBarry Smith   PetscFunctionBegin;
1083ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
10848ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
10858ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
10868ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
10878ba1e511SMatthew Knepley #endif
10888ba1e511SMatthew Knepley 
10893194b578SJed Brown   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
10907adad957SLisandro Dalcin 
109185385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
10922c155ee1SBarry Smith   snes->usesksp           = PETSC_TRUE;
10939b94acceSBarry Smith   snes->max_its           = 50;
10949750a799SBarry Smith   snes->max_funcs	  = 10000;
10959b94acceSBarry Smith   snes->norm		  = 0.0;
1096b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
1097b4874afaSBarry Smith   snes->ttol              = 0.0;
109870441072SBarry Smith   snes->abstol		  = 1.e-50;
10999b94acceSBarry Smith   snes->xtol		  = 1.e-8;
11004b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
11019b94acceSBarry Smith   snes->nfuncs            = 0;
110250ffb88aSMatthew Knepley   snes->numFailures       = 0;
110350ffb88aSMatthew Knepley   snes->maxFailures       = 1;
11047a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1105e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1106a8054027SBarry Smith   snes->lagpreconditioner = 1;
1107639f9d9dSBarry Smith   snes->numbermonitors    = 0;
11089b94acceSBarry Smith   snes->data              = 0;
11094dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1110186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
11116f24a144SLois Curfman McInnes   snes->nwork             = 0;
111258c9b817SLisandro Dalcin   snes->work              = 0;
111358c9b817SLisandro Dalcin   snes->nvwork            = 0;
111458c9b817SLisandro Dalcin   snes->vwork             = 0;
1115758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1116758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1117758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1118758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1119758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1120184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
11219b94acceSBarry Smith 
1122ea630c6eSPeter Brune   /* initialize the line search options */
1123ea630c6eSPeter Brune   snes->ls_type           = SNES_LS_BASIC;
1124af60355fSPeter Brune   snes->ls_its            = 1;
1125ea630c6eSPeter Brune   snes->damping           = 1.0;
1126ea630c6eSPeter Brune   snes->maxstep           = 1e8;
1127ea630c6eSPeter Brune   snes->steptol           = 1e-12;
1128ea630c6eSPeter Brune   snes->ls_alpha          = 1e-4;
1129ea630c6eSPeter Brune   snes->ls_monitor        = PETSC_NULL;
1130ea630c6eSPeter Brune 
1131ea630c6eSPeter Brune   snes->ops->linesearch   = PETSC_NULL;
1132ea630c6eSPeter Brune   snes->precheck          = PETSC_NULL;
1133ea630c6eSPeter Brune   snes->ops->precheckstep = PETSC_NULL;
1134ea630c6eSPeter Brune   snes->postcheck         = PETSC_NULL;
1135ea630c6eSPeter Brune   snes->ops->postcheckstep= PETSC_NULL;
1136ea630c6eSPeter Brune 
11373d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
11383d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
11393d4c4710SBarry Smith 
11409b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
114138f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
11429b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
11439b94acceSBarry Smith   kctx->version     = 2;
11449b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
11459b94acceSBarry Smith                              this was too large for some test cases */
114675567043SBarry Smith   kctx->rtol_last   = 0.0;
11479b94acceSBarry Smith   kctx->rtol_max    = .9;
11489b94acceSBarry Smith   kctx->gamma       = 1.0;
114962d1f40fSBarry Smith   kctx->alpha       = .5*(1.0 + PetscSqrtReal(5.0));
115071f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
11519b94acceSBarry Smith   kctx->threshold   = .1;
115275567043SBarry Smith   kctx->lresid_last = 0.0;
115375567043SBarry Smith   kctx->norm_last   = 0.0;
11549b94acceSBarry Smith 
11559b94acceSBarry Smith   *outsnes = snes;
11563a40ed3dSBarry Smith   PetscFunctionReturn(0);
11579b94acceSBarry Smith }
11589b94acceSBarry Smith 
11594a2ae208SSatish Balay #undef __FUNCT__
11604a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
11619b94acceSBarry Smith /*@C
11629b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
11639b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
11649b94acceSBarry Smith    equations.
11659b94acceSBarry Smith 
11663f9fe445SBarry Smith    Logically Collective on SNES
1167fee21e36SBarry Smith 
1168c7afd0dbSLois Curfman McInnes    Input Parameters:
1169c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1170c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1171de044059SHong Zhang .  func - function evaluation routine
1172c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1173c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
11749b94acceSBarry Smith 
1175c7afd0dbSLois Curfman McInnes    Calling sequence of func:
11768d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1177c7afd0dbSLois Curfman McInnes 
1178313e4042SLois Curfman McInnes .  f - function vector
1179c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
11809b94acceSBarry Smith 
11819b94acceSBarry Smith    Notes:
11829b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
11839b94acceSBarry Smith $      f'(x) x = -f(x),
1184c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
11859b94acceSBarry Smith 
118636851e7fSLois Curfman McInnes    Level: beginner
118736851e7fSLois Curfman McInnes 
11889b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
11899b94acceSBarry Smith 
1190a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
11919b94acceSBarry Smith @*/
11927087cfbeSBarry Smith PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
11939b94acceSBarry Smith {
119485385478SLisandro Dalcin   PetscErrorCode ierr;
11953a40ed3dSBarry Smith   PetscFunctionBegin;
11960700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1197d2a683ecSLisandro Dalcin   if (r) {
1198d2a683ecSLisandro Dalcin     PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1199d2a683ecSLisandro Dalcin     PetscCheckSameComm(snes,1,r,2);
120085385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
12016bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
120285385478SLisandro Dalcin     snes->vec_func = r;
1203d2a683ecSLisandro Dalcin   } else if (!snes->vec_func && snes->dm) {
1204d2a683ecSLisandro Dalcin     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1205d2a683ecSLisandro Dalcin   }
1206d2a683ecSLisandro Dalcin   if (func) snes->ops->computefunction = func;
1207d2a683ecSLisandro Dalcin   if (ctx)  snes->funP                 = ctx;
12083a40ed3dSBarry Smith   PetscFunctionReturn(0);
12099b94acceSBarry Smith }
12109b94acceSBarry Smith 
1211646217ecSPeter Brune 
1212646217ecSPeter Brune #undef __FUNCT__
1213646217ecSPeter Brune #define __FUNCT__ "SNESSetGS"
1214c79ef259SPeter Brune /*@C
1215c79ef259SPeter Brune    SNESSetGS - Sets the user nonlinear Gauss-Seidel routine for
1216c79ef259SPeter Brune    use with composed nonlinear solvers.
1217c79ef259SPeter Brune 
1218c79ef259SPeter Brune    Input Parameters:
1219c79ef259SPeter Brune +  snes   - the SNES context
1220c79ef259SPeter Brune .  gsfunc - function evaluation routine
1221c79ef259SPeter Brune -  ctx    - [optional] user-defined context for private data for the
1222c79ef259SPeter Brune             smoother evaluation routine (may be PETSC_NULL)
1223c79ef259SPeter Brune 
1224c79ef259SPeter Brune    Calling sequence of func:
1225c79ef259SPeter Brune $    func (SNES snes,Vec x,Vec b,void *ctx);
1226c79ef259SPeter Brune 
1227c79ef259SPeter Brune +  X   - solution vector
1228c79ef259SPeter Brune .  B   - RHS vector
1229c79ef259SPeter Brune -  ctx - optional user-defined function context
1230c79ef259SPeter Brune 
1231c79ef259SPeter Brune    Notes:
1232c79ef259SPeter Brune    The GS routines are used by the composed nonlinear solver to generate
1233c79ef259SPeter Brune     a problem appropriate update to the solution, particularly FAS.
1234c79ef259SPeter Brune 
1235c79ef259SPeter Brune    Level: beginner
1236c79ef259SPeter Brune 
1237c79ef259SPeter Brune .keywords: SNES, nonlinear, set, function
1238c79ef259SPeter Brune 
1239c79ef259SPeter Brune .seealso: SNESGetFunction(), SNESComputeGS()
1240c79ef259SPeter Brune @*/
1241646217ecSPeter Brune PetscErrorCode SNESSetGS(SNES snes, PetscErrorCode (*gsfunc)(SNES,Vec,Vec,void *), void * ctx) {
1242646217ecSPeter Brune   PetscFunctionBegin;
1243646217ecSPeter Brune   if (gsfunc) snes->ops->computegs = gsfunc;
1244646217ecSPeter Brune   if (ctx) snes->gsP = ctx;
1245646217ecSPeter Brune   PetscFunctionReturn(0);
1246646217ecSPeter Brune }
1247646217ecSPeter Brune 
1248d25893d9SBarry Smith #undef __FUNCT__
1249d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess"
1250d25893d9SBarry Smith /*@C
1251d25893d9SBarry Smith    SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem
1252d25893d9SBarry Smith 
1253d25893d9SBarry Smith    Logically Collective on SNES
1254d25893d9SBarry Smith 
1255d25893d9SBarry Smith    Input Parameters:
1256d25893d9SBarry Smith +  snes - the SNES context
1257d25893d9SBarry Smith .  func - function evaluation routine
1258d25893d9SBarry Smith -  ctx - [optional] user-defined context for private data for the
1259d25893d9SBarry Smith          function evaluation routine (may be PETSC_NULL)
1260d25893d9SBarry Smith 
1261d25893d9SBarry Smith    Calling sequence of func:
1262d25893d9SBarry Smith $    func (SNES snes,Vec x,void *ctx);
1263d25893d9SBarry Smith 
1264d25893d9SBarry Smith .  f - function vector
1265d25893d9SBarry Smith -  ctx - optional user-defined function context
1266d25893d9SBarry Smith 
1267d25893d9SBarry Smith    Level: intermediate
1268d25893d9SBarry Smith 
1269d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function
1270d25893d9SBarry Smith 
1271d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
1272d25893d9SBarry Smith @*/
1273d25893d9SBarry Smith PetscErrorCode  SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx)
1274d25893d9SBarry Smith {
1275d25893d9SBarry Smith   PetscFunctionBegin;
1276d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1277d25893d9SBarry Smith   if (func) snes->ops->computeinitialguess = func;
1278d25893d9SBarry Smith   if (ctx)  snes->initialguessP            = ctx;
1279d25893d9SBarry Smith   PetscFunctionReturn(0);
1280d25893d9SBarry Smith }
1281d25893d9SBarry Smith 
12823ab0aad5SBarry Smith /* --------------------------------------------------------------- */
12833ab0aad5SBarry Smith #undef __FUNCT__
12841096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
12851096aae1SMatthew Knepley /*@C
12861096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
12871096aae1SMatthew Knepley    it assumes a zero right hand side.
12881096aae1SMatthew Knepley 
12893f9fe445SBarry Smith    Logically Collective on SNES
12901096aae1SMatthew Knepley 
12911096aae1SMatthew Knepley    Input Parameter:
12921096aae1SMatthew Knepley .  snes - the SNES context
12931096aae1SMatthew Knepley 
12941096aae1SMatthew Knepley    Output Parameter:
1295bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
12961096aae1SMatthew Knepley 
12971096aae1SMatthew Knepley    Level: intermediate
12981096aae1SMatthew Knepley 
12991096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
13001096aae1SMatthew Knepley 
130185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
13021096aae1SMatthew Knepley @*/
13037087cfbeSBarry Smith PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
13041096aae1SMatthew Knepley {
13051096aae1SMatthew Knepley   PetscFunctionBegin;
13060700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13071096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
130885385478SLisandro Dalcin   *rhs = snes->vec_rhs;
13091096aae1SMatthew Knepley   PetscFunctionReturn(0);
13101096aae1SMatthew Knepley }
13111096aae1SMatthew Knepley 
13121096aae1SMatthew Knepley #undef __FUNCT__
13134a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
13149b94acceSBarry Smith /*@
131536851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
13169b94acceSBarry Smith                          SNESSetFunction().
13179b94acceSBarry Smith 
1318c7afd0dbSLois Curfman McInnes    Collective on SNES
1319c7afd0dbSLois Curfman McInnes 
13209b94acceSBarry Smith    Input Parameters:
1321c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1322c7afd0dbSLois Curfman McInnes -  x - input vector
13239b94acceSBarry Smith 
13249b94acceSBarry Smith    Output Parameter:
13253638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
13269b94acceSBarry Smith 
13271bffabb2SLois Curfman McInnes    Notes:
132836851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
132936851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
133036851e7fSLois Curfman McInnes    themselves.
133136851e7fSLois Curfman McInnes 
133236851e7fSLois Curfman McInnes    Level: developer
133336851e7fSLois Curfman McInnes 
13349b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
13359b94acceSBarry Smith 
1336a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
13379b94acceSBarry Smith @*/
13387087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
13399b94acceSBarry Smith {
1340dfbe8321SBarry Smith   PetscErrorCode ierr;
13419b94acceSBarry Smith 
13423a40ed3dSBarry Smith   PetscFunctionBegin;
13430700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13440700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
13450700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1346c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1347c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1348184914b5SBarry Smith 
1349d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1350e7788613SBarry Smith   if (snes->ops->computefunction) {
1351d64ed03dSBarry Smith     PetscStackPush("SNES user function");
135239d508bbSBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1353d64ed03dSBarry Smith     PetscStackPop;
135473250ac0SBarry Smith   } else if (snes->dm) {
1355644e2e5bSBarry Smith     ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr);
1356c90fad12SPeter Brune   } else if (snes->vec_rhs) {
1357c90fad12SPeter Brune     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
1358644e2e5bSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve().");
135985385478SLisandro Dalcin   if (snes->vec_rhs) {
136085385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
13613ab0aad5SBarry Smith   }
1362ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1363d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
13643a40ed3dSBarry Smith   PetscFunctionReturn(0);
13659b94acceSBarry Smith }
13669b94acceSBarry Smith 
13674a2ae208SSatish Balay #undef __FUNCT__
1368646217ecSPeter Brune #define __FUNCT__ "SNESComputeGS"
1369c79ef259SPeter Brune /*@
1370c79ef259SPeter Brune    SNESComputeGS - Calls the Gauss-Seidel function that has been set with
1371c79ef259SPeter Brune                    SNESSetGS().
1372c79ef259SPeter Brune 
1373c79ef259SPeter Brune    Collective on SNES
1374c79ef259SPeter Brune 
1375c79ef259SPeter Brune    Input Parameters:
1376c79ef259SPeter Brune +  snes - the SNES context
1377c79ef259SPeter Brune .  x - input vector
1378c79ef259SPeter Brune -  b - rhs vector
1379c79ef259SPeter Brune 
1380c79ef259SPeter Brune    Output Parameter:
1381c79ef259SPeter Brune .  x - new solution vector
1382c79ef259SPeter Brune 
1383c79ef259SPeter Brune    Notes:
1384c79ef259SPeter Brune    SNESComputeGS() is typically used within composed nonlinear solver
1385c79ef259SPeter Brune    implementations, so most users would not generally call this routine
1386c79ef259SPeter Brune    themselves.
1387c79ef259SPeter Brune 
1388c79ef259SPeter Brune    Level: developer
1389c79ef259SPeter Brune 
1390c79ef259SPeter Brune .keywords: SNES, nonlinear, compute, function
1391c79ef259SPeter Brune 
1392c79ef259SPeter Brune .seealso: SNESSetGS(), SNESComputeFunction()
1393c79ef259SPeter Brune @*/
1394646217ecSPeter Brune PetscErrorCode  SNESComputeGS(SNES snes,Vec b,Vec x)
1395646217ecSPeter Brune {
1396646217ecSPeter Brune   PetscErrorCode ierr;
1397646217ecSPeter Brune 
1398646217ecSPeter Brune   PetscFunctionBegin;
1399646217ecSPeter Brune   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1400646217ecSPeter Brune   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
1401646217ecSPeter Brune   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,3);
1402646217ecSPeter Brune   PetscCheckSameComm(snes,1,x,2);
1403646217ecSPeter Brune   if(b) PetscCheckSameComm(snes,1,b,3);
1404646217ecSPeter Brune   if (snes->ops->computegs) {
1405646217ecSPeter Brune     PetscStackPush("SNES user GS");
1406646217ecSPeter Brune     ierr = (*snes->ops->computegs)(snes,x,b,snes->gsP);CHKERRQ(ierr);
1407646217ecSPeter Brune     PetscStackPop;
1408646217ecSPeter Brune   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetGS() before SNESComputeGS(), likely called from SNESSolve().");
1409646217ecSPeter Brune   PetscFunctionReturn(0);
1410646217ecSPeter Brune }
1411646217ecSPeter Brune 
1412646217ecSPeter Brune 
1413646217ecSPeter Brune #undef __FUNCT__
14144a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
141562fef451SLois Curfman McInnes /*@
141662fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
141762fef451SLois Curfman McInnes    set with SNESSetJacobian().
141862fef451SLois Curfman McInnes 
1419c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1420c7afd0dbSLois Curfman McInnes 
142162fef451SLois Curfman McInnes    Input Parameters:
1422c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1423c7afd0dbSLois Curfman McInnes -  x - input vector
142462fef451SLois Curfman McInnes 
142562fef451SLois Curfman McInnes    Output Parameters:
1426c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
142762fef451SLois Curfman McInnes .  B - optional preconditioning matrix
14282b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1429fee21e36SBarry Smith 
1430e35cf81dSBarry Smith   Options Database Keys:
1431e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1432693365a8SJed Brown .    -snes_lag_jacobian <lag>
1433693365a8SJed Brown .    -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences
1434693365a8SJed Brown .    -snes_compare_explicit_draw  - Compare the computed Jacobian to the finite difference Jacobian and draw the result
1435693365a8SJed Brown .    -snes_compare_explicit_contour  - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result
14364c30e9fbSJed Brown .    -snes_compare_operator  - Make the comparison options above use the operator instead of the preconditioning matrix
1437c01495d3SJed Brown .    -snes_compare_coloring - Compute the finite differece Jacobian using coloring and display norms of difference
1438c01495d3SJed Brown .    -snes_compare_coloring_display - Compute the finite differece Jacobian using coloring and display verbose differences
1439c01495d3SJed Brown .    -snes_compare_coloring_threshold - Display only those matrix entries that differ by more than a given threshold
1440c01495d3SJed Brown .    -snes_compare_coloring_threshold_atol - Absolute tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold
1441c01495d3SJed Brown .    -snes_compare_coloring_threshold_rtol - Relative tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold
14424c30e9fbSJed Brown .    -snes_compare_coloring_draw - Compute the finite differece Jacobian using coloring and draw differences
1443c01495d3SJed Brown -    -snes_compare_coloring_draw_contour - Compute the finite differece Jacobian using coloring and show contours of matrices and differences
1444c01495d3SJed Brown 
1445e35cf81dSBarry Smith 
144662fef451SLois Curfman McInnes    Notes:
144762fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
144862fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
144962fef451SLois Curfman McInnes 
145094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1451dc5a77f8SLois Curfman McInnes    flag parameter.
145262fef451SLois Curfman McInnes 
145336851e7fSLois Curfman McInnes    Level: developer
145436851e7fSLois Curfman McInnes 
145562fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
145662fef451SLois Curfman McInnes 
1457e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
145862fef451SLois Curfman McInnes @*/
14597087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
14609b94acceSBarry Smith {
1461dfbe8321SBarry Smith   PetscErrorCode ierr;
1462ace3abfcSBarry Smith   PetscBool      flag;
14633a40ed3dSBarry Smith 
14643a40ed3dSBarry Smith   PetscFunctionBegin;
14650700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14660700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
14674482741eSBarry Smith   PetscValidPointer(flg,5);
1468c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1469e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1470ebd3b9afSBarry Smith 
1471ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1472ebd3b9afSBarry Smith 
1473fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1474fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1475fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1476fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1477e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1478e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1479ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1480ebd3b9afSBarry Smith     if (flag) {
1481ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1482ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1483ebd3b9afSBarry Smith     }
1484e35cf81dSBarry Smith     PetscFunctionReturn(0);
1485e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1486e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1487e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1488ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1489ebd3b9afSBarry Smith     if (flag) {
1490ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1491ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1492ebd3b9afSBarry Smith     }
1493e35cf81dSBarry Smith     PetscFunctionReturn(0);
1494e35cf81dSBarry Smith   }
1495e35cf81dSBarry Smith 
1496c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1497e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1498d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1499e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1500d64ed03dSBarry Smith   PetscStackPop;
1501d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1502a8054027SBarry Smith 
15033b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
15043b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
15053b4f5425SBarry Smith     snes->lagpreconditioner = -1;
15063b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1507a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1508a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1509a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1510a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1511a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1512a8054027SBarry Smith   }
1513a8054027SBarry Smith 
15146d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
15150700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
15160700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
1517693365a8SJed Brown   {
1518693365a8SJed Brown     PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE;
1519693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr);
1520693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr);
1521693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr);
1522693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr);
1523693365a8SJed Brown     if (flag || flag_draw || flag_contour) {
1524693365a8SJed Brown       Mat Bexp_mine = PETSC_NULL,Bexp,FDexp;
1525693365a8SJed Brown       MatStructure mstruct;
1526693365a8SJed Brown       PetscViewer vdraw,vstdout;
15276b3a5b13SJed Brown       PetscBool flg;
1528693365a8SJed Brown       if (flag_operator) {
1529693365a8SJed Brown         ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr);
1530693365a8SJed Brown         Bexp = Bexp_mine;
1531693365a8SJed Brown       } else {
1532693365a8SJed Brown         /* See if the preconditioning matrix can be viewed and added directly */
1533693365a8SJed Brown         ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr);
1534693365a8SJed Brown         if (flg) Bexp = *B;
1535693365a8SJed Brown         else {
1536693365a8SJed Brown           /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */
1537693365a8SJed Brown           ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr);
1538693365a8SJed Brown           Bexp = Bexp_mine;
1539693365a8SJed Brown         }
1540693365a8SJed Brown       }
1541693365a8SJed Brown       ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr);
1542693365a8SJed Brown       ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr);
1543693365a8SJed Brown       ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr);
1544693365a8SJed Brown       if (flag_draw || flag_contour) {
1545693365a8SJed Brown         ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr);
1546693365a8SJed Brown         if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);}
1547693365a8SJed Brown       } else vdraw = PETSC_NULL;
1548693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr);
1549693365a8SJed Brown       if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);}
1550693365a8SJed Brown       if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);}
1551693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr);
1552693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1553693365a8SJed Brown       if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);}
1554693365a8SJed Brown       ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
1555693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr);
1556693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1557693365a8SJed Brown       if (vdraw) {              /* Always use contour for the difference */
1558693365a8SJed Brown         ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
1559693365a8SJed Brown         ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);
1560693365a8SJed Brown         ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);
1561693365a8SJed Brown       }
1562693365a8SJed Brown       if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);}
1563693365a8SJed Brown       ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr);
1564693365a8SJed Brown       ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr);
1565693365a8SJed Brown       ierr = MatDestroy(&FDexp);CHKERRQ(ierr);
1566693365a8SJed Brown     }
1567693365a8SJed Brown   }
15684c30e9fbSJed Brown   {
15696719d8e4SJed Brown     PetscBool flag = PETSC_FALSE,flag_display = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_threshold = PETSC_FALSE;
15706719d8e4SJed Brown     PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON,threshold_rtol = 10*PETSC_SQRT_MACHINE_EPSILON;
15714c30e9fbSJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring",&flag,PETSC_NULL);CHKERRQ(ierr);
15726719d8e4SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_display",&flag_display,PETSC_NULL);CHKERRQ(ierr);
15734c30e9fbSJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr);
15744c30e9fbSJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr);
15756719d8e4SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold",&flag_threshold,PETSC_NULL);CHKERRQ(ierr);
15766719d8e4SJed Brown     ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_rtol",&threshold_rtol,PETSC_NULL);CHKERRQ(ierr);
15776719d8e4SJed Brown     ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_atol",&threshold_atol,PETSC_NULL);CHKERRQ(ierr);
15786719d8e4SJed Brown     if (flag || flag_display || flag_draw || flag_contour || flag_threshold) {
15794c30e9fbSJed Brown       Mat Bfd;
15804c30e9fbSJed Brown       MatStructure mstruct;
15814c30e9fbSJed Brown       PetscViewer vdraw,vstdout;
15824c30e9fbSJed Brown       ISColoring iscoloring;
15834c30e9fbSJed Brown       MatFDColoring matfdcoloring;
15844c30e9fbSJed Brown       PetscErrorCode (*func)(SNES,Vec,Vec,void*);
15854c30e9fbSJed Brown       void *funcctx;
15866719d8e4SJed Brown       PetscReal norm1,norm2,normmax;
15874c30e9fbSJed Brown 
15884c30e9fbSJed Brown       ierr = MatDuplicate(*B,MAT_DO_NOT_COPY_VALUES,&Bfd);CHKERRQ(ierr);
15894c30e9fbSJed Brown       ierr = MatGetColoring(Bfd,MATCOLORINGSL,&iscoloring);CHKERRQ(ierr);
15904c30e9fbSJed Brown       ierr = MatFDColoringCreate(Bfd,iscoloring,&matfdcoloring);CHKERRQ(ierr);
15914c30e9fbSJed Brown       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
15924c30e9fbSJed Brown 
15934c30e9fbSJed Brown       /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */
15944c30e9fbSJed Brown       ierr = SNESGetFunction(snes,PETSC_NULL,&func,&funcctx);CHKERRQ(ierr);
15954c30e9fbSJed Brown       ierr = MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode(*)(void))func,funcctx);CHKERRQ(ierr);
15964c30e9fbSJed Brown       ierr = PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring,((PetscObject)snes)->prefix);CHKERRQ(ierr);
15974c30e9fbSJed Brown       ierr = PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring,"coloring_");CHKERRQ(ierr);
15984c30e9fbSJed Brown       ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr);
15994c30e9fbSJed Brown       ierr = MatFDColoringApply(Bfd,matfdcoloring,X,&mstruct,snes);CHKERRQ(ierr);
16004c30e9fbSJed Brown       ierr = MatFDColoringDestroy(&matfdcoloring);CHKERRQ(ierr);
16014c30e9fbSJed Brown 
16024c30e9fbSJed Brown       ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr);
16034c30e9fbSJed Brown       if (flag_draw || flag_contour) {
16044c30e9fbSJed Brown         ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Colored Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr);
16054c30e9fbSJed Brown         if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);}
16064c30e9fbSJed Brown       } else vdraw = PETSC_NULL;
16074c30e9fbSJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Explicit preconditioning Jacobian\n");CHKERRQ(ierr);
16086719d8e4SJed Brown       if (flag_display) {ierr = MatView(*B,vstdout);CHKERRQ(ierr);}
16094c30e9fbSJed Brown       if (vdraw) {ierr = MatView(*B,vdraw);CHKERRQ(ierr);}
16104c30e9fbSJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Colored Finite difference Jacobian\n");CHKERRQ(ierr);
16116719d8e4SJed Brown       if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);}
16124c30e9fbSJed Brown       if (vdraw) {ierr = MatView(Bfd,vdraw);CHKERRQ(ierr);}
16134c30e9fbSJed Brown       ierr = MatAYPX(Bfd,-1.0,*B,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
16144c30e9fbSJed Brown       ierr = MatNorm(Bfd,NORM_1,&norm1);CHKERRQ(ierr);
16156719d8e4SJed Brown       ierr = MatNorm(Bfd,NORM_FROBENIUS,&norm2);CHKERRQ(ierr);
16164c30e9fbSJed Brown       ierr = MatNorm(Bfd,NORM_MAX,&normmax);CHKERRQ(ierr);
16176719d8e4SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian, norm1=%G normFrob=%G normmax=%G\n",norm1,norm2,normmax);CHKERRQ(ierr);
16186719d8e4SJed Brown       if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);}
16194c30e9fbSJed Brown       if (vdraw) {              /* Always use contour for the difference */
16204c30e9fbSJed Brown         ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
16214c30e9fbSJed Brown         ierr = MatView(Bfd,vdraw);CHKERRQ(ierr);
16224c30e9fbSJed Brown         ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);
16234c30e9fbSJed Brown       }
16244c30e9fbSJed Brown       if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);}
16256719d8e4SJed Brown 
16266719d8e4SJed Brown       if (flag_threshold) {
16276719d8e4SJed Brown         PetscInt bs,rstart,rend,i;
16286719d8e4SJed Brown         ierr = MatGetBlockSize(*B,&bs);CHKERRQ(ierr);
16296719d8e4SJed Brown         ierr = MatGetOwnershipRange(*B,&rstart,&rend);CHKERRQ(ierr);
16306719d8e4SJed Brown         for (i=rstart; i<rend; i++) {
16316719d8e4SJed Brown           const PetscScalar *ba,*ca;
16326719d8e4SJed Brown           const PetscInt *bj,*cj;
16336719d8e4SJed Brown           PetscInt bn,cn,j,maxentrycol = -1,maxdiffcol = -1,maxrdiffcol = -1;
16346719d8e4SJed Brown           PetscReal maxentry = 0,maxdiff = 0,maxrdiff = 0;
16356719d8e4SJed Brown           ierr = MatGetRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr);
16366719d8e4SJed Brown           ierr = MatGetRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr);
16376719d8e4SJed Brown           if (bn != cn) SETERRQ(((PetscObject)*A)->comm,PETSC_ERR_PLIB,"Unexpected different nonzero pattern in -snes_compare_coloring_threshold");
16386719d8e4SJed Brown           for (j=0; j<bn; j++) {
16396719d8e4SJed Brown             PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j]));
16406719d8e4SJed Brown             if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) {
16416719d8e4SJed Brown               maxentrycol = bj[j];
16426719d8e4SJed Brown               maxentry = PetscRealPart(ba[j]);
16436719d8e4SJed Brown             }
16446719d8e4SJed Brown             if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) {
16456719d8e4SJed Brown               maxdiffcol = bj[j];
16466719d8e4SJed Brown               maxdiff = PetscRealPart(ca[j]);
16476719d8e4SJed Brown             }
16486719d8e4SJed Brown             if (rdiff > maxrdiff) {
16496719d8e4SJed Brown               maxrdiffcol = bj[j];
16506719d8e4SJed Brown               maxrdiff = rdiff;
16516719d8e4SJed Brown             }
16526719d8e4SJed Brown           }
16536719d8e4SJed Brown           if (maxrdiff > 1) {
16546719d8e4SJed 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);
16556719d8e4SJed Brown             for (j=0; j<bn; j++) {
16566719d8e4SJed Brown               PetscReal rdiff;
16576719d8e4SJed Brown               rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j]));
16586719d8e4SJed Brown               if (rdiff > 1) {
16596719d8e4SJed Brown                 ierr = PetscViewerASCIIPrintf(vstdout," (%D,%G:%G)",bj[j],PetscRealPart(ba[j]),PetscRealPart(ca[j]));CHKERRQ(ierr);
16606719d8e4SJed Brown               }
16616719d8e4SJed Brown             }
16626719d8e4SJed Brown             ierr = PetscViewerASCIIPrintf(vstdout,"\n",i,maxentry,maxdiff,maxrdiff);CHKERRQ(ierr);
16636719d8e4SJed Brown           }
16646719d8e4SJed Brown           ierr = MatRestoreRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr);
16656719d8e4SJed Brown           ierr = MatRestoreRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr);
16666719d8e4SJed Brown         }
16676719d8e4SJed Brown       }
16684c30e9fbSJed Brown       ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr);
16694c30e9fbSJed Brown       ierr = MatDestroy(&Bfd);CHKERRQ(ierr);
16704c30e9fbSJed Brown     }
16714c30e9fbSJed Brown   }
16723a40ed3dSBarry Smith   PetscFunctionReturn(0);
16739b94acceSBarry Smith }
16749b94acceSBarry Smith 
16754a2ae208SSatish Balay #undef __FUNCT__
16764a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
16779b94acceSBarry Smith /*@C
16789b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1679044dda88SLois Curfman McInnes    location to store the matrix.
16809b94acceSBarry Smith 
16813f9fe445SBarry Smith    Logically Collective on SNES and Mat
1682c7afd0dbSLois Curfman McInnes 
16839b94acceSBarry Smith    Input Parameters:
1684c7afd0dbSLois Curfman McInnes +  snes - the SNES context
16859b94acceSBarry Smith .  A - Jacobian matrix
16869b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
1687efd51863SBarry Smith .  func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value)
1688c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1689efd51863SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value)
16909b94acceSBarry Smith 
16919b94acceSBarry Smith    Calling sequence of func:
16928d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
16939b94acceSBarry Smith 
1694c7afd0dbSLois Curfman McInnes +  x - input vector
16959b94acceSBarry Smith .  A - Jacobian matrix
16969b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1697ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
16982b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1699c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
17009b94acceSBarry Smith 
17019b94acceSBarry Smith    Notes:
170294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
17032cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1704ac21db08SLois Curfman McInnes 
1705ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
17069b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
17079b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
17089b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
17099b94acceSBarry Smith    throughout the global iterations.
17109b94acceSBarry Smith 
171116913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
171216913363SBarry Smith    each matrix.
171316913363SBarry Smith 
1714a8a26c1eSJed Brown    If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument
1715a8a26c1eSJed Brown    must be a MatFDColoring.
1716a8a26c1eSJed Brown 
1717c3cc8fd1SJed Brown    Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian.  One common
1718c3cc8fd1SJed Brown    example is to use the "Picard linearization" which only differentiates through the highest order parts of each term.
1719c3cc8fd1SJed Brown 
172036851e7fSLois Curfman McInnes    Level: beginner
172136851e7fSLois Curfman McInnes 
17229b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
17239b94acceSBarry Smith 
17243ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
17259b94acceSBarry Smith @*/
17267087cfbeSBarry Smith PetscErrorCode  SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
17279b94acceSBarry Smith {
1728dfbe8321SBarry Smith   PetscErrorCode ierr;
17293a7fca6bSBarry Smith 
17303a40ed3dSBarry Smith   PetscFunctionBegin;
17310700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
17320700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
17330700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1734c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
173506975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1736e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
17373a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
17383a7fca6bSBarry Smith   if (A) {
17397dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
17406bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
17419b94acceSBarry Smith     snes->jacobian = A;
17423a7fca6bSBarry Smith   }
17433a7fca6bSBarry Smith   if (B) {
17447dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
17456bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
17469b94acceSBarry Smith     snes->jacobian_pre = B;
17473a7fca6bSBarry Smith   }
17483a40ed3dSBarry Smith   PetscFunctionReturn(0);
17499b94acceSBarry Smith }
175062fef451SLois Curfman McInnes 
17514a2ae208SSatish Balay #undef __FUNCT__
17524a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1753c2aafc4cSSatish Balay /*@C
1754b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1755b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1756b4fd4287SBarry Smith 
1757c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1758c7afd0dbSLois Curfman McInnes 
1759b4fd4287SBarry Smith    Input Parameter:
1760b4fd4287SBarry Smith .  snes - the nonlinear solver context
1761b4fd4287SBarry Smith 
1762b4fd4287SBarry Smith    Output Parameters:
1763c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1764b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
176570e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
176670e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1767fee21e36SBarry Smith 
176836851e7fSLois Curfman McInnes    Level: advanced
176936851e7fSLois Curfman McInnes 
1770b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1771b4fd4287SBarry Smith @*/
17727087cfbeSBarry Smith PetscErrorCode  SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1773b4fd4287SBarry Smith {
17743a40ed3dSBarry Smith   PetscFunctionBegin;
17750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1776b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1777b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1778e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
177970e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
17803a40ed3dSBarry Smith   PetscFunctionReturn(0);
1781b4fd4287SBarry Smith }
1782b4fd4287SBarry Smith 
17839b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
17849b94acceSBarry Smith 
17854a2ae208SSatish Balay #undef __FUNCT__
17864a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
17879b94acceSBarry Smith /*@
17889b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1789272ac6f2SLois Curfman McInnes    of a nonlinear solver.
17909b94acceSBarry Smith 
1791fee21e36SBarry Smith    Collective on SNES
1792fee21e36SBarry Smith 
1793c7afd0dbSLois Curfman McInnes    Input Parameters:
179470e92668SMatthew Knepley .  snes - the SNES context
1795c7afd0dbSLois Curfman McInnes 
1796272ac6f2SLois Curfman McInnes    Notes:
1797272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1798272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1799272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1800272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1801272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1802272ac6f2SLois Curfman McInnes 
180336851e7fSLois Curfman McInnes    Level: advanced
180436851e7fSLois Curfman McInnes 
18059b94acceSBarry Smith .keywords: SNES, nonlinear, setup
18069b94acceSBarry Smith 
18079b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
18089b94acceSBarry Smith @*/
18097087cfbeSBarry Smith PetscErrorCode  SNESSetUp(SNES snes)
18109b94acceSBarry Smith {
1811dfbe8321SBarry Smith   PetscErrorCode ierr;
18123a40ed3dSBarry Smith 
18133a40ed3dSBarry Smith   PetscFunctionBegin;
18140700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
18154dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
18169b94acceSBarry Smith 
18177adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
181885385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
181985385478SLisandro Dalcin   }
182085385478SLisandro Dalcin 
1821c9b9fda1SJed Brown   if (!snes->vec_func) {
1822c9b9fda1SJed Brown     if (snes->vec_rhs) {
1823c9b9fda1SJed Brown       ierr = VecDuplicate(snes->vec_rhs,&snes->vec_func);CHKERRQ(ierr);
1824c9b9fda1SJed Brown     } else if (snes->vec_sol) {
1825c9b9fda1SJed Brown       ierr = VecDuplicate(snes->vec_sol,&snes->vec_func);CHKERRQ(ierr);
1826c9b9fda1SJed Brown     } else if (snes->dm) {
1827efd51863SBarry Smith       ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1828efd51863SBarry Smith     }
1829c9b9fda1SJed Brown   }
183017186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
183158c9b817SLisandro Dalcin   if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
183258c9b817SLisandro Dalcin 
183358c9b817SLisandro Dalcin   if (!snes->vec_sol_update /* && snes->vec_sol */) {
183458c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr);
183558c9b817SLisandro Dalcin     ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr);
183658c9b817SLisandro Dalcin   }
183758c9b817SLisandro Dalcin 
1838ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1839ef8dffc7SBarry Smith     Mat J;
1840ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1841cab2e9ccSBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDMComputeJacobian,PETSC_NULL);CHKERRQ(ierr);
1842cab2e9ccSBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
18432ef29e96SBarry Smith   } else if (!snes->jacobian && snes->ops->computejacobian == MatMFFDComputeJacobian) {
1844a8248277SBarry Smith     Mat J;
1845a8248277SBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
1846a8248277SBarry Smith     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
1847a8248277SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
1848a8248277SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
1849a8248277SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1850a8248277SBarry Smith   } else if (snes->dm && snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) {
1851a8248277SBarry Smith     Mat J,B;
1852a8248277SBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
1853a8248277SBarry Smith     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
1854a8248277SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
1855a8248277SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr);
1856a8248277SBarry Smith     ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,snes->funP);CHKERRQ(ierr);
1857a8248277SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1858a8248277SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
1859efd51863SBarry Smith   } else if (snes->dm && !snes->jacobian_pre){
1860efd51863SBarry Smith     Mat J;
1861efd51863SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
18623cbb28f5SBarry Smith     ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1863efd51863SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1864ef8dffc7SBarry Smith   }
1865cfaf3a74SBarry 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()");
1866c9b9fda1SJed Brown   if (!snes->vec_func) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a vector when calling SNESSetFunction() or call SNESSetDM()");
1867efd51863SBarry Smith 
1868b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1869b710008aSBarry Smith 
1870d25893d9SBarry Smith   if (snes->ops->usercompute && !snes->user) {
1871d25893d9SBarry Smith     ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr);
1872d25893d9SBarry Smith   }
1873d25893d9SBarry Smith 
1874410397dcSLisandro Dalcin   if (snes->ops->setup) {
1875410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1876410397dcSLisandro Dalcin   }
187758c9b817SLisandro Dalcin 
18787aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
18793a40ed3dSBarry Smith   PetscFunctionReturn(0);
18809b94acceSBarry Smith }
18819b94acceSBarry Smith 
18824a2ae208SSatish Balay #undef __FUNCT__
188337596af1SLisandro Dalcin #define __FUNCT__ "SNESReset"
188437596af1SLisandro Dalcin /*@
188537596af1SLisandro Dalcin    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats
188637596af1SLisandro Dalcin 
188737596af1SLisandro Dalcin    Collective on SNES
188837596af1SLisandro Dalcin 
188937596af1SLisandro Dalcin    Input Parameter:
189037596af1SLisandro Dalcin .  snes - iterative context obtained from SNESCreate()
189137596af1SLisandro Dalcin 
1892d25893d9SBarry Smith    Level: intermediate
1893d25893d9SBarry Smith 
1894d25893d9SBarry Smith    Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext()
189537596af1SLisandro Dalcin 
189637596af1SLisandro Dalcin .keywords: SNES, destroy
189737596af1SLisandro Dalcin 
189837596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
189937596af1SLisandro Dalcin @*/
190037596af1SLisandro Dalcin PetscErrorCode  SNESReset(SNES snes)
190137596af1SLisandro Dalcin {
190237596af1SLisandro Dalcin   PetscErrorCode ierr;
190337596af1SLisandro Dalcin 
190437596af1SLisandro Dalcin   PetscFunctionBegin;
190537596af1SLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1906d25893d9SBarry Smith   if (snes->ops->userdestroy && snes->user) {
1907d25893d9SBarry Smith     ierr       = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr);
1908d25893d9SBarry Smith     snes->user = PETSC_NULL;
1909d25893d9SBarry Smith   }
19108a23116dSBarry Smith   if (snes->pc) {
19118a23116dSBarry Smith     ierr = SNESReset(snes->pc);CHKERRQ(ierr);
19128a23116dSBarry Smith   }
19138a23116dSBarry Smith 
191437596af1SLisandro Dalcin   if (snes->ops->reset) {
191537596af1SLisandro Dalcin     ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr);
191637596af1SLisandro Dalcin   }
191737596af1SLisandro Dalcin   if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);}
19186bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
19196bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
19206bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr);
19216bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
19226bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
19236bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
192437596af1SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
192537596af1SLisandro Dalcin   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
192637596af1SLisandro Dalcin   snes->nwork = snes->nvwork = 0;
192737596af1SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
192837596af1SLisandro Dalcin   PetscFunctionReturn(0);
192937596af1SLisandro Dalcin }
193037596af1SLisandro Dalcin 
193137596af1SLisandro Dalcin #undef __FUNCT__
19324a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
193352baeb72SSatish Balay /*@
19349b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
19359b94acceSBarry Smith    with SNESCreate().
19369b94acceSBarry Smith 
1937c7afd0dbSLois Curfman McInnes    Collective on SNES
1938c7afd0dbSLois Curfman McInnes 
19399b94acceSBarry Smith    Input Parameter:
19409b94acceSBarry Smith .  snes - the SNES context
19419b94acceSBarry Smith 
194236851e7fSLois Curfman McInnes    Level: beginner
194336851e7fSLois Curfman McInnes 
19449b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
19459b94acceSBarry Smith 
194663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
19479b94acceSBarry Smith @*/
19486bf464f9SBarry Smith PetscErrorCode  SNESDestroy(SNES *snes)
19499b94acceSBarry Smith {
19506849ba73SBarry Smith   PetscErrorCode ierr;
19513a40ed3dSBarry Smith 
19523a40ed3dSBarry Smith   PetscFunctionBegin;
19536bf464f9SBarry Smith   if (!*snes) PetscFunctionReturn(0);
19546bf464f9SBarry Smith   PetscValidHeaderSpecific((*snes),SNES_CLASSID,1);
19556bf464f9SBarry Smith   if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);}
1956d4bb536fSBarry Smith 
19576bf464f9SBarry Smith   ierr = SNESReset((*snes));CHKERRQ(ierr);
19588a23116dSBarry Smith   ierr = SNESDestroy(&(*snes)->pc);CHKERRQ(ierr);
19596b8b9a38SLisandro Dalcin 
1960be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
19616bf464f9SBarry Smith   ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr);
19626bf464f9SBarry Smith   if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);}
19636d4c513bSLisandro Dalcin 
19646bf464f9SBarry Smith   ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr);
19656bf464f9SBarry Smith   ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr);
19666b8b9a38SLisandro Dalcin 
19676bf464f9SBarry Smith   ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr);
19686bf464f9SBarry Smith   if ((*snes)->ops->convergeddestroy) {
19696bf464f9SBarry Smith     ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr);
19706b8b9a38SLisandro Dalcin   }
19716bf464f9SBarry Smith   if ((*snes)->conv_malloc) {
19726bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr);
19736bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr);
197458c9b817SLisandro Dalcin   }
1975ea630c6eSPeter Brune   ierr = PetscViewerDestroy(&(*snes)->ls_monitor);CHKERRQ(ierr);
19766bf464f9SBarry Smith   ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr);
1977a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
19783a40ed3dSBarry Smith  PetscFunctionReturn(0);
19799b94acceSBarry Smith }
19809b94acceSBarry Smith 
19819b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
19829b94acceSBarry Smith 
19834a2ae208SSatish Balay #undef __FUNCT__
1984a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1985a8054027SBarry Smith /*@
1986a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1987a8054027SBarry Smith 
19883f9fe445SBarry Smith    Logically Collective on SNES
1989a8054027SBarry Smith 
1990a8054027SBarry Smith    Input Parameters:
1991a8054027SBarry Smith +  snes - the SNES context
1992a8054027SBarry 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
19933b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1994a8054027SBarry Smith 
1995a8054027SBarry Smith    Options Database Keys:
1996a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1997a8054027SBarry Smith 
1998a8054027SBarry Smith    Notes:
1999a8054027SBarry Smith    The default is 1
2000a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
2001a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
2002a8054027SBarry Smith 
2003a8054027SBarry Smith    Level: intermediate
2004a8054027SBarry Smith 
2005a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2006a8054027SBarry Smith 
2007e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
2008a8054027SBarry Smith 
2009a8054027SBarry Smith @*/
20107087cfbeSBarry Smith PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
2011a8054027SBarry Smith {
2012a8054027SBarry Smith   PetscFunctionBegin;
20130700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2014e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
2015e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
2016c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
2017a8054027SBarry Smith   snes->lagpreconditioner = lag;
2018a8054027SBarry Smith   PetscFunctionReturn(0);
2019a8054027SBarry Smith }
2020a8054027SBarry Smith 
2021a8054027SBarry Smith #undef __FUNCT__
2022efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence"
2023efd51863SBarry Smith /*@
2024efd51863SBarry Smith    SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does
2025efd51863SBarry Smith 
2026efd51863SBarry Smith    Logically Collective on SNES
2027efd51863SBarry Smith 
2028efd51863SBarry Smith    Input Parameters:
2029efd51863SBarry Smith +  snes - the SNES context
2030efd51863SBarry Smith -  steps - the number of refinements to do, defaults to 0
2031efd51863SBarry Smith 
2032efd51863SBarry Smith    Options Database Keys:
2033efd51863SBarry Smith .    -snes_grid_sequence <steps>
2034efd51863SBarry Smith 
2035efd51863SBarry Smith    Level: intermediate
2036efd51863SBarry Smith 
2037c0df2a02SJed Brown    Notes:
2038c0df2a02SJed Brown    Use SNESGetSolution() to extract the fine grid solution after grid sequencing.
2039c0df2a02SJed Brown 
2040efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2041efd51863SBarry Smith 
2042efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
2043efd51863SBarry Smith 
2044efd51863SBarry Smith @*/
2045efd51863SBarry Smith PetscErrorCode  SNESSetGridSequence(SNES snes,PetscInt steps)
2046efd51863SBarry Smith {
2047efd51863SBarry Smith   PetscFunctionBegin;
2048efd51863SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2049efd51863SBarry Smith   PetscValidLogicalCollectiveInt(snes,steps,2);
2050efd51863SBarry Smith   snes->gridsequence = steps;
2051efd51863SBarry Smith   PetscFunctionReturn(0);
2052efd51863SBarry Smith }
2053efd51863SBarry Smith 
2054efd51863SBarry Smith #undef __FUNCT__
2055a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
2056a8054027SBarry Smith /*@
2057a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
2058a8054027SBarry Smith 
20593f9fe445SBarry Smith    Not Collective
2060a8054027SBarry Smith 
2061a8054027SBarry Smith    Input Parameter:
2062a8054027SBarry Smith .  snes - the SNES context
2063a8054027SBarry Smith 
2064a8054027SBarry Smith    Output Parameter:
2065a8054027SBarry 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
20663b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
2067a8054027SBarry Smith 
2068a8054027SBarry Smith    Options Database Keys:
2069a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
2070a8054027SBarry Smith 
2071a8054027SBarry Smith    Notes:
2072a8054027SBarry Smith    The default is 1
2073a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
2074a8054027SBarry Smith 
2075a8054027SBarry Smith    Level: intermediate
2076a8054027SBarry Smith 
2077a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2078a8054027SBarry Smith 
2079a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
2080a8054027SBarry Smith 
2081a8054027SBarry Smith @*/
20827087cfbeSBarry Smith PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
2083a8054027SBarry Smith {
2084a8054027SBarry Smith   PetscFunctionBegin;
20850700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2086a8054027SBarry Smith   *lag = snes->lagpreconditioner;
2087a8054027SBarry Smith   PetscFunctionReturn(0);
2088a8054027SBarry Smith }
2089a8054027SBarry Smith 
2090a8054027SBarry Smith #undef __FUNCT__
2091e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
2092e35cf81dSBarry Smith /*@
2093e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
2094e35cf81dSBarry Smith      often the preconditioner is rebuilt.
2095e35cf81dSBarry Smith 
20963f9fe445SBarry Smith    Logically Collective on SNES
2097e35cf81dSBarry Smith 
2098e35cf81dSBarry Smith    Input Parameters:
2099e35cf81dSBarry Smith +  snes - the SNES context
2100e35cf81dSBarry 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
2101fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
2102e35cf81dSBarry Smith 
2103e35cf81dSBarry Smith    Options Database Keys:
2104e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
2105e35cf81dSBarry Smith 
2106e35cf81dSBarry Smith    Notes:
2107e35cf81dSBarry Smith    The default is 1
2108e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
2109fe3ffe1eSBarry 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
2110fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
2111e35cf81dSBarry Smith 
2112e35cf81dSBarry Smith    Level: intermediate
2113e35cf81dSBarry Smith 
2114e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2115e35cf81dSBarry Smith 
2116e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
2117e35cf81dSBarry Smith 
2118e35cf81dSBarry Smith @*/
21197087cfbeSBarry Smith PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
2120e35cf81dSBarry Smith {
2121e35cf81dSBarry Smith   PetscFunctionBegin;
21220700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2123e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
2124e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
2125c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
2126e35cf81dSBarry Smith   snes->lagjacobian = lag;
2127e35cf81dSBarry Smith   PetscFunctionReturn(0);
2128e35cf81dSBarry Smith }
2129e35cf81dSBarry Smith 
2130e35cf81dSBarry Smith #undef __FUNCT__
2131e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
2132e35cf81dSBarry Smith /*@
2133e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
2134e35cf81dSBarry Smith 
21353f9fe445SBarry Smith    Not Collective
2136e35cf81dSBarry Smith 
2137e35cf81dSBarry Smith    Input Parameter:
2138e35cf81dSBarry Smith .  snes - the SNES context
2139e35cf81dSBarry Smith 
2140e35cf81dSBarry Smith    Output Parameter:
2141e35cf81dSBarry 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
2142e35cf81dSBarry Smith          the Jacobian is built etc.
2143e35cf81dSBarry Smith 
2144e35cf81dSBarry Smith    Options Database Keys:
2145e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
2146e35cf81dSBarry Smith 
2147e35cf81dSBarry Smith    Notes:
2148e35cf81dSBarry Smith    The default is 1
2149e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
2150e35cf81dSBarry Smith 
2151e35cf81dSBarry Smith    Level: intermediate
2152e35cf81dSBarry Smith 
2153e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
2154e35cf81dSBarry Smith 
2155e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
2156e35cf81dSBarry Smith 
2157e35cf81dSBarry Smith @*/
21587087cfbeSBarry Smith PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
2159e35cf81dSBarry Smith {
2160e35cf81dSBarry Smith   PetscFunctionBegin;
21610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2162e35cf81dSBarry Smith   *lag = snes->lagjacobian;
2163e35cf81dSBarry Smith   PetscFunctionReturn(0);
2164e35cf81dSBarry Smith }
2165e35cf81dSBarry Smith 
2166e35cf81dSBarry Smith #undef __FUNCT__
21674a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
21689b94acceSBarry Smith /*@
2169d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
21709b94acceSBarry Smith 
21713f9fe445SBarry Smith    Logically Collective on SNES
2172c7afd0dbSLois Curfman McInnes 
21739b94acceSBarry Smith    Input Parameters:
2174c7afd0dbSLois Curfman McInnes +  snes - the SNES context
217570441072SBarry Smith .  abstol - absolute convergence tolerance
217633174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
217733174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
217833174efeSLois Curfman McInnes            of the change in the solution between steps
217933174efeSLois Curfman McInnes .  maxit - maximum number of iterations
2180c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
2181fee21e36SBarry Smith 
218233174efeSLois Curfman McInnes    Options Database Keys:
218370441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
2184c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
2185c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
2186c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
2187c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
21889b94acceSBarry Smith 
2189d7a720efSLois Curfman McInnes    Notes:
21909b94acceSBarry Smith    The default maximum number of iterations is 50.
21919b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
21929b94acceSBarry Smith 
219336851e7fSLois Curfman McInnes    Level: intermediate
219436851e7fSLois Curfman McInnes 
219533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
21969b94acceSBarry Smith 
21972492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
21989b94acceSBarry Smith @*/
21997087cfbeSBarry Smith PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
22009b94acceSBarry Smith {
22013a40ed3dSBarry Smith   PetscFunctionBegin;
22020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2203c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
2204c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
2205c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
2206c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
2207c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
2208c5eb9154SBarry Smith 
2209ab54825eSJed Brown   if (abstol != PETSC_DEFAULT) {
2210ab54825eSJed Brown     if (abstol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %G must be non-negative",abstol);
2211ab54825eSJed Brown     snes->abstol = abstol;
2212ab54825eSJed Brown   }
2213ab54825eSJed Brown   if (rtol != PETSC_DEFAULT) {
2214ab54825eSJed 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);
2215ab54825eSJed Brown     snes->rtol = rtol;
2216ab54825eSJed Brown   }
2217ab54825eSJed Brown   if (stol != PETSC_DEFAULT) {
2218ab54825eSJed Brown     if (stol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %G must be non-negative",stol);
2219ab54825eSJed Brown     snes->xtol = stol;
2220ab54825eSJed Brown   }
2221ab54825eSJed Brown   if (maxit != PETSC_DEFAULT) {
2222ab54825eSJed Brown     if (maxit < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit);
2223ab54825eSJed Brown     snes->max_its = maxit;
2224ab54825eSJed Brown   }
2225ab54825eSJed Brown   if (maxf != PETSC_DEFAULT) {
2226ab54825eSJed Brown     if (maxf < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be non-negative",maxf);
2227ab54825eSJed Brown     snes->max_funcs = maxf;
2228ab54825eSJed Brown   }
22293a40ed3dSBarry Smith   PetscFunctionReturn(0);
22309b94acceSBarry Smith }
22319b94acceSBarry Smith 
22324a2ae208SSatish Balay #undef __FUNCT__
22334a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
22349b94acceSBarry Smith /*@
223533174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
223633174efeSLois Curfman McInnes 
2237c7afd0dbSLois Curfman McInnes    Not Collective
2238c7afd0dbSLois Curfman McInnes 
223933174efeSLois Curfman McInnes    Input Parameters:
2240c7afd0dbSLois Curfman McInnes +  snes - the SNES context
224185385478SLisandro Dalcin .  atol - absolute convergence tolerance
224233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
224333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
224433174efeSLois Curfman McInnes            of the change in the solution between steps
224533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
2246c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
2247fee21e36SBarry Smith 
224833174efeSLois Curfman McInnes    Notes:
224933174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
225033174efeSLois Curfman McInnes 
225136851e7fSLois Curfman McInnes    Level: intermediate
225236851e7fSLois Curfman McInnes 
225333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
225433174efeSLois Curfman McInnes 
225533174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
225633174efeSLois Curfman McInnes @*/
22577087cfbeSBarry Smith PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
225833174efeSLois Curfman McInnes {
22593a40ed3dSBarry Smith   PetscFunctionBegin;
22600700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
226185385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
226233174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
226333174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
226433174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
226533174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
22663a40ed3dSBarry Smith   PetscFunctionReturn(0);
226733174efeSLois Curfman McInnes }
226833174efeSLois Curfman McInnes 
22694a2ae208SSatish Balay #undef __FUNCT__
22704a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
227133174efeSLois Curfman McInnes /*@
22729b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
22739b94acceSBarry Smith 
22743f9fe445SBarry Smith    Logically Collective on SNES
2275fee21e36SBarry Smith 
2276c7afd0dbSLois Curfman McInnes    Input Parameters:
2277c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2278c7afd0dbSLois Curfman McInnes -  tol - tolerance
2279c7afd0dbSLois Curfman McInnes 
22809b94acceSBarry Smith    Options Database Key:
2281c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
22829b94acceSBarry Smith 
228336851e7fSLois Curfman McInnes    Level: intermediate
228436851e7fSLois Curfman McInnes 
22859b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
22869b94acceSBarry Smith 
22872492ecdbSBarry Smith .seealso: SNESSetTolerances()
22889b94acceSBarry Smith @*/
22897087cfbeSBarry Smith PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
22909b94acceSBarry Smith {
22913a40ed3dSBarry Smith   PetscFunctionBegin;
22920700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2293c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
22949b94acceSBarry Smith   snes->deltatol = tol;
22953a40ed3dSBarry Smith   PetscFunctionReturn(0);
22969b94acceSBarry Smith }
22979b94acceSBarry Smith 
2298df9fa365SBarry Smith /*
2299df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
2300df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
2301df9fa365SBarry Smith    macros instead of functions
2302df9fa365SBarry Smith */
23034a2ae208SSatish Balay #undef __FUNCT__
2304a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
23057087cfbeSBarry Smith PetscErrorCode  SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
2306ce1608b8SBarry Smith {
2307dfbe8321SBarry Smith   PetscErrorCode ierr;
2308ce1608b8SBarry Smith 
2309ce1608b8SBarry Smith   PetscFunctionBegin;
23100700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2311a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
2312ce1608b8SBarry Smith   PetscFunctionReturn(0);
2313ce1608b8SBarry Smith }
2314ce1608b8SBarry Smith 
23154a2ae208SSatish Balay #undef __FUNCT__
2316a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
23177087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2318df9fa365SBarry Smith {
2319dfbe8321SBarry Smith   PetscErrorCode ierr;
2320df9fa365SBarry Smith 
2321df9fa365SBarry Smith   PetscFunctionBegin;
2322a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2323df9fa365SBarry Smith   PetscFunctionReturn(0);
2324df9fa365SBarry Smith }
2325df9fa365SBarry Smith 
23264a2ae208SSatish Balay #undef __FUNCT__
2327a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
23286bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGDestroy(PetscDrawLG *draw)
2329df9fa365SBarry Smith {
2330dfbe8321SBarry Smith   PetscErrorCode ierr;
2331df9fa365SBarry Smith 
2332df9fa365SBarry Smith   PetscFunctionBegin;
2333a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2334df9fa365SBarry Smith   PetscFunctionReturn(0);
2335df9fa365SBarry Smith }
2336df9fa365SBarry Smith 
23377087cfbeSBarry Smith extern PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
2338b271bb04SBarry Smith #undef __FUNCT__
2339b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
23407087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
2341b271bb04SBarry Smith {
2342b271bb04SBarry Smith   PetscDrawLG      lg;
2343b271bb04SBarry Smith   PetscErrorCode   ierr;
2344b271bb04SBarry Smith   PetscReal        x,y,per;
2345b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
2346b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
2347b271bb04SBarry Smith   PetscDraw        draw;
2348b271bb04SBarry Smith   PetscFunctionBegin;
2349b271bb04SBarry Smith   if (!monctx) {
2350b271bb04SBarry Smith     MPI_Comm    comm;
2351b271bb04SBarry Smith 
2352b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
2353b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
2354b271bb04SBarry Smith   }
2355b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
2356b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2357b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2358b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
2359b271bb04SBarry Smith   x = (PetscReal) n;
2360b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
2361b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2362b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2363b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2364b271bb04SBarry Smith   }
2365b271bb04SBarry Smith 
2366b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
2367b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2368b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2369b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
2370b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
2371b271bb04SBarry Smith   x = (PetscReal) n;
2372b271bb04SBarry Smith   y = 100.0*per;
2373b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2374b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2375b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2376b271bb04SBarry Smith   }
2377b271bb04SBarry Smith 
2378b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
2379b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2380b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2381b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
2382b271bb04SBarry Smith   x = (PetscReal) n;
2383b271bb04SBarry Smith   y = (prev - rnorm)/prev;
2384b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2385b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2386b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2387b271bb04SBarry Smith   }
2388b271bb04SBarry Smith 
2389b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
2390b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2391b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2392b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
2393b271bb04SBarry Smith   x = (PetscReal) n;
2394b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
2395b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
2396b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2397b271bb04SBarry Smith   }
2398b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2399b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2400b271bb04SBarry Smith   }
2401b271bb04SBarry Smith   prev = rnorm;
2402b271bb04SBarry Smith   PetscFunctionReturn(0);
2403b271bb04SBarry Smith }
2404b271bb04SBarry Smith 
2405b271bb04SBarry Smith #undef __FUNCT__
2406b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
24077087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2408b271bb04SBarry Smith {
2409b271bb04SBarry Smith   PetscErrorCode ierr;
2410b271bb04SBarry Smith 
2411b271bb04SBarry Smith   PetscFunctionBegin;
2412b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2413b271bb04SBarry Smith   PetscFunctionReturn(0);
2414b271bb04SBarry Smith }
2415b271bb04SBarry Smith 
2416b271bb04SBarry Smith #undef __FUNCT__
2417b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
24186bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGRangeDestroy(PetscDrawLG *draw)
2419b271bb04SBarry Smith {
2420b271bb04SBarry Smith   PetscErrorCode ierr;
2421b271bb04SBarry Smith 
2422b271bb04SBarry Smith   PetscFunctionBegin;
2423b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2424b271bb04SBarry Smith   PetscFunctionReturn(0);
2425b271bb04SBarry Smith }
2426b271bb04SBarry Smith 
24277a03ce2fSLisandro Dalcin #undef __FUNCT__
24287a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor"
2429228d79bcSJed Brown /*@
2430228d79bcSJed Brown    SNESMonitor - runs the user provided monitor routines, if they exist
2431228d79bcSJed Brown 
2432228d79bcSJed Brown    Collective on SNES
2433228d79bcSJed Brown 
2434228d79bcSJed Brown    Input Parameters:
2435228d79bcSJed Brown +  snes - nonlinear solver context obtained from SNESCreate()
2436228d79bcSJed Brown .  iter - iteration number
2437228d79bcSJed Brown -  rnorm - relative norm of the residual
2438228d79bcSJed Brown 
2439228d79bcSJed Brown    Notes:
2440228d79bcSJed Brown    This routine is called by the SNES implementations.
2441228d79bcSJed Brown    It does not typically need to be called by the user.
2442228d79bcSJed Brown 
2443228d79bcSJed Brown    Level: developer
2444228d79bcSJed Brown 
2445228d79bcSJed Brown .seealso: SNESMonitorSet()
2446228d79bcSJed Brown @*/
24477a03ce2fSLisandro Dalcin PetscErrorCode  SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm)
24487a03ce2fSLisandro Dalcin {
24497a03ce2fSLisandro Dalcin   PetscErrorCode ierr;
24507a03ce2fSLisandro Dalcin   PetscInt       i,n = snes->numbermonitors;
24517a03ce2fSLisandro Dalcin 
24527a03ce2fSLisandro Dalcin   PetscFunctionBegin;
24537a03ce2fSLisandro Dalcin   for (i=0; i<n; i++) {
24547a03ce2fSLisandro Dalcin     ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr);
24557a03ce2fSLisandro Dalcin   }
24567a03ce2fSLisandro Dalcin   PetscFunctionReturn(0);
24577a03ce2fSLisandro Dalcin }
24587a03ce2fSLisandro Dalcin 
24599b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
24609b94acceSBarry Smith 
24614a2ae208SSatish Balay #undef __FUNCT__
2462a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
24639b94acceSBarry Smith /*@C
2464a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
24659b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
24669b94acceSBarry Smith    progress.
24679b94acceSBarry Smith 
24683f9fe445SBarry Smith    Logically Collective on SNES
2469fee21e36SBarry Smith 
2470c7afd0dbSLois Curfman McInnes    Input Parameters:
2471c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2472c7afd0dbSLois Curfman McInnes .  func - monitoring routine
2473b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
2474e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
2475b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2476b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
24779b94acceSBarry Smith 
2478c7afd0dbSLois Curfman McInnes    Calling sequence of func:
2479a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
2480c7afd0dbSLois Curfman McInnes 
2481c7afd0dbSLois Curfman McInnes +    snes - the SNES context
2482c7afd0dbSLois Curfman McInnes .    its - iteration number
2483c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
248440a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
24859b94acceSBarry Smith 
24869665c990SLois Curfman McInnes    Options Database Keys:
2487a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
2488a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
2489a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
2490cca6129bSJed Brown -    -snes_monitor_cancel - cancels all monitors that have
2491c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
2492a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
2493c7afd0dbSLois Curfman McInnes                             does not cancel those set via
2494c7afd0dbSLois Curfman McInnes                             the options database.
24959665c990SLois Curfman McInnes 
2496639f9d9dSBarry Smith    Notes:
24976bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
2498a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
24996bc08f3fSLois Curfman McInnes    order in which they were set.
2500639f9d9dSBarry Smith 
2501025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
2502025f1a04SBarry Smith 
250336851e7fSLois Curfman McInnes    Level: intermediate
250436851e7fSLois Curfman McInnes 
25059b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
25069b94acceSBarry Smith 
2507a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
25089b94acceSBarry Smith @*/
2509c2efdce3SBarry Smith PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
25109b94acceSBarry Smith {
2511b90d0a6eSBarry Smith   PetscInt       i;
2512649052a6SBarry Smith   PetscErrorCode ierr;
2513b90d0a6eSBarry Smith 
25143a40ed3dSBarry Smith   PetscFunctionBegin;
25150700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
251617186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2517b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
2518649052a6SBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) {
2519649052a6SBarry Smith       if (monitordestroy) {
2520c2efdce3SBarry Smith         ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr);
2521649052a6SBarry Smith       }
2522b90d0a6eSBarry Smith       PetscFunctionReturn(0);
2523b90d0a6eSBarry Smith     }
2524b90d0a6eSBarry Smith   }
2525b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
2526b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
2527639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
25283a40ed3dSBarry Smith   PetscFunctionReturn(0);
25299b94acceSBarry Smith }
25309b94acceSBarry Smith 
25314a2ae208SSatish Balay #undef __FUNCT__
2532a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
25335cd90555SBarry Smith /*@C
2534a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
25355cd90555SBarry Smith 
25363f9fe445SBarry Smith    Logically Collective on SNES
2537c7afd0dbSLois Curfman McInnes 
25385cd90555SBarry Smith    Input Parameters:
25395cd90555SBarry Smith .  snes - the SNES context
25405cd90555SBarry Smith 
25411a480d89SAdministrator    Options Database Key:
2542a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
2543a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
2544c7afd0dbSLois Curfman McInnes     set via the options database
25455cd90555SBarry Smith 
25465cd90555SBarry Smith    Notes:
25475cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
25485cd90555SBarry Smith 
254936851e7fSLois Curfman McInnes    Level: intermediate
255036851e7fSLois Curfman McInnes 
25515cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
25525cd90555SBarry Smith 
2553a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
25545cd90555SBarry Smith @*/
25557087cfbeSBarry Smith PetscErrorCode  SNESMonitorCancel(SNES snes)
25565cd90555SBarry Smith {
2557d952e501SBarry Smith   PetscErrorCode ierr;
2558d952e501SBarry Smith   PetscInt       i;
2559d952e501SBarry Smith 
25605cd90555SBarry Smith   PetscFunctionBegin;
25610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2562d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2563d952e501SBarry Smith     if (snes->monitordestroy[i]) {
25643c4aec1bSBarry Smith       ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr);
2565d952e501SBarry Smith     }
2566d952e501SBarry Smith   }
25675cd90555SBarry Smith   snes->numbermonitors = 0;
25685cd90555SBarry Smith   PetscFunctionReturn(0);
25695cd90555SBarry Smith }
25705cd90555SBarry Smith 
25714a2ae208SSatish Balay #undef __FUNCT__
25724a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
25739b94acceSBarry Smith /*@C
25749b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
25759b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
25769b94acceSBarry Smith 
25773f9fe445SBarry Smith    Logically Collective on SNES
2578fee21e36SBarry Smith 
2579c7afd0dbSLois Curfman McInnes    Input Parameters:
2580c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2581c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
25827f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
25837f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
25849b94acceSBarry Smith 
2585c7afd0dbSLois Curfman McInnes    Calling sequence of func:
258606ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2587c7afd0dbSLois Curfman McInnes 
2588c7afd0dbSLois Curfman McInnes +    snes - the SNES context
258906ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2590c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2591184914b5SBarry Smith .    reason - reason for convergence/divergence
2592c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
25934b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
25944b27c08aSLois Curfman McInnes -    f - 2-norm of function
25959b94acceSBarry Smith 
259636851e7fSLois Curfman McInnes    Level: advanced
259736851e7fSLois Curfman McInnes 
25989b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
25999b94acceSBarry Smith 
260085385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
26019b94acceSBarry Smith @*/
26027087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
26039b94acceSBarry Smith {
26047f7931b9SBarry Smith   PetscErrorCode ierr;
26057f7931b9SBarry Smith 
26063a40ed3dSBarry Smith   PetscFunctionBegin;
26070700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
260885385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
26097f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
26107f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
26117f7931b9SBarry Smith   }
261285385478SLisandro Dalcin   snes->ops->converged        = func;
26137f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
261485385478SLisandro Dalcin   snes->cnvP                  = cctx;
26153a40ed3dSBarry Smith   PetscFunctionReturn(0);
26169b94acceSBarry Smith }
26179b94acceSBarry Smith 
26184a2ae208SSatish Balay #undef __FUNCT__
26194a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
262052baeb72SSatish Balay /*@
2621184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2622184914b5SBarry Smith 
2623184914b5SBarry Smith    Not Collective
2624184914b5SBarry Smith 
2625184914b5SBarry Smith    Input Parameter:
2626184914b5SBarry Smith .  snes - the SNES context
2627184914b5SBarry Smith 
2628184914b5SBarry Smith    Output Parameter:
26294d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2630184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2631184914b5SBarry Smith 
2632184914b5SBarry Smith    Level: intermediate
2633184914b5SBarry Smith 
2634184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2635184914b5SBarry Smith 
2636184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2637184914b5SBarry Smith 
263885385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2639184914b5SBarry Smith @*/
26407087cfbeSBarry Smith PetscErrorCode  SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2641184914b5SBarry Smith {
2642184914b5SBarry Smith   PetscFunctionBegin;
26430700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26444482741eSBarry Smith   PetscValidPointer(reason,2);
2645184914b5SBarry Smith   *reason = snes->reason;
2646184914b5SBarry Smith   PetscFunctionReturn(0);
2647184914b5SBarry Smith }
2648184914b5SBarry Smith 
26494a2ae208SSatish Balay #undef __FUNCT__
26504a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2651c9005455SLois Curfman McInnes /*@
2652c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2653c9005455SLois Curfman McInnes 
26543f9fe445SBarry Smith    Logically Collective on SNES
2655fee21e36SBarry Smith 
2656c7afd0dbSLois Curfman McInnes    Input Parameters:
2657c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
26588c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2659cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2660758f92a0SBarry Smith .  na  - size of a and its
266164731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2662758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2663c7afd0dbSLois Curfman McInnes 
2664308dcc3eSBarry Smith    Notes:
2665308dcc3eSBarry Smith    If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2666308dcc3eSBarry Smith    default array of length 10000 is allocated.
2667308dcc3eSBarry Smith 
2668c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2669c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2670c9005455SLois Curfman McInnes    during the section of code that is being timed.
2671c9005455SLois Curfman McInnes 
267236851e7fSLois Curfman McInnes    Level: intermediate
267336851e7fSLois Curfman McInnes 
2674c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2675758f92a0SBarry Smith 
267608405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2677758f92a0SBarry Smith 
2678c9005455SLois Curfman McInnes @*/
26797087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool  reset)
2680c9005455SLois Curfman McInnes {
2681308dcc3eSBarry Smith   PetscErrorCode ierr;
2682308dcc3eSBarry Smith 
26833a40ed3dSBarry Smith   PetscFunctionBegin;
26840700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26854482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2686a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2687308dcc3eSBarry Smith   if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) {
2688308dcc3eSBarry Smith     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2689308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr);
2690308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr);
2691308dcc3eSBarry Smith     snes->conv_malloc   = PETSC_TRUE;
2692308dcc3eSBarry Smith   }
2693c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2694758f92a0SBarry Smith   snes->conv_hist_its   = its;
2695758f92a0SBarry Smith   snes->conv_hist_max   = na;
2696a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2697758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2698758f92a0SBarry Smith   PetscFunctionReturn(0);
2699758f92a0SBarry Smith }
2700758f92a0SBarry Smith 
2701308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2702c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
2703c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
2704308dcc3eSBarry Smith EXTERN_C_BEGIN
2705308dcc3eSBarry Smith #undef __FUNCT__
2706308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab"
2707308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
2708308dcc3eSBarry Smith {
2709308dcc3eSBarry Smith   mxArray        *mat;
2710308dcc3eSBarry Smith   PetscInt       i;
2711308dcc3eSBarry Smith   PetscReal      *ar;
2712308dcc3eSBarry Smith 
2713308dcc3eSBarry Smith   PetscFunctionBegin;
2714308dcc3eSBarry Smith   mat  = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
2715308dcc3eSBarry Smith   ar   = (PetscReal*) mxGetData(mat);
2716308dcc3eSBarry Smith   for (i=0; i<snes->conv_hist_len; i++) {
2717308dcc3eSBarry Smith     ar[i] = snes->conv_hist[i];
2718308dcc3eSBarry Smith   }
2719308dcc3eSBarry Smith   PetscFunctionReturn(mat);
2720308dcc3eSBarry Smith }
2721308dcc3eSBarry Smith EXTERN_C_END
2722308dcc3eSBarry Smith #endif
2723308dcc3eSBarry Smith 
2724308dcc3eSBarry Smith 
27254a2ae208SSatish Balay #undef __FUNCT__
27264a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
27270c4c9dddSBarry Smith /*@C
2728758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2729758f92a0SBarry Smith 
27303f9fe445SBarry Smith    Not Collective
2731758f92a0SBarry Smith 
2732758f92a0SBarry Smith    Input Parameter:
2733758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2734758f92a0SBarry Smith 
2735758f92a0SBarry Smith    Output Parameters:
2736758f92a0SBarry Smith .  a   - array to hold history
2737758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2738758f92a0SBarry Smith          negative if not converged) for each solve.
2739758f92a0SBarry Smith -  na  - size of a and its
2740758f92a0SBarry Smith 
2741758f92a0SBarry Smith    Notes:
2742758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2743758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2744758f92a0SBarry Smith 
2745758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2746758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2747758f92a0SBarry Smith    during the section of code that is being timed.
2748758f92a0SBarry Smith 
2749758f92a0SBarry Smith    Level: intermediate
2750758f92a0SBarry Smith 
2751758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2752758f92a0SBarry Smith 
2753758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2754758f92a0SBarry Smith 
2755758f92a0SBarry Smith @*/
27567087cfbeSBarry Smith PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2757758f92a0SBarry Smith {
2758758f92a0SBarry Smith   PetscFunctionBegin;
27590700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2760758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2761758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2762758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
27633a40ed3dSBarry Smith   PetscFunctionReturn(0);
2764c9005455SLois Curfman McInnes }
2765c9005455SLois Curfman McInnes 
2766e74ef692SMatthew Knepley #undef __FUNCT__
2767e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2768ac226902SBarry Smith /*@C
276976b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
2770eec8f646SJed Brown   at the beginning of every iteration of the nonlinear solve. Specifically
27717e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
277276b2cf59SMatthew Knepley 
27733f9fe445SBarry Smith   Logically Collective on SNES
277476b2cf59SMatthew Knepley 
277576b2cf59SMatthew Knepley   Input Parameters:
277676b2cf59SMatthew Knepley . snes - The nonlinear solver context
277776b2cf59SMatthew Knepley . func - The function
277876b2cf59SMatthew Knepley 
277976b2cf59SMatthew Knepley   Calling sequence of func:
2780b5d30489SBarry Smith . func (SNES snes, PetscInt step);
278176b2cf59SMatthew Knepley 
278276b2cf59SMatthew Knepley . step - The current step of the iteration
278376b2cf59SMatthew Knepley 
2784fe97e370SBarry Smith   Level: advanced
2785fe97e370SBarry Smith 
2786fe97e370SBarry 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()
2787fe97e370SBarry Smith         This is not used by most users.
278876b2cf59SMatthew Knepley 
278976b2cf59SMatthew Knepley .keywords: SNES, update
2790b5d30489SBarry Smith 
279185385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
279276b2cf59SMatthew Knepley @*/
27937087cfbeSBarry Smith PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
279476b2cf59SMatthew Knepley {
279576b2cf59SMatthew Knepley   PetscFunctionBegin;
27960700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2797e7788613SBarry Smith   snes->ops->update = func;
279876b2cf59SMatthew Knepley   PetscFunctionReturn(0);
279976b2cf59SMatthew Knepley }
280076b2cf59SMatthew Knepley 
2801e74ef692SMatthew Knepley #undef __FUNCT__
2802e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
280376b2cf59SMatthew Knepley /*@
280476b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
280576b2cf59SMatthew Knepley 
280676b2cf59SMatthew Knepley   Not collective
280776b2cf59SMatthew Knepley 
280876b2cf59SMatthew Knepley   Input Parameters:
280976b2cf59SMatthew Knepley . snes - The nonlinear solver context
281076b2cf59SMatthew Knepley . step - The current step of the iteration
281176b2cf59SMatthew Knepley 
2812205452f4SMatthew Knepley   Level: intermediate
2813205452f4SMatthew Knepley 
281476b2cf59SMatthew Knepley .keywords: SNES, update
2815a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
281676b2cf59SMatthew Knepley @*/
28177087cfbeSBarry Smith PetscErrorCode  SNESDefaultUpdate(SNES snes, PetscInt step)
281876b2cf59SMatthew Knepley {
281976b2cf59SMatthew Knepley   PetscFunctionBegin;
282076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
282176b2cf59SMatthew Knepley }
282276b2cf59SMatthew Knepley 
28234a2ae208SSatish Balay #undef __FUNCT__
28244a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
28259b94acceSBarry Smith /*
28269b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
28279b94acceSBarry Smith    positive parameter delta.
28289b94acceSBarry Smith 
28299b94acceSBarry Smith     Input Parameters:
2830c7afd0dbSLois Curfman McInnes +   snes - the SNES context
28319b94acceSBarry Smith .   y - approximate solution of linear system
28329b94acceSBarry Smith .   fnorm - 2-norm of current function
2833c7afd0dbSLois Curfman McInnes -   delta - trust region size
28349b94acceSBarry Smith 
28359b94acceSBarry Smith     Output Parameters:
2836c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
28379b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
28389b94acceSBarry Smith     region, and exceeds zero otherwise.
2839c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
28409b94acceSBarry Smith 
28419b94acceSBarry Smith     Note:
28424b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
28439b94acceSBarry Smith     is set to be the maximum allowable step size.
28449b94acceSBarry Smith 
28459b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
28469b94acceSBarry Smith */
2847dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
28489b94acceSBarry Smith {
2849064f8208SBarry Smith   PetscReal      nrm;
2850ea709b57SSatish Balay   PetscScalar    cnorm;
2851dfbe8321SBarry Smith   PetscErrorCode ierr;
28523a40ed3dSBarry Smith 
28533a40ed3dSBarry Smith   PetscFunctionBegin;
28540700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
28550700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2856c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2857184914b5SBarry Smith 
2858064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2859064f8208SBarry Smith   if (nrm > *delta) {
2860064f8208SBarry Smith      nrm = *delta/nrm;
2861064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2862064f8208SBarry Smith      cnorm = nrm;
28632dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
28649b94acceSBarry Smith      *ynorm = *delta;
28659b94acceSBarry Smith   } else {
28669b94acceSBarry Smith      *gpnorm = 0.0;
2867064f8208SBarry Smith      *ynorm = nrm;
28689b94acceSBarry Smith   }
28693a40ed3dSBarry Smith   PetscFunctionReturn(0);
28709b94acceSBarry Smith }
28719b94acceSBarry Smith 
28724a2ae208SSatish Balay #undef __FUNCT__
28734a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
28746ce558aeSBarry Smith /*@C
2875f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2876f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
28779b94acceSBarry Smith 
2878c7afd0dbSLois Curfman McInnes    Collective on SNES
2879c7afd0dbSLois Curfman McInnes 
2880b2002411SLois Curfman McInnes    Input Parameters:
2881c7afd0dbSLois Curfman McInnes +  snes - the SNES context
28823cebf274SJed Brown .  b - the constant part of the equation F(x) = b, or PETSC_NULL to use zero.
288385385478SLisandro Dalcin -  x - the solution vector.
28849b94acceSBarry Smith 
2885b2002411SLois Curfman McInnes    Notes:
28868ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
28878ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
28888ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
28898ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
28908ddd3da0SLois Curfman McInnes 
289136851e7fSLois Curfman McInnes    Level: beginner
289236851e7fSLois Curfman McInnes 
28939b94acceSBarry Smith .keywords: SNES, nonlinear, solve
28949b94acceSBarry Smith 
2895c0df2a02SJed Brown .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetGridSequence(), SNESGetSolution()
28969b94acceSBarry Smith @*/
28977087cfbeSBarry Smith PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
28989b94acceSBarry Smith {
2899dfbe8321SBarry Smith   PetscErrorCode ierr;
2900ace3abfcSBarry Smith   PetscBool      flg;
2901eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2902eabae89aSBarry Smith   PetscViewer    viewer;
2903efd51863SBarry Smith   PetscInt       grid;
2904052efed2SBarry Smith 
29053a40ed3dSBarry Smith   PetscFunctionBegin;
29060700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
29070700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2908f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
29090700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
291085385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
291185385478SLisandro Dalcin 
2912a8248277SBarry Smith   for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);}
2913efd51863SBarry Smith   for (grid=0; grid<snes->gridsequence+1; grid++) {
2914efd51863SBarry Smith 
291585385478SLisandro Dalcin     /* set solution vector */
2916efd51863SBarry Smith     if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);}
29176bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
291885385478SLisandro Dalcin     snes->vec_sol = x;
291985385478SLisandro Dalcin     /* set afine vector if provided */
292085385478SLisandro Dalcin     if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
29216bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
292285385478SLisandro Dalcin     snes->vec_rhs = b;
292385385478SLisandro Dalcin 
292470e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
29253f149594SLisandro Dalcin 
2926d25893d9SBarry Smith     if (!grid && snes->ops->computeinitialguess) {
2927d25893d9SBarry Smith       ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr);
2928d25893d9SBarry Smith     }
2929d25893d9SBarry Smith 
2930abc0a331SBarry Smith     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
293150ffb88aSMatthew Knepley     snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2932d5e45103SBarry Smith 
29333f149594SLisandro Dalcin     ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
29344936397dSBarry Smith     ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
293585385478SLisandro Dalcin     ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
29364936397dSBarry Smith     if (snes->domainerror){
29374936397dSBarry Smith       snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
29384936397dSBarry Smith       snes->domainerror = PETSC_FALSE;
29394936397dSBarry Smith     }
294017186662SBarry Smith     if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
29413f149594SLisandro Dalcin 
29427adad957SLisandro Dalcin     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2943eabae89aSBarry Smith     if (flg && !PetscPreLoadingOn) {
29447adad957SLisandro Dalcin       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2945eabae89aSBarry Smith       ierr = SNESView(snes,viewer);CHKERRQ(ierr);
29466bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2947eabae89aSBarry Smith     }
2948eabae89aSBarry Smith 
294990d69ab7SBarry Smith     flg  = PETSC_FALSE;
2950acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2951da9b6338SBarry Smith     if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
29525968eb51SBarry Smith     if (snes->printreason) {
2953a8248277SBarry Smith       ierr = PetscViewerASCIIAddTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr);
29545968eb51SBarry Smith       if (snes->reason > 0) {
2955a8248277SBarry Smith         ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
29565968eb51SBarry Smith       } else {
2957a8248277SBarry Smith         ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
29585968eb51SBarry Smith       }
2959a8248277SBarry Smith       ierr = PetscViewerASCIISubtractTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr);
29605968eb51SBarry Smith     }
29615968eb51SBarry Smith 
29628501fc72SJed Brown     flg = PETSC_FALSE;
29638501fc72SJed Brown     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view_solution_vtk",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
29648501fc72SJed Brown     if (flg) {
29658501fc72SJed Brown       PetscViewer viewer;
29668501fc72SJed Brown       ierr = PetscViewerCreate(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
29678501fc72SJed Brown       ierr = PetscViewerSetType(viewer,PETSCVIEWERVTK);CHKERRQ(ierr);
29688501fc72SJed Brown       ierr = PetscViewerFileSetName(viewer,filename);CHKERRQ(ierr);
29698501fc72SJed Brown       ierr = VecView(snes->vec_sol,viewer);CHKERRQ(ierr);
29708501fc72SJed Brown       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
29718501fc72SJed Brown     }
29728501fc72SJed Brown 
2973e113a28aSBarry Smith     if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
2974efd51863SBarry Smith     if (grid <  snes->gridsequence) {
2975efd51863SBarry Smith       DM  fine;
2976efd51863SBarry Smith       Vec xnew;
2977efd51863SBarry Smith       Mat interp;
2978efd51863SBarry Smith 
2979efd51863SBarry Smith       ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr);
2980efd51863SBarry Smith       ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr);
2981efd51863SBarry Smith       ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr);
2982efd51863SBarry Smith       ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr);
2983efd51863SBarry Smith       ierr = MatDestroy(&interp);CHKERRQ(ierr);
2984efd51863SBarry Smith       x    = xnew;
2985efd51863SBarry Smith 
2986efd51863SBarry Smith       ierr = SNESReset(snes);CHKERRQ(ierr);
2987efd51863SBarry Smith       ierr = SNESSetDM(snes,fine);CHKERRQ(ierr);
2988efd51863SBarry Smith       ierr = DMDestroy(&fine);CHKERRQ(ierr);
2989a8248277SBarry Smith       ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);
2990efd51863SBarry Smith     }
2991efd51863SBarry Smith   }
29923a40ed3dSBarry Smith   PetscFunctionReturn(0);
29939b94acceSBarry Smith }
29949b94acceSBarry Smith 
29959b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
29969b94acceSBarry Smith 
29974a2ae208SSatish Balay #undef __FUNCT__
29984a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
299982bf6240SBarry Smith /*@C
30004b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
30019b94acceSBarry Smith 
3002fee21e36SBarry Smith    Collective on SNES
3003fee21e36SBarry Smith 
3004c7afd0dbSLois Curfman McInnes    Input Parameters:
3005c7afd0dbSLois Curfman McInnes +  snes - the SNES context
3006454a90a3SBarry Smith -  type - a known method
3007c7afd0dbSLois Curfman McInnes 
3008c7afd0dbSLois Curfman McInnes    Options Database Key:
3009454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
3010c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
3011ae12b187SLois Curfman McInnes 
30129b94acceSBarry Smith    Notes:
3013e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
30144b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
3015c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
30164b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
3017c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
30189b94acceSBarry Smith 
3019ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
3020ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
3021ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
3022ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
3023ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
3024ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
3025ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
3026ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
3027ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
3028b0a32e0cSBarry Smith   appropriate method.
302936851e7fSLois Curfman McInnes 
303036851e7fSLois Curfman McInnes   Level: intermediate
3031a703fe33SLois Curfman McInnes 
3032454a90a3SBarry Smith .keywords: SNES, set, type
3033435da068SBarry Smith 
3034435da068SBarry Smith .seealso: SNESType, SNESCreate()
3035435da068SBarry Smith 
30369b94acceSBarry Smith @*/
30377087cfbeSBarry Smith PetscErrorCode  SNESSetType(SNES snes,const SNESType type)
30389b94acceSBarry Smith {
3039dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
3040ace3abfcSBarry Smith   PetscBool      match;
30413a40ed3dSBarry Smith 
30423a40ed3dSBarry Smith   PetscFunctionBegin;
30430700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
30444482741eSBarry Smith   PetscValidCharPointer(type,2);
304582bf6240SBarry Smith 
30466831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
30470f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
304892ff6ae8SBarry Smith 
30494b91b6eaSBarry Smith   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
3050e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
305175396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
305275396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
305375396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
305475396ef9SLisandro Dalcin   snes->ops->setup          = 0;
305575396ef9SLisandro Dalcin   snes->ops->solve          = 0;
305675396ef9SLisandro Dalcin   snes->ops->view           = 0;
305775396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
305875396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
305975396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
306075396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
3061454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
306203bfa161SLisandro Dalcin   ierr = (*r)(snes);CHKERRQ(ierr);
30639fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
30649fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
30659fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr);
30669fb22e1aSBarry Smith   }
30679fb22e1aSBarry Smith #endif
30683a40ed3dSBarry Smith   PetscFunctionReturn(0);
30699b94acceSBarry Smith }
30709b94acceSBarry Smith 
3071a847f771SSatish Balay 
30729b94acceSBarry Smith /* --------------------------------------------------------------------- */
30734a2ae208SSatish Balay #undef __FUNCT__
30744a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
307552baeb72SSatish Balay /*@
30769b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
3077f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
30789b94acceSBarry Smith 
3079fee21e36SBarry Smith    Not Collective
3080fee21e36SBarry Smith 
308136851e7fSLois Curfman McInnes    Level: advanced
308236851e7fSLois Curfman McInnes 
30839b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
30849b94acceSBarry Smith 
30859b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
30869b94acceSBarry Smith @*/
30877087cfbeSBarry Smith PetscErrorCode  SNESRegisterDestroy(void)
30889b94acceSBarry Smith {
3089dfbe8321SBarry Smith   PetscErrorCode ierr;
309082bf6240SBarry Smith 
30913a40ed3dSBarry Smith   PetscFunctionBegin;
30921441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
30934c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
30943a40ed3dSBarry Smith   PetscFunctionReturn(0);
30959b94acceSBarry Smith }
30969b94acceSBarry Smith 
30974a2ae208SSatish Balay #undef __FUNCT__
30984a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
30999b94acceSBarry Smith /*@C
31009a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
31019b94acceSBarry Smith 
3102c7afd0dbSLois Curfman McInnes    Not Collective
3103c7afd0dbSLois Curfman McInnes 
31049b94acceSBarry Smith    Input Parameter:
31054b0e389bSBarry Smith .  snes - nonlinear solver context
31069b94acceSBarry Smith 
31079b94acceSBarry Smith    Output Parameter:
31083a7fca6bSBarry Smith .  type - SNES method (a character string)
31099b94acceSBarry Smith 
311036851e7fSLois Curfman McInnes    Level: intermediate
311136851e7fSLois Curfman McInnes 
3112454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
31139b94acceSBarry Smith @*/
31147087cfbeSBarry Smith PetscErrorCode  SNESGetType(SNES snes,const SNESType *type)
31159b94acceSBarry Smith {
31163a40ed3dSBarry Smith   PetscFunctionBegin;
31170700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31184482741eSBarry Smith   PetscValidPointer(type,2);
31197adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
31203a40ed3dSBarry Smith   PetscFunctionReturn(0);
31219b94acceSBarry Smith }
31229b94acceSBarry Smith 
31234a2ae208SSatish Balay #undef __FUNCT__
31244a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
312552baeb72SSatish Balay /*@
31269b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
3127c0df2a02SJed Brown    stored. This is the fine grid solution when using SNESSetGridSequence().
31289b94acceSBarry Smith 
3129c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
3130c7afd0dbSLois Curfman McInnes 
31319b94acceSBarry Smith    Input Parameter:
31329b94acceSBarry Smith .  snes - the SNES context
31339b94acceSBarry Smith 
31349b94acceSBarry Smith    Output Parameter:
31359b94acceSBarry Smith .  x - the solution
31369b94acceSBarry Smith 
313770e92668SMatthew Knepley    Level: intermediate
313836851e7fSLois Curfman McInnes 
31399b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
31409b94acceSBarry Smith 
314185385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
31429b94acceSBarry Smith @*/
31437087cfbeSBarry Smith PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
31449b94acceSBarry Smith {
31453a40ed3dSBarry Smith   PetscFunctionBegin;
31460700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31474482741eSBarry Smith   PetscValidPointer(x,2);
314885385478SLisandro Dalcin   *x = snes->vec_sol;
314970e92668SMatthew Knepley   PetscFunctionReturn(0);
315070e92668SMatthew Knepley }
315170e92668SMatthew Knepley 
315270e92668SMatthew Knepley #undef __FUNCT__
31534a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
315452baeb72SSatish Balay /*@
31559b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
31569b94acceSBarry Smith    stored.
31579b94acceSBarry Smith 
3158c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
3159c7afd0dbSLois Curfman McInnes 
31609b94acceSBarry Smith    Input Parameter:
31619b94acceSBarry Smith .  snes - the SNES context
31629b94acceSBarry Smith 
31639b94acceSBarry Smith    Output Parameter:
31649b94acceSBarry Smith .  x - the solution update
31659b94acceSBarry Smith 
316636851e7fSLois Curfman McInnes    Level: advanced
316736851e7fSLois Curfman McInnes 
31689b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
31699b94acceSBarry Smith 
317085385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
31719b94acceSBarry Smith @*/
31727087cfbeSBarry Smith PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
31739b94acceSBarry Smith {
31743a40ed3dSBarry Smith   PetscFunctionBegin;
31750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31764482741eSBarry Smith   PetscValidPointer(x,2);
317785385478SLisandro Dalcin   *x = snes->vec_sol_update;
31783a40ed3dSBarry Smith   PetscFunctionReturn(0);
31799b94acceSBarry Smith }
31809b94acceSBarry Smith 
31814a2ae208SSatish Balay #undef __FUNCT__
31824a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
31839b94acceSBarry Smith /*@C
31843638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
31859b94acceSBarry Smith 
3186c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
3187c7afd0dbSLois Curfman McInnes 
31889b94acceSBarry Smith    Input Parameter:
31899b94acceSBarry Smith .  snes - the SNES context
31909b94acceSBarry Smith 
31919b94acceSBarry Smith    Output Parameter:
31927bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
319370e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
319470e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
31959b94acceSBarry Smith 
319636851e7fSLois Curfman McInnes    Level: advanced
319736851e7fSLois Curfman McInnes 
3198a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
31999b94acceSBarry Smith 
32004b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
32019b94acceSBarry Smith @*/
32027087cfbeSBarry Smith PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
32039b94acceSBarry Smith {
32043a40ed3dSBarry Smith   PetscFunctionBegin;
32050700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
320685385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
3207e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
320870e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
32093a40ed3dSBarry Smith   PetscFunctionReturn(0);
32109b94acceSBarry Smith }
32119b94acceSBarry Smith 
3212c79ef259SPeter Brune /*@C
3213c79ef259SPeter Brune    SNESGetGS - Returns the GS function and context.
3214c79ef259SPeter Brune 
3215c79ef259SPeter Brune    Input Parameter:
3216c79ef259SPeter Brune .  snes - the SNES context
3217c79ef259SPeter Brune 
3218c79ef259SPeter Brune    Output Parameter:
3219c79ef259SPeter Brune +  gsfunc - the function (or PETSC_NULL)
3220c79ef259SPeter Brune -  ctx    - the function context (or PETSC_NULL)
3221c79ef259SPeter Brune 
3222c79ef259SPeter Brune    Level: advanced
3223c79ef259SPeter Brune 
3224c79ef259SPeter Brune .keywords: SNES, nonlinear, get, function
3225c79ef259SPeter Brune 
3226c79ef259SPeter Brune .seealso: SNESSetGS(), SNESGetFunction()
3227c79ef259SPeter Brune @*/
3228c79ef259SPeter Brune 
32294a2ae208SSatish Balay #undef __FUNCT__
3230646217ecSPeter Brune #define __FUNCT__ "SNESGetGS"
3231646217ecSPeter Brune PetscErrorCode SNESGetGS (SNES snes, PetscErrorCode(**func)(SNES, Vec, Vec, void*), void ** ctx)
3232646217ecSPeter Brune {
3233646217ecSPeter Brune   PetscFunctionBegin;
3234646217ecSPeter Brune   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3235646217ecSPeter Brune   if (func) *func = snes->ops->computegs;
3236646217ecSPeter Brune   if (ctx)  *ctx  = snes->funP;
3237646217ecSPeter Brune   PetscFunctionReturn(0);
3238646217ecSPeter Brune }
3239646217ecSPeter Brune 
3240646217ecSPeter Brune #undef __FUNCT__
32414a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
32423c7409f5SSatish Balay /*@C
32433c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
3244d850072dSLois Curfman McInnes    SNES options in the database.
32453c7409f5SSatish Balay 
32463f9fe445SBarry Smith    Logically Collective on SNES
3247fee21e36SBarry Smith 
3248c7afd0dbSLois Curfman McInnes    Input Parameter:
3249c7afd0dbSLois Curfman McInnes +  snes - the SNES context
3250c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
3251c7afd0dbSLois Curfman McInnes 
3252d850072dSLois Curfman McInnes    Notes:
3253a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
3254c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
3255d850072dSLois Curfman McInnes 
325636851e7fSLois Curfman McInnes    Level: advanced
325736851e7fSLois Curfman McInnes 
32583c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
3259a86d99e1SLois Curfman McInnes 
3260a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
32613c7409f5SSatish Balay @*/
32627087cfbeSBarry Smith PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
32633c7409f5SSatish Balay {
3264dfbe8321SBarry Smith   PetscErrorCode ierr;
32653c7409f5SSatish Balay 
32663a40ed3dSBarry Smith   PetscFunctionBegin;
32670700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3268639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
32691cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
327094b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
32713a40ed3dSBarry Smith   PetscFunctionReturn(0);
32723c7409f5SSatish Balay }
32733c7409f5SSatish Balay 
32744a2ae208SSatish Balay #undef __FUNCT__
32754a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
32763c7409f5SSatish Balay /*@C
3277f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
3278d850072dSLois Curfman McInnes    SNES options in the database.
32793c7409f5SSatish Balay 
32803f9fe445SBarry Smith    Logically Collective on SNES
3281fee21e36SBarry Smith 
3282c7afd0dbSLois Curfman McInnes    Input Parameters:
3283c7afd0dbSLois Curfman McInnes +  snes - the SNES context
3284c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
3285c7afd0dbSLois Curfman McInnes 
3286d850072dSLois Curfman McInnes    Notes:
3287a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
3288c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
3289d850072dSLois Curfman McInnes 
329036851e7fSLois Curfman McInnes    Level: advanced
329136851e7fSLois Curfman McInnes 
32923c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
3293a86d99e1SLois Curfman McInnes 
3294a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
32953c7409f5SSatish Balay @*/
32967087cfbeSBarry Smith PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
32973c7409f5SSatish Balay {
3298dfbe8321SBarry Smith   PetscErrorCode ierr;
32993c7409f5SSatish Balay 
33003a40ed3dSBarry Smith   PetscFunctionBegin;
33010700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3302639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
33031cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
330494b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
33053a40ed3dSBarry Smith   PetscFunctionReturn(0);
33063c7409f5SSatish Balay }
33073c7409f5SSatish Balay 
33084a2ae208SSatish Balay #undef __FUNCT__
33094a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
33109ab63eb5SSatish Balay /*@C
33113c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
33123c7409f5SSatish Balay    SNES options in the database.
33133c7409f5SSatish Balay 
3314c7afd0dbSLois Curfman McInnes    Not Collective
3315c7afd0dbSLois Curfman McInnes 
33163c7409f5SSatish Balay    Input Parameter:
33173c7409f5SSatish Balay .  snes - the SNES context
33183c7409f5SSatish Balay 
33193c7409f5SSatish Balay    Output Parameter:
33203c7409f5SSatish Balay .  prefix - pointer to the prefix string used
33213c7409f5SSatish Balay 
33224ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
33239ab63eb5SSatish Balay    sufficient length to hold the prefix.
33249ab63eb5SSatish Balay 
332536851e7fSLois Curfman McInnes    Level: advanced
332636851e7fSLois Curfman McInnes 
33273c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
3328a86d99e1SLois Curfman McInnes 
3329a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
33303c7409f5SSatish Balay @*/
33317087cfbeSBarry Smith PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
33323c7409f5SSatish Balay {
3333dfbe8321SBarry Smith   PetscErrorCode ierr;
33343c7409f5SSatish Balay 
33353a40ed3dSBarry Smith   PetscFunctionBegin;
33360700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3337639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
33383a40ed3dSBarry Smith   PetscFunctionReturn(0);
33393c7409f5SSatish Balay }
33403c7409f5SSatish Balay 
3341b2002411SLois Curfman McInnes 
33424a2ae208SSatish Balay #undef __FUNCT__
33434a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
33443cea93caSBarry Smith /*@C
33453cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
33463cea93caSBarry Smith 
33477f6c08e0SMatthew Knepley   Level: advanced
33483cea93caSBarry Smith @*/
33497087cfbeSBarry Smith PetscErrorCode  SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
3350b2002411SLois Curfman McInnes {
3351e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
3352dfbe8321SBarry Smith   PetscErrorCode ierr;
3353b2002411SLois Curfman McInnes 
3354b2002411SLois Curfman McInnes   PetscFunctionBegin;
3355b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
3356c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
3357b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
3358b2002411SLois Curfman McInnes }
3359da9b6338SBarry Smith 
3360da9b6338SBarry Smith #undef __FUNCT__
3361da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
33627087cfbeSBarry Smith PetscErrorCode  SNESTestLocalMin(SNES snes)
3363da9b6338SBarry Smith {
3364dfbe8321SBarry Smith   PetscErrorCode ierr;
336577431f27SBarry Smith   PetscInt       N,i,j;
3366da9b6338SBarry Smith   Vec            u,uh,fh;
3367da9b6338SBarry Smith   PetscScalar    value;
3368da9b6338SBarry Smith   PetscReal      norm;
3369da9b6338SBarry Smith 
3370da9b6338SBarry Smith   PetscFunctionBegin;
3371da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
3372da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
3373da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
3374da9b6338SBarry Smith 
3375da9b6338SBarry Smith   /* currently only works for sequential */
3376da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
3377da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
3378da9b6338SBarry Smith   for (i=0; i<N; i++) {
3379da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
338077431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
3381da9b6338SBarry Smith     for (j=-10; j<11; j++) {
3382ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
3383da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
33843ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
3385da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
338677431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
3387da9b6338SBarry Smith       value = -value;
3388da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
3389da9b6338SBarry Smith     }
3390da9b6338SBarry Smith   }
33916bf464f9SBarry Smith   ierr = VecDestroy(&uh);CHKERRQ(ierr);
33926bf464f9SBarry Smith   ierr = VecDestroy(&fh);CHKERRQ(ierr);
3393da9b6338SBarry Smith   PetscFunctionReturn(0);
3394da9b6338SBarry Smith }
339571f87433Sdalcinl 
339671f87433Sdalcinl #undef __FUNCT__
3397fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
339871f87433Sdalcinl /*@
3399fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
340071f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
340171f87433Sdalcinl    Newton method.
340271f87433Sdalcinl 
34033f9fe445SBarry Smith    Logically Collective on SNES
340471f87433Sdalcinl 
340571f87433Sdalcinl    Input Parameters:
340671f87433Sdalcinl +  snes - SNES context
340771f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
340871f87433Sdalcinl 
340964ba62caSBarry Smith     Options Database:
341064ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
341164ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
341264ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
341364ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
341464ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
341564ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
341664ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
341764ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
341864ba62caSBarry Smith 
341971f87433Sdalcinl    Notes:
342071f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
342171f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
342271f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
342371f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
342471f87433Sdalcinl    solver.
342571f87433Sdalcinl 
342671f87433Sdalcinl    Level: advanced
342771f87433Sdalcinl 
342871f87433Sdalcinl    Reference:
342971f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
343071f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
343171f87433Sdalcinl 
343271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
343371f87433Sdalcinl 
3434fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
343571f87433Sdalcinl @*/
34367087cfbeSBarry Smith PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool  flag)
343771f87433Sdalcinl {
343871f87433Sdalcinl   PetscFunctionBegin;
34390700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3440acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flag,2);
344171f87433Sdalcinl   snes->ksp_ewconv = flag;
344271f87433Sdalcinl   PetscFunctionReturn(0);
344371f87433Sdalcinl }
344471f87433Sdalcinl 
344571f87433Sdalcinl #undef __FUNCT__
3446fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
344771f87433Sdalcinl /*@
3448fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
344971f87433Sdalcinl    for computing relative tolerance for linear solvers within an
345071f87433Sdalcinl    inexact Newton method.
345171f87433Sdalcinl 
345271f87433Sdalcinl    Not Collective
345371f87433Sdalcinl 
345471f87433Sdalcinl    Input Parameter:
345571f87433Sdalcinl .  snes - SNES context
345671f87433Sdalcinl 
345771f87433Sdalcinl    Output Parameter:
345871f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
345971f87433Sdalcinl 
346071f87433Sdalcinl    Notes:
346171f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
346271f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
346371f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
346471f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
346571f87433Sdalcinl    solver.
346671f87433Sdalcinl 
346771f87433Sdalcinl    Level: advanced
346871f87433Sdalcinl 
346971f87433Sdalcinl    Reference:
347071f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
347171f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
347271f87433Sdalcinl 
347371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
347471f87433Sdalcinl 
3475fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
347671f87433Sdalcinl @*/
34777087cfbeSBarry Smith PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
347871f87433Sdalcinl {
347971f87433Sdalcinl   PetscFunctionBegin;
34800700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
348171f87433Sdalcinl   PetscValidPointer(flag,2);
348271f87433Sdalcinl   *flag = snes->ksp_ewconv;
348371f87433Sdalcinl   PetscFunctionReturn(0);
348471f87433Sdalcinl }
348571f87433Sdalcinl 
348671f87433Sdalcinl #undef __FUNCT__
3487fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
348871f87433Sdalcinl /*@
3489fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
349071f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
349171f87433Sdalcinl    Newton method.
349271f87433Sdalcinl 
34933f9fe445SBarry Smith    Logically Collective on SNES
349471f87433Sdalcinl 
349571f87433Sdalcinl    Input Parameters:
349671f87433Sdalcinl +    snes - SNES context
349771f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
349871f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
349971f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
350071f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
350171f87433Sdalcinl              (0 <= gamma2 <= 1)
350271f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
350371f87433Sdalcinl .    alpha2 - power for safeguard
350471f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
350571f87433Sdalcinl 
350671f87433Sdalcinl    Note:
350771f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
350871f87433Sdalcinl 
350971f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
351071f87433Sdalcinl 
351171f87433Sdalcinl    Level: advanced
351271f87433Sdalcinl 
351371f87433Sdalcinl    Reference:
351471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
351571f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
351671f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
351771f87433Sdalcinl 
351871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
351971f87433Sdalcinl 
3520fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
352171f87433Sdalcinl @*/
35227087cfbeSBarry Smith PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
352371f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
352471f87433Sdalcinl {
3525fa9f3622SBarry Smith   SNESKSPEW *kctx;
352671f87433Sdalcinl   PetscFunctionBegin;
35270700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3528fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3529e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
3530c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
3531c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
3532c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
3533c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
3534c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
3535c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
3536c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
353771f87433Sdalcinl 
353871f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
353971f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
354071f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
354171f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
354271f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
354371f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
354471f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
354571f87433Sdalcinl 
354671f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
3547e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
354871f87433Sdalcinl   }
354971f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
3550e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
355171f87433Sdalcinl   }
355271f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
3553e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
355471f87433Sdalcinl   }
355571f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
3556e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
355771f87433Sdalcinl   }
355871f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
3559e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
356071f87433Sdalcinl   }
356171f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
3562e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
356371f87433Sdalcinl   }
356471f87433Sdalcinl   PetscFunctionReturn(0);
356571f87433Sdalcinl }
356671f87433Sdalcinl 
356771f87433Sdalcinl #undef __FUNCT__
3568fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
356971f87433Sdalcinl /*@
3570fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
357171f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
357271f87433Sdalcinl    Newton method.
357371f87433Sdalcinl 
357471f87433Sdalcinl    Not Collective
357571f87433Sdalcinl 
357671f87433Sdalcinl    Input Parameters:
357771f87433Sdalcinl      snes - SNES context
357871f87433Sdalcinl 
357971f87433Sdalcinl    Output Parameters:
358071f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
358171f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
358271f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
358371f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
358471f87433Sdalcinl              (0 <= gamma2 <= 1)
358571f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
358671f87433Sdalcinl .    alpha2 - power for safeguard
358771f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
358871f87433Sdalcinl 
358971f87433Sdalcinl    Level: advanced
359071f87433Sdalcinl 
359171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
359271f87433Sdalcinl 
3593fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
359471f87433Sdalcinl @*/
35957087cfbeSBarry Smith PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
359671f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
359771f87433Sdalcinl {
3598fa9f3622SBarry Smith   SNESKSPEW *kctx;
359971f87433Sdalcinl   PetscFunctionBegin;
36000700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3601fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3602e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
360371f87433Sdalcinl   if(version)   *version   = kctx->version;
360471f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
360571f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
360671f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
360771f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
360871f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
360971f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
361071f87433Sdalcinl   PetscFunctionReturn(0);
361171f87433Sdalcinl }
361271f87433Sdalcinl 
361371f87433Sdalcinl #undef __FUNCT__
3614fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
3615fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
361671f87433Sdalcinl {
361771f87433Sdalcinl   PetscErrorCode ierr;
3618fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
361971f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
362071f87433Sdalcinl 
362171f87433Sdalcinl   PetscFunctionBegin;
3622e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
362371f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
362471f87433Sdalcinl     rtol = kctx->rtol_0;
362571f87433Sdalcinl   } else {
362671f87433Sdalcinl     if (kctx->version == 1) {
362771f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
362871f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
362971f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
363071f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
363171f87433Sdalcinl     } else if (kctx->version == 2) {
363271f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
363371f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
363471f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
363571f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
363671f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
363771f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
363871f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
363971f87433Sdalcinl       stol = PetscMax(rtol,stol);
364071f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
364171f87433Sdalcinl       /* safeguard: avoid oversolving */
364271f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
364371f87433Sdalcinl       stol = PetscMax(rtol,stol);
364471f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
3645e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
364671f87433Sdalcinl   }
364771f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
364871f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
364971f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
365071f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
365171f87433Sdalcinl   PetscFunctionReturn(0);
365271f87433Sdalcinl }
365371f87433Sdalcinl 
365471f87433Sdalcinl #undef __FUNCT__
3655fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
3656fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
365771f87433Sdalcinl {
365871f87433Sdalcinl   PetscErrorCode ierr;
3659fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
366071f87433Sdalcinl   PCSide         pcside;
366171f87433Sdalcinl   Vec            lres;
366271f87433Sdalcinl 
366371f87433Sdalcinl   PetscFunctionBegin;
3664e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
366571f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
366671f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
366771f87433Sdalcinl   if (kctx->version == 1) {
3668b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
366971f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
367071f87433Sdalcinl       /* KSP residual is true linear residual */
367171f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
367271f87433Sdalcinl     } else {
367371f87433Sdalcinl       /* KSP residual is preconditioned residual */
367471f87433Sdalcinl       /* compute true linear residual norm */
367571f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
367671f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
367771f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
367871f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
36796bf464f9SBarry Smith       ierr = VecDestroy(&lres);CHKERRQ(ierr);
368071f87433Sdalcinl     }
368171f87433Sdalcinl   }
368271f87433Sdalcinl   PetscFunctionReturn(0);
368371f87433Sdalcinl }
368471f87433Sdalcinl 
368571f87433Sdalcinl #undef __FUNCT__
368671f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
368771f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
368871f87433Sdalcinl {
368971f87433Sdalcinl   PetscErrorCode ierr;
369071f87433Sdalcinl 
369171f87433Sdalcinl   PetscFunctionBegin;
3692fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
369371f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3694fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
369571f87433Sdalcinl   PetscFunctionReturn(0);
369671f87433Sdalcinl }
36976c699258SBarry Smith 
36986c699258SBarry Smith #undef __FUNCT__
36996c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
37006c699258SBarry Smith /*@
37016c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
37026c699258SBarry Smith 
37033f9fe445SBarry Smith    Logically Collective on SNES
37046c699258SBarry Smith 
37056c699258SBarry Smith    Input Parameters:
37066c699258SBarry Smith +  snes - the preconditioner context
37076c699258SBarry Smith -  dm - the dm
37086c699258SBarry Smith 
37096c699258SBarry Smith    Level: intermediate
37106c699258SBarry Smith 
37116c699258SBarry Smith 
37126c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
37136c699258SBarry Smith @*/
37147087cfbeSBarry Smith PetscErrorCode  SNESSetDM(SNES snes,DM dm)
37156c699258SBarry Smith {
37166c699258SBarry Smith   PetscErrorCode ierr;
3717345fed2cSBarry Smith   KSP            ksp;
37186c699258SBarry Smith 
37196c699258SBarry Smith   PetscFunctionBegin;
37200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3721d0660788SBarry Smith   if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);}
37226bf464f9SBarry Smith   ierr = DMDestroy(&snes->dm);CHKERRQ(ierr);
37236c699258SBarry Smith   snes->dm = dm;
3724345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3725345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3726f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
37272c155ee1SBarry Smith   if (snes->pc) {
37282c155ee1SBarry Smith     ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr);
37292c155ee1SBarry Smith   }
37306c699258SBarry Smith   PetscFunctionReturn(0);
37316c699258SBarry Smith }
37326c699258SBarry Smith 
37336c699258SBarry Smith #undef __FUNCT__
37346c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
37356c699258SBarry Smith /*@
37366c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
37376c699258SBarry Smith 
37383f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
37396c699258SBarry Smith 
37406c699258SBarry Smith    Input Parameter:
37416c699258SBarry Smith . snes - the preconditioner context
37426c699258SBarry Smith 
37436c699258SBarry Smith    Output Parameter:
37446c699258SBarry Smith .  dm - the dm
37456c699258SBarry Smith 
37466c699258SBarry Smith    Level: intermediate
37476c699258SBarry Smith 
37486c699258SBarry Smith 
37496c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
37506c699258SBarry Smith @*/
37517087cfbeSBarry Smith PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
37526c699258SBarry Smith {
37536c699258SBarry Smith   PetscFunctionBegin;
37540700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
37556c699258SBarry Smith   *dm = snes->dm;
37566c699258SBarry Smith   PetscFunctionReturn(0);
37576c699258SBarry Smith }
37580807856dSBarry Smith 
375931823bd8SMatthew G Knepley #undef __FUNCT__
376031823bd8SMatthew G Knepley #define __FUNCT__ "SNESSetPC"
376131823bd8SMatthew G Knepley /*@
3762fd4b34e9SJed Brown   SNESSetPC - Sets the nonlinear preconditioner to be used.
376331823bd8SMatthew G Knepley 
376431823bd8SMatthew G Knepley   Collective on SNES
376531823bd8SMatthew G Knepley 
376631823bd8SMatthew G Knepley   Input Parameters:
376731823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate()
376831823bd8SMatthew G Knepley - pc   - the preconditioner object
376931823bd8SMatthew G Knepley 
377031823bd8SMatthew G Knepley   Notes:
377131823bd8SMatthew G Knepley   Use SNESGetPC() to retrieve the preconditioner context (for example,
377231823bd8SMatthew G Knepley   to configure it using the API).
377331823bd8SMatthew G Knepley 
377431823bd8SMatthew G Knepley   Level: developer
377531823bd8SMatthew G Knepley 
377631823bd8SMatthew G Knepley .keywords: SNES, set, precondition
377731823bd8SMatthew G Knepley .seealso: SNESGetPC()
377831823bd8SMatthew G Knepley @*/
377931823bd8SMatthew G Knepley PetscErrorCode SNESSetPC(SNES snes, SNES pc)
378031823bd8SMatthew G Knepley {
378131823bd8SMatthew G Knepley   PetscErrorCode ierr;
378231823bd8SMatthew G Knepley 
378331823bd8SMatthew G Knepley   PetscFunctionBegin;
378431823bd8SMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
378531823bd8SMatthew G Knepley   PetscValidHeaderSpecific(pc, SNES_CLASSID, 2);
378631823bd8SMatthew G Knepley   PetscCheckSameComm(snes, 1, pc, 2);
378731823bd8SMatthew G Knepley   ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr);
3788bf11d3b6SBarry Smith   ierr = SNESDestroy(&snes->pc);CHKERRQ(ierr);
378931823bd8SMatthew G Knepley   snes->pc = pc;
379031823bd8SMatthew G Knepley   ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr);
379131823bd8SMatthew G Knepley   PetscFunctionReturn(0);
379231823bd8SMatthew G Knepley }
379331823bd8SMatthew G Knepley 
379431823bd8SMatthew G Knepley #undef __FUNCT__
379531823bd8SMatthew G Knepley #define __FUNCT__ "SNESGetPC"
379631823bd8SMatthew G Knepley /*@
3797fd4b34e9SJed Brown   SNESGetPC - Returns a pointer to the nonlinear preconditioning context set with SNESSetPC().
379831823bd8SMatthew G Knepley 
379931823bd8SMatthew G Knepley   Not Collective
380031823bd8SMatthew G Knepley 
380131823bd8SMatthew G Knepley   Input Parameter:
380231823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate()
380331823bd8SMatthew G Knepley 
380431823bd8SMatthew G Knepley   Output Parameter:
380531823bd8SMatthew G Knepley . pc - preconditioner context
380631823bd8SMatthew G Knepley 
380731823bd8SMatthew G Knepley   Level: developer
380831823bd8SMatthew G Knepley 
380931823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner
381031823bd8SMatthew G Knepley .seealso: SNESSetPC()
381131823bd8SMatthew G Knepley @*/
381231823bd8SMatthew G Knepley PetscErrorCode SNESGetPC(SNES snes, SNES *pc)
381331823bd8SMatthew G Knepley {
381431823bd8SMatthew G Knepley   PetscErrorCode ierr;
381531823bd8SMatthew G Knepley 
381631823bd8SMatthew G Knepley   PetscFunctionBegin;
381731823bd8SMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
381831823bd8SMatthew G Knepley   PetscValidPointer(pc, 2);
381931823bd8SMatthew G Knepley   if (!snes->pc) {
382031823bd8SMatthew G Knepley     ierr = SNESCreate(((PetscObject) snes)->comm, &snes->pc);CHKERRQ(ierr);
38214a0c5b0cSMatthew G Knepley     ierr = PetscObjectIncrementTabLevel((PetscObject) snes->pc, (PetscObject) snes, 1);CHKERRQ(ierr);
382231823bd8SMatthew G Knepley     ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr);
382331823bd8SMatthew G Knepley   }
382431823bd8SMatthew G Knepley   *pc = snes->pc;
382531823bd8SMatthew G Knepley   PetscFunctionReturn(0);
382631823bd8SMatthew G Knepley }
382731823bd8SMatthew G Knepley 
382869b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3829c6db04a5SJed Brown #include <mex.h>
383069b4f73cSBarry Smith 
38318f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
38328f6e6473SBarry Smith 
38330807856dSBarry Smith #undef __FUNCT__
38340807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab"
38350807856dSBarry Smith /*
38360807856dSBarry Smith    SNESComputeFunction_Matlab - Calls the function that has been set with
38370807856dSBarry Smith                          SNESSetFunctionMatlab().
38380807856dSBarry Smith 
38390807856dSBarry Smith    Collective on SNES
38400807856dSBarry Smith 
38410807856dSBarry Smith    Input Parameters:
38420807856dSBarry Smith +  snes - the SNES context
38430807856dSBarry Smith -  x - input vector
38440807856dSBarry Smith 
38450807856dSBarry Smith    Output Parameter:
38460807856dSBarry Smith .  y - function vector, as set by SNESSetFunction()
38470807856dSBarry Smith 
38480807856dSBarry Smith    Notes:
38490807856dSBarry Smith    SNESComputeFunction() is typically used within nonlinear solvers
38500807856dSBarry Smith    implementations, so most users would not generally call this routine
38510807856dSBarry Smith    themselves.
38520807856dSBarry Smith 
38530807856dSBarry Smith    Level: developer
38540807856dSBarry Smith 
38550807856dSBarry Smith .keywords: SNES, nonlinear, compute, function
38560807856dSBarry Smith 
38570807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
385861b2408cSBarry Smith */
38597087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx)
38600807856dSBarry Smith {
3861e650e774SBarry Smith   PetscErrorCode    ierr;
38628f6e6473SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
38638f6e6473SBarry Smith   int               nlhs = 1,nrhs = 5;
38648f6e6473SBarry Smith   mxArray	    *plhs[1],*prhs[5];
386591621f2eSBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
3866e650e774SBarry Smith 
38670807856dSBarry Smith   PetscFunctionBegin;
38680807856dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
38690807856dSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
38700807856dSBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
38710807856dSBarry Smith   PetscCheckSameComm(snes,1,x,2);
38720807856dSBarry Smith   PetscCheckSameComm(snes,1,y,3);
38730807856dSBarry Smith 
38740807856dSBarry Smith   /* call Matlab function in ctx with arguments x and y */
3875e650e774SBarry Smith 
387691621f2eSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3877e650e774SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3878e650e774SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
387991621f2eSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
388091621f2eSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
388191621f2eSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
38828f6e6473SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
38838f6e6473SBarry Smith   prhs[4] =  sctx->ctx;
3884b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr);
3885e650e774SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3886e650e774SBarry Smith   mxDestroyArray(prhs[0]);
3887e650e774SBarry Smith   mxDestroyArray(prhs[1]);
3888e650e774SBarry Smith   mxDestroyArray(prhs[2]);
38898f6e6473SBarry Smith   mxDestroyArray(prhs[3]);
3890e650e774SBarry Smith   mxDestroyArray(plhs[0]);
38910807856dSBarry Smith   PetscFunctionReturn(0);
38920807856dSBarry Smith }
38930807856dSBarry Smith 
38940807856dSBarry Smith 
38950807856dSBarry Smith #undef __FUNCT__
38960807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab"
389761b2408cSBarry Smith /*
38980807856dSBarry Smith    SNESSetFunctionMatlab - Sets the function evaluation routine and function
38990807856dSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3900e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
39010807856dSBarry Smith 
39020807856dSBarry Smith    Logically Collective on SNES
39030807856dSBarry Smith 
39040807856dSBarry Smith    Input Parameters:
39050807856dSBarry Smith +  snes - the SNES context
39060807856dSBarry Smith .  r - vector to store function value
39070807856dSBarry Smith -  func - function evaluation routine
39080807856dSBarry Smith 
39090807856dSBarry Smith    Calling sequence of func:
391061b2408cSBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
39110807856dSBarry Smith 
39120807856dSBarry Smith 
39130807856dSBarry Smith    Notes:
39140807856dSBarry Smith    The Newton-like methods typically solve linear systems of the form
39150807856dSBarry Smith $      f'(x) x = -f(x),
39160807856dSBarry Smith    where f'(x) denotes the Jacobian matrix and f(x) is the function.
39170807856dSBarry Smith 
39180807856dSBarry Smith    Level: beginner
39190807856dSBarry Smith 
39200807856dSBarry Smith .keywords: SNES, nonlinear, set, function
39210807856dSBarry Smith 
39220807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
392361b2408cSBarry Smith */
39247087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx)
39250807856dSBarry Smith {
39260807856dSBarry Smith   PetscErrorCode    ierr;
39278f6e6473SBarry Smith   SNESMatlabContext *sctx;
39280807856dSBarry Smith 
39290807856dSBarry Smith   PetscFunctionBegin;
39308f6e6473SBarry Smith   /* currently sctx is memory bleed */
39318f6e6473SBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
39328f6e6473SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
39338f6e6473SBarry Smith   /*
39348f6e6473SBarry Smith      This should work, but it doesn't
39358f6e6473SBarry Smith   sctx->ctx = ctx;
39368f6e6473SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
39378f6e6473SBarry Smith   */
39388f6e6473SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
39398f6e6473SBarry Smith   ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr);
39400807856dSBarry Smith   PetscFunctionReturn(0);
39410807856dSBarry Smith }
394269b4f73cSBarry Smith 
394361b2408cSBarry Smith #undef __FUNCT__
394461b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab"
394561b2408cSBarry Smith /*
394661b2408cSBarry Smith    SNESComputeJacobian_Matlab - Calls the function that has been set with
394761b2408cSBarry Smith                          SNESSetJacobianMatlab().
394861b2408cSBarry Smith 
394961b2408cSBarry Smith    Collective on SNES
395061b2408cSBarry Smith 
395161b2408cSBarry Smith    Input Parameters:
395261b2408cSBarry Smith +  snes - the SNES context
395361b2408cSBarry Smith .  x - input vector
395461b2408cSBarry Smith .  A, B - the matrices
395561b2408cSBarry Smith -  ctx - user context
395661b2408cSBarry Smith 
395761b2408cSBarry Smith    Output Parameter:
395861b2408cSBarry Smith .  flag - structure of the matrix
395961b2408cSBarry Smith 
396061b2408cSBarry Smith    Level: developer
396161b2408cSBarry Smith 
396261b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function
396361b2408cSBarry Smith 
396461b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
396561b2408cSBarry Smith @*/
39667087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx)
396761b2408cSBarry Smith {
396861b2408cSBarry Smith   PetscErrorCode    ierr;
396961b2408cSBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
397061b2408cSBarry Smith   int               nlhs = 2,nrhs = 6;
397161b2408cSBarry Smith   mxArray	    *plhs[2],*prhs[6];
397261b2408cSBarry Smith   long long int     lx = 0,lA = 0,ls = 0, lB = 0;
397361b2408cSBarry Smith 
397461b2408cSBarry Smith   PetscFunctionBegin;
397561b2408cSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
397661b2408cSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
397761b2408cSBarry Smith 
397861b2408cSBarry Smith   /* call Matlab function in ctx with arguments x and y */
397961b2408cSBarry Smith 
398061b2408cSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
398161b2408cSBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
398261b2408cSBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
398361b2408cSBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
398461b2408cSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
398561b2408cSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
398661b2408cSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
398761b2408cSBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
398861b2408cSBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
398961b2408cSBarry Smith   prhs[5] =  sctx->ctx;
3990b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr);
399161b2408cSBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
399261b2408cSBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
399361b2408cSBarry Smith   mxDestroyArray(prhs[0]);
399461b2408cSBarry Smith   mxDestroyArray(prhs[1]);
399561b2408cSBarry Smith   mxDestroyArray(prhs[2]);
399661b2408cSBarry Smith   mxDestroyArray(prhs[3]);
399761b2408cSBarry Smith   mxDestroyArray(prhs[4]);
399861b2408cSBarry Smith   mxDestroyArray(plhs[0]);
399961b2408cSBarry Smith   mxDestroyArray(plhs[1]);
400061b2408cSBarry Smith   PetscFunctionReturn(0);
400161b2408cSBarry Smith }
400261b2408cSBarry Smith 
400361b2408cSBarry Smith 
400461b2408cSBarry Smith #undef __FUNCT__
400561b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab"
400661b2408cSBarry Smith /*
400761b2408cSBarry Smith    SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
400861b2408cSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
4009e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
401061b2408cSBarry Smith 
401161b2408cSBarry Smith    Logically Collective on SNES
401261b2408cSBarry Smith 
401361b2408cSBarry Smith    Input Parameters:
401461b2408cSBarry Smith +  snes - the SNES context
401561b2408cSBarry Smith .  A,B - Jacobian matrices
401661b2408cSBarry Smith .  func - function evaluation routine
401761b2408cSBarry Smith -  ctx - user context
401861b2408cSBarry Smith 
401961b2408cSBarry Smith    Calling sequence of func:
402061b2408cSBarry Smith $    flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx);
402161b2408cSBarry Smith 
402261b2408cSBarry Smith 
402361b2408cSBarry Smith    Level: developer
402461b2408cSBarry Smith 
402561b2408cSBarry Smith .keywords: SNES, nonlinear, set, function
402661b2408cSBarry Smith 
402761b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
402861b2408cSBarry Smith */
40297087cfbeSBarry Smith PetscErrorCode  SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx)
403061b2408cSBarry Smith {
403161b2408cSBarry Smith   PetscErrorCode    ierr;
403261b2408cSBarry Smith   SNESMatlabContext *sctx;
403361b2408cSBarry Smith 
403461b2408cSBarry Smith   PetscFunctionBegin;
403561b2408cSBarry Smith   /* currently sctx is memory bleed */
403661b2408cSBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
403761b2408cSBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
403861b2408cSBarry Smith   /*
403961b2408cSBarry Smith      This should work, but it doesn't
404061b2408cSBarry Smith   sctx->ctx = ctx;
404161b2408cSBarry Smith   mexMakeArrayPersistent(sctx->ctx);
404261b2408cSBarry Smith   */
404361b2408cSBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
404461b2408cSBarry Smith   ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
404561b2408cSBarry Smith   PetscFunctionReturn(0);
404661b2408cSBarry Smith }
404769b4f73cSBarry Smith 
4048f9eb7ae2SShri Abhyankar #undef __FUNCT__
4049f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab"
4050f9eb7ae2SShri Abhyankar /*
4051f9eb7ae2SShri Abhyankar    SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab().
4052f9eb7ae2SShri Abhyankar 
4053f9eb7ae2SShri Abhyankar    Collective on SNES
4054f9eb7ae2SShri Abhyankar 
4055f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction()
4056f9eb7ae2SShri Abhyankar @*/
40577087cfbeSBarry Smith PetscErrorCode  SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx)
4058f9eb7ae2SShri Abhyankar {
4059f9eb7ae2SShri Abhyankar   PetscErrorCode  ierr;
406048f37e70SShri Abhyankar   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
4061f9eb7ae2SShri Abhyankar   int             nlhs = 1,nrhs = 6;
4062f9eb7ae2SShri Abhyankar   mxArray	  *plhs[1],*prhs[6];
4063f9eb7ae2SShri Abhyankar   long long int   lx = 0,ls = 0;
4064f9eb7ae2SShri Abhyankar   Vec             x=snes->vec_sol;
4065f9eb7ae2SShri Abhyankar 
4066f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
4067f9eb7ae2SShri Abhyankar   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
4068f9eb7ae2SShri Abhyankar 
4069f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
4070f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
4071f9eb7ae2SShri Abhyankar   prhs[0] =  mxCreateDoubleScalar((double)ls);
4072f9eb7ae2SShri Abhyankar   prhs[1] =  mxCreateDoubleScalar((double)it);
4073f9eb7ae2SShri Abhyankar   prhs[2] =  mxCreateDoubleScalar((double)fnorm);
4074f9eb7ae2SShri Abhyankar   prhs[3] =  mxCreateDoubleScalar((double)lx);
4075f9eb7ae2SShri Abhyankar   prhs[4] =  mxCreateString(sctx->funcname);
4076f9eb7ae2SShri Abhyankar   prhs[5] =  sctx->ctx;
4077f9eb7ae2SShri Abhyankar   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr);
4078f9eb7ae2SShri Abhyankar   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4079f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[0]);
4080f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[1]);
4081f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[2]);
4082f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[3]);
4083f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[4]);
4084f9eb7ae2SShri Abhyankar   mxDestroyArray(plhs[0]);
4085f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
4086f9eb7ae2SShri Abhyankar }
4087f9eb7ae2SShri Abhyankar 
4088f9eb7ae2SShri Abhyankar 
4089f9eb7ae2SShri Abhyankar #undef __FUNCT__
4090f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab"
4091f9eb7ae2SShri Abhyankar /*
4092e3c5b3baSBarry Smith    SNESMonitorSetMatlab - Sets the monitor function from MATLAB
4093f9eb7ae2SShri Abhyankar 
4094f9eb7ae2SShri Abhyankar    Level: developer
4095f9eb7ae2SShri Abhyankar 
4096f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function
4097f9eb7ae2SShri Abhyankar 
4098f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
4099f9eb7ae2SShri Abhyankar */
41007087cfbeSBarry Smith PetscErrorCode  SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx)
4101f9eb7ae2SShri Abhyankar {
4102f9eb7ae2SShri Abhyankar   PetscErrorCode    ierr;
4103f9eb7ae2SShri Abhyankar   SNESMatlabContext *sctx;
4104f9eb7ae2SShri Abhyankar 
4105f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
4106f9eb7ae2SShri Abhyankar   /* currently sctx is memory bleed */
4107f9eb7ae2SShri Abhyankar   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
4108f9eb7ae2SShri Abhyankar   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4109f9eb7ae2SShri Abhyankar   /*
4110f9eb7ae2SShri Abhyankar      This should work, but it doesn't
4111f9eb7ae2SShri Abhyankar   sctx->ctx = ctx;
4112f9eb7ae2SShri Abhyankar   mexMakeArrayPersistent(sctx->ctx);
4113f9eb7ae2SShri Abhyankar   */
4114f9eb7ae2SShri Abhyankar   sctx->ctx = mxDuplicateArray(ctx);
4115f9eb7ae2SShri Abhyankar   ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4116f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
4117f9eb7ae2SShri Abhyankar }
4118f9eb7ae2SShri Abhyankar 
411969b4f73cSBarry Smith #endif
4120