xref: /petsc/src/snes/interface/snes.c (revision 5180491cb125dedef818b8dd3286c1c2b75aa340)
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) {
189eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is rebuilt every %D Newton 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   }
19694b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1974a0c5b0cSMatthew G Knepley   if (snes->pc) {
1984a0c5b0cSMatthew G Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1994a0c5b0cSMatthew G Knepley     ierr = SNESView(snes->pc, viewer);CHKERRQ(ierr);
2004a0c5b0cSMatthew G Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2014a0c5b0cSMatthew G Knepley   }
202b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
20394b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
204b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2053a40ed3dSBarry Smith   PetscFunctionReturn(0);
2069b94acceSBarry Smith }
2079b94acceSBarry Smith 
20876b2cf59SMatthew Knepley /*
20976b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
21076b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
21176b2cf59SMatthew Knepley */
21276b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
213a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
2146849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
21576b2cf59SMatthew Knepley 
216e74ef692SMatthew Knepley #undef __FUNCT__
217e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
218ac226902SBarry Smith /*@C
21976b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
22076b2cf59SMatthew Knepley 
22176b2cf59SMatthew Knepley   Not Collective
22276b2cf59SMatthew Knepley 
22376b2cf59SMatthew Knepley   Input Parameter:
22476b2cf59SMatthew Knepley . snescheck - function that checks for options
22576b2cf59SMatthew Knepley 
22676b2cf59SMatthew Knepley   Level: developer
22776b2cf59SMatthew Knepley 
22876b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
22976b2cf59SMatthew Knepley @*/
2307087cfbeSBarry Smith PetscErrorCode  SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
23176b2cf59SMatthew Knepley {
23276b2cf59SMatthew Knepley   PetscFunctionBegin;
23376b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
234e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
23576b2cf59SMatthew Knepley   }
23676b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
23776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
23876b2cf59SMatthew Knepley }
23976b2cf59SMatthew Knepley 
2407087cfbeSBarry Smith extern PetscErrorCode  SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
241aa3661deSLisandro Dalcin 
242aa3661deSLisandro Dalcin #undef __FUNCT__
243aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private"
244ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool  hasOperator, PetscInt version)
245aa3661deSLisandro Dalcin {
246aa3661deSLisandro Dalcin   Mat            J;
247aa3661deSLisandro Dalcin   KSP            ksp;
248aa3661deSLisandro Dalcin   PC             pc;
249ace3abfcSBarry Smith   PetscBool      match;
250aa3661deSLisandro Dalcin   PetscErrorCode ierr;
251aa3661deSLisandro Dalcin 
252aa3661deSLisandro Dalcin   PetscFunctionBegin;
2530700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
254aa3661deSLisandro Dalcin 
25598613b67SLisandro Dalcin   if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
25698613b67SLisandro Dalcin     Mat A = snes->jacobian, B = snes->jacobian_pre;
25798613b67SLisandro Dalcin     ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr);
25898613b67SLisandro Dalcin   }
25998613b67SLisandro Dalcin 
260aa3661deSLisandro Dalcin   if (version == 1) {
261aa3661deSLisandro Dalcin     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
26298613b67SLisandro Dalcin     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
2639c6ac3b3SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
264aa3661deSLisandro Dalcin   } else if (version == 2) {
265e32f2f54SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
266ce63c4c1SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) && !defined(PETSC_USE_REAL_LONG_DOUBLE)
267aa3661deSLisandro Dalcin     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr);
268aa3661deSLisandro Dalcin #else
269e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
270aa3661deSLisandro Dalcin #endif
271a8248277SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2");
272aa3661deSLisandro Dalcin 
273aa3661deSLisandro Dalcin   ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr);
274d3462f78SMatthew Knepley   if (hasOperator) {
275aa3661deSLisandro Dalcin     /* This version replaces the user provided Jacobian matrix with a
276aa3661deSLisandro Dalcin        matrix-free version but still employs the user-provided preconditioner matrix. */
277aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
278aa3661deSLisandro Dalcin   } else {
279aa3661deSLisandro Dalcin     /* This version replaces both the user-provided Jacobian and the user-
280aa3661deSLisandro Dalcin        provided preconditioner matrix with the default matrix free version. */
281aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
282aa3661deSLisandro Dalcin     /* Force no preconditioner */
283aa3661deSLisandro Dalcin     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
284aa3661deSLisandro Dalcin     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
285aa3661deSLisandro Dalcin     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr);
286aa3661deSLisandro Dalcin     if (!match) {
287aa3661deSLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
288aa3661deSLisandro Dalcin       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
289aa3661deSLisandro Dalcin     }
290aa3661deSLisandro Dalcin   }
2916bf464f9SBarry Smith   ierr = MatDestroy(&J);CHKERRQ(ierr);
292aa3661deSLisandro Dalcin   PetscFunctionReturn(0);
293aa3661deSLisandro Dalcin }
294aa3661deSLisandro Dalcin 
2954a2ae208SSatish Balay #undef __FUNCT__
2964a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
2979b94acceSBarry Smith /*@
29894b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
2999b94acceSBarry Smith 
300c7afd0dbSLois Curfman McInnes    Collective on SNES
301c7afd0dbSLois Curfman McInnes 
3029b94acceSBarry Smith    Input Parameter:
3039b94acceSBarry Smith .  snes - the SNES context
3049b94acceSBarry Smith 
30536851e7fSLois Curfman McInnes    Options Database Keys:
3066831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
30782738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
30882738288SBarry Smith                 of the change in the solution between steps
30970441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
310b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
311b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
312b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
31350ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
314ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
315a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
316e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
317b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
3182492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
31982738288SBarry Smith                                solver; hence iterations will continue until max_it
3201fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
32182738288SBarry Smith                                of convergence test
322e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
323e8105e01SRichard Katz                                        filename given prints to stdout
324a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
325a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
326a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
327a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
328e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
3295968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
330fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
33182738288SBarry Smith 
33282738288SBarry Smith     Options Database for Eisenstat-Walker method:
333fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
3344b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
33536851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
33636851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
33736851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
33836851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
33936851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
34036851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
34182738288SBarry Smith 
34211ca99fdSLois Curfman McInnes    Notes:
34311ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
3440598bfebSBarry Smith    the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A>.
34583e2fdc7SBarry Smith 
34636851e7fSLois Curfman McInnes    Level: beginner
34736851e7fSLois Curfman McInnes 
3489b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
3499b94acceSBarry Smith 
35069ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
3519b94acceSBarry Smith @*/
3527087cfbeSBarry Smith PetscErrorCode  SNESSetFromOptions(SNES snes)
3539b94acceSBarry Smith {
354ace3abfcSBarry Smith   PetscBool               flg,mf,mf_operator;
355efd51863SBarry Smith   PetscInt                i,indx,lag,mf_version,grids;
356aa3661deSLisandro Dalcin   MatStructure            matflag;
35785385478SLisandro Dalcin   const char              *deft = SNESLS;
35885385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
35985385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
360e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
361649052a6SBarry Smith   PetscViewer             monviewer;
36285385478SLisandro Dalcin   PetscErrorCode          ierr;
3639b94acceSBarry Smith 
3643a40ed3dSBarry Smith   PetscFunctionBegin;
3650700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
366ca161407SBarry Smith 
367186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
368cce0b1b2SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
3697adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
370b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
371d64ed03dSBarry Smith     if (flg) {
372186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
3737adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
374186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
375d64ed03dSBarry Smith     }
37690d69ab7SBarry Smith     /* not used here, but called so will go into help messaage */
377909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
37893c39befSBarry Smith 
37957034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
38057034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
381186905e3SBarry Smith 
38257034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
383b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
384b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
38550ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
386ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
387acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr);
38885385478SLisandro Dalcin 
389a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
390a8054027SBarry Smith     if (flg) {
391a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
392a8054027SBarry Smith     }
393e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
394e35cf81dSBarry Smith     if (flg) {
395e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
396e35cf81dSBarry Smith     }
397efd51863SBarry Smith     ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr);
398efd51863SBarry Smith     if (flg) {
399efd51863SBarry Smith       ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr);
400efd51863SBarry Smith     }
401a8054027SBarry Smith 
40285385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
40385385478SLisandro Dalcin     if (flg) {
40485385478SLisandro Dalcin       switch (indx) {
4057f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
4067f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
40785385478SLisandro Dalcin       }
40885385478SLisandro Dalcin     }
40985385478SLisandro Dalcin 
410acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
411186905e3SBarry Smith 
41285385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
41385385478SLisandro Dalcin 
414acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
415186905e3SBarry Smith 
416fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
417fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
418fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
419fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
420fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
421fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
422fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
423186905e3SBarry Smith 
42490d69ab7SBarry Smith     flg  = PETSC_FALSE;
425acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
426a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
427eabae89aSBarry Smith 
428a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
429e8105e01SRichard Katz     if (flg) {
430649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
431649052a6SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
432e8105e01SRichard Katz     }
433eabae89aSBarry Smith 
434b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
435b271bb04SBarry Smith     if (flg) {
436b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
437b271bb04SBarry Smith     }
438b271bb04SBarry Smith 
439a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
440eabae89aSBarry Smith     if (flg) {
441649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
442f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
443e8105e01SRichard Katz     }
444eabae89aSBarry Smith 
445a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
446eabae89aSBarry Smith     if (flg) {
447649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
448649052a6SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
449eabae89aSBarry Smith     }
450eabae89aSBarry Smith 
451*5180491cSLisandro Dalcin     ierr = PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
452*5180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)snes,monfilename);CHKERRQ(ierr);}
453*5180491cSLisandro Dalcin 
45490d69ab7SBarry Smith     flg  = PETSC_FALSE;
455acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
456a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
45790d69ab7SBarry Smith     flg  = PETSC_FALSE;
458acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
459a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
46090d69ab7SBarry Smith     flg  = PETSC_FALSE;
461acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
462a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
46390d69ab7SBarry Smith     flg  = PETSC_FALSE;
464acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
465a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
46690d69ab7SBarry Smith     flg  = PETSC_FALSE;
467acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
468b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
469e24b481bSBarry Smith 
47090d69ab7SBarry Smith     flg  = PETSC_FALSE;
471acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
4724b27c08aSLois Curfman McInnes     if (flg) {
473186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
474ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
4759b94acceSBarry Smith     }
476639f9d9dSBarry Smith 
477aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
478aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
479acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
480a8248277SBarry Smith     if (flg && mf_operator) {
481a8248277SBarry Smith       snes->mf_operator = PETSC_TRUE;
482a8248277SBarry Smith       mf = PETSC_TRUE;
483a8248277SBarry Smith     }
484aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
485acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
486aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
487aa3661deSLisandro Dalcin     mf_version = 1;
488aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
489aa3661deSLisandro Dalcin 
49076b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
49176b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
49276b2cf59SMatthew Knepley     }
49376b2cf59SMatthew Knepley 
494e7788613SBarry Smith     if (snes->ops->setfromoptions) {
495e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
496639f9d9dSBarry Smith     }
4975d973c19SBarry Smith 
4985d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
4995d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr);
500b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
5014bbc92c1SBarry Smith 
502aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
5031cee3971SBarry Smith 
5041cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
505aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
506aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
50785385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
50893993e2dSLois Curfman McInnes 
5094a0c5b0cSMatthew G Knepley   if (snes->pc) {
5104a0c5b0cSMatthew G Knepley     ierr = SNESSetOptionsPrefix(snes->pc, "npc_");CHKERRQ(ierr);
5114a0c5b0cSMatthew G Knepley     ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr);
5124a0c5b0cSMatthew G Knepley     /* Should we make a duplicate vector and matrix? Leave the DM to make it? */
5134a0c5b0cSMatthew G Knepley     ierr = SNESSetFunction(snes->pc, snes->vec_func, snes->ops->computefunction, snes->funP);CHKERRQ(ierr);
5144a0c5b0cSMatthew G Knepley     ierr = SNESSetJacobian(snes->pc, snes->jacobian, snes->jacobian_pre, snes->ops->computejacobian, snes->jacP);CHKERRQ(ierr);
5154a0c5b0cSMatthew G Knepley     ierr = SNESSetFromOptions(snes->pc);CHKERRQ(ierr);
5164a0c5b0cSMatthew G Knepley   }
5173a40ed3dSBarry Smith   PetscFunctionReturn(0);
5189b94acceSBarry Smith }
5199b94acceSBarry Smith 
520d25893d9SBarry Smith #undef __FUNCT__
521d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext"
522d25893d9SBarry Smith /*@
523d25893d9SBarry Smith    SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for
524d25893d9SBarry Smith    the nonlinear solvers.
525d25893d9SBarry Smith 
526d25893d9SBarry Smith    Logically Collective on SNES
527d25893d9SBarry Smith 
528d25893d9SBarry Smith    Input Parameters:
529d25893d9SBarry Smith +  snes - the SNES context
530d25893d9SBarry Smith .  compute - function to compute the context
531d25893d9SBarry Smith -  destroy - function to destroy the context
532d25893d9SBarry Smith 
533d25893d9SBarry Smith    Level: intermediate
534d25893d9SBarry Smith 
535d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context
536d25893d9SBarry Smith 
537d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext()
538d25893d9SBarry Smith @*/
539d25893d9SBarry Smith PetscErrorCode  SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**))
540d25893d9SBarry Smith {
541d25893d9SBarry Smith   PetscFunctionBegin;
542d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
543d25893d9SBarry Smith   snes->ops->usercompute = compute;
544d25893d9SBarry Smith   snes->ops->userdestroy = destroy;
545d25893d9SBarry Smith   PetscFunctionReturn(0);
546d25893d9SBarry Smith }
547a847f771SSatish Balay 
5484a2ae208SSatish Balay #undef __FUNCT__
5494a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
550b07ff414SBarry Smith /*@
5519b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
5529b94acceSBarry Smith    the nonlinear solvers.
5539b94acceSBarry Smith 
5543f9fe445SBarry Smith    Logically Collective on SNES
555fee21e36SBarry Smith 
556c7afd0dbSLois Curfman McInnes    Input Parameters:
557c7afd0dbSLois Curfman McInnes +  snes - the SNES context
558c7afd0dbSLois Curfman McInnes -  usrP - optional user context
559c7afd0dbSLois Curfman McInnes 
56036851e7fSLois Curfman McInnes    Level: intermediate
56136851e7fSLois Curfman McInnes 
5629b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
5639b94acceSBarry Smith 
564d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext()
5659b94acceSBarry Smith @*/
5667087cfbeSBarry Smith PetscErrorCode  SNESSetApplicationContext(SNES snes,void *usrP)
5679b94acceSBarry Smith {
5681b2093e4SBarry Smith   PetscErrorCode ierr;
569b07ff414SBarry Smith   KSP            ksp;
5701b2093e4SBarry Smith 
5713a40ed3dSBarry Smith   PetscFunctionBegin;
5720700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
573b07ff414SBarry Smith   ierr       = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
574b07ff414SBarry Smith   ierr       = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr);
5759b94acceSBarry Smith   snes->user = usrP;
5763a40ed3dSBarry Smith   PetscFunctionReturn(0);
5779b94acceSBarry Smith }
57874679c65SBarry Smith 
5794a2ae208SSatish Balay #undef __FUNCT__
5804a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
581b07ff414SBarry Smith /*@
5829b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
5839b94acceSBarry Smith    nonlinear solvers.
5849b94acceSBarry Smith 
585c7afd0dbSLois Curfman McInnes    Not Collective
586c7afd0dbSLois Curfman McInnes 
5879b94acceSBarry Smith    Input Parameter:
5889b94acceSBarry Smith .  snes - SNES context
5899b94acceSBarry Smith 
5909b94acceSBarry Smith    Output Parameter:
5919b94acceSBarry Smith .  usrP - user context
5929b94acceSBarry Smith 
59336851e7fSLois Curfman McInnes    Level: intermediate
59436851e7fSLois Curfman McInnes 
5959b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
5969b94acceSBarry Smith 
5979b94acceSBarry Smith .seealso: SNESSetApplicationContext()
5989b94acceSBarry Smith @*/
599e71120c6SJed Brown PetscErrorCode  SNESGetApplicationContext(SNES snes,void *usrP)
6009b94acceSBarry Smith {
6013a40ed3dSBarry Smith   PetscFunctionBegin;
6020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
603e71120c6SJed Brown   *(void**)usrP = snes->user;
6043a40ed3dSBarry Smith   PetscFunctionReturn(0);
6059b94acceSBarry Smith }
60674679c65SBarry Smith 
6074a2ae208SSatish Balay #undef __FUNCT__
6084a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
6099b94acceSBarry Smith /*@
610c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
611c8228a4eSBarry Smith    at this time.
6129b94acceSBarry Smith 
613c7afd0dbSLois Curfman McInnes    Not Collective
614c7afd0dbSLois Curfman McInnes 
6159b94acceSBarry Smith    Input Parameter:
6169b94acceSBarry Smith .  snes - SNES context
6179b94acceSBarry Smith 
6189b94acceSBarry Smith    Output Parameter:
6199b94acceSBarry Smith .  iter - iteration number
6209b94acceSBarry Smith 
621c8228a4eSBarry Smith    Notes:
622c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
623c8228a4eSBarry Smith 
624c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
62508405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
62608405cd6SLois Curfman McInnes .vb
62708405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
62808405cd6SLois Curfman McInnes       if (!(it % 2)) {
62908405cd6SLois Curfman McInnes         [compute Jacobian here]
63008405cd6SLois Curfman McInnes       }
63108405cd6SLois Curfman McInnes .ve
632c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
63308405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
634c8228a4eSBarry Smith 
63536851e7fSLois Curfman McInnes    Level: intermediate
63636851e7fSLois Curfman McInnes 
6372b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
6382b668275SBarry Smith 
639b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
6409b94acceSBarry Smith @*/
6417087cfbeSBarry Smith PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt* iter)
6429b94acceSBarry Smith {
6433a40ed3dSBarry Smith   PetscFunctionBegin;
6440700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6454482741eSBarry Smith   PetscValidIntPointer(iter,2);
6469b94acceSBarry Smith   *iter = snes->iter;
6473a40ed3dSBarry Smith   PetscFunctionReturn(0);
6489b94acceSBarry Smith }
64974679c65SBarry Smith 
6504a2ae208SSatish Balay #undef __FUNCT__
6514a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
6529b94acceSBarry Smith /*@
6539b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
6549b94acceSBarry Smith    with SNESSSetFunction().
6559b94acceSBarry Smith 
656c7afd0dbSLois Curfman McInnes    Collective on SNES
657c7afd0dbSLois Curfman McInnes 
6589b94acceSBarry Smith    Input Parameter:
6599b94acceSBarry Smith .  snes - SNES context
6609b94acceSBarry Smith 
6619b94acceSBarry Smith    Output Parameter:
6629b94acceSBarry Smith .  fnorm - 2-norm of function
6639b94acceSBarry Smith 
66436851e7fSLois Curfman McInnes    Level: intermediate
66536851e7fSLois Curfman McInnes 
6669b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
667a86d99e1SLois Curfman McInnes 
668b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
6699b94acceSBarry Smith @*/
6707087cfbeSBarry Smith PetscErrorCode  SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
6719b94acceSBarry Smith {
6723a40ed3dSBarry Smith   PetscFunctionBegin;
6730700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6744482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
6759b94acceSBarry Smith   *fnorm = snes->norm;
6763a40ed3dSBarry Smith   PetscFunctionReturn(0);
6779b94acceSBarry Smith }
67874679c65SBarry Smith 
6794a2ae208SSatish Balay #undef __FUNCT__
680b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
6819b94acceSBarry Smith /*@
682b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
6839b94acceSBarry Smith    attempted by the nonlinear solver.
6849b94acceSBarry Smith 
685c7afd0dbSLois Curfman McInnes    Not Collective
686c7afd0dbSLois Curfman McInnes 
6879b94acceSBarry Smith    Input Parameter:
6889b94acceSBarry Smith .  snes - SNES context
6899b94acceSBarry Smith 
6909b94acceSBarry Smith    Output Parameter:
6919b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
6929b94acceSBarry Smith 
693c96a6f78SLois Curfman McInnes    Notes:
694c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
695c96a6f78SLois Curfman McInnes 
69636851e7fSLois Curfman McInnes    Level: intermediate
69736851e7fSLois Curfman McInnes 
6989b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
69958ebbce7SBarry Smith 
700e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
70158ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
7029b94acceSBarry Smith @*/
7037087cfbeSBarry Smith PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
7049b94acceSBarry Smith {
7053a40ed3dSBarry Smith   PetscFunctionBegin;
7060700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7074482741eSBarry Smith   PetscValidIntPointer(nfails,2);
70850ffb88aSMatthew Knepley   *nfails = snes->numFailures;
70950ffb88aSMatthew Knepley   PetscFunctionReturn(0);
71050ffb88aSMatthew Knepley }
71150ffb88aSMatthew Knepley 
71250ffb88aSMatthew Knepley #undef __FUNCT__
713b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
71450ffb88aSMatthew Knepley /*@
715b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
71650ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
71750ffb88aSMatthew Knepley 
71850ffb88aSMatthew Knepley    Not Collective
71950ffb88aSMatthew Knepley 
72050ffb88aSMatthew Knepley    Input Parameters:
72150ffb88aSMatthew Knepley +  snes     - SNES context
72250ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
72350ffb88aSMatthew Knepley 
72450ffb88aSMatthew Knepley    Level: intermediate
72550ffb88aSMatthew Knepley 
72650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
72758ebbce7SBarry Smith 
728e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
72958ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
73050ffb88aSMatthew Knepley @*/
7317087cfbeSBarry Smith PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
73250ffb88aSMatthew Knepley {
73350ffb88aSMatthew Knepley   PetscFunctionBegin;
7340700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
73550ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
73650ffb88aSMatthew Knepley   PetscFunctionReturn(0);
73750ffb88aSMatthew Knepley }
73850ffb88aSMatthew Knepley 
73950ffb88aSMatthew Knepley #undef __FUNCT__
740b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
74150ffb88aSMatthew Knepley /*@
742b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
74350ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
74450ffb88aSMatthew Knepley 
74550ffb88aSMatthew Knepley    Not Collective
74650ffb88aSMatthew Knepley 
74750ffb88aSMatthew Knepley    Input Parameter:
74850ffb88aSMatthew Knepley .  snes     - SNES context
74950ffb88aSMatthew Knepley 
75050ffb88aSMatthew Knepley    Output Parameter:
75150ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
75250ffb88aSMatthew Knepley 
75350ffb88aSMatthew Knepley    Level: intermediate
75450ffb88aSMatthew Knepley 
75550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
75658ebbce7SBarry Smith 
757e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
75858ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
75958ebbce7SBarry Smith 
76050ffb88aSMatthew Knepley @*/
7617087cfbeSBarry Smith PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
76250ffb88aSMatthew Knepley {
76350ffb88aSMatthew Knepley   PetscFunctionBegin;
7640700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7654482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
76650ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
7673a40ed3dSBarry Smith   PetscFunctionReturn(0);
7689b94acceSBarry Smith }
769a847f771SSatish Balay 
7704a2ae208SSatish Balay #undef __FUNCT__
7712541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
7722541af92SBarry Smith /*@
7732541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
7742541af92SBarry Smith      done by SNES.
7752541af92SBarry Smith 
7762541af92SBarry Smith    Not Collective
7772541af92SBarry Smith 
7782541af92SBarry Smith    Input Parameter:
7792541af92SBarry Smith .  snes     - SNES context
7802541af92SBarry Smith 
7812541af92SBarry Smith    Output Parameter:
7822541af92SBarry Smith .  nfuncs - number of evaluations
7832541af92SBarry Smith 
7842541af92SBarry Smith    Level: intermediate
7852541af92SBarry Smith 
7862541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
78758ebbce7SBarry Smith 
788e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
7892541af92SBarry Smith @*/
7907087cfbeSBarry Smith PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
7912541af92SBarry Smith {
7922541af92SBarry Smith   PetscFunctionBegin;
7930700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7942541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
7952541af92SBarry Smith   *nfuncs = snes->nfuncs;
7962541af92SBarry Smith   PetscFunctionReturn(0);
7972541af92SBarry Smith }
7982541af92SBarry Smith 
7992541af92SBarry Smith #undef __FUNCT__
8003d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
8013d4c4710SBarry Smith /*@
8023d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
8033d4c4710SBarry Smith    linear solvers.
8043d4c4710SBarry Smith 
8053d4c4710SBarry Smith    Not Collective
8063d4c4710SBarry Smith 
8073d4c4710SBarry Smith    Input Parameter:
8083d4c4710SBarry Smith .  snes - SNES context
8093d4c4710SBarry Smith 
8103d4c4710SBarry Smith    Output Parameter:
8113d4c4710SBarry Smith .  nfails - number of failed solves
8123d4c4710SBarry Smith 
8133d4c4710SBarry Smith    Notes:
8143d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
8153d4c4710SBarry Smith 
8163d4c4710SBarry Smith    Level: intermediate
8173d4c4710SBarry Smith 
8183d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
81958ebbce7SBarry Smith 
820e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
8213d4c4710SBarry Smith @*/
8227087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
8233d4c4710SBarry Smith {
8243d4c4710SBarry Smith   PetscFunctionBegin;
8250700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8263d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
8273d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
8283d4c4710SBarry Smith   PetscFunctionReturn(0);
8293d4c4710SBarry Smith }
8303d4c4710SBarry Smith 
8313d4c4710SBarry Smith #undef __FUNCT__
8323d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
8333d4c4710SBarry Smith /*@
8343d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
8353d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
8363d4c4710SBarry Smith 
8373f9fe445SBarry Smith    Logically Collective on SNES
8383d4c4710SBarry Smith 
8393d4c4710SBarry Smith    Input Parameters:
8403d4c4710SBarry Smith +  snes     - SNES context
8413d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
8423d4c4710SBarry Smith 
8433d4c4710SBarry Smith    Level: intermediate
8443d4c4710SBarry Smith 
845a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
8463d4c4710SBarry Smith 
8473d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
8483d4c4710SBarry Smith 
84958ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
8503d4c4710SBarry Smith @*/
8517087cfbeSBarry Smith PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
8523d4c4710SBarry Smith {
8533d4c4710SBarry Smith   PetscFunctionBegin;
8540700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
855c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
8563d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
8573d4c4710SBarry Smith   PetscFunctionReturn(0);
8583d4c4710SBarry Smith }
8593d4c4710SBarry Smith 
8603d4c4710SBarry Smith #undef __FUNCT__
8613d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
8623d4c4710SBarry Smith /*@
8633d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
8643d4c4710SBarry Smith      are allowed before SNES terminates
8653d4c4710SBarry Smith 
8663d4c4710SBarry Smith    Not Collective
8673d4c4710SBarry Smith 
8683d4c4710SBarry Smith    Input Parameter:
8693d4c4710SBarry Smith .  snes     - SNES context
8703d4c4710SBarry Smith 
8713d4c4710SBarry Smith    Output Parameter:
8723d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
8733d4c4710SBarry Smith 
8743d4c4710SBarry Smith    Level: intermediate
8753d4c4710SBarry Smith 
8763d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
8773d4c4710SBarry Smith 
8783d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
8793d4c4710SBarry Smith 
880e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
8813d4c4710SBarry Smith @*/
8827087cfbeSBarry Smith PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
8833d4c4710SBarry Smith {
8843d4c4710SBarry Smith   PetscFunctionBegin;
8850700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8863d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
8873d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
8883d4c4710SBarry Smith   PetscFunctionReturn(0);
8893d4c4710SBarry Smith }
8903d4c4710SBarry Smith 
8913d4c4710SBarry Smith #undef __FUNCT__
892b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
893c96a6f78SLois Curfman McInnes /*@
894b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
895c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
896c96a6f78SLois Curfman McInnes 
897c7afd0dbSLois Curfman McInnes    Not Collective
898c7afd0dbSLois Curfman McInnes 
899c96a6f78SLois Curfman McInnes    Input Parameter:
900c96a6f78SLois Curfman McInnes .  snes - SNES context
901c96a6f78SLois Curfman McInnes 
902c96a6f78SLois Curfman McInnes    Output Parameter:
903c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
904c96a6f78SLois Curfman McInnes 
905c96a6f78SLois Curfman McInnes    Notes:
906c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
907c96a6f78SLois Curfman McInnes 
90836851e7fSLois Curfman McInnes    Level: intermediate
90936851e7fSLois Curfman McInnes 
910c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
9112b668275SBarry Smith 
9128c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
913c96a6f78SLois Curfman McInnes @*/
9147087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
915c96a6f78SLois Curfman McInnes {
9163a40ed3dSBarry Smith   PetscFunctionBegin;
9170700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9184482741eSBarry Smith   PetscValidIntPointer(lits,2);
919c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
9203a40ed3dSBarry Smith   PetscFunctionReturn(0);
921c96a6f78SLois Curfman McInnes }
922c96a6f78SLois Curfman McInnes 
9234a2ae208SSatish Balay #undef __FUNCT__
92494b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
92552baeb72SSatish Balay /*@
92694b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
9279b94acceSBarry Smith 
92894b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
929c7afd0dbSLois Curfman McInnes 
9309b94acceSBarry Smith    Input Parameter:
9319b94acceSBarry Smith .  snes - the SNES context
9329b94acceSBarry Smith 
9339b94acceSBarry Smith    Output Parameter:
93494b7f48cSBarry Smith .  ksp - the KSP context
9359b94acceSBarry Smith 
9369b94acceSBarry Smith    Notes:
93794b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
9389b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
9392999313aSBarry Smith    PC contexts as well.
9409b94acceSBarry Smith 
94136851e7fSLois Curfman McInnes    Level: beginner
94236851e7fSLois Curfman McInnes 
94394b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9449b94acceSBarry Smith 
9452999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9469b94acceSBarry Smith @*/
9477087cfbeSBarry Smith PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
9489b94acceSBarry Smith {
9491cee3971SBarry Smith   PetscErrorCode ierr;
9501cee3971SBarry Smith 
9513a40ed3dSBarry Smith   PetscFunctionBegin;
9520700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9534482741eSBarry Smith   PetscValidPointer(ksp,2);
9541cee3971SBarry Smith 
9551cee3971SBarry Smith   if (!snes->ksp) {
9561cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
9571cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
9581cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
9591cee3971SBarry Smith   }
96094b7f48cSBarry Smith   *ksp = snes->ksp;
9613a40ed3dSBarry Smith   PetscFunctionReturn(0);
9629b94acceSBarry Smith }
96382bf6240SBarry Smith 
9644a2ae208SSatish Balay #undef __FUNCT__
9652999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
9662999313aSBarry Smith /*@
9672999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
9682999313aSBarry Smith 
9692999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
9702999313aSBarry Smith 
9712999313aSBarry Smith    Input Parameters:
9722999313aSBarry Smith +  snes - the SNES context
9732999313aSBarry Smith -  ksp - the KSP context
9742999313aSBarry Smith 
9752999313aSBarry Smith    Notes:
9762999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
9772999313aSBarry Smith    so this routine is rarely needed.
9782999313aSBarry Smith 
9792999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
9802999313aSBarry Smith    decreased by one.
9812999313aSBarry Smith 
9822999313aSBarry Smith    Level: developer
9832999313aSBarry Smith 
9842999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9852999313aSBarry Smith 
9862999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9872999313aSBarry Smith @*/
9887087cfbeSBarry Smith PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
9892999313aSBarry Smith {
9902999313aSBarry Smith   PetscErrorCode ierr;
9912999313aSBarry Smith 
9922999313aSBarry Smith   PetscFunctionBegin;
9930700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9940700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
9952999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
9967dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
997906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
9982999313aSBarry Smith   snes->ksp = ksp;
9992999313aSBarry Smith   PetscFunctionReturn(0);
10002999313aSBarry Smith }
10012999313aSBarry Smith 
10027adad957SLisandro Dalcin #if 0
10032999313aSBarry Smith #undef __FUNCT__
10044a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
10056849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
1006e24b481bSBarry Smith {
1007e24b481bSBarry Smith   PetscFunctionBegin;
1008e24b481bSBarry Smith   PetscFunctionReturn(0);
1009e24b481bSBarry Smith }
10107adad957SLisandro Dalcin #endif
1011e24b481bSBarry Smith 
10129b94acceSBarry Smith /* -----------------------------------------------------------*/
10134a2ae208SSatish Balay #undef __FUNCT__
10144a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
101552baeb72SSatish Balay /*@
10169b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
10179b94acceSBarry Smith 
1018c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
1019c7afd0dbSLois Curfman McInnes 
1020c7afd0dbSLois Curfman McInnes    Input Parameters:
1021906ed7ccSBarry Smith .  comm - MPI communicator
10229b94acceSBarry Smith 
10239b94acceSBarry Smith    Output Parameter:
10249b94acceSBarry Smith .  outsnes - the new SNES context
10259b94acceSBarry Smith 
1026c7afd0dbSLois Curfman McInnes    Options Database Keys:
1027c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
1028c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
1029c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
1030c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
1031c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
1032c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
1033c1f60f51SBarry Smith 
103436851e7fSLois Curfman McInnes    Level: beginner
103536851e7fSLois Curfman McInnes 
10369b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
10379b94acceSBarry Smith 
1038a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
1039a8054027SBarry Smith 
10409b94acceSBarry Smith @*/
10417087cfbeSBarry Smith PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
10429b94acceSBarry Smith {
1043dfbe8321SBarry Smith   PetscErrorCode      ierr;
10449b94acceSBarry Smith   SNES                snes;
1045fa9f3622SBarry Smith   SNESKSPEW           *kctx;
104637fcc0dbSBarry Smith 
10473a40ed3dSBarry Smith   PetscFunctionBegin;
1048ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
10498ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
10508ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
10518ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
10528ba1e511SMatthew Knepley #endif
10538ba1e511SMatthew Knepley 
10540700a824SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
10557adad957SLisandro Dalcin 
105685385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
10579b94acceSBarry Smith   snes->max_its           = 50;
10589750a799SBarry Smith   snes->max_funcs	  = 10000;
10599b94acceSBarry Smith   snes->norm		  = 0.0;
1060b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
1061b4874afaSBarry Smith   snes->ttol              = 0.0;
106270441072SBarry Smith   snes->abstol		  = 1.e-50;
10639b94acceSBarry Smith   snes->xtol		  = 1.e-8;
10644b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
10659b94acceSBarry Smith   snes->nfuncs            = 0;
106650ffb88aSMatthew Knepley   snes->numFailures       = 0;
106750ffb88aSMatthew Knepley   snes->maxFailures       = 1;
10687a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1069e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1070a8054027SBarry Smith   snes->lagpreconditioner = 1;
1071639f9d9dSBarry Smith   snes->numbermonitors    = 0;
10729b94acceSBarry Smith   snes->data              = 0;
10734dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1074186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
10756f24a144SLois Curfman McInnes   snes->nwork             = 0;
107658c9b817SLisandro Dalcin   snes->work              = 0;
107758c9b817SLisandro Dalcin   snes->nvwork            = 0;
107858c9b817SLisandro Dalcin   snes->vwork             = 0;
1079758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1080758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1081758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1082758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1083758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1084184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
10859b94acceSBarry Smith 
10863d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
10873d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
10883d4c4710SBarry Smith 
10899b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
109038f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
10919b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
10929b94acceSBarry Smith   kctx->version     = 2;
10939b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
10949b94acceSBarry Smith                              this was too large for some test cases */
109575567043SBarry Smith   kctx->rtol_last   = 0.0;
10969b94acceSBarry Smith   kctx->rtol_max    = .9;
10979b94acceSBarry Smith   kctx->gamma       = 1.0;
109871f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
109971f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
11009b94acceSBarry Smith   kctx->threshold   = .1;
110175567043SBarry Smith   kctx->lresid_last = 0.0;
110275567043SBarry Smith   kctx->norm_last   = 0.0;
11039b94acceSBarry Smith 
11049b94acceSBarry Smith   *outsnes = snes;
11053a40ed3dSBarry Smith   PetscFunctionReturn(0);
11069b94acceSBarry Smith }
11079b94acceSBarry Smith 
11084a2ae208SSatish Balay #undef __FUNCT__
11094a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
11109b94acceSBarry Smith /*@C
11119b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
11129b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
11139b94acceSBarry Smith    equations.
11149b94acceSBarry Smith 
11153f9fe445SBarry Smith    Logically Collective on SNES
1116fee21e36SBarry Smith 
1117c7afd0dbSLois Curfman McInnes    Input Parameters:
1118c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1119c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1120de044059SHong Zhang .  func - function evaluation routine
1121c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1122c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
11239b94acceSBarry Smith 
1124c7afd0dbSLois Curfman McInnes    Calling sequence of func:
11258d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1126c7afd0dbSLois Curfman McInnes 
1127313e4042SLois Curfman McInnes .  f - function vector
1128c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
11299b94acceSBarry Smith 
11309b94acceSBarry Smith    Notes:
11319b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
11329b94acceSBarry Smith $      f'(x) x = -f(x),
1133c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
11349b94acceSBarry Smith 
113536851e7fSLois Curfman McInnes    Level: beginner
113636851e7fSLois Curfman McInnes 
11379b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
11389b94acceSBarry Smith 
1139a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
11409b94acceSBarry Smith @*/
11417087cfbeSBarry Smith PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
11429b94acceSBarry Smith {
114385385478SLisandro Dalcin   PetscErrorCode ierr;
11443a40ed3dSBarry Smith   PetscFunctionBegin;
11450700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1146d2a683ecSLisandro Dalcin   if (r) {
1147d2a683ecSLisandro Dalcin     PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1148d2a683ecSLisandro Dalcin     PetscCheckSameComm(snes,1,r,2);
114985385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
11506bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
115185385478SLisandro Dalcin     snes->vec_func = r;
1152d2a683ecSLisandro Dalcin   } else if (!snes->vec_func && snes->dm) {
1153d2a683ecSLisandro Dalcin     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1154d2a683ecSLisandro Dalcin   }
1155d2a683ecSLisandro Dalcin   if (func) snes->ops->computefunction = func;
1156d2a683ecSLisandro Dalcin   if (ctx)  snes->funP                 = ctx;
11573a40ed3dSBarry Smith   PetscFunctionReturn(0);
11589b94acceSBarry Smith }
11599b94acceSBarry Smith 
1160d25893d9SBarry Smith #undef __FUNCT__
1161d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess"
1162d25893d9SBarry Smith /*@C
1163d25893d9SBarry Smith    SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem
1164d25893d9SBarry Smith 
1165d25893d9SBarry Smith    Logically Collective on SNES
1166d25893d9SBarry Smith 
1167d25893d9SBarry Smith    Input Parameters:
1168d25893d9SBarry Smith +  snes - the SNES context
1169d25893d9SBarry Smith .  func - function evaluation routine
1170d25893d9SBarry Smith -  ctx - [optional] user-defined context for private data for the
1171d25893d9SBarry Smith          function evaluation routine (may be PETSC_NULL)
1172d25893d9SBarry Smith 
1173d25893d9SBarry Smith    Calling sequence of func:
1174d25893d9SBarry Smith $    func (SNES snes,Vec x,void *ctx);
1175d25893d9SBarry Smith 
1176d25893d9SBarry Smith .  f - function vector
1177d25893d9SBarry Smith -  ctx - optional user-defined function context
1178d25893d9SBarry Smith 
1179d25893d9SBarry Smith    Level: intermediate
1180d25893d9SBarry Smith 
1181d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function
1182d25893d9SBarry Smith 
1183d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
1184d25893d9SBarry Smith @*/
1185d25893d9SBarry Smith PetscErrorCode  SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx)
1186d25893d9SBarry Smith {
1187d25893d9SBarry Smith   PetscFunctionBegin;
1188d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1189d25893d9SBarry Smith   if (func) snes->ops->computeinitialguess = func;
1190d25893d9SBarry Smith   if (ctx)  snes->initialguessP            = ctx;
1191d25893d9SBarry Smith   PetscFunctionReturn(0);
1192d25893d9SBarry Smith }
1193d25893d9SBarry Smith 
11943ab0aad5SBarry Smith /* --------------------------------------------------------------- */
11953ab0aad5SBarry Smith #undef __FUNCT__
11961096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
11971096aae1SMatthew Knepley /*@C
11981096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
11991096aae1SMatthew Knepley    it assumes a zero right hand side.
12001096aae1SMatthew Knepley 
12013f9fe445SBarry Smith    Logically Collective on SNES
12021096aae1SMatthew Knepley 
12031096aae1SMatthew Knepley    Input Parameter:
12041096aae1SMatthew Knepley .  snes - the SNES context
12051096aae1SMatthew Knepley 
12061096aae1SMatthew Knepley    Output Parameter:
1207bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
12081096aae1SMatthew Knepley 
12091096aae1SMatthew Knepley    Level: intermediate
12101096aae1SMatthew Knepley 
12111096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
12121096aae1SMatthew Knepley 
121385385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
12141096aae1SMatthew Knepley @*/
12157087cfbeSBarry Smith PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
12161096aae1SMatthew Knepley {
12171096aae1SMatthew Knepley   PetscFunctionBegin;
12180700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12191096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
122085385478SLisandro Dalcin   *rhs = snes->vec_rhs;
12211096aae1SMatthew Knepley   PetscFunctionReturn(0);
12221096aae1SMatthew Knepley }
12231096aae1SMatthew Knepley 
12241096aae1SMatthew Knepley #undef __FUNCT__
12254a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
12269b94acceSBarry Smith /*@
122736851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
12289b94acceSBarry Smith                          SNESSetFunction().
12299b94acceSBarry Smith 
1230c7afd0dbSLois Curfman McInnes    Collective on SNES
1231c7afd0dbSLois Curfman McInnes 
12329b94acceSBarry Smith    Input Parameters:
1233c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1234c7afd0dbSLois Curfman McInnes -  x - input vector
12359b94acceSBarry Smith 
12369b94acceSBarry Smith    Output Parameter:
12373638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
12389b94acceSBarry Smith 
12391bffabb2SLois Curfman McInnes    Notes:
124036851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
124136851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
124236851e7fSLois Curfman McInnes    themselves.
124336851e7fSLois Curfman McInnes 
124436851e7fSLois Curfman McInnes    Level: developer
124536851e7fSLois Curfman McInnes 
12469b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
12479b94acceSBarry Smith 
1248a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
12499b94acceSBarry Smith @*/
12507087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
12519b94acceSBarry Smith {
1252dfbe8321SBarry Smith   PetscErrorCode ierr;
12539b94acceSBarry Smith 
12543a40ed3dSBarry Smith   PetscFunctionBegin;
12550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12560700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
12570700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1258c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1259c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1260184914b5SBarry Smith 
1261d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1262e7788613SBarry Smith   if (snes->ops->computefunction) {
1263d64ed03dSBarry Smith     PetscStackPush("SNES user function");
126439d508bbSBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1265d64ed03dSBarry Smith     PetscStackPop;
126685385478SLisandro Dalcin   } else if (snes->vec_rhs) {
12671096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
126873250ac0SBarry Smith   } else if (snes->dm) {
1269644e2e5bSBarry Smith     ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr);
1270644e2e5bSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve().");
127185385478SLisandro Dalcin   if (snes->vec_rhs) {
127285385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
12733ab0aad5SBarry Smith   }
1274ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1275d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
12763a40ed3dSBarry Smith   PetscFunctionReturn(0);
12779b94acceSBarry Smith }
12789b94acceSBarry Smith 
12794a2ae208SSatish Balay #undef __FUNCT__
12804a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
128162fef451SLois Curfman McInnes /*@
128262fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
128362fef451SLois Curfman McInnes    set with SNESSetJacobian().
128462fef451SLois Curfman McInnes 
1285c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1286c7afd0dbSLois Curfman McInnes 
128762fef451SLois Curfman McInnes    Input Parameters:
1288c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1289c7afd0dbSLois Curfman McInnes -  x - input vector
129062fef451SLois Curfman McInnes 
129162fef451SLois Curfman McInnes    Output Parameters:
1292c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
129362fef451SLois Curfman McInnes .  B - optional preconditioning matrix
12942b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1295fee21e36SBarry Smith 
1296e35cf81dSBarry Smith   Options Database Keys:
1297e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1298693365a8SJed Brown .    -snes_lag_jacobian <lag>
1299693365a8SJed Brown .    -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences
1300693365a8SJed Brown .    -snes_compare_explicit_draw  - Compare the computed Jacobian to the finite difference Jacobian and draw the result
1301693365a8SJed Brown .    -snes_compare_explicit_contour  - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result
1302693365a8SJed Brown -    -snes_compare_operator  - Make the comparison options above use the operator instead of the preconditioning matrix
1303e35cf81dSBarry Smith 
130462fef451SLois Curfman McInnes    Notes:
130562fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
130662fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
130762fef451SLois Curfman McInnes 
130894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1309dc5a77f8SLois Curfman McInnes    flag parameter.
131062fef451SLois Curfman McInnes 
131136851e7fSLois Curfman McInnes    Level: developer
131236851e7fSLois Curfman McInnes 
131362fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
131462fef451SLois Curfman McInnes 
1315e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
131662fef451SLois Curfman McInnes @*/
13177087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
13189b94acceSBarry Smith {
1319dfbe8321SBarry Smith   PetscErrorCode ierr;
1320ace3abfcSBarry Smith   PetscBool      flag;
13213a40ed3dSBarry Smith 
13223a40ed3dSBarry Smith   PetscFunctionBegin;
13230700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13240700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
13254482741eSBarry Smith   PetscValidPointer(flg,5);
1326c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1327e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1328ebd3b9afSBarry Smith 
1329ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1330ebd3b9afSBarry Smith 
1331fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1332fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1333fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1334fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1335e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1336e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1337ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1338ebd3b9afSBarry Smith     if (flag) {
1339ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1340ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1341ebd3b9afSBarry Smith     }
1342e35cf81dSBarry Smith     PetscFunctionReturn(0);
1343e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1344e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1345e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1346ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1347ebd3b9afSBarry Smith     if (flag) {
1348ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1349ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1350ebd3b9afSBarry Smith     }
1351e35cf81dSBarry Smith     PetscFunctionReturn(0);
1352e35cf81dSBarry Smith   }
1353e35cf81dSBarry Smith 
1354c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1355e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1356d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1357e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1358d64ed03dSBarry Smith   PetscStackPop;
1359d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1360a8054027SBarry Smith 
13613b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
13623b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
13633b4f5425SBarry Smith     snes->lagpreconditioner = -1;
13643b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1365a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1366a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1367a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1368a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1369a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1370a8054027SBarry Smith   }
1371a8054027SBarry Smith 
13726d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
13730700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
13740700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
1375693365a8SJed Brown   {
1376693365a8SJed Brown     PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE;
1377693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr);
1378693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr);
1379693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr);
1380693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr);
1381693365a8SJed Brown     if (flag || flag_draw || flag_contour) {
1382693365a8SJed Brown       Mat Bexp_mine = PETSC_NULL,Bexp,FDexp;
1383693365a8SJed Brown       MatStructure mstruct;
1384693365a8SJed Brown       PetscViewer vdraw,vstdout;
13856b3a5b13SJed Brown       PetscBool flg;
1386693365a8SJed Brown       if (flag_operator) {
1387693365a8SJed Brown         ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr);
1388693365a8SJed Brown         Bexp = Bexp_mine;
1389693365a8SJed Brown       } else {
1390693365a8SJed Brown         /* See if the preconditioning matrix can be viewed and added directly */
1391693365a8SJed Brown         ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr);
1392693365a8SJed Brown         if (flg) Bexp = *B;
1393693365a8SJed Brown         else {
1394693365a8SJed Brown           /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */
1395693365a8SJed Brown           ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr);
1396693365a8SJed Brown           Bexp = Bexp_mine;
1397693365a8SJed Brown         }
1398693365a8SJed Brown       }
1399693365a8SJed Brown       ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr);
1400693365a8SJed Brown       ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr);
1401693365a8SJed Brown       ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr);
1402693365a8SJed Brown       if (flag_draw || flag_contour) {
1403693365a8SJed Brown         ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr);
1404693365a8SJed Brown         if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);}
1405693365a8SJed Brown       } else vdraw = PETSC_NULL;
1406693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr);
1407693365a8SJed Brown       if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);}
1408693365a8SJed Brown       if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);}
1409693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr);
1410693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1411693365a8SJed Brown       if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);}
1412693365a8SJed Brown       ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
1413693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr);
1414693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1415693365a8SJed Brown       if (vdraw) {              /* Always use contour for the difference */
1416693365a8SJed Brown         ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
1417693365a8SJed Brown         ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);
1418693365a8SJed Brown         ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);
1419693365a8SJed Brown       }
1420693365a8SJed Brown       if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);}
1421693365a8SJed Brown       ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr);
1422693365a8SJed Brown       ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr);
1423693365a8SJed Brown       ierr = MatDestroy(&FDexp);CHKERRQ(ierr);
1424693365a8SJed Brown     }
1425693365a8SJed Brown   }
14263a40ed3dSBarry Smith   PetscFunctionReturn(0);
14279b94acceSBarry Smith }
14289b94acceSBarry Smith 
14294a2ae208SSatish Balay #undef __FUNCT__
14304a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
14319b94acceSBarry Smith /*@C
14329b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1433044dda88SLois Curfman McInnes    location to store the matrix.
14349b94acceSBarry Smith 
14353f9fe445SBarry Smith    Logically Collective on SNES and Mat
1436c7afd0dbSLois Curfman McInnes 
14379b94acceSBarry Smith    Input Parameters:
1438c7afd0dbSLois Curfman McInnes +  snes - the SNES context
14399b94acceSBarry Smith .  A - Jacobian matrix
14409b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
1441efd51863SBarry Smith .  func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value)
1442c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1443efd51863SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value)
14449b94acceSBarry Smith 
14459b94acceSBarry Smith    Calling sequence of func:
14468d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
14479b94acceSBarry Smith 
1448c7afd0dbSLois Curfman McInnes +  x - input vector
14499b94acceSBarry Smith .  A - Jacobian matrix
14509b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1451ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
14522b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1453c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
14549b94acceSBarry Smith 
14559b94acceSBarry Smith    Notes:
145694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
14572cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1458ac21db08SLois Curfman McInnes 
1459ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
14609b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
14619b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
14629b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
14639b94acceSBarry Smith    throughout the global iterations.
14649b94acceSBarry Smith 
146516913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
146616913363SBarry Smith    each matrix.
146716913363SBarry Smith 
1468a8a26c1eSJed Brown    If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument
1469a8a26c1eSJed Brown    must be a MatFDColoring.
1470a8a26c1eSJed Brown 
147136851e7fSLois Curfman McInnes    Level: beginner
147236851e7fSLois Curfman McInnes 
14739b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
14749b94acceSBarry Smith 
14753ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
14769b94acceSBarry Smith @*/
14777087cfbeSBarry Smith PetscErrorCode  SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
14789b94acceSBarry Smith {
1479dfbe8321SBarry Smith   PetscErrorCode ierr;
14803a7fca6bSBarry Smith 
14813a40ed3dSBarry Smith   PetscFunctionBegin;
14820700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14830700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
14840700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1485c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
148606975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1487e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
14883a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
14893a7fca6bSBarry Smith   if (A) {
14907dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
14916bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
14929b94acceSBarry Smith     snes->jacobian = A;
14933a7fca6bSBarry Smith   }
14943a7fca6bSBarry Smith   if (B) {
14957dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
14966bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
14979b94acceSBarry Smith     snes->jacobian_pre = B;
14983a7fca6bSBarry Smith   }
14993a40ed3dSBarry Smith   PetscFunctionReturn(0);
15009b94acceSBarry Smith }
150162fef451SLois Curfman McInnes 
15024a2ae208SSatish Balay #undef __FUNCT__
15034a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1504c2aafc4cSSatish Balay /*@C
1505b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1506b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1507b4fd4287SBarry Smith 
1508c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1509c7afd0dbSLois Curfman McInnes 
1510b4fd4287SBarry Smith    Input Parameter:
1511b4fd4287SBarry Smith .  snes - the nonlinear solver context
1512b4fd4287SBarry Smith 
1513b4fd4287SBarry Smith    Output Parameters:
1514c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1515b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
151670e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
151770e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1518fee21e36SBarry Smith 
151936851e7fSLois Curfman McInnes    Level: advanced
152036851e7fSLois Curfman McInnes 
1521b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1522b4fd4287SBarry Smith @*/
15237087cfbeSBarry Smith PetscErrorCode  SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1524b4fd4287SBarry Smith {
15253a40ed3dSBarry Smith   PetscFunctionBegin;
15260700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1527b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1528b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1529e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
153070e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
15313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1532b4fd4287SBarry Smith }
1533b4fd4287SBarry Smith 
15349b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
15359b94acceSBarry Smith 
15364a2ae208SSatish Balay #undef __FUNCT__
15374a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
15389b94acceSBarry Smith /*@
15399b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1540272ac6f2SLois Curfman McInnes    of a nonlinear solver.
15419b94acceSBarry Smith 
1542fee21e36SBarry Smith    Collective on SNES
1543fee21e36SBarry Smith 
1544c7afd0dbSLois Curfman McInnes    Input Parameters:
154570e92668SMatthew Knepley .  snes - the SNES context
1546c7afd0dbSLois Curfman McInnes 
1547272ac6f2SLois Curfman McInnes    Notes:
1548272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1549272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1550272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1551272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1552272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1553272ac6f2SLois Curfman McInnes 
155436851e7fSLois Curfman McInnes    Level: advanced
155536851e7fSLois Curfman McInnes 
15569b94acceSBarry Smith .keywords: SNES, nonlinear, setup
15579b94acceSBarry Smith 
15589b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
15599b94acceSBarry Smith @*/
15607087cfbeSBarry Smith PetscErrorCode  SNESSetUp(SNES snes)
15619b94acceSBarry Smith {
1562dfbe8321SBarry Smith   PetscErrorCode ierr;
15633a40ed3dSBarry Smith 
15643a40ed3dSBarry Smith   PetscFunctionBegin;
15650700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
15664dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
15679b94acceSBarry Smith 
15687adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
156985385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
157085385478SLisandro Dalcin   }
157185385478SLisandro Dalcin 
1572efd51863SBarry Smith   if (!snes->vec_func && snes->dm && !snes->vec_rhs) {
1573efd51863SBarry Smith     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1574efd51863SBarry Smith   }
157517186662SBarry Smith   if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
1576644e2e5bSBarry Smith   if (!snes->ops->computefunction && !snes->vec_rhs && !snes->dm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() or SNESSetDM() first");
157717186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
157858c9b817SLisandro Dalcin   if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
157958c9b817SLisandro Dalcin 
158058c9b817SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
158158c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_rhs, &snes->vec_func);CHKERRQ(ierr);
158258c9b817SLisandro Dalcin   }
158358c9b817SLisandro Dalcin   if (!snes->vec_sol_update /* && snes->vec_sol */) {
158458c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr);
158558c9b817SLisandro Dalcin     ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr);
158658c9b817SLisandro Dalcin   }
158758c9b817SLisandro Dalcin 
1588ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1589ef8dffc7SBarry Smith     Mat J;
1590ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1591cab2e9ccSBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDMComputeJacobian,PETSC_NULL);CHKERRQ(ierr);
1592cab2e9ccSBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1593a8248277SBarry Smith   } else if (snes->ops->computejacobian == MatMFFDComputeJacobian) {
1594a8248277SBarry Smith     Mat J;
1595a8248277SBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
1596a8248277SBarry Smith     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
1597a8248277SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
1598a8248277SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
1599a8248277SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1600a8248277SBarry Smith   } else if (snes->dm && snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) {
1601a8248277SBarry Smith     Mat J,B;
1602a8248277SBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
1603a8248277SBarry Smith     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
1604a8248277SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
1605a8248277SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr);
1606a8248277SBarry Smith     ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,snes->funP);CHKERRQ(ierr);
1607a8248277SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1608a8248277SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
1609efd51863SBarry Smith   } else if (snes->dm && !snes->jacobian_pre){
1610efd51863SBarry Smith     Mat J;
1611efd51863SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
16123cbb28f5SBarry Smith     ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1613efd51863SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1614ef8dffc7SBarry Smith   }
1615efd51863SBarry Smith 
1616b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1617b710008aSBarry Smith 
1618d25893d9SBarry Smith   if (snes->ops->usercompute && !snes->user) {
1619d25893d9SBarry Smith     ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr);
1620d25893d9SBarry Smith   }
1621d25893d9SBarry Smith 
1622410397dcSLisandro Dalcin   if (snes->ops->setup) {
1623410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1624410397dcSLisandro Dalcin   }
162558c9b817SLisandro Dalcin 
16267aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
16273a40ed3dSBarry Smith   PetscFunctionReturn(0);
16289b94acceSBarry Smith }
16299b94acceSBarry Smith 
16304a2ae208SSatish Balay #undef __FUNCT__
163137596af1SLisandro Dalcin #define __FUNCT__ "SNESReset"
163237596af1SLisandro Dalcin /*@
163337596af1SLisandro Dalcin    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats
163437596af1SLisandro Dalcin 
163537596af1SLisandro Dalcin    Collective on SNES
163637596af1SLisandro Dalcin 
163737596af1SLisandro Dalcin    Input Parameter:
163837596af1SLisandro Dalcin .  snes - iterative context obtained from SNESCreate()
163937596af1SLisandro Dalcin 
1640d25893d9SBarry Smith    Level: intermediate
1641d25893d9SBarry Smith 
1642d25893d9SBarry Smith    Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext()
164337596af1SLisandro Dalcin 
164437596af1SLisandro Dalcin .keywords: SNES, destroy
164537596af1SLisandro Dalcin 
164637596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
164737596af1SLisandro Dalcin @*/
164837596af1SLisandro Dalcin PetscErrorCode  SNESReset(SNES snes)
164937596af1SLisandro Dalcin {
165037596af1SLisandro Dalcin   PetscErrorCode ierr;
165137596af1SLisandro Dalcin 
165237596af1SLisandro Dalcin   PetscFunctionBegin;
165337596af1SLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1654d25893d9SBarry Smith   if (snes->ops->userdestroy && snes->user) {
1655d25893d9SBarry Smith     ierr       = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr);
1656d25893d9SBarry Smith     snes->user = PETSC_NULL;
1657d25893d9SBarry Smith   }
1658644e2e5bSBarry Smith   if (snes->ops->computejacobian == SNESDefaultComputeJacobianColor && snes->dm) {
1659644e2e5bSBarry Smith     ierr = MatFDColoringDestroy((MatFDColoring*)&snes->jacP);CHKERRQ(ierr);
1660644e2e5bSBarry Smith     snes->ops->computejacobian = PETSC_NULL;
1661644e2e5bSBarry Smith   }
1662d25893d9SBarry Smith 
166337596af1SLisandro Dalcin   if (snes->ops->reset) {
166437596af1SLisandro Dalcin     ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr);
166537596af1SLisandro Dalcin   }
166637596af1SLisandro Dalcin   if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);}
16676bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
16686bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
16696bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr);
16706bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
16716bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
16726bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
167337596af1SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
167437596af1SLisandro Dalcin   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
167537596af1SLisandro Dalcin   snes->nwork = snes->nvwork = 0;
167637596af1SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
167737596af1SLisandro Dalcin   PetscFunctionReturn(0);
167837596af1SLisandro Dalcin }
167937596af1SLisandro Dalcin 
168037596af1SLisandro Dalcin #undef __FUNCT__
16814a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
168252baeb72SSatish Balay /*@
16839b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
16849b94acceSBarry Smith    with SNESCreate().
16859b94acceSBarry Smith 
1686c7afd0dbSLois Curfman McInnes    Collective on SNES
1687c7afd0dbSLois Curfman McInnes 
16889b94acceSBarry Smith    Input Parameter:
16899b94acceSBarry Smith .  snes - the SNES context
16909b94acceSBarry Smith 
169136851e7fSLois Curfman McInnes    Level: beginner
169236851e7fSLois Curfman McInnes 
16939b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
16949b94acceSBarry Smith 
169563a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
16969b94acceSBarry Smith @*/
16976bf464f9SBarry Smith PetscErrorCode  SNESDestroy(SNES *snes)
16989b94acceSBarry Smith {
16996849ba73SBarry Smith   PetscErrorCode ierr;
17003a40ed3dSBarry Smith 
17013a40ed3dSBarry Smith   PetscFunctionBegin;
17026bf464f9SBarry Smith   if (!*snes) PetscFunctionReturn(0);
17036bf464f9SBarry Smith   PetscValidHeaderSpecific((*snes),SNES_CLASSID,1);
17046bf464f9SBarry Smith   if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);}
1705d4bb536fSBarry Smith 
17066bf464f9SBarry Smith   ierr = SNESReset((*snes));CHKERRQ(ierr);
17076b8b9a38SLisandro Dalcin 
1708be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
17096bf464f9SBarry Smith   ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr);
17106bf464f9SBarry Smith   if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);}
17116d4c513bSLisandro Dalcin 
17126bf464f9SBarry Smith   ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr);
17136bf464f9SBarry Smith   ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr);
17146b8b9a38SLisandro Dalcin 
17156bf464f9SBarry Smith   ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr);
17166bf464f9SBarry Smith   if ((*snes)->ops->convergeddestroy) {
17176bf464f9SBarry Smith     ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr);
17186b8b9a38SLisandro Dalcin   }
17196bf464f9SBarry Smith   if ((*snes)->conv_malloc) {
17206bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr);
17216bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr);
172258c9b817SLisandro Dalcin   }
17236bf464f9SBarry Smith   ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr);
1724a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
17253a40ed3dSBarry Smith   PetscFunctionReturn(0);
17269b94acceSBarry Smith }
17279b94acceSBarry Smith 
17289b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
17299b94acceSBarry Smith 
17304a2ae208SSatish Balay #undef __FUNCT__
1731a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1732a8054027SBarry Smith /*@
1733a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1734a8054027SBarry Smith 
17353f9fe445SBarry Smith    Logically Collective on SNES
1736a8054027SBarry Smith 
1737a8054027SBarry Smith    Input Parameters:
1738a8054027SBarry Smith +  snes - the SNES context
1739a8054027SBarry 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
17403b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1741a8054027SBarry Smith 
1742a8054027SBarry Smith    Options Database Keys:
1743a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1744a8054027SBarry Smith 
1745a8054027SBarry Smith    Notes:
1746a8054027SBarry Smith    The default is 1
1747a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1748a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1749a8054027SBarry Smith 
1750a8054027SBarry Smith    Level: intermediate
1751a8054027SBarry Smith 
1752a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1753a8054027SBarry Smith 
1754e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1755a8054027SBarry Smith 
1756a8054027SBarry Smith @*/
17577087cfbeSBarry Smith PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1758a8054027SBarry Smith {
1759a8054027SBarry Smith   PetscFunctionBegin;
17600700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1761e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1762e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1763c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1764a8054027SBarry Smith   snes->lagpreconditioner = lag;
1765a8054027SBarry Smith   PetscFunctionReturn(0);
1766a8054027SBarry Smith }
1767a8054027SBarry Smith 
1768a8054027SBarry Smith #undef __FUNCT__
1769efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence"
1770efd51863SBarry Smith /*@
1771efd51863SBarry Smith    SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does
1772efd51863SBarry Smith 
1773efd51863SBarry Smith    Logically Collective on SNES
1774efd51863SBarry Smith 
1775efd51863SBarry Smith    Input Parameters:
1776efd51863SBarry Smith +  snes - the SNES context
1777efd51863SBarry Smith -  steps - the number of refinements to do, defaults to 0
1778efd51863SBarry Smith 
1779efd51863SBarry Smith    Options Database Keys:
1780efd51863SBarry Smith .    -snes_grid_sequence <steps>
1781efd51863SBarry Smith 
1782efd51863SBarry Smith    Level: intermediate
1783efd51863SBarry Smith 
1784efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1785efd51863SBarry Smith 
1786efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1787efd51863SBarry Smith 
1788efd51863SBarry Smith @*/
1789efd51863SBarry Smith PetscErrorCode  SNESSetGridSequence(SNES snes,PetscInt steps)
1790efd51863SBarry Smith {
1791efd51863SBarry Smith   PetscFunctionBegin;
1792efd51863SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1793efd51863SBarry Smith   PetscValidLogicalCollectiveInt(snes,steps,2);
1794efd51863SBarry Smith   snes->gridsequence = steps;
1795efd51863SBarry Smith   PetscFunctionReturn(0);
1796efd51863SBarry Smith }
1797efd51863SBarry Smith 
1798efd51863SBarry Smith #undef __FUNCT__
1799a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1800a8054027SBarry Smith /*@
1801a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1802a8054027SBarry Smith 
18033f9fe445SBarry Smith    Not Collective
1804a8054027SBarry Smith 
1805a8054027SBarry Smith    Input Parameter:
1806a8054027SBarry Smith .  snes - the SNES context
1807a8054027SBarry Smith 
1808a8054027SBarry Smith    Output Parameter:
1809a8054027SBarry 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
18103b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1811a8054027SBarry Smith 
1812a8054027SBarry Smith    Options Database Keys:
1813a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1814a8054027SBarry Smith 
1815a8054027SBarry Smith    Notes:
1816a8054027SBarry Smith    The default is 1
1817a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1818a8054027SBarry Smith 
1819a8054027SBarry Smith    Level: intermediate
1820a8054027SBarry Smith 
1821a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1822a8054027SBarry Smith 
1823a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1824a8054027SBarry Smith 
1825a8054027SBarry Smith @*/
18267087cfbeSBarry Smith PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1827a8054027SBarry Smith {
1828a8054027SBarry Smith   PetscFunctionBegin;
18290700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1830a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1831a8054027SBarry Smith   PetscFunctionReturn(0);
1832a8054027SBarry Smith }
1833a8054027SBarry Smith 
1834a8054027SBarry Smith #undef __FUNCT__
1835e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1836e35cf81dSBarry Smith /*@
1837e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1838e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1839e35cf81dSBarry Smith 
18403f9fe445SBarry Smith    Logically Collective on SNES
1841e35cf81dSBarry Smith 
1842e35cf81dSBarry Smith    Input Parameters:
1843e35cf81dSBarry Smith +  snes - the SNES context
1844e35cf81dSBarry 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
1845fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1846e35cf81dSBarry Smith 
1847e35cf81dSBarry Smith    Options Database Keys:
1848e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1849e35cf81dSBarry Smith 
1850e35cf81dSBarry Smith    Notes:
1851e35cf81dSBarry Smith    The default is 1
1852e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1853fe3ffe1eSBarry 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
1854fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1855e35cf81dSBarry Smith 
1856e35cf81dSBarry Smith    Level: intermediate
1857e35cf81dSBarry Smith 
1858e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1859e35cf81dSBarry Smith 
1860e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1861e35cf81dSBarry Smith 
1862e35cf81dSBarry Smith @*/
18637087cfbeSBarry Smith PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
1864e35cf81dSBarry Smith {
1865e35cf81dSBarry Smith   PetscFunctionBegin;
18660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1867e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1868e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1869c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1870e35cf81dSBarry Smith   snes->lagjacobian = lag;
1871e35cf81dSBarry Smith   PetscFunctionReturn(0);
1872e35cf81dSBarry Smith }
1873e35cf81dSBarry Smith 
1874e35cf81dSBarry Smith #undef __FUNCT__
1875e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1876e35cf81dSBarry Smith /*@
1877e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1878e35cf81dSBarry Smith 
18793f9fe445SBarry Smith    Not Collective
1880e35cf81dSBarry Smith 
1881e35cf81dSBarry Smith    Input Parameter:
1882e35cf81dSBarry Smith .  snes - the SNES context
1883e35cf81dSBarry Smith 
1884e35cf81dSBarry Smith    Output Parameter:
1885e35cf81dSBarry 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
1886e35cf81dSBarry Smith          the Jacobian is built etc.
1887e35cf81dSBarry Smith 
1888e35cf81dSBarry Smith    Options Database Keys:
1889e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1890e35cf81dSBarry Smith 
1891e35cf81dSBarry Smith    Notes:
1892e35cf81dSBarry Smith    The default is 1
1893e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1894e35cf81dSBarry Smith 
1895e35cf81dSBarry Smith    Level: intermediate
1896e35cf81dSBarry Smith 
1897e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1898e35cf81dSBarry Smith 
1899e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1900e35cf81dSBarry Smith 
1901e35cf81dSBarry Smith @*/
19027087cfbeSBarry Smith PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
1903e35cf81dSBarry Smith {
1904e35cf81dSBarry Smith   PetscFunctionBegin;
19050700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1906e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1907e35cf81dSBarry Smith   PetscFunctionReturn(0);
1908e35cf81dSBarry Smith }
1909e35cf81dSBarry Smith 
1910e35cf81dSBarry Smith #undef __FUNCT__
19114a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
19129b94acceSBarry Smith /*@
1913d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
19149b94acceSBarry Smith 
19153f9fe445SBarry Smith    Logically Collective on SNES
1916c7afd0dbSLois Curfman McInnes 
19179b94acceSBarry Smith    Input Parameters:
1918c7afd0dbSLois Curfman McInnes +  snes - the SNES context
191970441072SBarry Smith .  abstol - absolute convergence tolerance
192033174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
192133174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
192233174efeSLois Curfman McInnes            of the change in the solution between steps
192333174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1924c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1925fee21e36SBarry Smith 
192633174efeSLois Curfman McInnes    Options Database Keys:
192770441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1928c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1929c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1930c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1931c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
19329b94acceSBarry Smith 
1933d7a720efSLois Curfman McInnes    Notes:
19349b94acceSBarry Smith    The default maximum number of iterations is 50.
19359b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
19369b94acceSBarry Smith 
193736851e7fSLois Curfman McInnes    Level: intermediate
193836851e7fSLois Curfman McInnes 
193933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
19409b94acceSBarry Smith 
19412492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
19429b94acceSBarry Smith @*/
19437087cfbeSBarry Smith PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
19449b94acceSBarry Smith {
19453a40ed3dSBarry Smith   PetscFunctionBegin;
19460700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1947c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
1948c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
1949c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
1950c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
1951c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
1952c5eb9154SBarry Smith 
195370441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1954d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1955d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1956d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1957d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
19583a40ed3dSBarry Smith   PetscFunctionReturn(0);
19599b94acceSBarry Smith }
19609b94acceSBarry Smith 
19614a2ae208SSatish Balay #undef __FUNCT__
19624a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
19639b94acceSBarry Smith /*@
196433174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
196533174efeSLois Curfman McInnes 
1966c7afd0dbSLois Curfman McInnes    Not Collective
1967c7afd0dbSLois Curfman McInnes 
196833174efeSLois Curfman McInnes    Input Parameters:
1969c7afd0dbSLois Curfman McInnes +  snes - the SNES context
197085385478SLisandro Dalcin .  atol - absolute convergence tolerance
197133174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
197233174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
197333174efeSLois Curfman McInnes            of the change in the solution between steps
197433174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1975c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1976fee21e36SBarry Smith 
197733174efeSLois Curfman McInnes    Notes:
197833174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
197933174efeSLois Curfman McInnes 
198036851e7fSLois Curfman McInnes    Level: intermediate
198136851e7fSLois Curfman McInnes 
198233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
198333174efeSLois Curfman McInnes 
198433174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
198533174efeSLois Curfman McInnes @*/
19867087cfbeSBarry Smith PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
198733174efeSLois Curfman McInnes {
19883a40ed3dSBarry Smith   PetscFunctionBegin;
19890700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
199085385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
199133174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
199233174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
199333174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
199433174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
19953a40ed3dSBarry Smith   PetscFunctionReturn(0);
199633174efeSLois Curfman McInnes }
199733174efeSLois Curfman McInnes 
19984a2ae208SSatish Balay #undef __FUNCT__
19994a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
200033174efeSLois Curfman McInnes /*@
20019b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
20029b94acceSBarry Smith 
20033f9fe445SBarry Smith    Logically Collective on SNES
2004fee21e36SBarry Smith 
2005c7afd0dbSLois Curfman McInnes    Input Parameters:
2006c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2007c7afd0dbSLois Curfman McInnes -  tol - tolerance
2008c7afd0dbSLois Curfman McInnes 
20099b94acceSBarry Smith    Options Database Key:
2010c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
20119b94acceSBarry Smith 
201236851e7fSLois Curfman McInnes    Level: intermediate
201336851e7fSLois Curfman McInnes 
20149b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
20159b94acceSBarry Smith 
20162492ecdbSBarry Smith .seealso: SNESSetTolerances()
20179b94acceSBarry Smith @*/
20187087cfbeSBarry Smith PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
20199b94acceSBarry Smith {
20203a40ed3dSBarry Smith   PetscFunctionBegin;
20210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2022c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
20239b94acceSBarry Smith   snes->deltatol = tol;
20243a40ed3dSBarry Smith   PetscFunctionReturn(0);
20259b94acceSBarry Smith }
20269b94acceSBarry Smith 
2027df9fa365SBarry Smith /*
2028df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
2029df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
2030df9fa365SBarry Smith    macros instead of functions
2031df9fa365SBarry Smith */
20324a2ae208SSatish Balay #undef __FUNCT__
2033a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
20347087cfbeSBarry Smith PetscErrorCode  SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
2035ce1608b8SBarry Smith {
2036dfbe8321SBarry Smith   PetscErrorCode ierr;
2037ce1608b8SBarry Smith 
2038ce1608b8SBarry Smith   PetscFunctionBegin;
20390700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2040a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
2041ce1608b8SBarry Smith   PetscFunctionReturn(0);
2042ce1608b8SBarry Smith }
2043ce1608b8SBarry Smith 
20444a2ae208SSatish Balay #undef __FUNCT__
2045a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
20467087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2047df9fa365SBarry Smith {
2048dfbe8321SBarry Smith   PetscErrorCode ierr;
2049df9fa365SBarry Smith 
2050df9fa365SBarry Smith   PetscFunctionBegin;
2051a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2052df9fa365SBarry Smith   PetscFunctionReturn(0);
2053df9fa365SBarry Smith }
2054df9fa365SBarry Smith 
20554a2ae208SSatish Balay #undef __FUNCT__
2056a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
20576bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGDestroy(PetscDrawLG *draw)
2058df9fa365SBarry Smith {
2059dfbe8321SBarry Smith   PetscErrorCode ierr;
2060df9fa365SBarry Smith 
2061df9fa365SBarry Smith   PetscFunctionBegin;
2062a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2063df9fa365SBarry Smith   PetscFunctionReturn(0);
2064df9fa365SBarry Smith }
2065df9fa365SBarry Smith 
20667087cfbeSBarry Smith extern PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
2067b271bb04SBarry Smith #undef __FUNCT__
2068b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
20697087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
2070b271bb04SBarry Smith {
2071b271bb04SBarry Smith   PetscDrawLG      lg;
2072b271bb04SBarry Smith   PetscErrorCode   ierr;
2073b271bb04SBarry Smith   PetscReal        x,y,per;
2074b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
2075b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
2076b271bb04SBarry Smith   PetscDraw        draw;
2077b271bb04SBarry Smith   PetscFunctionBegin;
2078b271bb04SBarry Smith   if (!monctx) {
2079b271bb04SBarry Smith     MPI_Comm    comm;
2080b271bb04SBarry Smith 
2081b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
2082b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
2083b271bb04SBarry Smith   }
2084b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
2085b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2086b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2087b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
2088b271bb04SBarry Smith   x = (PetscReal) n;
2089b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
2090b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2091b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2092b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2093b271bb04SBarry Smith   }
2094b271bb04SBarry Smith 
2095b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
2096b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2097b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2098b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
2099b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
2100b271bb04SBarry Smith   x = (PetscReal) n;
2101b271bb04SBarry Smith   y = 100.0*per;
2102b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2103b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2104b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2105b271bb04SBarry Smith   }
2106b271bb04SBarry Smith 
2107b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
2108b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2109b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2110b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
2111b271bb04SBarry Smith   x = (PetscReal) n;
2112b271bb04SBarry Smith   y = (prev - rnorm)/prev;
2113b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2114b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2115b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2116b271bb04SBarry Smith   }
2117b271bb04SBarry Smith 
2118b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
2119b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2120b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2121b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
2122b271bb04SBarry Smith   x = (PetscReal) n;
2123b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
2124b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
2125b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2126b271bb04SBarry Smith   }
2127b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2128b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2129b271bb04SBarry Smith   }
2130b271bb04SBarry Smith   prev = rnorm;
2131b271bb04SBarry Smith   PetscFunctionReturn(0);
2132b271bb04SBarry Smith }
2133b271bb04SBarry Smith 
2134b271bb04SBarry Smith #undef __FUNCT__
2135b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
21367087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2137b271bb04SBarry Smith {
2138b271bb04SBarry Smith   PetscErrorCode ierr;
2139b271bb04SBarry Smith 
2140b271bb04SBarry Smith   PetscFunctionBegin;
2141b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2142b271bb04SBarry Smith   PetscFunctionReturn(0);
2143b271bb04SBarry Smith }
2144b271bb04SBarry Smith 
2145b271bb04SBarry Smith #undef __FUNCT__
2146b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
21476bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGRangeDestroy(PetscDrawLG *draw)
2148b271bb04SBarry Smith {
2149b271bb04SBarry Smith   PetscErrorCode ierr;
2150b271bb04SBarry Smith 
2151b271bb04SBarry Smith   PetscFunctionBegin;
2152b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2153b271bb04SBarry Smith   PetscFunctionReturn(0);
2154b271bb04SBarry Smith }
2155b271bb04SBarry Smith 
21567a03ce2fSLisandro Dalcin #undef __FUNCT__
21577a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor"
21587a03ce2fSLisandro Dalcin /*
21597a03ce2fSLisandro Dalcin      Runs the user provided monitor routines, if they exists.
21607a03ce2fSLisandro Dalcin */
21617a03ce2fSLisandro Dalcin PetscErrorCode  SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm)
21627a03ce2fSLisandro Dalcin {
21637a03ce2fSLisandro Dalcin   PetscErrorCode ierr;
21647a03ce2fSLisandro Dalcin   PetscInt       i,n = snes->numbermonitors;
21657a03ce2fSLisandro Dalcin 
21667a03ce2fSLisandro Dalcin   PetscFunctionBegin;
21677a03ce2fSLisandro Dalcin   for (i=0; i<n; i++) {
21687a03ce2fSLisandro Dalcin     ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr);
21697a03ce2fSLisandro Dalcin   }
21707a03ce2fSLisandro Dalcin   PetscFunctionReturn(0);
21717a03ce2fSLisandro Dalcin }
21727a03ce2fSLisandro Dalcin 
21739b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
21749b94acceSBarry Smith 
21754a2ae208SSatish Balay #undef __FUNCT__
2176a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
21779b94acceSBarry Smith /*@C
2178a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
21799b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
21809b94acceSBarry Smith    progress.
21819b94acceSBarry Smith 
21823f9fe445SBarry Smith    Logically Collective on SNES
2183fee21e36SBarry Smith 
2184c7afd0dbSLois Curfman McInnes    Input Parameters:
2185c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2186c7afd0dbSLois Curfman McInnes .  func - monitoring routine
2187b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
2188e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
2189b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2190b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
21919b94acceSBarry Smith 
2192c7afd0dbSLois Curfman McInnes    Calling sequence of func:
2193a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
2194c7afd0dbSLois Curfman McInnes 
2195c7afd0dbSLois Curfman McInnes +    snes - the SNES context
2196c7afd0dbSLois Curfman McInnes .    its - iteration number
2197c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
219840a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
21999b94acceSBarry Smith 
22009665c990SLois Curfman McInnes    Options Database Keys:
2201a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
2202a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
2203a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
2204cca6129bSJed Brown -    -snes_monitor_cancel - cancels all monitors that have
2205c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
2206a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
2207c7afd0dbSLois Curfman McInnes                             does not cancel those set via
2208c7afd0dbSLois Curfman McInnes                             the options database.
22099665c990SLois Curfman McInnes 
2210639f9d9dSBarry Smith    Notes:
22116bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
2212a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
22136bc08f3fSLois Curfman McInnes    order in which they were set.
2214639f9d9dSBarry Smith 
2215025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
2216025f1a04SBarry Smith 
221736851e7fSLois Curfman McInnes    Level: intermediate
221836851e7fSLois Curfman McInnes 
22199b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
22209b94acceSBarry Smith 
2221a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
22229b94acceSBarry Smith @*/
2223c2efdce3SBarry Smith PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
22249b94acceSBarry Smith {
2225b90d0a6eSBarry Smith   PetscInt       i;
2226649052a6SBarry Smith   PetscErrorCode ierr;
2227b90d0a6eSBarry Smith 
22283a40ed3dSBarry Smith   PetscFunctionBegin;
22290700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
223017186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2231b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
2232649052a6SBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) {
2233649052a6SBarry Smith       if (monitordestroy) {
2234c2efdce3SBarry Smith         ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr);
2235649052a6SBarry Smith       }
2236b90d0a6eSBarry Smith       PetscFunctionReturn(0);
2237b90d0a6eSBarry Smith     }
2238b90d0a6eSBarry Smith   }
2239b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
2240b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
2241639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
22423a40ed3dSBarry Smith   PetscFunctionReturn(0);
22439b94acceSBarry Smith }
22449b94acceSBarry Smith 
22454a2ae208SSatish Balay #undef __FUNCT__
2246a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
22475cd90555SBarry Smith /*@C
2248a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
22495cd90555SBarry Smith 
22503f9fe445SBarry Smith    Logically Collective on SNES
2251c7afd0dbSLois Curfman McInnes 
22525cd90555SBarry Smith    Input Parameters:
22535cd90555SBarry Smith .  snes - the SNES context
22545cd90555SBarry Smith 
22551a480d89SAdministrator    Options Database Key:
2256a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
2257a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
2258c7afd0dbSLois Curfman McInnes     set via the options database
22595cd90555SBarry Smith 
22605cd90555SBarry Smith    Notes:
22615cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
22625cd90555SBarry Smith 
226336851e7fSLois Curfman McInnes    Level: intermediate
226436851e7fSLois Curfman McInnes 
22655cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
22665cd90555SBarry Smith 
2267a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
22685cd90555SBarry Smith @*/
22697087cfbeSBarry Smith PetscErrorCode  SNESMonitorCancel(SNES snes)
22705cd90555SBarry Smith {
2271d952e501SBarry Smith   PetscErrorCode ierr;
2272d952e501SBarry Smith   PetscInt       i;
2273d952e501SBarry Smith 
22745cd90555SBarry Smith   PetscFunctionBegin;
22750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2276d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2277d952e501SBarry Smith     if (snes->monitordestroy[i]) {
22783c4aec1bSBarry Smith       ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr);
2279d952e501SBarry Smith     }
2280d952e501SBarry Smith   }
22815cd90555SBarry Smith   snes->numbermonitors = 0;
22825cd90555SBarry Smith   PetscFunctionReturn(0);
22835cd90555SBarry Smith }
22845cd90555SBarry Smith 
22854a2ae208SSatish Balay #undef __FUNCT__
22864a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
22879b94acceSBarry Smith /*@C
22889b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
22899b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
22909b94acceSBarry Smith 
22913f9fe445SBarry Smith    Logically Collective on SNES
2292fee21e36SBarry Smith 
2293c7afd0dbSLois Curfman McInnes    Input Parameters:
2294c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2295c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
22967f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
22977f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
22989b94acceSBarry Smith 
2299c7afd0dbSLois Curfman McInnes    Calling sequence of func:
230006ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2301c7afd0dbSLois Curfman McInnes 
2302c7afd0dbSLois Curfman McInnes +    snes - the SNES context
230306ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2304c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2305184914b5SBarry Smith .    reason - reason for convergence/divergence
2306c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
23074b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
23084b27c08aSLois Curfman McInnes -    f - 2-norm of function
23099b94acceSBarry Smith 
231036851e7fSLois Curfman McInnes    Level: advanced
231136851e7fSLois Curfman McInnes 
23129b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
23139b94acceSBarry Smith 
231485385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
23159b94acceSBarry Smith @*/
23167087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
23179b94acceSBarry Smith {
23187f7931b9SBarry Smith   PetscErrorCode ierr;
23197f7931b9SBarry Smith 
23203a40ed3dSBarry Smith   PetscFunctionBegin;
23210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
232285385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
23237f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
23247f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
23257f7931b9SBarry Smith   }
232685385478SLisandro Dalcin   snes->ops->converged        = func;
23277f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
232885385478SLisandro Dalcin   snes->cnvP                  = cctx;
23293a40ed3dSBarry Smith   PetscFunctionReturn(0);
23309b94acceSBarry Smith }
23319b94acceSBarry Smith 
23324a2ae208SSatish Balay #undef __FUNCT__
23334a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
233452baeb72SSatish Balay /*@
2335184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2336184914b5SBarry Smith 
2337184914b5SBarry Smith    Not Collective
2338184914b5SBarry Smith 
2339184914b5SBarry Smith    Input Parameter:
2340184914b5SBarry Smith .  snes - the SNES context
2341184914b5SBarry Smith 
2342184914b5SBarry Smith    Output Parameter:
23434d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2344184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2345184914b5SBarry Smith 
2346184914b5SBarry Smith    Level: intermediate
2347184914b5SBarry Smith 
2348184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2349184914b5SBarry Smith 
2350184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2351184914b5SBarry Smith 
235285385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2353184914b5SBarry Smith @*/
23547087cfbeSBarry Smith PetscErrorCode  SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2355184914b5SBarry Smith {
2356184914b5SBarry Smith   PetscFunctionBegin;
23570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23584482741eSBarry Smith   PetscValidPointer(reason,2);
2359184914b5SBarry Smith   *reason = snes->reason;
2360184914b5SBarry Smith   PetscFunctionReturn(0);
2361184914b5SBarry Smith }
2362184914b5SBarry Smith 
23634a2ae208SSatish Balay #undef __FUNCT__
23644a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2365c9005455SLois Curfman McInnes /*@
2366c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2367c9005455SLois Curfman McInnes 
23683f9fe445SBarry Smith    Logically Collective on SNES
2369fee21e36SBarry Smith 
2370c7afd0dbSLois Curfman McInnes    Input Parameters:
2371c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
23728c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2373cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2374758f92a0SBarry Smith .  na  - size of a and its
237564731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2376758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2377c7afd0dbSLois Curfman McInnes 
2378308dcc3eSBarry Smith    Notes:
2379308dcc3eSBarry Smith    If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2380308dcc3eSBarry Smith    default array of length 10000 is allocated.
2381308dcc3eSBarry Smith 
2382c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2383c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2384c9005455SLois Curfman McInnes    during the section of code that is being timed.
2385c9005455SLois Curfman McInnes 
238636851e7fSLois Curfman McInnes    Level: intermediate
238736851e7fSLois Curfman McInnes 
2388c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2389758f92a0SBarry Smith 
239008405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2391758f92a0SBarry Smith 
2392c9005455SLois Curfman McInnes @*/
23937087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool  reset)
2394c9005455SLois Curfman McInnes {
2395308dcc3eSBarry Smith   PetscErrorCode ierr;
2396308dcc3eSBarry Smith 
23973a40ed3dSBarry Smith   PetscFunctionBegin;
23980700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23994482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2400a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2401308dcc3eSBarry Smith   if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) {
2402308dcc3eSBarry Smith     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2403308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr);
2404308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr);
2405308dcc3eSBarry Smith     snes->conv_malloc   = PETSC_TRUE;
2406308dcc3eSBarry Smith   }
2407c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2408758f92a0SBarry Smith   snes->conv_hist_its   = its;
2409758f92a0SBarry Smith   snes->conv_hist_max   = na;
2410a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2411758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2412758f92a0SBarry Smith   PetscFunctionReturn(0);
2413758f92a0SBarry Smith }
2414758f92a0SBarry Smith 
2415308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2416c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
2417c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
2418308dcc3eSBarry Smith EXTERN_C_BEGIN
2419308dcc3eSBarry Smith #undef __FUNCT__
2420308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab"
2421308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
2422308dcc3eSBarry Smith {
2423308dcc3eSBarry Smith   mxArray        *mat;
2424308dcc3eSBarry Smith   PetscInt       i;
2425308dcc3eSBarry Smith   PetscReal      *ar;
2426308dcc3eSBarry Smith 
2427308dcc3eSBarry Smith   PetscFunctionBegin;
2428308dcc3eSBarry Smith   mat  = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
2429308dcc3eSBarry Smith   ar   = (PetscReal*) mxGetData(mat);
2430308dcc3eSBarry Smith   for (i=0; i<snes->conv_hist_len; i++) {
2431308dcc3eSBarry Smith     ar[i] = snes->conv_hist[i];
2432308dcc3eSBarry Smith   }
2433308dcc3eSBarry Smith   PetscFunctionReturn(mat);
2434308dcc3eSBarry Smith }
2435308dcc3eSBarry Smith EXTERN_C_END
2436308dcc3eSBarry Smith #endif
2437308dcc3eSBarry Smith 
2438308dcc3eSBarry Smith 
24394a2ae208SSatish Balay #undef __FUNCT__
24404a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
24410c4c9dddSBarry Smith /*@C
2442758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2443758f92a0SBarry Smith 
24443f9fe445SBarry Smith    Not Collective
2445758f92a0SBarry Smith 
2446758f92a0SBarry Smith    Input Parameter:
2447758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2448758f92a0SBarry Smith 
2449758f92a0SBarry Smith    Output Parameters:
2450758f92a0SBarry Smith .  a   - array to hold history
2451758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2452758f92a0SBarry Smith          negative if not converged) for each solve.
2453758f92a0SBarry Smith -  na  - size of a and its
2454758f92a0SBarry Smith 
2455758f92a0SBarry Smith    Notes:
2456758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2457758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2458758f92a0SBarry Smith 
2459758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2460758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2461758f92a0SBarry Smith    during the section of code that is being timed.
2462758f92a0SBarry Smith 
2463758f92a0SBarry Smith    Level: intermediate
2464758f92a0SBarry Smith 
2465758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2466758f92a0SBarry Smith 
2467758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2468758f92a0SBarry Smith 
2469758f92a0SBarry Smith @*/
24707087cfbeSBarry Smith PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2471758f92a0SBarry Smith {
2472758f92a0SBarry Smith   PetscFunctionBegin;
24730700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2474758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2475758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2476758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
24773a40ed3dSBarry Smith   PetscFunctionReturn(0);
2478c9005455SLois Curfman McInnes }
2479c9005455SLois Curfman McInnes 
2480e74ef692SMatthew Knepley #undef __FUNCT__
2481e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2482ac226902SBarry Smith /*@C
248376b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
2484eec8f646SJed Brown   at the beginning of every iteration of the nonlinear solve. Specifically
24857e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
248676b2cf59SMatthew Knepley 
24873f9fe445SBarry Smith   Logically Collective on SNES
248876b2cf59SMatthew Knepley 
248976b2cf59SMatthew Knepley   Input Parameters:
249076b2cf59SMatthew Knepley . snes - The nonlinear solver context
249176b2cf59SMatthew Knepley . func - The function
249276b2cf59SMatthew Knepley 
249376b2cf59SMatthew Knepley   Calling sequence of func:
2494b5d30489SBarry Smith . func (SNES snes, PetscInt step);
249576b2cf59SMatthew Knepley 
249676b2cf59SMatthew Knepley . step - The current step of the iteration
249776b2cf59SMatthew Knepley 
2498fe97e370SBarry Smith   Level: advanced
2499fe97e370SBarry Smith 
2500fe97e370SBarry 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()
2501fe97e370SBarry Smith         This is not used by most users.
250276b2cf59SMatthew Knepley 
250376b2cf59SMatthew Knepley .keywords: SNES, update
2504b5d30489SBarry Smith 
250585385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
250676b2cf59SMatthew Knepley @*/
25077087cfbeSBarry Smith PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
250876b2cf59SMatthew Knepley {
250976b2cf59SMatthew Knepley   PetscFunctionBegin;
25100700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2511e7788613SBarry Smith   snes->ops->update = func;
251276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
251376b2cf59SMatthew Knepley }
251476b2cf59SMatthew Knepley 
2515e74ef692SMatthew Knepley #undef __FUNCT__
2516e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
251776b2cf59SMatthew Knepley /*@
251876b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
251976b2cf59SMatthew Knepley 
252076b2cf59SMatthew Knepley   Not collective
252176b2cf59SMatthew Knepley 
252276b2cf59SMatthew Knepley   Input Parameters:
252376b2cf59SMatthew Knepley . snes - The nonlinear solver context
252476b2cf59SMatthew Knepley . step - The current step of the iteration
252576b2cf59SMatthew Knepley 
2526205452f4SMatthew Knepley   Level: intermediate
2527205452f4SMatthew Knepley 
252876b2cf59SMatthew Knepley .keywords: SNES, update
2529a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
253076b2cf59SMatthew Knepley @*/
25317087cfbeSBarry Smith PetscErrorCode  SNESDefaultUpdate(SNES snes, PetscInt step)
253276b2cf59SMatthew Knepley {
253376b2cf59SMatthew Knepley   PetscFunctionBegin;
253476b2cf59SMatthew Knepley   PetscFunctionReturn(0);
253576b2cf59SMatthew Knepley }
253676b2cf59SMatthew Knepley 
25374a2ae208SSatish Balay #undef __FUNCT__
25384a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
25399b94acceSBarry Smith /*
25409b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
25419b94acceSBarry Smith    positive parameter delta.
25429b94acceSBarry Smith 
25439b94acceSBarry Smith     Input Parameters:
2544c7afd0dbSLois Curfman McInnes +   snes - the SNES context
25459b94acceSBarry Smith .   y - approximate solution of linear system
25469b94acceSBarry Smith .   fnorm - 2-norm of current function
2547c7afd0dbSLois Curfman McInnes -   delta - trust region size
25489b94acceSBarry Smith 
25499b94acceSBarry Smith     Output Parameters:
2550c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
25519b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
25529b94acceSBarry Smith     region, and exceeds zero otherwise.
2553c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
25549b94acceSBarry Smith 
25559b94acceSBarry Smith     Note:
25564b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
25579b94acceSBarry Smith     is set to be the maximum allowable step size.
25589b94acceSBarry Smith 
25599b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
25609b94acceSBarry Smith */
2561dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
25629b94acceSBarry Smith {
2563064f8208SBarry Smith   PetscReal      nrm;
2564ea709b57SSatish Balay   PetscScalar    cnorm;
2565dfbe8321SBarry Smith   PetscErrorCode ierr;
25663a40ed3dSBarry Smith 
25673a40ed3dSBarry Smith   PetscFunctionBegin;
25680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25690700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2570c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2571184914b5SBarry Smith 
2572064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2573064f8208SBarry Smith   if (nrm > *delta) {
2574064f8208SBarry Smith      nrm = *delta/nrm;
2575064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2576064f8208SBarry Smith      cnorm = nrm;
25772dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
25789b94acceSBarry Smith      *ynorm = *delta;
25799b94acceSBarry Smith   } else {
25809b94acceSBarry Smith      *gpnorm = 0.0;
2581064f8208SBarry Smith      *ynorm = nrm;
25829b94acceSBarry Smith   }
25833a40ed3dSBarry Smith   PetscFunctionReturn(0);
25849b94acceSBarry Smith }
25859b94acceSBarry Smith 
25864a2ae208SSatish Balay #undef __FUNCT__
25874a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
25886ce558aeSBarry Smith /*@C
2589f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2590f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
25919b94acceSBarry Smith 
2592c7afd0dbSLois Curfman McInnes    Collective on SNES
2593c7afd0dbSLois Curfman McInnes 
2594b2002411SLois Curfman McInnes    Input Parameters:
2595c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2596f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
259785385478SLisandro Dalcin -  x - the solution vector.
25989b94acceSBarry Smith 
2599b2002411SLois Curfman McInnes    Notes:
26008ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
26018ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
26028ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
26038ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
26048ddd3da0SLois Curfman McInnes 
260536851e7fSLois Curfman McInnes    Level: beginner
260636851e7fSLois Curfman McInnes 
26079b94acceSBarry Smith .keywords: SNES, nonlinear, solve
26089b94acceSBarry Smith 
260985385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
26109b94acceSBarry Smith @*/
26117087cfbeSBarry Smith PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
26129b94acceSBarry Smith {
2613dfbe8321SBarry Smith   PetscErrorCode ierr;
2614ace3abfcSBarry Smith   PetscBool      flg;
2615eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2616eabae89aSBarry Smith   PetscViewer    viewer;
2617efd51863SBarry Smith   PetscInt       grid;
2618052efed2SBarry Smith 
26193a40ed3dSBarry Smith   PetscFunctionBegin;
26200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26210700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2622f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
26230700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
262485385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
262585385478SLisandro Dalcin 
2626a8248277SBarry Smith   for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);}
2627efd51863SBarry Smith   for (grid=0; grid<snes->gridsequence+1; grid++) {
2628efd51863SBarry Smith 
262985385478SLisandro Dalcin     /* set solution vector */
2630efd51863SBarry Smith     if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);}
26316bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
263285385478SLisandro Dalcin     snes->vec_sol = x;
263385385478SLisandro Dalcin     /* set afine vector if provided */
263485385478SLisandro Dalcin     if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
26356bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
263685385478SLisandro Dalcin     snes->vec_rhs = b;
263785385478SLisandro Dalcin 
263870e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
26393f149594SLisandro Dalcin 
2640d25893d9SBarry Smith     if (!grid && snes->ops->computeinitialguess) {
2641d25893d9SBarry Smith       ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr);
2642d25893d9SBarry Smith     }
2643d25893d9SBarry Smith 
2644abc0a331SBarry Smith     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
264550ffb88aSMatthew Knepley     snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2646d5e45103SBarry Smith 
26473f149594SLisandro Dalcin     ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
26484936397dSBarry Smith     ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
264985385478SLisandro Dalcin     ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
26504936397dSBarry Smith     if (snes->domainerror){
26514936397dSBarry Smith       snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
26524936397dSBarry Smith       snes->domainerror = PETSC_FALSE;
26534936397dSBarry Smith     }
265417186662SBarry Smith     if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
26553f149594SLisandro Dalcin 
26567adad957SLisandro Dalcin     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2657eabae89aSBarry Smith     if (flg && !PetscPreLoadingOn) {
26587adad957SLisandro Dalcin       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2659eabae89aSBarry Smith       ierr = SNESView(snes,viewer);CHKERRQ(ierr);
26606bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2661eabae89aSBarry Smith     }
2662eabae89aSBarry Smith 
266390d69ab7SBarry Smith     flg  = PETSC_FALSE;
2664acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2665da9b6338SBarry Smith     if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
26665968eb51SBarry Smith     if (snes->printreason) {
2667a8248277SBarry Smith       ierr = PetscViewerASCIIAddTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr);
26685968eb51SBarry Smith       if (snes->reason > 0) {
2669a8248277SBarry Smith         ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
26705968eb51SBarry Smith       } else {
2671a8248277SBarry Smith         ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
26725968eb51SBarry Smith       }
2673a8248277SBarry Smith       ierr = PetscViewerASCIISubtractTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr);
26745968eb51SBarry Smith     }
26755968eb51SBarry Smith 
2676e113a28aSBarry Smith     if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
2677efd51863SBarry Smith     if (grid <  snes->gridsequence) {
2678efd51863SBarry Smith       DM  fine;
2679efd51863SBarry Smith       Vec xnew;
2680efd51863SBarry Smith       Mat interp;
2681efd51863SBarry Smith 
2682efd51863SBarry Smith       ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr);
2683efd51863SBarry Smith       ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr);
2684efd51863SBarry Smith       ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr);
2685efd51863SBarry Smith       ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr);
2686efd51863SBarry Smith       ierr = MatDestroy(&interp);CHKERRQ(ierr);
2687efd51863SBarry Smith       x    = xnew;
2688efd51863SBarry Smith 
2689efd51863SBarry Smith       ierr = SNESReset(snes);CHKERRQ(ierr);
2690efd51863SBarry Smith       ierr = SNESSetDM(snes,fine);CHKERRQ(ierr);
2691efd51863SBarry Smith       ierr = DMDestroy(&fine);CHKERRQ(ierr);
2692a8248277SBarry Smith       ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);
2693efd51863SBarry Smith     }
2694efd51863SBarry Smith   }
26953a40ed3dSBarry Smith   PetscFunctionReturn(0);
26969b94acceSBarry Smith }
26979b94acceSBarry Smith 
26989b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
26999b94acceSBarry Smith 
27004a2ae208SSatish Balay #undef __FUNCT__
27014a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
270282bf6240SBarry Smith /*@C
27034b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
27049b94acceSBarry Smith 
2705fee21e36SBarry Smith    Collective on SNES
2706fee21e36SBarry Smith 
2707c7afd0dbSLois Curfman McInnes    Input Parameters:
2708c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2709454a90a3SBarry Smith -  type - a known method
2710c7afd0dbSLois Curfman McInnes 
2711c7afd0dbSLois Curfman McInnes    Options Database Key:
2712454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2713c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2714ae12b187SLois Curfman McInnes 
27159b94acceSBarry Smith    Notes:
2716e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
27174b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2718c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
27194b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2720c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
27219b94acceSBarry Smith 
2722ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2723ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2724ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2725ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2726ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2727ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2728ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2729ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2730ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2731b0a32e0cSBarry Smith   appropriate method.
273236851e7fSLois Curfman McInnes 
273336851e7fSLois Curfman McInnes   Level: intermediate
2734a703fe33SLois Curfman McInnes 
2735454a90a3SBarry Smith .keywords: SNES, set, type
2736435da068SBarry Smith 
2737435da068SBarry Smith .seealso: SNESType, SNESCreate()
2738435da068SBarry Smith 
27399b94acceSBarry Smith @*/
27407087cfbeSBarry Smith PetscErrorCode  SNESSetType(SNES snes,const SNESType type)
27419b94acceSBarry Smith {
2742dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
2743ace3abfcSBarry Smith   PetscBool      match;
27443a40ed3dSBarry Smith 
27453a40ed3dSBarry Smith   PetscFunctionBegin;
27460700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27474482741eSBarry Smith   PetscValidCharPointer(type,2);
274882bf6240SBarry Smith 
27496831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
27500f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
275192ff6ae8SBarry Smith 
27524b91b6eaSBarry Smith   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2753e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
275475396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
275575396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
275675396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
275775396ef9SLisandro Dalcin   snes->ops->setup          = 0;
275875396ef9SLisandro Dalcin   snes->ops->solve          = 0;
275975396ef9SLisandro Dalcin   snes->ops->view           = 0;
276075396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
276175396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
276275396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
276375396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
2764454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
276503bfa161SLisandro Dalcin   ierr = (*r)(snes);CHKERRQ(ierr);
27669fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
27679fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
27689fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr);
27699fb22e1aSBarry Smith   }
27709fb22e1aSBarry Smith #endif
27713a40ed3dSBarry Smith   PetscFunctionReturn(0);
27729b94acceSBarry Smith }
27739b94acceSBarry Smith 
2774a847f771SSatish Balay 
27759b94acceSBarry Smith /* --------------------------------------------------------------------- */
27764a2ae208SSatish Balay #undef __FUNCT__
27774a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
277852baeb72SSatish Balay /*@
27799b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2780f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
27819b94acceSBarry Smith 
2782fee21e36SBarry Smith    Not Collective
2783fee21e36SBarry Smith 
278436851e7fSLois Curfman McInnes    Level: advanced
278536851e7fSLois Curfman McInnes 
27869b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
27879b94acceSBarry Smith 
27889b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
27899b94acceSBarry Smith @*/
27907087cfbeSBarry Smith PetscErrorCode  SNESRegisterDestroy(void)
27919b94acceSBarry Smith {
2792dfbe8321SBarry Smith   PetscErrorCode ierr;
279382bf6240SBarry Smith 
27943a40ed3dSBarry Smith   PetscFunctionBegin;
27951441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
27964c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
27973a40ed3dSBarry Smith   PetscFunctionReturn(0);
27989b94acceSBarry Smith }
27999b94acceSBarry Smith 
28004a2ae208SSatish Balay #undef __FUNCT__
28014a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
28029b94acceSBarry Smith /*@C
28039a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
28049b94acceSBarry Smith 
2805c7afd0dbSLois Curfman McInnes    Not Collective
2806c7afd0dbSLois Curfman McInnes 
28079b94acceSBarry Smith    Input Parameter:
28084b0e389bSBarry Smith .  snes - nonlinear solver context
28099b94acceSBarry Smith 
28109b94acceSBarry Smith    Output Parameter:
28113a7fca6bSBarry Smith .  type - SNES method (a character string)
28129b94acceSBarry Smith 
281336851e7fSLois Curfman McInnes    Level: intermediate
281436851e7fSLois Curfman McInnes 
2815454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
28169b94acceSBarry Smith @*/
28177087cfbeSBarry Smith PetscErrorCode  SNESGetType(SNES snes,const SNESType *type)
28189b94acceSBarry Smith {
28193a40ed3dSBarry Smith   PetscFunctionBegin;
28200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
28214482741eSBarry Smith   PetscValidPointer(type,2);
28227adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
28233a40ed3dSBarry Smith   PetscFunctionReturn(0);
28249b94acceSBarry Smith }
28259b94acceSBarry Smith 
28264a2ae208SSatish Balay #undef __FUNCT__
28274a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
282852baeb72SSatish Balay /*@
28299b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
28309b94acceSBarry Smith    stored.
28319b94acceSBarry Smith 
2832c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2833c7afd0dbSLois Curfman McInnes 
28349b94acceSBarry Smith    Input Parameter:
28359b94acceSBarry Smith .  snes - the SNES context
28369b94acceSBarry Smith 
28379b94acceSBarry Smith    Output Parameter:
28389b94acceSBarry Smith .  x - the solution
28399b94acceSBarry Smith 
284070e92668SMatthew Knepley    Level: intermediate
284136851e7fSLois Curfman McInnes 
28429b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
28439b94acceSBarry Smith 
284485385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
28459b94acceSBarry Smith @*/
28467087cfbeSBarry Smith PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
28479b94acceSBarry Smith {
28483a40ed3dSBarry Smith   PetscFunctionBegin;
28490700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
28504482741eSBarry Smith   PetscValidPointer(x,2);
285185385478SLisandro Dalcin   *x = snes->vec_sol;
285270e92668SMatthew Knepley   PetscFunctionReturn(0);
285370e92668SMatthew Knepley }
285470e92668SMatthew Knepley 
285570e92668SMatthew Knepley #undef __FUNCT__
28564a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
285752baeb72SSatish Balay /*@
28589b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
28599b94acceSBarry Smith    stored.
28609b94acceSBarry Smith 
2861c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2862c7afd0dbSLois Curfman McInnes 
28639b94acceSBarry Smith    Input Parameter:
28649b94acceSBarry Smith .  snes - the SNES context
28659b94acceSBarry Smith 
28669b94acceSBarry Smith    Output Parameter:
28679b94acceSBarry Smith .  x - the solution update
28689b94acceSBarry Smith 
286936851e7fSLois Curfman McInnes    Level: advanced
287036851e7fSLois Curfman McInnes 
28719b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
28729b94acceSBarry Smith 
287385385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
28749b94acceSBarry Smith @*/
28757087cfbeSBarry Smith PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
28769b94acceSBarry Smith {
28773a40ed3dSBarry Smith   PetscFunctionBegin;
28780700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
28794482741eSBarry Smith   PetscValidPointer(x,2);
288085385478SLisandro Dalcin   *x = snes->vec_sol_update;
28813a40ed3dSBarry Smith   PetscFunctionReturn(0);
28829b94acceSBarry Smith }
28839b94acceSBarry Smith 
28844a2ae208SSatish Balay #undef __FUNCT__
28854a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
28869b94acceSBarry Smith /*@C
28873638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
28889b94acceSBarry Smith 
2889c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2890c7afd0dbSLois Curfman McInnes 
28919b94acceSBarry Smith    Input Parameter:
28929b94acceSBarry Smith .  snes - the SNES context
28939b94acceSBarry Smith 
28949b94acceSBarry Smith    Output Parameter:
28957bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
289670e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
289770e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
28989b94acceSBarry Smith 
289936851e7fSLois Curfman McInnes    Level: advanced
290036851e7fSLois Curfman McInnes 
2901a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
29029b94acceSBarry Smith 
29034b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
29049b94acceSBarry Smith @*/
29057087cfbeSBarry Smith PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
29069b94acceSBarry Smith {
29073a40ed3dSBarry Smith   PetscFunctionBegin;
29080700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
290985385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2910e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
291170e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
29123a40ed3dSBarry Smith   PetscFunctionReturn(0);
29139b94acceSBarry Smith }
29149b94acceSBarry Smith 
29154a2ae208SSatish Balay #undef __FUNCT__
29164a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
29173c7409f5SSatish Balay /*@C
29183c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2919d850072dSLois Curfman McInnes    SNES options in the database.
29203c7409f5SSatish Balay 
29213f9fe445SBarry Smith    Logically Collective on SNES
2922fee21e36SBarry Smith 
2923c7afd0dbSLois Curfman McInnes    Input Parameter:
2924c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2925c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2926c7afd0dbSLois Curfman McInnes 
2927d850072dSLois Curfman McInnes    Notes:
2928a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2929c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2930d850072dSLois Curfman McInnes 
293136851e7fSLois Curfman McInnes    Level: advanced
293236851e7fSLois Curfman McInnes 
29333c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2934a86d99e1SLois Curfman McInnes 
2935a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
29363c7409f5SSatish Balay @*/
29377087cfbeSBarry Smith PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
29383c7409f5SSatish Balay {
2939dfbe8321SBarry Smith   PetscErrorCode ierr;
29403c7409f5SSatish Balay 
29413a40ed3dSBarry Smith   PetscFunctionBegin;
29420700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2943639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
29441cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
294594b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
29463a40ed3dSBarry Smith   PetscFunctionReturn(0);
29473c7409f5SSatish Balay }
29483c7409f5SSatish Balay 
29494a2ae208SSatish Balay #undef __FUNCT__
29504a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
29513c7409f5SSatish Balay /*@C
2952f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2953d850072dSLois Curfman McInnes    SNES options in the database.
29543c7409f5SSatish Balay 
29553f9fe445SBarry Smith    Logically Collective on SNES
2956fee21e36SBarry Smith 
2957c7afd0dbSLois Curfman McInnes    Input Parameters:
2958c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2959c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2960c7afd0dbSLois Curfman McInnes 
2961d850072dSLois Curfman McInnes    Notes:
2962a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2963c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2964d850072dSLois Curfman McInnes 
296536851e7fSLois Curfman McInnes    Level: advanced
296636851e7fSLois Curfman McInnes 
29673c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2968a86d99e1SLois Curfman McInnes 
2969a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
29703c7409f5SSatish Balay @*/
29717087cfbeSBarry Smith PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
29723c7409f5SSatish Balay {
2973dfbe8321SBarry Smith   PetscErrorCode ierr;
29743c7409f5SSatish Balay 
29753a40ed3dSBarry Smith   PetscFunctionBegin;
29760700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2977639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
29781cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
297994b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
29803a40ed3dSBarry Smith   PetscFunctionReturn(0);
29813c7409f5SSatish Balay }
29823c7409f5SSatish Balay 
29834a2ae208SSatish Balay #undef __FUNCT__
29844a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
29859ab63eb5SSatish Balay /*@C
29863c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
29873c7409f5SSatish Balay    SNES options in the database.
29883c7409f5SSatish Balay 
2989c7afd0dbSLois Curfman McInnes    Not Collective
2990c7afd0dbSLois Curfman McInnes 
29913c7409f5SSatish Balay    Input Parameter:
29923c7409f5SSatish Balay .  snes - the SNES context
29933c7409f5SSatish Balay 
29943c7409f5SSatish Balay    Output Parameter:
29953c7409f5SSatish Balay .  prefix - pointer to the prefix string used
29963c7409f5SSatish Balay 
29974ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
29989ab63eb5SSatish Balay    sufficient length to hold the prefix.
29999ab63eb5SSatish Balay 
300036851e7fSLois Curfman McInnes    Level: advanced
300136851e7fSLois Curfman McInnes 
30023c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
3003a86d99e1SLois Curfman McInnes 
3004a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
30053c7409f5SSatish Balay @*/
30067087cfbeSBarry Smith PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
30073c7409f5SSatish Balay {
3008dfbe8321SBarry Smith   PetscErrorCode ierr;
30093c7409f5SSatish Balay 
30103a40ed3dSBarry Smith   PetscFunctionBegin;
30110700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3012639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
30133a40ed3dSBarry Smith   PetscFunctionReturn(0);
30143c7409f5SSatish Balay }
30153c7409f5SSatish Balay 
3016b2002411SLois Curfman McInnes 
30174a2ae208SSatish Balay #undef __FUNCT__
30184a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
30193cea93caSBarry Smith /*@C
30203cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
30213cea93caSBarry Smith 
30227f6c08e0SMatthew Knepley   Level: advanced
30233cea93caSBarry Smith @*/
30247087cfbeSBarry Smith PetscErrorCode  SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
3025b2002411SLois Curfman McInnes {
3026e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
3027dfbe8321SBarry Smith   PetscErrorCode ierr;
3028b2002411SLois Curfman McInnes 
3029b2002411SLois Curfman McInnes   PetscFunctionBegin;
3030b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
3031c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
3032b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
3033b2002411SLois Curfman McInnes }
3034da9b6338SBarry Smith 
3035da9b6338SBarry Smith #undef __FUNCT__
3036da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
30377087cfbeSBarry Smith PetscErrorCode  SNESTestLocalMin(SNES snes)
3038da9b6338SBarry Smith {
3039dfbe8321SBarry Smith   PetscErrorCode ierr;
304077431f27SBarry Smith   PetscInt       N,i,j;
3041da9b6338SBarry Smith   Vec            u,uh,fh;
3042da9b6338SBarry Smith   PetscScalar    value;
3043da9b6338SBarry Smith   PetscReal      norm;
3044da9b6338SBarry Smith 
3045da9b6338SBarry Smith   PetscFunctionBegin;
3046da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
3047da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
3048da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
3049da9b6338SBarry Smith 
3050da9b6338SBarry Smith   /* currently only works for sequential */
3051da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
3052da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
3053da9b6338SBarry Smith   for (i=0; i<N; i++) {
3054da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
305577431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
3056da9b6338SBarry Smith     for (j=-10; j<11; j++) {
3057ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
3058da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
30593ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
3060da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
306177431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
3062da9b6338SBarry Smith       value = -value;
3063da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
3064da9b6338SBarry Smith     }
3065da9b6338SBarry Smith   }
30666bf464f9SBarry Smith   ierr = VecDestroy(&uh);CHKERRQ(ierr);
30676bf464f9SBarry Smith   ierr = VecDestroy(&fh);CHKERRQ(ierr);
3068da9b6338SBarry Smith   PetscFunctionReturn(0);
3069da9b6338SBarry Smith }
307071f87433Sdalcinl 
307171f87433Sdalcinl #undef __FUNCT__
3072fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
307371f87433Sdalcinl /*@
3074fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
307571f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
307671f87433Sdalcinl    Newton method.
307771f87433Sdalcinl 
30783f9fe445SBarry Smith    Logically Collective on SNES
307971f87433Sdalcinl 
308071f87433Sdalcinl    Input Parameters:
308171f87433Sdalcinl +  snes - SNES context
308271f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
308371f87433Sdalcinl 
308464ba62caSBarry Smith     Options Database:
308564ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
308664ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
308764ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
308864ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
308964ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
309064ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
309164ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
309264ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
309364ba62caSBarry Smith 
309471f87433Sdalcinl    Notes:
309571f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
309671f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
309771f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
309871f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
309971f87433Sdalcinl    solver.
310071f87433Sdalcinl 
310171f87433Sdalcinl    Level: advanced
310271f87433Sdalcinl 
310371f87433Sdalcinl    Reference:
310471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
310571f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
310671f87433Sdalcinl 
310771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
310871f87433Sdalcinl 
3109fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
311071f87433Sdalcinl @*/
31117087cfbeSBarry Smith PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool  flag)
311271f87433Sdalcinl {
311371f87433Sdalcinl   PetscFunctionBegin;
31140700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3115acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flag,2);
311671f87433Sdalcinl   snes->ksp_ewconv = flag;
311771f87433Sdalcinl   PetscFunctionReturn(0);
311871f87433Sdalcinl }
311971f87433Sdalcinl 
312071f87433Sdalcinl #undef __FUNCT__
3121fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
312271f87433Sdalcinl /*@
3123fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
312471f87433Sdalcinl    for computing relative tolerance for linear solvers within an
312571f87433Sdalcinl    inexact Newton method.
312671f87433Sdalcinl 
312771f87433Sdalcinl    Not Collective
312871f87433Sdalcinl 
312971f87433Sdalcinl    Input Parameter:
313071f87433Sdalcinl .  snes - SNES context
313171f87433Sdalcinl 
313271f87433Sdalcinl    Output Parameter:
313371f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
313471f87433Sdalcinl 
313571f87433Sdalcinl    Notes:
313671f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
313771f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
313871f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
313971f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
314071f87433Sdalcinl    solver.
314171f87433Sdalcinl 
314271f87433Sdalcinl    Level: advanced
314371f87433Sdalcinl 
314471f87433Sdalcinl    Reference:
314571f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
314671f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
314771f87433Sdalcinl 
314871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
314971f87433Sdalcinl 
3150fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
315171f87433Sdalcinl @*/
31527087cfbeSBarry Smith PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
315371f87433Sdalcinl {
315471f87433Sdalcinl   PetscFunctionBegin;
31550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
315671f87433Sdalcinl   PetscValidPointer(flag,2);
315771f87433Sdalcinl   *flag = snes->ksp_ewconv;
315871f87433Sdalcinl   PetscFunctionReturn(0);
315971f87433Sdalcinl }
316071f87433Sdalcinl 
316171f87433Sdalcinl #undef __FUNCT__
3162fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
316371f87433Sdalcinl /*@
3164fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
316571f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
316671f87433Sdalcinl    Newton method.
316771f87433Sdalcinl 
31683f9fe445SBarry Smith    Logically Collective on SNES
316971f87433Sdalcinl 
317071f87433Sdalcinl    Input Parameters:
317171f87433Sdalcinl +    snes - SNES context
317271f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
317371f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
317471f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
317571f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
317671f87433Sdalcinl              (0 <= gamma2 <= 1)
317771f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
317871f87433Sdalcinl .    alpha2 - power for safeguard
317971f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
318071f87433Sdalcinl 
318171f87433Sdalcinl    Note:
318271f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
318371f87433Sdalcinl 
318471f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
318571f87433Sdalcinl 
318671f87433Sdalcinl    Level: advanced
318771f87433Sdalcinl 
318871f87433Sdalcinl    Reference:
318971f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
319071f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
319171f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
319271f87433Sdalcinl 
319371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
319471f87433Sdalcinl 
3195fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
319671f87433Sdalcinl @*/
31977087cfbeSBarry Smith PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
319871f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
319971f87433Sdalcinl {
3200fa9f3622SBarry Smith   SNESKSPEW *kctx;
320171f87433Sdalcinl   PetscFunctionBegin;
32020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3203fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3204e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
3205c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
3206c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
3207c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
3208c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
3209c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
3210c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
3211c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
321271f87433Sdalcinl 
321371f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
321471f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
321571f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
321671f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
321771f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
321871f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
321971f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
322071f87433Sdalcinl 
322171f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
3222e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
322371f87433Sdalcinl   }
322471f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
3225e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
322671f87433Sdalcinl   }
322771f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
3228e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
322971f87433Sdalcinl   }
323071f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
3231e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
323271f87433Sdalcinl   }
323371f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
3234e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
323571f87433Sdalcinl   }
323671f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
3237e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
323871f87433Sdalcinl   }
323971f87433Sdalcinl   PetscFunctionReturn(0);
324071f87433Sdalcinl }
324171f87433Sdalcinl 
324271f87433Sdalcinl #undef __FUNCT__
3243fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
324471f87433Sdalcinl /*@
3245fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
324671f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
324771f87433Sdalcinl    Newton method.
324871f87433Sdalcinl 
324971f87433Sdalcinl    Not Collective
325071f87433Sdalcinl 
325171f87433Sdalcinl    Input Parameters:
325271f87433Sdalcinl      snes - SNES context
325371f87433Sdalcinl 
325471f87433Sdalcinl    Output Parameters:
325571f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
325671f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
325771f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
325871f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
325971f87433Sdalcinl              (0 <= gamma2 <= 1)
326071f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
326171f87433Sdalcinl .    alpha2 - power for safeguard
326271f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
326371f87433Sdalcinl 
326471f87433Sdalcinl    Level: advanced
326571f87433Sdalcinl 
326671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
326771f87433Sdalcinl 
3268fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
326971f87433Sdalcinl @*/
32707087cfbeSBarry Smith PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
327171f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
327271f87433Sdalcinl {
3273fa9f3622SBarry Smith   SNESKSPEW *kctx;
327471f87433Sdalcinl   PetscFunctionBegin;
32750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3276fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3277e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
327871f87433Sdalcinl   if(version)   *version   = kctx->version;
327971f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
328071f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
328171f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
328271f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
328371f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
328471f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
328571f87433Sdalcinl   PetscFunctionReturn(0);
328671f87433Sdalcinl }
328771f87433Sdalcinl 
328871f87433Sdalcinl #undef __FUNCT__
3289fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
3290fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
329171f87433Sdalcinl {
329271f87433Sdalcinl   PetscErrorCode ierr;
3293fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
329471f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
329571f87433Sdalcinl 
329671f87433Sdalcinl   PetscFunctionBegin;
3297e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
329871f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
329971f87433Sdalcinl     rtol = kctx->rtol_0;
330071f87433Sdalcinl   } else {
330171f87433Sdalcinl     if (kctx->version == 1) {
330271f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
330371f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
330471f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
330571f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
330671f87433Sdalcinl     } else if (kctx->version == 2) {
330771f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
330871f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
330971f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
331071f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
331171f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
331271f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
331371f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
331471f87433Sdalcinl       stol = PetscMax(rtol,stol);
331571f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
331671f87433Sdalcinl       /* safeguard: avoid oversolving */
331771f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
331871f87433Sdalcinl       stol = PetscMax(rtol,stol);
331971f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
3320e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
332171f87433Sdalcinl   }
332271f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
332371f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
332471f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
332571f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
332671f87433Sdalcinl   PetscFunctionReturn(0);
332771f87433Sdalcinl }
332871f87433Sdalcinl 
332971f87433Sdalcinl #undef __FUNCT__
3330fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
3331fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
333271f87433Sdalcinl {
333371f87433Sdalcinl   PetscErrorCode ierr;
3334fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
333571f87433Sdalcinl   PCSide         pcside;
333671f87433Sdalcinl   Vec            lres;
333771f87433Sdalcinl 
333871f87433Sdalcinl   PetscFunctionBegin;
3339e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
334071f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
334171f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
334271f87433Sdalcinl   if (kctx->version == 1) {
3343b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
334471f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
334571f87433Sdalcinl       /* KSP residual is true linear residual */
334671f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
334771f87433Sdalcinl     } else {
334871f87433Sdalcinl       /* KSP residual is preconditioned residual */
334971f87433Sdalcinl       /* compute true linear residual norm */
335071f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
335171f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
335271f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
335371f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
33546bf464f9SBarry Smith       ierr = VecDestroy(&lres);CHKERRQ(ierr);
335571f87433Sdalcinl     }
335671f87433Sdalcinl   }
335771f87433Sdalcinl   PetscFunctionReturn(0);
335871f87433Sdalcinl }
335971f87433Sdalcinl 
336071f87433Sdalcinl #undef __FUNCT__
336171f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
336271f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
336371f87433Sdalcinl {
336471f87433Sdalcinl   PetscErrorCode ierr;
336571f87433Sdalcinl 
336671f87433Sdalcinl   PetscFunctionBegin;
3367fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
336871f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3369fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
337071f87433Sdalcinl   PetscFunctionReturn(0);
337171f87433Sdalcinl }
33726c699258SBarry Smith 
33736c699258SBarry Smith #undef __FUNCT__
33746c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
33756c699258SBarry Smith /*@
33766c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
33776c699258SBarry Smith 
33783f9fe445SBarry Smith    Logically Collective on SNES
33796c699258SBarry Smith 
33806c699258SBarry Smith    Input Parameters:
33816c699258SBarry Smith +  snes - the preconditioner context
33826c699258SBarry Smith -  dm - the dm
33836c699258SBarry Smith 
33846c699258SBarry Smith    Level: intermediate
33856c699258SBarry Smith 
33866c699258SBarry Smith 
33876c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
33886c699258SBarry Smith @*/
33897087cfbeSBarry Smith PetscErrorCode  SNESSetDM(SNES snes,DM dm)
33906c699258SBarry Smith {
33916c699258SBarry Smith   PetscErrorCode ierr;
3392345fed2cSBarry Smith   KSP            ksp;
33936c699258SBarry Smith 
33946c699258SBarry Smith   PetscFunctionBegin;
33950700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3396d0660788SBarry Smith   if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);}
33976bf464f9SBarry Smith   ierr = DMDestroy(&snes->dm);CHKERRQ(ierr);
33986c699258SBarry Smith   snes->dm = dm;
3399345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3400345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3401f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
34026c699258SBarry Smith   PetscFunctionReturn(0);
34036c699258SBarry Smith }
34046c699258SBarry Smith 
34056c699258SBarry Smith #undef __FUNCT__
34066c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
34076c699258SBarry Smith /*@
34086c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
34096c699258SBarry Smith 
34103f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
34116c699258SBarry Smith 
34126c699258SBarry Smith    Input Parameter:
34136c699258SBarry Smith . snes - the preconditioner context
34146c699258SBarry Smith 
34156c699258SBarry Smith    Output Parameter:
34166c699258SBarry Smith .  dm - the dm
34176c699258SBarry Smith 
34186c699258SBarry Smith    Level: intermediate
34196c699258SBarry Smith 
34206c699258SBarry Smith 
34216c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
34226c699258SBarry Smith @*/
34237087cfbeSBarry Smith PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
34246c699258SBarry Smith {
34256c699258SBarry Smith   PetscFunctionBegin;
34260700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34276c699258SBarry Smith   *dm = snes->dm;
34286c699258SBarry Smith   PetscFunctionReturn(0);
34296c699258SBarry Smith }
34300807856dSBarry Smith 
343131823bd8SMatthew G Knepley #undef __FUNCT__
343231823bd8SMatthew G Knepley #define __FUNCT__ "SNESSetPC"
343331823bd8SMatthew G Knepley /*@
343431823bd8SMatthew G Knepley   SNESSetPC - Sets the preconditioner to be used.
343531823bd8SMatthew G Knepley 
343631823bd8SMatthew G Knepley   Collective on SNES
343731823bd8SMatthew G Knepley 
343831823bd8SMatthew G Knepley   Input Parameters:
343931823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate()
344031823bd8SMatthew G Knepley - pc   - the preconditioner object
344131823bd8SMatthew G Knepley 
344231823bd8SMatthew G Knepley   Notes:
344331823bd8SMatthew G Knepley   Use SNESGetPC() to retrieve the preconditioner context (for example,
344431823bd8SMatthew G Knepley   to configure it using the API).
344531823bd8SMatthew G Knepley 
344631823bd8SMatthew G Knepley   Level: developer
344731823bd8SMatthew G Knepley 
344831823bd8SMatthew G Knepley .keywords: SNES, set, precondition
344931823bd8SMatthew G Knepley .seealso: SNESGetPC()
345031823bd8SMatthew G Knepley @*/
345131823bd8SMatthew G Knepley PetscErrorCode SNESSetPC(SNES snes, SNES pc)
345231823bd8SMatthew G Knepley {
345331823bd8SMatthew G Knepley   PetscErrorCode ierr;
345431823bd8SMatthew G Knepley 
345531823bd8SMatthew G Knepley   PetscFunctionBegin;
345631823bd8SMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
345731823bd8SMatthew G Knepley   PetscValidHeaderSpecific(pc, SNES_CLASSID, 2);
345831823bd8SMatthew G Knepley   PetscCheckSameComm(snes, 1, pc, 2);
345931823bd8SMatthew G Knepley   ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr);
3460bf11d3b6SBarry Smith   ierr = SNESDestroy(&snes->pc);CHKERRQ(ierr);
346131823bd8SMatthew G Knepley   snes->pc = pc;
346231823bd8SMatthew G Knepley   ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr);
346331823bd8SMatthew G Knepley   PetscFunctionReturn(0);
346431823bd8SMatthew G Knepley }
346531823bd8SMatthew G Knepley 
346631823bd8SMatthew G Knepley #undef __FUNCT__
346731823bd8SMatthew G Knepley #define __FUNCT__ "SNESGetPC"
346831823bd8SMatthew G Knepley /*@
346931823bd8SMatthew G Knepley   SNESGetPC - Returns a pointer to the preconditioner context set with SNESSetPC().
347031823bd8SMatthew G Knepley 
347131823bd8SMatthew G Knepley   Not Collective
347231823bd8SMatthew G Knepley 
347331823bd8SMatthew G Knepley   Input Parameter:
347431823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate()
347531823bd8SMatthew G Knepley 
347631823bd8SMatthew G Knepley   Output Parameter:
347731823bd8SMatthew G Knepley . pc - preconditioner context
347831823bd8SMatthew G Knepley 
347931823bd8SMatthew G Knepley   Level: developer
348031823bd8SMatthew G Knepley 
348131823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner
348231823bd8SMatthew G Knepley .seealso: SNESSetPC()
348331823bd8SMatthew G Knepley @*/
348431823bd8SMatthew G Knepley PetscErrorCode SNESGetPC(SNES snes, SNES *pc)
348531823bd8SMatthew G Knepley {
348631823bd8SMatthew G Knepley   PetscErrorCode ierr;
348731823bd8SMatthew G Knepley 
348831823bd8SMatthew G Knepley   PetscFunctionBegin;
348931823bd8SMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
349031823bd8SMatthew G Knepley   PetscValidPointer(pc, 2);
349131823bd8SMatthew G Knepley   if (!snes->pc) {
349231823bd8SMatthew G Knepley     ierr = SNESCreate(((PetscObject) snes)->comm, &snes->pc);CHKERRQ(ierr);
34934a0c5b0cSMatthew G Knepley     ierr = PetscObjectIncrementTabLevel((PetscObject) snes->pc, (PetscObject) snes, 1);CHKERRQ(ierr);
349431823bd8SMatthew G Knepley     ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr);
349531823bd8SMatthew G Knepley   }
349631823bd8SMatthew G Knepley   *pc = snes->pc;
349731823bd8SMatthew G Knepley   PetscFunctionReturn(0);
349831823bd8SMatthew G Knepley }
349931823bd8SMatthew G Knepley 
350069b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3501c6db04a5SJed Brown #include <mex.h>
350269b4f73cSBarry Smith 
35038f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
35048f6e6473SBarry Smith 
35050807856dSBarry Smith #undef __FUNCT__
35060807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab"
35070807856dSBarry Smith /*
35080807856dSBarry Smith    SNESComputeFunction_Matlab - Calls the function that has been set with
35090807856dSBarry Smith                          SNESSetFunctionMatlab().
35100807856dSBarry Smith 
35110807856dSBarry Smith    Collective on SNES
35120807856dSBarry Smith 
35130807856dSBarry Smith    Input Parameters:
35140807856dSBarry Smith +  snes - the SNES context
35150807856dSBarry Smith -  x - input vector
35160807856dSBarry Smith 
35170807856dSBarry Smith    Output Parameter:
35180807856dSBarry Smith .  y - function vector, as set by SNESSetFunction()
35190807856dSBarry Smith 
35200807856dSBarry Smith    Notes:
35210807856dSBarry Smith    SNESComputeFunction() is typically used within nonlinear solvers
35220807856dSBarry Smith    implementations, so most users would not generally call this routine
35230807856dSBarry Smith    themselves.
35240807856dSBarry Smith 
35250807856dSBarry Smith    Level: developer
35260807856dSBarry Smith 
35270807856dSBarry Smith .keywords: SNES, nonlinear, compute, function
35280807856dSBarry Smith 
35290807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
353061b2408cSBarry Smith */
35317087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx)
35320807856dSBarry Smith {
3533e650e774SBarry Smith   PetscErrorCode    ierr;
35348f6e6473SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
35358f6e6473SBarry Smith   int               nlhs = 1,nrhs = 5;
35368f6e6473SBarry Smith   mxArray	    *plhs[1],*prhs[5];
353791621f2eSBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
3538e650e774SBarry Smith 
35390807856dSBarry Smith   PetscFunctionBegin;
35400807856dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
35410807856dSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
35420807856dSBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
35430807856dSBarry Smith   PetscCheckSameComm(snes,1,x,2);
35440807856dSBarry Smith   PetscCheckSameComm(snes,1,y,3);
35450807856dSBarry Smith 
35460807856dSBarry Smith   /* call Matlab function in ctx with arguments x and y */
3547e650e774SBarry Smith 
354891621f2eSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3549e650e774SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3550e650e774SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
355191621f2eSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
355291621f2eSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
355391621f2eSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
35548f6e6473SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
35558f6e6473SBarry Smith   prhs[4] =  sctx->ctx;
3556b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr);
3557e650e774SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3558e650e774SBarry Smith   mxDestroyArray(prhs[0]);
3559e650e774SBarry Smith   mxDestroyArray(prhs[1]);
3560e650e774SBarry Smith   mxDestroyArray(prhs[2]);
35618f6e6473SBarry Smith   mxDestroyArray(prhs[3]);
3562e650e774SBarry Smith   mxDestroyArray(plhs[0]);
35630807856dSBarry Smith   PetscFunctionReturn(0);
35640807856dSBarry Smith }
35650807856dSBarry Smith 
35660807856dSBarry Smith 
35670807856dSBarry Smith #undef __FUNCT__
35680807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab"
356961b2408cSBarry Smith /*
35700807856dSBarry Smith    SNESSetFunctionMatlab - Sets the function evaluation routine and function
35710807856dSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3572e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
35730807856dSBarry Smith 
35740807856dSBarry Smith    Logically Collective on SNES
35750807856dSBarry Smith 
35760807856dSBarry Smith    Input Parameters:
35770807856dSBarry Smith +  snes - the SNES context
35780807856dSBarry Smith .  r - vector to store function value
35790807856dSBarry Smith -  func - function evaluation routine
35800807856dSBarry Smith 
35810807856dSBarry Smith    Calling sequence of func:
358261b2408cSBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
35830807856dSBarry Smith 
35840807856dSBarry Smith 
35850807856dSBarry Smith    Notes:
35860807856dSBarry Smith    The Newton-like methods typically solve linear systems of the form
35870807856dSBarry Smith $      f'(x) x = -f(x),
35880807856dSBarry Smith    where f'(x) denotes the Jacobian matrix and f(x) is the function.
35890807856dSBarry Smith 
35900807856dSBarry Smith    Level: beginner
35910807856dSBarry Smith 
35920807856dSBarry Smith .keywords: SNES, nonlinear, set, function
35930807856dSBarry Smith 
35940807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
359561b2408cSBarry Smith */
35967087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx)
35970807856dSBarry Smith {
35980807856dSBarry Smith   PetscErrorCode    ierr;
35998f6e6473SBarry Smith   SNESMatlabContext *sctx;
36000807856dSBarry Smith 
36010807856dSBarry Smith   PetscFunctionBegin;
36028f6e6473SBarry Smith   /* currently sctx is memory bleed */
36038f6e6473SBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
36048f6e6473SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
36058f6e6473SBarry Smith   /*
36068f6e6473SBarry Smith      This should work, but it doesn't
36078f6e6473SBarry Smith   sctx->ctx = ctx;
36088f6e6473SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
36098f6e6473SBarry Smith   */
36108f6e6473SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
36118f6e6473SBarry Smith   ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr);
36120807856dSBarry Smith   PetscFunctionReturn(0);
36130807856dSBarry Smith }
361469b4f73cSBarry Smith 
361561b2408cSBarry Smith #undef __FUNCT__
361661b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab"
361761b2408cSBarry Smith /*
361861b2408cSBarry Smith    SNESComputeJacobian_Matlab - Calls the function that has been set with
361961b2408cSBarry Smith                          SNESSetJacobianMatlab().
362061b2408cSBarry Smith 
362161b2408cSBarry Smith    Collective on SNES
362261b2408cSBarry Smith 
362361b2408cSBarry Smith    Input Parameters:
362461b2408cSBarry Smith +  snes - the SNES context
362561b2408cSBarry Smith .  x - input vector
362661b2408cSBarry Smith .  A, B - the matrices
362761b2408cSBarry Smith -  ctx - user context
362861b2408cSBarry Smith 
362961b2408cSBarry Smith    Output Parameter:
363061b2408cSBarry Smith .  flag - structure of the matrix
363161b2408cSBarry Smith 
363261b2408cSBarry Smith    Level: developer
363361b2408cSBarry Smith 
363461b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function
363561b2408cSBarry Smith 
363661b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
363761b2408cSBarry Smith @*/
36387087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx)
363961b2408cSBarry Smith {
364061b2408cSBarry Smith   PetscErrorCode    ierr;
364161b2408cSBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
364261b2408cSBarry Smith   int               nlhs = 2,nrhs = 6;
364361b2408cSBarry Smith   mxArray	    *plhs[2],*prhs[6];
364461b2408cSBarry Smith   long long int     lx = 0,lA = 0,ls = 0, lB = 0;
364561b2408cSBarry Smith 
364661b2408cSBarry Smith   PetscFunctionBegin;
364761b2408cSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
364861b2408cSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
364961b2408cSBarry Smith 
365061b2408cSBarry Smith   /* call Matlab function in ctx with arguments x and y */
365161b2408cSBarry Smith 
365261b2408cSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
365361b2408cSBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
365461b2408cSBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
365561b2408cSBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
365661b2408cSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
365761b2408cSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
365861b2408cSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
365961b2408cSBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
366061b2408cSBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
366161b2408cSBarry Smith   prhs[5] =  sctx->ctx;
3662b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr);
366361b2408cSBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
366461b2408cSBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
366561b2408cSBarry Smith   mxDestroyArray(prhs[0]);
366661b2408cSBarry Smith   mxDestroyArray(prhs[1]);
366761b2408cSBarry Smith   mxDestroyArray(prhs[2]);
366861b2408cSBarry Smith   mxDestroyArray(prhs[3]);
366961b2408cSBarry Smith   mxDestroyArray(prhs[4]);
367061b2408cSBarry Smith   mxDestroyArray(plhs[0]);
367161b2408cSBarry Smith   mxDestroyArray(plhs[1]);
367261b2408cSBarry Smith   PetscFunctionReturn(0);
367361b2408cSBarry Smith }
367461b2408cSBarry Smith 
367561b2408cSBarry Smith 
367661b2408cSBarry Smith #undef __FUNCT__
367761b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab"
367861b2408cSBarry Smith /*
367961b2408cSBarry Smith    SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
368061b2408cSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3681e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
368261b2408cSBarry Smith 
368361b2408cSBarry Smith    Logically Collective on SNES
368461b2408cSBarry Smith 
368561b2408cSBarry Smith    Input Parameters:
368661b2408cSBarry Smith +  snes - the SNES context
368761b2408cSBarry Smith .  A,B - Jacobian matrices
368861b2408cSBarry Smith .  func - function evaluation routine
368961b2408cSBarry Smith -  ctx - user context
369061b2408cSBarry Smith 
369161b2408cSBarry Smith    Calling sequence of func:
369261b2408cSBarry Smith $    flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx);
369361b2408cSBarry Smith 
369461b2408cSBarry Smith 
369561b2408cSBarry Smith    Level: developer
369661b2408cSBarry Smith 
369761b2408cSBarry Smith .keywords: SNES, nonlinear, set, function
369861b2408cSBarry Smith 
369961b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
370061b2408cSBarry Smith */
37017087cfbeSBarry Smith PetscErrorCode  SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx)
370261b2408cSBarry Smith {
370361b2408cSBarry Smith   PetscErrorCode    ierr;
370461b2408cSBarry Smith   SNESMatlabContext *sctx;
370561b2408cSBarry Smith 
370661b2408cSBarry Smith   PetscFunctionBegin;
370761b2408cSBarry Smith   /* currently sctx is memory bleed */
370861b2408cSBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
370961b2408cSBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
371061b2408cSBarry Smith   /*
371161b2408cSBarry Smith      This should work, but it doesn't
371261b2408cSBarry Smith   sctx->ctx = ctx;
371361b2408cSBarry Smith   mexMakeArrayPersistent(sctx->ctx);
371461b2408cSBarry Smith   */
371561b2408cSBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
371661b2408cSBarry Smith   ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
371761b2408cSBarry Smith   PetscFunctionReturn(0);
371861b2408cSBarry Smith }
371969b4f73cSBarry Smith 
3720f9eb7ae2SShri Abhyankar #undef __FUNCT__
3721f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab"
3722f9eb7ae2SShri Abhyankar /*
3723f9eb7ae2SShri Abhyankar    SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab().
3724f9eb7ae2SShri Abhyankar 
3725f9eb7ae2SShri Abhyankar    Collective on SNES
3726f9eb7ae2SShri Abhyankar 
3727f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction()
3728f9eb7ae2SShri Abhyankar @*/
37297087cfbeSBarry Smith PetscErrorCode  SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx)
3730f9eb7ae2SShri Abhyankar {
3731f9eb7ae2SShri Abhyankar   PetscErrorCode  ierr;
373248f37e70SShri Abhyankar   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
3733f9eb7ae2SShri Abhyankar   int             nlhs = 1,nrhs = 6;
3734f9eb7ae2SShri Abhyankar   mxArray	  *plhs[1],*prhs[6];
3735f9eb7ae2SShri Abhyankar   long long int   lx = 0,ls = 0;
3736f9eb7ae2SShri Abhyankar   Vec             x=snes->vec_sol;
3737f9eb7ae2SShri Abhyankar 
3738f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3739f9eb7ae2SShri Abhyankar   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3740f9eb7ae2SShri Abhyankar 
3741f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3742f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3743f9eb7ae2SShri Abhyankar   prhs[0] =  mxCreateDoubleScalar((double)ls);
3744f9eb7ae2SShri Abhyankar   prhs[1] =  mxCreateDoubleScalar((double)it);
3745f9eb7ae2SShri Abhyankar   prhs[2] =  mxCreateDoubleScalar((double)fnorm);
3746f9eb7ae2SShri Abhyankar   prhs[3] =  mxCreateDoubleScalar((double)lx);
3747f9eb7ae2SShri Abhyankar   prhs[4] =  mxCreateString(sctx->funcname);
3748f9eb7ae2SShri Abhyankar   prhs[5] =  sctx->ctx;
3749f9eb7ae2SShri Abhyankar   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr);
3750f9eb7ae2SShri Abhyankar   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3751f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[0]);
3752f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[1]);
3753f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[2]);
3754f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[3]);
3755f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[4]);
3756f9eb7ae2SShri Abhyankar   mxDestroyArray(plhs[0]);
3757f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3758f9eb7ae2SShri Abhyankar }
3759f9eb7ae2SShri Abhyankar 
3760f9eb7ae2SShri Abhyankar 
3761f9eb7ae2SShri Abhyankar #undef __FUNCT__
3762f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab"
3763f9eb7ae2SShri Abhyankar /*
3764e3c5b3baSBarry Smith    SNESMonitorSetMatlab - Sets the monitor function from MATLAB
3765f9eb7ae2SShri Abhyankar 
3766f9eb7ae2SShri Abhyankar    Level: developer
3767f9eb7ae2SShri Abhyankar 
3768f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function
3769f9eb7ae2SShri Abhyankar 
3770f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
3771f9eb7ae2SShri Abhyankar */
37727087cfbeSBarry Smith PetscErrorCode  SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx)
3773f9eb7ae2SShri Abhyankar {
3774f9eb7ae2SShri Abhyankar   PetscErrorCode    ierr;
3775f9eb7ae2SShri Abhyankar   SNESMatlabContext *sctx;
3776f9eb7ae2SShri Abhyankar 
3777f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3778f9eb7ae2SShri Abhyankar   /* currently sctx is memory bleed */
3779f9eb7ae2SShri Abhyankar   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
3780f9eb7ae2SShri Abhyankar   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3781f9eb7ae2SShri Abhyankar   /*
3782f9eb7ae2SShri Abhyankar      This should work, but it doesn't
3783f9eb7ae2SShri Abhyankar   sctx->ctx = ctx;
3784f9eb7ae2SShri Abhyankar   mexMakeArrayPersistent(sctx->ctx);
3785f9eb7ae2SShri Abhyankar   */
3786f9eb7ae2SShri Abhyankar   sctx->ctx = mxDuplicateArray(ctx);
3787f9eb7ae2SShri Abhyankar   ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3788f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3789f9eb7ae2SShri Abhyankar }
3790f9eb7ae2SShri Abhyankar 
379169b4f73cSBarry Smith #endif
3792