xref: /petsc/src/snes/interface/snes.c (revision efd5186314f4d22cd94ddfc39d3917eab26dd525)
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   }
2726bf464f9SBarry Smith   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;
337*efd51863SBarry Smith   PetscInt                i,indx,lag,mf_version,grids;
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     }
379*efd51863SBarry Smith     ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr);
380*efd51863SBarry Smith     if (flg) {
381*efd51863SBarry Smith       ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr);
382*efd51863SBarry Smith     }
383a8054027SBarry Smith 
38485385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
38585385478SLisandro Dalcin     if (flg) {
38685385478SLisandro Dalcin       switch (indx) {
3877f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
3887f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
38985385478SLisandro Dalcin       }
39085385478SLisandro Dalcin     }
39185385478SLisandro Dalcin 
392acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
393186905e3SBarry Smith 
39485385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
39585385478SLisandro Dalcin 
396acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
397186905e3SBarry Smith 
398fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
399fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
400fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
401fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
402fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
403fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
404fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
405186905e3SBarry Smith 
40690d69ab7SBarry Smith     flg  = PETSC_FALSE;
407acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
408a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
409eabae89aSBarry Smith 
410a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
411e8105e01SRichard Katz     if (flg) {
412050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
413c2efdce3SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
414e8105e01SRichard Katz     }
415eabae89aSBarry Smith 
416b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
417b271bb04SBarry Smith     if (flg) {
418b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
419b271bb04SBarry Smith     }
420b271bb04SBarry Smith 
421a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
422eabae89aSBarry Smith     if (flg) {
423050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
424f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
425e8105e01SRichard Katz     }
426eabae89aSBarry Smith 
427a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
428eabae89aSBarry Smith     if (flg) {
429050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
430c2efdce3SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void**))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
431eabae89aSBarry Smith     }
432eabae89aSBarry Smith 
43390d69ab7SBarry Smith     flg  = PETSC_FALSE;
434acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
435a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
43690d69ab7SBarry Smith     flg  = PETSC_FALSE;
437acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
438a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
43990d69ab7SBarry Smith     flg  = PETSC_FALSE;
440acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
441a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
44290d69ab7SBarry Smith     flg  = PETSC_FALSE;
443acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
444a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
44590d69ab7SBarry Smith     flg  = PETSC_FALSE;
446acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
447b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
448e24b481bSBarry Smith 
44990d69ab7SBarry Smith     flg  = PETSC_FALSE;
450acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
4514b27c08aSLois Curfman McInnes     if (flg) {
452186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
453ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
4549b94acceSBarry Smith     }
455639f9d9dSBarry Smith 
456aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
457aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
458acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
459aa3661deSLisandro Dalcin     if (flg && mf_operator) mf = PETSC_TRUE;
460aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
461acfcf0e5SJed Brown     ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
462aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
463aa3661deSLisandro Dalcin     mf_version = 1;
464aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
465aa3661deSLisandro Dalcin 
46676b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
46776b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
46876b2cf59SMatthew Knepley     }
46976b2cf59SMatthew Knepley 
470e7788613SBarry Smith     if (snes->ops->setfromoptions) {
471e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
472639f9d9dSBarry Smith     }
4735d973c19SBarry Smith 
4745d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
4755d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr);
476b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4774bbc92c1SBarry Smith 
478aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
4791cee3971SBarry Smith 
4801cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
481aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
482aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
48385385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
48493993e2dSLois Curfman McInnes 
4853a40ed3dSBarry Smith   PetscFunctionReturn(0);
4869b94acceSBarry Smith }
4879b94acceSBarry Smith 
488a847f771SSatish Balay 
4894a2ae208SSatish Balay #undef __FUNCT__
4904a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
4919b94acceSBarry Smith /*@
4929b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
4939b94acceSBarry Smith    the nonlinear solvers.
4949b94acceSBarry Smith 
4953f9fe445SBarry Smith    Logically Collective on SNES
496fee21e36SBarry Smith 
497c7afd0dbSLois Curfman McInnes    Input Parameters:
498c7afd0dbSLois Curfman McInnes +  snes - the SNES context
499c7afd0dbSLois Curfman McInnes -  usrP - optional user context
500c7afd0dbSLois Curfman McInnes 
50136851e7fSLois Curfman McInnes    Level: intermediate
50236851e7fSLois Curfman McInnes 
5039b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
5049b94acceSBarry Smith 
5059b94acceSBarry Smith .seealso: SNESGetApplicationContext()
5069b94acceSBarry Smith @*/
5077087cfbeSBarry Smith PetscErrorCode  SNESSetApplicationContext(SNES snes,void *usrP)
5089b94acceSBarry Smith {
5093a40ed3dSBarry Smith   PetscFunctionBegin;
5100700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5119b94acceSBarry Smith   snes->user		= usrP;
5123a40ed3dSBarry Smith   PetscFunctionReturn(0);
5139b94acceSBarry Smith }
51474679c65SBarry Smith 
5154a2ae208SSatish Balay #undef __FUNCT__
5164a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
5179b94acceSBarry Smith /*@C
5189b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
5199b94acceSBarry Smith    nonlinear solvers.
5209b94acceSBarry Smith 
521c7afd0dbSLois Curfman McInnes    Not Collective
522c7afd0dbSLois Curfman McInnes 
5239b94acceSBarry Smith    Input Parameter:
5249b94acceSBarry Smith .  snes - SNES context
5259b94acceSBarry Smith 
5269b94acceSBarry Smith    Output Parameter:
5279b94acceSBarry Smith .  usrP - user context
5289b94acceSBarry Smith 
52936851e7fSLois Curfman McInnes    Level: intermediate
53036851e7fSLois Curfman McInnes 
5319b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
5329b94acceSBarry Smith 
5339b94acceSBarry Smith .seealso: SNESSetApplicationContext()
5349b94acceSBarry Smith @*/
5357087cfbeSBarry Smith PetscErrorCode  SNESGetApplicationContext(SNES snes,void **usrP)
5369b94acceSBarry Smith {
5373a40ed3dSBarry Smith   PetscFunctionBegin;
5380700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5399b94acceSBarry Smith   *usrP = snes->user;
5403a40ed3dSBarry Smith   PetscFunctionReturn(0);
5419b94acceSBarry Smith }
54274679c65SBarry Smith 
5434a2ae208SSatish Balay #undef __FUNCT__
5444a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
5459b94acceSBarry Smith /*@
546c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
547c8228a4eSBarry Smith    at this time.
5489b94acceSBarry Smith 
549c7afd0dbSLois Curfman McInnes    Not Collective
550c7afd0dbSLois Curfman McInnes 
5519b94acceSBarry Smith    Input Parameter:
5529b94acceSBarry Smith .  snes - SNES context
5539b94acceSBarry Smith 
5549b94acceSBarry Smith    Output Parameter:
5559b94acceSBarry Smith .  iter - iteration number
5569b94acceSBarry Smith 
557c8228a4eSBarry Smith    Notes:
558c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
559c8228a4eSBarry Smith 
560c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
56108405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
56208405cd6SLois Curfman McInnes .vb
56308405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
56408405cd6SLois Curfman McInnes       if (!(it % 2)) {
56508405cd6SLois Curfman McInnes         [compute Jacobian here]
56608405cd6SLois Curfman McInnes       }
56708405cd6SLois Curfman McInnes .ve
568c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
56908405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
570c8228a4eSBarry Smith 
57136851e7fSLois Curfman McInnes    Level: intermediate
57236851e7fSLois Curfman McInnes 
5732b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
5742b668275SBarry Smith 
575b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
5769b94acceSBarry Smith @*/
5777087cfbeSBarry Smith PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt* iter)
5789b94acceSBarry Smith {
5793a40ed3dSBarry Smith   PetscFunctionBegin;
5800700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5814482741eSBarry Smith   PetscValidIntPointer(iter,2);
5829b94acceSBarry Smith   *iter = snes->iter;
5833a40ed3dSBarry Smith   PetscFunctionReturn(0);
5849b94acceSBarry Smith }
58574679c65SBarry Smith 
5864a2ae208SSatish Balay #undef __FUNCT__
5874a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
5889b94acceSBarry Smith /*@
5899b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
5909b94acceSBarry Smith    with SNESSSetFunction().
5919b94acceSBarry Smith 
592c7afd0dbSLois Curfman McInnes    Collective on SNES
593c7afd0dbSLois Curfman McInnes 
5949b94acceSBarry Smith    Input Parameter:
5959b94acceSBarry Smith .  snes - SNES context
5969b94acceSBarry Smith 
5979b94acceSBarry Smith    Output Parameter:
5989b94acceSBarry Smith .  fnorm - 2-norm of function
5999b94acceSBarry Smith 
60036851e7fSLois Curfman McInnes    Level: intermediate
60136851e7fSLois Curfman McInnes 
6029b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
603a86d99e1SLois Curfman McInnes 
604b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
6059b94acceSBarry Smith @*/
6067087cfbeSBarry Smith PetscErrorCode  SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
6079b94acceSBarry Smith {
6083a40ed3dSBarry Smith   PetscFunctionBegin;
6090700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6104482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
6119b94acceSBarry Smith   *fnorm = snes->norm;
6123a40ed3dSBarry Smith   PetscFunctionReturn(0);
6139b94acceSBarry Smith }
61474679c65SBarry Smith 
6154a2ae208SSatish Balay #undef __FUNCT__
616b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
6179b94acceSBarry Smith /*@
618b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
6199b94acceSBarry Smith    attempted by the nonlinear solver.
6209b94acceSBarry Smith 
621c7afd0dbSLois Curfman McInnes    Not Collective
622c7afd0dbSLois Curfman McInnes 
6239b94acceSBarry Smith    Input Parameter:
6249b94acceSBarry Smith .  snes - SNES context
6259b94acceSBarry Smith 
6269b94acceSBarry Smith    Output Parameter:
6279b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
6289b94acceSBarry Smith 
629c96a6f78SLois Curfman McInnes    Notes:
630c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
631c96a6f78SLois Curfman McInnes 
63236851e7fSLois Curfman McInnes    Level: intermediate
63336851e7fSLois Curfman McInnes 
6349b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
63558ebbce7SBarry Smith 
636e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
63758ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
6389b94acceSBarry Smith @*/
6397087cfbeSBarry Smith PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
6409b94acceSBarry Smith {
6413a40ed3dSBarry Smith   PetscFunctionBegin;
6420700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6434482741eSBarry Smith   PetscValidIntPointer(nfails,2);
64450ffb88aSMatthew Knepley   *nfails = snes->numFailures;
64550ffb88aSMatthew Knepley   PetscFunctionReturn(0);
64650ffb88aSMatthew Knepley }
64750ffb88aSMatthew Knepley 
64850ffb88aSMatthew Knepley #undef __FUNCT__
649b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
65050ffb88aSMatthew Knepley /*@
651b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
65250ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
65350ffb88aSMatthew Knepley 
65450ffb88aSMatthew Knepley    Not Collective
65550ffb88aSMatthew Knepley 
65650ffb88aSMatthew Knepley    Input Parameters:
65750ffb88aSMatthew Knepley +  snes     - SNES context
65850ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
65950ffb88aSMatthew Knepley 
66050ffb88aSMatthew Knepley    Level: intermediate
66150ffb88aSMatthew Knepley 
66250ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
66358ebbce7SBarry Smith 
664e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
66558ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
66650ffb88aSMatthew Knepley @*/
6677087cfbeSBarry Smith PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
66850ffb88aSMatthew Knepley {
66950ffb88aSMatthew Knepley   PetscFunctionBegin;
6700700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
67150ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
67250ffb88aSMatthew Knepley   PetscFunctionReturn(0);
67350ffb88aSMatthew Knepley }
67450ffb88aSMatthew Knepley 
67550ffb88aSMatthew Knepley #undef __FUNCT__
676b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
67750ffb88aSMatthew Knepley /*@
678b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
67950ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
68050ffb88aSMatthew Knepley 
68150ffb88aSMatthew Knepley    Not Collective
68250ffb88aSMatthew Knepley 
68350ffb88aSMatthew Knepley    Input Parameter:
68450ffb88aSMatthew Knepley .  snes     - SNES context
68550ffb88aSMatthew Knepley 
68650ffb88aSMatthew Knepley    Output Parameter:
68750ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
68850ffb88aSMatthew Knepley 
68950ffb88aSMatthew Knepley    Level: intermediate
69050ffb88aSMatthew Knepley 
69150ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
69258ebbce7SBarry Smith 
693e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
69458ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
69558ebbce7SBarry Smith 
69650ffb88aSMatthew Knepley @*/
6977087cfbeSBarry Smith PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
69850ffb88aSMatthew Knepley {
69950ffb88aSMatthew Knepley   PetscFunctionBegin;
7000700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7014482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
70250ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
7033a40ed3dSBarry Smith   PetscFunctionReturn(0);
7049b94acceSBarry Smith }
705a847f771SSatish Balay 
7064a2ae208SSatish Balay #undef __FUNCT__
7072541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
7082541af92SBarry Smith /*@
7092541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
7102541af92SBarry Smith      done by SNES.
7112541af92SBarry Smith 
7122541af92SBarry Smith    Not Collective
7132541af92SBarry Smith 
7142541af92SBarry Smith    Input Parameter:
7152541af92SBarry Smith .  snes     - SNES context
7162541af92SBarry Smith 
7172541af92SBarry Smith    Output Parameter:
7182541af92SBarry Smith .  nfuncs - number of evaluations
7192541af92SBarry Smith 
7202541af92SBarry Smith    Level: intermediate
7212541af92SBarry Smith 
7222541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
72358ebbce7SBarry Smith 
724e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
7252541af92SBarry Smith @*/
7267087cfbeSBarry Smith PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
7272541af92SBarry Smith {
7282541af92SBarry Smith   PetscFunctionBegin;
7290700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7302541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
7312541af92SBarry Smith   *nfuncs = snes->nfuncs;
7322541af92SBarry Smith   PetscFunctionReturn(0);
7332541af92SBarry Smith }
7342541af92SBarry Smith 
7352541af92SBarry Smith #undef __FUNCT__
7363d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
7373d4c4710SBarry Smith /*@
7383d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
7393d4c4710SBarry Smith    linear solvers.
7403d4c4710SBarry Smith 
7413d4c4710SBarry Smith    Not Collective
7423d4c4710SBarry Smith 
7433d4c4710SBarry Smith    Input Parameter:
7443d4c4710SBarry Smith .  snes - SNES context
7453d4c4710SBarry Smith 
7463d4c4710SBarry Smith    Output Parameter:
7473d4c4710SBarry Smith .  nfails - number of failed solves
7483d4c4710SBarry Smith 
7493d4c4710SBarry Smith    Notes:
7503d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
7513d4c4710SBarry Smith 
7523d4c4710SBarry Smith    Level: intermediate
7533d4c4710SBarry Smith 
7543d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
75558ebbce7SBarry Smith 
756e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
7573d4c4710SBarry Smith @*/
7587087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
7593d4c4710SBarry Smith {
7603d4c4710SBarry Smith   PetscFunctionBegin;
7610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7623d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
7633d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
7643d4c4710SBarry Smith   PetscFunctionReturn(0);
7653d4c4710SBarry Smith }
7663d4c4710SBarry Smith 
7673d4c4710SBarry Smith #undef __FUNCT__
7683d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
7693d4c4710SBarry Smith /*@
7703d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
7713d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
7723d4c4710SBarry Smith 
7733f9fe445SBarry Smith    Logically Collective on SNES
7743d4c4710SBarry Smith 
7753d4c4710SBarry Smith    Input Parameters:
7763d4c4710SBarry Smith +  snes     - SNES context
7773d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
7783d4c4710SBarry Smith 
7793d4c4710SBarry Smith    Level: intermediate
7803d4c4710SBarry Smith 
781a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
7823d4c4710SBarry Smith 
7833d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
7843d4c4710SBarry Smith 
78558ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
7863d4c4710SBarry Smith @*/
7877087cfbeSBarry Smith PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
7883d4c4710SBarry Smith {
7893d4c4710SBarry Smith   PetscFunctionBegin;
7900700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
791c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
7923d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
7933d4c4710SBarry Smith   PetscFunctionReturn(0);
7943d4c4710SBarry Smith }
7953d4c4710SBarry Smith 
7963d4c4710SBarry Smith #undef __FUNCT__
7973d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
7983d4c4710SBarry Smith /*@
7993d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
8003d4c4710SBarry Smith      are allowed before SNES terminates
8013d4c4710SBarry Smith 
8023d4c4710SBarry Smith    Not Collective
8033d4c4710SBarry Smith 
8043d4c4710SBarry Smith    Input Parameter:
8053d4c4710SBarry Smith .  snes     - SNES context
8063d4c4710SBarry Smith 
8073d4c4710SBarry Smith    Output Parameter:
8083d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
8093d4c4710SBarry Smith 
8103d4c4710SBarry Smith    Level: intermediate
8113d4c4710SBarry Smith 
8123d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
8133d4c4710SBarry Smith 
8143d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
8153d4c4710SBarry Smith 
816e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
8173d4c4710SBarry Smith @*/
8187087cfbeSBarry Smith PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
8193d4c4710SBarry Smith {
8203d4c4710SBarry Smith   PetscFunctionBegin;
8210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8223d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
8233d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
8243d4c4710SBarry Smith   PetscFunctionReturn(0);
8253d4c4710SBarry Smith }
8263d4c4710SBarry Smith 
8273d4c4710SBarry Smith #undef __FUNCT__
828b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
829c96a6f78SLois Curfman McInnes /*@
830b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
831c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
832c96a6f78SLois Curfman McInnes 
833c7afd0dbSLois Curfman McInnes    Not Collective
834c7afd0dbSLois Curfman McInnes 
835c96a6f78SLois Curfman McInnes    Input Parameter:
836c96a6f78SLois Curfman McInnes .  snes - SNES context
837c96a6f78SLois Curfman McInnes 
838c96a6f78SLois Curfman McInnes    Output Parameter:
839c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
840c96a6f78SLois Curfman McInnes 
841c96a6f78SLois Curfman McInnes    Notes:
842c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
843c96a6f78SLois Curfman McInnes 
84436851e7fSLois Curfman McInnes    Level: intermediate
84536851e7fSLois Curfman McInnes 
846c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
8472b668275SBarry Smith 
8488c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
849c96a6f78SLois Curfman McInnes @*/
8507087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
851c96a6f78SLois Curfman McInnes {
8523a40ed3dSBarry Smith   PetscFunctionBegin;
8530700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8544482741eSBarry Smith   PetscValidIntPointer(lits,2);
855c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
8563a40ed3dSBarry Smith   PetscFunctionReturn(0);
857c96a6f78SLois Curfman McInnes }
858c96a6f78SLois Curfman McInnes 
8594a2ae208SSatish Balay #undef __FUNCT__
86094b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
86152baeb72SSatish Balay /*@
86294b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
8639b94acceSBarry Smith 
86494b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
865c7afd0dbSLois Curfman McInnes 
8669b94acceSBarry Smith    Input Parameter:
8679b94acceSBarry Smith .  snes - the SNES context
8689b94acceSBarry Smith 
8699b94acceSBarry Smith    Output Parameter:
87094b7f48cSBarry Smith .  ksp - the KSP context
8719b94acceSBarry Smith 
8729b94acceSBarry Smith    Notes:
87394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
8749b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
8752999313aSBarry Smith    PC contexts as well.
8769b94acceSBarry Smith 
87736851e7fSLois Curfman McInnes    Level: beginner
87836851e7fSLois Curfman McInnes 
87994b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
8809b94acceSBarry Smith 
8812999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
8829b94acceSBarry Smith @*/
8837087cfbeSBarry Smith PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
8849b94acceSBarry Smith {
8851cee3971SBarry Smith   PetscErrorCode ierr;
8861cee3971SBarry Smith 
8873a40ed3dSBarry Smith   PetscFunctionBegin;
8880700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8894482741eSBarry Smith   PetscValidPointer(ksp,2);
8901cee3971SBarry Smith 
8911cee3971SBarry Smith   if (!snes->ksp) {
8921cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
8931cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
8941cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
8951cee3971SBarry Smith   }
89694b7f48cSBarry Smith   *ksp = snes->ksp;
8973a40ed3dSBarry Smith   PetscFunctionReturn(0);
8989b94acceSBarry Smith }
89982bf6240SBarry Smith 
9004a2ae208SSatish Balay #undef __FUNCT__
9012999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
9022999313aSBarry Smith /*@
9032999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
9042999313aSBarry Smith 
9052999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
9062999313aSBarry Smith 
9072999313aSBarry Smith    Input Parameters:
9082999313aSBarry Smith +  snes - the SNES context
9092999313aSBarry Smith -  ksp - the KSP context
9102999313aSBarry Smith 
9112999313aSBarry Smith    Notes:
9122999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
9132999313aSBarry Smith    so this routine is rarely needed.
9142999313aSBarry Smith 
9152999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
9162999313aSBarry Smith    decreased by one.
9172999313aSBarry Smith 
9182999313aSBarry Smith    Level: developer
9192999313aSBarry Smith 
9202999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9212999313aSBarry Smith 
9222999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9232999313aSBarry Smith @*/
9247087cfbeSBarry Smith PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
9252999313aSBarry Smith {
9262999313aSBarry Smith   PetscErrorCode ierr;
9272999313aSBarry Smith 
9282999313aSBarry Smith   PetscFunctionBegin;
9290700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9300700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
9312999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
9327dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
933906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
9342999313aSBarry Smith   snes->ksp = ksp;
9352999313aSBarry Smith   PetscFunctionReturn(0);
9362999313aSBarry Smith }
9372999313aSBarry Smith 
9387adad957SLisandro Dalcin #if 0
9392999313aSBarry Smith #undef __FUNCT__
9404a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
9416849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
942e24b481bSBarry Smith {
943e24b481bSBarry Smith   PetscFunctionBegin;
944e24b481bSBarry Smith   PetscFunctionReturn(0);
945e24b481bSBarry Smith }
9467adad957SLisandro Dalcin #endif
947e24b481bSBarry Smith 
9489b94acceSBarry Smith /* -----------------------------------------------------------*/
9494a2ae208SSatish Balay #undef __FUNCT__
9504a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
95152baeb72SSatish Balay /*@
9529b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
9539b94acceSBarry Smith 
954c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
955c7afd0dbSLois Curfman McInnes 
956c7afd0dbSLois Curfman McInnes    Input Parameters:
957906ed7ccSBarry Smith .  comm - MPI communicator
9589b94acceSBarry Smith 
9599b94acceSBarry Smith    Output Parameter:
9609b94acceSBarry Smith .  outsnes - the new SNES context
9619b94acceSBarry Smith 
962c7afd0dbSLois Curfman McInnes    Options Database Keys:
963c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
964c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
965c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
966c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
967c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
968c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
969c1f60f51SBarry Smith 
97036851e7fSLois Curfman McInnes    Level: beginner
97136851e7fSLois Curfman McInnes 
9729b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
9739b94acceSBarry Smith 
974a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
975a8054027SBarry Smith 
9769b94acceSBarry Smith @*/
9777087cfbeSBarry Smith PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
9789b94acceSBarry Smith {
979dfbe8321SBarry Smith   PetscErrorCode      ierr;
9809b94acceSBarry Smith   SNES                snes;
981fa9f3622SBarry Smith   SNESKSPEW           *kctx;
98237fcc0dbSBarry Smith 
9833a40ed3dSBarry Smith   PetscFunctionBegin;
984ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
9858ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
9868ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
9878ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
9888ba1e511SMatthew Knepley #endif
9898ba1e511SMatthew Knepley 
9900700a824SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
9917adad957SLisandro Dalcin 
99285385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
9939b94acceSBarry Smith   snes->max_its           = 50;
9949750a799SBarry Smith   snes->max_funcs	  = 10000;
9959b94acceSBarry Smith   snes->norm		  = 0.0;
996b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
997b4874afaSBarry Smith   snes->ttol              = 0.0;
99870441072SBarry Smith   snes->abstol		  = 1.e-50;
9999b94acceSBarry Smith   snes->xtol		  = 1.e-8;
10004b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
10019b94acceSBarry Smith   snes->nfuncs            = 0;
100250ffb88aSMatthew Knepley   snes->numFailures       = 0;
100350ffb88aSMatthew Knepley   snes->maxFailures       = 1;
10047a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1005e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1006a8054027SBarry Smith   snes->lagpreconditioner = 1;
1007639f9d9dSBarry Smith   snes->numbermonitors    = 0;
10089b94acceSBarry Smith   snes->data              = 0;
10094dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1010186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
10116f24a144SLois Curfman McInnes   snes->nwork             = 0;
101258c9b817SLisandro Dalcin   snes->work              = 0;
101358c9b817SLisandro Dalcin   snes->nvwork            = 0;
101458c9b817SLisandro Dalcin   snes->vwork             = 0;
1015758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1016758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1017758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1018758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1019758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1020184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
10219b94acceSBarry Smith 
10223d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
10233d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
10243d4c4710SBarry Smith 
10259b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
102638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
10279b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
10289b94acceSBarry Smith   kctx->version     = 2;
10299b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
10309b94acceSBarry Smith                              this was too large for some test cases */
103175567043SBarry Smith   kctx->rtol_last   = 0.0;
10329b94acceSBarry Smith   kctx->rtol_max    = .9;
10339b94acceSBarry Smith   kctx->gamma       = 1.0;
103471f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
103571f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
10369b94acceSBarry Smith   kctx->threshold   = .1;
103775567043SBarry Smith   kctx->lresid_last = 0.0;
103875567043SBarry Smith   kctx->norm_last   = 0.0;
10399b94acceSBarry Smith 
10409b94acceSBarry Smith   *outsnes = snes;
10413a40ed3dSBarry Smith   PetscFunctionReturn(0);
10429b94acceSBarry Smith }
10439b94acceSBarry Smith 
10444a2ae208SSatish Balay #undef __FUNCT__
10454a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
10469b94acceSBarry Smith /*@C
10479b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
10489b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
10499b94acceSBarry Smith    equations.
10509b94acceSBarry Smith 
10513f9fe445SBarry Smith    Logically Collective on SNES
1052fee21e36SBarry Smith 
1053c7afd0dbSLois Curfman McInnes    Input Parameters:
1054c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1055c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1056de044059SHong Zhang .  func - function evaluation routine
1057c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1058c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
10599b94acceSBarry Smith 
1060c7afd0dbSLois Curfman McInnes    Calling sequence of func:
10618d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1062c7afd0dbSLois Curfman McInnes 
1063313e4042SLois Curfman McInnes .  f - function vector
1064c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
10659b94acceSBarry Smith 
10669b94acceSBarry Smith    Notes:
10679b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
10689b94acceSBarry Smith $      f'(x) x = -f(x),
1069c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
10709b94acceSBarry Smith 
107136851e7fSLois Curfman McInnes    Level: beginner
107236851e7fSLois Curfman McInnes 
10739b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
10749b94acceSBarry Smith 
1075a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
10769b94acceSBarry Smith @*/
10777087cfbeSBarry Smith PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
10789b94acceSBarry Smith {
107985385478SLisandro Dalcin   PetscErrorCode ierr;
10803a40ed3dSBarry Smith   PetscFunctionBegin;
10810700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1082d2a683ecSLisandro Dalcin   if (r) {
1083d2a683ecSLisandro Dalcin     PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1084d2a683ecSLisandro Dalcin     PetscCheckSameComm(snes,1,r,2);
108585385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
10866bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
108785385478SLisandro Dalcin     snes->vec_func = r;
1088d2a683ecSLisandro Dalcin   } else if (!snes->vec_func && snes->dm) {
1089d2a683ecSLisandro Dalcin     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1090d2a683ecSLisandro Dalcin   }
1091d2a683ecSLisandro Dalcin   if (func) snes->ops->computefunction = func;
1092d2a683ecSLisandro Dalcin   if (ctx)  snes->funP                 = ctx;
10933a40ed3dSBarry Smith   PetscFunctionReturn(0);
10949b94acceSBarry Smith }
10959b94acceSBarry Smith 
10963ab0aad5SBarry Smith /* --------------------------------------------------------------- */
10973ab0aad5SBarry Smith #undef __FUNCT__
10981096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
10991096aae1SMatthew Knepley /*@C
11001096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
11011096aae1SMatthew Knepley    it assumes a zero right hand side.
11021096aae1SMatthew Knepley 
11033f9fe445SBarry Smith    Logically Collective on SNES
11041096aae1SMatthew Knepley 
11051096aae1SMatthew Knepley    Input Parameter:
11061096aae1SMatthew Knepley .  snes - the SNES context
11071096aae1SMatthew Knepley 
11081096aae1SMatthew Knepley    Output Parameter:
1109bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
11101096aae1SMatthew Knepley 
11111096aae1SMatthew Knepley    Level: intermediate
11121096aae1SMatthew Knepley 
11131096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
11141096aae1SMatthew Knepley 
111585385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
11161096aae1SMatthew Knepley @*/
11177087cfbeSBarry Smith PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
11181096aae1SMatthew Knepley {
11191096aae1SMatthew Knepley   PetscFunctionBegin;
11200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11211096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
112285385478SLisandro Dalcin   *rhs = snes->vec_rhs;
11231096aae1SMatthew Knepley   PetscFunctionReturn(0);
11241096aae1SMatthew Knepley }
11251096aae1SMatthew Knepley 
11261096aae1SMatthew Knepley #undef __FUNCT__
11274a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
11289b94acceSBarry Smith /*@
112936851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
11309b94acceSBarry Smith                          SNESSetFunction().
11319b94acceSBarry Smith 
1132c7afd0dbSLois Curfman McInnes    Collective on SNES
1133c7afd0dbSLois Curfman McInnes 
11349b94acceSBarry Smith    Input Parameters:
1135c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1136c7afd0dbSLois Curfman McInnes -  x - input vector
11379b94acceSBarry Smith 
11389b94acceSBarry Smith    Output Parameter:
11393638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
11409b94acceSBarry Smith 
11411bffabb2SLois Curfman McInnes    Notes:
114236851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
114336851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
114436851e7fSLois Curfman McInnes    themselves.
114536851e7fSLois Curfman McInnes 
114636851e7fSLois Curfman McInnes    Level: developer
114736851e7fSLois Curfman McInnes 
11489b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
11499b94acceSBarry Smith 
1150a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
11519b94acceSBarry Smith @*/
11527087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
11539b94acceSBarry Smith {
1154dfbe8321SBarry Smith   PetscErrorCode ierr;
11559b94acceSBarry Smith 
11563a40ed3dSBarry Smith   PetscFunctionBegin;
11570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11580700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
11590700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1160c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1161c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1162184914b5SBarry Smith 
1163d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1164e7788613SBarry Smith   if (snes->ops->computefunction) {
1165d64ed03dSBarry Smith     PetscStackPush("SNES user function");
116639d508bbSBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1167d64ed03dSBarry Smith     PetscStackPop;
116885385478SLisandro Dalcin   } else if (snes->vec_rhs) {
11691096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
117017186662SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
117185385478SLisandro Dalcin   if (snes->vec_rhs) {
117285385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
11733ab0aad5SBarry Smith   }
1174ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1175d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
11763a40ed3dSBarry Smith   PetscFunctionReturn(0);
11779b94acceSBarry Smith }
11789b94acceSBarry Smith 
11794a2ae208SSatish Balay #undef __FUNCT__
11804a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
118162fef451SLois Curfman McInnes /*@
118262fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
118362fef451SLois Curfman McInnes    set with SNESSetJacobian().
118462fef451SLois Curfman McInnes 
1185c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1186c7afd0dbSLois Curfman McInnes 
118762fef451SLois Curfman McInnes    Input Parameters:
1188c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1189c7afd0dbSLois Curfman McInnes -  x - input vector
119062fef451SLois Curfman McInnes 
119162fef451SLois Curfman McInnes    Output Parameters:
1192c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
119362fef451SLois Curfman McInnes .  B - optional preconditioning matrix
11942b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1195fee21e36SBarry Smith 
1196e35cf81dSBarry Smith   Options Database Keys:
1197e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1198e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1199e35cf81dSBarry Smith 
120062fef451SLois Curfman McInnes    Notes:
120162fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
120262fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
120362fef451SLois Curfman McInnes 
120494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1205dc5a77f8SLois Curfman McInnes    flag parameter.
120662fef451SLois Curfman McInnes 
120736851e7fSLois Curfman McInnes    Level: developer
120836851e7fSLois Curfman McInnes 
120962fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
121062fef451SLois Curfman McInnes 
1211e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
121262fef451SLois Curfman McInnes @*/
12137087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
12149b94acceSBarry Smith {
1215dfbe8321SBarry Smith   PetscErrorCode ierr;
1216ace3abfcSBarry Smith   PetscBool      flag;
12173a40ed3dSBarry Smith 
12183a40ed3dSBarry Smith   PetscFunctionBegin;
12190700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12200700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
12214482741eSBarry Smith   PetscValidPointer(flg,5);
1222c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1223e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1224ebd3b9afSBarry Smith 
1225ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1226ebd3b9afSBarry Smith 
1227fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1228fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1229fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1230fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1231e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1232e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1233ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1234ebd3b9afSBarry Smith     if (flag) {
1235ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1236ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1237ebd3b9afSBarry Smith     }
1238e35cf81dSBarry Smith     PetscFunctionReturn(0);
1239e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1240e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1241e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1242ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1243ebd3b9afSBarry Smith     if (flag) {
1244ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1245ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1246ebd3b9afSBarry Smith     }
1247e35cf81dSBarry Smith     PetscFunctionReturn(0);
1248e35cf81dSBarry Smith   }
1249e35cf81dSBarry Smith 
1250c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1251e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1252d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1253e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1254d64ed03dSBarry Smith   PetscStackPop;
1255d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1256a8054027SBarry Smith 
12573b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
12583b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
12593b4f5425SBarry Smith     snes->lagpreconditioner = -1;
12603b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1261a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1262a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1263a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1264a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1265a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1266a8054027SBarry Smith   }
1267a8054027SBarry Smith 
12686d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
12690700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
12700700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
12713a40ed3dSBarry Smith   PetscFunctionReturn(0);
12729b94acceSBarry Smith }
12739b94acceSBarry Smith 
12744a2ae208SSatish Balay #undef __FUNCT__
12754a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
12769b94acceSBarry Smith /*@C
12779b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1278044dda88SLois Curfman McInnes    location to store the matrix.
12799b94acceSBarry Smith 
12803f9fe445SBarry Smith    Logically Collective on SNES and Mat
1281c7afd0dbSLois Curfman McInnes 
12829b94acceSBarry Smith    Input Parameters:
1283c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12849b94acceSBarry Smith .  A - Jacobian matrix
12859b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
1286*efd51863SBarry Smith .  func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value)
1287c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1288*efd51863SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value)
12899b94acceSBarry Smith 
12909b94acceSBarry Smith    Calling sequence of func:
12918d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12929b94acceSBarry Smith 
1293c7afd0dbSLois Curfman McInnes +  x - input vector
12949b94acceSBarry Smith .  A - Jacobian matrix
12959b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1296ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
12972b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1298c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
12999b94acceSBarry Smith 
13009b94acceSBarry Smith    Notes:
130194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
13022cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1303ac21db08SLois Curfman McInnes 
1304ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
13059b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
13069b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
13079b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
13089b94acceSBarry Smith    throughout the global iterations.
13099b94acceSBarry Smith 
131016913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
131116913363SBarry Smith    each matrix.
131216913363SBarry Smith 
1313a8a26c1eSJed Brown    If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument
1314a8a26c1eSJed Brown    must be a MatFDColoring.
1315a8a26c1eSJed Brown 
131636851e7fSLois Curfman McInnes    Level: beginner
131736851e7fSLois Curfman McInnes 
13189b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
13199b94acceSBarry Smith 
13203ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
13219b94acceSBarry Smith @*/
13227087cfbeSBarry Smith PetscErrorCode  SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
13239b94acceSBarry Smith {
1324dfbe8321SBarry Smith   PetscErrorCode ierr;
13253a7fca6bSBarry Smith 
13263a40ed3dSBarry Smith   PetscFunctionBegin;
13270700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13280700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
13290700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1330c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
133106975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1332e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
13333a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
13343a7fca6bSBarry Smith   if (A) {
13357dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
13366bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
13379b94acceSBarry Smith     snes->jacobian = A;
13383a7fca6bSBarry Smith   }
13393a7fca6bSBarry Smith   if (B) {
13407dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
13416bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
13429b94acceSBarry Smith     snes->jacobian_pre = B;
13433a7fca6bSBarry Smith   }
13443a40ed3dSBarry Smith   PetscFunctionReturn(0);
13459b94acceSBarry Smith }
134662fef451SLois Curfman McInnes 
13474a2ae208SSatish Balay #undef __FUNCT__
13484a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1349c2aafc4cSSatish Balay /*@C
1350b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1351b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1352b4fd4287SBarry Smith 
1353c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1354c7afd0dbSLois Curfman McInnes 
1355b4fd4287SBarry Smith    Input Parameter:
1356b4fd4287SBarry Smith .  snes - the nonlinear solver context
1357b4fd4287SBarry Smith 
1358b4fd4287SBarry Smith    Output Parameters:
1359c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1360b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
136170e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
136270e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1363fee21e36SBarry Smith 
136436851e7fSLois Curfman McInnes    Level: advanced
136536851e7fSLois Curfman McInnes 
1366b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1367b4fd4287SBarry Smith @*/
13687087cfbeSBarry Smith PetscErrorCode  SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1369b4fd4287SBarry Smith {
13703a40ed3dSBarry Smith   PetscFunctionBegin;
13710700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1372b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1373b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1374e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
137570e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
13763a40ed3dSBarry Smith   PetscFunctionReturn(0);
1377b4fd4287SBarry Smith }
1378b4fd4287SBarry Smith 
13799b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13809b94acceSBarry Smith 
13814a2ae208SSatish Balay #undef __FUNCT__
13824a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
13839b94acceSBarry Smith /*@
13849b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1385272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13869b94acceSBarry Smith 
1387fee21e36SBarry Smith    Collective on SNES
1388fee21e36SBarry Smith 
1389c7afd0dbSLois Curfman McInnes    Input Parameters:
139070e92668SMatthew Knepley .  snes - the SNES context
1391c7afd0dbSLois Curfman McInnes 
1392272ac6f2SLois Curfman McInnes    Notes:
1393272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1394272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1395272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1396272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1397272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1398272ac6f2SLois Curfman McInnes 
139936851e7fSLois Curfman McInnes    Level: advanced
140036851e7fSLois Curfman McInnes 
14019b94acceSBarry Smith .keywords: SNES, nonlinear, setup
14029b94acceSBarry Smith 
14039b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
14049b94acceSBarry Smith @*/
14057087cfbeSBarry Smith PetscErrorCode  SNESSetUp(SNES snes)
14069b94acceSBarry Smith {
1407dfbe8321SBarry Smith   PetscErrorCode ierr;
14083a40ed3dSBarry Smith 
14093a40ed3dSBarry Smith   PetscFunctionBegin;
14100700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14114dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
14129b94acceSBarry Smith 
14137adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
141485385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
141585385478SLisandro Dalcin   }
141685385478SLisandro Dalcin 
1417*efd51863SBarry Smith   if (!snes->vec_func && snes->dm && !snes->vec_rhs) {
1418*efd51863SBarry Smith     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1419*efd51863SBarry Smith   }
142017186662SBarry Smith   if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
142117186662SBarry Smith   if (!snes->ops->computefunction && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
142217186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
142358c9b817SLisandro Dalcin   if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
142458c9b817SLisandro Dalcin 
142558c9b817SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
142658c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_rhs, &snes->vec_func);CHKERRQ(ierr);
142758c9b817SLisandro Dalcin   }
142858c9b817SLisandro Dalcin   if (!snes->vec_sol_update /* && snes->vec_sol */) {
142958c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr);
143058c9b817SLisandro Dalcin     ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr);
143158c9b817SLisandro Dalcin   }
143258c9b817SLisandro Dalcin 
1433ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1434ef8dffc7SBarry Smith     Mat           J;
1435ef8dffc7SBarry Smith     ISColoring    coloring;
1436ef8dffc7SBarry Smith     MatFDColoring fd;
1437a703fe33SLois Curfman McInnes 
1438ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1439ef8dffc7SBarry Smith     ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
1440ef8dffc7SBarry Smith     ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr);
1441ef8dffc7SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr);
1442ef8dffc7SBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr);
14436bf464f9SBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
1444*efd51863SBarry Smith   } else if (snes->dm && !snes->jacobian_pre){
1445*efd51863SBarry Smith     Mat J;
1446*efd51863SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1447*efd51863SBarry Smith     ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1448*efd51863SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1449ef8dffc7SBarry Smith   }
1450*efd51863SBarry Smith 
1451b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1452b710008aSBarry Smith 
1453410397dcSLisandro Dalcin   if (snes->ops->setup) {
1454410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1455410397dcSLisandro Dalcin   }
145658c9b817SLisandro Dalcin 
14577aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
14583a40ed3dSBarry Smith   PetscFunctionReturn(0);
14599b94acceSBarry Smith }
14609b94acceSBarry Smith 
14614a2ae208SSatish Balay #undef __FUNCT__
146237596af1SLisandro Dalcin #define __FUNCT__ "SNESReset"
146337596af1SLisandro Dalcin /*@
146437596af1SLisandro Dalcin    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats
146537596af1SLisandro Dalcin 
146637596af1SLisandro Dalcin    Collective on SNES
146737596af1SLisandro Dalcin 
146837596af1SLisandro Dalcin    Input Parameter:
146937596af1SLisandro Dalcin .  snes - iterative context obtained from SNESCreate()
147037596af1SLisandro Dalcin 
147137596af1SLisandro Dalcin    Level: beginner
147237596af1SLisandro Dalcin 
147337596af1SLisandro Dalcin .keywords: SNES, destroy
147437596af1SLisandro Dalcin 
147537596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
147637596af1SLisandro Dalcin @*/
147737596af1SLisandro Dalcin PetscErrorCode  SNESReset(SNES snes)
147837596af1SLisandro Dalcin {
147937596af1SLisandro Dalcin   PetscErrorCode ierr;
148037596af1SLisandro Dalcin 
148137596af1SLisandro Dalcin   PetscFunctionBegin;
148237596af1SLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
148337596af1SLisandro Dalcin   if (snes->ops->reset) {
148437596af1SLisandro Dalcin     ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr);
148537596af1SLisandro Dalcin   }
148637596af1SLisandro Dalcin   if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);}
14876bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
14886bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
14896bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr);
14906bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
14916bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
14926bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
149337596af1SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
149437596af1SLisandro Dalcin   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
149537596af1SLisandro Dalcin   snes->nwork = snes->nvwork = 0;
149637596af1SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
149737596af1SLisandro Dalcin   PetscFunctionReturn(0);
149837596af1SLisandro Dalcin }
149937596af1SLisandro Dalcin 
150037596af1SLisandro Dalcin #undef __FUNCT__
15014a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
150252baeb72SSatish Balay /*@
15039b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
15049b94acceSBarry Smith    with SNESCreate().
15059b94acceSBarry Smith 
1506c7afd0dbSLois Curfman McInnes    Collective on SNES
1507c7afd0dbSLois Curfman McInnes 
15089b94acceSBarry Smith    Input Parameter:
15099b94acceSBarry Smith .  snes - the SNES context
15109b94acceSBarry Smith 
151136851e7fSLois Curfman McInnes    Level: beginner
151236851e7fSLois Curfman McInnes 
15139b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
15149b94acceSBarry Smith 
151563a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
15169b94acceSBarry Smith @*/
15176bf464f9SBarry Smith PetscErrorCode  SNESDestroy(SNES *snes)
15189b94acceSBarry Smith {
15196849ba73SBarry Smith   PetscErrorCode ierr;
15203a40ed3dSBarry Smith 
15213a40ed3dSBarry Smith   PetscFunctionBegin;
15226bf464f9SBarry Smith   if (!*snes) PetscFunctionReturn(0);
15236bf464f9SBarry Smith   PetscValidHeaderSpecific((*snes),SNES_CLASSID,1);
15246bf464f9SBarry Smith   if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);}
1525d4bb536fSBarry Smith 
15266bf464f9SBarry Smith   ierr = SNESReset((*snes));CHKERRQ(ierr);
15276b8b9a38SLisandro Dalcin 
1528be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
15296bf464f9SBarry Smith   ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr);
15306bf464f9SBarry Smith   if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);}
15316d4c513bSLisandro Dalcin 
15326bf464f9SBarry Smith   ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr);
15336bf464f9SBarry Smith   ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr);
15346b8b9a38SLisandro Dalcin 
15356bf464f9SBarry Smith   ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr);
15366bf464f9SBarry Smith   if ((*snes)->ops->convergeddestroy) {
15376bf464f9SBarry Smith     ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr);
15386b8b9a38SLisandro Dalcin   }
15396bf464f9SBarry Smith   if ((*snes)->conv_malloc) {
15406bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr);
15416bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr);
154258c9b817SLisandro Dalcin   }
15436bf464f9SBarry Smith   ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr);
1544a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
15453a40ed3dSBarry Smith   PetscFunctionReturn(0);
15469b94acceSBarry Smith }
15479b94acceSBarry Smith 
15489b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
15499b94acceSBarry Smith 
15504a2ae208SSatish Balay #undef __FUNCT__
1551a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1552a8054027SBarry Smith /*@
1553a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1554a8054027SBarry Smith 
15553f9fe445SBarry Smith    Logically Collective on SNES
1556a8054027SBarry Smith 
1557a8054027SBarry Smith    Input Parameters:
1558a8054027SBarry Smith +  snes - the SNES context
1559a8054027SBarry 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
15603b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1561a8054027SBarry Smith 
1562a8054027SBarry Smith    Options Database Keys:
1563a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1564a8054027SBarry Smith 
1565a8054027SBarry Smith    Notes:
1566a8054027SBarry Smith    The default is 1
1567a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1568a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1569a8054027SBarry Smith 
1570a8054027SBarry Smith    Level: intermediate
1571a8054027SBarry Smith 
1572a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1573a8054027SBarry Smith 
1574e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1575a8054027SBarry Smith 
1576a8054027SBarry Smith @*/
15777087cfbeSBarry Smith PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1578a8054027SBarry Smith {
1579a8054027SBarry Smith   PetscFunctionBegin;
15800700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1581e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1582e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1583c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1584a8054027SBarry Smith   snes->lagpreconditioner = lag;
1585a8054027SBarry Smith   PetscFunctionReturn(0);
1586a8054027SBarry Smith }
1587a8054027SBarry Smith 
1588a8054027SBarry Smith #undef __FUNCT__
1589*efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence"
1590*efd51863SBarry Smith /*@
1591*efd51863SBarry Smith    SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does
1592*efd51863SBarry Smith 
1593*efd51863SBarry Smith    Logically Collective on SNES
1594*efd51863SBarry Smith 
1595*efd51863SBarry Smith    Input Parameters:
1596*efd51863SBarry Smith +  snes - the SNES context
1597*efd51863SBarry Smith -  steps - the number of refinements to do, defaults to 0
1598*efd51863SBarry Smith 
1599*efd51863SBarry Smith    Options Database Keys:
1600*efd51863SBarry Smith .    -snes_grid_sequence <steps>
1601*efd51863SBarry Smith 
1602*efd51863SBarry Smith    Level: intermediate
1603*efd51863SBarry Smith 
1604*efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1605*efd51863SBarry Smith 
1606*efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1607*efd51863SBarry Smith 
1608*efd51863SBarry Smith @*/
1609*efd51863SBarry Smith PetscErrorCode  SNESSetGridSequence(SNES snes,PetscInt steps)
1610*efd51863SBarry Smith {
1611*efd51863SBarry Smith   PetscFunctionBegin;
1612*efd51863SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1613*efd51863SBarry Smith   PetscValidLogicalCollectiveInt(snes,steps,2);
1614*efd51863SBarry Smith   snes->gridsequence = steps;
1615*efd51863SBarry Smith   PetscFunctionReturn(0);
1616*efd51863SBarry Smith }
1617*efd51863SBarry Smith 
1618*efd51863SBarry Smith #undef __FUNCT__
1619a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1620a8054027SBarry Smith /*@
1621a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1622a8054027SBarry Smith 
16233f9fe445SBarry Smith    Not Collective
1624a8054027SBarry Smith 
1625a8054027SBarry Smith    Input Parameter:
1626a8054027SBarry Smith .  snes - the SNES context
1627a8054027SBarry Smith 
1628a8054027SBarry Smith    Output Parameter:
1629a8054027SBarry 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
16303b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1631a8054027SBarry Smith 
1632a8054027SBarry Smith    Options Database Keys:
1633a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1634a8054027SBarry Smith 
1635a8054027SBarry Smith    Notes:
1636a8054027SBarry Smith    The default is 1
1637a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1638a8054027SBarry Smith 
1639a8054027SBarry Smith    Level: intermediate
1640a8054027SBarry Smith 
1641a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1642a8054027SBarry Smith 
1643a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1644a8054027SBarry Smith 
1645a8054027SBarry Smith @*/
16467087cfbeSBarry Smith PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1647a8054027SBarry Smith {
1648a8054027SBarry Smith   PetscFunctionBegin;
16490700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1650a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1651a8054027SBarry Smith   PetscFunctionReturn(0);
1652a8054027SBarry Smith }
1653a8054027SBarry Smith 
1654a8054027SBarry Smith #undef __FUNCT__
1655e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1656e35cf81dSBarry Smith /*@
1657e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1658e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1659e35cf81dSBarry Smith 
16603f9fe445SBarry Smith    Logically Collective on SNES
1661e35cf81dSBarry Smith 
1662e35cf81dSBarry Smith    Input Parameters:
1663e35cf81dSBarry Smith +  snes - the SNES context
1664e35cf81dSBarry 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
1665fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1666e35cf81dSBarry Smith 
1667e35cf81dSBarry Smith    Options Database Keys:
1668e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1669e35cf81dSBarry Smith 
1670e35cf81dSBarry Smith    Notes:
1671e35cf81dSBarry Smith    The default is 1
1672e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1673fe3ffe1eSBarry 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
1674fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1675e35cf81dSBarry Smith 
1676e35cf81dSBarry Smith    Level: intermediate
1677e35cf81dSBarry Smith 
1678e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1679e35cf81dSBarry Smith 
1680e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1681e35cf81dSBarry Smith 
1682e35cf81dSBarry Smith @*/
16837087cfbeSBarry Smith PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
1684e35cf81dSBarry Smith {
1685e35cf81dSBarry Smith   PetscFunctionBegin;
16860700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1687e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1688e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1689c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1690e35cf81dSBarry Smith   snes->lagjacobian = lag;
1691e35cf81dSBarry Smith   PetscFunctionReturn(0);
1692e35cf81dSBarry Smith }
1693e35cf81dSBarry Smith 
1694e35cf81dSBarry Smith #undef __FUNCT__
1695e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1696e35cf81dSBarry Smith /*@
1697e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1698e35cf81dSBarry Smith 
16993f9fe445SBarry Smith    Not Collective
1700e35cf81dSBarry Smith 
1701e35cf81dSBarry Smith    Input Parameter:
1702e35cf81dSBarry Smith .  snes - the SNES context
1703e35cf81dSBarry Smith 
1704e35cf81dSBarry Smith    Output Parameter:
1705e35cf81dSBarry 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
1706e35cf81dSBarry Smith          the Jacobian is built etc.
1707e35cf81dSBarry Smith 
1708e35cf81dSBarry Smith    Options Database Keys:
1709e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1710e35cf81dSBarry Smith 
1711e35cf81dSBarry Smith    Notes:
1712e35cf81dSBarry Smith    The default is 1
1713e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1714e35cf81dSBarry Smith 
1715e35cf81dSBarry Smith    Level: intermediate
1716e35cf81dSBarry Smith 
1717e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1718e35cf81dSBarry Smith 
1719e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1720e35cf81dSBarry Smith 
1721e35cf81dSBarry Smith @*/
17227087cfbeSBarry Smith PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
1723e35cf81dSBarry Smith {
1724e35cf81dSBarry Smith   PetscFunctionBegin;
17250700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1726e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1727e35cf81dSBarry Smith   PetscFunctionReturn(0);
1728e35cf81dSBarry Smith }
1729e35cf81dSBarry Smith 
1730e35cf81dSBarry Smith #undef __FUNCT__
17314a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
17329b94acceSBarry Smith /*@
1733d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
17349b94acceSBarry Smith 
17353f9fe445SBarry Smith    Logically Collective on SNES
1736c7afd0dbSLois Curfman McInnes 
17379b94acceSBarry Smith    Input Parameters:
1738c7afd0dbSLois Curfman McInnes +  snes - the SNES context
173970441072SBarry Smith .  abstol - absolute convergence tolerance
174033174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
174133174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
174233174efeSLois Curfman McInnes            of the change in the solution between steps
174333174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1744c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1745fee21e36SBarry Smith 
174633174efeSLois Curfman McInnes    Options Database Keys:
174770441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1748c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1749c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1750c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1751c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
17529b94acceSBarry Smith 
1753d7a720efSLois Curfman McInnes    Notes:
17549b94acceSBarry Smith    The default maximum number of iterations is 50.
17559b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
17569b94acceSBarry Smith 
175736851e7fSLois Curfman McInnes    Level: intermediate
175836851e7fSLois Curfman McInnes 
175933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
17609b94acceSBarry Smith 
17612492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
17629b94acceSBarry Smith @*/
17637087cfbeSBarry Smith PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
17649b94acceSBarry Smith {
17653a40ed3dSBarry Smith   PetscFunctionBegin;
17660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1767c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
1768c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
1769c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
1770c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
1771c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
1772c5eb9154SBarry Smith 
177370441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1774d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1775d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1776d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1777d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
17783a40ed3dSBarry Smith   PetscFunctionReturn(0);
17799b94acceSBarry Smith }
17809b94acceSBarry Smith 
17814a2ae208SSatish Balay #undef __FUNCT__
17824a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
17839b94acceSBarry Smith /*@
178433174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
178533174efeSLois Curfman McInnes 
1786c7afd0dbSLois Curfman McInnes    Not Collective
1787c7afd0dbSLois Curfman McInnes 
178833174efeSLois Curfman McInnes    Input Parameters:
1789c7afd0dbSLois Curfman McInnes +  snes - the SNES context
179085385478SLisandro Dalcin .  atol - absolute convergence tolerance
179133174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
179233174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
179333174efeSLois Curfman McInnes            of the change in the solution between steps
179433174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1795c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1796fee21e36SBarry Smith 
179733174efeSLois Curfman McInnes    Notes:
179833174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
179933174efeSLois Curfman McInnes 
180036851e7fSLois Curfman McInnes    Level: intermediate
180136851e7fSLois Curfman McInnes 
180233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
180333174efeSLois Curfman McInnes 
180433174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
180533174efeSLois Curfman McInnes @*/
18067087cfbeSBarry Smith PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
180733174efeSLois Curfman McInnes {
18083a40ed3dSBarry Smith   PetscFunctionBegin;
18090700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
181085385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
181133174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
181233174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
181333174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
181433174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
18153a40ed3dSBarry Smith   PetscFunctionReturn(0);
181633174efeSLois Curfman McInnes }
181733174efeSLois Curfman McInnes 
18184a2ae208SSatish Balay #undef __FUNCT__
18194a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
182033174efeSLois Curfman McInnes /*@
18219b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
18229b94acceSBarry Smith 
18233f9fe445SBarry Smith    Logically Collective on SNES
1824fee21e36SBarry Smith 
1825c7afd0dbSLois Curfman McInnes    Input Parameters:
1826c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1827c7afd0dbSLois Curfman McInnes -  tol - tolerance
1828c7afd0dbSLois Curfman McInnes 
18299b94acceSBarry Smith    Options Database Key:
1830c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
18319b94acceSBarry Smith 
183236851e7fSLois Curfman McInnes    Level: intermediate
183336851e7fSLois Curfman McInnes 
18349b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
18359b94acceSBarry Smith 
18362492ecdbSBarry Smith .seealso: SNESSetTolerances()
18379b94acceSBarry Smith @*/
18387087cfbeSBarry Smith PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
18399b94acceSBarry Smith {
18403a40ed3dSBarry Smith   PetscFunctionBegin;
18410700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1842c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
18439b94acceSBarry Smith   snes->deltatol = tol;
18443a40ed3dSBarry Smith   PetscFunctionReturn(0);
18459b94acceSBarry Smith }
18469b94acceSBarry Smith 
1847df9fa365SBarry Smith /*
1848df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1849df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1850df9fa365SBarry Smith    macros instead of functions
1851df9fa365SBarry Smith */
18524a2ae208SSatish Balay #undef __FUNCT__
1853a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
18547087cfbeSBarry Smith PetscErrorCode  SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1855ce1608b8SBarry Smith {
1856dfbe8321SBarry Smith   PetscErrorCode ierr;
1857ce1608b8SBarry Smith 
1858ce1608b8SBarry Smith   PetscFunctionBegin;
18590700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1860a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1861ce1608b8SBarry Smith   PetscFunctionReturn(0);
1862ce1608b8SBarry Smith }
1863ce1608b8SBarry Smith 
18644a2ae208SSatish Balay #undef __FUNCT__
1865a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
18667087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1867df9fa365SBarry Smith {
1868dfbe8321SBarry Smith   PetscErrorCode ierr;
1869df9fa365SBarry Smith 
1870df9fa365SBarry Smith   PetscFunctionBegin;
1871a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1872df9fa365SBarry Smith   PetscFunctionReturn(0);
1873df9fa365SBarry Smith }
1874df9fa365SBarry Smith 
18754a2ae208SSatish Balay #undef __FUNCT__
1876a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
18776bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGDestroy(PetscDrawLG *draw)
1878df9fa365SBarry Smith {
1879dfbe8321SBarry Smith   PetscErrorCode ierr;
1880df9fa365SBarry Smith 
1881df9fa365SBarry Smith   PetscFunctionBegin;
1882a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1883df9fa365SBarry Smith   PetscFunctionReturn(0);
1884df9fa365SBarry Smith }
1885df9fa365SBarry Smith 
18867087cfbeSBarry Smith extern PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1887b271bb04SBarry Smith #undef __FUNCT__
1888b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
18897087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1890b271bb04SBarry Smith {
1891b271bb04SBarry Smith   PetscDrawLG      lg;
1892b271bb04SBarry Smith   PetscErrorCode   ierr;
1893b271bb04SBarry Smith   PetscReal        x,y,per;
1894b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1895b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1896b271bb04SBarry Smith   PetscDraw        draw;
1897b271bb04SBarry Smith   PetscFunctionBegin;
1898b271bb04SBarry Smith   if (!monctx) {
1899b271bb04SBarry Smith     MPI_Comm    comm;
1900b271bb04SBarry Smith 
1901b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1902b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1903b271bb04SBarry Smith   }
1904b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1905b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1906b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1907b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1908b271bb04SBarry Smith   x = (PetscReal) n;
1909b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1910b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1911b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1912b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1913b271bb04SBarry Smith   }
1914b271bb04SBarry Smith 
1915b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1916b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1917b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1918b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1919b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1920b271bb04SBarry Smith   x = (PetscReal) n;
1921b271bb04SBarry Smith   y = 100.0*per;
1922b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1923b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1924b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1925b271bb04SBarry Smith   }
1926b271bb04SBarry Smith 
1927b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1928b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1929b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1930b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1931b271bb04SBarry Smith   x = (PetscReal) n;
1932b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1933b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1934b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1935b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1936b271bb04SBarry Smith   }
1937b271bb04SBarry Smith 
1938b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1939b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1940b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1941b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1942b271bb04SBarry Smith   x = (PetscReal) n;
1943b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1944b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1945b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1946b271bb04SBarry Smith   }
1947b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1948b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1949b271bb04SBarry Smith   }
1950b271bb04SBarry Smith   prev = rnorm;
1951b271bb04SBarry Smith   PetscFunctionReturn(0);
1952b271bb04SBarry Smith }
1953b271bb04SBarry Smith 
1954b271bb04SBarry Smith #undef __FUNCT__
1955b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
19567087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1957b271bb04SBarry Smith {
1958b271bb04SBarry Smith   PetscErrorCode ierr;
1959b271bb04SBarry Smith 
1960b271bb04SBarry Smith   PetscFunctionBegin;
1961b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1962b271bb04SBarry Smith   PetscFunctionReturn(0);
1963b271bb04SBarry Smith }
1964b271bb04SBarry Smith 
1965b271bb04SBarry Smith #undef __FUNCT__
1966b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
19676bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGRangeDestroy(PetscDrawLG *draw)
1968b271bb04SBarry Smith {
1969b271bb04SBarry Smith   PetscErrorCode ierr;
1970b271bb04SBarry Smith 
1971b271bb04SBarry Smith   PetscFunctionBegin;
1972b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1973b271bb04SBarry Smith   PetscFunctionReturn(0);
1974b271bb04SBarry Smith }
1975b271bb04SBarry Smith 
19767a03ce2fSLisandro Dalcin #undef __FUNCT__
19777a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor"
19787a03ce2fSLisandro Dalcin /*
19797a03ce2fSLisandro Dalcin      Runs the user provided monitor routines, if they exists.
19807a03ce2fSLisandro Dalcin */
19817a03ce2fSLisandro Dalcin PetscErrorCode  SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm)
19827a03ce2fSLisandro Dalcin {
19837a03ce2fSLisandro Dalcin   PetscErrorCode ierr;
19847a03ce2fSLisandro Dalcin   PetscInt       i,n = snes->numbermonitors;
19857a03ce2fSLisandro Dalcin 
19867a03ce2fSLisandro Dalcin   PetscFunctionBegin;
19877a03ce2fSLisandro Dalcin   for (i=0; i<n; i++) {
19887a03ce2fSLisandro Dalcin     ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr);
19897a03ce2fSLisandro Dalcin   }
19907a03ce2fSLisandro Dalcin   PetscFunctionReturn(0);
19917a03ce2fSLisandro Dalcin }
19927a03ce2fSLisandro Dalcin 
19939b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
19949b94acceSBarry Smith 
19954a2ae208SSatish Balay #undef __FUNCT__
1996a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
19979b94acceSBarry Smith /*@C
1998a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
19999b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
20009b94acceSBarry Smith    progress.
20019b94acceSBarry Smith 
20023f9fe445SBarry Smith    Logically Collective on SNES
2003fee21e36SBarry Smith 
2004c7afd0dbSLois Curfman McInnes    Input Parameters:
2005c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2006c7afd0dbSLois Curfman McInnes .  func - monitoring routine
2007b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
2008e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
2009b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2010b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
20119b94acceSBarry Smith 
2012c7afd0dbSLois Curfman McInnes    Calling sequence of func:
2013a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
2014c7afd0dbSLois Curfman McInnes 
2015c7afd0dbSLois Curfman McInnes +    snes - the SNES context
2016c7afd0dbSLois Curfman McInnes .    its - iteration number
2017c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
201840a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
20199b94acceSBarry Smith 
20209665c990SLois Curfman McInnes    Options Database Keys:
2021a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
2022a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
2023a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
2024cca6129bSJed Brown -    -snes_monitor_cancel - cancels all monitors that have
2025c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
2026a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
2027c7afd0dbSLois Curfman McInnes                             does not cancel those set via
2028c7afd0dbSLois Curfman McInnes                             the options database.
20299665c990SLois Curfman McInnes 
2030639f9d9dSBarry Smith    Notes:
20316bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
2032a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
20336bc08f3fSLois Curfman McInnes    order in which they were set.
2034639f9d9dSBarry Smith 
2035025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
2036025f1a04SBarry Smith 
203736851e7fSLois Curfman McInnes    Level: intermediate
203836851e7fSLois Curfman McInnes 
20399b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
20409b94acceSBarry Smith 
2041a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
20429b94acceSBarry Smith @*/
2043c2efdce3SBarry Smith PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
20449b94acceSBarry Smith {
2045b90d0a6eSBarry Smith   PetscInt i;
2046b90d0a6eSBarry Smith 
20473a40ed3dSBarry Smith   PetscFunctionBegin;
20480700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
204917186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2050b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
2051b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
2052b90d0a6eSBarry Smith 
2053b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
2054b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
2055b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
2056b90d0a6eSBarry Smith         PetscErrorCode          ierr;
2057b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
2058b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
2059b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
2060c2efdce3SBarry Smith           ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr);
2061b90d0a6eSBarry Smith           PetscFunctionReturn(0);
2062b90d0a6eSBarry Smith         }
2063b90d0a6eSBarry Smith       }
2064b90d0a6eSBarry Smith     }
2065b90d0a6eSBarry Smith   }
2066b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
2067b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
2068639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
20693a40ed3dSBarry Smith   PetscFunctionReturn(0);
20709b94acceSBarry Smith }
20719b94acceSBarry Smith 
20724a2ae208SSatish Balay #undef __FUNCT__
2073a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
20745cd90555SBarry Smith /*@C
2075a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
20765cd90555SBarry Smith 
20773f9fe445SBarry Smith    Logically Collective on SNES
2078c7afd0dbSLois Curfman McInnes 
20795cd90555SBarry Smith    Input Parameters:
20805cd90555SBarry Smith .  snes - the SNES context
20815cd90555SBarry Smith 
20821a480d89SAdministrator    Options Database Key:
2083a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
2084a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
2085c7afd0dbSLois Curfman McInnes     set via the options database
20865cd90555SBarry Smith 
20875cd90555SBarry Smith    Notes:
20885cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
20895cd90555SBarry Smith 
209036851e7fSLois Curfman McInnes    Level: intermediate
209136851e7fSLois Curfman McInnes 
20925cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
20935cd90555SBarry Smith 
2094a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
20955cd90555SBarry Smith @*/
20967087cfbeSBarry Smith PetscErrorCode  SNESMonitorCancel(SNES snes)
20975cd90555SBarry Smith {
2098d952e501SBarry Smith   PetscErrorCode ierr;
2099d952e501SBarry Smith   PetscInt       i;
2100d952e501SBarry Smith 
21015cd90555SBarry Smith   PetscFunctionBegin;
21020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2103d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2104d952e501SBarry Smith     if (snes->monitordestroy[i]) {
21053c4aec1bSBarry Smith       ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr);
2106d952e501SBarry Smith     }
2107d952e501SBarry Smith   }
21085cd90555SBarry Smith   snes->numbermonitors = 0;
21095cd90555SBarry Smith   PetscFunctionReturn(0);
21105cd90555SBarry Smith }
21115cd90555SBarry Smith 
21124a2ae208SSatish Balay #undef __FUNCT__
21134a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
21149b94acceSBarry Smith /*@C
21159b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
21169b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
21179b94acceSBarry Smith 
21183f9fe445SBarry Smith    Logically Collective on SNES
2119fee21e36SBarry Smith 
2120c7afd0dbSLois Curfman McInnes    Input Parameters:
2121c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2122c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
21237f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
21247f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
21259b94acceSBarry Smith 
2126c7afd0dbSLois Curfman McInnes    Calling sequence of func:
212706ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2128c7afd0dbSLois Curfman McInnes 
2129c7afd0dbSLois Curfman McInnes +    snes - the SNES context
213006ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2131c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2132184914b5SBarry Smith .    reason - reason for convergence/divergence
2133c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
21344b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
21354b27c08aSLois Curfman McInnes -    f - 2-norm of function
21369b94acceSBarry Smith 
213736851e7fSLois Curfman McInnes    Level: advanced
213836851e7fSLois Curfman McInnes 
21399b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
21409b94acceSBarry Smith 
214185385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
21429b94acceSBarry Smith @*/
21437087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
21449b94acceSBarry Smith {
21457f7931b9SBarry Smith   PetscErrorCode ierr;
21467f7931b9SBarry Smith 
21473a40ed3dSBarry Smith   PetscFunctionBegin;
21480700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
214985385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
21507f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
21517f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
21527f7931b9SBarry Smith   }
215385385478SLisandro Dalcin   snes->ops->converged        = func;
21547f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
215585385478SLisandro Dalcin   snes->cnvP                  = cctx;
21563a40ed3dSBarry Smith   PetscFunctionReturn(0);
21579b94acceSBarry Smith }
21589b94acceSBarry Smith 
21594a2ae208SSatish Balay #undef __FUNCT__
21604a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
216152baeb72SSatish Balay /*@
2162184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2163184914b5SBarry Smith 
2164184914b5SBarry Smith    Not Collective
2165184914b5SBarry Smith 
2166184914b5SBarry Smith    Input Parameter:
2167184914b5SBarry Smith .  snes - the SNES context
2168184914b5SBarry Smith 
2169184914b5SBarry Smith    Output Parameter:
21704d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2171184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2172184914b5SBarry Smith 
2173184914b5SBarry Smith    Level: intermediate
2174184914b5SBarry Smith 
2175184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2176184914b5SBarry Smith 
2177184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2178184914b5SBarry Smith 
217985385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2180184914b5SBarry Smith @*/
21817087cfbeSBarry Smith PetscErrorCode  SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2182184914b5SBarry Smith {
2183184914b5SBarry Smith   PetscFunctionBegin;
21840700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
21854482741eSBarry Smith   PetscValidPointer(reason,2);
2186184914b5SBarry Smith   *reason = snes->reason;
2187184914b5SBarry Smith   PetscFunctionReturn(0);
2188184914b5SBarry Smith }
2189184914b5SBarry Smith 
21904a2ae208SSatish Balay #undef __FUNCT__
21914a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2192c9005455SLois Curfman McInnes /*@
2193c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2194c9005455SLois Curfman McInnes 
21953f9fe445SBarry Smith    Logically Collective on SNES
2196fee21e36SBarry Smith 
2197c7afd0dbSLois Curfman McInnes    Input Parameters:
2198c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
21998c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2200cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2201758f92a0SBarry Smith .  na  - size of a and its
220264731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2203758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2204c7afd0dbSLois Curfman McInnes 
2205308dcc3eSBarry Smith    Notes:
2206308dcc3eSBarry Smith    If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2207308dcc3eSBarry Smith    default array of length 10000 is allocated.
2208308dcc3eSBarry Smith 
2209c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2210c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2211c9005455SLois Curfman McInnes    during the section of code that is being timed.
2212c9005455SLois Curfman McInnes 
221336851e7fSLois Curfman McInnes    Level: intermediate
221436851e7fSLois Curfman McInnes 
2215c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2216758f92a0SBarry Smith 
221708405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2218758f92a0SBarry Smith 
2219c9005455SLois Curfman McInnes @*/
22207087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool  reset)
2221c9005455SLois Curfman McInnes {
2222308dcc3eSBarry Smith   PetscErrorCode ierr;
2223308dcc3eSBarry Smith 
22243a40ed3dSBarry Smith   PetscFunctionBegin;
22250700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22264482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2227a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2228308dcc3eSBarry Smith   if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) {
2229308dcc3eSBarry Smith     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2230308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr);
2231308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr);
2232308dcc3eSBarry Smith     snes->conv_malloc   = PETSC_TRUE;
2233308dcc3eSBarry Smith   }
2234c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2235758f92a0SBarry Smith   snes->conv_hist_its   = its;
2236758f92a0SBarry Smith   snes->conv_hist_max   = na;
2237a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2238758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2239758f92a0SBarry Smith   PetscFunctionReturn(0);
2240758f92a0SBarry Smith }
2241758f92a0SBarry Smith 
2242308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2243c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
2244c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
2245308dcc3eSBarry Smith EXTERN_C_BEGIN
2246308dcc3eSBarry Smith #undef __FUNCT__
2247308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab"
2248308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
2249308dcc3eSBarry Smith {
2250308dcc3eSBarry Smith   mxArray        *mat;
2251308dcc3eSBarry Smith   PetscInt       i;
2252308dcc3eSBarry Smith   PetscReal      *ar;
2253308dcc3eSBarry Smith 
2254308dcc3eSBarry Smith   PetscFunctionBegin;
2255308dcc3eSBarry Smith   mat  = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
2256308dcc3eSBarry Smith   ar   = (PetscReal*) mxGetData(mat);
2257308dcc3eSBarry Smith   for (i=0; i<snes->conv_hist_len; i++) {
2258308dcc3eSBarry Smith     ar[i] = snes->conv_hist[i];
2259308dcc3eSBarry Smith   }
2260308dcc3eSBarry Smith   PetscFunctionReturn(mat);
2261308dcc3eSBarry Smith }
2262308dcc3eSBarry Smith EXTERN_C_END
2263308dcc3eSBarry Smith #endif
2264308dcc3eSBarry Smith 
2265308dcc3eSBarry Smith 
22664a2ae208SSatish Balay #undef __FUNCT__
22674a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
22680c4c9dddSBarry Smith /*@C
2269758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2270758f92a0SBarry Smith 
22713f9fe445SBarry Smith    Not Collective
2272758f92a0SBarry Smith 
2273758f92a0SBarry Smith    Input Parameter:
2274758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2275758f92a0SBarry Smith 
2276758f92a0SBarry Smith    Output Parameters:
2277758f92a0SBarry Smith .  a   - array to hold history
2278758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2279758f92a0SBarry Smith          negative if not converged) for each solve.
2280758f92a0SBarry Smith -  na  - size of a and its
2281758f92a0SBarry Smith 
2282758f92a0SBarry Smith    Notes:
2283758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2284758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2285758f92a0SBarry Smith 
2286758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2287758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2288758f92a0SBarry Smith    during the section of code that is being timed.
2289758f92a0SBarry Smith 
2290758f92a0SBarry Smith    Level: intermediate
2291758f92a0SBarry Smith 
2292758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2293758f92a0SBarry Smith 
2294758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2295758f92a0SBarry Smith 
2296758f92a0SBarry Smith @*/
22977087cfbeSBarry Smith PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2298758f92a0SBarry Smith {
2299758f92a0SBarry Smith   PetscFunctionBegin;
23000700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2301758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2302758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2303758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
23043a40ed3dSBarry Smith   PetscFunctionReturn(0);
2305c9005455SLois Curfman McInnes }
2306c9005455SLois Curfman McInnes 
2307e74ef692SMatthew Knepley #undef __FUNCT__
2308e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2309ac226902SBarry Smith /*@C
231076b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
2311eec8f646SJed Brown   at the beginning of every iteration of the nonlinear solve. Specifically
23127e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
231376b2cf59SMatthew Knepley 
23143f9fe445SBarry Smith   Logically Collective on SNES
231576b2cf59SMatthew Knepley 
231676b2cf59SMatthew Knepley   Input Parameters:
231776b2cf59SMatthew Knepley . snes - The nonlinear solver context
231876b2cf59SMatthew Knepley . func - The function
231976b2cf59SMatthew Knepley 
232076b2cf59SMatthew Knepley   Calling sequence of func:
2321b5d30489SBarry Smith . func (SNES snes, PetscInt step);
232276b2cf59SMatthew Knepley 
232376b2cf59SMatthew Knepley . step - The current step of the iteration
232476b2cf59SMatthew Knepley 
2325fe97e370SBarry Smith   Level: advanced
2326fe97e370SBarry Smith 
2327fe97e370SBarry 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()
2328fe97e370SBarry Smith         This is not used by most users.
232976b2cf59SMatthew Knepley 
233076b2cf59SMatthew Knepley .keywords: SNES, update
2331b5d30489SBarry Smith 
233285385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
233376b2cf59SMatthew Knepley @*/
23347087cfbeSBarry Smith PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
233576b2cf59SMatthew Knepley {
233676b2cf59SMatthew Knepley   PetscFunctionBegin;
23370700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2338e7788613SBarry Smith   snes->ops->update = func;
233976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
234076b2cf59SMatthew Knepley }
234176b2cf59SMatthew Knepley 
2342e74ef692SMatthew Knepley #undef __FUNCT__
2343e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
234476b2cf59SMatthew Knepley /*@
234576b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
234676b2cf59SMatthew Knepley 
234776b2cf59SMatthew Knepley   Not collective
234876b2cf59SMatthew Knepley 
234976b2cf59SMatthew Knepley   Input Parameters:
235076b2cf59SMatthew Knepley . snes - The nonlinear solver context
235176b2cf59SMatthew Knepley . step - The current step of the iteration
235276b2cf59SMatthew Knepley 
2353205452f4SMatthew Knepley   Level: intermediate
2354205452f4SMatthew Knepley 
235576b2cf59SMatthew Knepley .keywords: SNES, update
2356a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
235776b2cf59SMatthew Knepley @*/
23587087cfbeSBarry Smith PetscErrorCode  SNESDefaultUpdate(SNES snes, PetscInt step)
235976b2cf59SMatthew Knepley {
236076b2cf59SMatthew Knepley   PetscFunctionBegin;
236176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
236276b2cf59SMatthew Knepley }
236376b2cf59SMatthew Knepley 
23644a2ae208SSatish Balay #undef __FUNCT__
23654a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
23669b94acceSBarry Smith /*
23679b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
23689b94acceSBarry Smith    positive parameter delta.
23699b94acceSBarry Smith 
23709b94acceSBarry Smith     Input Parameters:
2371c7afd0dbSLois Curfman McInnes +   snes - the SNES context
23729b94acceSBarry Smith .   y - approximate solution of linear system
23739b94acceSBarry Smith .   fnorm - 2-norm of current function
2374c7afd0dbSLois Curfman McInnes -   delta - trust region size
23759b94acceSBarry Smith 
23769b94acceSBarry Smith     Output Parameters:
2377c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
23789b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
23799b94acceSBarry Smith     region, and exceeds zero otherwise.
2380c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
23819b94acceSBarry Smith 
23829b94acceSBarry Smith     Note:
23834b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
23849b94acceSBarry Smith     is set to be the maximum allowable step size.
23859b94acceSBarry Smith 
23869b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
23879b94acceSBarry Smith */
2388dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
23899b94acceSBarry Smith {
2390064f8208SBarry Smith   PetscReal      nrm;
2391ea709b57SSatish Balay   PetscScalar    cnorm;
2392dfbe8321SBarry Smith   PetscErrorCode ierr;
23933a40ed3dSBarry Smith 
23943a40ed3dSBarry Smith   PetscFunctionBegin;
23950700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23960700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2397c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2398184914b5SBarry Smith 
2399064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2400064f8208SBarry Smith   if (nrm > *delta) {
2401064f8208SBarry Smith      nrm = *delta/nrm;
2402064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2403064f8208SBarry Smith      cnorm = nrm;
24042dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
24059b94acceSBarry Smith      *ynorm = *delta;
24069b94acceSBarry Smith   } else {
24079b94acceSBarry Smith      *gpnorm = 0.0;
2408064f8208SBarry Smith      *ynorm = nrm;
24099b94acceSBarry Smith   }
24103a40ed3dSBarry Smith   PetscFunctionReturn(0);
24119b94acceSBarry Smith }
24129b94acceSBarry Smith 
24134a2ae208SSatish Balay #undef __FUNCT__
24144a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
24156ce558aeSBarry Smith /*@C
2416f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2417f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
24189b94acceSBarry Smith 
2419c7afd0dbSLois Curfman McInnes    Collective on SNES
2420c7afd0dbSLois Curfman McInnes 
2421b2002411SLois Curfman McInnes    Input Parameters:
2422c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2423f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
242485385478SLisandro Dalcin -  x - the solution vector.
24259b94acceSBarry Smith 
2426b2002411SLois Curfman McInnes    Notes:
24278ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
24288ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
24298ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
24308ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
24318ddd3da0SLois Curfman McInnes 
243236851e7fSLois Curfman McInnes    Level: beginner
243336851e7fSLois Curfman McInnes 
24349b94acceSBarry Smith .keywords: SNES, nonlinear, solve
24359b94acceSBarry Smith 
243685385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
24379b94acceSBarry Smith @*/
24387087cfbeSBarry Smith PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
24399b94acceSBarry Smith {
2440dfbe8321SBarry Smith   PetscErrorCode ierr;
2441ace3abfcSBarry Smith   PetscBool      flg;
2442eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2443eabae89aSBarry Smith   PetscViewer    viewer;
2444*efd51863SBarry Smith   PetscInt       grid;
2445052efed2SBarry Smith 
24463a40ed3dSBarry Smith   PetscFunctionBegin;
24470700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24480700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2449f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
24500700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
245185385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
245285385478SLisandro Dalcin 
2453*efd51863SBarry Smith   for (grid=0; grid<snes->gridsequence+1; grid++) {
2454*efd51863SBarry Smith 
245585385478SLisandro Dalcin     /* set solution vector */
2456*efd51863SBarry Smith     if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);}
24576bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
245885385478SLisandro Dalcin     snes->vec_sol = x;
245985385478SLisandro Dalcin     /* set afine vector if provided */
246085385478SLisandro Dalcin     if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
24616bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
246285385478SLisandro Dalcin     snes->vec_rhs = b;
246385385478SLisandro Dalcin 
246470e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
24653f149594SLisandro Dalcin 
2466abc0a331SBarry Smith     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
246750ffb88aSMatthew Knepley     snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2468d5e45103SBarry Smith 
24693f149594SLisandro Dalcin     ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
24704936397dSBarry Smith     ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
247185385478SLisandro Dalcin     ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
24724936397dSBarry Smith     if (snes->domainerror){
24734936397dSBarry Smith       snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
24744936397dSBarry Smith       snes->domainerror = PETSC_FALSE;
24754936397dSBarry Smith     }
247617186662SBarry Smith     if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
24773f149594SLisandro Dalcin 
24787adad957SLisandro Dalcin     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2479eabae89aSBarry Smith     if (flg && !PetscPreLoadingOn) {
24807adad957SLisandro Dalcin       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2481eabae89aSBarry Smith       ierr = SNESView(snes,viewer);CHKERRQ(ierr);
24826bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2483eabae89aSBarry Smith     }
2484eabae89aSBarry Smith 
248590d69ab7SBarry Smith     flg  = PETSC_FALSE;
2486acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2487da9b6338SBarry Smith     if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
24885968eb51SBarry Smith     if (snes->printreason) {
24895968eb51SBarry Smith       if (snes->reason > 0) {
24907adad957SLisandro Dalcin         ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
24915968eb51SBarry Smith       } else {
24927adad957SLisandro Dalcin         ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
24935968eb51SBarry Smith       }
24945968eb51SBarry Smith     }
24955968eb51SBarry Smith 
2496e113a28aSBarry Smith     if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
2497*efd51863SBarry Smith     if (grid <  snes->gridsequence) {
2498*efd51863SBarry Smith       DM  fine;
2499*efd51863SBarry Smith       Vec xnew;
2500*efd51863SBarry Smith       Mat interp;
2501*efd51863SBarry Smith 
2502*efd51863SBarry Smith       ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr);
2503*efd51863SBarry Smith       ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr);
2504*efd51863SBarry Smith       ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr);
2505*efd51863SBarry Smith       ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr);
2506*efd51863SBarry Smith       ierr = MatDestroy(&interp);CHKERRQ(ierr);
2507*efd51863SBarry Smith       x    = xnew;
2508*efd51863SBarry Smith 
2509*efd51863SBarry Smith       ierr = SNESReset(snes);CHKERRQ(ierr);
2510*efd51863SBarry Smith       ierr = SNESSetDM(snes,fine);CHKERRQ(ierr);
2511*efd51863SBarry Smith       ierr = DMDestroy(&fine);CHKERRQ(ierr);
2512*efd51863SBarry Smith     }
2513*efd51863SBarry Smith   }
25143a40ed3dSBarry Smith   PetscFunctionReturn(0);
25159b94acceSBarry Smith }
25169b94acceSBarry Smith 
25179b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
25189b94acceSBarry Smith 
25194a2ae208SSatish Balay #undef __FUNCT__
25204a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
252182bf6240SBarry Smith /*@C
25224b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
25239b94acceSBarry Smith 
2524fee21e36SBarry Smith    Collective on SNES
2525fee21e36SBarry Smith 
2526c7afd0dbSLois Curfman McInnes    Input Parameters:
2527c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2528454a90a3SBarry Smith -  type - a known method
2529c7afd0dbSLois Curfman McInnes 
2530c7afd0dbSLois Curfman McInnes    Options Database Key:
2531454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2532c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2533ae12b187SLois Curfman McInnes 
25349b94acceSBarry Smith    Notes:
2535e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
25364b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2537c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
25384b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2539c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
25409b94acceSBarry Smith 
2541ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2542ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2543ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2544ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2545ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2546ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2547ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2548ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2549ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2550b0a32e0cSBarry Smith   appropriate method.
255136851e7fSLois Curfman McInnes 
255236851e7fSLois Curfman McInnes   Level: intermediate
2553a703fe33SLois Curfman McInnes 
2554454a90a3SBarry Smith .keywords: SNES, set, type
2555435da068SBarry Smith 
2556435da068SBarry Smith .seealso: SNESType, SNESCreate()
2557435da068SBarry Smith 
25589b94acceSBarry Smith @*/
25597087cfbeSBarry Smith PetscErrorCode  SNESSetType(SNES snes,const SNESType type)
25609b94acceSBarry Smith {
2561dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
2562ace3abfcSBarry Smith   PetscBool      match;
25633a40ed3dSBarry Smith 
25643a40ed3dSBarry Smith   PetscFunctionBegin;
25650700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25664482741eSBarry Smith   PetscValidCharPointer(type,2);
256782bf6240SBarry Smith 
25686831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
25690f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
257092ff6ae8SBarry Smith 
25714b91b6eaSBarry Smith   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2572e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
257375396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
257475396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
257575396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
257675396ef9SLisandro Dalcin   snes->ops->setup          = 0;
257775396ef9SLisandro Dalcin   snes->ops->solve          = 0;
257875396ef9SLisandro Dalcin   snes->ops->view           = 0;
257975396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
258075396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
258175396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
258275396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
2583454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
258403bfa161SLisandro Dalcin   ierr = (*r)(snes);CHKERRQ(ierr);
25859fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
25869fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
25879fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr);
25889fb22e1aSBarry Smith   }
25899fb22e1aSBarry Smith #endif
25903a40ed3dSBarry Smith   PetscFunctionReturn(0);
25919b94acceSBarry Smith }
25929b94acceSBarry Smith 
2593a847f771SSatish Balay 
25949b94acceSBarry Smith /* --------------------------------------------------------------------- */
25954a2ae208SSatish Balay #undef __FUNCT__
25964a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
259752baeb72SSatish Balay /*@
25989b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2599f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
26009b94acceSBarry Smith 
2601fee21e36SBarry Smith    Not Collective
2602fee21e36SBarry Smith 
260336851e7fSLois Curfman McInnes    Level: advanced
260436851e7fSLois Curfman McInnes 
26059b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
26069b94acceSBarry Smith 
26079b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
26089b94acceSBarry Smith @*/
26097087cfbeSBarry Smith PetscErrorCode  SNESRegisterDestroy(void)
26109b94acceSBarry Smith {
2611dfbe8321SBarry Smith   PetscErrorCode ierr;
261282bf6240SBarry Smith 
26133a40ed3dSBarry Smith   PetscFunctionBegin;
26141441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
26154c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
26163a40ed3dSBarry Smith   PetscFunctionReturn(0);
26179b94acceSBarry Smith }
26189b94acceSBarry Smith 
26194a2ae208SSatish Balay #undef __FUNCT__
26204a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
26219b94acceSBarry Smith /*@C
26229a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
26239b94acceSBarry Smith 
2624c7afd0dbSLois Curfman McInnes    Not Collective
2625c7afd0dbSLois Curfman McInnes 
26269b94acceSBarry Smith    Input Parameter:
26274b0e389bSBarry Smith .  snes - nonlinear solver context
26289b94acceSBarry Smith 
26299b94acceSBarry Smith    Output Parameter:
26303a7fca6bSBarry Smith .  type - SNES method (a character string)
26319b94acceSBarry Smith 
263236851e7fSLois Curfman McInnes    Level: intermediate
263336851e7fSLois Curfman McInnes 
2634454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
26359b94acceSBarry Smith @*/
26367087cfbeSBarry Smith PetscErrorCode  SNESGetType(SNES snes,const SNESType *type)
26379b94acceSBarry Smith {
26383a40ed3dSBarry Smith   PetscFunctionBegin;
26390700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26404482741eSBarry Smith   PetscValidPointer(type,2);
26417adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
26423a40ed3dSBarry Smith   PetscFunctionReturn(0);
26439b94acceSBarry Smith }
26449b94acceSBarry Smith 
26454a2ae208SSatish Balay #undef __FUNCT__
26464a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
264752baeb72SSatish Balay /*@
26489b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
26499b94acceSBarry Smith    stored.
26509b94acceSBarry Smith 
2651c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2652c7afd0dbSLois Curfman McInnes 
26539b94acceSBarry Smith    Input Parameter:
26549b94acceSBarry Smith .  snes - the SNES context
26559b94acceSBarry Smith 
26569b94acceSBarry Smith    Output Parameter:
26579b94acceSBarry Smith .  x - the solution
26589b94acceSBarry Smith 
265970e92668SMatthew Knepley    Level: intermediate
266036851e7fSLois Curfman McInnes 
26619b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
26629b94acceSBarry Smith 
266385385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
26649b94acceSBarry Smith @*/
26657087cfbeSBarry Smith PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
26669b94acceSBarry Smith {
26673a40ed3dSBarry Smith   PetscFunctionBegin;
26680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26694482741eSBarry Smith   PetscValidPointer(x,2);
267085385478SLisandro Dalcin   *x = snes->vec_sol;
267170e92668SMatthew Knepley   PetscFunctionReturn(0);
267270e92668SMatthew Knepley }
267370e92668SMatthew Knepley 
267470e92668SMatthew Knepley #undef __FUNCT__
26754a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
267652baeb72SSatish Balay /*@
26779b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
26789b94acceSBarry Smith    stored.
26799b94acceSBarry Smith 
2680c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2681c7afd0dbSLois Curfman McInnes 
26829b94acceSBarry Smith    Input Parameter:
26839b94acceSBarry Smith .  snes - the SNES context
26849b94acceSBarry Smith 
26859b94acceSBarry Smith    Output Parameter:
26869b94acceSBarry Smith .  x - the solution update
26879b94acceSBarry Smith 
268836851e7fSLois Curfman McInnes    Level: advanced
268936851e7fSLois Curfman McInnes 
26909b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
26919b94acceSBarry Smith 
269285385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
26939b94acceSBarry Smith @*/
26947087cfbeSBarry Smith PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
26959b94acceSBarry Smith {
26963a40ed3dSBarry Smith   PetscFunctionBegin;
26970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26984482741eSBarry Smith   PetscValidPointer(x,2);
269985385478SLisandro Dalcin   *x = snes->vec_sol_update;
27003a40ed3dSBarry Smith   PetscFunctionReturn(0);
27019b94acceSBarry Smith }
27029b94acceSBarry Smith 
27034a2ae208SSatish Balay #undef __FUNCT__
27044a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
27059b94acceSBarry Smith /*@C
27063638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
27079b94acceSBarry Smith 
2708c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2709c7afd0dbSLois Curfman McInnes 
27109b94acceSBarry Smith    Input Parameter:
27119b94acceSBarry Smith .  snes - the SNES context
27129b94acceSBarry Smith 
27139b94acceSBarry Smith    Output Parameter:
27147bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
271570e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
271670e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
27179b94acceSBarry Smith 
271836851e7fSLois Curfman McInnes    Level: advanced
271936851e7fSLois Curfman McInnes 
2720a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
27219b94acceSBarry Smith 
27224b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
27239b94acceSBarry Smith @*/
27247087cfbeSBarry Smith PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
27259b94acceSBarry Smith {
27263a40ed3dSBarry Smith   PetscFunctionBegin;
27270700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
272885385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2729e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
273070e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
27313a40ed3dSBarry Smith   PetscFunctionReturn(0);
27329b94acceSBarry Smith }
27339b94acceSBarry Smith 
27344a2ae208SSatish Balay #undef __FUNCT__
27354a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
27363c7409f5SSatish Balay /*@C
27373c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2738d850072dSLois Curfman McInnes    SNES options in the database.
27393c7409f5SSatish Balay 
27403f9fe445SBarry Smith    Logically Collective on SNES
2741fee21e36SBarry Smith 
2742c7afd0dbSLois Curfman McInnes    Input Parameter:
2743c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2744c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2745c7afd0dbSLois Curfman McInnes 
2746d850072dSLois Curfman McInnes    Notes:
2747a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2748c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2749d850072dSLois Curfman McInnes 
275036851e7fSLois Curfman McInnes    Level: advanced
275136851e7fSLois Curfman McInnes 
27523c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2753a86d99e1SLois Curfman McInnes 
2754a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
27553c7409f5SSatish Balay @*/
27567087cfbeSBarry Smith PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
27573c7409f5SSatish Balay {
2758dfbe8321SBarry Smith   PetscErrorCode ierr;
27593c7409f5SSatish Balay 
27603a40ed3dSBarry Smith   PetscFunctionBegin;
27610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2762639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
27631cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
276494b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
27653a40ed3dSBarry Smith   PetscFunctionReturn(0);
27663c7409f5SSatish Balay }
27673c7409f5SSatish Balay 
27684a2ae208SSatish Balay #undef __FUNCT__
27694a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
27703c7409f5SSatish Balay /*@C
2771f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2772d850072dSLois Curfman McInnes    SNES options in the database.
27733c7409f5SSatish Balay 
27743f9fe445SBarry Smith    Logically Collective on SNES
2775fee21e36SBarry Smith 
2776c7afd0dbSLois Curfman McInnes    Input Parameters:
2777c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2778c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2779c7afd0dbSLois Curfman McInnes 
2780d850072dSLois Curfman McInnes    Notes:
2781a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2782c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2783d850072dSLois Curfman McInnes 
278436851e7fSLois Curfman McInnes    Level: advanced
278536851e7fSLois Curfman McInnes 
27863c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2787a86d99e1SLois Curfman McInnes 
2788a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
27893c7409f5SSatish Balay @*/
27907087cfbeSBarry Smith PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
27913c7409f5SSatish Balay {
2792dfbe8321SBarry Smith   PetscErrorCode ierr;
27933c7409f5SSatish Balay 
27943a40ed3dSBarry Smith   PetscFunctionBegin;
27950700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2796639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
27971cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
279894b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
27993a40ed3dSBarry Smith   PetscFunctionReturn(0);
28003c7409f5SSatish Balay }
28013c7409f5SSatish Balay 
28024a2ae208SSatish Balay #undef __FUNCT__
28034a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
28049ab63eb5SSatish Balay /*@C
28053c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
28063c7409f5SSatish Balay    SNES options in the database.
28073c7409f5SSatish Balay 
2808c7afd0dbSLois Curfman McInnes    Not Collective
2809c7afd0dbSLois Curfman McInnes 
28103c7409f5SSatish Balay    Input Parameter:
28113c7409f5SSatish Balay .  snes - the SNES context
28123c7409f5SSatish Balay 
28133c7409f5SSatish Balay    Output Parameter:
28143c7409f5SSatish Balay .  prefix - pointer to the prefix string used
28153c7409f5SSatish Balay 
28164ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
28179ab63eb5SSatish Balay    sufficient length to hold the prefix.
28189ab63eb5SSatish Balay 
281936851e7fSLois Curfman McInnes    Level: advanced
282036851e7fSLois Curfman McInnes 
28213c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2822a86d99e1SLois Curfman McInnes 
2823a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
28243c7409f5SSatish Balay @*/
28257087cfbeSBarry Smith PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
28263c7409f5SSatish Balay {
2827dfbe8321SBarry Smith   PetscErrorCode ierr;
28283c7409f5SSatish Balay 
28293a40ed3dSBarry Smith   PetscFunctionBegin;
28300700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2831639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
28323a40ed3dSBarry Smith   PetscFunctionReturn(0);
28333c7409f5SSatish Balay }
28343c7409f5SSatish Balay 
2835b2002411SLois Curfman McInnes 
28364a2ae208SSatish Balay #undef __FUNCT__
28374a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
28383cea93caSBarry Smith /*@C
28393cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
28403cea93caSBarry Smith 
28417f6c08e0SMatthew Knepley   Level: advanced
28423cea93caSBarry Smith @*/
28437087cfbeSBarry Smith PetscErrorCode  SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2844b2002411SLois Curfman McInnes {
2845e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2846dfbe8321SBarry Smith   PetscErrorCode ierr;
2847b2002411SLois Curfman McInnes 
2848b2002411SLois Curfman McInnes   PetscFunctionBegin;
2849b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2850c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2851b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2852b2002411SLois Curfman McInnes }
2853da9b6338SBarry Smith 
2854da9b6338SBarry Smith #undef __FUNCT__
2855da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
28567087cfbeSBarry Smith PetscErrorCode  SNESTestLocalMin(SNES snes)
2857da9b6338SBarry Smith {
2858dfbe8321SBarry Smith   PetscErrorCode ierr;
285977431f27SBarry Smith   PetscInt       N,i,j;
2860da9b6338SBarry Smith   Vec            u,uh,fh;
2861da9b6338SBarry Smith   PetscScalar    value;
2862da9b6338SBarry Smith   PetscReal      norm;
2863da9b6338SBarry Smith 
2864da9b6338SBarry Smith   PetscFunctionBegin;
2865da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2866da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2867da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2868da9b6338SBarry Smith 
2869da9b6338SBarry Smith   /* currently only works for sequential */
2870da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2871da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2872da9b6338SBarry Smith   for (i=0; i<N; i++) {
2873da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
287477431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2875da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2876ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2877da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
28783ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2879da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
288077431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2881da9b6338SBarry Smith       value = -value;
2882da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2883da9b6338SBarry Smith     }
2884da9b6338SBarry Smith   }
28856bf464f9SBarry Smith   ierr = VecDestroy(&uh);CHKERRQ(ierr);
28866bf464f9SBarry Smith   ierr = VecDestroy(&fh);CHKERRQ(ierr);
2887da9b6338SBarry Smith   PetscFunctionReturn(0);
2888da9b6338SBarry Smith }
288971f87433Sdalcinl 
289071f87433Sdalcinl #undef __FUNCT__
2891fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
289271f87433Sdalcinl /*@
2893fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
289471f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
289571f87433Sdalcinl    Newton method.
289671f87433Sdalcinl 
28973f9fe445SBarry Smith    Logically Collective on SNES
289871f87433Sdalcinl 
289971f87433Sdalcinl    Input Parameters:
290071f87433Sdalcinl +  snes - SNES context
290171f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
290271f87433Sdalcinl 
290364ba62caSBarry Smith     Options Database:
290464ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
290564ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
290664ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
290764ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
290864ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
290964ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
291064ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
291164ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
291264ba62caSBarry Smith 
291371f87433Sdalcinl    Notes:
291471f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
291571f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
291671f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
291771f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
291871f87433Sdalcinl    solver.
291971f87433Sdalcinl 
292071f87433Sdalcinl    Level: advanced
292171f87433Sdalcinl 
292271f87433Sdalcinl    Reference:
292371f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
292471f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
292571f87433Sdalcinl 
292671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
292771f87433Sdalcinl 
2928fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
292971f87433Sdalcinl @*/
29307087cfbeSBarry Smith PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool  flag)
293171f87433Sdalcinl {
293271f87433Sdalcinl   PetscFunctionBegin;
29330700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2934acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flag,2);
293571f87433Sdalcinl   snes->ksp_ewconv = flag;
293671f87433Sdalcinl   PetscFunctionReturn(0);
293771f87433Sdalcinl }
293871f87433Sdalcinl 
293971f87433Sdalcinl #undef __FUNCT__
2940fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
294171f87433Sdalcinl /*@
2942fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
294371f87433Sdalcinl    for computing relative tolerance for linear solvers within an
294471f87433Sdalcinl    inexact Newton method.
294571f87433Sdalcinl 
294671f87433Sdalcinl    Not Collective
294771f87433Sdalcinl 
294871f87433Sdalcinl    Input Parameter:
294971f87433Sdalcinl .  snes - SNES context
295071f87433Sdalcinl 
295171f87433Sdalcinl    Output Parameter:
295271f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
295371f87433Sdalcinl 
295471f87433Sdalcinl    Notes:
295571f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
295671f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
295771f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
295871f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
295971f87433Sdalcinl    solver.
296071f87433Sdalcinl 
296171f87433Sdalcinl    Level: advanced
296271f87433Sdalcinl 
296371f87433Sdalcinl    Reference:
296471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
296571f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
296671f87433Sdalcinl 
296771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
296871f87433Sdalcinl 
2969fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
297071f87433Sdalcinl @*/
29717087cfbeSBarry Smith PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
297271f87433Sdalcinl {
297371f87433Sdalcinl   PetscFunctionBegin;
29740700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
297571f87433Sdalcinl   PetscValidPointer(flag,2);
297671f87433Sdalcinl   *flag = snes->ksp_ewconv;
297771f87433Sdalcinl   PetscFunctionReturn(0);
297871f87433Sdalcinl }
297971f87433Sdalcinl 
298071f87433Sdalcinl #undef __FUNCT__
2981fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
298271f87433Sdalcinl /*@
2983fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
298471f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
298571f87433Sdalcinl    Newton method.
298671f87433Sdalcinl 
29873f9fe445SBarry Smith    Logically Collective on SNES
298871f87433Sdalcinl 
298971f87433Sdalcinl    Input Parameters:
299071f87433Sdalcinl +    snes - SNES context
299171f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
299271f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
299371f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
299471f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
299571f87433Sdalcinl              (0 <= gamma2 <= 1)
299671f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
299771f87433Sdalcinl .    alpha2 - power for safeguard
299871f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
299971f87433Sdalcinl 
300071f87433Sdalcinl    Note:
300171f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
300271f87433Sdalcinl 
300371f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
300471f87433Sdalcinl 
300571f87433Sdalcinl    Level: advanced
300671f87433Sdalcinl 
300771f87433Sdalcinl    Reference:
300871f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
300971f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
301071f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
301171f87433Sdalcinl 
301271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
301371f87433Sdalcinl 
3014fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
301571f87433Sdalcinl @*/
30167087cfbeSBarry Smith PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
301771f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
301871f87433Sdalcinl {
3019fa9f3622SBarry Smith   SNESKSPEW *kctx;
302071f87433Sdalcinl   PetscFunctionBegin;
30210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3022fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3023e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
3024c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
3025c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
3026c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
3027c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
3028c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
3029c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
3030c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
303171f87433Sdalcinl 
303271f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
303371f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
303471f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
303571f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
303671f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
303771f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
303871f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
303971f87433Sdalcinl 
304071f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
3041e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
304271f87433Sdalcinl   }
304371f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
3044e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
304571f87433Sdalcinl   }
304671f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
3047e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
304871f87433Sdalcinl   }
304971f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
3050e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
305171f87433Sdalcinl   }
305271f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
3053e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
305471f87433Sdalcinl   }
305571f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
3056e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
305771f87433Sdalcinl   }
305871f87433Sdalcinl   PetscFunctionReturn(0);
305971f87433Sdalcinl }
306071f87433Sdalcinl 
306171f87433Sdalcinl #undef __FUNCT__
3062fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
306371f87433Sdalcinl /*@
3064fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
306571f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
306671f87433Sdalcinl    Newton method.
306771f87433Sdalcinl 
306871f87433Sdalcinl    Not Collective
306971f87433Sdalcinl 
307071f87433Sdalcinl    Input Parameters:
307171f87433Sdalcinl      snes - SNES context
307271f87433Sdalcinl 
307371f87433Sdalcinl    Output Parameters:
307471f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
307571f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
307671f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
307771f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
307871f87433Sdalcinl              (0 <= gamma2 <= 1)
307971f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
308071f87433Sdalcinl .    alpha2 - power for safeguard
308171f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
308271f87433Sdalcinl 
308371f87433Sdalcinl    Level: advanced
308471f87433Sdalcinl 
308571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
308671f87433Sdalcinl 
3087fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
308871f87433Sdalcinl @*/
30897087cfbeSBarry Smith PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
309071f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
309171f87433Sdalcinl {
3092fa9f3622SBarry Smith   SNESKSPEW *kctx;
309371f87433Sdalcinl   PetscFunctionBegin;
30940700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3095fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3096e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
309771f87433Sdalcinl   if(version)   *version   = kctx->version;
309871f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
309971f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
310071f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
310171f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
310271f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
310371f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
310471f87433Sdalcinl   PetscFunctionReturn(0);
310571f87433Sdalcinl }
310671f87433Sdalcinl 
310771f87433Sdalcinl #undef __FUNCT__
3108fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
3109fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
311071f87433Sdalcinl {
311171f87433Sdalcinl   PetscErrorCode ierr;
3112fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
311371f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
311471f87433Sdalcinl 
311571f87433Sdalcinl   PetscFunctionBegin;
3116e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
311771f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
311871f87433Sdalcinl     rtol = kctx->rtol_0;
311971f87433Sdalcinl   } else {
312071f87433Sdalcinl     if (kctx->version == 1) {
312171f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
312271f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
312371f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
312471f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
312571f87433Sdalcinl     } else if (kctx->version == 2) {
312671f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
312771f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
312871f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
312971f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
313071f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
313171f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
313271f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
313371f87433Sdalcinl       stol = PetscMax(rtol,stol);
313471f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
313571f87433Sdalcinl       /* safeguard: avoid oversolving */
313671f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
313771f87433Sdalcinl       stol = PetscMax(rtol,stol);
313871f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
3139e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
314071f87433Sdalcinl   }
314171f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
314271f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
314371f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
314471f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
314571f87433Sdalcinl   PetscFunctionReturn(0);
314671f87433Sdalcinl }
314771f87433Sdalcinl 
314871f87433Sdalcinl #undef __FUNCT__
3149fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
3150fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
315171f87433Sdalcinl {
315271f87433Sdalcinl   PetscErrorCode ierr;
3153fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
315471f87433Sdalcinl   PCSide         pcside;
315571f87433Sdalcinl   Vec            lres;
315671f87433Sdalcinl 
315771f87433Sdalcinl   PetscFunctionBegin;
3158e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
315971f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
316071f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
316171f87433Sdalcinl   if (kctx->version == 1) {
3162b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
316371f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
316471f87433Sdalcinl       /* KSP residual is true linear residual */
316571f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
316671f87433Sdalcinl     } else {
316771f87433Sdalcinl       /* KSP residual is preconditioned residual */
316871f87433Sdalcinl       /* compute true linear residual norm */
316971f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
317071f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
317171f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
317271f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
31736bf464f9SBarry Smith       ierr = VecDestroy(&lres);CHKERRQ(ierr);
317471f87433Sdalcinl     }
317571f87433Sdalcinl   }
317671f87433Sdalcinl   PetscFunctionReturn(0);
317771f87433Sdalcinl }
317871f87433Sdalcinl 
317971f87433Sdalcinl #undef __FUNCT__
318071f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
318171f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
318271f87433Sdalcinl {
318371f87433Sdalcinl   PetscErrorCode ierr;
318471f87433Sdalcinl 
318571f87433Sdalcinl   PetscFunctionBegin;
3186fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
318771f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3188fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
318971f87433Sdalcinl   PetscFunctionReturn(0);
319071f87433Sdalcinl }
31916c699258SBarry Smith 
31926c699258SBarry Smith #undef __FUNCT__
31936c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
31946c699258SBarry Smith /*@
31956c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
31966c699258SBarry Smith 
31973f9fe445SBarry Smith    Logically Collective on SNES
31986c699258SBarry Smith 
31996c699258SBarry Smith    Input Parameters:
32006c699258SBarry Smith +  snes - the preconditioner context
32016c699258SBarry Smith -  dm - the dm
32026c699258SBarry Smith 
32036c699258SBarry Smith    Level: intermediate
32046c699258SBarry Smith 
32056c699258SBarry Smith 
32066c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
32076c699258SBarry Smith @*/
32087087cfbeSBarry Smith PetscErrorCode  SNESSetDM(SNES snes,DM dm)
32096c699258SBarry Smith {
32106c699258SBarry Smith   PetscErrorCode ierr;
3211345fed2cSBarry Smith   KSP            ksp;
32126c699258SBarry Smith 
32136c699258SBarry Smith   PetscFunctionBegin;
32140700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3215d0660788SBarry Smith   if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);}
32166bf464f9SBarry Smith   ierr = DMDestroy(&snes->dm);CHKERRQ(ierr);
32176c699258SBarry Smith   snes->dm = dm;
3218345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3219345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3220f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
32216c699258SBarry Smith   PetscFunctionReturn(0);
32226c699258SBarry Smith }
32236c699258SBarry Smith 
32246c699258SBarry Smith #undef __FUNCT__
32256c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
32266c699258SBarry Smith /*@
32276c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
32286c699258SBarry Smith 
32293f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
32306c699258SBarry Smith 
32316c699258SBarry Smith    Input Parameter:
32326c699258SBarry Smith . snes - the preconditioner context
32336c699258SBarry Smith 
32346c699258SBarry Smith    Output Parameter:
32356c699258SBarry Smith .  dm - the dm
32366c699258SBarry Smith 
32376c699258SBarry Smith    Level: intermediate
32386c699258SBarry Smith 
32396c699258SBarry Smith 
32406c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
32416c699258SBarry Smith @*/
32427087cfbeSBarry Smith PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
32436c699258SBarry Smith {
32446c699258SBarry Smith   PetscFunctionBegin;
32450700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32466c699258SBarry Smith   *dm = snes->dm;
32476c699258SBarry Smith   PetscFunctionReturn(0);
32486c699258SBarry Smith }
32490807856dSBarry Smith 
325069b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3251c6db04a5SJed Brown #include <mex.h>
325269b4f73cSBarry Smith 
32538f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
32548f6e6473SBarry Smith 
32550807856dSBarry Smith #undef __FUNCT__
32560807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab"
32570807856dSBarry Smith /*
32580807856dSBarry Smith    SNESComputeFunction_Matlab - Calls the function that has been set with
32590807856dSBarry Smith                          SNESSetFunctionMatlab().
32600807856dSBarry Smith 
32610807856dSBarry Smith    Collective on SNES
32620807856dSBarry Smith 
32630807856dSBarry Smith    Input Parameters:
32640807856dSBarry Smith +  snes - the SNES context
32650807856dSBarry Smith -  x - input vector
32660807856dSBarry Smith 
32670807856dSBarry Smith    Output Parameter:
32680807856dSBarry Smith .  y - function vector, as set by SNESSetFunction()
32690807856dSBarry Smith 
32700807856dSBarry Smith    Notes:
32710807856dSBarry Smith    SNESComputeFunction() is typically used within nonlinear solvers
32720807856dSBarry Smith    implementations, so most users would not generally call this routine
32730807856dSBarry Smith    themselves.
32740807856dSBarry Smith 
32750807856dSBarry Smith    Level: developer
32760807856dSBarry Smith 
32770807856dSBarry Smith .keywords: SNES, nonlinear, compute, function
32780807856dSBarry Smith 
32790807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
328061b2408cSBarry Smith */
32817087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx)
32820807856dSBarry Smith {
3283e650e774SBarry Smith   PetscErrorCode    ierr;
32848f6e6473SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
32858f6e6473SBarry Smith   int               nlhs = 1,nrhs = 5;
32868f6e6473SBarry Smith   mxArray	    *plhs[1],*prhs[5];
328791621f2eSBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
3288e650e774SBarry Smith 
32890807856dSBarry Smith   PetscFunctionBegin;
32900807856dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
32910807856dSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
32920807856dSBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
32930807856dSBarry Smith   PetscCheckSameComm(snes,1,x,2);
32940807856dSBarry Smith   PetscCheckSameComm(snes,1,y,3);
32950807856dSBarry Smith 
32960807856dSBarry Smith   /* call Matlab function in ctx with arguments x and y */
3297e650e774SBarry Smith 
329891621f2eSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3299e650e774SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3300e650e774SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
330191621f2eSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
330291621f2eSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
330391621f2eSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
33048f6e6473SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
33058f6e6473SBarry Smith   prhs[4] =  sctx->ctx;
3306b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr);
3307e650e774SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3308e650e774SBarry Smith   mxDestroyArray(prhs[0]);
3309e650e774SBarry Smith   mxDestroyArray(prhs[1]);
3310e650e774SBarry Smith   mxDestroyArray(prhs[2]);
33118f6e6473SBarry Smith   mxDestroyArray(prhs[3]);
3312e650e774SBarry Smith   mxDestroyArray(plhs[0]);
33130807856dSBarry Smith   PetscFunctionReturn(0);
33140807856dSBarry Smith }
33150807856dSBarry Smith 
33160807856dSBarry Smith 
33170807856dSBarry Smith #undef __FUNCT__
33180807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab"
331961b2408cSBarry Smith /*
33200807856dSBarry Smith    SNESSetFunctionMatlab - Sets the function evaluation routine and function
33210807856dSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3322e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
33230807856dSBarry Smith 
33240807856dSBarry Smith    Logically Collective on SNES
33250807856dSBarry Smith 
33260807856dSBarry Smith    Input Parameters:
33270807856dSBarry Smith +  snes - the SNES context
33280807856dSBarry Smith .  r - vector to store function value
33290807856dSBarry Smith -  func - function evaluation routine
33300807856dSBarry Smith 
33310807856dSBarry Smith    Calling sequence of func:
333261b2408cSBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
33330807856dSBarry Smith 
33340807856dSBarry Smith 
33350807856dSBarry Smith    Notes:
33360807856dSBarry Smith    The Newton-like methods typically solve linear systems of the form
33370807856dSBarry Smith $      f'(x) x = -f(x),
33380807856dSBarry Smith    where f'(x) denotes the Jacobian matrix and f(x) is the function.
33390807856dSBarry Smith 
33400807856dSBarry Smith    Level: beginner
33410807856dSBarry Smith 
33420807856dSBarry Smith .keywords: SNES, nonlinear, set, function
33430807856dSBarry Smith 
33440807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
334561b2408cSBarry Smith */
33467087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx)
33470807856dSBarry Smith {
33480807856dSBarry Smith   PetscErrorCode    ierr;
33498f6e6473SBarry Smith   SNESMatlabContext *sctx;
33500807856dSBarry Smith 
33510807856dSBarry Smith   PetscFunctionBegin;
33528f6e6473SBarry Smith   /* currently sctx is memory bleed */
33538f6e6473SBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
33548f6e6473SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
33558f6e6473SBarry Smith   /*
33568f6e6473SBarry Smith      This should work, but it doesn't
33578f6e6473SBarry Smith   sctx->ctx = ctx;
33588f6e6473SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
33598f6e6473SBarry Smith   */
33608f6e6473SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
33618f6e6473SBarry Smith   ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr);
33620807856dSBarry Smith   PetscFunctionReturn(0);
33630807856dSBarry Smith }
336469b4f73cSBarry Smith 
336561b2408cSBarry Smith #undef __FUNCT__
336661b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab"
336761b2408cSBarry Smith /*
336861b2408cSBarry Smith    SNESComputeJacobian_Matlab - Calls the function that has been set with
336961b2408cSBarry Smith                          SNESSetJacobianMatlab().
337061b2408cSBarry Smith 
337161b2408cSBarry Smith    Collective on SNES
337261b2408cSBarry Smith 
337361b2408cSBarry Smith    Input Parameters:
337461b2408cSBarry Smith +  snes - the SNES context
337561b2408cSBarry Smith .  x - input vector
337661b2408cSBarry Smith .  A, B - the matrices
337761b2408cSBarry Smith -  ctx - user context
337861b2408cSBarry Smith 
337961b2408cSBarry Smith    Output Parameter:
338061b2408cSBarry Smith .  flag - structure of the matrix
338161b2408cSBarry Smith 
338261b2408cSBarry Smith    Level: developer
338361b2408cSBarry Smith 
338461b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function
338561b2408cSBarry Smith 
338661b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
338761b2408cSBarry Smith @*/
33887087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx)
338961b2408cSBarry Smith {
339061b2408cSBarry Smith   PetscErrorCode    ierr;
339161b2408cSBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
339261b2408cSBarry Smith   int               nlhs = 2,nrhs = 6;
339361b2408cSBarry Smith   mxArray	    *plhs[2],*prhs[6];
339461b2408cSBarry Smith   long long int     lx = 0,lA = 0,ls = 0, lB = 0;
339561b2408cSBarry Smith 
339661b2408cSBarry Smith   PetscFunctionBegin;
339761b2408cSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
339861b2408cSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
339961b2408cSBarry Smith 
340061b2408cSBarry Smith   /* call Matlab function in ctx with arguments x and y */
340161b2408cSBarry Smith 
340261b2408cSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
340361b2408cSBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
340461b2408cSBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
340561b2408cSBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
340661b2408cSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
340761b2408cSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
340861b2408cSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
340961b2408cSBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
341061b2408cSBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
341161b2408cSBarry Smith   prhs[5] =  sctx->ctx;
3412b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr);
341361b2408cSBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
341461b2408cSBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
341561b2408cSBarry Smith   mxDestroyArray(prhs[0]);
341661b2408cSBarry Smith   mxDestroyArray(prhs[1]);
341761b2408cSBarry Smith   mxDestroyArray(prhs[2]);
341861b2408cSBarry Smith   mxDestroyArray(prhs[3]);
341961b2408cSBarry Smith   mxDestroyArray(prhs[4]);
342061b2408cSBarry Smith   mxDestroyArray(plhs[0]);
342161b2408cSBarry Smith   mxDestroyArray(plhs[1]);
342261b2408cSBarry Smith   PetscFunctionReturn(0);
342361b2408cSBarry Smith }
342461b2408cSBarry Smith 
342561b2408cSBarry Smith 
342661b2408cSBarry Smith #undef __FUNCT__
342761b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab"
342861b2408cSBarry Smith /*
342961b2408cSBarry Smith    SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
343061b2408cSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3431e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
343261b2408cSBarry Smith 
343361b2408cSBarry Smith    Logically Collective on SNES
343461b2408cSBarry Smith 
343561b2408cSBarry Smith    Input Parameters:
343661b2408cSBarry Smith +  snes - the SNES context
343761b2408cSBarry Smith .  A,B - Jacobian matrices
343861b2408cSBarry Smith .  func - function evaluation routine
343961b2408cSBarry Smith -  ctx - user context
344061b2408cSBarry Smith 
344161b2408cSBarry Smith    Calling sequence of func:
344261b2408cSBarry Smith $    flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx);
344361b2408cSBarry Smith 
344461b2408cSBarry Smith 
344561b2408cSBarry Smith    Level: developer
344661b2408cSBarry Smith 
344761b2408cSBarry Smith .keywords: SNES, nonlinear, set, function
344861b2408cSBarry Smith 
344961b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
345061b2408cSBarry Smith */
34517087cfbeSBarry Smith PetscErrorCode  SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx)
345261b2408cSBarry Smith {
345361b2408cSBarry Smith   PetscErrorCode    ierr;
345461b2408cSBarry Smith   SNESMatlabContext *sctx;
345561b2408cSBarry Smith 
345661b2408cSBarry Smith   PetscFunctionBegin;
345761b2408cSBarry Smith   /* currently sctx is memory bleed */
345861b2408cSBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
345961b2408cSBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
346061b2408cSBarry Smith   /*
346161b2408cSBarry Smith      This should work, but it doesn't
346261b2408cSBarry Smith   sctx->ctx = ctx;
346361b2408cSBarry Smith   mexMakeArrayPersistent(sctx->ctx);
346461b2408cSBarry Smith   */
346561b2408cSBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
346661b2408cSBarry Smith   ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
346761b2408cSBarry Smith   PetscFunctionReturn(0);
346861b2408cSBarry Smith }
346969b4f73cSBarry Smith 
3470f9eb7ae2SShri Abhyankar #undef __FUNCT__
3471f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab"
3472f9eb7ae2SShri Abhyankar /*
3473f9eb7ae2SShri Abhyankar    SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab().
3474f9eb7ae2SShri Abhyankar 
3475f9eb7ae2SShri Abhyankar    Collective on SNES
3476f9eb7ae2SShri Abhyankar 
3477f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction()
3478f9eb7ae2SShri Abhyankar @*/
34797087cfbeSBarry Smith PetscErrorCode  SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx)
3480f9eb7ae2SShri Abhyankar {
3481f9eb7ae2SShri Abhyankar   PetscErrorCode  ierr;
348248f37e70SShri Abhyankar   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
3483f9eb7ae2SShri Abhyankar   int             nlhs = 1,nrhs = 6;
3484f9eb7ae2SShri Abhyankar   mxArray	  *plhs[1],*prhs[6];
3485f9eb7ae2SShri Abhyankar   long long int   lx = 0,ls = 0;
3486f9eb7ae2SShri Abhyankar   Vec             x=snes->vec_sol;
3487f9eb7ae2SShri Abhyankar 
3488f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3489f9eb7ae2SShri Abhyankar   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3490f9eb7ae2SShri Abhyankar 
3491f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3492f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3493f9eb7ae2SShri Abhyankar   prhs[0] =  mxCreateDoubleScalar((double)ls);
3494f9eb7ae2SShri Abhyankar   prhs[1] =  mxCreateDoubleScalar((double)it);
3495f9eb7ae2SShri Abhyankar   prhs[2] =  mxCreateDoubleScalar((double)fnorm);
3496f9eb7ae2SShri Abhyankar   prhs[3] =  mxCreateDoubleScalar((double)lx);
3497f9eb7ae2SShri Abhyankar   prhs[4] =  mxCreateString(sctx->funcname);
3498f9eb7ae2SShri Abhyankar   prhs[5] =  sctx->ctx;
3499f9eb7ae2SShri Abhyankar   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr);
3500f9eb7ae2SShri Abhyankar   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3501f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[0]);
3502f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[1]);
3503f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[2]);
3504f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[3]);
3505f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[4]);
3506f9eb7ae2SShri Abhyankar   mxDestroyArray(plhs[0]);
3507f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3508f9eb7ae2SShri Abhyankar }
3509f9eb7ae2SShri Abhyankar 
3510f9eb7ae2SShri Abhyankar 
3511f9eb7ae2SShri Abhyankar #undef __FUNCT__
3512f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab"
3513f9eb7ae2SShri Abhyankar /*
3514e3c5b3baSBarry Smith    SNESMonitorSetMatlab - Sets the monitor function from MATLAB
3515f9eb7ae2SShri Abhyankar 
3516f9eb7ae2SShri Abhyankar    Level: developer
3517f9eb7ae2SShri Abhyankar 
3518f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function
3519f9eb7ae2SShri Abhyankar 
3520f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
3521f9eb7ae2SShri Abhyankar */
35227087cfbeSBarry Smith PetscErrorCode  SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx)
3523f9eb7ae2SShri Abhyankar {
3524f9eb7ae2SShri Abhyankar   PetscErrorCode    ierr;
3525f9eb7ae2SShri Abhyankar   SNESMatlabContext *sctx;
3526f9eb7ae2SShri Abhyankar 
3527f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3528f9eb7ae2SShri Abhyankar   /* currently sctx is memory bleed */
3529f9eb7ae2SShri Abhyankar   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
3530f9eb7ae2SShri Abhyankar   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3531f9eb7ae2SShri Abhyankar   /*
3532f9eb7ae2SShri Abhyankar      This should work, but it doesn't
3533f9eb7ae2SShri Abhyankar   sctx->ctx = ctx;
3534f9eb7ae2SShri Abhyankar   mexMakeArrayPersistent(sctx->ctx);
3535f9eb7ae2SShri Abhyankar   */
3536f9eb7ae2SShri Abhyankar   sctx->ctx = mxDuplicateArray(ctx);
3537f9eb7ae2SShri Abhyankar   ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3538f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3539f9eb7ae2SShri Abhyankar }
3540f9eb7ae2SShri Abhyankar 
354169b4f73cSBarry Smith #endif
3542