xref: /petsc/src/snes/interface/snes.c (revision 37596af14955e368516fbde0b473e60c919ba968)
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__
12e113a28aSBarry Smith #define __FUNCT__ "SNESSetErrorIfNotConverged"
13e113a28aSBarry Smith /*@
14e113a28aSBarry Smith    SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged.
15e113a28aSBarry Smith 
163f9fe445SBarry Smith    Logically Collective on SNES
17e113a28aSBarry Smith 
18e113a28aSBarry Smith    Input Parameters:
19e113a28aSBarry Smith +  snes - iterative context obtained from SNESCreate()
20e113a28aSBarry Smith -  flg - PETSC_TRUE indicates you want the error generated
21e113a28aSBarry Smith 
22e113a28aSBarry Smith    Options database keys:
23e113a28aSBarry Smith .  -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false)
24e113a28aSBarry Smith 
25e113a28aSBarry Smith    Level: intermediate
26e113a28aSBarry Smith 
27e113a28aSBarry Smith    Notes:
28e113a28aSBarry Smith     Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve()
29e113a28aSBarry Smith     to determine if it has converged.
30e113a28aSBarry Smith 
31e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
32e113a28aSBarry Smith 
33e113a28aSBarry Smith .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
34e113a28aSBarry Smith @*/
357087cfbeSBarry Smith PetscErrorCode  SNESSetErrorIfNotConverged(SNES snes,PetscBool  flg)
36e113a28aSBarry Smith {
37e113a28aSBarry Smith   PetscFunctionBegin;
38e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
39acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flg,2);
40e113a28aSBarry Smith   snes->errorifnotconverged = flg;
41e113a28aSBarry Smith   PetscFunctionReturn(0);
42e113a28aSBarry Smith }
43e113a28aSBarry Smith 
44e113a28aSBarry Smith #undef __FUNCT__
45e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged"
46e113a28aSBarry Smith /*@
47e113a28aSBarry Smith    SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge?
48e113a28aSBarry Smith 
49e113a28aSBarry Smith    Not Collective
50e113a28aSBarry Smith 
51e113a28aSBarry Smith    Input Parameter:
52e113a28aSBarry Smith .  snes - iterative context obtained from SNESCreate()
53e113a28aSBarry Smith 
54e113a28aSBarry Smith    Output Parameter:
55e113a28aSBarry Smith .  flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE
56e113a28aSBarry Smith 
57e113a28aSBarry Smith    Level: intermediate
58e113a28aSBarry Smith 
59e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
60e113a28aSBarry Smith 
61e113a28aSBarry Smith .seealso:  SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
62e113a28aSBarry Smith @*/
637087cfbeSBarry Smith PetscErrorCode  SNESGetErrorIfNotConverged(SNES snes,PetscBool  *flag)
64e113a28aSBarry Smith {
65e113a28aSBarry Smith   PetscFunctionBegin;
66e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
67e113a28aSBarry Smith   PetscValidPointer(flag,2);
68e113a28aSBarry Smith   *flag = snes->errorifnotconverged;
69e113a28aSBarry Smith   PetscFunctionReturn(0);
70e113a28aSBarry Smith }
71e113a28aSBarry Smith 
72e113a28aSBarry Smith #undef __FUNCT__
734936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
74e725d27bSBarry Smith /*@
754936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
764936397dSBarry Smith      in the functions domain. For example, negative pressure.
774936397dSBarry Smith 
783f9fe445SBarry Smith    Logically Collective on SNES
794936397dSBarry Smith 
804936397dSBarry Smith    Input Parameters:
814936397dSBarry Smith .  SNES - the SNES context
824936397dSBarry Smith 
8328529972SSatish Balay    Level: advanced
844936397dSBarry Smith 
854936397dSBarry Smith .keywords: SNES, view
864936397dSBarry Smith 
874936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
884936397dSBarry Smith @*/
897087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionDomainError(SNES snes)
904936397dSBarry Smith {
914936397dSBarry Smith   PetscFunctionBegin;
920700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
934936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
944936397dSBarry Smith   PetscFunctionReturn(0);
954936397dSBarry Smith }
964936397dSBarry Smith 
974936397dSBarry Smith #undef __FUNCT__
984a2ae208SSatish Balay #define __FUNCT__ "SNESView"
997e2c5f70SBarry Smith /*@C
1009b94acceSBarry Smith    SNESView - Prints the SNES data structure.
1019b94acceSBarry Smith 
1024c49b128SBarry Smith    Collective on SNES
103fee21e36SBarry Smith 
104c7afd0dbSLois Curfman McInnes    Input Parameters:
105c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
106c7afd0dbSLois Curfman McInnes -  viewer - visualization context
107c7afd0dbSLois Curfman McInnes 
1089b94acceSBarry Smith    Options Database Key:
109c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
1109b94acceSBarry Smith 
1119b94acceSBarry Smith    Notes:
1129b94acceSBarry Smith    The available visualization contexts include
113b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
114b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
115c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
116c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
117c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
1189b94acceSBarry Smith 
1193e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
120b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
1219b94acceSBarry Smith 
12236851e7fSLois Curfman McInnes    Level: beginner
12336851e7fSLois Curfman McInnes 
1249b94acceSBarry Smith .keywords: SNES, view
1259b94acceSBarry Smith 
126b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1279b94acceSBarry Smith @*/
1287087cfbeSBarry Smith PetscErrorCode  SNESView(SNES snes,PetscViewer viewer)
1299b94acceSBarry Smith {
130fa9f3622SBarry Smith   SNESKSPEW           *kctx;
131dfbe8321SBarry Smith   PetscErrorCode      ierr;
13294b7f48cSBarry Smith   KSP                 ksp;
133ace3abfcSBarry Smith   PetscBool           iascii,isstring;
1349b94acceSBarry Smith 
1353a40ed3dSBarry Smith   PetscFunctionBegin;
1360700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1373050cee2SBarry Smith   if (!viewer) {
1387adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
1393050cee2SBarry Smith   }
1400700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
141c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
14274679c65SBarry Smith 
1432692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1442692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
14532077d6dSBarry Smith   if (iascii) {
146317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer,"SNES Object");CHKERRQ(ierr);
147e7788613SBarry Smith     if (snes->ops->view) {
148b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
149e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
150b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1510ef38995SBarry Smith     }
15277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
153a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
15470441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
15577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
15677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1579b94acceSBarry Smith     if (snes->ksp_ewconv) {
158fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1599b94acceSBarry Smith       if (kctx) {
16077431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
161a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
162a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1639b94acceSBarry Smith       }
1649b94acceSBarry Smith     }
165eb1f6c34SBarry Smith     if (snes->lagpreconditioner == -1) {
166eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is never rebuilt\n");CHKERRQ(ierr);
167eb1f6c34SBarry Smith     } else if (snes->lagpreconditioner > 1) {
168eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr);
169eb1f6c34SBarry Smith     }
170eb1f6c34SBarry Smith     if (snes->lagjacobian == -1) {
171eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is never rebuilt\n");CHKERRQ(ierr);
172eb1f6c34SBarry Smith     } else if (snes->lagjacobian > 1) {
173eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is rebuilt every %D Newton iterations\n",snes->lagjacobian);CHKERRQ(ierr);
174eb1f6c34SBarry Smith     }
1750f5bd95cSBarry Smith   } else if (isstring) {
176317d6ea6SBarry Smith     const char *type;
177454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
178b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
17919bcc07fSBarry Smith   }
18094b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
181b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
18294b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
183b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1843a40ed3dSBarry Smith   PetscFunctionReturn(0);
1859b94acceSBarry Smith }
1869b94acceSBarry Smith 
18776b2cf59SMatthew Knepley /*
18876b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
18976b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
19076b2cf59SMatthew Knepley */
19176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
192a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
1936849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
19476b2cf59SMatthew Knepley 
195e74ef692SMatthew Knepley #undef __FUNCT__
196e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
197ac226902SBarry Smith /*@C
19876b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
19976b2cf59SMatthew Knepley 
20076b2cf59SMatthew Knepley   Not Collective
20176b2cf59SMatthew Knepley 
20276b2cf59SMatthew Knepley   Input Parameter:
20376b2cf59SMatthew Knepley . snescheck - function that checks for options
20476b2cf59SMatthew Knepley 
20576b2cf59SMatthew Knepley   Level: developer
20676b2cf59SMatthew Knepley 
20776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
20876b2cf59SMatthew Knepley @*/
2097087cfbeSBarry Smith PetscErrorCode  SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
21076b2cf59SMatthew Knepley {
21176b2cf59SMatthew Knepley   PetscFunctionBegin;
21276b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
213e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
21476b2cf59SMatthew Knepley   }
21576b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
21676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
21776b2cf59SMatthew Knepley }
21876b2cf59SMatthew Knepley 
2197087cfbeSBarry Smith extern PetscErrorCode  SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
220aa3661deSLisandro Dalcin 
221aa3661deSLisandro Dalcin #undef __FUNCT__
222aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private"
223ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool  hasOperator, PetscInt version)
224aa3661deSLisandro Dalcin {
225aa3661deSLisandro Dalcin   Mat            J;
226aa3661deSLisandro Dalcin   KSP            ksp;
227aa3661deSLisandro Dalcin   PC             pc;
228ace3abfcSBarry Smith   PetscBool      match;
229aa3661deSLisandro Dalcin   PetscErrorCode ierr;
230aa3661deSLisandro Dalcin 
231aa3661deSLisandro Dalcin   PetscFunctionBegin;
2320700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
233aa3661deSLisandro Dalcin 
23498613b67SLisandro Dalcin   if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
23598613b67SLisandro Dalcin     Mat A = snes->jacobian, B = snes->jacobian_pre;
23698613b67SLisandro Dalcin     ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr);
23798613b67SLisandro Dalcin   }
23898613b67SLisandro Dalcin 
239aa3661deSLisandro Dalcin   if (version == 1) {
240aa3661deSLisandro Dalcin     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
24198613b67SLisandro Dalcin     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
2429c6ac3b3SBarry Smith     ierr = MatSetFromOptions(J);CHKERRQ(ierr);
243aa3661deSLisandro Dalcin   } else if (version == 2) {
244e32f2f54SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
245ce63c4c1SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) && !defined(PETSC_USE_REAL_LONG_DOUBLE)
246aa3661deSLisandro Dalcin     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr);
247aa3661deSLisandro Dalcin #else
248e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
249aa3661deSLisandro Dalcin #endif
250aa3661deSLisandro Dalcin   } else {
251e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2");
252aa3661deSLisandro Dalcin   }
253aa3661deSLisandro Dalcin 
254aa3661deSLisandro Dalcin   ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr);
255d3462f78SMatthew Knepley   if (hasOperator) {
256aa3661deSLisandro Dalcin     /* This version replaces the user provided Jacobian matrix with a
257aa3661deSLisandro Dalcin        matrix-free version but still employs the user-provided preconditioner matrix. */
258aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
259aa3661deSLisandro Dalcin   } else {
260aa3661deSLisandro Dalcin     /* This version replaces both the user-provided Jacobian and the user-
261aa3661deSLisandro Dalcin        provided preconditioner matrix with the default matrix free version. */
262aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
263aa3661deSLisandro Dalcin     /* Force no preconditioner */
264aa3661deSLisandro Dalcin     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
265aa3661deSLisandro Dalcin     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
266aa3661deSLisandro Dalcin     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr);
267aa3661deSLisandro Dalcin     if (!match) {
268aa3661deSLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
269aa3661deSLisandro Dalcin       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
270aa3661deSLisandro Dalcin     }
271aa3661deSLisandro Dalcin   }
272aa3661deSLisandro Dalcin   ierr = MatDestroy(J);CHKERRQ(ierr);
273aa3661deSLisandro Dalcin 
274aa3661deSLisandro Dalcin   PetscFunctionReturn(0);
275aa3661deSLisandro Dalcin }
276aa3661deSLisandro Dalcin 
2774a2ae208SSatish Balay #undef __FUNCT__
2784a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
2799b94acceSBarry Smith /*@
28094b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
2819b94acceSBarry Smith 
282c7afd0dbSLois Curfman McInnes    Collective on SNES
283c7afd0dbSLois Curfman McInnes 
2849b94acceSBarry Smith    Input Parameter:
2859b94acceSBarry Smith .  snes - the SNES context
2869b94acceSBarry Smith 
28736851e7fSLois Curfman McInnes    Options Database Keys:
2886831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
28982738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
29082738288SBarry Smith                 of the change in the solution between steps
29170441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
292b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
293b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
294b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
29550ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
296ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
297a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
298e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
299b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
3002492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
30182738288SBarry Smith                                solver; hence iterations will continue until max_it
3021fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
30382738288SBarry Smith                                of convergence test
304e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
305e8105e01SRichard Katz                                        filename given prints to stdout
306a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
307a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
308a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
309a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
310e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
3115968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
312fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
31382738288SBarry Smith 
31482738288SBarry Smith     Options Database for Eisenstat-Walker method:
315fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
3164b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
31736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
31836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
31936851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
32036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
32136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
32236851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
32382738288SBarry Smith 
32411ca99fdSLois Curfman McInnes    Notes:
32511ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
3260598bfebSBarry Smith    the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A>.
32783e2fdc7SBarry Smith 
32836851e7fSLois Curfman McInnes    Level: beginner
32936851e7fSLois Curfman McInnes 
3309b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
3319b94acceSBarry Smith 
33269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
3339b94acceSBarry Smith @*/
3347087cfbeSBarry Smith PetscErrorCode  SNESSetFromOptions(SNES snes)
3359b94acceSBarry Smith {
336ace3abfcSBarry Smith   PetscBool               flg,mf,mf_operator;
337aa3661deSLisandro Dalcin   PetscInt                i,indx,lag,mf_version;
338aa3661deSLisandro Dalcin   MatStructure            matflag;
33985385478SLisandro Dalcin   const char              *deft = SNESLS;
34085385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
34185385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
342e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
34323d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
34485385478SLisandro Dalcin   PetscErrorCode          ierr;
3459b94acceSBarry Smith 
3463a40ed3dSBarry Smith   PetscFunctionBegin;
3470700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
348ca161407SBarry Smith 
349186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
350cce0b1b2SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
3517adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
352b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
353d64ed03dSBarry Smith     if (flg) {
354186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
3557adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
356186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
357d64ed03dSBarry Smith     }
35890d69ab7SBarry Smith     /* not used here, but called so will go into help messaage */
359909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
36093c39befSBarry Smith 
36157034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
36257034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
363186905e3SBarry Smith 
36457034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
365b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
366b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
36750ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
368ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
369acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr);
37085385478SLisandro Dalcin 
371a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
372a8054027SBarry Smith     if (flg) {
373a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
374a8054027SBarry Smith     }
375e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
376e35cf81dSBarry Smith     if (flg) {
377e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
378e35cf81dSBarry Smith     }
379a8054027SBarry Smith 
38085385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
38185385478SLisandro Dalcin     if (flg) {
38285385478SLisandro Dalcin       switch (indx) {
3837f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
3847f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
38585385478SLisandro Dalcin       }
38685385478SLisandro Dalcin     }
38785385478SLisandro Dalcin 
388acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
389186905e3SBarry Smith 
39085385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
39185385478SLisandro Dalcin 
392acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
393186905e3SBarry Smith 
394fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
395fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
396fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
397fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
398fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
399fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
400fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
401186905e3SBarry Smith 
40290d69ab7SBarry Smith     flg  = PETSC_FALSE;
403acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
404a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
405eabae89aSBarry Smith 
406a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
407e8105e01SRichard Katz     if (flg) {
408050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
40923d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
410e8105e01SRichard Katz     }
411eabae89aSBarry Smith 
412b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
413b271bb04SBarry Smith     if (flg) {
414b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
415b271bb04SBarry Smith     }
416b271bb04SBarry Smith 
417a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
418eabae89aSBarry Smith     if (flg) {
419050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
420f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
421e8105e01SRichard Katz     }
422eabae89aSBarry Smith 
423a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
424eabae89aSBarry Smith     if (flg) {
425050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
42623d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
427eabae89aSBarry Smith     }
428eabae89aSBarry Smith 
42990d69ab7SBarry Smith     flg  = PETSC_FALSE;
430acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
431a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
43290d69ab7SBarry Smith     flg  = PETSC_FALSE;
433acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
434a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
43590d69ab7SBarry Smith     flg  = PETSC_FALSE;
436acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
437a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
43890d69ab7SBarry Smith     flg  = PETSC_FALSE;
439acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
440a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
44190d69ab7SBarry Smith     flg  = PETSC_FALSE;
442acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
443b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
444e24b481bSBarry Smith 
44590d69ab7SBarry Smith     flg  = PETSC_FALSE;
446acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
4474b27c08aSLois Curfman McInnes     if (flg) {
448186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
449ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
4509b94acceSBarry Smith     }
451639f9d9dSBarry Smith 
452aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
453aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
454acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
455aa3661deSLisandro Dalcin     if (flg && mf_operator) mf = PETSC_TRUE;
456aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
457acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
458aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
459aa3661deSLisandro Dalcin     mf_version = 1;
460aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
461aa3661deSLisandro Dalcin 
46276b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
46376b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
46476b2cf59SMatthew Knepley     }
46576b2cf59SMatthew Knepley 
466e7788613SBarry Smith     if (snes->ops->setfromoptions) {
467e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
468639f9d9dSBarry Smith     }
4695d973c19SBarry Smith 
4705d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
4715d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr);
472b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4734bbc92c1SBarry Smith 
474aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
4751cee3971SBarry Smith 
4761cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
477aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
478aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
47985385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
48093993e2dSLois Curfman McInnes 
4813a40ed3dSBarry Smith   PetscFunctionReturn(0);
4829b94acceSBarry Smith }
4839b94acceSBarry Smith 
484a847f771SSatish Balay 
4854a2ae208SSatish Balay #undef __FUNCT__
4864a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
4879b94acceSBarry Smith /*@
4889b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
4899b94acceSBarry Smith    the nonlinear solvers.
4909b94acceSBarry Smith 
4913f9fe445SBarry Smith    Logically Collective on SNES
492fee21e36SBarry Smith 
493c7afd0dbSLois Curfman McInnes    Input Parameters:
494c7afd0dbSLois Curfman McInnes +  snes - the SNES context
495c7afd0dbSLois Curfman McInnes -  usrP - optional user context
496c7afd0dbSLois Curfman McInnes 
49736851e7fSLois Curfman McInnes    Level: intermediate
49836851e7fSLois Curfman McInnes 
4999b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
5009b94acceSBarry Smith 
5019b94acceSBarry Smith .seealso: SNESGetApplicationContext()
5029b94acceSBarry Smith @*/
5037087cfbeSBarry Smith PetscErrorCode  SNESSetApplicationContext(SNES snes,void *usrP)
5049b94acceSBarry Smith {
5053a40ed3dSBarry Smith   PetscFunctionBegin;
5060700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5079b94acceSBarry Smith   snes->user		= usrP;
5083a40ed3dSBarry Smith   PetscFunctionReturn(0);
5099b94acceSBarry Smith }
51074679c65SBarry Smith 
5114a2ae208SSatish Balay #undef __FUNCT__
5124a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
5139b94acceSBarry Smith /*@C
5149b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
5159b94acceSBarry Smith    nonlinear solvers.
5169b94acceSBarry Smith 
517c7afd0dbSLois Curfman McInnes    Not Collective
518c7afd0dbSLois Curfman McInnes 
5199b94acceSBarry Smith    Input Parameter:
5209b94acceSBarry Smith .  snes - SNES context
5219b94acceSBarry Smith 
5229b94acceSBarry Smith    Output Parameter:
5239b94acceSBarry Smith .  usrP - user context
5249b94acceSBarry Smith 
52536851e7fSLois Curfman McInnes    Level: intermediate
52636851e7fSLois Curfman McInnes 
5279b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
5289b94acceSBarry Smith 
5299b94acceSBarry Smith .seealso: SNESSetApplicationContext()
5309b94acceSBarry Smith @*/
5317087cfbeSBarry Smith PetscErrorCode  SNESGetApplicationContext(SNES snes,void **usrP)
5329b94acceSBarry Smith {
5333a40ed3dSBarry Smith   PetscFunctionBegin;
5340700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5359b94acceSBarry Smith   *usrP = snes->user;
5363a40ed3dSBarry Smith   PetscFunctionReturn(0);
5379b94acceSBarry Smith }
53874679c65SBarry Smith 
5394a2ae208SSatish Balay #undef __FUNCT__
5404a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
5419b94acceSBarry Smith /*@
542c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
543c8228a4eSBarry Smith    at this time.
5449b94acceSBarry Smith 
545c7afd0dbSLois Curfman McInnes    Not Collective
546c7afd0dbSLois Curfman McInnes 
5479b94acceSBarry Smith    Input Parameter:
5489b94acceSBarry Smith .  snes - SNES context
5499b94acceSBarry Smith 
5509b94acceSBarry Smith    Output Parameter:
5519b94acceSBarry Smith .  iter - iteration number
5529b94acceSBarry Smith 
553c8228a4eSBarry Smith    Notes:
554c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
555c8228a4eSBarry Smith 
556c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
55708405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
55808405cd6SLois Curfman McInnes .vb
55908405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
56008405cd6SLois Curfman McInnes       if (!(it % 2)) {
56108405cd6SLois Curfman McInnes         [compute Jacobian here]
56208405cd6SLois Curfman McInnes       }
56308405cd6SLois Curfman McInnes .ve
564c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
56508405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
566c8228a4eSBarry Smith 
56736851e7fSLois Curfman McInnes    Level: intermediate
56836851e7fSLois Curfman McInnes 
5692b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
5702b668275SBarry Smith 
571b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
5729b94acceSBarry Smith @*/
5737087cfbeSBarry Smith PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt* iter)
5749b94acceSBarry Smith {
5753a40ed3dSBarry Smith   PetscFunctionBegin;
5760700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5774482741eSBarry Smith   PetscValidIntPointer(iter,2);
5789b94acceSBarry Smith   *iter = snes->iter;
5793a40ed3dSBarry Smith   PetscFunctionReturn(0);
5809b94acceSBarry Smith }
58174679c65SBarry Smith 
5824a2ae208SSatish Balay #undef __FUNCT__
5834a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
5849b94acceSBarry Smith /*@
5859b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
5869b94acceSBarry Smith    with SNESSSetFunction().
5879b94acceSBarry Smith 
588c7afd0dbSLois Curfman McInnes    Collective on SNES
589c7afd0dbSLois Curfman McInnes 
5909b94acceSBarry Smith    Input Parameter:
5919b94acceSBarry Smith .  snes - SNES context
5929b94acceSBarry Smith 
5939b94acceSBarry Smith    Output Parameter:
5949b94acceSBarry Smith .  fnorm - 2-norm of function
5959b94acceSBarry Smith 
59636851e7fSLois Curfman McInnes    Level: intermediate
59736851e7fSLois Curfman McInnes 
5989b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
599a86d99e1SLois Curfman McInnes 
600b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
6019b94acceSBarry Smith @*/
6027087cfbeSBarry Smith PetscErrorCode  SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
6039b94acceSBarry Smith {
6043a40ed3dSBarry Smith   PetscFunctionBegin;
6050700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6064482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
6079b94acceSBarry Smith   *fnorm = snes->norm;
6083a40ed3dSBarry Smith   PetscFunctionReturn(0);
6099b94acceSBarry Smith }
61074679c65SBarry Smith 
6114a2ae208SSatish Balay #undef __FUNCT__
612b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
6139b94acceSBarry Smith /*@
614b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
6159b94acceSBarry Smith    attempted by the nonlinear solver.
6169b94acceSBarry Smith 
617c7afd0dbSLois Curfman McInnes    Not Collective
618c7afd0dbSLois Curfman McInnes 
6199b94acceSBarry Smith    Input Parameter:
6209b94acceSBarry Smith .  snes - SNES context
6219b94acceSBarry Smith 
6229b94acceSBarry Smith    Output Parameter:
6239b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
6249b94acceSBarry Smith 
625c96a6f78SLois Curfman McInnes    Notes:
626c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
627c96a6f78SLois Curfman McInnes 
62836851e7fSLois Curfman McInnes    Level: intermediate
62936851e7fSLois Curfman McInnes 
6309b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
63158ebbce7SBarry Smith 
632e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
63358ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
6349b94acceSBarry Smith @*/
6357087cfbeSBarry Smith PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
6369b94acceSBarry Smith {
6373a40ed3dSBarry Smith   PetscFunctionBegin;
6380700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6394482741eSBarry Smith   PetscValidIntPointer(nfails,2);
64050ffb88aSMatthew Knepley   *nfails = snes->numFailures;
64150ffb88aSMatthew Knepley   PetscFunctionReturn(0);
64250ffb88aSMatthew Knepley }
64350ffb88aSMatthew Knepley 
64450ffb88aSMatthew Knepley #undef __FUNCT__
645b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
64650ffb88aSMatthew Knepley /*@
647b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
64850ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
64950ffb88aSMatthew Knepley 
65050ffb88aSMatthew Knepley    Not Collective
65150ffb88aSMatthew Knepley 
65250ffb88aSMatthew Knepley    Input Parameters:
65350ffb88aSMatthew Knepley +  snes     - SNES context
65450ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
65550ffb88aSMatthew Knepley 
65650ffb88aSMatthew Knepley    Level: intermediate
65750ffb88aSMatthew Knepley 
65850ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
65958ebbce7SBarry Smith 
660e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
66158ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
66250ffb88aSMatthew Knepley @*/
6637087cfbeSBarry Smith PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
66450ffb88aSMatthew Knepley {
66550ffb88aSMatthew Knepley   PetscFunctionBegin;
6660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
66750ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
66850ffb88aSMatthew Knepley   PetscFunctionReturn(0);
66950ffb88aSMatthew Knepley }
67050ffb88aSMatthew Knepley 
67150ffb88aSMatthew Knepley #undef __FUNCT__
672b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
67350ffb88aSMatthew Knepley /*@
674b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
67550ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
67650ffb88aSMatthew Knepley 
67750ffb88aSMatthew Knepley    Not Collective
67850ffb88aSMatthew Knepley 
67950ffb88aSMatthew Knepley    Input Parameter:
68050ffb88aSMatthew Knepley .  snes     - SNES context
68150ffb88aSMatthew Knepley 
68250ffb88aSMatthew Knepley    Output Parameter:
68350ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
68450ffb88aSMatthew Knepley 
68550ffb88aSMatthew Knepley    Level: intermediate
68650ffb88aSMatthew Knepley 
68750ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
68858ebbce7SBarry Smith 
689e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
69058ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
69158ebbce7SBarry Smith 
69250ffb88aSMatthew Knepley @*/
6937087cfbeSBarry Smith PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
69450ffb88aSMatthew Knepley {
69550ffb88aSMatthew Knepley   PetscFunctionBegin;
6960700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6974482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
69850ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
6993a40ed3dSBarry Smith   PetscFunctionReturn(0);
7009b94acceSBarry Smith }
701a847f771SSatish Balay 
7024a2ae208SSatish Balay #undef __FUNCT__
7032541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
7042541af92SBarry Smith /*@
7052541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
7062541af92SBarry Smith      done by SNES.
7072541af92SBarry Smith 
7082541af92SBarry Smith    Not Collective
7092541af92SBarry Smith 
7102541af92SBarry Smith    Input Parameter:
7112541af92SBarry Smith .  snes     - SNES context
7122541af92SBarry Smith 
7132541af92SBarry Smith    Output Parameter:
7142541af92SBarry Smith .  nfuncs - number of evaluations
7152541af92SBarry Smith 
7162541af92SBarry Smith    Level: intermediate
7172541af92SBarry Smith 
7182541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
71958ebbce7SBarry Smith 
720e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
7212541af92SBarry Smith @*/
7227087cfbeSBarry Smith PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
7232541af92SBarry Smith {
7242541af92SBarry Smith   PetscFunctionBegin;
7250700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7262541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
7272541af92SBarry Smith   *nfuncs = snes->nfuncs;
7282541af92SBarry Smith   PetscFunctionReturn(0);
7292541af92SBarry Smith }
7302541af92SBarry Smith 
7312541af92SBarry Smith #undef __FUNCT__
7323d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
7333d4c4710SBarry Smith /*@
7343d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
7353d4c4710SBarry Smith    linear solvers.
7363d4c4710SBarry Smith 
7373d4c4710SBarry Smith    Not Collective
7383d4c4710SBarry Smith 
7393d4c4710SBarry Smith    Input Parameter:
7403d4c4710SBarry Smith .  snes - SNES context
7413d4c4710SBarry Smith 
7423d4c4710SBarry Smith    Output Parameter:
7433d4c4710SBarry Smith .  nfails - number of failed solves
7443d4c4710SBarry Smith 
7453d4c4710SBarry Smith    Notes:
7463d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
7473d4c4710SBarry Smith 
7483d4c4710SBarry Smith    Level: intermediate
7493d4c4710SBarry Smith 
7503d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
75158ebbce7SBarry Smith 
752e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
7533d4c4710SBarry Smith @*/
7547087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
7553d4c4710SBarry Smith {
7563d4c4710SBarry Smith   PetscFunctionBegin;
7570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7583d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
7593d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
7603d4c4710SBarry Smith   PetscFunctionReturn(0);
7613d4c4710SBarry Smith }
7623d4c4710SBarry Smith 
7633d4c4710SBarry Smith #undef __FUNCT__
7643d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
7653d4c4710SBarry Smith /*@
7663d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
7673d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
7683d4c4710SBarry Smith 
7693f9fe445SBarry Smith    Logically Collective on SNES
7703d4c4710SBarry Smith 
7713d4c4710SBarry Smith    Input Parameters:
7723d4c4710SBarry Smith +  snes     - SNES context
7733d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
7743d4c4710SBarry Smith 
7753d4c4710SBarry Smith    Level: intermediate
7763d4c4710SBarry Smith 
777a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
7783d4c4710SBarry Smith 
7793d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
7803d4c4710SBarry Smith 
78158ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
7823d4c4710SBarry Smith @*/
7837087cfbeSBarry Smith PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
7843d4c4710SBarry Smith {
7853d4c4710SBarry Smith   PetscFunctionBegin;
7860700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
787c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
7883d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
7893d4c4710SBarry Smith   PetscFunctionReturn(0);
7903d4c4710SBarry Smith }
7913d4c4710SBarry Smith 
7923d4c4710SBarry Smith #undef __FUNCT__
7933d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
7943d4c4710SBarry Smith /*@
7953d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
7963d4c4710SBarry Smith      are allowed before SNES terminates
7973d4c4710SBarry Smith 
7983d4c4710SBarry Smith    Not Collective
7993d4c4710SBarry Smith 
8003d4c4710SBarry Smith    Input Parameter:
8013d4c4710SBarry Smith .  snes     - SNES context
8023d4c4710SBarry Smith 
8033d4c4710SBarry Smith    Output Parameter:
8043d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
8053d4c4710SBarry Smith 
8063d4c4710SBarry Smith    Level: intermediate
8073d4c4710SBarry Smith 
8083d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
8093d4c4710SBarry Smith 
8103d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
8113d4c4710SBarry Smith 
812e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
8133d4c4710SBarry Smith @*/
8147087cfbeSBarry Smith PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
8153d4c4710SBarry Smith {
8163d4c4710SBarry Smith   PetscFunctionBegin;
8170700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8183d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
8193d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
8203d4c4710SBarry Smith   PetscFunctionReturn(0);
8213d4c4710SBarry Smith }
8223d4c4710SBarry Smith 
8233d4c4710SBarry Smith #undef __FUNCT__
824b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
825c96a6f78SLois Curfman McInnes /*@
826b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
827c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
828c96a6f78SLois Curfman McInnes 
829c7afd0dbSLois Curfman McInnes    Not Collective
830c7afd0dbSLois Curfman McInnes 
831c96a6f78SLois Curfman McInnes    Input Parameter:
832c96a6f78SLois Curfman McInnes .  snes - SNES context
833c96a6f78SLois Curfman McInnes 
834c96a6f78SLois Curfman McInnes    Output Parameter:
835c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
836c96a6f78SLois Curfman McInnes 
837c96a6f78SLois Curfman McInnes    Notes:
838c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
839c96a6f78SLois Curfman McInnes 
84036851e7fSLois Curfman McInnes    Level: intermediate
84136851e7fSLois Curfman McInnes 
842c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
8432b668275SBarry Smith 
8448c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
845c96a6f78SLois Curfman McInnes @*/
8467087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
847c96a6f78SLois Curfman McInnes {
8483a40ed3dSBarry Smith   PetscFunctionBegin;
8490700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8504482741eSBarry Smith   PetscValidIntPointer(lits,2);
851c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
8523a40ed3dSBarry Smith   PetscFunctionReturn(0);
853c96a6f78SLois Curfman McInnes }
854c96a6f78SLois Curfman McInnes 
8554a2ae208SSatish Balay #undef __FUNCT__
85694b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
85752baeb72SSatish Balay /*@
85894b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
8599b94acceSBarry Smith 
86094b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
861c7afd0dbSLois Curfman McInnes 
8629b94acceSBarry Smith    Input Parameter:
8639b94acceSBarry Smith .  snes - the SNES context
8649b94acceSBarry Smith 
8659b94acceSBarry Smith    Output Parameter:
86694b7f48cSBarry Smith .  ksp - the KSP context
8679b94acceSBarry Smith 
8689b94acceSBarry Smith    Notes:
86994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
8709b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
8712999313aSBarry Smith    PC contexts as well.
8729b94acceSBarry Smith 
87336851e7fSLois Curfman McInnes    Level: beginner
87436851e7fSLois Curfman McInnes 
87594b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
8769b94acceSBarry Smith 
8772999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
8789b94acceSBarry Smith @*/
8797087cfbeSBarry Smith PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
8809b94acceSBarry Smith {
8811cee3971SBarry Smith   PetscErrorCode ierr;
8821cee3971SBarry Smith 
8833a40ed3dSBarry Smith   PetscFunctionBegin;
8840700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8854482741eSBarry Smith   PetscValidPointer(ksp,2);
8861cee3971SBarry Smith 
8871cee3971SBarry Smith   if (!snes->ksp) {
8881cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
8891cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
8901cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
8911cee3971SBarry Smith   }
89294b7f48cSBarry Smith   *ksp = snes->ksp;
8933a40ed3dSBarry Smith   PetscFunctionReturn(0);
8949b94acceSBarry Smith }
89582bf6240SBarry Smith 
8964a2ae208SSatish Balay #undef __FUNCT__
8972999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
8982999313aSBarry Smith /*@
8992999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
9002999313aSBarry Smith 
9012999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
9022999313aSBarry Smith 
9032999313aSBarry Smith    Input Parameters:
9042999313aSBarry Smith +  snes - the SNES context
9052999313aSBarry Smith -  ksp - the KSP context
9062999313aSBarry Smith 
9072999313aSBarry Smith    Notes:
9082999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
9092999313aSBarry Smith    so this routine is rarely needed.
9102999313aSBarry Smith 
9112999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
9122999313aSBarry Smith    decreased by one.
9132999313aSBarry Smith 
9142999313aSBarry Smith    Level: developer
9152999313aSBarry Smith 
9162999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9172999313aSBarry Smith 
9182999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9192999313aSBarry Smith @*/
9207087cfbeSBarry Smith PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
9212999313aSBarry Smith {
9222999313aSBarry Smith   PetscErrorCode ierr;
9232999313aSBarry Smith 
9242999313aSBarry Smith   PetscFunctionBegin;
9250700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9260700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
9272999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
9287dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
929906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
9302999313aSBarry Smith   snes->ksp = ksp;
9312999313aSBarry Smith   PetscFunctionReturn(0);
9322999313aSBarry Smith }
9332999313aSBarry Smith 
9347adad957SLisandro Dalcin #if 0
9352999313aSBarry Smith #undef __FUNCT__
9364a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
9376849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
938e24b481bSBarry Smith {
939e24b481bSBarry Smith   PetscFunctionBegin;
940e24b481bSBarry Smith   PetscFunctionReturn(0);
941e24b481bSBarry Smith }
9427adad957SLisandro Dalcin #endif
943e24b481bSBarry Smith 
9449b94acceSBarry Smith /* -----------------------------------------------------------*/
9454a2ae208SSatish Balay #undef __FUNCT__
9464a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
94752baeb72SSatish Balay /*@
9489b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
9499b94acceSBarry Smith 
950c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
951c7afd0dbSLois Curfman McInnes 
952c7afd0dbSLois Curfman McInnes    Input Parameters:
953906ed7ccSBarry Smith .  comm - MPI communicator
9549b94acceSBarry Smith 
9559b94acceSBarry Smith    Output Parameter:
9569b94acceSBarry Smith .  outsnes - the new SNES context
9579b94acceSBarry Smith 
958c7afd0dbSLois Curfman McInnes    Options Database Keys:
959c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
960c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
961c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
962c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
963c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
964c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
965c1f60f51SBarry Smith 
96636851e7fSLois Curfman McInnes    Level: beginner
96736851e7fSLois Curfman McInnes 
9689b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
9699b94acceSBarry Smith 
970a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
971a8054027SBarry Smith 
9729b94acceSBarry Smith @*/
9737087cfbeSBarry Smith PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
9749b94acceSBarry Smith {
975dfbe8321SBarry Smith   PetscErrorCode      ierr;
9769b94acceSBarry Smith   SNES                snes;
977fa9f3622SBarry Smith   SNESKSPEW           *kctx;
97837fcc0dbSBarry Smith 
9793a40ed3dSBarry Smith   PetscFunctionBegin;
980ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
9818ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
9828ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
9838ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
9848ba1e511SMatthew Knepley #endif
9858ba1e511SMatthew Knepley 
9860700a824SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
9877adad957SLisandro Dalcin 
98885385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
9899b94acceSBarry Smith   snes->max_its           = 50;
9909750a799SBarry Smith   snes->max_funcs	  = 10000;
9919b94acceSBarry Smith   snes->norm		  = 0.0;
992b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
993b4874afaSBarry Smith   snes->ttol              = 0.0;
99470441072SBarry Smith   snes->abstol		  = 1.e-50;
9959b94acceSBarry Smith   snes->xtol		  = 1.e-8;
9964b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
9979b94acceSBarry Smith   snes->nfuncs            = 0;
99850ffb88aSMatthew Knepley   snes->numFailures       = 0;
99950ffb88aSMatthew Knepley   snes->maxFailures       = 1;
10007a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1001e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1002a8054027SBarry Smith   snes->lagpreconditioner = 1;
1003639f9d9dSBarry Smith   snes->numbermonitors    = 0;
10049b94acceSBarry Smith   snes->data              = 0;
10054dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1006186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
10076f24a144SLois Curfman McInnes   snes->nwork             = 0;
100858c9b817SLisandro Dalcin   snes->work              = 0;
100958c9b817SLisandro Dalcin   snes->nvwork            = 0;
101058c9b817SLisandro Dalcin   snes->vwork             = 0;
1011758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1012758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1013758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1014758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1015758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1016184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
10179b94acceSBarry Smith 
10183d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
10193d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
10203d4c4710SBarry Smith 
10219b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
102238f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
10239b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
10249b94acceSBarry Smith   kctx->version     = 2;
10259b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
10269b94acceSBarry Smith                              this was too large for some test cases */
102775567043SBarry Smith   kctx->rtol_last   = 0.0;
10289b94acceSBarry Smith   kctx->rtol_max    = .9;
10299b94acceSBarry Smith   kctx->gamma       = 1.0;
103071f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
103171f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
10329b94acceSBarry Smith   kctx->threshold   = .1;
103375567043SBarry Smith   kctx->lresid_last = 0.0;
103475567043SBarry Smith   kctx->norm_last   = 0.0;
10359b94acceSBarry Smith 
10369b94acceSBarry Smith   *outsnes = snes;
10373a40ed3dSBarry Smith   PetscFunctionReturn(0);
10389b94acceSBarry Smith }
10399b94acceSBarry Smith 
10404a2ae208SSatish Balay #undef __FUNCT__
10414a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
10429b94acceSBarry Smith /*@C
10439b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
10449b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
10459b94acceSBarry Smith    equations.
10469b94acceSBarry Smith 
10473f9fe445SBarry Smith    Logically Collective on SNES
1048fee21e36SBarry Smith 
1049c7afd0dbSLois Curfman McInnes    Input Parameters:
1050c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1051c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1052de044059SHong Zhang .  func - function evaluation routine
1053c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1054c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
10559b94acceSBarry Smith 
1056c7afd0dbSLois Curfman McInnes    Calling sequence of func:
10578d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1058c7afd0dbSLois Curfman McInnes 
1059313e4042SLois Curfman McInnes .  f - function vector
1060c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
10619b94acceSBarry Smith 
10629b94acceSBarry Smith    Notes:
10639b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
10649b94acceSBarry Smith $      f'(x) x = -f(x),
1065c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
10669b94acceSBarry Smith 
106736851e7fSLois Curfman McInnes    Level: beginner
106836851e7fSLois Curfman McInnes 
10699b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
10709b94acceSBarry Smith 
1071a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
10729b94acceSBarry Smith @*/
10737087cfbeSBarry Smith PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
10749b94acceSBarry Smith {
107585385478SLisandro Dalcin   PetscErrorCode ierr;
10763a40ed3dSBarry Smith   PetscFunctionBegin;
10770700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1078d2a683ecSLisandro Dalcin   if (r) {
1079d2a683ecSLisandro Dalcin     PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1080d2a683ecSLisandro Dalcin     PetscCheckSameComm(snes,1,r,2);
108185385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
108285385478SLisandro Dalcin     if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
108385385478SLisandro Dalcin     snes->vec_func = r;
1084d2a683ecSLisandro Dalcin   } else if (!snes->vec_func && snes->dm) {
1085d2a683ecSLisandro Dalcin     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1086d2a683ecSLisandro Dalcin   }
1087d2a683ecSLisandro Dalcin   if (func) snes->ops->computefunction = func;
1088d2a683ecSLisandro Dalcin   if (ctx)  snes->funP                 = ctx;
10893a40ed3dSBarry Smith   PetscFunctionReturn(0);
10909b94acceSBarry Smith }
10919b94acceSBarry Smith 
10923ab0aad5SBarry Smith /* --------------------------------------------------------------- */
10933ab0aad5SBarry Smith #undef __FUNCT__
10941096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
10951096aae1SMatthew Knepley /*@C
10961096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
10971096aae1SMatthew Knepley    it assumes a zero right hand side.
10981096aae1SMatthew Knepley 
10993f9fe445SBarry Smith    Logically Collective on SNES
11001096aae1SMatthew Knepley 
11011096aae1SMatthew Knepley    Input Parameter:
11021096aae1SMatthew Knepley .  snes - the SNES context
11031096aae1SMatthew Knepley 
11041096aae1SMatthew Knepley    Output Parameter:
1105bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
11061096aae1SMatthew Knepley 
11071096aae1SMatthew Knepley    Level: intermediate
11081096aae1SMatthew Knepley 
11091096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
11101096aae1SMatthew Knepley 
111185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
11121096aae1SMatthew Knepley @*/
11137087cfbeSBarry Smith PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
11141096aae1SMatthew Knepley {
11151096aae1SMatthew Knepley   PetscFunctionBegin;
11160700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11171096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
111885385478SLisandro Dalcin   *rhs = snes->vec_rhs;
11191096aae1SMatthew Knepley   PetscFunctionReturn(0);
11201096aae1SMatthew Knepley }
11211096aae1SMatthew Knepley 
11221096aae1SMatthew Knepley #undef __FUNCT__
11234a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
11249b94acceSBarry Smith /*@
112536851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
11269b94acceSBarry Smith                          SNESSetFunction().
11279b94acceSBarry Smith 
1128c7afd0dbSLois Curfman McInnes    Collective on SNES
1129c7afd0dbSLois Curfman McInnes 
11309b94acceSBarry Smith    Input Parameters:
1131c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1132c7afd0dbSLois Curfman McInnes -  x - input vector
11339b94acceSBarry Smith 
11349b94acceSBarry Smith    Output Parameter:
11353638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
11369b94acceSBarry Smith 
11371bffabb2SLois Curfman McInnes    Notes:
113836851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
113936851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
114036851e7fSLois Curfman McInnes    themselves.
114136851e7fSLois Curfman McInnes 
114236851e7fSLois Curfman McInnes    Level: developer
114336851e7fSLois Curfman McInnes 
11449b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
11459b94acceSBarry Smith 
1146a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
11479b94acceSBarry Smith @*/
11487087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
11499b94acceSBarry Smith {
1150dfbe8321SBarry Smith   PetscErrorCode ierr;
11519b94acceSBarry Smith 
11523a40ed3dSBarry Smith   PetscFunctionBegin;
11530700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11540700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
11550700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1156c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1157c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1158184914b5SBarry Smith 
1159d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1160e7788613SBarry Smith   if (snes->ops->computefunction) {
1161d64ed03dSBarry Smith     PetscStackPush("SNES user function");
116239d508bbSBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1163d64ed03dSBarry Smith     PetscStackPop;
116485385478SLisandro Dalcin   } else if (snes->vec_rhs) {
11651096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
116617186662SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
116785385478SLisandro Dalcin   if (snes->vec_rhs) {
116885385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
11693ab0aad5SBarry Smith   }
1170ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1171d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
11723a40ed3dSBarry Smith   PetscFunctionReturn(0);
11739b94acceSBarry Smith }
11749b94acceSBarry Smith 
11754a2ae208SSatish Balay #undef __FUNCT__
11764a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
117762fef451SLois Curfman McInnes /*@
117862fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
117962fef451SLois Curfman McInnes    set with SNESSetJacobian().
118062fef451SLois Curfman McInnes 
1181c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1182c7afd0dbSLois Curfman McInnes 
118362fef451SLois Curfman McInnes    Input Parameters:
1184c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1185c7afd0dbSLois Curfman McInnes -  x - input vector
118662fef451SLois Curfman McInnes 
118762fef451SLois Curfman McInnes    Output Parameters:
1188c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
118962fef451SLois Curfman McInnes .  B - optional preconditioning matrix
11902b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1191fee21e36SBarry Smith 
1192e35cf81dSBarry Smith   Options Database Keys:
1193e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1194e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1195e35cf81dSBarry Smith 
119662fef451SLois Curfman McInnes    Notes:
119762fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
119862fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
119962fef451SLois Curfman McInnes 
120094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1201dc5a77f8SLois Curfman McInnes    flag parameter.
120262fef451SLois Curfman McInnes 
120336851e7fSLois Curfman McInnes    Level: developer
120436851e7fSLois Curfman McInnes 
120562fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
120662fef451SLois Curfman McInnes 
1207e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
120862fef451SLois Curfman McInnes @*/
12097087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
12109b94acceSBarry Smith {
1211dfbe8321SBarry Smith   PetscErrorCode ierr;
1212ace3abfcSBarry Smith   PetscBool      flag;
12133a40ed3dSBarry Smith 
12143a40ed3dSBarry Smith   PetscFunctionBegin;
12150700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12160700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
12174482741eSBarry Smith   PetscValidPointer(flg,5);
1218c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1219e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1220ebd3b9afSBarry Smith 
1221ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1222ebd3b9afSBarry Smith 
1223fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1224fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1225fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1226fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1227e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1228e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1229ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1230ebd3b9afSBarry Smith     if (flag) {
1231ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1232ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1233ebd3b9afSBarry Smith     }
1234e35cf81dSBarry Smith     PetscFunctionReturn(0);
1235e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1236e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1237e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1238ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1239ebd3b9afSBarry Smith     if (flag) {
1240ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1241ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1242ebd3b9afSBarry Smith     }
1243e35cf81dSBarry Smith     PetscFunctionReturn(0);
1244e35cf81dSBarry Smith   }
1245e35cf81dSBarry Smith 
1246c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1247e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1248d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1249e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1250d64ed03dSBarry Smith   PetscStackPop;
1251d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1252a8054027SBarry Smith 
12533b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
12543b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
12553b4f5425SBarry Smith     snes->lagpreconditioner = -1;
12563b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1257a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1258a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1259a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1260a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1261a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1262a8054027SBarry Smith   }
1263a8054027SBarry Smith 
12646d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
12650700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
12660700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
12673a40ed3dSBarry Smith   PetscFunctionReturn(0);
12689b94acceSBarry Smith }
12699b94acceSBarry Smith 
12704a2ae208SSatish Balay #undef __FUNCT__
12714a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
12729b94acceSBarry Smith /*@C
12739b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1274044dda88SLois Curfman McInnes    location to store the matrix.
12759b94acceSBarry Smith 
12763f9fe445SBarry Smith    Logically Collective on SNES and Mat
1277c7afd0dbSLois Curfman McInnes 
12789b94acceSBarry Smith    Input Parameters:
1279c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12809b94acceSBarry Smith .  A - Jacobian matrix
12819b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
12829b94acceSBarry Smith .  func - Jacobian evaluation routine
1283c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
12842cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
12859b94acceSBarry Smith 
12869b94acceSBarry Smith    Calling sequence of func:
12878d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12889b94acceSBarry Smith 
1289c7afd0dbSLois Curfman McInnes +  x - input vector
12909b94acceSBarry Smith .  A - Jacobian matrix
12919b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1292ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
12932b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1294c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
12959b94acceSBarry Smith 
12969b94acceSBarry Smith    Notes:
129794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
12982cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1299ac21db08SLois Curfman McInnes 
1300ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
13019b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
13029b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
13039b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
13049b94acceSBarry Smith    throughout the global iterations.
13059b94acceSBarry Smith 
130616913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
130716913363SBarry Smith    each matrix.
130816913363SBarry Smith 
1309a8a26c1eSJed Brown    If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument
1310a8a26c1eSJed Brown    must be a MatFDColoring.
1311a8a26c1eSJed Brown 
131236851e7fSLois Curfman McInnes    Level: beginner
131336851e7fSLois Curfman McInnes 
13149b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
13159b94acceSBarry Smith 
13163ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
13179b94acceSBarry Smith @*/
13187087cfbeSBarry Smith PetscErrorCode  SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
13199b94acceSBarry Smith {
1320dfbe8321SBarry Smith   PetscErrorCode ierr;
13213a7fca6bSBarry Smith 
13223a40ed3dSBarry Smith   PetscFunctionBegin;
13230700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13240700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
13250700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1326c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
132706975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1328e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
13293a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
13303a7fca6bSBarry Smith   if (A) {
13317dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
13323a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13339b94acceSBarry Smith     snes->jacobian = A;
13343a7fca6bSBarry Smith   }
13353a7fca6bSBarry Smith   if (B) {
13367dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
13373a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13389b94acceSBarry Smith     snes->jacobian_pre = B;
13393a7fca6bSBarry Smith   }
13403a40ed3dSBarry Smith   PetscFunctionReturn(0);
13419b94acceSBarry Smith }
134262fef451SLois Curfman McInnes 
13434a2ae208SSatish Balay #undef __FUNCT__
13444a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1345c2aafc4cSSatish Balay /*@C
1346b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1347b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1348b4fd4287SBarry Smith 
1349c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1350c7afd0dbSLois Curfman McInnes 
1351b4fd4287SBarry Smith    Input Parameter:
1352b4fd4287SBarry Smith .  snes - the nonlinear solver context
1353b4fd4287SBarry Smith 
1354b4fd4287SBarry Smith    Output Parameters:
1355c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1356b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
135770e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
135870e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1359fee21e36SBarry Smith 
136036851e7fSLois Curfman McInnes    Level: advanced
136136851e7fSLois Curfman McInnes 
1362b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1363b4fd4287SBarry Smith @*/
13647087cfbeSBarry Smith PetscErrorCode  SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1365b4fd4287SBarry Smith {
13663a40ed3dSBarry Smith   PetscFunctionBegin;
13670700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1368b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1369b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1370e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
137170e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
13723a40ed3dSBarry Smith   PetscFunctionReturn(0);
1373b4fd4287SBarry Smith }
1374b4fd4287SBarry Smith 
13759b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13769b94acceSBarry Smith 
13774a2ae208SSatish Balay #undef __FUNCT__
13784a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
13799b94acceSBarry Smith /*@
13809b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1381272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13829b94acceSBarry Smith 
1383fee21e36SBarry Smith    Collective on SNES
1384fee21e36SBarry Smith 
1385c7afd0dbSLois Curfman McInnes    Input Parameters:
138670e92668SMatthew Knepley .  snes - the SNES context
1387c7afd0dbSLois Curfman McInnes 
1388272ac6f2SLois Curfman McInnes    Notes:
1389272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1390272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1391272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1392272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1393272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1394272ac6f2SLois Curfman McInnes 
139536851e7fSLois Curfman McInnes    Level: advanced
139636851e7fSLois Curfman McInnes 
13979b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13989b94acceSBarry Smith 
13999b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
14009b94acceSBarry Smith @*/
14017087cfbeSBarry Smith PetscErrorCode  SNESSetUp(SNES snes)
14029b94acceSBarry Smith {
1403dfbe8321SBarry Smith   PetscErrorCode ierr;
14043a40ed3dSBarry Smith 
14053a40ed3dSBarry Smith   PetscFunctionBegin;
14060700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14074dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
14089b94acceSBarry Smith 
14097adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
141085385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
141185385478SLisandro Dalcin   }
141285385478SLisandro Dalcin 
141317186662SBarry Smith   if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
141417186662SBarry Smith   if (!snes->ops->computefunction && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
141517186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
141658c9b817SLisandro Dalcin   if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
141758c9b817SLisandro Dalcin 
141858c9b817SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
141958c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_rhs, &snes->vec_func);CHKERRQ(ierr);
142058c9b817SLisandro Dalcin   }
142158c9b817SLisandro Dalcin   if (!snes->vec_sol_update /* && snes->vec_sol */) {
142258c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr);
142358c9b817SLisandro Dalcin     ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr);
142458c9b817SLisandro Dalcin   }
142558c9b817SLisandro Dalcin 
1426ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1427ef8dffc7SBarry Smith     Mat           J;
1428ef8dffc7SBarry Smith     ISColoring    coloring;
1429ef8dffc7SBarry Smith     MatFDColoring fd;
1430a703fe33SLois Curfman McInnes 
1431ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1432ef8dffc7SBarry Smith     ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
1433ef8dffc7SBarry Smith     ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr);
1434ef8dffc7SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr);
1435ef8dffc7SBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr);
1436ef8dffc7SBarry Smith     ierr = ISColoringDestroy(coloring);CHKERRQ(ierr);
1437ef8dffc7SBarry Smith   }
1438b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1439b710008aSBarry Smith 
1440410397dcSLisandro Dalcin   if (snes->ops->setup) {
1441410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1442410397dcSLisandro Dalcin   }
144358c9b817SLisandro Dalcin 
14447aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
14453a40ed3dSBarry Smith   PetscFunctionReturn(0);
14469b94acceSBarry Smith }
14479b94acceSBarry Smith 
14484a2ae208SSatish Balay #undef __FUNCT__
1449*37596af1SLisandro Dalcin #define __FUNCT__ "SNESReset"
1450*37596af1SLisandro Dalcin /*@
1451*37596af1SLisandro Dalcin    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats
1452*37596af1SLisandro Dalcin 
1453*37596af1SLisandro Dalcin    Collective on SNES
1454*37596af1SLisandro Dalcin 
1455*37596af1SLisandro Dalcin    Input Parameter:
1456*37596af1SLisandro Dalcin .  snes - iterative context obtained from SNESCreate()
1457*37596af1SLisandro Dalcin 
1458*37596af1SLisandro Dalcin    Level: beginner
1459*37596af1SLisandro Dalcin 
1460*37596af1SLisandro Dalcin .keywords: SNES, destroy
1461*37596af1SLisandro Dalcin 
1462*37596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
1463*37596af1SLisandro Dalcin @*/
1464*37596af1SLisandro Dalcin PetscErrorCode  SNESReset(SNES snes)
1465*37596af1SLisandro Dalcin {
1466*37596af1SLisandro Dalcin   PetscErrorCode ierr;
1467*37596af1SLisandro Dalcin 
1468*37596af1SLisandro Dalcin   PetscFunctionBegin;
1469*37596af1SLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1470*37596af1SLisandro Dalcin   if (snes->ops->reset) {
1471*37596af1SLisandro Dalcin     ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr);
1472*37596af1SLisandro Dalcin   }
1473*37596af1SLisandro Dalcin   if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);}
1474*37596af1SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
1475*37596af1SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
1476*37596af1SLisandro Dalcin   if (snes->vec_sol_update) {ierr = VecDestroy(snes->vec_sol_update);CHKERRQ(ierr);}
1477*37596af1SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
1478*37596af1SLisandro Dalcin   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
1479*37596af1SLisandro Dalcin   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
1480*37596af1SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
1481*37596af1SLisandro Dalcin   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
1482*37596af1SLisandro Dalcin   snes->nwork = snes->nvwork = 0;
1483*37596af1SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
1484*37596af1SLisandro Dalcin   PetscFunctionReturn(0);
1485*37596af1SLisandro Dalcin }
1486*37596af1SLisandro Dalcin 
1487*37596af1SLisandro Dalcin #undef __FUNCT__
14884a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
148952baeb72SSatish Balay /*@
14909b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
14919b94acceSBarry Smith    with SNESCreate().
14929b94acceSBarry Smith 
1493c7afd0dbSLois Curfman McInnes    Collective on SNES
1494c7afd0dbSLois Curfman McInnes 
14959b94acceSBarry Smith    Input Parameter:
14969b94acceSBarry Smith .  snes - the SNES context
14979b94acceSBarry Smith 
149836851e7fSLois Curfman McInnes    Level: beginner
149936851e7fSLois Curfman McInnes 
15009b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
15019b94acceSBarry Smith 
150263a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
15039b94acceSBarry Smith @*/
15047087cfbeSBarry Smith PetscErrorCode  SNESDestroy(SNES snes)
15059b94acceSBarry Smith {
15066849ba73SBarry Smith   PetscErrorCode ierr;
15073a40ed3dSBarry Smith 
15083a40ed3dSBarry Smith   PetscFunctionBegin;
15090700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
15107adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1511d4bb536fSBarry Smith 
1512be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
15130f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1514e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
15156d4c513bSLisandro Dalcin 
151658c9b817SLisandro Dalcin   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
151785385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
151885385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
151958c9b817SLisandro Dalcin   if (snes->vec_sol_update) {ierr = VecDestroy(snes->vec_sol_update);CHKERRQ(ierr);}
152085385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
15213a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
15223a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
15231cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
152458c9b817SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
1525574deadeSBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
152658c9b817SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
15277f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
152858c9b817SLisandro Dalcin   if (snes->conv_malloc) {
152958c9b817SLisandro Dalcin     ierr = PetscFree(snes->conv_hist);CHKERRQ(ierr);
153058c9b817SLisandro Dalcin     ierr = PetscFree(snes->conv_hist_its);CHKERRQ(ierr);
153158c9b817SLisandro Dalcin   }
15326d4c513bSLisandro Dalcin   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
15336d4c513bSLisandro Dalcin 
1534a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
15353a40ed3dSBarry Smith   PetscFunctionReturn(0);
15369b94acceSBarry Smith }
15379b94acceSBarry Smith 
15389b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
15399b94acceSBarry Smith 
15404a2ae208SSatish Balay #undef __FUNCT__
1541a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1542a8054027SBarry Smith /*@
1543a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1544a8054027SBarry Smith 
15453f9fe445SBarry Smith    Logically Collective on SNES
1546a8054027SBarry Smith 
1547a8054027SBarry Smith    Input Parameters:
1548a8054027SBarry Smith +  snes - the SNES context
1549a8054027SBarry 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
15503b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1551a8054027SBarry Smith 
1552a8054027SBarry Smith    Options Database Keys:
1553a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1554a8054027SBarry Smith 
1555a8054027SBarry Smith    Notes:
1556a8054027SBarry Smith    The default is 1
1557a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1558a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1559a8054027SBarry Smith 
1560a8054027SBarry Smith    Level: intermediate
1561a8054027SBarry Smith 
1562a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1563a8054027SBarry Smith 
1564e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1565a8054027SBarry Smith 
1566a8054027SBarry Smith @*/
15677087cfbeSBarry Smith PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1568a8054027SBarry Smith {
1569a8054027SBarry Smith   PetscFunctionBegin;
15700700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1571e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1572e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1573c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1574a8054027SBarry Smith   snes->lagpreconditioner = lag;
1575a8054027SBarry Smith   PetscFunctionReturn(0);
1576a8054027SBarry Smith }
1577a8054027SBarry Smith 
1578a8054027SBarry Smith #undef __FUNCT__
1579a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1580a8054027SBarry Smith /*@
1581a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1582a8054027SBarry Smith 
15833f9fe445SBarry Smith    Not Collective
1584a8054027SBarry Smith 
1585a8054027SBarry Smith    Input Parameter:
1586a8054027SBarry Smith .  snes - the SNES context
1587a8054027SBarry Smith 
1588a8054027SBarry Smith    Output Parameter:
1589a8054027SBarry 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
15903b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1591a8054027SBarry Smith 
1592a8054027SBarry Smith    Options Database Keys:
1593a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1594a8054027SBarry Smith 
1595a8054027SBarry Smith    Notes:
1596a8054027SBarry Smith    The default is 1
1597a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1598a8054027SBarry Smith 
1599a8054027SBarry Smith    Level: intermediate
1600a8054027SBarry Smith 
1601a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1602a8054027SBarry Smith 
1603a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1604a8054027SBarry Smith 
1605a8054027SBarry Smith @*/
16067087cfbeSBarry Smith PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1607a8054027SBarry Smith {
1608a8054027SBarry Smith   PetscFunctionBegin;
16090700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1610a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1611a8054027SBarry Smith   PetscFunctionReturn(0);
1612a8054027SBarry Smith }
1613a8054027SBarry Smith 
1614a8054027SBarry Smith #undef __FUNCT__
1615e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1616e35cf81dSBarry Smith /*@
1617e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1618e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1619e35cf81dSBarry Smith 
16203f9fe445SBarry Smith    Logically Collective on SNES
1621e35cf81dSBarry Smith 
1622e35cf81dSBarry Smith    Input Parameters:
1623e35cf81dSBarry Smith +  snes - the SNES context
1624e35cf81dSBarry 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
1625fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1626e35cf81dSBarry Smith 
1627e35cf81dSBarry Smith    Options Database Keys:
1628e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1629e35cf81dSBarry Smith 
1630e35cf81dSBarry Smith    Notes:
1631e35cf81dSBarry Smith    The default is 1
1632e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1633fe3ffe1eSBarry 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
1634fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1635e35cf81dSBarry Smith 
1636e35cf81dSBarry Smith    Level: intermediate
1637e35cf81dSBarry Smith 
1638e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1639e35cf81dSBarry Smith 
1640e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1641e35cf81dSBarry Smith 
1642e35cf81dSBarry Smith @*/
16437087cfbeSBarry Smith PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
1644e35cf81dSBarry Smith {
1645e35cf81dSBarry Smith   PetscFunctionBegin;
16460700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1647e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1648e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1649c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1650e35cf81dSBarry Smith   snes->lagjacobian = lag;
1651e35cf81dSBarry Smith   PetscFunctionReturn(0);
1652e35cf81dSBarry Smith }
1653e35cf81dSBarry Smith 
1654e35cf81dSBarry Smith #undef __FUNCT__
1655e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1656e35cf81dSBarry Smith /*@
1657e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1658e35cf81dSBarry Smith 
16593f9fe445SBarry Smith    Not Collective
1660e35cf81dSBarry Smith 
1661e35cf81dSBarry Smith    Input Parameter:
1662e35cf81dSBarry Smith .  snes - the SNES context
1663e35cf81dSBarry Smith 
1664e35cf81dSBarry Smith    Output Parameter:
1665e35cf81dSBarry 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
1666e35cf81dSBarry Smith          the Jacobian is built etc.
1667e35cf81dSBarry Smith 
1668e35cf81dSBarry Smith    Options Database Keys:
1669e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1670e35cf81dSBarry Smith 
1671e35cf81dSBarry Smith    Notes:
1672e35cf81dSBarry Smith    The default is 1
1673e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1674e35cf81dSBarry Smith 
1675e35cf81dSBarry Smith    Level: intermediate
1676e35cf81dSBarry Smith 
1677e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1678e35cf81dSBarry Smith 
1679e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1680e35cf81dSBarry Smith 
1681e35cf81dSBarry Smith @*/
16827087cfbeSBarry Smith PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
1683e35cf81dSBarry Smith {
1684e35cf81dSBarry Smith   PetscFunctionBegin;
16850700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1686e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1687e35cf81dSBarry Smith   PetscFunctionReturn(0);
1688e35cf81dSBarry Smith }
1689e35cf81dSBarry Smith 
1690e35cf81dSBarry Smith #undef __FUNCT__
16914a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
16929b94acceSBarry Smith /*@
1693d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
16949b94acceSBarry Smith 
16953f9fe445SBarry Smith    Logically Collective on SNES
1696c7afd0dbSLois Curfman McInnes 
16979b94acceSBarry Smith    Input Parameters:
1698c7afd0dbSLois Curfman McInnes +  snes - the SNES context
169970441072SBarry Smith .  abstol - absolute convergence tolerance
170033174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
170133174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
170233174efeSLois Curfman McInnes            of the change in the solution between steps
170333174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1704c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1705fee21e36SBarry Smith 
170633174efeSLois Curfman McInnes    Options Database Keys:
170770441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1708c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1709c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1710c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1711c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
17129b94acceSBarry Smith 
1713d7a720efSLois Curfman McInnes    Notes:
17149b94acceSBarry Smith    The default maximum number of iterations is 50.
17159b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
17169b94acceSBarry Smith 
171736851e7fSLois Curfman McInnes    Level: intermediate
171836851e7fSLois Curfman McInnes 
171933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
17209b94acceSBarry Smith 
17212492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
17229b94acceSBarry Smith @*/
17237087cfbeSBarry Smith PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
17249b94acceSBarry Smith {
17253a40ed3dSBarry Smith   PetscFunctionBegin;
17260700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1727c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
1728c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
1729c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
1730c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
1731c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
1732c5eb9154SBarry Smith 
173370441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1734d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1735d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1736d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1737d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
17383a40ed3dSBarry Smith   PetscFunctionReturn(0);
17399b94acceSBarry Smith }
17409b94acceSBarry Smith 
17414a2ae208SSatish Balay #undef __FUNCT__
17424a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
17439b94acceSBarry Smith /*@
174433174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
174533174efeSLois Curfman McInnes 
1746c7afd0dbSLois Curfman McInnes    Not Collective
1747c7afd0dbSLois Curfman McInnes 
174833174efeSLois Curfman McInnes    Input Parameters:
1749c7afd0dbSLois Curfman McInnes +  snes - the SNES context
175085385478SLisandro Dalcin .  atol - absolute convergence tolerance
175133174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
175233174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
175333174efeSLois Curfman McInnes            of the change in the solution between steps
175433174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1755c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1756fee21e36SBarry Smith 
175733174efeSLois Curfman McInnes    Notes:
175833174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
175933174efeSLois Curfman McInnes 
176036851e7fSLois Curfman McInnes    Level: intermediate
176136851e7fSLois Curfman McInnes 
176233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
176333174efeSLois Curfman McInnes 
176433174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
176533174efeSLois Curfman McInnes @*/
17667087cfbeSBarry Smith PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
176733174efeSLois Curfman McInnes {
17683a40ed3dSBarry Smith   PetscFunctionBegin;
17690700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
177085385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
177133174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
177233174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
177333174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
177433174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
17753a40ed3dSBarry Smith   PetscFunctionReturn(0);
177633174efeSLois Curfman McInnes }
177733174efeSLois Curfman McInnes 
17784a2ae208SSatish Balay #undef __FUNCT__
17794a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
178033174efeSLois Curfman McInnes /*@
17819b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
17829b94acceSBarry Smith 
17833f9fe445SBarry Smith    Logically Collective on SNES
1784fee21e36SBarry Smith 
1785c7afd0dbSLois Curfman McInnes    Input Parameters:
1786c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1787c7afd0dbSLois Curfman McInnes -  tol - tolerance
1788c7afd0dbSLois Curfman McInnes 
17899b94acceSBarry Smith    Options Database Key:
1790c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
17919b94acceSBarry Smith 
179236851e7fSLois Curfman McInnes    Level: intermediate
179336851e7fSLois Curfman McInnes 
17949b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
17959b94acceSBarry Smith 
17962492ecdbSBarry Smith .seealso: SNESSetTolerances()
17979b94acceSBarry Smith @*/
17987087cfbeSBarry Smith PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
17999b94acceSBarry Smith {
18003a40ed3dSBarry Smith   PetscFunctionBegin;
18010700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1802c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
18039b94acceSBarry Smith   snes->deltatol = tol;
18043a40ed3dSBarry Smith   PetscFunctionReturn(0);
18059b94acceSBarry Smith }
18069b94acceSBarry Smith 
1807df9fa365SBarry Smith /*
1808df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1809df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1810df9fa365SBarry Smith    macros instead of functions
1811df9fa365SBarry Smith */
18124a2ae208SSatish Balay #undef __FUNCT__
1813a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
18147087cfbeSBarry Smith PetscErrorCode  SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1815ce1608b8SBarry Smith {
1816dfbe8321SBarry Smith   PetscErrorCode ierr;
1817ce1608b8SBarry Smith 
1818ce1608b8SBarry Smith   PetscFunctionBegin;
18190700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1820a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1821ce1608b8SBarry Smith   PetscFunctionReturn(0);
1822ce1608b8SBarry Smith }
1823ce1608b8SBarry Smith 
18244a2ae208SSatish Balay #undef __FUNCT__
1825a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
18267087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1827df9fa365SBarry Smith {
1828dfbe8321SBarry Smith   PetscErrorCode ierr;
1829df9fa365SBarry Smith 
1830df9fa365SBarry Smith   PetscFunctionBegin;
1831a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1832df9fa365SBarry Smith   PetscFunctionReturn(0);
1833df9fa365SBarry Smith }
1834df9fa365SBarry Smith 
18354a2ae208SSatish Balay #undef __FUNCT__
1836a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
18377087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGDestroy(PetscDrawLG draw)
1838df9fa365SBarry Smith {
1839dfbe8321SBarry Smith   PetscErrorCode ierr;
1840df9fa365SBarry Smith 
1841df9fa365SBarry Smith   PetscFunctionBegin;
1842a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1843df9fa365SBarry Smith   PetscFunctionReturn(0);
1844df9fa365SBarry Smith }
1845df9fa365SBarry Smith 
18467087cfbeSBarry Smith extern PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1847b271bb04SBarry Smith #undef __FUNCT__
1848b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
18497087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1850b271bb04SBarry Smith {
1851b271bb04SBarry Smith   PetscDrawLG      lg;
1852b271bb04SBarry Smith   PetscErrorCode   ierr;
1853b271bb04SBarry Smith   PetscReal        x,y,per;
1854b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1855b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1856b271bb04SBarry Smith   PetscDraw        draw;
1857b271bb04SBarry Smith   PetscFunctionBegin;
1858b271bb04SBarry Smith   if (!monctx) {
1859b271bb04SBarry Smith     MPI_Comm    comm;
1860b271bb04SBarry Smith 
1861b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1862b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1863b271bb04SBarry Smith   }
1864b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1865b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1866b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1867b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1868b271bb04SBarry Smith   x = (PetscReal) n;
1869b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1870b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1871b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1872b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1873b271bb04SBarry Smith   }
1874b271bb04SBarry Smith 
1875b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1876b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1877b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1878b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1879b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1880b271bb04SBarry Smith   x = (PetscReal) n;
1881b271bb04SBarry Smith   y = 100.0*per;
1882b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1883b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1884b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1885b271bb04SBarry Smith   }
1886b271bb04SBarry Smith 
1887b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1888b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1889b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1890b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1891b271bb04SBarry Smith   x = (PetscReal) n;
1892b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1893b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1894b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1895b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1896b271bb04SBarry Smith   }
1897b271bb04SBarry Smith 
1898b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1899b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1900b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1901b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1902b271bb04SBarry Smith   x = (PetscReal) n;
1903b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1904b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1905b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1906b271bb04SBarry Smith   }
1907b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1908b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1909b271bb04SBarry Smith   }
1910b271bb04SBarry Smith   prev = rnorm;
1911b271bb04SBarry Smith   PetscFunctionReturn(0);
1912b271bb04SBarry Smith }
1913b271bb04SBarry Smith 
1914b271bb04SBarry Smith #undef __FUNCT__
1915b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
19167087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1917b271bb04SBarry Smith {
1918b271bb04SBarry Smith   PetscErrorCode ierr;
1919b271bb04SBarry Smith 
1920b271bb04SBarry Smith   PetscFunctionBegin;
1921b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1922b271bb04SBarry Smith   PetscFunctionReturn(0);
1923b271bb04SBarry Smith }
1924b271bb04SBarry Smith 
1925b271bb04SBarry Smith #undef __FUNCT__
1926b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
19277087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeDestroy(PetscDrawLG draw)
1928b271bb04SBarry Smith {
1929b271bb04SBarry Smith   PetscErrorCode ierr;
1930b271bb04SBarry Smith 
1931b271bb04SBarry Smith   PetscFunctionBegin;
1932b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1933b271bb04SBarry Smith   PetscFunctionReturn(0);
1934b271bb04SBarry Smith }
1935b271bb04SBarry Smith 
19369b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
19379b94acceSBarry Smith 
19384a2ae208SSatish Balay #undef __FUNCT__
1939a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
19409b94acceSBarry Smith /*@C
1941a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
19429b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
19439b94acceSBarry Smith    progress.
19449b94acceSBarry Smith 
19453f9fe445SBarry Smith    Logically Collective on SNES
1946fee21e36SBarry Smith 
1947c7afd0dbSLois Curfman McInnes    Input Parameters:
1948c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1949c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1950b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1951e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1952b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1953b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
19549b94acceSBarry Smith 
1955c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1956a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1957c7afd0dbSLois Curfman McInnes 
1958c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1959c7afd0dbSLois Curfman McInnes .    its - iteration number
1960c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
196140a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
19629b94acceSBarry Smith 
19639665c990SLois Curfman McInnes    Options Database Keys:
1964a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1965a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1966a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1967a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1968c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1969a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1970c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1971c7afd0dbSLois Curfman McInnes                             the options database.
19729665c990SLois Curfman McInnes 
1973639f9d9dSBarry Smith    Notes:
19746bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1975a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
19766bc08f3fSLois Curfman McInnes    order in which they were set.
1977639f9d9dSBarry Smith 
1978025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1979025f1a04SBarry Smith 
198036851e7fSLois Curfman McInnes    Level: intermediate
198136851e7fSLois Curfman McInnes 
19829b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
19839b94acceSBarry Smith 
1984a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
19859b94acceSBarry Smith @*/
19867087cfbeSBarry Smith PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
19879b94acceSBarry Smith {
1988b90d0a6eSBarry Smith   PetscInt i;
1989b90d0a6eSBarry Smith 
19903a40ed3dSBarry Smith   PetscFunctionBegin;
19910700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
199217186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1993b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1994b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1995b90d0a6eSBarry Smith 
1996b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1997b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1998b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1999b90d0a6eSBarry Smith         PetscErrorCode          ierr;
2000b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
2001b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
2002b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
2003b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
2004b90d0a6eSBarry Smith           PetscFunctionReturn(0);
2005b90d0a6eSBarry Smith         }
2006b90d0a6eSBarry Smith       }
2007b90d0a6eSBarry Smith     }
2008b90d0a6eSBarry Smith   }
2009b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
2010b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
2011639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
20123a40ed3dSBarry Smith   PetscFunctionReturn(0);
20139b94acceSBarry Smith }
20149b94acceSBarry Smith 
20154a2ae208SSatish Balay #undef __FUNCT__
2016a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
20175cd90555SBarry Smith /*@C
2018a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
20195cd90555SBarry Smith 
20203f9fe445SBarry Smith    Logically Collective on SNES
2021c7afd0dbSLois Curfman McInnes 
20225cd90555SBarry Smith    Input Parameters:
20235cd90555SBarry Smith .  snes - the SNES context
20245cd90555SBarry Smith 
20251a480d89SAdministrator    Options Database Key:
2026a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
2027a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
2028c7afd0dbSLois Curfman McInnes     set via the options database
20295cd90555SBarry Smith 
20305cd90555SBarry Smith    Notes:
20315cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
20325cd90555SBarry Smith 
203336851e7fSLois Curfman McInnes    Level: intermediate
203436851e7fSLois Curfman McInnes 
20355cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
20365cd90555SBarry Smith 
2037a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
20385cd90555SBarry Smith @*/
20397087cfbeSBarry Smith PetscErrorCode  SNESMonitorCancel(SNES snes)
20405cd90555SBarry Smith {
2041d952e501SBarry Smith   PetscErrorCode ierr;
2042d952e501SBarry Smith   PetscInt       i;
2043d952e501SBarry Smith 
20445cd90555SBarry Smith   PetscFunctionBegin;
20450700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2046d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2047d952e501SBarry Smith     if (snes->monitordestroy[i]) {
2048d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
2049d952e501SBarry Smith     }
2050d952e501SBarry Smith   }
20515cd90555SBarry Smith   snes->numbermonitors = 0;
20525cd90555SBarry Smith   PetscFunctionReturn(0);
20535cd90555SBarry Smith }
20545cd90555SBarry Smith 
20554a2ae208SSatish Balay #undef __FUNCT__
20564a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
20579b94acceSBarry Smith /*@C
20589b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
20599b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
20609b94acceSBarry Smith 
20613f9fe445SBarry Smith    Logically Collective on SNES
2062fee21e36SBarry Smith 
2063c7afd0dbSLois Curfman McInnes    Input Parameters:
2064c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2065c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
20667f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
20677f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
20689b94acceSBarry Smith 
2069c7afd0dbSLois Curfman McInnes    Calling sequence of func:
207006ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2071c7afd0dbSLois Curfman McInnes 
2072c7afd0dbSLois Curfman McInnes +    snes - the SNES context
207306ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2074c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2075184914b5SBarry Smith .    reason - reason for convergence/divergence
2076c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
20774b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
20784b27c08aSLois Curfman McInnes -    f - 2-norm of function
20799b94acceSBarry Smith 
208036851e7fSLois Curfman McInnes    Level: advanced
208136851e7fSLois Curfman McInnes 
20829b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
20839b94acceSBarry Smith 
208485385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
20859b94acceSBarry Smith @*/
20867087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
20879b94acceSBarry Smith {
20887f7931b9SBarry Smith   PetscErrorCode ierr;
20897f7931b9SBarry Smith 
20903a40ed3dSBarry Smith   PetscFunctionBegin;
20910700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
209285385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
20937f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
20947f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
20957f7931b9SBarry Smith   }
209685385478SLisandro Dalcin   snes->ops->converged        = func;
20977f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
209885385478SLisandro Dalcin   snes->cnvP                  = cctx;
20993a40ed3dSBarry Smith   PetscFunctionReturn(0);
21009b94acceSBarry Smith }
21019b94acceSBarry Smith 
21024a2ae208SSatish Balay #undef __FUNCT__
21034a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
210452baeb72SSatish Balay /*@
2105184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2106184914b5SBarry Smith 
2107184914b5SBarry Smith    Not Collective
2108184914b5SBarry Smith 
2109184914b5SBarry Smith    Input Parameter:
2110184914b5SBarry Smith .  snes - the SNES context
2111184914b5SBarry Smith 
2112184914b5SBarry Smith    Output Parameter:
21134d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2114184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2115184914b5SBarry Smith 
2116184914b5SBarry Smith    Level: intermediate
2117184914b5SBarry Smith 
2118184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2119184914b5SBarry Smith 
2120184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2121184914b5SBarry Smith 
212285385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2123184914b5SBarry Smith @*/
21247087cfbeSBarry Smith PetscErrorCode  SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2125184914b5SBarry Smith {
2126184914b5SBarry Smith   PetscFunctionBegin;
21270700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
21284482741eSBarry Smith   PetscValidPointer(reason,2);
2129184914b5SBarry Smith   *reason = snes->reason;
2130184914b5SBarry Smith   PetscFunctionReturn(0);
2131184914b5SBarry Smith }
2132184914b5SBarry Smith 
21334a2ae208SSatish Balay #undef __FUNCT__
21344a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2135c9005455SLois Curfman McInnes /*@
2136c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2137c9005455SLois Curfman McInnes 
21383f9fe445SBarry Smith    Logically Collective on SNES
2139fee21e36SBarry Smith 
2140c7afd0dbSLois Curfman McInnes    Input Parameters:
2141c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
21428c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2143cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2144758f92a0SBarry Smith .  na  - size of a and its
214564731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2146758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2147c7afd0dbSLois Curfman McInnes 
2148308dcc3eSBarry Smith    Notes:
2149308dcc3eSBarry Smith    If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2150308dcc3eSBarry Smith    default array of length 10000 is allocated.
2151308dcc3eSBarry Smith 
2152c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2153c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2154c9005455SLois Curfman McInnes    during the section of code that is being timed.
2155c9005455SLois Curfman McInnes 
215636851e7fSLois Curfman McInnes    Level: intermediate
215736851e7fSLois Curfman McInnes 
2158c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2159758f92a0SBarry Smith 
216008405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2161758f92a0SBarry Smith 
2162c9005455SLois Curfman McInnes @*/
21637087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool  reset)
2164c9005455SLois Curfman McInnes {
2165308dcc3eSBarry Smith   PetscErrorCode ierr;
2166308dcc3eSBarry Smith 
21673a40ed3dSBarry Smith   PetscFunctionBegin;
21680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
21694482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2170a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2171308dcc3eSBarry Smith   if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) {
2172308dcc3eSBarry Smith     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2173308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr);
2174308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr);
2175308dcc3eSBarry Smith     snes->conv_malloc   = PETSC_TRUE;
2176308dcc3eSBarry Smith   }
2177c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2178758f92a0SBarry Smith   snes->conv_hist_its   = its;
2179758f92a0SBarry Smith   snes->conv_hist_max   = na;
2180a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2181758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2182758f92a0SBarry Smith   PetscFunctionReturn(0);
2183758f92a0SBarry Smith }
2184758f92a0SBarry Smith 
2185308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2186c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
2187c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
2188308dcc3eSBarry Smith EXTERN_C_BEGIN
2189308dcc3eSBarry Smith #undef __FUNCT__
2190308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab"
2191308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
2192308dcc3eSBarry Smith {
2193308dcc3eSBarry Smith   mxArray        *mat;
2194308dcc3eSBarry Smith   PetscInt       i;
2195308dcc3eSBarry Smith   PetscReal      *ar;
2196308dcc3eSBarry Smith 
2197308dcc3eSBarry Smith   PetscFunctionBegin;
2198308dcc3eSBarry Smith   mat  = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
2199308dcc3eSBarry Smith   ar   = (PetscReal*) mxGetData(mat);
2200308dcc3eSBarry Smith   for (i=0; i<snes->conv_hist_len; i++) {
2201308dcc3eSBarry Smith     ar[i] = snes->conv_hist[i];
2202308dcc3eSBarry Smith   }
2203308dcc3eSBarry Smith   PetscFunctionReturn(mat);
2204308dcc3eSBarry Smith }
2205308dcc3eSBarry Smith EXTERN_C_END
2206308dcc3eSBarry Smith #endif
2207308dcc3eSBarry Smith 
2208308dcc3eSBarry Smith 
22094a2ae208SSatish Balay #undef __FUNCT__
22104a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
22110c4c9dddSBarry Smith /*@C
2212758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2213758f92a0SBarry Smith 
22143f9fe445SBarry Smith    Not Collective
2215758f92a0SBarry Smith 
2216758f92a0SBarry Smith    Input Parameter:
2217758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2218758f92a0SBarry Smith 
2219758f92a0SBarry Smith    Output Parameters:
2220758f92a0SBarry Smith .  a   - array to hold history
2221758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2222758f92a0SBarry Smith          negative if not converged) for each solve.
2223758f92a0SBarry Smith -  na  - size of a and its
2224758f92a0SBarry Smith 
2225758f92a0SBarry Smith    Notes:
2226758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2227758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2228758f92a0SBarry Smith 
2229758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2230758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2231758f92a0SBarry Smith    during the section of code that is being timed.
2232758f92a0SBarry Smith 
2233758f92a0SBarry Smith    Level: intermediate
2234758f92a0SBarry Smith 
2235758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2236758f92a0SBarry Smith 
2237758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2238758f92a0SBarry Smith 
2239758f92a0SBarry Smith @*/
22407087cfbeSBarry Smith PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2241758f92a0SBarry Smith {
2242758f92a0SBarry Smith   PetscFunctionBegin;
22430700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2244758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2245758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2246758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
22473a40ed3dSBarry Smith   PetscFunctionReturn(0);
2248c9005455SLois Curfman McInnes }
2249c9005455SLois Curfman McInnes 
2250e74ef692SMatthew Knepley #undef __FUNCT__
2251e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2252ac226902SBarry Smith /*@C
225376b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
2254eec8f646SJed Brown   at the beginning of every iteration of the nonlinear solve. Specifically
22557e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
225676b2cf59SMatthew Knepley 
22573f9fe445SBarry Smith   Logically Collective on SNES
225876b2cf59SMatthew Knepley 
225976b2cf59SMatthew Knepley   Input Parameters:
226076b2cf59SMatthew Knepley . snes - The nonlinear solver context
226176b2cf59SMatthew Knepley . func - The function
226276b2cf59SMatthew Knepley 
226376b2cf59SMatthew Knepley   Calling sequence of func:
2264b5d30489SBarry Smith . func (SNES snes, PetscInt step);
226576b2cf59SMatthew Knepley 
226676b2cf59SMatthew Knepley . step - The current step of the iteration
226776b2cf59SMatthew Knepley 
2268fe97e370SBarry Smith   Level: advanced
2269fe97e370SBarry Smith 
2270fe97e370SBarry 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()
2271fe97e370SBarry Smith         This is not used by most users.
227276b2cf59SMatthew Knepley 
227376b2cf59SMatthew Knepley .keywords: SNES, update
2274b5d30489SBarry Smith 
227585385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
227676b2cf59SMatthew Knepley @*/
22777087cfbeSBarry Smith PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
227876b2cf59SMatthew Knepley {
227976b2cf59SMatthew Knepley   PetscFunctionBegin;
22800700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2281e7788613SBarry Smith   snes->ops->update = func;
228276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
228376b2cf59SMatthew Knepley }
228476b2cf59SMatthew Knepley 
2285e74ef692SMatthew Knepley #undef __FUNCT__
2286e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
228776b2cf59SMatthew Knepley /*@
228876b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
228976b2cf59SMatthew Knepley 
229076b2cf59SMatthew Knepley   Not collective
229176b2cf59SMatthew Knepley 
229276b2cf59SMatthew Knepley   Input Parameters:
229376b2cf59SMatthew Knepley . snes - The nonlinear solver context
229476b2cf59SMatthew Knepley . step - The current step of the iteration
229576b2cf59SMatthew Knepley 
2296205452f4SMatthew Knepley   Level: intermediate
2297205452f4SMatthew Knepley 
229876b2cf59SMatthew Knepley .keywords: SNES, update
2299a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
230076b2cf59SMatthew Knepley @*/
23017087cfbeSBarry Smith PetscErrorCode  SNESDefaultUpdate(SNES snes, PetscInt step)
230276b2cf59SMatthew Knepley {
230376b2cf59SMatthew Knepley   PetscFunctionBegin;
230476b2cf59SMatthew Knepley   PetscFunctionReturn(0);
230576b2cf59SMatthew Knepley }
230676b2cf59SMatthew Knepley 
23074a2ae208SSatish Balay #undef __FUNCT__
23084a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
23099b94acceSBarry Smith /*
23109b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
23119b94acceSBarry Smith    positive parameter delta.
23129b94acceSBarry Smith 
23139b94acceSBarry Smith     Input Parameters:
2314c7afd0dbSLois Curfman McInnes +   snes - the SNES context
23159b94acceSBarry Smith .   y - approximate solution of linear system
23169b94acceSBarry Smith .   fnorm - 2-norm of current function
2317c7afd0dbSLois Curfman McInnes -   delta - trust region size
23189b94acceSBarry Smith 
23199b94acceSBarry Smith     Output Parameters:
2320c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
23219b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
23229b94acceSBarry Smith     region, and exceeds zero otherwise.
2323c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
23249b94acceSBarry Smith 
23259b94acceSBarry Smith     Note:
23264b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
23279b94acceSBarry Smith     is set to be the maximum allowable step size.
23289b94acceSBarry Smith 
23299b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
23309b94acceSBarry Smith */
2331dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
23329b94acceSBarry Smith {
2333064f8208SBarry Smith   PetscReal      nrm;
2334ea709b57SSatish Balay   PetscScalar    cnorm;
2335dfbe8321SBarry Smith   PetscErrorCode ierr;
23363a40ed3dSBarry Smith 
23373a40ed3dSBarry Smith   PetscFunctionBegin;
23380700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23390700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2340c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2341184914b5SBarry Smith 
2342064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2343064f8208SBarry Smith   if (nrm > *delta) {
2344064f8208SBarry Smith      nrm = *delta/nrm;
2345064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2346064f8208SBarry Smith      cnorm = nrm;
23472dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
23489b94acceSBarry Smith      *ynorm = *delta;
23499b94acceSBarry Smith   } else {
23509b94acceSBarry Smith      *gpnorm = 0.0;
2351064f8208SBarry Smith      *ynorm = nrm;
23529b94acceSBarry Smith   }
23533a40ed3dSBarry Smith   PetscFunctionReturn(0);
23549b94acceSBarry Smith }
23559b94acceSBarry Smith 
23564a2ae208SSatish Balay #undef __FUNCT__
23574a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
23586ce558aeSBarry Smith /*@C
2359f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2360f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
23619b94acceSBarry Smith 
2362c7afd0dbSLois Curfman McInnes    Collective on SNES
2363c7afd0dbSLois Curfman McInnes 
2364b2002411SLois Curfman McInnes    Input Parameters:
2365c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2366f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
236785385478SLisandro Dalcin -  x - the solution vector.
23689b94acceSBarry Smith 
2369b2002411SLois Curfman McInnes    Notes:
23708ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
23718ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
23728ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
23738ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
23748ddd3da0SLois Curfman McInnes 
237536851e7fSLois Curfman McInnes    Level: beginner
237636851e7fSLois Curfman McInnes 
23779b94acceSBarry Smith .keywords: SNES, nonlinear, solve
23789b94acceSBarry Smith 
237985385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
23809b94acceSBarry Smith @*/
23817087cfbeSBarry Smith PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
23829b94acceSBarry Smith {
2383dfbe8321SBarry Smith   PetscErrorCode ierr;
2384ace3abfcSBarry Smith   PetscBool      flg;
2385eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2386eabae89aSBarry Smith   PetscViewer    viewer;
2387052efed2SBarry Smith 
23883a40ed3dSBarry Smith   PetscFunctionBegin;
23890700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23900700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2391f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
23920700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
239385385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
239485385478SLisandro Dalcin 
239585385478SLisandro Dalcin   /* set solution vector */
239685385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
239785385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
239885385478SLisandro Dalcin   snes->vec_sol = x;
239985385478SLisandro Dalcin   /* set afine vector if provided */
240085385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
240185385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
240285385478SLisandro Dalcin   snes->vec_rhs = b;
240385385478SLisandro Dalcin 
240470e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
24053f149594SLisandro Dalcin 
2406abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
240750ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2408d5e45103SBarry Smith 
24093f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
24104936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
241185385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
24124936397dSBarry Smith   if (snes->domainerror){
24134936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
24144936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
24154936397dSBarry Smith   }
241617186662SBarry Smith   if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
24173f149594SLisandro Dalcin 
24187adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2419eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
24207adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2421eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2422eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2423eabae89aSBarry Smith   }
2424eabae89aSBarry Smith 
242590d69ab7SBarry Smith   flg  = PETSC_FALSE;
2426acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2427da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
24285968eb51SBarry Smith   if (snes->printreason) {
24295968eb51SBarry Smith     if (snes->reason > 0) {
24307adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
24315968eb51SBarry Smith     } else {
24327adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
24335968eb51SBarry Smith     }
24345968eb51SBarry Smith   }
24355968eb51SBarry Smith 
2436e113a28aSBarry Smith   if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
24373a40ed3dSBarry Smith   PetscFunctionReturn(0);
24389b94acceSBarry Smith }
24399b94acceSBarry Smith 
24409b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
24419b94acceSBarry Smith 
24424a2ae208SSatish Balay #undef __FUNCT__
24434a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
244482bf6240SBarry Smith /*@C
24454b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
24469b94acceSBarry Smith 
2447fee21e36SBarry Smith    Collective on SNES
2448fee21e36SBarry Smith 
2449c7afd0dbSLois Curfman McInnes    Input Parameters:
2450c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2451454a90a3SBarry Smith -  type - a known method
2452c7afd0dbSLois Curfman McInnes 
2453c7afd0dbSLois Curfman McInnes    Options Database Key:
2454454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2455c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2456ae12b187SLois Curfman McInnes 
24579b94acceSBarry Smith    Notes:
2458e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
24594b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2460c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
24614b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2462c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
24639b94acceSBarry Smith 
2464ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2465ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2466ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2467ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2468ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2469ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2470ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2471ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2472ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2473b0a32e0cSBarry Smith   appropriate method.
247436851e7fSLois Curfman McInnes 
247536851e7fSLois Curfman McInnes   Level: intermediate
2476a703fe33SLois Curfman McInnes 
2477454a90a3SBarry Smith .keywords: SNES, set, type
2478435da068SBarry Smith 
2479435da068SBarry Smith .seealso: SNESType, SNESCreate()
2480435da068SBarry Smith 
24819b94acceSBarry Smith @*/
24827087cfbeSBarry Smith PetscErrorCode  SNESSetType(SNES snes,const SNESType type)
24839b94acceSBarry Smith {
2484dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
2485ace3abfcSBarry Smith   PetscBool      match;
24863a40ed3dSBarry Smith 
24873a40ed3dSBarry Smith   PetscFunctionBegin;
24880700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24894482741eSBarry Smith   PetscValidCharPointer(type,2);
249082bf6240SBarry Smith 
24916831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
24920f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
249392ff6ae8SBarry Smith 
24944b91b6eaSBarry Smith   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2495e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
249675396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
249775396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
249875396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
249975396ef9SLisandro Dalcin   snes->ops->setup          = 0;
250075396ef9SLisandro Dalcin   snes->ops->solve          = 0;
250175396ef9SLisandro Dalcin   snes->ops->view           = 0;
250275396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
250375396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
250475396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
250575396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
25063a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2507454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
25089fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
25099fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
25109fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr);
25119fb22e1aSBarry Smith   }
25129fb22e1aSBarry Smith #endif
25133a40ed3dSBarry Smith   PetscFunctionReturn(0);
25149b94acceSBarry Smith }
25159b94acceSBarry Smith 
2516a847f771SSatish Balay 
25179b94acceSBarry Smith /* --------------------------------------------------------------------- */
25184a2ae208SSatish Balay #undef __FUNCT__
25194a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
252052baeb72SSatish Balay /*@
25219b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2522f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
25239b94acceSBarry Smith 
2524fee21e36SBarry Smith    Not Collective
2525fee21e36SBarry Smith 
252636851e7fSLois Curfman McInnes    Level: advanced
252736851e7fSLois Curfman McInnes 
25289b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
25299b94acceSBarry Smith 
25309b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
25319b94acceSBarry Smith @*/
25327087cfbeSBarry Smith PetscErrorCode  SNESRegisterDestroy(void)
25339b94acceSBarry Smith {
2534dfbe8321SBarry Smith   PetscErrorCode ierr;
253582bf6240SBarry Smith 
25363a40ed3dSBarry Smith   PetscFunctionBegin;
25371441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
25384c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
25393a40ed3dSBarry Smith   PetscFunctionReturn(0);
25409b94acceSBarry Smith }
25419b94acceSBarry Smith 
25424a2ae208SSatish Balay #undef __FUNCT__
25434a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
25449b94acceSBarry Smith /*@C
25459a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
25469b94acceSBarry Smith 
2547c7afd0dbSLois Curfman McInnes    Not Collective
2548c7afd0dbSLois Curfman McInnes 
25499b94acceSBarry Smith    Input Parameter:
25504b0e389bSBarry Smith .  snes - nonlinear solver context
25519b94acceSBarry Smith 
25529b94acceSBarry Smith    Output Parameter:
25533a7fca6bSBarry Smith .  type - SNES method (a character string)
25549b94acceSBarry Smith 
255536851e7fSLois Curfman McInnes    Level: intermediate
255636851e7fSLois Curfman McInnes 
2557454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
25589b94acceSBarry Smith @*/
25597087cfbeSBarry Smith PetscErrorCode  SNESGetType(SNES snes,const SNESType *type)
25609b94acceSBarry Smith {
25613a40ed3dSBarry Smith   PetscFunctionBegin;
25620700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25634482741eSBarry Smith   PetscValidPointer(type,2);
25647adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
25653a40ed3dSBarry Smith   PetscFunctionReturn(0);
25669b94acceSBarry Smith }
25679b94acceSBarry Smith 
25684a2ae208SSatish Balay #undef __FUNCT__
25694a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
257052baeb72SSatish Balay /*@
25719b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
25729b94acceSBarry Smith    stored.
25739b94acceSBarry Smith 
2574c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2575c7afd0dbSLois Curfman McInnes 
25769b94acceSBarry Smith    Input Parameter:
25779b94acceSBarry Smith .  snes - the SNES context
25789b94acceSBarry Smith 
25799b94acceSBarry Smith    Output Parameter:
25809b94acceSBarry Smith .  x - the solution
25819b94acceSBarry Smith 
258270e92668SMatthew Knepley    Level: intermediate
258336851e7fSLois Curfman McInnes 
25849b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
25859b94acceSBarry Smith 
258685385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
25879b94acceSBarry Smith @*/
25887087cfbeSBarry Smith PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
25899b94acceSBarry Smith {
25903a40ed3dSBarry Smith   PetscFunctionBegin;
25910700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25924482741eSBarry Smith   PetscValidPointer(x,2);
259385385478SLisandro Dalcin   *x = snes->vec_sol;
259470e92668SMatthew Knepley   PetscFunctionReturn(0);
259570e92668SMatthew Knepley }
259670e92668SMatthew Knepley 
259770e92668SMatthew Knepley #undef __FUNCT__
25984a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
259952baeb72SSatish Balay /*@
26009b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
26019b94acceSBarry Smith    stored.
26029b94acceSBarry Smith 
2603c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2604c7afd0dbSLois Curfman McInnes 
26059b94acceSBarry Smith    Input Parameter:
26069b94acceSBarry Smith .  snes - the SNES context
26079b94acceSBarry Smith 
26089b94acceSBarry Smith    Output Parameter:
26099b94acceSBarry Smith .  x - the solution update
26109b94acceSBarry Smith 
261136851e7fSLois Curfman McInnes    Level: advanced
261236851e7fSLois Curfman McInnes 
26139b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
26149b94acceSBarry Smith 
261585385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
26169b94acceSBarry Smith @*/
26177087cfbeSBarry Smith PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
26189b94acceSBarry Smith {
26193a40ed3dSBarry Smith   PetscFunctionBegin;
26200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26214482741eSBarry Smith   PetscValidPointer(x,2);
262285385478SLisandro Dalcin   *x = snes->vec_sol_update;
26233a40ed3dSBarry Smith   PetscFunctionReturn(0);
26249b94acceSBarry Smith }
26259b94acceSBarry Smith 
26264a2ae208SSatish Balay #undef __FUNCT__
26274a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
26289b94acceSBarry Smith /*@C
26293638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
26309b94acceSBarry Smith 
2631c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2632c7afd0dbSLois Curfman McInnes 
26339b94acceSBarry Smith    Input Parameter:
26349b94acceSBarry Smith .  snes - the SNES context
26359b94acceSBarry Smith 
26369b94acceSBarry Smith    Output Parameter:
26377bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
263870e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
263970e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
26409b94acceSBarry Smith 
264136851e7fSLois Curfman McInnes    Level: advanced
264236851e7fSLois Curfman McInnes 
2643a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
26449b94acceSBarry Smith 
26454b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
26469b94acceSBarry Smith @*/
26477087cfbeSBarry Smith PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
26489b94acceSBarry Smith {
26493a40ed3dSBarry Smith   PetscFunctionBegin;
26500700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
265185385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2652e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
265370e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
26543a40ed3dSBarry Smith   PetscFunctionReturn(0);
26559b94acceSBarry Smith }
26569b94acceSBarry Smith 
26574a2ae208SSatish Balay #undef __FUNCT__
26584a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
26593c7409f5SSatish Balay /*@C
26603c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2661d850072dSLois Curfman McInnes    SNES options in the database.
26623c7409f5SSatish Balay 
26633f9fe445SBarry Smith    Logically Collective on SNES
2664fee21e36SBarry Smith 
2665c7afd0dbSLois Curfman McInnes    Input Parameter:
2666c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2667c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2668c7afd0dbSLois Curfman McInnes 
2669d850072dSLois Curfman McInnes    Notes:
2670a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2671c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2672d850072dSLois Curfman McInnes 
267336851e7fSLois Curfman McInnes    Level: advanced
267436851e7fSLois Curfman McInnes 
26753c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2676a86d99e1SLois Curfman McInnes 
2677a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
26783c7409f5SSatish Balay @*/
26797087cfbeSBarry Smith PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
26803c7409f5SSatish Balay {
2681dfbe8321SBarry Smith   PetscErrorCode ierr;
26823c7409f5SSatish Balay 
26833a40ed3dSBarry Smith   PetscFunctionBegin;
26840700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2685639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26861cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
268794b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
26883a40ed3dSBarry Smith   PetscFunctionReturn(0);
26893c7409f5SSatish Balay }
26903c7409f5SSatish Balay 
26914a2ae208SSatish Balay #undef __FUNCT__
26924a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
26933c7409f5SSatish Balay /*@C
2694f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2695d850072dSLois Curfman McInnes    SNES options in the database.
26963c7409f5SSatish Balay 
26973f9fe445SBarry Smith    Logically Collective on SNES
2698fee21e36SBarry Smith 
2699c7afd0dbSLois Curfman McInnes    Input Parameters:
2700c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2701c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2702c7afd0dbSLois Curfman McInnes 
2703d850072dSLois Curfman McInnes    Notes:
2704a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2705c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2706d850072dSLois Curfman McInnes 
270736851e7fSLois Curfman McInnes    Level: advanced
270836851e7fSLois Curfman McInnes 
27093c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2710a86d99e1SLois Curfman McInnes 
2711a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
27123c7409f5SSatish Balay @*/
27137087cfbeSBarry Smith PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
27143c7409f5SSatish Balay {
2715dfbe8321SBarry Smith   PetscErrorCode ierr;
27163c7409f5SSatish Balay 
27173a40ed3dSBarry Smith   PetscFunctionBegin;
27180700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2719639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
27201cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
272194b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
27223a40ed3dSBarry Smith   PetscFunctionReturn(0);
27233c7409f5SSatish Balay }
27243c7409f5SSatish Balay 
27254a2ae208SSatish Balay #undef __FUNCT__
27264a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
27279ab63eb5SSatish Balay /*@C
27283c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
27293c7409f5SSatish Balay    SNES options in the database.
27303c7409f5SSatish Balay 
2731c7afd0dbSLois Curfman McInnes    Not Collective
2732c7afd0dbSLois Curfman McInnes 
27333c7409f5SSatish Balay    Input Parameter:
27343c7409f5SSatish Balay .  snes - the SNES context
27353c7409f5SSatish Balay 
27363c7409f5SSatish Balay    Output Parameter:
27373c7409f5SSatish Balay .  prefix - pointer to the prefix string used
27383c7409f5SSatish Balay 
27394ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
27409ab63eb5SSatish Balay    sufficient length to hold the prefix.
27419ab63eb5SSatish Balay 
274236851e7fSLois Curfman McInnes    Level: advanced
274336851e7fSLois Curfman McInnes 
27443c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2745a86d99e1SLois Curfman McInnes 
2746a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
27473c7409f5SSatish Balay @*/
27487087cfbeSBarry Smith PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
27493c7409f5SSatish Balay {
2750dfbe8321SBarry Smith   PetscErrorCode ierr;
27513c7409f5SSatish Balay 
27523a40ed3dSBarry Smith   PetscFunctionBegin;
27530700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2754639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
27553a40ed3dSBarry Smith   PetscFunctionReturn(0);
27563c7409f5SSatish Balay }
27573c7409f5SSatish Balay 
2758b2002411SLois Curfman McInnes 
27594a2ae208SSatish Balay #undef __FUNCT__
27604a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
27613cea93caSBarry Smith /*@C
27623cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
27633cea93caSBarry Smith 
27647f6c08e0SMatthew Knepley   Level: advanced
27653cea93caSBarry Smith @*/
27667087cfbeSBarry Smith PetscErrorCode  SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2767b2002411SLois Curfman McInnes {
2768e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2769dfbe8321SBarry Smith   PetscErrorCode ierr;
2770b2002411SLois Curfman McInnes 
2771b2002411SLois Curfman McInnes   PetscFunctionBegin;
2772b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2773c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2774b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2775b2002411SLois Curfman McInnes }
2776da9b6338SBarry Smith 
2777da9b6338SBarry Smith #undef __FUNCT__
2778da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
27797087cfbeSBarry Smith PetscErrorCode  SNESTestLocalMin(SNES snes)
2780da9b6338SBarry Smith {
2781dfbe8321SBarry Smith   PetscErrorCode ierr;
278277431f27SBarry Smith   PetscInt       N,i,j;
2783da9b6338SBarry Smith   Vec            u,uh,fh;
2784da9b6338SBarry Smith   PetscScalar    value;
2785da9b6338SBarry Smith   PetscReal      norm;
2786da9b6338SBarry Smith 
2787da9b6338SBarry Smith   PetscFunctionBegin;
2788da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2789da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2790da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2791da9b6338SBarry Smith 
2792da9b6338SBarry Smith   /* currently only works for sequential */
2793da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2794da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2795da9b6338SBarry Smith   for (i=0; i<N; i++) {
2796da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
279777431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2798da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2799ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2800da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
28013ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2802da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
280377431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2804da9b6338SBarry Smith       value = -value;
2805da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2806da9b6338SBarry Smith     }
2807da9b6338SBarry Smith   }
2808da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2809da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2810da9b6338SBarry Smith   PetscFunctionReturn(0);
2811da9b6338SBarry Smith }
281271f87433Sdalcinl 
281371f87433Sdalcinl #undef __FUNCT__
2814fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
281571f87433Sdalcinl /*@
2816fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
281771f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
281871f87433Sdalcinl    Newton method.
281971f87433Sdalcinl 
28203f9fe445SBarry Smith    Logically Collective on SNES
282171f87433Sdalcinl 
282271f87433Sdalcinl    Input Parameters:
282371f87433Sdalcinl +  snes - SNES context
282471f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
282571f87433Sdalcinl 
282664ba62caSBarry Smith     Options Database:
282764ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
282864ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
282964ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
283064ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
283164ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
283264ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
283364ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
283464ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
283564ba62caSBarry Smith 
283671f87433Sdalcinl    Notes:
283771f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
283871f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
283971f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
284071f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
284171f87433Sdalcinl    solver.
284271f87433Sdalcinl 
284371f87433Sdalcinl    Level: advanced
284471f87433Sdalcinl 
284571f87433Sdalcinl    Reference:
284671f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
284771f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
284871f87433Sdalcinl 
284971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
285071f87433Sdalcinl 
2851fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
285271f87433Sdalcinl @*/
28537087cfbeSBarry Smith PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool  flag)
285471f87433Sdalcinl {
285571f87433Sdalcinl   PetscFunctionBegin;
28560700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2857acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flag,2);
285871f87433Sdalcinl   snes->ksp_ewconv = flag;
285971f87433Sdalcinl   PetscFunctionReturn(0);
286071f87433Sdalcinl }
286171f87433Sdalcinl 
286271f87433Sdalcinl #undef __FUNCT__
2863fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
286471f87433Sdalcinl /*@
2865fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
286671f87433Sdalcinl    for computing relative tolerance for linear solvers within an
286771f87433Sdalcinl    inexact Newton method.
286871f87433Sdalcinl 
286971f87433Sdalcinl    Not Collective
287071f87433Sdalcinl 
287171f87433Sdalcinl    Input Parameter:
287271f87433Sdalcinl .  snes - SNES context
287371f87433Sdalcinl 
287471f87433Sdalcinl    Output Parameter:
287571f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
287671f87433Sdalcinl 
287771f87433Sdalcinl    Notes:
287871f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
287971f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
288071f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
288171f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
288271f87433Sdalcinl    solver.
288371f87433Sdalcinl 
288471f87433Sdalcinl    Level: advanced
288571f87433Sdalcinl 
288671f87433Sdalcinl    Reference:
288771f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
288871f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
288971f87433Sdalcinl 
289071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
289171f87433Sdalcinl 
2892fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
289371f87433Sdalcinl @*/
28947087cfbeSBarry Smith PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
289571f87433Sdalcinl {
289671f87433Sdalcinl   PetscFunctionBegin;
28970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
289871f87433Sdalcinl   PetscValidPointer(flag,2);
289971f87433Sdalcinl   *flag = snes->ksp_ewconv;
290071f87433Sdalcinl   PetscFunctionReturn(0);
290171f87433Sdalcinl }
290271f87433Sdalcinl 
290371f87433Sdalcinl #undef __FUNCT__
2904fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
290571f87433Sdalcinl /*@
2906fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
290771f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
290871f87433Sdalcinl    Newton method.
290971f87433Sdalcinl 
29103f9fe445SBarry Smith    Logically Collective on SNES
291171f87433Sdalcinl 
291271f87433Sdalcinl    Input Parameters:
291371f87433Sdalcinl +    snes - SNES context
291471f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
291571f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
291671f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
291771f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
291871f87433Sdalcinl              (0 <= gamma2 <= 1)
291971f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
292071f87433Sdalcinl .    alpha2 - power for safeguard
292171f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
292271f87433Sdalcinl 
292371f87433Sdalcinl    Note:
292471f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
292571f87433Sdalcinl 
292671f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
292771f87433Sdalcinl 
292871f87433Sdalcinl    Level: advanced
292971f87433Sdalcinl 
293071f87433Sdalcinl    Reference:
293171f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
293271f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
293371f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
293471f87433Sdalcinl 
293571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
293671f87433Sdalcinl 
2937fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
293871f87433Sdalcinl @*/
29397087cfbeSBarry Smith PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
294071f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
294171f87433Sdalcinl {
2942fa9f3622SBarry Smith   SNESKSPEW *kctx;
294371f87433Sdalcinl   PetscFunctionBegin;
29440700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2945fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
2946e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
2947c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
2948c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
2949c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
2950c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
2951c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
2952c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
2953c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
295471f87433Sdalcinl 
295571f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
295671f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
295771f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
295871f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
295971f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
296071f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
296171f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
296271f87433Sdalcinl 
296371f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
2964e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
296571f87433Sdalcinl   }
296671f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
2967e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
296871f87433Sdalcinl   }
296971f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
2970e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
297171f87433Sdalcinl   }
297271f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
2973e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
297471f87433Sdalcinl   }
297571f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
2976e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
297771f87433Sdalcinl   }
297871f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
2979e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
298071f87433Sdalcinl   }
298171f87433Sdalcinl   PetscFunctionReturn(0);
298271f87433Sdalcinl }
298371f87433Sdalcinl 
298471f87433Sdalcinl #undef __FUNCT__
2985fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
298671f87433Sdalcinl /*@
2987fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
298871f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
298971f87433Sdalcinl    Newton method.
299071f87433Sdalcinl 
299171f87433Sdalcinl    Not Collective
299271f87433Sdalcinl 
299371f87433Sdalcinl    Input Parameters:
299471f87433Sdalcinl      snes - SNES context
299571f87433Sdalcinl 
299671f87433Sdalcinl    Output Parameters:
299771f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
299871f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
299971f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
300071f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
300171f87433Sdalcinl              (0 <= gamma2 <= 1)
300271f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
300371f87433Sdalcinl .    alpha2 - power for safeguard
300471f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
300571f87433Sdalcinl 
300671f87433Sdalcinl    Level: advanced
300771f87433Sdalcinl 
300871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
300971f87433Sdalcinl 
3010fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
301171f87433Sdalcinl @*/
30127087cfbeSBarry Smith PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
301371f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
301471f87433Sdalcinl {
3015fa9f3622SBarry Smith   SNESKSPEW *kctx;
301671f87433Sdalcinl   PetscFunctionBegin;
30170700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3018fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3019e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
302071f87433Sdalcinl   if(version)   *version   = kctx->version;
302171f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
302271f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
302371f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
302471f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
302571f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
302671f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
302771f87433Sdalcinl   PetscFunctionReturn(0);
302871f87433Sdalcinl }
302971f87433Sdalcinl 
303071f87433Sdalcinl #undef __FUNCT__
3031fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
3032fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
303371f87433Sdalcinl {
303471f87433Sdalcinl   PetscErrorCode ierr;
3035fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
303671f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
303771f87433Sdalcinl 
303871f87433Sdalcinl   PetscFunctionBegin;
3039e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
304071f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
304171f87433Sdalcinl     rtol = kctx->rtol_0;
304271f87433Sdalcinl   } else {
304371f87433Sdalcinl     if (kctx->version == 1) {
304471f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
304571f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
304671f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
304771f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
304871f87433Sdalcinl     } else if (kctx->version == 2) {
304971f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
305071f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
305171f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
305271f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
305371f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
305471f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
305571f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
305671f87433Sdalcinl       stol = PetscMax(rtol,stol);
305771f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
305871f87433Sdalcinl       /* safeguard: avoid oversolving */
305971f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
306071f87433Sdalcinl       stol = PetscMax(rtol,stol);
306171f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
3062e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
306371f87433Sdalcinl   }
306471f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
306571f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
306671f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
306771f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
306871f87433Sdalcinl   PetscFunctionReturn(0);
306971f87433Sdalcinl }
307071f87433Sdalcinl 
307171f87433Sdalcinl #undef __FUNCT__
3072fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
3073fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
307471f87433Sdalcinl {
307571f87433Sdalcinl   PetscErrorCode ierr;
3076fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
307771f87433Sdalcinl   PCSide         pcside;
307871f87433Sdalcinl   Vec            lres;
307971f87433Sdalcinl 
308071f87433Sdalcinl   PetscFunctionBegin;
3081e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
308271f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
308371f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
308471f87433Sdalcinl   if (kctx->version == 1) {
3085b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
308671f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
308771f87433Sdalcinl       /* KSP residual is true linear residual */
308871f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
308971f87433Sdalcinl     } else {
309071f87433Sdalcinl       /* KSP residual is preconditioned residual */
309171f87433Sdalcinl       /* compute true linear residual norm */
309271f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
309371f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
309471f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
309571f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
309671f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
309771f87433Sdalcinl     }
309871f87433Sdalcinl   }
309971f87433Sdalcinl   PetscFunctionReturn(0);
310071f87433Sdalcinl }
310171f87433Sdalcinl 
310271f87433Sdalcinl #undef __FUNCT__
310371f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
310471f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
310571f87433Sdalcinl {
310671f87433Sdalcinl   PetscErrorCode ierr;
310771f87433Sdalcinl 
310871f87433Sdalcinl   PetscFunctionBegin;
3109fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
311071f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3111fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
311271f87433Sdalcinl   PetscFunctionReturn(0);
311371f87433Sdalcinl }
31146c699258SBarry Smith 
31156c699258SBarry Smith #undef __FUNCT__
31166c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
31176c699258SBarry Smith /*@
31186c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
31196c699258SBarry Smith 
31203f9fe445SBarry Smith    Logically Collective on SNES
31216c699258SBarry Smith 
31226c699258SBarry Smith    Input Parameters:
31236c699258SBarry Smith +  snes - the preconditioner context
31246c699258SBarry Smith -  dm - the dm
31256c699258SBarry Smith 
31266c699258SBarry Smith    Level: intermediate
31276c699258SBarry Smith 
31286c699258SBarry Smith 
31296c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
31306c699258SBarry Smith @*/
31317087cfbeSBarry Smith PetscErrorCode  SNESSetDM(SNES snes,DM dm)
31326c699258SBarry Smith {
31336c699258SBarry Smith   PetscErrorCode ierr;
3134345fed2cSBarry Smith   KSP            ksp;
31356c699258SBarry Smith 
31366c699258SBarry Smith   PetscFunctionBegin;
31370700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
313870663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
31396c699258SBarry Smith   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
31406c699258SBarry Smith   snes->dm = dm;
3141345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3142345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3143f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
31446c699258SBarry Smith   PetscFunctionReturn(0);
31456c699258SBarry Smith }
31466c699258SBarry Smith 
31476c699258SBarry Smith #undef __FUNCT__
31486c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
31496c699258SBarry Smith /*@
31506c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
31516c699258SBarry Smith 
31523f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
31536c699258SBarry Smith 
31546c699258SBarry Smith    Input Parameter:
31556c699258SBarry Smith . snes - the preconditioner context
31566c699258SBarry Smith 
31576c699258SBarry Smith    Output Parameter:
31586c699258SBarry Smith .  dm - the dm
31596c699258SBarry Smith 
31606c699258SBarry Smith    Level: intermediate
31616c699258SBarry Smith 
31626c699258SBarry Smith 
31636c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
31646c699258SBarry Smith @*/
31657087cfbeSBarry Smith PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
31666c699258SBarry Smith {
31676c699258SBarry Smith   PetscFunctionBegin;
31680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31696c699258SBarry Smith   *dm = snes->dm;
31706c699258SBarry Smith   PetscFunctionReturn(0);
31716c699258SBarry Smith }
31720807856dSBarry Smith 
317369b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3174c6db04a5SJed Brown #include <mex.h>
317569b4f73cSBarry Smith 
31768f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
31778f6e6473SBarry Smith 
31780807856dSBarry Smith #undef __FUNCT__
31790807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab"
31800807856dSBarry Smith /*
31810807856dSBarry Smith    SNESComputeFunction_Matlab - Calls the function that has been set with
31820807856dSBarry Smith                          SNESSetFunctionMatlab().
31830807856dSBarry Smith 
31840807856dSBarry Smith    Collective on SNES
31850807856dSBarry Smith 
31860807856dSBarry Smith    Input Parameters:
31870807856dSBarry Smith +  snes - the SNES context
31880807856dSBarry Smith -  x - input vector
31890807856dSBarry Smith 
31900807856dSBarry Smith    Output Parameter:
31910807856dSBarry Smith .  y - function vector, as set by SNESSetFunction()
31920807856dSBarry Smith 
31930807856dSBarry Smith    Notes:
31940807856dSBarry Smith    SNESComputeFunction() is typically used within nonlinear solvers
31950807856dSBarry Smith    implementations, so most users would not generally call this routine
31960807856dSBarry Smith    themselves.
31970807856dSBarry Smith 
31980807856dSBarry Smith    Level: developer
31990807856dSBarry Smith 
32000807856dSBarry Smith .keywords: SNES, nonlinear, compute, function
32010807856dSBarry Smith 
32020807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
320361b2408cSBarry Smith */
32047087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx)
32050807856dSBarry Smith {
3206e650e774SBarry Smith   PetscErrorCode    ierr;
32078f6e6473SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
32088f6e6473SBarry Smith   int               nlhs = 1,nrhs = 5;
32098f6e6473SBarry Smith   mxArray	    *plhs[1],*prhs[5];
321091621f2eSBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
3211e650e774SBarry Smith 
32120807856dSBarry Smith   PetscFunctionBegin;
32130807856dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32140807856dSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
32150807856dSBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
32160807856dSBarry Smith   PetscCheckSameComm(snes,1,x,2);
32170807856dSBarry Smith   PetscCheckSameComm(snes,1,y,3);
32180807856dSBarry Smith 
32190807856dSBarry Smith   /* call Matlab function in ctx with arguments x and y */
3220e650e774SBarry Smith 
322191621f2eSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3222e650e774SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3223e650e774SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
322491621f2eSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
322591621f2eSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
322691621f2eSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
32278f6e6473SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
32288f6e6473SBarry Smith   prhs[4] =  sctx->ctx;
3229b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr);
3230e650e774SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3231e650e774SBarry Smith   mxDestroyArray(prhs[0]);
3232e650e774SBarry Smith   mxDestroyArray(prhs[1]);
3233e650e774SBarry Smith   mxDestroyArray(prhs[2]);
32348f6e6473SBarry Smith   mxDestroyArray(prhs[3]);
3235e650e774SBarry Smith   mxDestroyArray(plhs[0]);
32360807856dSBarry Smith   PetscFunctionReturn(0);
32370807856dSBarry Smith }
32380807856dSBarry Smith 
32390807856dSBarry Smith 
32400807856dSBarry Smith #undef __FUNCT__
32410807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab"
324261b2408cSBarry Smith /*
32430807856dSBarry Smith    SNESSetFunctionMatlab - Sets the function evaluation routine and function
32440807856dSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3245e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
32460807856dSBarry Smith 
32470807856dSBarry Smith    Logically Collective on SNES
32480807856dSBarry Smith 
32490807856dSBarry Smith    Input Parameters:
32500807856dSBarry Smith +  snes - the SNES context
32510807856dSBarry Smith .  r - vector to store function value
32520807856dSBarry Smith -  func - function evaluation routine
32530807856dSBarry Smith 
32540807856dSBarry Smith    Calling sequence of func:
325561b2408cSBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
32560807856dSBarry Smith 
32570807856dSBarry Smith 
32580807856dSBarry Smith    Notes:
32590807856dSBarry Smith    The Newton-like methods typically solve linear systems of the form
32600807856dSBarry Smith $      f'(x) x = -f(x),
32610807856dSBarry Smith    where f'(x) denotes the Jacobian matrix and f(x) is the function.
32620807856dSBarry Smith 
32630807856dSBarry Smith    Level: beginner
32640807856dSBarry Smith 
32650807856dSBarry Smith .keywords: SNES, nonlinear, set, function
32660807856dSBarry Smith 
32670807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
326861b2408cSBarry Smith */
32697087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx)
32700807856dSBarry Smith {
32710807856dSBarry Smith   PetscErrorCode    ierr;
32728f6e6473SBarry Smith   SNESMatlabContext *sctx;
32730807856dSBarry Smith 
32740807856dSBarry Smith   PetscFunctionBegin;
32758f6e6473SBarry Smith   /* currently sctx is memory bleed */
32768f6e6473SBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
32778f6e6473SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
32788f6e6473SBarry Smith   /*
32798f6e6473SBarry Smith      This should work, but it doesn't
32808f6e6473SBarry Smith   sctx->ctx = ctx;
32818f6e6473SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
32828f6e6473SBarry Smith   */
32838f6e6473SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
32848f6e6473SBarry Smith   ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr);
32850807856dSBarry Smith   PetscFunctionReturn(0);
32860807856dSBarry Smith }
328769b4f73cSBarry Smith 
328861b2408cSBarry Smith #undef __FUNCT__
328961b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab"
329061b2408cSBarry Smith /*
329161b2408cSBarry Smith    SNESComputeJacobian_Matlab - Calls the function that has been set with
329261b2408cSBarry Smith                          SNESSetJacobianMatlab().
329361b2408cSBarry Smith 
329461b2408cSBarry Smith    Collective on SNES
329561b2408cSBarry Smith 
329661b2408cSBarry Smith    Input Parameters:
329761b2408cSBarry Smith +  snes - the SNES context
329861b2408cSBarry Smith .  x - input vector
329961b2408cSBarry Smith .  A, B - the matrices
330061b2408cSBarry Smith -  ctx - user context
330161b2408cSBarry Smith 
330261b2408cSBarry Smith    Output Parameter:
330361b2408cSBarry Smith .  flag - structure of the matrix
330461b2408cSBarry Smith 
330561b2408cSBarry Smith    Level: developer
330661b2408cSBarry Smith 
330761b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function
330861b2408cSBarry Smith 
330961b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
331061b2408cSBarry Smith @*/
33117087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx)
331261b2408cSBarry Smith {
331361b2408cSBarry Smith   PetscErrorCode    ierr;
331461b2408cSBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
331561b2408cSBarry Smith   int               nlhs = 2,nrhs = 6;
331661b2408cSBarry Smith   mxArray	    *plhs[2],*prhs[6];
331761b2408cSBarry Smith   long long int     lx = 0,lA = 0,ls = 0, lB = 0;
331861b2408cSBarry Smith 
331961b2408cSBarry Smith   PetscFunctionBegin;
332061b2408cSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
332161b2408cSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
332261b2408cSBarry Smith 
332361b2408cSBarry Smith   /* call Matlab function in ctx with arguments x and y */
332461b2408cSBarry Smith 
332561b2408cSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
332661b2408cSBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
332761b2408cSBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
332861b2408cSBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
332961b2408cSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
333061b2408cSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
333161b2408cSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
333261b2408cSBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
333361b2408cSBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
333461b2408cSBarry Smith   prhs[5] =  sctx->ctx;
3335b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr);
333661b2408cSBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
333761b2408cSBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
333861b2408cSBarry Smith   mxDestroyArray(prhs[0]);
333961b2408cSBarry Smith   mxDestroyArray(prhs[1]);
334061b2408cSBarry Smith   mxDestroyArray(prhs[2]);
334161b2408cSBarry Smith   mxDestroyArray(prhs[3]);
334261b2408cSBarry Smith   mxDestroyArray(prhs[4]);
334361b2408cSBarry Smith   mxDestroyArray(plhs[0]);
334461b2408cSBarry Smith   mxDestroyArray(plhs[1]);
334561b2408cSBarry Smith   PetscFunctionReturn(0);
334661b2408cSBarry Smith }
334761b2408cSBarry Smith 
334861b2408cSBarry Smith 
334961b2408cSBarry Smith #undef __FUNCT__
335061b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab"
335161b2408cSBarry Smith /*
335261b2408cSBarry Smith    SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
335361b2408cSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3354e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
335561b2408cSBarry Smith 
335661b2408cSBarry Smith    Logically Collective on SNES
335761b2408cSBarry Smith 
335861b2408cSBarry Smith    Input Parameters:
335961b2408cSBarry Smith +  snes - the SNES context
336061b2408cSBarry Smith .  A,B - Jacobian matrices
336161b2408cSBarry Smith .  func - function evaluation routine
336261b2408cSBarry Smith -  ctx - user context
336361b2408cSBarry Smith 
336461b2408cSBarry Smith    Calling sequence of func:
336561b2408cSBarry Smith $    flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx);
336661b2408cSBarry Smith 
336761b2408cSBarry Smith 
336861b2408cSBarry Smith    Level: developer
336961b2408cSBarry Smith 
337061b2408cSBarry Smith .keywords: SNES, nonlinear, set, function
337161b2408cSBarry Smith 
337261b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
337361b2408cSBarry Smith */
33747087cfbeSBarry Smith PetscErrorCode  SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx)
337561b2408cSBarry Smith {
337661b2408cSBarry Smith   PetscErrorCode    ierr;
337761b2408cSBarry Smith   SNESMatlabContext *sctx;
337861b2408cSBarry Smith 
337961b2408cSBarry Smith   PetscFunctionBegin;
338061b2408cSBarry Smith   /* currently sctx is memory bleed */
338161b2408cSBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
338261b2408cSBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
338361b2408cSBarry Smith   /*
338461b2408cSBarry Smith      This should work, but it doesn't
338561b2408cSBarry Smith   sctx->ctx = ctx;
338661b2408cSBarry Smith   mexMakeArrayPersistent(sctx->ctx);
338761b2408cSBarry Smith   */
338861b2408cSBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
338961b2408cSBarry Smith   ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
339061b2408cSBarry Smith   PetscFunctionReturn(0);
339161b2408cSBarry Smith }
339269b4f73cSBarry Smith 
3393f9eb7ae2SShri Abhyankar #undef __FUNCT__
3394f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab"
3395f9eb7ae2SShri Abhyankar /*
3396f9eb7ae2SShri Abhyankar    SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab().
3397f9eb7ae2SShri Abhyankar 
3398f9eb7ae2SShri Abhyankar    Collective on SNES
3399f9eb7ae2SShri Abhyankar 
3400f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction()
3401f9eb7ae2SShri Abhyankar @*/
34027087cfbeSBarry Smith PetscErrorCode  SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx)
3403f9eb7ae2SShri Abhyankar {
3404f9eb7ae2SShri Abhyankar   PetscErrorCode  ierr;
340548f37e70SShri Abhyankar   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
3406f9eb7ae2SShri Abhyankar   int             nlhs = 1,nrhs = 6;
3407f9eb7ae2SShri Abhyankar   mxArray	  *plhs[1],*prhs[6];
3408f9eb7ae2SShri Abhyankar   long long int   lx = 0,ls = 0;
3409f9eb7ae2SShri Abhyankar   Vec             x=snes->vec_sol;
3410f9eb7ae2SShri Abhyankar 
3411f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3412f9eb7ae2SShri Abhyankar   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3413f9eb7ae2SShri Abhyankar 
3414f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3415f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3416f9eb7ae2SShri Abhyankar   prhs[0] =  mxCreateDoubleScalar((double)ls);
3417f9eb7ae2SShri Abhyankar   prhs[1] =  mxCreateDoubleScalar((double)it);
3418f9eb7ae2SShri Abhyankar   prhs[2] =  mxCreateDoubleScalar((double)fnorm);
3419f9eb7ae2SShri Abhyankar   prhs[3] =  mxCreateDoubleScalar((double)lx);
3420f9eb7ae2SShri Abhyankar   prhs[4] =  mxCreateString(sctx->funcname);
3421f9eb7ae2SShri Abhyankar   prhs[5] =  sctx->ctx;
3422f9eb7ae2SShri Abhyankar   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr);
3423f9eb7ae2SShri Abhyankar   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3424f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[0]);
3425f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[1]);
3426f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[2]);
3427f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[3]);
3428f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[4]);
3429f9eb7ae2SShri Abhyankar   mxDestroyArray(plhs[0]);
3430f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3431f9eb7ae2SShri Abhyankar }
3432f9eb7ae2SShri Abhyankar 
3433f9eb7ae2SShri Abhyankar 
3434f9eb7ae2SShri Abhyankar #undef __FUNCT__
3435f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab"
3436f9eb7ae2SShri Abhyankar /*
3437e3c5b3baSBarry Smith    SNESMonitorSetMatlab - Sets the monitor function from MATLAB
3438f9eb7ae2SShri Abhyankar 
3439f9eb7ae2SShri Abhyankar    Level: developer
3440f9eb7ae2SShri Abhyankar 
3441f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function
3442f9eb7ae2SShri Abhyankar 
3443f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
3444f9eb7ae2SShri Abhyankar */
34457087cfbeSBarry Smith PetscErrorCode  SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx)
3446f9eb7ae2SShri Abhyankar {
3447f9eb7ae2SShri Abhyankar   PetscErrorCode    ierr;
3448f9eb7ae2SShri Abhyankar   SNESMatlabContext *sctx;
3449f9eb7ae2SShri Abhyankar 
3450f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3451f9eb7ae2SShri Abhyankar   /* currently sctx is memory bleed */
3452f9eb7ae2SShri Abhyankar   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
3453f9eb7ae2SShri Abhyankar   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3454f9eb7ae2SShri Abhyankar   /*
3455f9eb7ae2SShri Abhyankar      This should work, but it doesn't
3456f9eb7ae2SShri Abhyankar   sctx->ctx = ctx;
3457f9eb7ae2SShri Abhyankar   mexMakeArrayPersistent(sctx->ctx);
3458f9eb7ae2SShri Abhyankar   */
3459f9eb7ae2SShri Abhyankar   sctx->ctx = mxDuplicateArray(ctx);
3460f9eb7ae2SShri Abhyankar   ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3461f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3462f9eb7ae2SShri Abhyankar }
3463f9eb7ae2SShri Abhyankar 
346469b4f73cSBarry Smith #endif
3465