xref: /petsc/src/snes/interface/snes.c (revision 51e86f29c3fd516e9ae7246283f19bf1c4390cdb)
19b94acceSBarry Smith 
2c6db04a5SJed Brown #include <private/snesimpl.h>      /*I "petscsnes.h"  I*/
39b94acceSBarry Smith 
4ace3abfcSBarry Smith PetscBool  SNESRegisterAllCalled = PETSC_FALSE;
58ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
68ba1e511SMatthew Knepley 
78ba1e511SMatthew Knepley /* Logging support */
87087cfbeSBarry Smith PetscClassId  SNES_CLASSID;
9166c7f25SBarry Smith PetscLogEvent  SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval;
10a09944afSBarry Smith 
11a09944afSBarry Smith #undef __FUNCT__
12cab2e9ccSBarry Smith #define __FUNCT__ "SNESDMComputeJacobian"
13cab2e9ccSBarry Smith /*
14cab2e9ccSBarry Smith     Translates from a SNES call to a DM call in computing a Jacobian
15cab2e9ccSBarry Smith */
16cab2e9ccSBarry Smith PetscErrorCode SNESDMComputeJacobian(SNES snes,Vec X,Mat *J,Mat *B,MatStructure *flag,void *ptr)
17cab2e9ccSBarry Smith {
18cab2e9ccSBarry Smith   PetscErrorCode ierr;
19cab2e9ccSBarry Smith   DM             dm;
20cab2e9ccSBarry Smith 
21cab2e9ccSBarry Smith   PetscFunctionBegin;
22cab2e9ccSBarry Smith   ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr);
23cab2e9ccSBarry Smith   ierr = DMComputeJacobian(dm,X,*J,*B,flag);CHKERRQ(ierr);
24cab2e9ccSBarry Smith   PetscFunctionReturn(0);
25cab2e9ccSBarry Smith }
26cab2e9ccSBarry Smith 
27cab2e9ccSBarry Smith #undef __FUNCT__
28e113a28aSBarry Smith #define __FUNCT__ "SNESSetErrorIfNotConverged"
29e113a28aSBarry Smith /*@
30e113a28aSBarry Smith    SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged.
31e113a28aSBarry Smith 
323f9fe445SBarry Smith    Logically Collective on SNES
33e113a28aSBarry Smith 
34e113a28aSBarry Smith    Input Parameters:
35e113a28aSBarry Smith +  snes - iterative context obtained from SNESCreate()
36e113a28aSBarry Smith -  flg - PETSC_TRUE indicates you want the error generated
37e113a28aSBarry Smith 
38e113a28aSBarry Smith    Options database keys:
39e113a28aSBarry Smith .  -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false)
40e113a28aSBarry Smith 
41e113a28aSBarry Smith    Level: intermediate
42e113a28aSBarry Smith 
43e113a28aSBarry Smith    Notes:
44e113a28aSBarry Smith     Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve()
45e113a28aSBarry Smith     to determine if it has converged.
46e113a28aSBarry Smith 
47e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
48e113a28aSBarry Smith 
49e113a28aSBarry Smith .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
50e113a28aSBarry Smith @*/
517087cfbeSBarry Smith PetscErrorCode  SNESSetErrorIfNotConverged(SNES snes,PetscBool  flg)
52e113a28aSBarry Smith {
53e113a28aSBarry Smith   PetscFunctionBegin;
54e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
55acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flg,2);
56e113a28aSBarry Smith   snes->errorifnotconverged = flg;
57e113a28aSBarry Smith   PetscFunctionReturn(0);
58e113a28aSBarry Smith }
59e113a28aSBarry Smith 
60e113a28aSBarry Smith #undef __FUNCT__
61e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged"
62e113a28aSBarry Smith /*@
63e113a28aSBarry Smith    SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge?
64e113a28aSBarry Smith 
65e113a28aSBarry Smith    Not Collective
66e113a28aSBarry Smith 
67e113a28aSBarry Smith    Input Parameter:
68e113a28aSBarry Smith .  snes - iterative context obtained from SNESCreate()
69e113a28aSBarry Smith 
70e113a28aSBarry Smith    Output Parameter:
71e113a28aSBarry Smith .  flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE
72e113a28aSBarry Smith 
73e113a28aSBarry Smith    Level: intermediate
74e113a28aSBarry Smith 
75e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
76e113a28aSBarry Smith 
77e113a28aSBarry Smith .seealso:  SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
78e113a28aSBarry Smith @*/
797087cfbeSBarry Smith PetscErrorCode  SNESGetErrorIfNotConverged(SNES snes,PetscBool  *flag)
80e113a28aSBarry Smith {
81e113a28aSBarry Smith   PetscFunctionBegin;
82e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
83e113a28aSBarry Smith   PetscValidPointer(flag,2);
84e113a28aSBarry Smith   *flag = snes->errorifnotconverged;
85e113a28aSBarry Smith   PetscFunctionReturn(0);
86e113a28aSBarry Smith }
87e113a28aSBarry Smith 
88e113a28aSBarry Smith #undef __FUNCT__
894936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
90e725d27bSBarry Smith /*@
914936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
924936397dSBarry Smith      in the functions domain. For example, negative pressure.
934936397dSBarry Smith 
943f9fe445SBarry Smith    Logically Collective on SNES
954936397dSBarry Smith 
964936397dSBarry Smith    Input Parameters:
974936397dSBarry Smith .  SNES - the SNES context
984936397dSBarry Smith 
9928529972SSatish Balay    Level: advanced
1004936397dSBarry Smith 
1014936397dSBarry Smith .keywords: SNES, view
1024936397dSBarry Smith 
1034936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
1044936397dSBarry Smith @*/
1057087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionDomainError(SNES snes)
1064936397dSBarry Smith {
1074936397dSBarry Smith   PetscFunctionBegin;
1080700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1094936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
1104936397dSBarry Smith   PetscFunctionReturn(0);
1114936397dSBarry Smith }
1124936397dSBarry Smith 
1134936397dSBarry Smith #undef __FUNCT__
1144a2ae208SSatish Balay #define __FUNCT__ "SNESView"
1157e2c5f70SBarry Smith /*@C
1169b94acceSBarry Smith    SNESView - Prints the SNES data structure.
1179b94acceSBarry Smith 
1184c49b128SBarry Smith    Collective on SNES
119fee21e36SBarry Smith 
120c7afd0dbSLois Curfman McInnes    Input Parameters:
121c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
122c7afd0dbSLois Curfman McInnes -  viewer - visualization context
123c7afd0dbSLois Curfman McInnes 
1249b94acceSBarry Smith    Options Database Key:
125c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
1269b94acceSBarry Smith 
1279b94acceSBarry Smith    Notes:
1289b94acceSBarry Smith    The available visualization contexts include
129b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
130b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
131c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
132c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
133c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
1349b94acceSBarry Smith 
1353e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
136b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
1379b94acceSBarry Smith 
13836851e7fSLois Curfman McInnes    Level: beginner
13936851e7fSLois Curfman McInnes 
1409b94acceSBarry Smith .keywords: SNES, view
1419b94acceSBarry Smith 
142b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1439b94acceSBarry Smith @*/
1447087cfbeSBarry Smith PetscErrorCode  SNESView(SNES snes,PetscViewer viewer)
1459b94acceSBarry Smith {
146fa9f3622SBarry Smith   SNESKSPEW           *kctx;
147dfbe8321SBarry Smith   PetscErrorCode      ierr;
14894b7f48cSBarry Smith   KSP                 ksp;
149ace3abfcSBarry Smith   PetscBool           iascii,isstring;
1509b94acceSBarry Smith 
1513a40ed3dSBarry Smith   PetscFunctionBegin;
1520700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1533050cee2SBarry Smith   if (!viewer) {
1547adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
1553050cee2SBarry Smith   }
1560700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
157c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
15874679c65SBarry Smith 
1592692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1602692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
16132077d6dSBarry Smith   if (iascii) {
162317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer,"SNES Object");CHKERRQ(ierr);
163e7788613SBarry Smith     if (snes->ops->view) {
164b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
165e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
166b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1670ef38995SBarry Smith     }
16877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
169a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
17070441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
17177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
17277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1739b94acceSBarry Smith     if (snes->ksp_ewconv) {
174fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1759b94acceSBarry Smith       if (kctx) {
17677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
177a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
178a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1799b94acceSBarry Smith       }
1809b94acceSBarry Smith     }
181eb1f6c34SBarry Smith     if (snes->lagpreconditioner == -1) {
182eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is never rebuilt\n");CHKERRQ(ierr);
183eb1f6c34SBarry Smith     } else if (snes->lagpreconditioner > 1) {
184eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr);
185eb1f6c34SBarry Smith     }
186eb1f6c34SBarry Smith     if (snes->lagjacobian == -1) {
187eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is never rebuilt\n");CHKERRQ(ierr);
188eb1f6c34SBarry Smith     } else if (snes->lagjacobian > 1) {
18942f4f86dSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is rebuilt every %D SNES iterations\n",snes->lagjacobian);CHKERRQ(ierr);
190eb1f6c34SBarry Smith     }
1910f5bd95cSBarry Smith   } else if (isstring) {
192317d6ea6SBarry Smith     const char *type;
193454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
194b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
19519bcc07fSBarry Smith   }
19642f4f86dSBarry Smith   if (snes->pc && snes->usespc) {
1974a0c5b0cSMatthew G Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1984a0c5b0cSMatthew G Knepley     ierr = SNESView(snes->pc, viewer);CHKERRQ(ierr);
1994a0c5b0cSMatthew G Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2004a0c5b0cSMatthew G Knepley   }
2012c155ee1SBarry Smith   if (snes->usesksp) {
2022c155ee1SBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
203b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
20494b7f48cSBarry Smith     ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
205b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2062c155ee1SBarry Smith   }
2073a40ed3dSBarry Smith   PetscFunctionReturn(0);
2089b94acceSBarry Smith }
2099b94acceSBarry Smith 
21076b2cf59SMatthew Knepley /*
21176b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
21276b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
21376b2cf59SMatthew Knepley */
21476b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
215a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
2166849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
21776b2cf59SMatthew Knepley 
218e74ef692SMatthew Knepley #undef __FUNCT__
219e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
220ac226902SBarry Smith /*@C
22176b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
22276b2cf59SMatthew Knepley 
22376b2cf59SMatthew Knepley   Not Collective
22476b2cf59SMatthew Knepley 
22576b2cf59SMatthew Knepley   Input Parameter:
22676b2cf59SMatthew Knepley . snescheck - function that checks for options
22776b2cf59SMatthew Knepley 
22876b2cf59SMatthew Knepley   Level: developer
22976b2cf59SMatthew Knepley 
23076b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
23176b2cf59SMatthew Knepley @*/
2327087cfbeSBarry Smith PetscErrorCode  SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
23376b2cf59SMatthew Knepley {
23476b2cf59SMatthew Knepley   PetscFunctionBegin;
23576b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
236e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
23776b2cf59SMatthew Knepley   }
23876b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
23976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
24076b2cf59SMatthew Knepley }
24176b2cf59SMatthew Knepley 
2427087cfbeSBarry Smith extern PetscErrorCode  SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
243aa3661deSLisandro Dalcin 
244aa3661deSLisandro Dalcin #undef __FUNCT__
245aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private"
246ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool  hasOperator, PetscInt version)
247aa3661deSLisandro Dalcin {
248aa3661deSLisandro Dalcin   Mat            J;
249aa3661deSLisandro Dalcin   KSP            ksp;
250aa3661deSLisandro Dalcin   PC             pc;
251ace3abfcSBarry Smith   PetscBool      match;
252aa3661deSLisandro Dalcin   PetscErrorCode ierr;
253aa3661deSLisandro Dalcin 
254aa3661deSLisandro Dalcin   PetscFunctionBegin;
2550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
256aa3661deSLisandro Dalcin 
25798613b67SLisandro Dalcin   if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
25898613b67SLisandro Dalcin     Mat A = snes->jacobian, B = snes->jacobian_pre;
25998613b67SLisandro Dalcin     ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr);
26098613b67SLisandro Dalcin   }
26198613b67SLisandro Dalcin 
262aa3661deSLisandro Dalcin   if (version == 1) {
263aa3661deSLisandro Dalcin     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
26498613b67SLisandro Dalcin     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
2659c6ac3b3SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
266aa3661deSLisandro Dalcin   } else if (version == 2) {
267e32f2f54SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
26882a7e548SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
269aa3661deSLisandro Dalcin     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr);
270aa3661deSLisandro Dalcin #else
271e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
272aa3661deSLisandro Dalcin #endif
273a8248277SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2");
274aa3661deSLisandro Dalcin 
275aa3661deSLisandro Dalcin   ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr);
276d3462f78SMatthew Knepley   if (hasOperator) {
277aa3661deSLisandro Dalcin     /* This version replaces the user provided Jacobian matrix with a
278aa3661deSLisandro Dalcin        matrix-free version but still employs the user-provided preconditioner matrix. */
279aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
280aa3661deSLisandro Dalcin   } else {
281aa3661deSLisandro Dalcin     /* This version replaces both the user-provided Jacobian and the user-
282aa3661deSLisandro Dalcin        provided preconditioner matrix with the default matrix free version. */
283aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
284aa3661deSLisandro Dalcin     /* Force no preconditioner */
285aa3661deSLisandro Dalcin     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
286aa3661deSLisandro Dalcin     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
287aa3661deSLisandro Dalcin     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr);
288aa3661deSLisandro Dalcin     if (!match) {
289aa3661deSLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
290aa3661deSLisandro Dalcin       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
291aa3661deSLisandro Dalcin     }
292aa3661deSLisandro Dalcin   }
2936bf464f9SBarry Smith   ierr = MatDestroy(&J);CHKERRQ(ierr);
294aa3661deSLisandro Dalcin   PetscFunctionReturn(0);
295aa3661deSLisandro Dalcin }
296aa3661deSLisandro Dalcin 
2974a2ae208SSatish Balay #undef __FUNCT__
2984a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
2999b94acceSBarry Smith /*@
30094b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
3019b94acceSBarry Smith 
302c7afd0dbSLois Curfman McInnes    Collective on SNES
303c7afd0dbSLois Curfman McInnes 
3049b94acceSBarry Smith    Input Parameter:
3059b94acceSBarry Smith .  snes - the SNES context
3069b94acceSBarry Smith 
30736851e7fSLois Curfman McInnes    Options Database Keys:
3086831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
30982738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
31082738288SBarry Smith                 of the change in the solution between steps
31170441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
312b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
313b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
314b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
31550ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
316ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
317a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
318e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
319b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
3202492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
32182738288SBarry Smith                                solver; hence iterations will continue until max_it
3221fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
32382738288SBarry Smith                                of convergence test
324e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
325e8105e01SRichard Katz                                        filename given prints to stdout
326a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
327a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
328a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
329a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
330e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
3315968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
332fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
33382738288SBarry Smith 
33482738288SBarry Smith     Options Database for Eisenstat-Walker method:
335fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
3364b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
33736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
33836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
33936851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
34036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
34136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
34236851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
34382738288SBarry Smith 
34411ca99fdSLois Curfman McInnes    Notes:
34511ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
3460598bfebSBarry Smith    the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A>.
34783e2fdc7SBarry Smith 
34836851e7fSLois Curfman McInnes    Level: beginner
34936851e7fSLois Curfman McInnes 
3509b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
3519b94acceSBarry Smith 
35269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
3539b94acceSBarry Smith @*/
3547087cfbeSBarry Smith PetscErrorCode  SNESSetFromOptions(SNES snes)
3559b94acceSBarry Smith {
356*51e86f29SPeter Brune   PetscBool               flg,mf,mf_operator,pcset;
357efd51863SBarry Smith   PetscInt                i,indx,lag,mf_version,grids;
358aa3661deSLisandro Dalcin   MatStructure            matflag;
35985385478SLisandro Dalcin   const char              *deft = SNESLS;
36085385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
36185385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
362e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
363*51e86f29SPeter Brune   const char              *optionsprefix;
364649052a6SBarry Smith   PetscViewer             monviewer;
36585385478SLisandro Dalcin   PetscErrorCode          ierr;
3669b94acceSBarry Smith 
3673a40ed3dSBarry Smith   PetscFunctionBegin;
3680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
369ca161407SBarry Smith 
370186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
3713194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)snes);CHKERRQ(ierr);
3727adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
373b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
374d64ed03dSBarry Smith     if (flg) {
375186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
3767adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
377186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
378d64ed03dSBarry Smith     }
37990d69ab7SBarry Smith     /* not used here, but called so will go into help messaage */
380909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
38193c39befSBarry Smith 
38257034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
38357034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
384186905e3SBarry Smith 
38557034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
386b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
387b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
38850ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
389ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
390acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr);
39185385478SLisandro Dalcin 
392a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
393a8054027SBarry Smith     if (flg) {
394a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
395a8054027SBarry Smith     }
396e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
397e35cf81dSBarry Smith     if (flg) {
398e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
399e35cf81dSBarry Smith     }
400efd51863SBarry Smith     ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr);
401efd51863SBarry Smith     if (flg) {
402efd51863SBarry Smith       ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr);
403efd51863SBarry Smith     }
404a8054027SBarry Smith 
40585385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
40685385478SLisandro Dalcin     if (flg) {
40785385478SLisandro Dalcin       switch (indx) {
4087f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
4097f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
41085385478SLisandro Dalcin       }
41185385478SLisandro Dalcin     }
41285385478SLisandro Dalcin 
413acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
414186905e3SBarry Smith 
41585385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
41685385478SLisandro Dalcin 
417acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
418186905e3SBarry Smith 
419fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
420fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
421fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
422fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
423fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
424fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
425fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
426186905e3SBarry Smith 
42790d69ab7SBarry Smith     flg  = PETSC_FALSE;
428acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
429a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
430eabae89aSBarry Smith 
431a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
432e8105e01SRichard Katz     if (flg) {
433649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
434649052a6SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
435e8105e01SRichard Katz     }
436eabae89aSBarry Smith 
437b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
438b271bb04SBarry Smith     if (flg) {
439b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
440b271bb04SBarry Smith     }
441b271bb04SBarry Smith 
442a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
443eabae89aSBarry Smith     if (flg) {
444649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
445f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
446e8105e01SRichard Katz     }
447eabae89aSBarry Smith 
448a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
449eabae89aSBarry Smith     if (flg) {
450649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr);
451649052a6SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
452eabae89aSBarry Smith     }
453eabae89aSBarry Smith 
4545180491cSLisandro Dalcin     ierr = PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
4555180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)snes,monfilename);CHKERRQ(ierr);}
4565180491cSLisandro Dalcin 
45790d69ab7SBarry Smith     flg  = PETSC_FALSE;
458acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
459a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
46090d69ab7SBarry Smith     flg  = PETSC_FALSE;
461acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
462a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
46390d69ab7SBarry Smith     flg  = PETSC_FALSE;
464acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
465a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
46690d69ab7SBarry Smith     flg  = PETSC_FALSE;
467acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
468a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
46990d69ab7SBarry Smith     flg  = PETSC_FALSE;
470acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
471b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
472e24b481bSBarry Smith 
47390d69ab7SBarry Smith     flg  = PETSC_FALSE;
474acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
4754b27c08aSLois Curfman McInnes     if (flg) {
476186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
477ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
4789b94acceSBarry Smith     }
479639f9d9dSBarry Smith 
480aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
481aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
482acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
483a8248277SBarry Smith     if (flg && mf_operator) {
484a8248277SBarry Smith       snes->mf_operator = PETSC_TRUE;
485a8248277SBarry Smith       mf = PETSC_TRUE;
486a8248277SBarry Smith     }
487aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
488acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
489aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
490aa3661deSLisandro Dalcin     mf_version = 1;
491aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
492aa3661deSLisandro Dalcin 
49376b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
49476b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
49576b2cf59SMatthew Knepley     }
49676b2cf59SMatthew Knepley 
497e7788613SBarry Smith     if (snes->ops->setfromoptions) {
498e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
499639f9d9dSBarry Smith     }
5005d973c19SBarry Smith 
5015d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
5025d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr);
503b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
5044bbc92c1SBarry Smith 
505aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
5061cee3971SBarry Smith 
5071cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
508aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
509aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
51085385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
51193993e2dSLois Curfman McInnes 
512*51e86f29SPeter Brune   /* if someone has set the SNES PC type, create it. */
513*51e86f29SPeter Brune   ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr);
514*51e86f29SPeter Brune   ierr = PetscOptionsHasName(optionsprefix, "-npc_snes_type", &pcset);CHKERRQ(ierr);
515*51e86f29SPeter Brune   if (pcset && (!snes->pc)) {
516*51e86f29SPeter Brune     ierr = SNESGetPC(snes, &snes->pc);CHKERRQ(ierr);
517*51e86f29SPeter Brune   }
518*51e86f29SPeter Brune 
5194a0c5b0cSMatthew G Knepley   if (snes->pc) {
5204a0c5b0cSMatthew G Knepley     ierr = SNESSetOptionsPrefix(snes->pc, "npc_");CHKERRQ(ierr);
5214a0c5b0cSMatthew G Knepley     ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr);
5224a0c5b0cSMatthew G Knepley     /* Should we make a duplicate vector and matrix? Leave the DM to make it? */
5234a0c5b0cSMatthew G Knepley     ierr = SNESSetFunction(snes->pc, snes->vec_func, snes->ops->computefunction, snes->funP);CHKERRQ(ierr);
5244a0c5b0cSMatthew G Knepley     ierr = SNESSetJacobian(snes->pc, snes->jacobian, snes->jacobian_pre, snes->ops->computejacobian, snes->jacP);CHKERRQ(ierr);
5254a0c5b0cSMatthew G Knepley     ierr = SNESSetFromOptions(snes->pc);CHKERRQ(ierr);
5264a0c5b0cSMatthew G Knepley   }
5273a40ed3dSBarry Smith   PetscFunctionReturn(0);
5289b94acceSBarry Smith }
5299b94acceSBarry Smith 
530d25893d9SBarry Smith #undef __FUNCT__
531d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext"
532d25893d9SBarry Smith /*@
533d25893d9SBarry Smith    SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for
534d25893d9SBarry Smith    the nonlinear solvers.
535d25893d9SBarry Smith 
536d25893d9SBarry Smith    Logically Collective on SNES
537d25893d9SBarry Smith 
538d25893d9SBarry Smith    Input Parameters:
539d25893d9SBarry Smith +  snes - the SNES context
540d25893d9SBarry Smith .  compute - function to compute the context
541d25893d9SBarry Smith -  destroy - function to destroy the context
542d25893d9SBarry Smith 
543d25893d9SBarry Smith    Level: intermediate
544d25893d9SBarry Smith 
545d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context
546d25893d9SBarry Smith 
547d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext()
548d25893d9SBarry Smith @*/
549d25893d9SBarry Smith PetscErrorCode  SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**))
550d25893d9SBarry Smith {
551d25893d9SBarry Smith   PetscFunctionBegin;
552d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
553d25893d9SBarry Smith   snes->ops->usercompute = compute;
554d25893d9SBarry Smith   snes->ops->userdestroy = destroy;
555d25893d9SBarry Smith   PetscFunctionReturn(0);
556d25893d9SBarry Smith }
557a847f771SSatish Balay 
5584a2ae208SSatish Balay #undef __FUNCT__
5594a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
560b07ff414SBarry Smith /*@
5619b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
5629b94acceSBarry Smith    the nonlinear solvers.
5639b94acceSBarry Smith 
5643f9fe445SBarry Smith    Logically Collective on SNES
565fee21e36SBarry Smith 
566c7afd0dbSLois Curfman McInnes    Input Parameters:
567c7afd0dbSLois Curfman McInnes +  snes - the SNES context
568c7afd0dbSLois Curfman McInnes -  usrP - optional user context
569c7afd0dbSLois Curfman McInnes 
57036851e7fSLois Curfman McInnes    Level: intermediate
57136851e7fSLois Curfman McInnes 
5729b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
5739b94acceSBarry Smith 
574d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext()
5759b94acceSBarry Smith @*/
5767087cfbeSBarry Smith PetscErrorCode  SNESSetApplicationContext(SNES snes,void *usrP)
5779b94acceSBarry Smith {
5781b2093e4SBarry Smith   PetscErrorCode ierr;
579b07ff414SBarry Smith   KSP            ksp;
5801b2093e4SBarry Smith 
5813a40ed3dSBarry Smith   PetscFunctionBegin;
5820700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
583b07ff414SBarry Smith   ierr       = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
584b07ff414SBarry Smith   ierr       = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr);
5859b94acceSBarry Smith   snes->user = usrP;
5863a40ed3dSBarry Smith   PetscFunctionReturn(0);
5879b94acceSBarry Smith }
58874679c65SBarry Smith 
5894a2ae208SSatish Balay #undef __FUNCT__
5904a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
591b07ff414SBarry Smith /*@
5929b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
5939b94acceSBarry Smith    nonlinear solvers.
5949b94acceSBarry Smith 
595c7afd0dbSLois Curfman McInnes    Not Collective
596c7afd0dbSLois Curfman McInnes 
5979b94acceSBarry Smith    Input Parameter:
5989b94acceSBarry Smith .  snes - SNES context
5999b94acceSBarry Smith 
6009b94acceSBarry Smith    Output Parameter:
6019b94acceSBarry Smith .  usrP - user context
6029b94acceSBarry Smith 
60336851e7fSLois Curfman McInnes    Level: intermediate
60436851e7fSLois Curfman McInnes 
6059b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
6069b94acceSBarry Smith 
6079b94acceSBarry Smith .seealso: SNESSetApplicationContext()
6089b94acceSBarry Smith @*/
609e71120c6SJed Brown PetscErrorCode  SNESGetApplicationContext(SNES snes,void *usrP)
6109b94acceSBarry Smith {
6113a40ed3dSBarry Smith   PetscFunctionBegin;
6120700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
613e71120c6SJed Brown   *(void**)usrP = snes->user;
6143a40ed3dSBarry Smith   PetscFunctionReturn(0);
6159b94acceSBarry Smith }
61674679c65SBarry Smith 
6174a2ae208SSatish Balay #undef __FUNCT__
6184a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
6199b94acceSBarry Smith /*@
620c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
621c8228a4eSBarry Smith    at this time.
6229b94acceSBarry Smith 
623c7afd0dbSLois Curfman McInnes    Not Collective
624c7afd0dbSLois Curfman McInnes 
6259b94acceSBarry Smith    Input Parameter:
6269b94acceSBarry Smith .  snes - SNES context
6279b94acceSBarry Smith 
6289b94acceSBarry Smith    Output Parameter:
6299b94acceSBarry Smith .  iter - iteration number
6309b94acceSBarry Smith 
631c8228a4eSBarry Smith    Notes:
632c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
633c8228a4eSBarry Smith 
634c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
63508405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
63608405cd6SLois Curfman McInnes .vb
63708405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
63808405cd6SLois Curfman McInnes       if (!(it % 2)) {
63908405cd6SLois Curfman McInnes         [compute Jacobian here]
64008405cd6SLois Curfman McInnes       }
64108405cd6SLois Curfman McInnes .ve
642c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
64308405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
644c8228a4eSBarry Smith 
64536851e7fSLois Curfman McInnes    Level: intermediate
64636851e7fSLois Curfman McInnes 
6472b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
6482b668275SBarry Smith 
649b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
6509b94acceSBarry Smith @*/
6517087cfbeSBarry Smith PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt* iter)
6529b94acceSBarry Smith {
6533a40ed3dSBarry Smith   PetscFunctionBegin;
6540700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6554482741eSBarry Smith   PetscValidIntPointer(iter,2);
6569b94acceSBarry Smith   *iter = snes->iter;
6573a40ed3dSBarry Smith   PetscFunctionReturn(0);
6589b94acceSBarry Smith }
65974679c65SBarry Smith 
6604a2ae208SSatish Balay #undef __FUNCT__
6614a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
6629b94acceSBarry Smith /*@
6639b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
6649b94acceSBarry Smith    with SNESSSetFunction().
6659b94acceSBarry Smith 
666c7afd0dbSLois Curfman McInnes    Collective on SNES
667c7afd0dbSLois Curfman McInnes 
6689b94acceSBarry Smith    Input Parameter:
6699b94acceSBarry Smith .  snes - SNES context
6709b94acceSBarry Smith 
6719b94acceSBarry Smith    Output Parameter:
6729b94acceSBarry Smith .  fnorm - 2-norm of function
6739b94acceSBarry Smith 
67436851e7fSLois Curfman McInnes    Level: intermediate
67536851e7fSLois Curfman McInnes 
6769b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
677a86d99e1SLois Curfman McInnes 
678b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
6799b94acceSBarry Smith @*/
6807087cfbeSBarry Smith PetscErrorCode  SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
6819b94acceSBarry Smith {
6823a40ed3dSBarry Smith   PetscFunctionBegin;
6830700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6844482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
6859b94acceSBarry Smith   *fnorm = snes->norm;
6863a40ed3dSBarry Smith   PetscFunctionReturn(0);
6879b94acceSBarry Smith }
68874679c65SBarry Smith 
6894a2ae208SSatish Balay #undef __FUNCT__
690b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
6919b94acceSBarry Smith /*@
692b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
6939b94acceSBarry Smith    attempted by the nonlinear solver.
6949b94acceSBarry Smith 
695c7afd0dbSLois Curfman McInnes    Not Collective
696c7afd0dbSLois Curfman McInnes 
6979b94acceSBarry Smith    Input Parameter:
6989b94acceSBarry Smith .  snes - SNES context
6999b94acceSBarry Smith 
7009b94acceSBarry Smith    Output Parameter:
7019b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
7029b94acceSBarry Smith 
703c96a6f78SLois Curfman McInnes    Notes:
704c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
705c96a6f78SLois Curfman McInnes 
70636851e7fSLois Curfman McInnes    Level: intermediate
70736851e7fSLois Curfman McInnes 
7089b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
70958ebbce7SBarry Smith 
710e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
71158ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
7129b94acceSBarry Smith @*/
7137087cfbeSBarry Smith PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
7149b94acceSBarry Smith {
7153a40ed3dSBarry Smith   PetscFunctionBegin;
7160700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7174482741eSBarry Smith   PetscValidIntPointer(nfails,2);
71850ffb88aSMatthew Knepley   *nfails = snes->numFailures;
71950ffb88aSMatthew Knepley   PetscFunctionReturn(0);
72050ffb88aSMatthew Knepley }
72150ffb88aSMatthew Knepley 
72250ffb88aSMatthew Knepley #undef __FUNCT__
723b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
72450ffb88aSMatthew Knepley /*@
725b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
72650ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
72750ffb88aSMatthew Knepley 
72850ffb88aSMatthew Knepley    Not Collective
72950ffb88aSMatthew Knepley 
73050ffb88aSMatthew Knepley    Input Parameters:
73150ffb88aSMatthew Knepley +  snes     - SNES context
73250ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
73350ffb88aSMatthew Knepley 
73450ffb88aSMatthew Knepley    Level: intermediate
73550ffb88aSMatthew Knepley 
73650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
73758ebbce7SBarry Smith 
738e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
73958ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
74050ffb88aSMatthew Knepley @*/
7417087cfbeSBarry Smith PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
74250ffb88aSMatthew Knepley {
74350ffb88aSMatthew Knepley   PetscFunctionBegin;
7440700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
74550ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
74650ffb88aSMatthew Knepley   PetscFunctionReturn(0);
74750ffb88aSMatthew Knepley }
74850ffb88aSMatthew Knepley 
74950ffb88aSMatthew Knepley #undef __FUNCT__
750b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
75150ffb88aSMatthew Knepley /*@
752b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
75350ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
75450ffb88aSMatthew Knepley 
75550ffb88aSMatthew Knepley    Not Collective
75650ffb88aSMatthew Knepley 
75750ffb88aSMatthew Knepley    Input Parameter:
75850ffb88aSMatthew Knepley .  snes     - SNES context
75950ffb88aSMatthew Knepley 
76050ffb88aSMatthew Knepley    Output Parameter:
76150ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
76250ffb88aSMatthew Knepley 
76350ffb88aSMatthew Knepley    Level: intermediate
76450ffb88aSMatthew Knepley 
76550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
76658ebbce7SBarry Smith 
767e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
76858ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
76958ebbce7SBarry Smith 
77050ffb88aSMatthew Knepley @*/
7717087cfbeSBarry Smith PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
77250ffb88aSMatthew Knepley {
77350ffb88aSMatthew Knepley   PetscFunctionBegin;
7740700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7754482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
77650ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
7773a40ed3dSBarry Smith   PetscFunctionReturn(0);
7789b94acceSBarry Smith }
779a847f771SSatish Balay 
7804a2ae208SSatish Balay #undef __FUNCT__
7812541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
7822541af92SBarry Smith /*@
7832541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
7842541af92SBarry Smith      done by SNES.
7852541af92SBarry Smith 
7862541af92SBarry Smith    Not Collective
7872541af92SBarry Smith 
7882541af92SBarry Smith    Input Parameter:
7892541af92SBarry Smith .  snes     - SNES context
7902541af92SBarry Smith 
7912541af92SBarry Smith    Output Parameter:
7922541af92SBarry Smith .  nfuncs - number of evaluations
7932541af92SBarry Smith 
7942541af92SBarry Smith    Level: intermediate
7952541af92SBarry Smith 
7962541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
79758ebbce7SBarry Smith 
798e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
7992541af92SBarry Smith @*/
8007087cfbeSBarry Smith PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
8012541af92SBarry Smith {
8022541af92SBarry Smith   PetscFunctionBegin;
8030700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8042541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
8052541af92SBarry Smith   *nfuncs = snes->nfuncs;
8062541af92SBarry Smith   PetscFunctionReturn(0);
8072541af92SBarry Smith }
8082541af92SBarry Smith 
8092541af92SBarry Smith #undef __FUNCT__
8103d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
8113d4c4710SBarry Smith /*@
8123d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
8133d4c4710SBarry Smith    linear solvers.
8143d4c4710SBarry Smith 
8153d4c4710SBarry Smith    Not Collective
8163d4c4710SBarry Smith 
8173d4c4710SBarry Smith    Input Parameter:
8183d4c4710SBarry Smith .  snes - SNES context
8193d4c4710SBarry Smith 
8203d4c4710SBarry Smith    Output Parameter:
8213d4c4710SBarry Smith .  nfails - number of failed solves
8223d4c4710SBarry Smith 
8233d4c4710SBarry Smith    Notes:
8243d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
8253d4c4710SBarry Smith 
8263d4c4710SBarry Smith    Level: intermediate
8273d4c4710SBarry Smith 
8283d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
82958ebbce7SBarry Smith 
830e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
8313d4c4710SBarry Smith @*/
8327087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
8333d4c4710SBarry Smith {
8343d4c4710SBarry Smith   PetscFunctionBegin;
8350700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8363d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
8373d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
8383d4c4710SBarry Smith   PetscFunctionReturn(0);
8393d4c4710SBarry Smith }
8403d4c4710SBarry Smith 
8413d4c4710SBarry Smith #undef __FUNCT__
8423d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
8433d4c4710SBarry Smith /*@
8443d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
8453d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
8463d4c4710SBarry Smith 
8473f9fe445SBarry Smith    Logically Collective on SNES
8483d4c4710SBarry Smith 
8493d4c4710SBarry Smith    Input Parameters:
8503d4c4710SBarry Smith +  snes     - SNES context
8513d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
8523d4c4710SBarry Smith 
8533d4c4710SBarry Smith    Level: intermediate
8543d4c4710SBarry Smith 
855a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
8563d4c4710SBarry Smith 
8573d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
8583d4c4710SBarry Smith 
85958ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
8603d4c4710SBarry Smith @*/
8617087cfbeSBarry Smith PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
8623d4c4710SBarry Smith {
8633d4c4710SBarry Smith   PetscFunctionBegin;
8640700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
865c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
8663d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
8673d4c4710SBarry Smith   PetscFunctionReturn(0);
8683d4c4710SBarry Smith }
8693d4c4710SBarry Smith 
8703d4c4710SBarry Smith #undef __FUNCT__
8713d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
8723d4c4710SBarry Smith /*@
8733d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
8743d4c4710SBarry Smith      are allowed before SNES terminates
8753d4c4710SBarry Smith 
8763d4c4710SBarry Smith    Not Collective
8773d4c4710SBarry Smith 
8783d4c4710SBarry Smith    Input Parameter:
8793d4c4710SBarry Smith .  snes     - SNES context
8803d4c4710SBarry Smith 
8813d4c4710SBarry Smith    Output Parameter:
8823d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
8833d4c4710SBarry Smith 
8843d4c4710SBarry Smith    Level: intermediate
8853d4c4710SBarry Smith 
8863d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
8873d4c4710SBarry Smith 
8883d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
8893d4c4710SBarry Smith 
890e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
8913d4c4710SBarry Smith @*/
8927087cfbeSBarry Smith PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
8933d4c4710SBarry Smith {
8943d4c4710SBarry Smith   PetscFunctionBegin;
8950700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8963d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
8973d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
8983d4c4710SBarry Smith   PetscFunctionReturn(0);
8993d4c4710SBarry Smith }
9003d4c4710SBarry Smith 
9013d4c4710SBarry Smith #undef __FUNCT__
902b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
903c96a6f78SLois Curfman McInnes /*@
904b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
905c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
906c96a6f78SLois Curfman McInnes 
907c7afd0dbSLois Curfman McInnes    Not Collective
908c7afd0dbSLois Curfman McInnes 
909c96a6f78SLois Curfman McInnes    Input Parameter:
910c96a6f78SLois Curfman McInnes .  snes - SNES context
911c96a6f78SLois Curfman McInnes 
912c96a6f78SLois Curfman McInnes    Output Parameter:
913c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
914c96a6f78SLois Curfman McInnes 
915c96a6f78SLois Curfman McInnes    Notes:
916c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
917c96a6f78SLois Curfman McInnes 
91836851e7fSLois Curfman McInnes    Level: intermediate
91936851e7fSLois Curfman McInnes 
920c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
9212b668275SBarry Smith 
9228c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
923c96a6f78SLois Curfman McInnes @*/
9247087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
925c96a6f78SLois Curfman McInnes {
9263a40ed3dSBarry Smith   PetscFunctionBegin;
9270700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9284482741eSBarry Smith   PetscValidIntPointer(lits,2);
929c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
9303a40ed3dSBarry Smith   PetscFunctionReturn(0);
931c96a6f78SLois Curfman McInnes }
932c96a6f78SLois Curfman McInnes 
9334a2ae208SSatish Balay #undef __FUNCT__
93494b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
93552baeb72SSatish Balay /*@
93694b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
9379b94acceSBarry Smith 
93894b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
939c7afd0dbSLois Curfman McInnes 
9409b94acceSBarry Smith    Input Parameter:
9419b94acceSBarry Smith .  snes - the SNES context
9429b94acceSBarry Smith 
9439b94acceSBarry Smith    Output Parameter:
94494b7f48cSBarry Smith .  ksp - the KSP context
9459b94acceSBarry Smith 
9469b94acceSBarry Smith    Notes:
94794b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
9489b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
9492999313aSBarry Smith    PC contexts as well.
9509b94acceSBarry Smith 
95136851e7fSLois Curfman McInnes    Level: beginner
95236851e7fSLois Curfman McInnes 
95394b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9549b94acceSBarry Smith 
9552999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9569b94acceSBarry Smith @*/
9577087cfbeSBarry Smith PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
9589b94acceSBarry Smith {
9591cee3971SBarry Smith   PetscErrorCode ierr;
9601cee3971SBarry Smith 
9613a40ed3dSBarry Smith   PetscFunctionBegin;
9620700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9634482741eSBarry Smith   PetscValidPointer(ksp,2);
9641cee3971SBarry Smith 
9651cee3971SBarry Smith   if (!snes->ksp) {
9661cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
9671cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
9681cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
9691cee3971SBarry Smith   }
97094b7f48cSBarry Smith   *ksp = snes->ksp;
9713a40ed3dSBarry Smith   PetscFunctionReturn(0);
9729b94acceSBarry Smith }
97382bf6240SBarry Smith 
9744a2ae208SSatish Balay #undef __FUNCT__
9752999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
9762999313aSBarry Smith /*@
9772999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
9782999313aSBarry Smith 
9792999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
9802999313aSBarry Smith 
9812999313aSBarry Smith    Input Parameters:
9822999313aSBarry Smith +  snes - the SNES context
9832999313aSBarry Smith -  ksp - the KSP context
9842999313aSBarry Smith 
9852999313aSBarry Smith    Notes:
9862999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
9872999313aSBarry Smith    so this routine is rarely needed.
9882999313aSBarry Smith 
9892999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
9902999313aSBarry Smith    decreased by one.
9912999313aSBarry Smith 
9922999313aSBarry Smith    Level: developer
9932999313aSBarry Smith 
9942999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9952999313aSBarry Smith 
9962999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9972999313aSBarry Smith @*/
9987087cfbeSBarry Smith PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
9992999313aSBarry Smith {
10002999313aSBarry Smith   PetscErrorCode ierr;
10012999313aSBarry Smith 
10022999313aSBarry Smith   PetscFunctionBegin;
10030700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
10040700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
10052999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
10067dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
1007906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
10082999313aSBarry Smith   snes->ksp = ksp;
10092999313aSBarry Smith   PetscFunctionReturn(0);
10102999313aSBarry Smith }
10112999313aSBarry Smith 
10127adad957SLisandro Dalcin #if 0
10132999313aSBarry Smith #undef __FUNCT__
10144a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
10156849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
1016e24b481bSBarry Smith {
1017e24b481bSBarry Smith   PetscFunctionBegin;
1018e24b481bSBarry Smith   PetscFunctionReturn(0);
1019e24b481bSBarry Smith }
10207adad957SLisandro Dalcin #endif
1021e24b481bSBarry Smith 
10229b94acceSBarry Smith /* -----------------------------------------------------------*/
10234a2ae208SSatish Balay #undef __FUNCT__
10244a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
102552baeb72SSatish Balay /*@
10269b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
10279b94acceSBarry Smith 
1028c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
1029c7afd0dbSLois Curfman McInnes 
1030c7afd0dbSLois Curfman McInnes    Input Parameters:
1031906ed7ccSBarry Smith .  comm - MPI communicator
10329b94acceSBarry Smith 
10339b94acceSBarry Smith    Output Parameter:
10349b94acceSBarry Smith .  outsnes - the new SNES context
10359b94acceSBarry Smith 
1036c7afd0dbSLois Curfman McInnes    Options Database Keys:
1037c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
1038c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
1039c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
1040c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
1041c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
1042c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
1043c1f60f51SBarry Smith 
104436851e7fSLois Curfman McInnes    Level: beginner
104536851e7fSLois Curfman McInnes 
10469b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
10479b94acceSBarry Smith 
1048a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
1049a8054027SBarry Smith 
10509b94acceSBarry Smith @*/
10517087cfbeSBarry Smith PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
10529b94acceSBarry Smith {
1053dfbe8321SBarry Smith   PetscErrorCode      ierr;
10549b94acceSBarry Smith   SNES                snes;
1055fa9f3622SBarry Smith   SNESKSPEW           *kctx;
105637fcc0dbSBarry Smith 
10573a40ed3dSBarry Smith   PetscFunctionBegin;
1058ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
10598ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
10608ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
10618ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
10628ba1e511SMatthew Knepley #endif
10638ba1e511SMatthew Knepley 
10643194b578SJed Brown   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
10657adad957SLisandro Dalcin 
106685385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
10672c155ee1SBarry Smith   snes->usesksp           = PETSC_TRUE;
10689b94acceSBarry Smith   snes->max_its           = 50;
10699750a799SBarry Smith   snes->max_funcs	  = 10000;
10709b94acceSBarry Smith   snes->norm		  = 0.0;
1071b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
1072b4874afaSBarry Smith   snes->ttol              = 0.0;
107370441072SBarry Smith   snes->abstol		  = 1.e-50;
10749b94acceSBarry Smith   snes->xtol		  = 1.e-8;
10754b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
10769b94acceSBarry Smith   snes->nfuncs            = 0;
107750ffb88aSMatthew Knepley   snes->numFailures       = 0;
107850ffb88aSMatthew Knepley   snes->maxFailures       = 1;
10797a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1080e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1081a8054027SBarry Smith   snes->lagpreconditioner = 1;
1082639f9d9dSBarry Smith   snes->numbermonitors    = 0;
10839b94acceSBarry Smith   snes->data              = 0;
10844dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1085186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
10866f24a144SLois Curfman McInnes   snes->nwork             = 0;
108758c9b817SLisandro Dalcin   snes->work              = 0;
108858c9b817SLisandro Dalcin   snes->nvwork            = 0;
108958c9b817SLisandro Dalcin   snes->vwork             = 0;
1090758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1091758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1092758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1093758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1094758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1095184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
10969b94acceSBarry Smith 
10973d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
10983d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
10993d4c4710SBarry Smith 
11009b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
110138f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
11029b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
11039b94acceSBarry Smith   kctx->version     = 2;
11049b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
11059b94acceSBarry Smith                              this was too large for some test cases */
110675567043SBarry Smith   kctx->rtol_last   = 0.0;
11079b94acceSBarry Smith   kctx->rtol_max    = .9;
11089b94acceSBarry Smith   kctx->gamma       = 1.0;
110971f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
111071f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
11119b94acceSBarry Smith   kctx->threshold   = .1;
111275567043SBarry Smith   kctx->lresid_last = 0.0;
111375567043SBarry Smith   kctx->norm_last   = 0.0;
11149b94acceSBarry Smith 
11159b94acceSBarry Smith   *outsnes = snes;
11163a40ed3dSBarry Smith   PetscFunctionReturn(0);
11179b94acceSBarry Smith }
11189b94acceSBarry Smith 
11194a2ae208SSatish Balay #undef __FUNCT__
11204a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
11219b94acceSBarry Smith /*@C
11229b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
11239b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
11249b94acceSBarry Smith    equations.
11259b94acceSBarry Smith 
11263f9fe445SBarry Smith    Logically Collective on SNES
1127fee21e36SBarry Smith 
1128c7afd0dbSLois Curfman McInnes    Input Parameters:
1129c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1130c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1131de044059SHong Zhang .  func - function evaluation routine
1132c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1133c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
11349b94acceSBarry Smith 
1135c7afd0dbSLois Curfman McInnes    Calling sequence of func:
11368d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1137c7afd0dbSLois Curfman McInnes 
1138313e4042SLois Curfman McInnes .  f - function vector
1139c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
11409b94acceSBarry Smith 
11419b94acceSBarry Smith    Notes:
11429b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
11439b94acceSBarry Smith $      f'(x) x = -f(x),
1144c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
11459b94acceSBarry Smith 
114636851e7fSLois Curfman McInnes    Level: beginner
114736851e7fSLois Curfman McInnes 
11489b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
11499b94acceSBarry Smith 
1150a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
11519b94acceSBarry Smith @*/
11527087cfbeSBarry Smith PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
11539b94acceSBarry Smith {
115485385478SLisandro Dalcin   PetscErrorCode ierr;
11553a40ed3dSBarry Smith   PetscFunctionBegin;
11560700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1157d2a683ecSLisandro Dalcin   if (r) {
1158d2a683ecSLisandro Dalcin     PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1159d2a683ecSLisandro Dalcin     PetscCheckSameComm(snes,1,r,2);
116085385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
11616bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
116285385478SLisandro Dalcin     snes->vec_func = r;
1163d2a683ecSLisandro Dalcin   } else if (!snes->vec_func && snes->dm) {
1164d2a683ecSLisandro Dalcin     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1165d2a683ecSLisandro Dalcin   }
1166d2a683ecSLisandro Dalcin   if (func) snes->ops->computefunction = func;
1167d2a683ecSLisandro Dalcin   if (ctx)  snes->funP                 = ctx;
11683a40ed3dSBarry Smith   PetscFunctionReturn(0);
11699b94acceSBarry Smith }
11709b94acceSBarry Smith 
1171d25893d9SBarry Smith #undef __FUNCT__
1172d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess"
1173d25893d9SBarry Smith /*@C
1174d25893d9SBarry Smith    SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem
1175d25893d9SBarry Smith 
1176d25893d9SBarry Smith    Logically Collective on SNES
1177d25893d9SBarry Smith 
1178d25893d9SBarry Smith    Input Parameters:
1179d25893d9SBarry Smith +  snes - the SNES context
1180d25893d9SBarry Smith .  func - function evaluation routine
1181d25893d9SBarry Smith -  ctx - [optional] user-defined context for private data for the
1182d25893d9SBarry Smith          function evaluation routine (may be PETSC_NULL)
1183d25893d9SBarry Smith 
1184d25893d9SBarry Smith    Calling sequence of func:
1185d25893d9SBarry Smith $    func (SNES snes,Vec x,void *ctx);
1186d25893d9SBarry Smith 
1187d25893d9SBarry Smith .  f - function vector
1188d25893d9SBarry Smith -  ctx - optional user-defined function context
1189d25893d9SBarry Smith 
1190d25893d9SBarry Smith    Level: intermediate
1191d25893d9SBarry Smith 
1192d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function
1193d25893d9SBarry Smith 
1194d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
1195d25893d9SBarry Smith @*/
1196d25893d9SBarry Smith PetscErrorCode  SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx)
1197d25893d9SBarry Smith {
1198d25893d9SBarry Smith   PetscFunctionBegin;
1199d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1200d25893d9SBarry Smith   if (func) snes->ops->computeinitialguess = func;
1201d25893d9SBarry Smith   if (ctx)  snes->initialguessP            = ctx;
1202d25893d9SBarry Smith   PetscFunctionReturn(0);
1203d25893d9SBarry Smith }
1204d25893d9SBarry Smith 
12053ab0aad5SBarry Smith /* --------------------------------------------------------------- */
12063ab0aad5SBarry Smith #undef __FUNCT__
12071096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
12081096aae1SMatthew Knepley /*@C
12091096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
12101096aae1SMatthew Knepley    it assumes a zero right hand side.
12111096aae1SMatthew Knepley 
12123f9fe445SBarry Smith    Logically Collective on SNES
12131096aae1SMatthew Knepley 
12141096aae1SMatthew Knepley    Input Parameter:
12151096aae1SMatthew Knepley .  snes - the SNES context
12161096aae1SMatthew Knepley 
12171096aae1SMatthew Knepley    Output Parameter:
1218bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
12191096aae1SMatthew Knepley 
12201096aae1SMatthew Knepley    Level: intermediate
12211096aae1SMatthew Knepley 
12221096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
12231096aae1SMatthew Knepley 
122485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
12251096aae1SMatthew Knepley @*/
12267087cfbeSBarry Smith PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
12271096aae1SMatthew Knepley {
12281096aae1SMatthew Knepley   PetscFunctionBegin;
12290700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12301096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
123185385478SLisandro Dalcin   *rhs = snes->vec_rhs;
12321096aae1SMatthew Knepley   PetscFunctionReturn(0);
12331096aae1SMatthew Knepley }
12341096aae1SMatthew Knepley 
12351096aae1SMatthew Knepley #undef __FUNCT__
12364a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
12379b94acceSBarry Smith /*@
123836851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
12399b94acceSBarry Smith                          SNESSetFunction().
12409b94acceSBarry Smith 
1241c7afd0dbSLois Curfman McInnes    Collective on SNES
1242c7afd0dbSLois Curfman McInnes 
12439b94acceSBarry Smith    Input Parameters:
1244c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1245c7afd0dbSLois Curfman McInnes -  x - input vector
12469b94acceSBarry Smith 
12479b94acceSBarry Smith    Output Parameter:
12483638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
12499b94acceSBarry Smith 
12501bffabb2SLois Curfman McInnes    Notes:
125136851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
125236851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
125336851e7fSLois Curfman McInnes    themselves.
125436851e7fSLois Curfman McInnes 
125536851e7fSLois Curfman McInnes    Level: developer
125636851e7fSLois Curfman McInnes 
12579b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
12589b94acceSBarry Smith 
1259a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
12609b94acceSBarry Smith @*/
12617087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
12629b94acceSBarry Smith {
1263dfbe8321SBarry Smith   PetscErrorCode ierr;
12649b94acceSBarry Smith 
12653a40ed3dSBarry Smith   PetscFunctionBegin;
12660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12670700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
12680700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1269c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1270c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1271184914b5SBarry Smith 
1272d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1273e7788613SBarry Smith   if (snes->ops->computefunction) {
1274d64ed03dSBarry Smith     PetscStackPush("SNES user function");
127539d508bbSBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1276d64ed03dSBarry Smith     PetscStackPop;
127773250ac0SBarry Smith   } else if (snes->dm) {
1278644e2e5bSBarry Smith     ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr);
1279c90fad12SPeter Brune   } else if (snes->vec_rhs) {
1280c90fad12SPeter Brune     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
1281644e2e5bSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve().");
128285385478SLisandro Dalcin   if (snes->vec_rhs) {
128385385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
12843ab0aad5SBarry Smith   }
1285ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1286d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
12873a40ed3dSBarry Smith   PetscFunctionReturn(0);
12889b94acceSBarry Smith }
12899b94acceSBarry Smith 
12904a2ae208SSatish Balay #undef __FUNCT__
12914a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
129262fef451SLois Curfman McInnes /*@
129362fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
129462fef451SLois Curfman McInnes    set with SNESSetJacobian().
129562fef451SLois Curfman McInnes 
1296c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1297c7afd0dbSLois Curfman McInnes 
129862fef451SLois Curfman McInnes    Input Parameters:
1299c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1300c7afd0dbSLois Curfman McInnes -  x - input vector
130162fef451SLois Curfman McInnes 
130262fef451SLois Curfman McInnes    Output Parameters:
1303c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
130462fef451SLois Curfman McInnes .  B - optional preconditioning matrix
13052b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1306fee21e36SBarry Smith 
1307e35cf81dSBarry Smith   Options Database Keys:
1308e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1309693365a8SJed Brown .    -snes_lag_jacobian <lag>
1310693365a8SJed Brown .    -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences
1311693365a8SJed Brown .    -snes_compare_explicit_draw  - Compare the computed Jacobian to the finite difference Jacobian and draw the result
1312693365a8SJed Brown .    -snes_compare_explicit_contour  - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result
1313693365a8SJed Brown -    -snes_compare_operator  - Make the comparison options above use the operator instead of the preconditioning matrix
1314e35cf81dSBarry Smith 
131562fef451SLois Curfman McInnes    Notes:
131662fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
131762fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
131862fef451SLois Curfman McInnes 
131994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1320dc5a77f8SLois Curfman McInnes    flag parameter.
132162fef451SLois Curfman McInnes 
132236851e7fSLois Curfman McInnes    Level: developer
132336851e7fSLois Curfman McInnes 
132462fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
132562fef451SLois Curfman McInnes 
1326e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
132762fef451SLois Curfman McInnes @*/
13287087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
13299b94acceSBarry Smith {
1330dfbe8321SBarry Smith   PetscErrorCode ierr;
1331ace3abfcSBarry Smith   PetscBool      flag;
13323a40ed3dSBarry Smith 
13333a40ed3dSBarry Smith   PetscFunctionBegin;
13340700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13350700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
13364482741eSBarry Smith   PetscValidPointer(flg,5);
1337c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1338e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1339ebd3b9afSBarry Smith 
1340ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1341ebd3b9afSBarry Smith 
1342fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1343fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1344fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1345fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1346e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1347e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1348ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1349ebd3b9afSBarry Smith     if (flag) {
1350ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1351ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1352ebd3b9afSBarry Smith     }
1353e35cf81dSBarry Smith     PetscFunctionReturn(0);
1354e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1355e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1356e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1357ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1358ebd3b9afSBarry Smith     if (flag) {
1359ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1360ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1361ebd3b9afSBarry Smith     }
1362e35cf81dSBarry Smith     PetscFunctionReturn(0);
1363e35cf81dSBarry Smith   }
1364e35cf81dSBarry Smith 
1365c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1366e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1367d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1368e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1369d64ed03dSBarry Smith   PetscStackPop;
1370d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1371a8054027SBarry Smith 
13723b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
13733b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
13743b4f5425SBarry Smith     snes->lagpreconditioner = -1;
13753b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1376a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1377a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1378a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1379a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1380a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1381a8054027SBarry Smith   }
1382a8054027SBarry Smith 
13836d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
13840700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
13850700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
1386693365a8SJed Brown   {
1387693365a8SJed Brown     PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE;
1388693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr);
1389693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr);
1390693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr);
1391693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr);
1392693365a8SJed Brown     if (flag || flag_draw || flag_contour) {
1393693365a8SJed Brown       Mat Bexp_mine = PETSC_NULL,Bexp,FDexp;
1394693365a8SJed Brown       MatStructure mstruct;
1395693365a8SJed Brown       PetscViewer vdraw,vstdout;
13966b3a5b13SJed Brown       PetscBool flg;
1397693365a8SJed Brown       if (flag_operator) {
1398693365a8SJed Brown         ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr);
1399693365a8SJed Brown         Bexp = Bexp_mine;
1400693365a8SJed Brown       } else {
1401693365a8SJed Brown         /* See if the preconditioning matrix can be viewed and added directly */
1402693365a8SJed Brown         ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr);
1403693365a8SJed Brown         if (flg) Bexp = *B;
1404693365a8SJed Brown         else {
1405693365a8SJed Brown           /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */
1406693365a8SJed Brown           ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr);
1407693365a8SJed Brown           Bexp = Bexp_mine;
1408693365a8SJed Brown         }
1409693365a8SJed Brown       }
1410693365a8SJed Brown       ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr);
1411693365a8SJed Brown       ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr);
1412693365a8SJed Brown       ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr);
1413693365a8SJed Brown       if (flag_draw || flag_contour) {
1414693365a8SJed Brown         ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr);
1415693365a8SJed Brown         if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);}
1416693365a8SJed Brown       } else vdraw = PETSC_NULL;
1417693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr);
1418693365a8SJed Brown       if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);}
1419693365a8SJed Brown       if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);}
1420693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr);
1421693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1422693365a8SJed Brown       if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);}
1423693365a8SJed Brown       ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
1424693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr);
1425693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1426693365a8SJed Brown       if (vdraw) {              /* Always use contour for the difference */
1427693365a8SJed Brown         ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
1428693365a8SJed Brown         ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);
1429693365a8SJed Brown         ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);
1430693365a8SJed Brown       }
1431693365a8SJed Brown       if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);}
1432693365a8SJed Brown       ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr);
1433693365a8SJed Brown       ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr);
1434693365a8SJed Brown       ierr = MatDestroy(&FDexp);CHKERRQ(ierr);
1435693365a8SJed Brown     }
1436693365a8SJed Brown   }
14373a40ed3dSBarry Smith   PetscFunctionReturn(0);
14389b94acceSBarry Smith }
14399b94acceSBarry Smith 
14404a2ae208SSatish Balay #undef __FUNCT__
14414a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
14429b94acceSBarry Smith /*@C
14439b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1444044dda88SLois Curfman McInnes    location to store the matrix.
14459b94acceSBarry Smith 
14463f9fe445SBarry Smith    Logically Collective on SNES and Mat
1447c7afd0dbSLois Curfman McInnes 
14489b94acceSBarry Smith    Input Parameters:
1449c7afd0dbSLois Curfman McInnes +  snes - the SNES context
14509b94acceSBarry Smith .  A - Jacobian matrix
14519b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
1452efd51863SBarry Smith .  func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value)
1453c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1454efd51863SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value)
14559b94acceSBarry Smith 
14569b94acceSBarry Smith    Calling sequence of func:
14578d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
14589b94acceSBarry Smith 
1459c7afd0dbSLois Curfman McInnes +  x - input vector
14609b94acceSBarry Smith .  A - Jacobian matrix
14619b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1462ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
14632b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1464c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
14659b94acceSBarry Smith 
14669b94acceSBarry Smith    Notes:
146794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
14682cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1469ac21db08SLois Curfman McInnes 
1470ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
14719b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
14729b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
14739b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
14749b94acceSBarry Smith    throughout the global iterations.
14759b94acceSBarry Smith 
147616913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
147716913363SBarry Smith    each matrix.
147816913363SBarry Smith 
1479a8a26c1eSJed Brown    If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument
1480a8a26c1eSJed Brown    must be a MatFDColoring.
1481a8a26c1eSJed Brown 
1482c3cc8fd1SJed Brown    Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian.  One common
1483c3cc8fd1SJed Brown    example is to use the "Picard linearization" which only differentiates through the highest order parts of each term.
1484c3cc8fd1SJed Brown 
148536851e7fSLois Curfman McInnes    Level: beginner
148636851e7fSLois Curfman McInnes 
14879b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
14889b94acceSBarry Smith 
14893ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
14909b94acceSBarry Smith @*/
14917087cfbeSBarry Smith PetscErrorCode  SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
14929b94acceSBarry Smith {
1493dfbe8321SBarry Smith   PetscErrorCode ierr;
14943a7fca6bSBarry Smith 
14953a40ed3dSBarry Smith   PetscFunctionBegin;
14960700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14970700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
14980700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1499c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
150006975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1501e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
15023a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
15033a7fca6bSBarry Smith   if (A) {
15047dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
15056bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
15069b94acceSBarry Smith     snes->jacobian = A;
15073a7fca6bSBarry Smith   }
15083a7fca6bSBarry Smith   if (B) {
15097dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
15106bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
15119b94acceSBarry Smith     snes->jacobian_pre = B;
15123a7fca6bSBarry Smith   }
15133a40ed3dSBarry Smith   PetscFunctionReturn(0);
15149b94acceSBarry Smith }
151562fef451SLois Curfman McInnes 
15164a2ae208SSatish Balay #undef __FUNCT__
15174a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1518c2aafc4cSSatish Balay /*@C
1519b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1520b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1521b4fd4287SBarry Smith 
1522c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1523c7afd0dbSLois Curfman McInnes 
1524b4fd4287SBarry Smith    Input Parameter:
1525b4fd4287SBarry Smith .  snes - the nonlinear solver context
1526b4fd4287SBarry Smith 
1527b4fd4287SBarry Smith    Output Parameters:
1528c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1529b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
153070e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
153170e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1532fee21e36SBarry Smith 
153336851e7fSLois Curfman McInnes    Level: advanced
153436851e7fSLois Curfman McInnes 
1535b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1536b4fd4287SBarry Smith @*/
15377087cfbeSBarry Smith PetscErrorCode  SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1538b4fd4287SBarry Smith {
15393a40ed3dSBarry Smith   PetscFunctionBegin;
15400700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1541b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1542b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1543e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
154470e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
15453a40ed3dSBarry Smith   PetscFunctionReturn(0);
1546b4fd4287SBarry Smith }
1547b4fd4287SBarry Smith 
15489b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
15499b94acceSBarry Smith 
15504a2ae208SSatish Balay #undef __FUNCT__
15514a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
15529b94acceSBarry Smith /*@
15539b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1554272ac6f2SLois Curfman McInnes    of a nonlinear solver.
15559b94acceSBarry Smith 
1556fee21e36SBarry Smith    Collective on SNES
1557fee21e36SBarry Smith 
1558c7afd0dbSLois Curfman McInnes    Input Parameters:
155970e92668SMatthew Knepley .  snes - the SNES context
1560c7afd0dbSLois Curfman McInnes 
1561272ac6f2SLois Curfman McInnes    Notes:
1562272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1563272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1564272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1565272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1566272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1567272ac6f2SLois Curfman McInnes 
156836851e7fSLois Curfman McInnes    Level: advanced
156936851e7fSLois Curfman McInnes 
15709b94acceSBarry Smith .keywords: SNES, nonlinear, setup
15719b94acceSBarry Smith 
15729b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
15739b94acceSBarry Smith @*/
15747087cfbeSBarry Smith PetscErrorCode  SNESSetUp(SNES snes)
15759b94acceSBarry Smith {
1576dfbe8321SBarry Smith   PetscErrorCode ierr;
15773a40ed3dSBarry Smith 
15783a40ed3dSBarry Smith   PetscFunctionBegin;
15790700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
15804dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
15819b94acceSBarry Smith 
15827adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
158385385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
158485385478SLisandro Dalcin   }
158585385478SLisandro Dalcin 
1586c9b9fda1SJed Brown   if (!snes->vec_func) {
1587c9b9fda1SJed Brown     if (snes->vec_rhs) {
1588c9b9fda1SJed Brown       ierr = VecDuplicate(snes->vec_rhs,&snes->vec_func);CHKERRQ(ierr);
1589c9b9fda1SJed Brown     } else if (snes->vec_sol) {
1590c9b9fda1SJed Brown       ierr = VecDuplicate(snes->vec_sol,&snes->vec_func);CHKERRQ(ierr);
1591c9b9fda1SJed Brown     } else if (snes->dm) {
1592efd51863SBarry Smith       ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1593efd51863SBarry Smith     }
1594c9b9fda1SJed Brown   }
159517186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
159658c9b817SLisandro Dalcin   if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
159758c9b817SLisandro Dalcin 
159858c9b817SLisandro Dalcin   if (!snes->vec_sol_update /* && snes->vec_sol */) {
159958c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr);
160058c9b817SLisandro Dalcin     ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr);
160158c9b817SLisandro Dalcin   }
160258c9b817SLisandro Dalcin 
1603ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1604ef8dffc7SBarry Smith     Mat J;
1605ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1606cab2e9ccSBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDMComputeJacobian,PETSC_NULL);CHKERRQ(ierr);
1607cab2e9ccSBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
16082ef29e96SBarry Smith   } else if (!snes->jacobian && snes->ops->computejacobian == MatMFFDComputeJacobian) {
1609a8248277SBarry Smith     Mat J;
1610a8248277SBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
1611a8248277SBarry Smith     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
1612a8248277SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
1613a8248277SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
1614a8248277SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1615a8248277SBarry Smith   } else if (snes->dm && snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) {
1616a8248277SBarry Smith     Mat J,B;
1617a8248277SBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
1618a8248277SBarry Smith     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
1619a8248277SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
1620a8248277SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr);
1621a8248277SBarry Smith     ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,snes->funP);CHKERRQ(ierr);
1622a8248277SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1623a8248277SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
1624efd51863SBarry Smith   } else if (snes->dm && !snes->jacobian_pre){
1625efd51863SBarry Smith     Mat J;
1626efd51863SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
16273cbb28f5SBarry Smith     ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1628efd51863SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1629ef8dffc7SBarry Smith   }
1630cfaf3a74SBarry Smith   if (!snes->ops->computefunction && !snes->dm) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a residual function with SNESSetFunction() or call SNESSetDM()");
1631c9b9fda1SJed Brown   if (!snes->vec_func) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a vector when calling SNESSetFunction() or call SNESSetDM()");
1632efd51863SBarry Smith 
1633b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1634b710008aSBarry Smith 
1635d25893d9SBarry Smith   if (snes->ops->usercompute && !snes->user) {
1636d25893d9SBarry Smith     ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr);
1637d25893d9SBarry Smith   }
1638d25893d9SBarry Smith 
1639410397dcSLisandro Dalcin   if (snes->ops->setup) {
1640410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1641410397dcSLisandro Dalcin   }
164258c9b817SLisandro Dalcin 
16437aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
16443a40ed3dSBarry Smith   PetscFunctionReturn(0);
16459b94acceSBarry Smith }
16469b94acceSBarry Smith 
16474a2ae208SSatish Balay #undef __FUNCT__
164837596af1SLisandro Dalcin #define __FUNCT__ "SNESReset"
164937596af1SLisandro Dalcin /*@
165037596af1SLisandro Dalcin    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats
165137596af1SLisandro Dalcin 
165237596af1SLisandro Dalcin    Collective on SNES
165337596af1SLisandro Dalcin 
165437596af1SLisandro Dalcin    Input Parameter:
165537596af1SLisandro Dalcin .  snes - iterative context obtained from SNESCreate()
165637596af1SLisandro Dalcin 
1657d25893d9SBarry Smith    Level: intermediate
1658d25893d9SBarry Smith 
1659d25893d9SBarry Smith    Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext()
166037596af1SLisandro Dalcin 
166137596af1SLisandro Dalcin .keywords: SNES, destroy
166237596af1SLisandro Dalcin 
166337596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
166437596af1SLisandro Dalcin @*/
166537596af1SLisandro Dalcin PetscErrorCode  SNESReset(SNES snes)
166637596af1SLisandro Dalcin {
166737596af1SLisandro Dalcin   PetscErrorCode ierr;
166837596af1SLisandro Dalcin 
166937596af1SLisandro Dalcin   PetscFunctionBegin;
167037596af1SLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1671d25893d9SBarry Smith   if (snes->ops->userdestroy && snes->user) {
1672d25893d9SBarry Smith     ierr       = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr);
1673d25893d9SBarry Smith     snes->user = PETSC_NULL;
1674d25893d9SBarry Smith   }
16758a23116dSBarry Smith   if (snes->pc) {
16768a23116dSBarry Smith     ierr = SNESReset(snes->pc);CHKERRQ(ierr);
16778a23116dSBarry Smith   }
16788a23116dSBarry Smith 
167937596af1SLisandro Dalcin   if (snes->ops->reset) {
168037596af1SLisandro Dalcin     ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr);
168137596af1SLisandro Dalcin   }
168237596af1SLisandro Dalcin   if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);}
16836bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
16846bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
16856bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr);
16866bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
16876bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
16886bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
168937596af1SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
169037596af1SLisandro Dalcin   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
169137596af1SLisandro Dalcin   snes->nwork = snes->nvwork = 0;
169237596af1SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
169337596af1SLisandro Dalcin   PetscFunctionReturn(0);
169437596af1SLisandro Dalcin }
169537596af1SLisandro Dalcin 
169637596af1SLisandro Dalcin #undef __FUNCT__
16974a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
169852baeb72SSatish Balay /*@
16999b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
17009b94acceSBarry Smith    with SNESCreate().
17019b94acceSBarry Smith 
1702c7afd0dbSLois Curfman McInnes    Collective on SNES
1703c7afd0dbSLois Curfman McInnes 
17049b94acceSBarry Smith    Input Parameter:
17059b94acceSBarry Smith .  snes - the SNES context
17069b94acceSBarry Smith 
170736851e7fSLois Curfman McInnes    Level: beginner
170836851e7fSLois Curfman McInnes 
17099b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
17109b94acceSBarry Smith 
171163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
17129b94acceSBarry Smith @*/
17136bf464f9SBarry Smith PetscErrorCode  SNESDestroy(SNES *snes)
17149b94acceSBarry Smith {
17156849ba73SBarry Smith   PetscErrorCode ierr;
17163a40ed3dSBarry Smith 
17173a40ed3dSBarry Smith   PetscFunctionBegin;
17186bf464f9SBarry Smith   if (!*snes) PetscFunctionReturn(0);
17196bf464f9SBarry Smith   PetscValidHeaderSpecific((*snes),SNES_CLASSID,1);
17206bf464f9SBarry Smith   if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);}
1721d4bb536fSBarry Smith 
17226bf464f9SBarry Smith   ierr = SNESReset((*snes));CHKERRQ(ierr);
17238a23116dSBarry Smith   ierr = SNESDestroy(&(*snes)->pc);CHKERRQ(ierr);
17246b8b9a38SLisandro Dalcin 
1725be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
17266bf464f9SBarry Smith   ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr);
17276bf464f9SBarry Smith   if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);}
17286d4c513bSLisandro Dalcin 
17296bf464f9SBarry Smith   ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr);
17306bf464f9SBarry Smith   ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr);
17316b8b9a38SLisandro Dalcin 
17326bf464f9SBarry Smith   ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr);
17336bf464f9SBarry Smith   if ((*snes)->ops->convergeddestroy) {
17346bf464f9SBarry Smith     ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr);
17356b8b9a38SLisandro Dalcin   }
17366bf464f9SBarry Smith   if ((*snes)->conv_malloc) {
17376bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr);
17386bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr);
173958c9b817SLisandro Dalcin   }
17406bf464f9SBarry Smith   ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr);
1741a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
17423a40ed3dSBarry Smith   PetscFunctionReturn(0);
17439b94acceSBarry Smith }
17449b94acceSBarry Smith 
17459b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
17469b94acceSBarry Smith 
17474a2ae208SSatish Balay #undef __FUNCT__
1748a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1749a8054027SBarry Smith /*@
1750a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1751a8054027SBarry Smith 
17523f9fe445SBarry Smith    Logically Collective on SNES
1753a8054027SBarry Smith 
1754a8054027SBarry Smith    Input Parameters:
1755a8054027SBarry Smith +  snes - the SNES context
1756a8054027SBarry 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
17573b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1758a8054027SBarry Smith 
1759a8054027SBarry Smith    Options Database Keys:
1760a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1761a8054027SBarry Smith 
1762a8054027SBarry Smith    Notes:
1763a8054027SBarry Smith    The default is 1
1764a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1765a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1766a8054027SBarry Smith 
1767a8054027SBarry Smith    Level: intermediate
1768a8054027SBarry Smith 
1769a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1770a8054027SBarry Smith 
1771e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1772a8054027SBarry Smith 
1773a8054027SBarry Smith @*/
17747087cfbeSBarry Smith PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1775a8054027SBarry Smith {
1776a8054027SBarry Smith   PetscFunctionBegin;
17770700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1778e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1779e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1780c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1781a8054027SBarry Smith   snes->lagpreconditioner = lag;
1782a8054027SBarry Smith   PetscFunctionReturn(0);
1783a8054027SBarry Smith }
1784a8054027SBarry Smith 
1785a8054027SBarry Smith #undef __FUNCT__
1786efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence"
1787efd51863SBarry Smith /*@
1788efd51863SBarry Smith    SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does
1789efd51863SBarry Smith 
1790efd51863SBarry Smith    Logically Collective on SNES
1791efd51863SBarry Smith 
1792efd51863SBarry Smith    Input Parameters:
1793efd51863SBarry Smith +  snes - the SNES context
1794efd51863SBarry Smith -  steps - the number of refinements to do, defaults to 0
1795efd51863SBarry Smith 
1796efd51863SBarry Smith    Options Database Keys:
1797efd51863SBarry Smith .    -snes_grid_sequence <steps>
1798efd51863SBarry Smith 
1799efd51863SBarry Smith    Level: intermediate
1800efd51863SBarry Smith 
1801efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1802efd51863SBarry Smith 
1803efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1804efd51863SBarry Smith 
1805efd51863SBarry Smith @*/
1806efd51863SBarry Smith PetscErrorCode  SNESSetGridSequence(SNES snes,PetscInt steps)
1807efd51863SBarry Smith {
1808efd51863SBarry Smith   PetscFunctionBegin;
1809efd51863SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1810efd51863SBarry Smith   PetscValidLogicalCollectiveInt(snes,steps,2);
1811efd51863SBarry Smith   snes->gridsequence = steps;
1812efd51863SBarry Smith   PetscFunctionReturn(0);
1813efd51863SBarry Smith }
1814efd51863SBarry Smith 
1815efd51863SBarry Smith #undef __FUNCT__
1816a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1817a8054027SBarry Smith /*@
1818a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1819a8054027SBarry Smith 
18203f9fe445SBarry Smith    Not Collective
1821a8054027SBarry Smith 
1822a8054027SBarry Smith    Input Parameter:
1823a8054027SBarry Smith .  snes - the SNES context
1824a8054027SBarry Smith 
1825a8054027SBarry Smith    Output Parameter:
1826a8054027SBarry 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
18273b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1828a8054027SBarry Smith 
1829a8054027SBarry Smith    Options Database Keys:
1830a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1831a8054027SBarry Smith 
1832a8054027SBarry Smith    Notes:
1833a8054027SBarry Smith    The default is 1
1834a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1835a8054027SBarry Smith 
1836a8054027SBarry Smith    Level: intermediate
1837a8054027SBarry Smith 
1838a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1839a8054027SBarry Smith 
1840a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1841a8054027SBarry Smith 
1842a8054027SBarry Smith @*/
18437087cfbeSBarry Smith PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1844a8054027SBarry Smith {
1845a8054027SBarry Smith   PetscFunctionBegin;
18460700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1847a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1848a8054027SBarry Smith   PetscFunctionReturn(0);
1849a8054027SBarry Smith }
1850a8054027SBarry Smith 
1851a8054027SBarry Smith #undef __FUNCT__
1852e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1853e35cf81dSBarry Smith /*@
1854e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1855e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1856e35cf81dSBarry Smith 
18573f9fe445SBarry Smith    Logically Collective on SNES
1858e35cf81dSBarry Smith 
1859e35cf81dSBarry Smith    Input Parameters:
1860e35cf81dSBarry Smith +  snes - the SNES context
1861e35cf81dSBarry 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
1862fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1863e35cf81dSBarry Smith 
1864e35cf81dSBarry Smith    Options Database Keys:
1865e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1866e35cf81dSBarry Smith 
1867e35cf81dSBarry Smith    Notes:
1868e35cf81dSBarry Smith    The default is 1
1869e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1870fe3ffe1eSBarry 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
1871fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1872e35cf81dSBarry Smith 
1873e35cf81dSBarry Smith    Level: intermediate
1874e35cf81dSBarry Smith 
1875e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1876e35cf81dSBarry Smith 
1877e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1878e35cf81dSBarry Smith 
1879e35cf81dSBarry Smith @*/
18807087cfbeSBarry Smith PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
1881e35cf81dSBarry Smith {
1882e35cf81dSBarry Smith   PetscFunctionBegin;
18830700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1884e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1885e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1886c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1887e35cf81dSBarry Smith   snes->lagjacobian = lag;
1888e35cf81dSBarry Smith   PetscFunctionReturn(0);
1889e35cf81dSBarry Smith }
1890e35cf81dSBarry Smith 
1891e35cf81dSBarry Smith #undef __FUNCT__
1892e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1893e35cf81dSBarry Smith /*@
1894e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1895e35cf81dSBarry Smith 
18963f9fe445SBarry Smith    Not Collective
1897e35cf81dSBarry Smith 
1898e35cf81dSBarry Smith    Input Parameter:
1899e35cf81dSBarry Smith .  snes - the SNES context
1900e35cf81dSBarry Smith 
1901e35cf81dSBarry Smith    Output Parameter:
1902e35cf81dSBarry 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
1903e35cf81dSBarry Smith          the Jacobian is built etc.
1904e35cf81dSBarry Smith 
1905e35cf81dSBarry Smith    Options Database Keys:
1906e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1907e35cf81dSBarry Smith 
1908e35cf81dSBarry Smith    Notes:
1909e35cf81dSBarry Smith    The default is 1
1910e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1911e35cf81dSBarry Smith 
1912e35cf81dSBarry Smith    Level: intermediate
1913e35cf81dSBarry Smith 
1914e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1915e35cf81dSBarry Smith 
1916e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1917e35cf81dSBarry Smith 
1918e35cf81dSBarry Smith @*/
19197087cfbeSBarry Smith PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
1920e35cf81dSBarry Smith {
1921e35cf81dSBarry Smith   PetscFunctionBegin;
19220700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1923e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1924e35cf81dSBarry Smith   PetscFunctionReturn(0);
1925e35cf81dSBarry Smith }
1926e35cf81dSBarry Smith 
1927e35cf81dSBarry Smith #undef __FUNCT__
19284a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
19299b94acceSBarry Smith /*@
1930d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
19319b94acceSBarry Smith 
19323f9fe445SBarry Smith    Logically Collective on SNES
1933c7afd0dbSLois Curfman McInnes 
19349b94acceSBarry Smith    Input Parameters:
1935c7afd0dbSLois Curfman McInnes +  snes - the SNES context
193670441072SBarry Smith .  abstol - absolute convergence tolerance
193733174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
193833174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
193933174efeSLois Curfman McInnes            of the change in the solution between steps
194033174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1941c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1942fee21e36SBarry Smith 
194333174efeSLois Curfman McInnes    Options Database Keys:
194470441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1945c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1946c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1947c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1948c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
19499b94acceSBarry Smith 
1950d7a720efSLois Curfman McInnes    Notes:
19519b94acceSBarry Smith    The default maximum number of iterations is 50.
19529b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
19539b94acceSBarry Smith 
195436851e7fSLois Curfman McInnes    Level: intermediate
195536851e7fSLois Curfman McInnes 
195633174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
19579b94acceSBarry Smith 
19582492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
19599b94acceSBarry Smith @*/
19607087cfbeSBarry Smith PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
19619b94acceSBarry Smith {
19623a40ed3dSBarry Smith   PetscFunctionBegin;
19630700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1964c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
1965c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
1966c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
1967c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
1968c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
1969c5eb9154SBarry Smith 
1970ab54825eSJed Brown   if (abstol != PETSC_DEFAULT) {
1971ab54825eSJed Brown     if (abstol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %G must be non-negative",abstol);
1972ab54825eSJed Brown     snes->abstol = abstol;
1973ab54825eSJed Brown   }
1974ab54825eSJed Brown   if (rtol != PETSC_DEFAULT) {
1975ab54825eSJed Brown     if (rtol < 0.0 || 1.0 <= rtol) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Relative tolerance %G must be non-negative and less than 1.0",rtol);
1976ab54825eSJed Brown     snes->rtol = rtol;
1977ab54825eSJed Brown   }
1978ab54825eSJed Brown   if (stol != PETSC_DEFAULT) {
1979ab54825eSJed Brown     if (stol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %G must be non-negative",stol);
1980ab54825eSJed Brown     snes->xtol = stol;
1981ab54825eSJed Brown   }
1982ab54825eSJed Brown   if (maxit != PETSC_DEFAULT) {
1983ab54825eSJed Brown     if (maxit < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit);
1984ab54825eSJed Brown     snes->max_its = maxit;
1985ab54825eSJed Brown   }
1986ab54825eSJed Brown   if (maxf != PETSC_DEFAULT) {
1987ab54825eSJed Brown     if (maxf < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be non-negative",maxf);
1988ab54825eSJed Brown     snes->max_funcs = maxf;
1989ab54825eSJed Brown   }
19903a40ed3dSBarry Smith   PetscFunctionReturn(0);
19919b94acceSBarry Smith }
19929b94acceSBarry Smith 
19934a2ae208SSatish Balay #undef __FUNCT__
19944a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
19959b94acceSBarry Smith /*@
199633174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
199733174efeSLois Curfman McInnes 
1998c7afd0dbSLois Curfman McInnes    Not Collective
1999c7afd0dbSLois Curfman McInnes 
200033174efeSLois Curfman McInnes    Input Parameters:
2001c7afd0dbSLois Curfman McInnes +  snes - the SNES context
200285385478SLisandro Dalcin .  atol - absolute convergence tolerance
200333174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
200433174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
200533174efeSLois Curfman McInnes            of the change in the solution between steps
200633174efeSLois Curfman McInnes .  maxit - maximum number of iterations
2007c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
2008fee21e36SBarry Smith 
200933174efeSLois Curfman McInnes    Notes:
201033174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
201133174efeSLois Curfman McInnes 
201236851e7fSLois Curfman McInnes    Level: intermediate
201336851e7fSLois Curfman McInnes 
201433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
201533174efeSLois Curfman McInnes 
201633174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
201733174efeSLois Curfman McInnes @*/
20187087cfbeSBarry Smith PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
201933174efeSLois Curfman McInnes {
20203a40ed3dSBarry Smith   PetscFunctionBegin;
20210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
202285385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
202333174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
202433174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
202533174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
202633174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
20273a40ed3dSBarry Smith   PetscFunctionReturn(0);
202833174efeSLois Curfman McInnes }
202933174efeSLois Curfman McInnes 
20304a2ae208SSatish Balay #undef __FUNCT__
20314a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
203233174efeSLois Curfman McInnes /*@
20339b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
20349b94acceSBarry Smith 
20353f9fe445SBarry Smith    Logically Collective on SNES
2036fee21e36SBarry Smith 
2037c7afd0dbSLois Curfman McInnes    Input Parameters:
2038c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2039c7afd0dbSLois Curfman McInnes -  tol - tolerance
2040c7afd0dbSLois Curfman McInnes 
20419b94acceSBarry Smith    Options Database Key:
2042c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
20439b94acceSBarry Smith 
204436851e7fSLois Curfman McInnes    Level: intermediate
204536851e7fSLois Curfman McInnes 
20469b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
20479b94acceSBarry Smith 
20482492ecdbSBarry Smith .seealso: SNESSetTolerances()
20499b94acceSBarry Smith @*/
20507087cfbeSBarry Smith PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
20519b94acceSBarry Smith {
20523a40ed3dSBarry Smith   PetscFunctionBegin;
20530700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2054c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
20559b94acceSBarry Smith   snes->deltatol = tol;
20563a40ed3dSBarry Smith   PetscFunctionReturn(0);
20579b94acceSBarry Smith }
20589b94acceSBarry Smith 
2059df9fa365SBarry Smith /*
2060df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
2061df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
2062df9fa365SBarry Smith    macros instead of functions
2063df9fa365SBarry Smith */
20644a2ae208SSatish Balay #undef __FUNCT__
2065a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
20667087cfbeSBarry Smith PetscErrorCode  SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
2067ce1608b8SBarry Smith {
2068dfbe8321SBarry Smith   PetscErrorCode ierr;
2069ce1608b8SBarry Smith 
2070ce1608b8SBarry Smith   PetscFunctionBegin;
20710700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2072a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
2073ce1608b8SBarry Smith   PetscFunctionReturn(0);
2074ce1608b8SBarry Smith }
2075ce1608b8SBarry Smith 
20764a2ae208SSatish Balay #undef __FUNCT__
2077a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
20787087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2079df9fa365SBarry Smith {
2080dfbe8321SBarry Smith   PetscErrorCode ierr;
2081df9fa365SBarry Smith 
2082df9fa365SBarry Smith   PetscFunctionBegin;
2083a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2084df9fa365SBarry Smith   PetscFunctionReturn(0);
2085df9fa365SBarry Smith }
2086df9fa365SBarry Smith 
20874a2ae208SSatish Balay #undef __FUNCT__
2088a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
20896bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGDestroy(PetscDrawLG *draw)
2090df9fa365SBarry Smith {
2091dfbe8321SBarry Smith   PetscErrorCode ierr;
2092df9fa365SBarry Smith 
2093df9fa365SBarry Smith   PetscFunctionBegin;
2094a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2095df9fa365SBarry Smith   PetscFunctionReturn(0);
2096df9fa365SBarry Smith }
2097df9fa365SBarry Smith 
20987087cfbeSBarry Smith extern PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
2099b271bb04SBarry Smith #undef __FUNCT__
2100b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
21017087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
2102b271bb04SBarry Smith {
2103b271bb04SBarry Smith   PetscDrawLG      lg;
2104b271bb04SBarry Smith   PetscErrorCode   ierr;
2105b271bb04SBarry Smith   PetscReal        x,y,per;
2106b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
2107b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
2108b271bb04SBarry Smith   PetscDraw        draw;
2109b271bb04SBarry Smith   PetscFunctionBegin;
2110b271bb04SBarry Smith   if (!monctx) {
2111b271bb04SBarry Smith     MPI_Comm    comm;
2112b271bb04SBarry Smith 
2113b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
2114b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
2115b271bb04SBarry Smith   }
2116b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
2117b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2118b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2119b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
2120b271bb04SBarry Smith   x = (PetscReal) n;
2121b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
2122b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2123b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2124b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2125b271bb04SBarry Smith   }
2126b271bb04SBarry Smith 
2127b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
2128b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2129b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2130b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
2131b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
2132b271bb04SBarry Smith   x = (PetscReal) n;
2133b271bb04SBarry Smith   y = 100.0*per;
2134b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2135b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2136b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2137b271bb04SBarry Smith   }
2138b271bb04SBarry Smith 
2139b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
2140b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2141b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2142b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
2143b271bb04SBarry Smith   x = (PetscReal) n;
2144b271bb04SBarry Smith   y = (prev - rnorm)/prev;
2145b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2146b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2147b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2148b271bb04SBarry Smith   }
2149b271bb04SBarry Smith 
2150b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
2151b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2152b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2153b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
2154b271bb04SBarry Smith   x = (PetscReal) n;
2155b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
2156b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
2157b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2158b271bb04SBarry Smith   }
2159b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2160b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2161b271bb04SBarry Smith   }
2162b271bb04SBarry Smith   prev = rnorm;
2163b271bb04SBarry Smith   PetscFunctionReturn(0);
2164b271bb04SBarry Smith }
2165b271bb04SBarry Smith 
2166b271bb04SBarry Smith #undef __FUNCT__
2167b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
21687087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2169b271bb04SBarry Smith {
2170b271bb04SBarry Smith   PetscErrorCode ierr;
2171b271bb04SBarry Smith 
2172b271bb04SBarry Smith   PetscFunctionBegin;
2173b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2174b271bb04SBarry Smith   PetscFunctionReturn(0);
2175b271bb04SBarry Smith }
2176b271bb04SBarry Smith 
2177b271bb04SBarry Smith #undef __FUNCT__
2178b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
21796bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGRangeDestroy(PetscDrawLG *draw)
2180b271bb04SBarry Smith {
2181b271bb04SBarry Smith   PetscErrorCode ierr;
2182b271bb04SBarry Smith 
2183b271bb04SBarry Smith   PetscFunctionBegin;
2184b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2185b271bb04SBarry Smith   PetscFunctionReturn(0);
2186b271bb04SBarry Smith }
2187b271bb04SBarry Smith 
21887a03ce2fSLisandro Dalcin #undef __FUNCT__
21897a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor"
2190228d79bcSJed Brown /*@
2191228d79bcSJed Brown    SNESMonitor - runs the user provided monitor routines, if they exist
2192228d79bcSJed Brown 
2193228d79bcSJed Brown    Collective on SNES
2194228d79bcSJed Brown 
2195228d79bcSJed Brown    Input Parameters:
2196228d79bcSJed Brown +  snes - nonlinear solver context obtained from SNESCreate()
2197228d79bcSJed Brown .  iter - iteration number
2198228d79bcSJed Brown -  rnorm - relative norm of the residual
2199228d79bcSJed Brown 
2200228d79bcSJed Brown    Notes:
2201228d79bcSJed Brown    This routine is called by the SNES implementations.
2202228d79bcSJed Brown    It does not typically need to be called by the user.
2203228d79bcSJed Brown 
2204228d79bcSJed Brown    Level: developer
2205228d79bcSJed Brown 
2206228d79bcSJed Brown .seealso: SNESMonitorSet()
2207228d79bcSJed Brown @*/
22087a03ce2fSLisandro Dalcin PetscErrorCode  SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm)
22097a03ce2fSLisandro Dalcin {
22107a03ce2fSLisandro Dalcin   PetscErrorCode ierr;
22117a03ce2fSLisandro Dalcin   PetscInt       i,n = snes->numbermonitors;
22127a03ce2fSLisandro Dalcin 
22137a03ce2fSLisandro Dalcin   PetscFunctionBegin;
22147a03ce2fSLisandro Dalcin   for (i=0; i<n; i++) {
22157a03ce2fSLisandro Dalcin     ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr);
22167a03ce2fSLisandro Dalcin   }
22177a03ce2fSLisandro Dalcin   PetscFunctionReturn(0);
22187a03ce2fSLisandro Dalcin }
22197a03ce2fSLisandro Dalcin 
22209b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
22219b94acceSBarry Smith 
22224a2ae208SSatish Balay #undef __FUNCT__
2223a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
22249b94acceSBarry Smith /*@C
2225a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
22269b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
22279b94acceSBarry Smith    progress.
22289b94acceSBarry Smith 
22293f9fe445SBarry Smith    Logically Collective on SNES
2230fee21e36SBarry Smith 
2231c7afd0dbSLois Curfman McInnes    Input Parameters:
2232c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2233c7afd0dbSLois Curfman McInnes .  func - monitoring routine
2234b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
2235e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
2236b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2237b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
22389b94acceSBarry Smith 
2239c7afd0dbSLois Curfman McInnes    Calling sequence of func:
2240a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
2241c7afd0dbSLois Curfman McInnes 
2242c7afd0dbSLois Curfman McInnes +    snes - the SNES context
2243c7afd0dbSLois Curfman McInnes .    its - iteration number
2244c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
224540a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
22469b94acceSBarry Smith 
22479665c990SLois Curfman McInnes    Options Database Keys:
2248a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
2249a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
2250a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
2251cca6129bSJed Brown -    -snes_monitor_cancel - cancels all monitors that have
2252c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
2253a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
2254c7afd0dbSLois Curfman McInnes                             does not cancel those set via
2255c7afd0dbSLois Curfman McInnes                             the options database.
22569665c990SLois Curfman McInnes 
2257639f9d9dSBarry Smith    Notes:
22586bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
2259a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
22606bc08f3fSLois Curfman McInnes    order in which they were set.
2261639f9d9dSBarry Smith 
2262025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
2263025f1a04SBarry Smith 
226436851e7fSLois Curfman McInnes    Level: intermediate
226536851e7fSLois Curfman McInnes 
22669b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
22679b94acceSBarry Smith 
2268a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
22699b94acceSBarry Smith @*/
2270c2efdce3SBarry Smith PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
22719b94acceSBarry Smith {
2272b90d0a6eSBarry Smith   PetscInt       i;
2273649052a6SBarry Smith   PetscErrorCode ierr;
2274b90d0a6eSBarry Smith 
22753a40ed3dSBarry Smith   PetscFunctionBegin;
22760700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
227717186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2278b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
2279649052a6SBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) {
2280649052a6SBarry Smith       if (monitordestroy) {
2281c2efdce3SBarry Smith         ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr);
2282649052a6SBarry Smith       }
2283b90d0a6eSBarry Smith       PetscFunctionReturn(0);
2284b90d0a6eSBarry Smith     }
2285b90d0a6eSBarry Smith   }
2286b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
2287b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
2288639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
22893a40ed3dSBarry Smith   PetscFunctionReturn(0);
22909b94acceSBarry Smith }
22919b94acceSBarry Smith 
22924a2ae208SSatish Balay #undef __FUNCT__
2293a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
22945cd90555SBarry Smith /*@C
2295a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
22965cd90555SBarry Smith 
22973f9fe445SBarry Smith    Logically Collective on SNES
2298c7afd0dbSLois Curfman McInnes 
22995cd90555SBarry Smith    Input Parameters:
23005cd90555SBarry Smith .  snes - the SNES context
23015cd90555SBarry Smith 
23021a480d89SAdministrator    Options Database Key:
2303a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
2304a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
2305c7afd0dbSLois Curfman McInnes     set via the options database
23065cd90555SBarry Smith 
23075cd90555SBarry Smith    Notes:
23085cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
23095cd90555SBarry Smith 
231036851e7fSLois Curfman McInnes    Level: intermediate
231136851e7fSLois Curfman McInnes 
23125cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
23135cd90555SBarry Smith 
2314a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
23155cd90555SBarry Smith @*/
23167087cfbeSBarry Smith PetscErrorCode  SNESMonitorCancel(SNES snes)
23175cd90555SBarry Smith {
2318d952e501SBarry Smith   PetscErrorCode ierr;
2319d952e501SBarry Smith   PetscInt       i;
2320d952e501SBarry Smith 
23215cd90555SBarry Smith   PetscFunctionBegin;
23220700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2323d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2324d952e501SBarry Smith     if (snes->monitordestroy[i]) {
23253c4aec1bSBarry Smith       ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr);
2326d952e501SBarry Smith     }
2327d952e501SBarry Smith   }
23285cd90555SBarry Smith   snes->numbermonitors = 0;
23295cd90555SBarry Smith   PetscFunctionReturn(0);
23305cd90555SBarry Smith }
23315cd90555SBarry Smith 
23324a2ae208SSatish Balay #undef __FUNCT__
23334a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
23349b94acceSBarry Smith /*@C
23359b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
23369b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
23379b94acceSBarry Smith 
23383f9fe445SBarry Smith    Logically Collective on SNES
2339fee21e36SBarry Smith 
2340c7afd0dbSLois Curfman McInnes    Input Parameters:
2341c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2342c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
23437f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
23447f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
23459b94acceSBarry Smith 
2346c7afd0dbSLois Curfman McInnes    Calling sequence of func:
234706ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2348c7afd0dbSLois Curfman McInnes 
2349c7afd0dbSLois Curfman McInnes +    snes - the SNES context
235006ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2351c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2352184914b5SBarry Smith .    reason - reason for convergence/divergence
2353c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
23544b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
23554b27c08aSLois Curfman McInnes -    f - 2-norm of function
23569b94acceSBarry Smith 
235736851e7fSLois Curfman McInnes    Level: advanced
235836851e7fSLois Curfman McInnes 
23599b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
23609b94acceSBarry Smith 
236185385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
23629b94acceSBarry Smith @*/
23637087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
23649b94acceSBarry Smith {
23657f7931b9SBarry Smith   PetscErrorCode ierr;
23667f7931b9SBarry Smith 
23673a40ed3dSBarry Smith   PetscFunctionBegin;
23680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
236985385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
23707f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
23717f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
23727f7931b9SBarry Smith   }
237385385478SLisandro Dalcin   snes->ops->converged        = func;
23747f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
237585385478SLisandro Dalcin   snes->cnvP                  = cctx;
23763a40ed3dSBarry Smith   PetscFunctionReturn(0);
23779b94acceSBarry Smith }
23789b94acceSBarry Smith 
23794a2ae208SSatish Balay #undef __FUNCT__
23804a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
238152baeb72SSatish Balay /*@
2382184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2383184914b5SBarry Smith 
2384184914b5SBarry Smith    Not Collective
2385184914b5SBarry Smith 
2386184914b5SBarry Smith    Input Parameter:
2387184914b5SBarry Smith .  snes - the SNES context
2388184914b5SBarry Smith 
2389184914b5SBarry Smith    Output Parameter:
23904d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2391184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2392184914b5SBarry Smith 
2393184914b5SBarry Smith    Level: intermediate
2394184914b5SBarry Smith 
2395184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2396184914b5SBarry Smith 
2397184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2398184914b5SBarry Smith 
239985385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2400184914b5SBarry Smith @*/
24017087cfbeSBarry Smith PetscErrorCode  SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2402184914b5SBarry Smith {
2403184914b5SBarry Smith   PetscFunctionBegin;
24040700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24054482741eSBarry Smith   PetscValidPointer(reason,2);
2406184914b5SBarry Smith   *reason = snes->reason;
2407184914b5SBarry Smith   PetscFunctionReturn(0);
2408184914b5SBarry Smith }
2409184914b5SBarry Smith 
24104a2ae208SSatish Balay #undef __FUNCT__
24114a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2412c9005455SLois Curfman McInnes /*@
2413c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2414c9005455SLois Curfman McInnes 
24153f9fe445SBarry Smith    Logically Collective on SNES
2416fee21e36SBarry Smith 
2417c7afd0dbSLois Curfman McInnes    Input Parameters:
2418c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
24198c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2420cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2421758f92a0SBarry Smith .  na  - size of a and its
242264731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2423758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2424c7afd0dbSLois Curfman McInnes 
2425308dcc3eSBarry Smith    Notes:
2426308dcc3eSBarry Smith    If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2427308dcc3eSBarry Smith    default array of length 10000 is allocated.
2428308dcc3eSBarry Smith 
2429c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2430c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2431c9005455SLois Curfman McInnes    during the section of code that is being timed.
2432c9005455SLois Curfman McInnes 
243336851e7fSLois Curfman McInnes    Level: intermediate
243436851e7fSLois Curfman McInnes 
2435c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2436758f92a0SBarry Smith 
243708405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2438758f92a0SBarry Smith 
2439c9005455SLois Curfman McInnes @*/
24407087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool  reset)
2441c9005455SLois Curfman McInnes {
2442308dcc3eSBarry Smith   PetscErrorCode ierr;
2443308dcc3eSBarry Smith 
24443a40ed3dSBarry Smith   PetscFunctionBegin;
24450700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24464482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2447a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2448308dcc3eSBarry Smith   if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) {
2449308dcc3eSBarry Smith     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2450308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr);
2451308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr);
2452308dcc3eSBarry Smith     snes->conv_malloc   = PETSC_TRUE;
2453308dcc3eSBarry Smith   }
2454c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2455758f92a0SBarry Smith   snes->conv_hist_its   = its;
2456758f92a0SBarry Smith   snes->conv_hist_max   = na;
2457a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2458758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2459758f92a0SBarry Smith   PetscFunctionReturn(0);
2460758f92a0SBarry Smith }
2461758f92a0SBarry Smith 
2462308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2463c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
2464c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
2465308dcc3eSBarry Smith EXTERN_C_BEGIN
2466308dcc3eSBarry Smith #undef __FUNCT__
2467308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab"
2468308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
2469308dcc3eSBarry Smith {
2470308dcc3eSBarry Smith   mxArray        *mat;
2471308dcc3eSBarry Smith   PetscInt       i;
2472308dcc3eSBarry Smith   PetscReal      *ar;
2473308dcc3eSBarry Smith 
2474308dcc3eSBarry Smith   PetscFunctionBegin;
2475308dcc3eSBarry Smith   mat  = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
2476308dcc3eSBarry Smith   ar   = (PetscReal*) mxGetData(mat);
2477308dcc3eSBarry Smith   for (i=0; i<snes->conv_hist_len; i++) {
2478308dcc3eSBarry Smith     ar[i] = snes->conv_hist[i];
2479308dcc3eSBarry Smith   }
2480308dcc3eSBarry Smith   PetscFunctionReturn(mat);
2481308dcc3eSBarry Smith }
2482308dcc3eSBarry Smith EXTERN_C_END
2483308dcc3eSBarry Smith #endif
2484308dcc3eSBarry Smith 
2485308dcc3eSBarry Smith 
24864a2ae208SSatish Balay #undef __FUNCT__
24874a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
24880c4c9dddSBarry Smith /*@C
2489758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2490758f92a0SBarry Smith 
24913f9fe445SBarry Smith    Not Collective
2492758f92a0SBarry Smith 
2493758f92a0SBarry Smith    Input Parameter:
2494758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2495758f92a0SBarry Smith 
2496758f92a0SBarry Smith    Output Parameters:
2497758f92a0SBarry Smith .  a   - array to hold history
2498758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2499758f92a0SBarry Smith          negative if not converged) for each solve.
2500758f92a0SBarry Smith -  na  - size of a and its
2501758f92a0SBarry Smith 
2502758f92a0SBarry Smith    Notes:
2503758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2504758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2505758f92a0SBarry Smith 
2506758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2507758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2508758f92a0SBarry Smith    during the section of code that is being timed.
2509758f92a0SBarry Smith 
2510758f92a0SBarry Smith    Level: intermediate
2511758f92a0SBarry Smith 
2512758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2513758f92a0SBarry Smith 
2514758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2515758f92a0SBarry Smith 
2516758f92a0SBarry Smith @*/
25177087cfbeSBarry Smith PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2518758f92a0SBarry Smith {
2519758f92a0SBarry Smith   PetscFunctionBegin;
25200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2521758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2522758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2523758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
25243a40ed3dSBarry Smith   PetscFunctionReturn(0);
2525c9005455SLois Curfman McInnes }
2526c9005455SLois Curfman McInnes 
2527e74ef692SMatthew Knepley #undef __FUNCT__
2528e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2529ac226902SBarry Smith /*@C
253076b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
2531eec8f646SJed Brown   at the beginning of every iteration of the nonlinear solve. Specifically
25327e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
253376b2cf59SMatthew Knepley 
25343f9fe445SBarry Smith   Logically Collective on SNES
253576b2cf59SMatthew Knepley 
253676b2cf59SMatthew Knepley   Input Parameters:
253776b2cf59SMatthew Knepley . snes - The nonlinear solver context
253876b2cf59SMatthew Knepley . func - The function
253976b2cf59SMatthew Knepley 
254076b2cf59SMatthew Knepley   Calling sequence of func:
2541b5d30489SBarry Smith . func (SNES snes, PetscInt step);
254276b2cf59SMatthew Knepley 
254376b2cf59SMatthew Knepley . step - The current step of the iteration
254476b2cf59SMatthew Knepley 
2545fe97e370SBarry Smith   Level: advanced
2546fe97e370SBarry Smith 
2547fe97e370SBarry 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()
2548fe97e370SBarry Smith         This is not used by most users.
254976b2cf59SMatthew Knepley 
255076b2cf59SMatthew Knepley .keywords: SNES, update
2551b5d30489SBarry Smith 
255285385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
255376b2cf59SMatthew Knepley @*/
25547087cfbeSBarry Smith PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
255576b2cf59SMatthew Knepley {
255676b2cf59SMatthew Knepley   PetscFunctionBegin;
25570700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2558e7788613SBarry Smith   snes->ops->update = func;
255976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
256076b2cf59SMatthew Knepley }
256176b2cf59SMatthew Knepley 
2562e74ef692SMatthew Knepley #undef __FUNCT__
2563e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
256476b2cf59SMatthew Knepley /*@
256576b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
256676b2cf59SMatthew Knepley 
256776b2cf59SMatthew Knepley   Not collective
256876b2cf59SMatthew Knepley 
256976b2cf59SMatthew Knepley   Input Parameters:
257076b2cf59SMatthew Knepley . snes - The nonlinear solver context
257176b2cf59SMatthew Knepley . step - The current step of the iteration
257276b2cf59SMatthew Knepley 
2573205452f4SMatthew Knepley   Level: intermediate
2574205452f4SMatthew Knepley 
257576b2cf59SMatthew Knepley .keywords: SNES, update
2576a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
257776b2cf59SMatthew Knepley @*/
25787087cfbeSBarry Smith PetscErrorCode  SNESDefaultUpdate(SNES snes, PetscInt step)
257976b2cf59SMatthew Knepley {
258076b2cf59SMatthew Knepley   PetscFunctionBegin;
258176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
258276b2cf59SMatthew Knepley }
258376b2cf59SMatthew Knepley 
25844a2ae208SSatish Balay #undef __FUNCT__
25854a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
25869b94acceSBarry Smith /*
25879b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
25889b94acceSBarry Smith    positive parameter delta.
25899b94acceSBarry Smith 
25909b94acceSBarry Smith     Input Parameters:
2591c7afd0dbSLois Curfman McInnes +   snes - the SNES context
25929b94acceSBarry Smith .   y - approximate solution of linear system
25939b94acceSBarry Smith .   fnorm - 2-norm of current function
2594c7afd0dbSLois Curfman McInnes -   delta - trust region size
25959b94acceSBarry Smith 
25969b94acceSBarry Smith     Output Parameters:
2597c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
25989b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
25999b94acceSBarry Smith     region, and exceeds zero otherwise.
2600c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
26019b94acceSBarry Smith 
26029b94acceSBarry Smith     Note:
26034b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
26049b94acceSBarry Smith     is set to be the maximum allowable step size.
26059b94acceSBarry Smith 
26069b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
26079b94acceSBarry Smith */
2608dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
26099b94acceSBarry Smith {
2610064f8208SBarry Smith   PetscReal      nrm;
2611ea709b57SSatish Balay   PetscScalar    cnorm;
2612dfbe8321SBarry Smith   PetscErrorCode ierr;
26133a40ed3dSBarry Smith 
26143a40ed3dSBarry Smith   PetscFunctionBegin;
26150700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26160700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2617c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2618184914b5SBarry Smith 
2619064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2620064f8208SBarry Smith   if (nrm > *delta) {
2621064f8208SBarry Smith      nrm = *delta/nrm;
2622064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2623064f8208SBarry Smith      cnorm = nrm;
26242dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
26259b94acceSBarry Smith      *ynorm = *delta;
26269b94acceSBarry Smith   } else {
26279b94acceSBarry Smith      *gpnorm = 0.0;
2628064f8208SBarry Smith      *ynorm = nrm;
26299b94acceSBarry Smith   }
26303a40ed3dSBarry Smith   PetscFunctionReturn(0);
26319b94acceSBarry Smith }
26329b94acceSBarry Smith 
26334a2ae208SSatish Balay #undef __FUNCT__
26344a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
26356ce558aeSBarry Smith /*@C
2636f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2637f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
26389b94acceSBarry Smith 
2639c7afd0dbSLois Curfman McInnes    Collective on SNES
2640c7afd0dbSLois Curfman McInnes 
2641b2002411SLois Curfman McInnes    Input Parameters:
2642c7afd0dbSLois Curfman McInnes +  snes - the SNES context
26433cebf274SJed Brown .  b - the constant part of the equation F(x) = b, or PETSC_NULL to use zero.
264485385478SLisandro Dalcin -  x - the solution vector.
26459b94acceSBarry Smith 
2646b2002411SLois Curfman McInnes    Notes:
26478ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
26488ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
26498ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
26508ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
26518ddd3da0SLois Curfman McInnes 
265236851e7fSLois Curfman McInnes    Level: beginner
265336851e7fSLois Curfman McInnes 
26549b94acceSBarry Smith .keywords: SNES, nonlinear, solve
26559b94acceSBarry Smith 
265685385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
26579b94acceSBarry Smith @*/
26587087cfbeSBarry Smith PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
26599b94acceSBarry Smith {
2660dfbe8321SBarry Smith   PetscErrorCode ierr;
2661ace3abfcSBarry Smith   PetscBool      flg;
2662eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2663eabae89aSBarry Smith   PetscViewer    viewer;
2664efd51863SBarry Smith   PetscInt       grid;
2665052efed2SBarry Smith 
26663a40ed3dSBarry Smith   PetscFunctionBegin;
26670700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26680700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2669f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
26700700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
267185385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
267285385478SLisandro Dalcin 
2673a8248277SBarry Smith   for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);}
2674efd51863SBarry Smith   for (grid=0; grid<snes->gridsequence+1; grid++) {
2675efd51863SBarry Smith 
267685385478SLisandro Dalcin     /* set solution vector */
2677efd51863SBarry Smith     if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);}
26786bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
267985385478SLisandro Dalcin     snes->vec_sol = x;
268085385478SLisandro Dalcin     /* set afine vector if provided */
268185385478SLisandro Dalcin     if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
26826bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
268385385478SLisandro Dalcin     snes->vec_rhs = b;
268485385478SLisandro Dalcin 
268570e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
26863f149594SLisandro Dalcin 
2687d25893d9SBarry Smith     if (!grid && snes->ops->computeinitialguess) {
2688d25893d9SBarry Smith       ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr);
2689d25893d9SBarry Smith     }
2690d25893d9SBarry Smith 
2691abc0a331SBarry Smith     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
269250ffb88aSMatthew Knepley     snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2693d5e45103SBarry Smith 
26943f149594SLisandro Dalcin     ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
26954936397dSBarry Smith     ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
269685385478SLisandro Dalcin     ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
26974936397dSBarry Smith     if (snes->domainerror){
26984936397dSBarry Smith       snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
26994936397dSBarry Smith       snes->domainerror = PETSC_FALSE;
27004936397dSBarry Smith     }
270117186662SBarry Smith     if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
27023f149594SLisandro Dalcin 
27037adad957SLisandro Dalcin     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2704eabae89aSBarry Smith     if (flg && !PetscPreLoadingOn) {
27057adad957SLisandro Dalcin       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2706eabae89aSBarry Smith       ierr = SNESView(snes,viewer);CHKERRQ(ierr);
27076bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2708eabae89aSBarry Smith     }
2709eabae89aSBarry Smith 
271090d69ab7SBarry Smith     flg  = PETSC_FALSE;
2711acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2712da9b6338SBarry Smith     if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
27135968eb51SBarry Smith     if (snes->printreason) {
2714a8248277SBarry Smith       ierr = PetscViewerASCIIAddTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr);
27155968eb51SBarry Smith       if (snes->reason > 0) {
2716a8248277SBarry Smith         ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
27175968eb51SBarry Smith       } else {
2718a8248277SBarry Smith         ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
27195968eb51SBarry Smith       }
2720a8248277SBarry Smith       ierr = PetscViewerASCIISubtractTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr);
27215968eb51SBarry Smith     }
27225968eb51SBarry Smith 
2723e113a28aSBarry Smith     if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
2724efd51863SBarry Smith     if (grid <  snes->gridsequence) {
2725efd51863SBarry Smith       DM  fine;
2726efd51863SBarry Smith       Vec xnew;
2727efd51863SBarry Smith       Mat interp;
2728efd51863SBarry Smith 
2729efd51863SBarry Smith       ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr);
2730efd51863SBarry Smith       ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr);
2731efd51863SBarry Smith       ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr);
2732efd51863SBarry Smith       ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr);
2733efd51863SBarry Smith       ierr = MatDestroy(&interp);CHKERRQ(ierr);
2734efd51863SBarry Smith       x    = xnew;
2735efd51863SBarry Smith 
2736efd51863SBarry Smith       ierr = SNESReset(snes);CHKERRQ(ierr);
2737efd51863SBarry Smith       ierr = SNESSetDM(snes,fine);CHKERRQ(ierr);
2738efd51863SBarry Smith       ierr = DMDestroy(&fine);CHKERRQ(ierr);
2739a8248277SBarry Smith       ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);
2740efd51863SBarry Smith     }
2741efd51863SBarry Smith   }
27423a40ed3dSBarry Smith   PetscFunctionReturn(0);
27439b94acceSBarry Smith }
27449b94acceSBarry Smith 
27459b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
27469b94acceSBarry Smith 
27474a2ae208SSatish Balay #undef __FUNCT__
27484a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
274982bf6240SBarry Smith /*@C
27504b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
27519b94acceSBarry Smith 
2752fee21e36SBarry Smith    Collective on SNES
2753fee21e36SBarry Smith 
2754c7afd0dbSLois Curfman McInnes    Input Parameters:
2755c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2756454a90a3SBarry Smith -  type - a known method
2757c7afd0dbSLois Curfman McInnes 
2758c7afd0dbSLois Curfman McInnes    Options Database Key:
2759454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2760c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2761ae12b187SLois Curfman McInnes 
27629b94acceSBarry Smith    Notes:
2763e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
27644b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2765c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
27664b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2767c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
27689b94acceSBarry Smith 
2769ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2770ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2771ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2772ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2773ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2774ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2775ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2776ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2777ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2778b0a32e0cSBarry Smith   appropriate method.
277936851e7fSLois Curfman McInnes 
278036851e7fSLois Curfman McInnes   Level: intermediate
2781a703fe33SLois Curfman McInnes 
2782454a90a3SBarry Smith .keywords: SNES, set, type
2783435da068SBarry Smith 
2784435da068SBarry Smith .seealso: SNESType, SNESCreate()
2785435da068SBarry Smith 
27869b94acceSBarry Smith @*/
27877087cfbeSBarry Smith PetscErrorCode  SNESSetType(SNES snes,const SNESType type)
27889b94acceSBarry Smith {
2789dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
2790ace3abfcSBarry Smith   PetscBool      match;
27913a40ed3dSBarry Smith 
27923a40ed3dSBarry Smith   PetscFunctionBegin;
27930700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27944482741eSBarry Smith   PetscValidCharPointer(type,2);
279582bf6240SBarry Smith 
27966831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
27970f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
279892ff6ae8SBarry Smith 
27994b91b6eaSBarry Smith   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2800e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
280175396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
280275396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
280375396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
280475396ef9SLisandro Dalcin   snes->ops->setup          = 0;
280575396ef9SLisandro Dalcin   snes->ops->solve          = 0;
280675396ef9SLisandro Dalcin   snes->ops->view           = 0;
280775396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
280875396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
280975396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
281075396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
2811454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
281203bfa161SLisandro Dalcin   ierr = (*r)(snes);CHKERRQ(ierr);
28139fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
28149fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
28159fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr);
28169fb22e1aSBarry Smith   }
28179fb22e1aSBarry Smith #endif
28183a40ed3dSBarry Smith   PetscFunctionReturn(0);
28199b94acceSBarry Smith }
28209b94acceSBarry Smith 
2821a847f771SSatish Balay 
28229b94acceSBarry Smith /* --------------------------------------------------------------------- */
28234a2ae208SSatish Balay #undef __FUNCT__
28244a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
282552baeb72SSatish Balay /*@
28269b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2827f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
28289b94acceSBarry Smith 
2829fee21e36SBarry Smith    Not Collective
2830fee21e36SBarry Smith 
283136851e7fSLois Curfman McInnes    Level: advanced
283236851e7fSLois Curfman McInnes 
28339b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
28349b94acceSBarry Smith 
28359b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
28369b94acceSBarry Smith @*/
28377087cfbeSBarry Smith PetscErrorCode  SNESRegisterDestroy(void)
28389b94acceSBarry Smith {
2839dfbe8321SBarry Smith   PetscErrorCode ierr;
284082bf6240SBarry Smith 
28413a40ed3dSBarry Smith   PetscFunctionBegin;
28421441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
28434c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
28443a40ed3dSBarry Smith   PetscFunctionReturn(0);
28459b94acceSBarry Smith }
28469b94acceSBarry Smith 
28474a2ae208SSatish Balay #undef __FUNCT__
28484a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
28499b94acceSBarry Smith /*@C
28509a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
28519b94acceSBarry Smith 
2852c7afd0dbSLois Curfman McInnes    Not Collective
2853c7afd0dbSLois Curfman McInnes 
28549b94acceSBarry Smith    Input Parameter:
28554b0e389bSBarry Smith .  snes - nonlinear solver context
28569b94acceSBarry Smith 
28579b94acceSBarry Smith    Output Parameter:
28583a7fca6bSBarry Smith .  type - SNES method (a character string)
28599b94acceSBarry Smith 
286036851e7fSLois Curfman McInnes    Level: intermediate
286136851e7fSLois Curfman McInnes 
2862454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
28639b94acceSBarry Smith @*/
28647087cfbeSBarry Smith PetscErrorCode  SNESGetType(SNES snes,const SNESType *type)
28659b94acceSBarry Smith {
28663a40ed3dSBarry Smith   PetscFunctionBegin;
28670700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
28684482741eSBarry Smith   PetscValidPointer(type,2);
28697adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
28703a40ed3dSBarry Smith   PetscFunctionReturn(0);
28719b94acceSBarry Smith }
28729b94acceSBarry Smith 
28734a2ae208SSatish Balay #undef __FUNCT__
28744a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
287552baeb72SSatish Balay /*@
28769b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
28779b94acceSBarry Smith    stored.
28789b94acceSBarry Smith 
2879c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2880c7afd0dbSLois Curfman McInnes 
28819b94acceSBarry Smith    Input Parameter:
28829b94acceSBarry Smith .  snes - the SNES context
28839b94acceSBarry Smith 
28849b94acceSBarry Smith    Output Parameter:
28859b94acceSBarry Smith .  x - the solution
28869b94acceSBarry Smith 
288770e92668SMatthew Knepley    Level: intermediate
288836851e7fSLois Curfman McInnes 
28899b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
28909b94acceSBarry Smith 
289185385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
28929b94acceSBarry Smith @*/
28937087cfbeSBarry Smith PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
28949b94acceSBarry Smith {
28953a40ed3dSBarry Smith   PetscFunctionBegin;
28960700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
28974482741eSBarry Smith   PetscValidPointer(x,2);
289885385478SLisandro Dalcin   *x = snes->vec_sol;
289970e92668SMatthew Knepley   PetscFunctionReturn(0);
290070e92668SMatthew Knepley }
290170e92668SMatthew Knepley 
290270e92668SMatthew Knepley #undef __FUNCT__
29034a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
290452baeb72SSatish Balay /*@
29059b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
29069b94acceSBarry Smith    stored.
29079b94acceSBarry Smith 
2908c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2909c7afd0dbSLois Curfman McInnes 
29109b94acceSBarry Smith    Input Parameter:
29119b94acceSBarry Smith .  snes - the SNES context
29129b94acceSBarry Smith 
29139b94acceSBarry Smith    Output Parameter:
29149b94acceSBarry Smith .  x - the solution update
29159b94acceSBarry Smith 
291636851e7fSLois Curfman McInnes    Level: advanced
291736851e7fSLois Curfman McInnes 
29189b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
29199b94acceSBarry Smith 
292085385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
29219b94acceSBarry Smith @*/
29227087cfbeSBarry Smith PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
29239b94acceSBarry Smith {
29243a40ed3dSBarry Smith   PetscFunctionBegin;
29250700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
29264482741eSBarry Smith   PetscValidPointer(x,2);
292785385478SLisandro Dalcin   *x = snes->vec_sol_update;
29283a40ed3dSBarry Smith   PetscFunctionReturn(0);
29299b94acceSBarry Smith }
29309b94acceSBarry Smith 
29314a2ae208SSatish Balay #undef __FUNCT__
29324a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
29339b94acceSBarry Smith /*@C
29343638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
29359b94acceSBarry Smith 
2936c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2937c7afd0dbSLois Curfman McInnes 
29389b94acceSBarry Smith    Input Parameter:
29399b94acceSBarry Smith .  snes - the SNES context
29409b94acceSBarry Smith 
29419b94acceSBarry Smith    Output Parameter:
29427bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
294370e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
294470e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
29459b94acceSBarry Smith 
294636851e7fSLois Curfman McInnes    Level: advanced
294736851e7fSLois Curfman McInnes 
2948a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
29499b94acceSBarry Smith 
29504b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
29519b94acceSBarry Smith @*/
29527087cfbeSBarry Smith PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
29539b94acceSBarry Smith {
29543a40ed3dSBarry Smith   PetscFunctionBegin;
29550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
295685385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2957e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
295870e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
29593a40ed3dSBarry Smith   PetscFunctionReturn(0);
29609b94acceSBarry Smith }
29619b94acceSBarry Smith 
29624a2ae208SSatish Balay #undef __FUNCT__
29634a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
29643c7409f5SSatish Balay /*@C
29653c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2966d850072dSLois Curfman McInnes    SNES options in the database.
29673c7409f5SSatish Balay 
29683f9fe445SBarry Smith    Logically Collective on SNES
2969fee21e36SBarry Smith 
2970c7afd0dbSLois Curfman McInnes    Input Parameter:
2971c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2972c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2973c7afd0dbSLois Curfman McInnes 
2974d850072dSLois Curfman McInnes    Notes:
2975a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2976c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2977d850072dSLois Curfman McInnes 
297836851e7fSLois Curfman McInnes    Level: advanced
297936851e7fSLois Curfman McInnes 
29803c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2981a86d99e1SLois Curfman McInnes 
2982a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
29833c7409f5SSatish Balay @*/
29847087cfbeSBarry Smith PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
29853c7409f5SSatish Balay {
2986dfbe8321SBarry Smith   PetscErrorCode ierr;
29873c7409f5SSatish Balay 
29883a40ed3dSBarry Smith   PetscFunctionBegin;
29890700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2990639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
29911cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
299294b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
29933a40ed3dSBarry Smith   PetscFunctionReturn(0);
29943c7409f5SSatish Balay }
29953c7409f5SSatish Balay 
29964a2ae208SSatish Balay #undef __FUNCT__
29974a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
29983c7409f5SSatish Balay /*@C
2999f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
3000d850072dSLois Curfman McInnes    SNES options in the database.
30013c7409f5SSatish Balay 
30023f9fe445SBarry Smith    Logically Collective on SNES
3003fee21e36SBarry Smith 
3004c7afd0dbSLois Curfman McInnes    Input Parameters:
3005c7afd0dbSLois Curfman McInnes +  snes - the SNES context
3006c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
3007c7afd0dbSLois Curfman McInnes 
3008d850072dSLois Curfman McInnes    Notes:
3009a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
3010c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
3011d850072dSLois Curfman McInnes 
301236851e7fSLois Curfman McInnes    Level: advanced
301336851e7fSLois Curfman McInnes 
30143c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
3015a86d99e1SLois Curfman McInnes 
3016a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
30173c7409f5SSatish Balay @*/
30187087cfbeSBarry Smith PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
30193c7409f5SSatish Balay {
3020dfbe8321SBarry Smith   PetscErrorCode ierr;
30213c7409f5SSatish Balay 
30223a40ed3dSBarry Smith   PetscFunctionBegin;
30230700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3024639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
30251cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
302694b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
30273a40ed3dSBarry Smith   PetscFunctionReturn(0);
30283c7409f5SSatish Balay }
30293c7409f5SSatish Balay 
30304a2ae208SSatish Balay #undef __FUNCT__
30314a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
30329ab63eb5SSatish Balay /*@C
30333c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
30343c7409f5SSatish Balay    SNES options in the database.
30353c7409f5SSatish Balay 
3036c7afd0dbSLois Curfman McInnes    Not Collective
3037c7afd0dbSLois Curfman McInnes 
30383c7409f5SSatish Balay    Input Parameter:
30393c7409f5SSatish Balay .  snes - the SNES context
30403c7409f5SSatish Balay 
30413c7409f5SSatish Balay    Output Parameter:
30423c7409f5SSatish Balay .  prefix - pointer to the prefix string used
30433c7409f5SSatish Balay 
30444ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
30459ab63eb5SSatish Balay    sufficient length to hold the prefix.
30469ab63eb5SSatish Balay 
304736851e7fSLois Curfman McInnes    Level: advanced
304836851e7fSLois Curfman McInnes 
30493c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
3050a86d99e1SLois Curfman McInnes 
3051a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
30523c7409f5SSatish Balay @*/
30537087cfbeSBarry Smith PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
30543c7409f5SSatish Balay {
3055dfbe8321SBarry Smith   PetscErrorCode ierr;
30563c7409f5SSatish Balay 
30573a40ed3dSBarry Smith   PetscFunctionBegin;
30580700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3059639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
30603a40ed3dSBarry Smith   PetscFunctionReturn(0);
30613c7409f5SSatish Balay }
30623c7409f5SSatish Balay 
3063b2002411SLois Curfman McInnes 
30644a2ae208SSatish Balay #undef __FUNCT__
30654a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
30663cea93caSBarry Smith /*@C
30673cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
30683cea93caSBarry Smith 
30697f6c08e0SMatthew Knepley   Level: advanced
30703cea93caSBarry Smith @*/
30717087cfbeSBarry Smith PetscErrorCode  SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
3072b2002411SLois Curfman McInnes {
3073e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
3074dfbe8321SBarry Smith   PetscErrorCode ierr;
3075b2002411SLois Curfman McInnes 
3076b2002411SLois Curfman McInnes   PetscFunctionBegin;
3077b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
3078c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
3079b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
3080b2002411SLois Curfman McInnes }
3081da9b6338SBarry Smith 
3082da9b6338SBarry Smith #undef __FUNCT__
3083da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
30847087cfbeSBarry Smith PetscErrorCode  SNESTestLocalMin(SNES snes)
3085da9b6338SBarry Smith {
3086dfbe8321SBarry Smith   PetscErrorCode ierr;
308777431f27SBarry Smith   PetscInt       N,i,j;
3088da9b6338SBarry Smith   Vec            u,uh,fh;
3089da9b6338SBarry Smith   PetscScalar    value;
3090da9b6338SBarry Smith   PetscReal      norm;
3091da9b6338SBarry Smith 
3092da9b6338SBarry Smith   PetscFunctionBegin;
3093da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
3094da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
3095da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
3096da9b6338SBarry Smith 
3097da9b6338SBarry Smith   /* currently only works for sequential */
3098da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
3099da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
3100da9b6338SBarry Smith   for (i=0; i<N; i++) {
3101da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
310277431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
3103da9b6338SBarry Smith     for (j=-10; j<11; j++) {
3104ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
3105da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
31063ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
3107da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
310877431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
3109da9b6338SBarry Smith       value = -value;
3110da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
3111da9b6338SBarry Smith     }
3112da9b6338SBarry Smith   }
31136bf464f9SBarry Smith   ierr = VecDestroy(&uh);CHKERRQ(ierr);
31146bf464f9SBarry Smith   ierr = VecDestroy(&fh);CHKERRQ(ierr);
3115da9b6338SBarry Smith   PetscFunctionReturn(0);
3116da9b6338SBarry Smith }
311771f87433Sdalcinl 
311871f87433Sdalcinl #undef __FUNCT__
3119fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
312071f87433Sdalcinl /*@
3121fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
312271f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
312371f87433Sdalcinl    Newton method.
312471f87433Sdalcinl 
31253f9fe445SBarry Smith    Logically Collective on SNES
312671f87433Sdalcinl 
312771f87433Sdalcinl    Input Parameters:
312871f87433Sdalcinl +  snes - SNES context
312971f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
313071f87433Sdalcinl 
313164ba62caSBarry Smith     Options Database:
313264ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
313364ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
313464ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
313564ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
313664ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
313764ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
313864ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
313964ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
314064ba62caSBarry Smith 
314171f87433Sdalcinl    Notes:
314271f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
314371f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
314471f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
314571f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
314671f87433Sdalcinl    solver.
314771f87433Sdalcinl 
314871f87433Sdalcinl    Level: advanced
314971f87433Sdalcinl 
315071f87433Sdalcinl    Reference:
315171f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
315271f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
315371f87433Sdalcinl 
315471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
315571f87433Sdalcinl 
3156fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
315771f87433Sdalcinl @*/
31587087cfbeSBarry Smith PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool  flag)
315971f87433Sdalcinl {
316071f87433Sdalcinl   PetscFunctionBegin;
31610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3162acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flag,2);
316371f87433Sdalcinl   snes->ksp_ewconv = flag;
316471f87433Sdalcinl   PetscFunctionReturn(0);
316571f87433Sdalcinl }
316671f87433Sdalcinl 
316771f87433Sdalcinl #undef __FUNCT__
3168fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
316971f87433Sdalcinl /*@
3170fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
317171f87433Sdalcinl    for computing relative tolerance for linear solvers within an
317271f87433Sdalcinl    inexact Newton method.
317371f87433Sdalcinl 
317471f87433Sdalcinl    Not Collective
317571f87433Sdalcinl 
317671f87433Sdalcinl    Input Parameter:
317771f87433Sdalcinl .  snes - SNES context
317871f87433Sdalcinl 
317971f87433Sdalcinl    Output Parameter:
318071f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
318171f87433Sdalcinl 
318271f87433Sdalcinl    Notes:
318371f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
318471f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
318571f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
318671f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
318771f87433Sdalcinl    solver.
318871f87433Sdalcinl 
318971f87433Sdalcinl    Level: advanced
319071f87433Sdalcinl 
319171f87433Sdalcinl    Reference:
319271f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
319371f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
319471f87433Sdalcinl 
319571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
319671f87433Sdalcinl 
3197fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
319871f87433Sdalcinl @*/
31997087cfbeSBarry Smith PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
320071f87433Sdalcinl {
320171f87433Sdalcinl   PetscFunctionBegin;
32020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
320371f87433Sdalcinl   PetscValidPointer(flag,2);
320471f87433Sdalcinl   *flag = snes->ksp_ewconv;
320571f87433Sdalcinl   PetscFunctionReturn(0);
320671f87433Sdalcinl }
320771f87433Sdalcinl 
320871f87433Sdalcinl #undef __FUNCT__
3209fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
321071f87433Sdalcinl /*@
3211fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
321271f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
321371f87433Sdalcinl    Newton method.
321471f87433Sdalcinl 
32153f9fe445SBarry Smith    Logically Collective on SNES
321671f87433Sdalcinl 
321771f87433Sdalcinl    Input Parameters:
321871f87433Sdalcinl +    snes - SNES context
321971f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
322071f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
322171f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
322271f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
322371f87433Sdalcinl              (0 <= gamma2 <= 1)
322471f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
322571f87433Sdalcinl .    alpha2 - power for safeguard
322671f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
322771f87433Sdalcinl 
322871f87433Sdalcinl    Note:
322971f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
323071f87433Sdalcinl 
323171f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
323271f87433Sdalcinl 
323371f87433Sdalcinl    Level: advanced
323471f87433Sdalcinl 
323571f87433Sdalcinl    Reference:
323671f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
323771f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
323871f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
323971f87433Sdalcinl 
324071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
324171f87433Sdalcinl 
3242fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
324371f87433Sdalcinl @*/
32447087cfbeSBarry Smith PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
324571f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
324671f87433Sdalcinl {
3247fa9f3622SBarry Smith   SNESKSPEW *kctx;
324871f87433Sdalcinl   PetscFunctionBegin;
32490700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3250fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3251e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
3252c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
3253c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
3254c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
3255c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
3256c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
3257c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
3258c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
325971f87433Sdalcinl 
326071f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
326171f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
326271f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
326371f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
326471f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
326571f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
326671f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
326771f87433Sdalcinl 
326871f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
3269e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
327071f87433Sdalcinl   }
327171f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
3272e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
327371f87433Sdalcinl   }
327471f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
3275e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
327671f87433Sdalcinl   }
327771f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
3278e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
327971f87433Sdalcinl   }
328071f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
3281e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
328271f87433Sdalcinl   }
328371f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
3284e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
328571f87433Sdalcinl   }
328671f87433Sdalcinl   PetscFunctionReturn(0);
328771f87433Sdalcinl }
328871f87433Sdalcinl 
328971f87433Sdalcinl #undef __FUNCT__
3290fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
329171f87433Sdalcinl /*@
3292fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
329371f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
329471f87433Sdalcinl    Newton method.
329571f87433Sdalcinl 
329671f87433Sdalcinl    Not Collective
329771f87433Sdalcinl 
329871f87433Sdalcinl    Input Parameters:
329971f87433Sdalcinl      snes - SNES context
330071f87433Sdalcinl 
330171f87433Sdalcinl    Output Parameters:
330271f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
330371f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
330471f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
330571f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
330671f87433Sdalcinl              (0 <= gamma2 <= 1)
330771f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
330871f87433Sdalcinl .    alpha2 - power for safeguard
330971f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
331071f87433Sdalcinl 
331171f87433Sdalcinl    Level: advanced
331271f87433Sdalcinl 
331371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
331471f87433Sdalcinl 
3315fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
331671f87433Sdalcinl @*/
33177087cfbeSBarry Smith PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
331871f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
331971f87433Sdalcinl {
3320fa9f3622SBarry Smith   SNESKSPEW *kctx;
332171f87433Sdalcinl   PetscFunctionBegin;
33220700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3323fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3324e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
332571f87433Sdalcinl   if(version)   *version   = kctx->version;
332671f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
332771f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
332871f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
332971f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
333071f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
333171f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
333271f87433Sdalcinl   PetscFunctionReturn(0);
333371f87433Sdalcinl }
333471f87433Sdalcinl 
333571f87433Sdalcinl #undef __FUNCT__
3336fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
3337fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
333871f87433Sdalcinl {
333971f87433Sdalcinl   PetscErrorCode ierr;
3340fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
334171f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
334271f87433Sdalcinl 
334371f87433Sdalcinl   PetscFunctionBegin;
3344e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
334571f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
334671f87433Sdalcinl     rtol = kctx->rtol_0;
334771f87433Sdalcinl   } else {
334871f87433Sdalcinl     if (kctx->version == 1) {
334971f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
335071f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
335171f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
335271f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
335371f87433Sdalcinl     } else if (kctx->version == 2) {
335471f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
335571f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
335671f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
335771f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
335871f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
335971f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
336071f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
336171f87433Sdalcinl       stol = PetscMax(rtol,stol);
336271f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
336371f87433Sdalcinl       /* safeguard: avoid oversolving */
336471f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
336571f87433Sdalcinl       stol = PetscMax(rtol,stol);
336671f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
3367e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
336871f87433Sdalcinl   }
336971f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
337071f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
337171f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
337271f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
337371f87433Sdalcinl   PetscFunctionReturn(0);
337471f87433Sdalcinl }
337571f87433Sdalcinl 
337671f87433Sdalcinl #undef __FUNCT__
3377fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
3378fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
337971f87433Sdalcinl {
338071f87433Sdalcinl   PetscErrorCode ierr;
3381fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
338271f87433Sdalcinl   PCSide         pcside;
338371f87433Sdalcinl   Vec            lres;
338471f87433Sdalcinl 
338571f87433Sdalcinl   PetscFunctionBegin;
3386e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
338771f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
338871f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
338971f87433Sdalcinl   if (kctx->version == 1) {
3390b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
339171f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
339271f87433Sdalcinl       /* KSP residual is true linear residual */
339371f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
339471f87433Sdalcinl     } else {
339571f87433Sdalcinl       /* KSP residual is preconditioned residual */
339671f87433Sdalcinl       /* compute true linear residual norm */
339771f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
339871f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
339971f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
340071f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
34016bf464f9SBarry Smith       ierr = VecDestroy(&lres);CHKERRQ(ierr);
340271f87433Sdalcinl     }
340371f87433Sdalcinl   }
340471f87433Sdalcinl   PetscFunctionReturn(0);
340571f87433Sdalcinl }
340671f87433Sdalcinl 
340771f87433Sdalcinl #undef __FUNCT__
340871f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
340971f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
341071f87433Sdalcinl {
341171f87433Sdalcinl   PetscErrorCode ierr;
341271f87433Sdalcinl 
341371f87433Sdalcinl   PetscFunctionBegin;
3414fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
341571f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3416fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
341771f87433Sdalcinl   PetscFunctionReturn(0);
341871f87433Sdalcinl }
34196c699258SBarry Smith 
34206c699258SBarry Smith #undef __FUNCT__
34216c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
34226c699258SBarry Smith /*@
34236c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
34246c699258SBarry Smith 
34253f9fe445SBarry Smith    Logically Collective on SNES
34266c699258SBarry Smith 
34276c699258SBarry Smith    Input Parameters:
34286c699258SBarry Smith +  snes - the preconditioner context
34296c699258SBarry Smith -  dm - the dm
34306c699258SBarry Smith 
34316c699258SBarry Smith    Level: intermediate
34326c699258SBarry Smith 
34336c699258SBarry Smith 
34346c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
34356c699258SBarry Smith @*/
34367087cfbeSBarry Smith PetscErrorCode  SNESSetDM(SNES snes,DM dm)
34376c699258SBarry Smith {
34386c699258SBarry Smith   PetscErrorCode ierr;
3439345fed2cSBarry Smith   KSP            ksp;
34406c699258SBarry Smith 
34416c699258SBarry Smith   PetscFunctionBegin;
34420700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3443d0660788SBarry Smith   if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);}
34446bf464f9SBarry Smith   ierr = DMDestroy(&snes->dm);CHKERRQ(ierr);
34456c699258SBarry Smith   snes->dm = dm;
3446345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3447345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3448f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
34492c155ee1SBarry Smith   if (snes->pc) {
34502c155ee1SBarry Smith     ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr);
34512c155ee1SBarry Smith   }
34526c699258SBarry Smith   PetscFunctionReturn(0);
34536c699258SBarry Smith }
34546c699258SBarry Smith 
34556c699258SBarry Smith #undef __FUNCT__
34566c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
34576c699258SBarry Smith /*@
34586c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
34596c699258SBarry Smith 
34603f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
34616c699258SBarry Smith 
34626c699258SBarry Smith    Input Parameter:
34636c699258SBarry Smith . snes - the preconditioner context
34646c699258SBarry Smith 
34656c699258SBarry Smith    Output Parameter:
34666c699258SBarry Smith .  dm - the dm
34676c699258SBarry Smith 
34686c699258SBarry Smith    Level: intermediate
34696c699258SBarry Smith 
34706c699258SBarry Smith 
34716c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
34726c699258SBarry Smith @*/
34737087cfbeSBarry Smith PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
34746c699258SBarry Smith {
34756c699258SBarry Smith   PetscFunctionBegin;
34760700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34776c699258SBarry Smith   *dm = snes->dm;
34786c699258SBarry Smith   PetscFunctionReturn(0);
34796c699258SBarry Smith }
34800807856dSBarry Smith 
348131823bd8SMatthew G Knepley #undef __FUNCT__
348231823bd8SMatthew G Knepley #define __FUNCT__ "SNESSetPC"
348331823bd8SMatthew G Knepley /*@
3484fd4b34e9SJed Brown   SNESSetPC - Sets the nonlinear preconditioner to be used.
348531823bd8SMatthew G Knepley 
348631823bd8SMatthew G Knepley   Collective on SNES
348731823bd8SMatthew G Knepley 
348831823bd8SMatthew G Knepley   Input Parameters:
348931823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate()
349031823bd8SMatthew G Knepley - pc   - the preconditioner object
349131823bd8SMatthew G Knepley 
349231823bd8SMatthew G Knepley   Notes:
349331823bd8SMatthew G Knepley   Use SNESGetPC() to retrieve the preconditioner context (for example,
349431823bd8SMatthew G Knepley   to configure it using the API).
349531823bd8SMatthew G Knepley 
349631823bd8SMatthew G Knepley   Level: developer
349731823bd8SMatthew G Knepley 
349831823bd8SMatthew G Knepley .keywords: SNES, set, precondition
349931823bd8SMatthew G Knepley .seealso: SNESGetPC()
350031823bd8SMatthew G Knepley @*/
350131823bd8SMatthew G Knepley PetscErrorCode SNESSetPC(SNES snes, SNES pc)
350231823bd8SMatthew G Knepley {
350331823bd8SMatthew G Knepley   PetscErrorCode ierr;
350431823bd8SMatthew G Knepley 
350531823bd8SMatthew G Knepley   PetscFunctionBegin;
350631823bd8SMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
350731823bd8SMatthew G Knepley   PetscValidHeaderSpecific(pc, SNES_CLASSID, 2);
350831823bd8SMatthew G Knepley   PetscCheckSameComm(snes, 1, pc, 2);
350931823bd8SMatthew G Knepley   ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr);
3510bf11d3b6SBarry Smith   ierr = SNESDestroy(&snes->pc);CHKERRQ(ierr);
351131823bd8SMatthew G Knepley   snes->pc = pc;
351231823bd8SMatthew G Knepley   ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr);
351331823bd8SMatthew G Knepley   PetscFunctionReturn(0);
351431823bd8SMatthew G Knepley }
351531823bd8SMatthew G Knepley 
351631823bd8SMatthew G Knepley #undef __FUNCT__
351731823bd8SMatthew G Knepley #define __FUNCT__ "SNESGetPC"
351831823bd8SMatthew G Knepley /*@
3519fd4b34e9SJed Brown   SNESGetPC - Returns a pointer to the nonlinear preconditioning context set with SNESSetPC().
352031823bd8SMatthew G Knepley 
352131823bd8SMatthew G Knepley   Not Collective
352231823bd8SMatthew G Knepley 
352331823bd8SMatthew G Knepley   Input Parameter:
352431823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate()
352531823bd8SMatthew G Knepley 
352631823bd8SMatthew G Knepley   Output Parameter:
352731823bd8SMatthew G Knepley . pc - preconditioner context
352831823bd8SMatthew G Knepley 
352931823bd8SMatthew G Knepley   Level: developer
353031823bd8SMatthew G Knepley 
353131823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner
353231823bd8SMatthew G Knepley .seealso: SNESSetPC()
353331823bd8SMatthew G Knepley @*/
353431823bd8SMatthew G Knepley PetscErrorCode SNESGetPC(SNES snes, SNES *pc)
353531823bd8SMatthew G Knepley {
353631823bd8SMatthew G Knepley   PetscErrorCode ierr;
353731823bd8SMatthew G Knepley 
353831823bd8SMatthew G Knepley   PetscFunctionBegin;
353931823bd8SMatthew G Knepley   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
354031823bd8SMatthew G Knepley   PetscValidPointer(pc, 2);
354131823bd8SMatthew G Knepley   if (!snes->pc) {
354231823bd8SMatthew G Knepley     ierr = SNESCreate(((PetscObject) snes)->comm, &snes->pc);CHKERRQ(ierr);
35434a0c5b0cSMatthew G Knepley     ierr = PetscObjectIncrementTabLevel((PetscObject) snes->pc, (PetscObject) snes, 1);CHKERRQ(ierr);
354431823bd8SMatthew G Knepley     ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr);
354531823bd8SMatthew G Knepley   }
354631823bd8SMatthew G Knepley   *pc = snes->pc;
354731823bd8SMatthew G Knepley   PetscFunctionReturn(0);
354831823bd8SMatthew G Knepley }
354931823bd8SMatthew G Knepley 
355069b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3551c6db04a5SJed Brown #include <mex.h>
355269b4f73cSBarry Smith 
35538f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
35548f6e6473SBarry Smith 
35550807856dSBarry Smith #undef __FUNCT__
35560807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab"
35570807856dSBarry Smith /*
35580807856dSBarry Smith    SNESComputeFunction_Matlab - Calls the function that has been set with
35590807856dSBarry Smith                          SNESSetFunctionMatlab().
35600807856dSBarry Smith 
35610807856dSBarry Smith    Collective on SNES
35620807856dSBarry Smith 
35630807856dSBarry Smith    Input Parameters:
35640807856dSBarry Smith +  snes - the SNES context
35650807856dSBarry Smith -  x - input vector
35660807856dSBarry Smith 
35670807856dSBarry Smith    Output Parameter:
35680807856dSBarry Smith .  y - function vector, as set by SNESSetFunction()
35690807856dSBarry Smith 
35700807856dSBarry Smith    Notes:
35710807856dSBarry Smith    SNESComputeFunction() is typically used within nonlinear solvers
35720807856dSBarry Smith    implementations, so most users would not generally call this routine
35730807856dSBarry Smith    themselves.
35740807856dSBarry Smith 
35750807856dSBarry Smith    Level: developer
35760807856dSBarry Smith 
35770807856dSBarry Smith .keywords: SNES, nonlinear, compute, function
35780807856dSBarry Smith 
35790807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
358061b2408cSBarry Smith */
35817087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx)
35820807856dSBarry Smith {
3583e650e774SBarry Smith   PetscErrorCode    ierr;
35848f6e6473SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
35858f6e6473SBarry Smith   int               nlhs = 1,nrhs = 5;
35868f6e6473SBarry Smith   mxArray	    *plhs[1],*prhs[5];
358791621f2eSBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
3588e650e774SBarry Smith 
35890807856dSBarry Smith   PetscFunctionBegin;
35900807856dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
35910807856dSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
35920807856dSBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
35930807856dSBarry Smith   PetscCheckSameComm(snes,1,x,2);
35940807856dSBarry Smith   PetscCheckSameComm(snes,1,y,3);
35950807856dSBarry Smith 
35960807856dSBarry Smith   /* call Matlab function in ctx with arguments x and y */
3597e650e774SBarry Smith 
359891621f2eSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3599e650e774SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3600e650e774SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
360191621f2eSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
360291621f2eSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
360391621f2eSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
36048f6e6473SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
36058f6e6473SBarry Smith   prhs[4] =  sctx->ctx;
3606b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr);
3607e650e774SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3608e650e774SBarry Smith   mxDestroyArray(prhs[0]);
3609e650e774SBarry Smith   mxDestroyArray(prhs[1]);
3610e650e774SBarry Smith   mxDestroyArray(prhs[2]);
36118f6e6473SBarry Smith   mxDestroyArray(prhs[3]);
3612e650e774SBarry Smith   mxDestroyArray(plhs[0]);
36130807856dSBarry Smith   PetscFunctionReturn(0);
36140807856dSBarry Smith }
36150807856dSBarry Smith 
36160807856dSBarry Smith 
36170807856dSBarry Smith #undef __FUNCT__
36180807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab"
361961b2408cSBarry Smith /*
36200807856dSBarry Smith    SNESSetFunctionMatlab - Sets the function evaluation routine and function
36210807856dSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3622e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
36230807856dSBarry Smith 
36240807856dSBarry Smith    Logically Collective on SNES
36250807856dSBarry Smith 
36260807856dSBarry Smith    Input Parameters:
36270807856dSBarry Smith +  snes - the SNES context
36280807856dSBarry Smith .  r - vector to store function value
36290807856dSBarry Smith -  func - function evaluation routine
36300807856dSBarry Smith 
36310807856dSBarry Smith    Calling sequence of func:
363261b2408cSBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
36330807856dSBarry Smith 
36340807856dSBarry Smith 
36350807856dSBarry Smith    Notes:
36360807856dSBarry Smith    The Newton-like methods typically solve linear systems of the form
36370807856dSBarry Smith $      f'(x) x = -f(x),
36380807856dSBarry Smith    where f'(x) denotes the Jacobian matrix and f(x) is the function.
36390807856dSBarry Smith 
36400807856dSBarry Smith    Level: beginner
36410807856dSBarry Smith 
36420807856dSBarry Smith .keywords: SNES, nonlinear, set, function
36430807856dSBarry Smith 
36440807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
364561b2408cSBarry Smith */
36467087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx)
36470807856dSBarry Smith {
36480807856dSBarry Smith   PetscErrorCode    ierr;
36498f6e6473SBarry Smith   SNESMatlabContext *sctx;
36500807856dSBarry Smith 
36510807856dSBarry Smith   PetscFunctionBegin;
36528f6e6473SBarry Smith   /* currently sctx is memory bleed */
36538f6e6473SBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
36548f6e6473SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
36558f6e6473SBarry Smith   /*
36568f6e6473SBarry Smith      This should work, but it doesn't
36578f6e6473SBarry Smith   sctx->ctx = ctx;
36588f6e6473SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
36598f6e6473SBarry Smith   */
36608f6e6473SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
36618f6e6473SBarry Smith   ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr);
36620807856dSBarry Smith   PetscFunctionReturn(0);
36630807856dSBarry Smith }
366469b4f73cSBarry Smith 
366561b2408cSBarry Smith #undef __FUNCT__
366661b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab"
366761b2408cSBarry Smith /*
366861b2408cSBarry Smith    SNESComputeJacobian_Matlab - Calls the function that has been set with
366961b2408cSBarry Smith                          SNESSetJacobianMatlab().
367061b2408cSBarry Smith 
367161b2408cSBarry Smith    Collective on SNES
367261b2408cSBarry Smith 
367361b2408cSBarry Smith    Input Parameters:
367461b2408cSBarry Smith +  snes - the SNES context
367561b2408cSBarry Smith .  x - input vector
367661b2408cSBarry Smith .  A, B - the matrices
367761b2408cSBarry Smith -  ctx - user context
367861b2408cSBarry Smith 
367961b2408cSBarry Smith    Output Parameter:
368061b2408cSBarry Smith .  flag - structure of the matrix
368161b2408cSBarry Smith 
368261b2408cSBarry Smith    Level: developer
368361b2408cSBarry Smith 
368461b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function
368561b2408cSBarry Smith 
368661b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
368761b2408cSBarry Smith @*/
36887087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx)
368961b2408cSBarry Smith {
369061b2408cSBarry Smith   PetscErrorCode    ierr;
369161b2408cSBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
369261b2408cSBarry Smith   int               nlhs = 2,nrhs = 6;
369361b2408cSBarry Smith   mxArray	    *plhs[2],*prhs[6];
369461b2408cSBarry Smith   long long int     lx = 0,lA = 0,ls = 0, lB = 0;
369561b2408cSBarry Smith 
369661b2408cSBarry Smith   PetscFunctionBegin;
369761b2408cSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
369861b2408cSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
369961b2408cSBarry Smith 
370061b2408cSBarry Smith   /* call Matlab function in ctx with arguments x and y */
370161b2408cSBarry Smith 
370261b2408cSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
370361b2408cSBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
370461b2408cSBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
370561b2408cSBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
370661b2408cSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
370761b2408cSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
370861b2408cSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
370961b2408cSBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
371061b2408cSBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
371161b2408cSBarry Smith   prhs[5] =  sctx->ctx;
3712b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr);
371361b2408cSBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
371461b2408cSBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
371561b2408cSBarry Smith   mxDestroyArray(prhs[0]);
371661b2408cSBarry Smith   mxDestroyArray(prhs[1]);
371761b2408cSBarry Smith   mxDestroyArray(prhs[2]);
371861b2408cSBarry Smith   mxDestroyArray(prhs[3]);
371961b2408cSBarry Smith   mxDestroyArray(prhs[4]);
372061b2408cSBarry Smith   mxDestroyArray(plhs[0]);
372161b2408cSBarry Smith   mxDestroyArray(plhs[1]);
372261b2408cSBarry Smith   PetscFunctionReturn(0);
372361b2408cSBarry Smith }
372461b2408cSBarry Smith 
372561b2408cSBarry Smith 
372661b2408cSBarry Smith #undef __FUNCT__
372761b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab"
372861b2408cSBarry Smith /*
372961b2408cSBarry Smith    SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
373061b2408cSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3731e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
373261b2408cSBarry Smith 
373361b2408cSBarry Smith    Logically Collective on SNES
373461b2408cSBarry Smith 
373561b2408cSBarry Smith    Input Parameters:
373661b2408cSBarry Smith +  snes - the SNES context
373761b2408cSBarry Smith .  A,B - Jacobian matrices
373861b2408cSBarry Smith .  func - function evaluation routine
373961b2408cSBarry Smith -  ctx - user context
374061b2408cSBarry Smith 
374161b2408cSBarry Smith    Calling sequence of func:
374261b2408cSBarry Smith $    flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx);
374361b2408cSBarry Smith 
374461b2408cSBarry Smith 
374561b2408cSBarry Smith    Level: developer
374661b2408cSBarry Smith 
374761b2408cSBarry Smith .keywords: SNES, nonlinear, set, function
374861b2408cSBarry Smith 
374961b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
375061b2408cSBarry Smith */
37517087cfbeSBarry Smith PetscErrorCode  SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx)
375261b2408cSBarry Smith {
375361b2408cSBarry Smith   PetscErrorCode    ierr;
375461b2408cSBarry Smith   SNESMatlabContext *sctx;
375561b2408cSBarry Smith 
375661b2408cSBarry Smith   PetscFunctionBegin;
375761b2408cSBarry Smith   /* currently sctx is memory bleed */
375861b2408cSBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
375961b2408cSBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
376061b2408cSBarry Smith   /*
376161b2408cSBarry Smith      This should work, but it doesn't
376261b2408cSBarry Smith   sctx->ctx = ctx;
376361b2408cSBarry Smith   mexMakeArrayPersistent(sctx->ctx);
376461b2408cSBarry Smith   */
376561b2408cSBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
376661b2408cSBarry Smith   ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
376761b2408cSBarry Smith   PetscFunctionReturn(0);
376861b2408cSBarry Smith }
376969b4f73cSBarry Smith 
3770f9eb7ae2SShri Abhyankar #undef __FUNCT__
3771f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab"
3772f9eb7ae2SShri Abhyankar /*
3773f9eb7ae2SShri Abhyankar    SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab().
3774f9eb7ae2SShri Abhyankar 
3775f9eb7ae2SShri Abhyankar    Collective on SNES
3776f9eb7ae2SShri Abhyankar 
3777f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction()
3778f9eb7ae2SShri Abhyankar @*/
37797087cfbeSBarry Smith PetscErrorCode  SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx)
3780f9eb7ae2SShri Abhyankar {
3781f9eb7ae2SShri Abhyankar   PetscErrorCode  ierr;
378248f37e70SShri Abhyankar   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
3783f9eb7ae2SShri Abhyankar   int             nlhs = 1,nrhs = 6;
3784f9eb7ae2SShri Abhyankar   mxArray	  *plhs[1],*prhs[6];
3785f9eb7ae2SShri Abhyankar   long long int   lx = 0,ls = 0;
3786f9eb7ae2SShri Abhyankar   Vec             x=snes->vec_sol;
3787f9eb7ae2SShri Abhyankar 
3788f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3789f9eb7ae2SShri Abhyankar   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3790f9eb7ae2SShri Abhyankar 
3791f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3792f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3793f9eb7ae2SShri Abhyankar   prhs[0] =  mxCreateDoubleScalar((double)ls);
3794f9eb7ae2SShri Abhyankar   prhs[1] =  mxCreateDoubleScalar((double)it);
3795f9eb7ae2SShri Abhyankar   prhs[2] =  mxCreateDoubleScalar((double)fnorm);
3796f9eb7ae2SShri Abhyankar   prhs[3] =  mxCreateDoubleScalar((double)lx);
3797f9eb7ae2SShri Abhyankar   prhs[4] =  mxCreateString(sctx->funcname);
3798f9eb7ae2SShri Abhyankar   prhs[5] =  sctx->ctx;
3799f9eb7ae2SShri Abhyankar   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr);
3800f9eb7ae2SShri Abhyankar   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3801f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[0]);
3802f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[1]);
3803f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[2]);
3804f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[3]);
3805f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[4]);
3806f9eb7ae2SShri Abhyankar   mxDestroyArray(plhs[0]);
3807f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3808f9eb7ae2SShri Abhyankar }
3809f9eb7ae2SShri Abhyankar 
3810f9eb7ae2SShri Abhyankar 
3811f9eb7ae2SShri Abhyankar #undef __FUNCT__
3812f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab"
3813f9eb7ae2SShri Abhyankar /*
3814e3c5b3baSBarry Smith    SNESMonitorSetMatlab - Sets the monitor function from MATLAB
3815f9eb7ae2SShri Abhyankar 
3816f9eb7ae2SShri Abhyankar    Level: developer
3817f9eb7ae2SShri Abhyankar 
3818f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function
3819f9eb7ae2SShri Abhyankar 
3820f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
3821f9eb7ae2SShri Abhyankar */
38227087cfbeSBarry Smith PetscErrorCode  SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx)
3823f9eb7ae2SShri Abhyankar {
3824f9eb7ae2SShri Abhyankar   PetscErrorCode    ierr;
3825f9eb7ae2SShri Abhyankar   SNESMatlabContext *sctx;
3826f9eb7ae2SShri Abhyankar 
3827f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3828f9eb7ae2SShri Abhyankar   /* currently sctx is memory bleed */
3829f9eb7ae2SShri Abhyankar   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
3830f9eb7ae2SShri Abhyankar   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3831f9eb7ae2SShri Abhyankar   /*
3832f9eb7ae2SShri Abhyankar      This should work, but it doesn't
3833f9eb7ae2SShri Abhyankar   sctx->ctx = ctx;
3834f9eb7ae2SShri Abhyankar   mexMakeArrayPersistent(sctx->ctx);
3835f9eb7ae2SShri Abhyankar   */
3836f9eb7ae2SShri Abhyankar   sctx->ctx = mxDuplicateArray(ctx);
3837f9eb7ae2SShri Abhyankar   ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3838f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3839f9eb7ae2SShri Abhyankar }
3840f9eb7ae2SShri Abhyankar 
384169b4f73cSBarry Smith #endif
3842