xref: /petsc/src/snes/interface/snes.c (revision 1b2093e4d1d741eb25c3aec22d4fd75e2597f056)
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;
337efd51863SBarry 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     }
379efd51863SBarry Smith     ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr);
380efd51863SBarry Smith     if (flg) {
381efd51863SBarry Smith       ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr);
382efd51863SBarry 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 
488d25893d9SBarry Smith #undef __FUNCT__
489d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext"
490d25893d9SBarry Smith /*@
491d25893d9SBarry Smith    SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for
492d25893d9SBarry Smith    the nonlinear solvers.
493d25893d9SBarry Smith 
494d25893d9SBarry Smith    Logically Collective on SNES
495d25893d9SBarry Smith 
496d25893d9SBarry Smith    Input Parameters:
497d25893d9SBarry Smith +  snes - the SNES context
498d25893d9SBarry Smith .  compute - function to compute the context
499d25893d9SBarry Smith -  destroy - function to destroy the context
500d25893d9SBarry Smith 
501d25893d9SBarry Smith    Level: intermediate
502d25893d9SBarry Smith 
503d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context
504d25893d9SBarry Smith 
505d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext()
506d25893d9SBarry Smith @*/
507d25893d9SBarry Smith PetscErrorCode  SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**))
508d25893d9SBarry Smith {
509d25893d9SBarry Smith   PetscFunctionBegin;
510d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
511d25893d9SBarry Smith   snes->ops->usercompute = compute;
512d25893d9SBarry Smith   snes->ops->userdestroy = destroy;
513d25893d9SBarry Smith   PetscFunctionReturn(0);
514d25893d9SBarry Smith }
515a847f771SSatish Balay 
5164a2ae208SSatish Balay #undef __FUNCT__
5174a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
5189b94acceSBarry Smith /*@
5199b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
5209b94acceSBarry Smith    the nonlinear solvers.
5219b94acceSBarry Smith 
5223f9fe445SBarry Smith    Logically Collective on SNES
523fee21e36SBarry Smith 
524c7afd0dbSLois Curfman McInnes    Input Parameters:
525c7afd0dbSLois Curfman McInnes +  snes - the SNES context
526c7afd0dbSLois Curfman McInnes -  usrP - optional user context
527c7afd0dbSLois Curfman McInnes 
52836851e7fSLois Curfman McInnes    Level: intermediate
52936851e7fSLois Curfman McInnes 
5309b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
5319b94acceSBarry Smith 
532d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext()
5339b94acceSBarry Smith @*/
5347087cfbeSBarry Smith PetscErrorCode  SNESSetApplicationContext(SNES snes,void *usrP)
5359b94acceSBarry Smith {
536*1b2093e4SBarry Smith   PetscErrorCode ierr;
537*1b2093e4SBarry Smith 
5383a40ed3dSBarry Smith   PetscFunctionBegin;
5390700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
540*1b2093e4SBarry Smith   ierr       = KSPSetApplicationContext(snes->ksp,usrP);CHKERRQ(ierr);
5419b94acceSBarry Smith   snes->user = usrP;
5423a40ed3dSBarry Smith   PetscFunctionReturn(0);
5439b94acceSBarry Smith }
54474679c65SBarry Smith 
5454a2ae208SSatish Balay #undef __FUNCT__
5464a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
5479b94acceSBarry Smith /*@C
5489b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
5499b94acceSBarry Smith    nonlinear solvers.
5509b94acceSBarry Smith 
551c7afd0dbSLois Curfman McInnes    Not Collective
552c7afd0dbSLois Curfman McInnes 
5539b94acceSBarry Smith    Input Parameter:
5549b94acceSBarry Smith .  snes - SNES context
5559b94acceSBarry Smith 
5569b94acceSBarry Smith    Output Parameter:
5579b94acceSBarry Smith .  usrP - user context
5589b94acceSBarry Smith 
55936851e7fSLois Curfman McInnes    Level: intermediate
56036851e7fSLois Curfman McInnes 
5619b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
5629b94acceSBarry Smith 
5639b94acceSBarry Smith .seealso: SNESSetApplicationContext()
5649b94acceSBarry Smith @*/
5657087cfbeSBarry Smith PetscErrorCode  SNESGetApplicationContext(SNES snes,void **usrP)
5669b94acceSBarry Smith {
5673a40ed3dSBarry Smith   PetscFunctionBegin;
5680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5699b94acceSBarry Smith   *usrP = snes->user;
5703a40ed3dSBarry Smith   PetscFunctionReturn(0);
5719b94acceSBarry Smith }
57274679c65SBarry Smith 
5734a2ae208SSatish Balay #undef __FUNCT__
5744a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
5759b94acceSBarry Smith /*@
576c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
577c8228a4eSBarry Smith    at this time.
5789b94acceSBarry Smith 
579c7afd0dbSLois Curfman McInnes    Not Collective
580c7afd0dbSLois Curfman McInnes 
5819b94acceSBarry Smith    Input Parameter:
5829b94acceSBarry Smith .  snes - SNES context
5839b94acceSBarry Smith 
5849b94acceSBarry Smith    Output Parameter:
5859b94acceSBarry Smith .  iter - iteration number
5869b94acceSBarry Smith 
587c8228a4eSBarry Smith    Notes:
588c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
589c8228a4eSBarry Smith 
590c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
59108405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
59208405cd6SLois Curfman McInnes .vb
59308405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
59408405cd6SLois Curfman McInnes       if (!(it % 2)) {
59508405cd6SLois Curfman McInnes         [compute Jacobian here]
59608405cd6SLois Curfman McInnes       }
59708405cd6SLois Curfman McInnes .ve
598c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
59908405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
600c8228a4eSBarry Smith 
60136851e7fSLois Curfman McInnes    Level: intermediate
60236851e7fSLois Curfman McInnes 
6032b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
6042b668275SBarry Smith 
605b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
6069b94acceSBarry Smith @*/
6077087cfbeSBarry Smith PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt* iter)
6089b94acceSBarry Smith {
6093a40ed3dSBarry Smith   PetscFunctionBegin;
6100700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6114482741eSBarry Smith   PetscValidIntPointer(iter,2);
6129b94acceSBarry Smith   *iter = snes->iter;
6133a40ed3dSBarry Smith   PetscFunctionReturn(0);
6149b94acceSBarry Smith }
61574679c65SBarry Smith 
6164a2ae208SSatish Balay #undef __FUNCT__
6174a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
6189b94acceSBarry Smith /*@
6199b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
6209b94acceSBarry Smith    with SNESSSetFunction().
6219b94acceSBarry Smith 
622c7afd0dbSLois Curfman McInnes    Collective on SNES
623c7afd0dbSLois Curfman McInnes 
6249b94acceSBarry Smith    Input Parameter:
6259b94acceSBarry Smith .  snes - SNES context
6269b94acceSBarry Smith 
6279b94acceSBarry Smith    Output Parameter:
6289b94acceSBarry Smith .  fnorm - 2-norm of function
6299b94acceSBarry Smith 
63036851e7fSLois Curfman McInnes    Level: intermediate
63136851e7fSLois Curfman McInnes 
6329b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
633a86d99e1SLois Curfman McInnes 
634b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
6359b94acceSBarry Smith @*/
6367087cfbeSBarry Smith PetscErrorCode  SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
6379b94acceSBarry Smith {
6383a40ed3dSBarry Smith   PetscFunctionBegin;
6390700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6404482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
6419b94acceSBarry Smith   *fnorm = snes->norm;
6423a40ed3dSBarry Smith   PetscFunctionReturn(0);
6439b94acceSBarry Smith }
64474679c65SBarry Smith 
6454a2ae208SSatish Balay #undef __FUNCT__
646b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
6479b94acceSBarry Smith /*@
648b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
6499b94acceSBarry Smith    attempted by the nonlinear solver.
6509b94acceSBarry Smith 
651c7afd0dbSLois Curfman McInnes    Not Collective
652c7afd0dbSLois Curfman McInnes 
6539b94acceSBarry Smith    Input Parameter:
6549b94acceSBarry Smith .  snes - SNES context
6559b94acceSBarry Smith 
6569b94acceSBarry Smith    Output Parameter:
6579b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
6589b94acceSBarry Smith 
659c96a6f78SLois Curfman McInnes    Notes:
660c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
661c96a6f78SLois Curfman McInnes 
66236851e7fSLois Curfman McInnes    Level: intermediate
66336851e7fSLois Curfman McInnes 
6649b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
66558ebbce7SBarry Smith 
666e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
66758ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
6689b94acceSBarry Smith @*/
6697087cfbeSBarry Smith PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
6709b94acceSBarry Smith {
6713a40ed3dSBarry Smith   PetscFunctionBegin;
6720700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6734482741eSBarry Smith   PetscValidIntPointer(nfails,2);
67450ffb88aSMatthew Knepley   *nfails = snes->numFailures;
67550ffb88aSMatthew Knepley   PetscFunctionReturn(0);
67650ffb88aSMatthew Knepley }
67750ffb88aSMatthew Knepley 
67850ffb88aSMatthew Knepley #undef __FUNCT__
679b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
68050ffb88aSMatthew Knepley /*@
681b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
68250ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
68350ffb88aSMatthew Knepley 
68450ffb88aSMatthew Knepley    Not Collective
68550ffb88aSMatthew Knepley 
68650ffb88aSMatthew Knepley    Input Parameters:
68750ffb88aSMatthew Knepley +  snes     - SNES context
68850ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
68950ffb88aSMatthew Knepley 
69050ffb88aSMatthew Knepley    Level: intermediate
69150ffb88aSMatthew Knepley 
69250ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
69358ebbce7SBarry Smith 
694e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
69558ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
69650ffb88aSMatthew Knepley @*/
6977087cfbeSBarry Smith PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
69850ffb88aSMatthew Knepley {
69950ffb88aSMatthew Knepley   PetscFunctionBegin;
7000700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
70150ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
70250ffb88aSMatthew Knepley   PetscFunctionReturn(0);
70350ffb88aSMatthew Knepley }
70450ffb88aSMatthew Knepley 
70550ffb88aSMatthew Knepley #undef __FUNCT__
706b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
70750ffb88aSMatthew Knepley /*@
708b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
70950ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
71050ffb88aSMatthew Knepley 
71150ffb88aSMatthew Knepley    Not Collective
71250ffb88aSMatthew Knepley 
71350ffb88aSMatthew Knepley    Input Parameter:
71450ffb88aSMatthew Knepley .  snes     - SNES context
71550ffb88aSMatthew Knepley 
71650ffb88aSMatthew Knepley    Output Parameter:
71750ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
71850ffb88aSMatthew Knepley 
71950ffb88aSMatthew Knepley    Level: intermediate
72050ffb88aSMatthew Knepley 
72150ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
72258ebbce7SBarry Smith 
723e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
72458ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
72558ebbce7SBarry Smith 
72650ffb88aSMatthew Knepley @*/
7277087cfbeSBarry Smith PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
72850ffb88aSMatthew Knepley {
72950ffb88aSMatthew Knepley   PetscFunctionBegin;
7300700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7314482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
73250ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
7333a40ed3dSBarry Smith   PetscFunctionReturn(0);
7349b94acceSBarry Smith }
735a847f771SSatish Balay 
7364a2ae208SSatish Balay #undef __FUNCT__
7372541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
7382541af92SBarry Smith /*@
7392541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
7402541af92SBarry Smith      done by SNES.
7412541af92SBarry Smith 
7422541af92SBarry Smith    Not Collective
7432541af92SBarry Smith 
7442541af92SBarry Smith    Input Parameter:
7452541af92SBarry Smith .  snes     - SNES context
7462541af92SBarry Smith 
7472541af92SBarry Smith    Output Parameter:
7482541af92SBarry Smith .  nfuncs - number of evaluations
7492541af92SBarry Smith 
7502541af92SBarry Smith    Level: intermediate
7512541af92SBarry Smith 
7522541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
75358ebbce7SBarry Smith 
754e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
7552541af92SBarry Smith @*/
7567087cfbeSBarry Smith PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
7572541af92SBarry Smith {
7582541af92SBarry Smith   PetscFunctionBegin;
7590700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7602541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
7612541af92SBarry Smith   *nfuncs = snes->nfuncs;
7622541af92SBarry Smith   PetscFunctionReturn(0);
7632541af92SBarry Smith }
7642541af92SBarry Smith 
7652541af92SBarry Smith #undef __FUNCT__
7663d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
7673d4c4710SBarry Smith /*@
7683d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
7693d4c4710SBarry Smith    linear solvers.
7703d4c4710SBarry Smith 
7713d4c4710SBarry Smith    Not Collective
7723d4c4710SBarry Smith 
7733d4c4710SBarry Smith    Input Parameter:
7743d4c4710SBarry Smith .  snes - SNES context
7753d4c4710SBarry Smith 
7763d4c4710SBarry Smith    Output Parameter:
7773d4c4710SBarry Smith .  nfails - number of failed solves
7783d4c4710SBarry Smith 
7793d4c4710SBarry Smith    Notes:
7803d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
7813d4c4710SBarry Smith 
7823d4c4710SBarry Smith    Level: intermediate
7833d4c4710SBarry Smith 
7843d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
78558ebbce7SBarry Smith 
786e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
7873d4c4710SBarry Smith @*/
7887087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
7893d4c4710SBarry Smith {
7903d4c4710SBarry Smith   PetscFunctionBegin;
7910700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7923d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
7933d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
7943d4c4710SBarry Smith   PetscFunctionReturn(0);
7953d4c4710SBarry Smith }
7963d4c4710SBarry Smith 
7973d4c4710SBarry Smith #undef __FUNCT__
7983d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
7993d4c4710SBarry Smith /*@
8003d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
8013d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
8023d4c4710SBarry Smith 
8033f9fe445SBarry Smith    Logically Collective on SNES
8043d4c4710SBarry Smith 
8053d4c4710SBarry Smith    Input Parameters:
8063d4c4710SBarry Smith +  snes     - SNES context
8073d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
8083d4c4710SBarry Smith 
8093d4c4710SBarry Smith    Level: intermediate
8103d4c4710SBarry Smith 
811a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
8123d4c4710SBarry Smith 
8133d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
8143d4c4710SBarry Smith 
81558ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
8163d4c4710SBarry Smith @*/
8177087cfbeSBarry Smith PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
8183d4c4710SBarry Smith {
8193d4c4710SBarry Smith   PetscFunctionBegin;
8200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
821c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
8223d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
8233d4c4710SBarry Smith   PetscFunctionReturn(0);
8243d4c4710SBarry Smith }
8253d4c4710SBarry Smith 
8263d4c4710SBarry Smith #undef __FUNCT__
8273d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
8283d4c4710SBarry Smith /*@
8293d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
8303d4c4710SBarry Smith      are allowed before SNES terminates
8313d4c4710SBarry Smith 
8323d4c4710SBarry Smith    Not Collective
8333d4c4710SBarry Smith 
8343d4c4710SBarry Smith    Input Parameter:
8353d4c4710SBarry Smith .  snes     - SNES context
8363d4c4710SBarry Smith 
8373d4c4710SBarry Smith    Output Parameter:
8383d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
8393d4c4710SBarry Smith 
8403d4c4710SBarry Smith    Level: intermediate
8413d4c4710SBarry Smith 
8423d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
8433d4c4710SBarry Smith 
8443d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
8453d4c4710SBarry Smith 
846e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
8473d4c4710SBarry Smith @*/
8487087cfbeSBarry Smith PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
8493d4c4710SBarry Smith {
8503d4c4710SBarry Smith   PetscFunctionBegin;
8510700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8523d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
8533d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
8543d4c4710SBarry Smith   PetscFunctionReturn(0);
8553d4c4710SBarry Smith }
8563d4c4710SBarry Smith 
8573d4c4710SBarry Smith #undef __FUNCT__
858b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
859c96a6f78SLois Curfman McInnes /*@
860b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
861c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
862c96a6f78SLois Curfman McInnes 
863c7afd0dbSLois Curfman McInnes    Not Collective
864c7afd0dbSLois Curfman McInnes 
865c96a6f78SLois Curfman McInnes    Input Parameter:
866c96a6f78SLois Curfman McInnes .  snes - SNES context
867c96a6f78SLois Curfman McInnes 
868c96a6f78SLois Curfman McInnes    Output Parameter:
869c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
870c96a6f78SLois Curfman McInnes 
871c96a6f78SLois Curfman McInnes    Notes:
872c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
873c96a6f78SLois Curfman McInnes 
87436851e7fSLois Curfman McInnes    Level: intermediate
87536851e7fSLois Curfman McInnes 
876c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
8772b668275SBarry Smith 
8788c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
879c96a6f78SLois Curfman McInnes @*/
8807087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
881c96a6f78SLois Curfman McInnes {
8823a40ed3dSBarry Smith   PetscFunctionBegin;
8830700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8844482741eSBarry Smith   PetscValidIntPointer(lits,2);
885c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
8863a40ed3dSBarry Smith   PetscFunctionReturn(0);
887c96a6f78SLois Curfman McInnes }
888c96a6f78SLois Curfman McInnes 
8894a2ae208SSatish Balay #undef __FUNCT__
89094b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
89152baeb72SSatish Balay /*@
89294b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
8939b94acceSBarry Smith 
89494b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
895c7afd0dbSLois Curfman McInnes 
8969b94acceSBarry Smith    Input Parameter:
8979b94acceSBarry Smith .  snes - the SNES context
8989b94acceSBarry Smith 
8999b94acceSBarry Smith    Output Parameter:
90094b7f48cSBarry Smith .  ksp - the KSP context
9019b94acceSBarry Smith 
9029b94acceSBarry Smith    Notes:
90394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
9049b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
9052999313aSBarry Smith    PC contexts as well.
9069b94acceSBarry Smith 
90736851e7fSLois Curfman McInnes    Level: beginner
90836851e7fSLois Curfman McInnes 
90994b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9109b94acceSBarry Smith 
9112999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9129b94acceSBarry Smith @*/
9137087cfbeSBarry Smith PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
9149b94acceSBarry Smith {
9151cee3971SBarry Smith   PetscErrorCode ierr;
9161cee3971SBarry Smith 
9173a40ed3dSBarry Smith   PetscFunctionBegin;
9180700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9194482741eSBarry Smith   PetscValidPointer(ksp,2);
9201cee3971SBarry Smith 
9211cee3971SBarry Smith   if (!snes->ksp) {
9221cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
9231cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
9241cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
9251cee3971SBarry Smith   }
92694b7f48cSBarry Smith   *ksp = snes->ksp;
9273a40ed3dSBarry Smith   PetscFunctionReturn(0);
9289b94acceSBarry Smith }
92982bf6240SBarry Smith 
9304a2ae208SSatish Balay #undef __FUNCT__
9312999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
9322999313aSBarry Smith /*@
9332999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
9342999313aSBarry Smith 
9352999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
9362999313aSBarry Smith 
9372999313aSBarry Smith    Input Parameters:
9382999313aSBarry Smith +  snes - the SNES context
9392999313aSBarry Smith -  ksp - the KSP context
9402999313aSBarry Smith 
9412999313aSBarry Smith    Notes:
9422999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
9432999313aSBarry Smith    so this routine is rarely needed.
9442999313aSBarry Smith 
9452999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
9462999313aSBarry Smith    decreased by one.
9472999313aSBarry Smith 
9482999313aSBarry Smith    Level: developer
9492999313aSBarry Smith 
9502999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9512999313aSBarry Smith 
9522999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9532999313aSBarry Smith @*/
9547087cfbeSBarry Smith PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
9552999313aSBarry Smith {
9562999313aSBarry Smith   PetscErrorCode ierr;
9572999313aSBarry Smith 
9582999313aSBarry Smith   PetscFunctionBegin;
9590700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9600700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
9612999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
9627dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
963906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
9642999313aSBarry Smith   snes->ksp = ksp;
9652999313aSBarry Smith   PetscFunctionReturn(0);
9662999313aSBarry Smith }
9672999313aSBarry Smith 
9687adad957SLisandro Dalcin #if 0
9692999313aSBarry Smith #undef __FUNCT__
9704a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
9716849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
972e24b481bSBarry Smith {
973e24b481bSBarry Smith   PetscFunctionBegin;
974e24b481bSBarry Smith   PetscFunctionReturn(0);
975e24b481bSBarry Smith }
9767adad957SLisandro Dalcin #endif
977e24b481bSBarry Smith 
9789b94acceSBarry Smith /* -----------------------------------------------------------*/
9794a2ae208SSatish Balay #undef __FUNCT__
9804a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
98152baeb72SSatish Balay /*@
9829b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
9839b94acceSBarry Smith 
984c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
985c7afd0dbSLois Curfman McInnes 
986c7afd0dbSLois Curfman McInnes    Input Parameters:
987906ed7ccSBarry Smith .  comm - MPI communicator
9889b94acceSBarry Smith 
9899b94acceSBarry Smith    Output Parameter:
9909b94acceSBarry Smith .  outsnes - the new SNES context
9919b94acceSBarry Smith 
992c7afd0dbSLois Curfman McInnes    Options Database Keys:
993c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
994c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
995c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
996c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
997c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
998c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
999c1f60f51SBarry Smith 
100036851e7fSLois Curfman McInnes    Level: beginner
100136851e7fSLois Curfman McInnes 
10029b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
10039b94acceSBarry Smith 
1004a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
1005a8054027SBarry Smith 
10069b94acceSBarry Smith @*/
10077087cfbeSBarry Smith PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
10089b94acceSBarry Smith {
1009dfbe8321SBarry Smith   PetscErrorCode      ierr;
10109b94acceSBarry Smith   SNES                snes;
1011fa9f3622SBarry Smith   SNESKSPEW           *kctx;
101237fcc0dbSBarry Smith 
10133a40ed3dSBarry Smith   PetscFunctionBegin;
1014ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
10158ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
10168ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
10178ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
10188ba1e511SMatthew Knepley #endif
10198ba1e511SMatthew Knepley 
10200700a824SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
10217adad957SLisandro Dalcin 
102285385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
10239b94acceSBarry Smith   snes->max_its           = 50;
10249750a799SBarry Smith   snes->max_funcs	  = 10000;
10259b94acceSBarry Smith   snes->norm		  = 0.0;
1026b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
1027b4874afaSBarry Smith   snes->ttol              = 0.0;
102870441072SBarry Smith   snes->abstol		  = 1.e-50;
10299b94acceSBarry Smith   snes->xtol		  = 1.e-8;
10304b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
10319b94acceSBarry Smith   snes->nfuncs            = 0;
103250ffb88aSMatthew Knepley   snes->numFailures       = 0;
103350ffb88aSMatthew Knepley   snes->maxFailures       = 1;
10347a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1035e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1036a8054027SBarry Smith   snes->lagpreconditioner = 1;
1037639f9d9dSBarry Smith   snes->numbermonitors    = 0;
10389b94acceSBarry Smith   snes->data              = 0;
10394dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1040186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
10416f24a144SLois Curfman McInnes   snes->nwork             = 0;
104258c9b817SLisandro Dalcin   snes->work              = 0;
104358c9b817SLisandro Dalcin   snes->nvwork            = 0;
104458c9b817SLisandro Dalcin   snes->vwork             = 0;
1045758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1046758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1047758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1048758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1049758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1050184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
10519b94acceSBarry Smith 
10523d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
10533d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
10543d4c4710SBarry Smith 
10559b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
105638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
10579b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
10589b94acceSBarry Smith   kctx->version     = 2;
10599b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
10609b94acceSBarry Smith                              this was too large for some test cases */
106175567043SBarry Smith   kctx->rtol_last   = 0.0;
10629b94acceSBarry Smith   kctx->rtol_max    = .9;
10639b94acceSBarry Smith   kctx->gamma       = 1.0;
106471f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
106571f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
10669b94acceSBarry Smith   kctx->threshold   = .1;
106775567043SBarry Smith   kctx->lresid_last = 0.0;
106875567043SBarry Smith   kctx->norm_last   = 0.0;
10699b94acceSBarry Smith 
10709b94acceSBarry Smith   *outsnes = snes;
10713a40ed3dSBarry Smith   PetscFunctionReturn(0);
10729b94acceSBarry Smith }
10739b94acceSBarry Smith 
10744a2ae208SSatish Balay #undef __FUNCT__
10754a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
10769b94acceSBarry Smith /*@C
10779b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
10789b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
10799b94acceSBarry Smith    equations.
10809b94acceSBarry Smith 
10813f9fe445SBarry Smith    Logically Collective on SNES
1082fee21e36SBarry Smith 
1083c7afd0dbSLois Curfman McInnes    Input Parameters:
1084c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1085c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1086de044059SHong Zhang .  func - function evaluation routine
1087c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1088c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
10899b94acceSBarry Smith 
1090c7afd0dbSLois Curfman McInnes    Calling sequence of func:
10918d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1092c7afd0dbSLois Curfman McInnes 
1093313e4042SLois Curfman McInnes .  f - function vector
1094c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
10959b94acceSBarry Smith 
10969b94acceSBarry Smith    Notes:
10979b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
10989b94acceSBarry Smith $      f'(x) x = -f(x),
1099c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
11009b94acceSBarry Smith 
110136851e7fSLois Curfman McInnes    Level: beginner
110236851e7fSLois Curfman McInnes 
11039b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
11049b94acceSBarry Smith 
1105a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
11069b94acceSBarry Smith @*/
11077087cfbeSBarry Smith PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
11089b94acceSBarry Smith {
110985385478SLisandro Dalcin   PetscErrorCode ierr;
11103a40ed3dSBarry Smith   PetscFunctionBegin;
11110700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1112d2a683ecSLisandro Dalcin   if (r) {
1113d2a683ecSLisandro Dalcin     PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1114d2a683ecSLisandro Dalcin     PetscCheckSameComm(snes,1,r,2);
111585385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
11166bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
111785385478SLisandro Dalcin     snes->vec_func = r;
1118d2a683ecSLisandro Dalcin   } else if (!snes->vec_func && snes->dm) {
1119d2a683ecSLisandro Dalcin     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1120d2a683ecSLisandro Dalcin   }
1121d2a683ecSLisandro Dalcin   if (func) snes->ops->computefunction = func;
1122d2a683ecSLisandro Dalcin   if (ctx)  snes->funP                 = ctx;
11233a40ed3dSBarry Smith   PetscFunctionReturn(0);
11249b94acceSBarry Smith }
11259b94acceSBarry Smith 
1126d25893d9SBarry Smith #undef __FUNCT__
1127d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess"
1128d25893d9SBarry Smith /*@C
1129d25893d9SBarry Smith    SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem
1130d25893d9SBarry Smith 
1131d25893d9SBarry Smith    Logically Collective on SNES
1132d25893d9SBarry Smith 
1133d25893d9SBarry Smith    Input Parameters:
1134d25893d9SBarry Smith +  snes - the SNES context
1135d25893d9SBarry Smith .  func - function evaluation routine
1136d25893d9SBarry Smith -  ctx - [optional] user-defined context for private data for the
1137d25893d9SBarry Smith          function evaluation routine (may be PETSC_NULL)
1138d25893d9SBarry Smith 
1139d25893d9SBarry Smith    Calling sequence of func:
1140d25893d9SBarry Smith $    func (SNES snes,Vec x,void *ctx);
1141d25893d9SBarry Smith 
1142d25893d9SBarry Smith .  f - function vector
1143d25893d9SBarry Smith -  ctx - optional user-defined function context
1144d25893d9SBarry Smith 
1145d25893d9SBarry Smith    Level: intermediate
1146d25893d9SBarry Smith 
1147d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function
1148d25893d9SBarry Smith 
1149d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
1150d25893d9SBarry Smith @*/
1151d25893d9SBarry Smith PetscErrorCode  SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx)
1152d25893d9SBarry Smith {
1153d25893d9SBarry Smith   PetscFunctionBegin;
1154d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1155d25893d9SBarry Smith   if (func) snes->ops->computeinitialguess = func;
1156d25893d9SBarry Smith   if (ctx)  snes->initialguessP            = ctx;
1157d25893d9SBarry Smith   PetscFunctionReturn(0);
1158d25893d9SBarry Smith }
1159d25893d9SBarry Smith 
11603ab0aad5SBarry Smith /* --------------------------------------------------------------- */
11613ab0aad5SBarry Smith #undef __FUNCT__
11621096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
11631096aae1SMatthew Knepley /*@C
11641096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
11651096aae1SMatthew Knepley    it assumes a zero right hand side.
11661096aae1SMatthew Knepley 
11673f9fe445SBarry Smith    Logically Collective on SNES
11681096aae1SMatthew Knepley 
11691096aae1SMatthew Knepley    Input Parameter:
11701096aae1SMatthew Knepley .  snes - the SNES context
11711096aae1SMatthew Knepley 
11721096aae1SMatthew Knepley    Output Parameter:
1173bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
11741096aae1SMatthew Knepley 
11751096aae1SMatthew Knepley    Level: intermediate
11761096aae1SMatthew Knepley 
11771096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
11781096aae1SMatthew Knepley 
117985385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
11801096aae1SMatthew Knepley @*/
11817087cfbeSBarry Smith PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
11821096aae1SMatthew Knepley {
11831096aae1SMatthew Knepley   PetscFunctionBegin;
11840700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11851096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
118685385478SLisandro Dalcin   *rhs = snes->vec_rhs;
11871096aae1SMatthew Knepley   PetscFunctionReturn(0);
11881096aae1SMatthew Knepley }
11891096aae1SMatthew Knepley 
11901096aae1SMatthew Knepley #undef __FUNCT__
11914a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
11929b94acceSBarry Smith /*@
119336851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
11949b94acceSBarry Smith                          SNESSetFunction().
11959b94acceSBarry Smith 
1196c7afd0dbSLois Curfman McInnes    Collective on SNES
1197c7afd0dbSLois Curfman McInnes 
11989b94acceSBarry Smith    Input Parameters:
1199c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1200c7afd0dbSLois Curfman McInnes -  x - input vector
12019b94acceSBarry Smith 
12029b94acceSBarry Smith    Output Parameter:
12033638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
12049b94acceSBarry Smith 
12051bffabb2SLois Curfman McInnes    Notes:
120636851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
120736851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
120836851e7fSLois Curfman McInnes    themselves.
120936851e7fSLois Curfman McInnes 
121036851e7fSLois Curfman McInnes    Level: developer
121136851e7fSLois Curfman McInnes 
12129b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
12139b94acceSBarry Smith 
1214a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
12159b94acceSBarry Smith @*/
12167087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
12179b94acceSBarry Smith {
1218dfbe8321SBarry Smith   PetscErrorCode ierr;
12199b94acceSBarry Smith 
12203a40ed3dSBarry Smith   PetscFunctionBegin;
12210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12220700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
12230700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1224c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1225c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1226184914b5SBarry Smith 
1227d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1228e7788613SBarry Smith   if (snes->ops->computefunction) {
1229d64ed03dSBarry Smith     PetscStackPush("SNES user function");
123039d508bbSBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1231d64ed03dSBarry Smith     PetscStackPop;
123285385478SLisandro Dalcin   } else if (snes->vec_rhs) {
12331096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
123417186662SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
123585385478SLisandro Dalcin   if (snes->vec_rhs) {
123685385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
12373ab0aad5SBarry Smith   }
1238ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1239d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
12403a40ed3dSBarry Smith   PetscFunctionReturn(0);
12419b94acceSBarry Smith }
12429b94acceSBarry Smith 
12434a2ae208SSatish Balay #undef __FUNCT__
12444a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
124562fef451SLois Curfman McInnes /*@
124662fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
124762fef451SLois Curfman McInnes    set with SNESSetJacobian().
124862fef451SLois Curfman McInnes 
1249c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1250c7afd0dbSLois Curfman McInnes 
125162fef451SLois Curfman McInnes    Input Parameters:
1252c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1253c7afd0dbSLois Curfman McInnes -  x - input vector
125462fef451SLois Curfman McInnes 
125562fef451SLois Curfman McInnes    Output Parameters:
1256c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
125762fef451SLois Curfman McInnes .  B - optional preconditioning matrix
12582b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1259fee21e36SBarry Smith 
1260e35cf81dSBarry Smith   Options Database Keys:
1261e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1262e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1263e35cf81dSBarry Smith 
126462fef451SLois Curfman McInnes    Notes:
126562fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
126662fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
126762fef451SLois Curfman McInnes 
126894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1269dc5a77f8SLois Curfman McInnes    flag parameter.
127062fef451SLois Curfman McInnes 
127136851e7fSLois Curfman McInnes    Level: developer
127236851e7fSLois Curfman McInnes 
127362fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
127462fef451SLois Curfman McInnes 
1275e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
127662fef451SLois Curfman McInnes @*/
12777087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
12789b94acceSBarry Smith {
1279dfbe8321SBarry Smith   PetscErrorCode ierr;
1280ace3abfcSBarry Smith   PetscBool      flag;
12813a40ed3dSBarry Smith 
12823a40ed3dSBarry Smith   PetscFunctionBegin;
12830700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12840700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
12854482741eSBarry Smith   PetscValidPointer(flg,5);
1286c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1287e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1288ebd3b9afSBarry Smith 
1289ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1290ebd3b9afSBarry Smith 
1291fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1292fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1293fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1294fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1295e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1296e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1297ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1298ebd3b9afSBarry Smith     if (flag) {
1299ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1300ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1301ebd3b9afSBarry Smith     }
1302e35cf81dSBarry Smith     PetscFunctionReturn(0);
1303e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1304e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1305e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1306ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1307ebd3b9afSBarry Smith     if (flag) {
1308ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1309ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1310ebd3b9afSBarry Smith     }
1311e35cf81dSBarry Smith     PetscFunctionReturn(0);
1312e35cf81dSBarry Smith   }
1313e35cf81dSBarry Smith 
1314c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1315e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1316d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1317e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1318d64ed03dSBarry Smith   PetscStackPop;
1319d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1320a8054027SBarry Smith 
13213b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
13223b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
13233b4f5425SBarry Smith     snes->lagpreconditioner = -1;
13243b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1325a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1326a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1327a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1328a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1329a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1330a8054027SBarry Smith   }
1331a8054027SBarry Smith 
13326d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
13330700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
13340700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
13353a40ed3dSBarry Smith   PetscFunctionReturn(0);
13369b94acceSBarry Smith }
13379b94acceSBarry Smith 
13384a2ae208SSatish Balay #undef __FUNCT__
13394a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
13409b94acceSBarry Smith /*@C
13419b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1342044dda88SLois Curfman McInnes    location to store the matrix.
13439b94acceSBarry Smith 
13443f9fe445SBarry Smith    Logically Collective on SNES and Mat
1345c7afd0dbSLois Curfman McInnes 
13469b94acceSBarry Smith    Input Parameters:
1347c7afd0dbSLois Curfman McInnes +  snes - the SNES context
13489b94acceSBarry Smith .  A - Jacobian matrix
13499b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
1350efd51863SBarry Smith .  func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value)
1351c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1352efd51863SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value)
13539b94acceSBarry Smith 
13549b94acceSBarry Smith    Calling sequence of func:
13558d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
13569b94acceSBarry Smith 
1357c7afd0dbSLois Curfman McInnes +  x - input vector
13589b94acceSBarry Smith .  A - Jacobian matrix
13599b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1360ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
13612b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1362c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
13639b94acceSBarry Smith 
13649b94acceSBarry Smith    Notes:
136594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
13662cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1367ac21db08SLois Curfman McInnes 
1368ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
13699b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
13709b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
13719b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
13729b94acceSBarry Smith    throughout the global iterations.
13739b94acceSBarry Smith 
137416913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
137516913363SBarry Smith    each matrix.
137616913363SBarry Smith 
1377a8a26c1eSJed Brown    If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument
1378a8a26c1eSJed Brown    must be a MatFDColoring.
1379a8a26c1eSJed Brown 
138036851e7fSLois Curfman McInnes    Level: beginner
138136851e7fSLois Curfman McInnes 
13829b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
13839b94acceSBarry Smith 
13843ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
13859b94acceSBarry Smith @*/
13867087cfbeSBarry Smith PetscErrorCode  SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
13879b94acceSBarry Smith {
1388dfbe8321SBarry Smith   PetscErrorCode ierr;
13893a7fca6bSBarry Smith 
13903a40ed3dSBarry Smith   PetscFunctionBegin;
13910700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13920700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
13930700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1394c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
139506975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1396e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
13973a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
13983a7fca6bSBarry Smith   if (A) {
13997dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
14006bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
14019b94acceSBarry Smith     snes->jacobian = A;
14023a7fca6bSBarry Smith   }
14033a7fca6bSBarry Smith   if (B) {
14047dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
14056bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
14069b94acceSBarry Smith     snes->jacobian_pre = B;
14073a7fca6bSBarry Smith   }
14083a40ed3dSBarry Smith   PetscFunctionReturn(0);
14099b94acceSBarry Smith }
141062fef451SLois Curfman McInnes 
14114a2ae208SSatish Balay #undef __FUNCT__
14124a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1413c2aafc4cSSatish Balay /*@C
1414b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1415b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1416b4fd4287SBarry Smith 
1417c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1418c7afd0dbSLois Curfman McInnes 
1419b4fd4287SBarry Smith    Input Parameter:
1420b4fd4287SBarry Smith .  snes - the nonlinear solver context
1421b4fd4287SBarry Smith 
1422b4fd4287SBarry Smith    Output Parameters:
1423c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1424b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
142570e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
142670e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1427fee21e36SBarry Smith 
142836851e7fSLois Curfman McInnes    Level: advanced
142936851e7fSLois Curfman McInnes 
1430b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1431b4fd4287SBarry Smith @*/
14327087cfbeSBarry Smith PetscErrorCode  SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1433b4fd4287SBarry Smith {
14343a40ed3dSBarry Smith   PetscFunctionBegin;
14350700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1436b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1437b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1438e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
143970e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
14403a40ed3dSBarry Smith   PetscFunctionReturn(0);
1441b4fd4287SBarry Smith }
1442b4fd4287SBarry Smith 
14439b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
14449b94acceSBarry Smith 
14454a2ae208SSatish Balay #undef __FUNCT__
14464a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
14479b94acceSBarry Smith /*@
14489b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1449272ac6f2SLois Curfman McInnes    of a nonlinear solver.
14509b94acceSBarry Smith 
1451fee21e36SBarry Smith    Collective on SNES
1452fee21e36SBarry Smith 
1453c7afd0dbSLois Curfman McInnes    Input Parameters:
145470e92668SMatthew Knepley .  snes - the SNES context
1455c7afd0dbSLois Curfman McInnes 
1456272ac6f2SLois Curfman McInnes    Notes:
1457272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1458272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1459272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1460272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1461272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1462272ac6f2SLois Curfman McInnes 
146336851e7fSLois Curfman McInnes    Level: advanced
146436851e7fSLois Curfman McInnes 
14659b94acceSBarry Smith .keywords: SNES, nonlinear, setup
14669b94acceSBarry Smith 
14679b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
14689b94acceSBarry Smith @*/
14697087cfbeSBarry Smith PetscErrorCode  SNESSetUp(SNES snes)
14709b94acceSBarry Smith {
1471dfbe8321SBarry Smith   PetscErrorCode ierr;
14723a40ed3dSBarry Smith 
14733a40ed3dSBarry Smith   PetscFunctionBegin;
14740700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14754dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
14769b94acceSBarry Smith 
14777adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
147885385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
147985385478SLisandro Dalcin   }
148085385478SLisandro Dalcin 
1481efd51863SBarry Smith   if (!snes->vec_func && snes->dm && !snes->vec_rhs) {
1482efd51863SBarry Smith     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1483efd51863SBarry Smith   }
148417186662SBarry Smith   if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
148517186662SBarry Smith   if (!snes->ops->computefunction && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
148617186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
148758c9b817SLisandro Dalcin   if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
148858c9b817SLisandro Dalcin 
148958c9b817SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
149058c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_rhs, &snes->vec_func);CHKERRQ(ierr);
149158c9b817SLisandro Dalcin   }
149258c9b817SLisandro Dalcin   if (!snes->vec_sol_update /* && snes->vec_sol */) {
149358c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr);
149458c9b817SLisandro Dalcin     ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr);
149558c9b817SLisandro Dalcin   }
149658c9b817SLisandro Dalcin 
1497ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1498ef8dffc7SBarry Smith     Mat           J;
1499ef8dffc7SBarry Smith     ISColoring    coloring;
1500ef8dffc7SBarry Smith     MatFDColoring fd;
1501a703fe33SLois Curfman McInnes 
1502ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1503ef8dffc7SBarry Smith     ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
1504ef8dffc7SBarry Smith     ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr);
1505ef8dffc7SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr);
1506ef8dffc7SBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr);
15076bf464f9SBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
1508efd51863SBarry Smith   } else if (snes->dm && !snes->jacobian_pre){
1509efd51863SBarry Smith     Mat J;
1510efd51863SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1511efd51863SBarry Smith     ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1512efd51863SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1513ef8dffc7SBarry Smith   }
1514efd51863SBarry Smith 
1515b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1516b710008aSBarry Smith 
1517d25893d9SBarry Smith   if (snes->ops->usercompute && !snes->user) {
1518d25893d9SBarry Smith     ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr);
1519d25893d9SBarry Smith   }
1520d25893d9SBarry Smith 
1521410397dcSLisandro Dalcin   if (snes->ops->setup) {
1522410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1523410397dcSLisandro Dalcin   }
152458c9b817SLisandro Dalcin 
15257aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
15263a40ed3dSBarry Smith   PetscFunctionReturn(0);
15279b94acceSBarry Smith }
15289b94acceSBarry Smith 
15294a2ae208SSatish Balay #undef __FUNCT__
153037596af1SLisandro Dalcin #define __FUNCT__ "SNESReset"
153137596af1SLisandro Dalcin /*@
153237596af1SLisandro Dalcin    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats
153337596af1SLisandro Dalcin 
153437596af1SLisandro Dalcin    Collective on SNES
153537596af1SLisandro Dalcin 
153637596af1SLisandro Dalcin    Input Parameter:
153737596af1SLisandro Dalcin .  snes - iterative context obtained from SNESCreate()
153837596af1SLisandro Dalcin 
1539d25893d9SBarry Smith    Level: intermediate
1540d25893d9SBarry Smith 
1541d25893d9SBarry Smith    Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext()
154237596af1SLisandro Dalcin 
154337596af1SLisandro Dalcin .keywords: SNES, destroy
154437596af1SLisandro Dalcin 
154537596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
154637596af1SLisandro Dalcin @*/
154737596af1SLisandro Dalcin PetscErrorCode  SNESReset(SNES snes)
154837596af1SLisandro Dalcin {
154937596af1SLisandro Dalcin   PetscErrorCode ierr;
155037596af1SLisandro Dalcin 
155137596af1SLisandro Dalcin   PetscFunctionBegin;
155237596af1SLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1553d25893d9SBarry Smith   if (snes->ops->userdestroy && snes->user) {
1554d25893d9SBarry Smith     ierr       = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr);
1555d25893d9SBarry Smith     snes->user = PETSC_NULL;
1556d25893d9SBarry Smith   }
1557d25893d9SBarry Smith 
155837596af1SLisandro Dalcin   if (snes->ops->reset) {
155937596af1SLisandro Dalcin     ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr);
156037596af1SLisandro Dalcin   }
156137596af1SLisandro Dalcin   if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);}
15626bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
15636bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
15646bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr);
15656bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
15666bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
15676bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
156837596af1SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
156937596af1SLisandro Dalcin   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
157037596af1SLisandro Dalcin   snes->nwork = snes->nvwork = 0;
157137596af1SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
157237596af1SLisandro Dalcin   PetscFunctionReturn(0);
157337596af1SLisandro Dalcin }
157437596af1SLisandro Dalcin 
157537596af1SLisandro Dalcin #undef __FUNCT__
15764a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
157752baeb72SSatish Balay /*@
15789b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
15799b94acceSBarry Smith    with SNESCreate().
15809b94acceSBarry Smith 
1581c7afd0dbSLois Curfman McInnes    Collective on SNES
1582c7afd0dbSLois Curfman McInnes 
15839b94acceSBarry Smith    Input Parameter:
15849b94acceSBarry Smith .  snes - the SNES context
15859b94acceSBarry Smith 
158636851e7fSLois Curfman McInnes    Level: beginner
158736851e7fSLois Curfman McInnes 
15889b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
15899b94acceSBarry Smith 
159063a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
15919b94acceSBarry Smith @*/
15926bf464f9SBarry Smith PetscErrorCode  SNESDestroy(SNES *snes)
15939b94acceSBarry Smith {
15946849ba73SBarry Smith   PetscErrorCode ierr;
15953a40ed3dSBarry Smith 
15963a40ed3dSBarry Smith   PetscFunctionBegin;
15976bf464f9SBarry Smith   if (!*snes) PetscFunctionReturn(0);
15986bf464f9SBarry Smith   PetscValidHeaderSpecific((*snes),SNES_CLASSID,1);
15996bf464f9SBarry Smith   if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);}
1600d4bb536fSBarry Smith 
16016bf464f9SBarry Smith   ierr = SNESReset((*snes));CHKERRQ(ierr);
16026b8b9a38SLisandro Dalcin 
1603be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
16046bf464f9SBarry Smith   ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr);
16056bf464f9SBarry Smith   if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);}
16066d4c513bSLisandro Dalcin 
16076bf464f9SBarry Smith   ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr);
16086bf464f9SBarry Smith   ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr);
16096b8b9a38SLisandro Dalcin 
16106bf464f9SBarry Smith   ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr);
16116bf464f9SBarry Smith   if ((*snes)->ops->convergeddestroy) {
16126bf464f9SBarry Smith     ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr);
16136b8b9a38SLisandro Dalcin   }
16146bf464f9SBarry Smith   if ((*snes)->conv_malloc) {
16156bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr);
16166bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr);
161758c9b817SLisandro Dalcin   }
16186bf464f9SBarry Smith   ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr);
1619a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
16203a40ed3dSBarry Smith   PetscFunctionReturn(0);
16219b94acceSBarry Smith }
16229b94acceSBarry Smith 
16239b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
16249b94acceSBarry Smith 
16254a2ae208SSatish Balay #undef __FUNCT__
1626a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1627a8054027SBarry Smith /*@
1628a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1629a8054027SBarry Smith 
16303f9fe445SBarry Smith    Logically Collective on SNES
1631a8054027SBarry Smith 
1632a8054027SBarry Smith    Input Parameters:
1633a8054027SBarry Smith +  snes - the SNES context
1634a8054027SBarry 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
16353b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1636a8054027SBarry Smith 
1637a8054027SBarry Smith    Options Database Keys:
1638a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1639a8054027SBarry Smith 
1640a8054027SBarry Smith    Notes:
1641a8054027SBarry Smith    The default is 1
1642a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1643a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1644a8054027SBarry Smith 
1645a8054027SBarry Smith    Level: intermediate
1646a8054027SBarry Smith 
1647a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1648a8054027SBarry Smith 
1649e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1650a8054027SBarry Smith 
1651a8054027SBarry Smith @*/
16527087cfbeSBarry Smith PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1653a8054027SBarry Smith {
1654a8054027SBarry Smith   PetscFunctionBegin;
16550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1656e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1657e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1658c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1659a8054027SBarry Smith   snes->lagpreconditioner = lag;
1660a8054027SBarry Smith   PetscFunctionReturn(0);
1661a8054027SBarry Smith }
1662a8054027SBarry Smith 
1663a8054027SBarry Smith #undef __FUNCT__
1664efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence"
1665efd51863SBarry Smith /*@
1666efd51863SBarry Smith    SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does
1667efd51863SBarry Smith 
1668efd51863SBarry Smith    Logically Collective on SNES
1669efd51863SBarry Smith 
1670efd51863SBarry Smith    Input Parameters:
1671efd51863SBarry Smith +  snes - the SNES context
1672efd51863SBarry Smith -  steps - the number of refinements to do, defaults to 0
1673efd51863SBarry Smith 
1674efd51863SBarry Smith    Options Database Keys:
1675efd51863SBarry Smith .    -snes_grid_sequence <steps>
1676efd51863SBarry Smith 
1677efd51863SBarry Smith    Level: intermediate
1678efd51863SBarry Smith 
1679efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1680efd51863SBarry Smith 
1681efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1682efd51863SBarry Smith 
1683efd51863SBarry Smith @*/
1684efd51863SBarry Smith PetscErrorCode  SNESSetGridSequence(SNES snes,PetscInt steps)
1685efd51863SBarry Smith {
1686efd51863SBarry Smith   PetscFunctionBegin;
1687efd51863SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1688efd51863SBarry Smith   PetscValidLogicalCollectiveInt(snes,steps,2);
1689efd51863SBarry Smith   snes->gridsequence = steps;
1690efd51863SBarry Smith   PetscFunctionReturn(0);
1691efd51863SBarry Smith }
1692efd51863SBarry Smith 
1693efd51863SBarry Smith #undef __FUNCT__
1694a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1695a8054027SBarry Smith /*@
1696a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1697a8054027SBarry Smith 
16983f9fe445SBarry Smith    Not Collective
1699a8054027SBarry Smith 
1700a8054027SBarry Smith    Input Parameter:
1701a8054027SBarry Smith .  snes - the SNES context
1702a8054027SBarry Smith 
1703a8054027SBarry Smith    Output Parameter:
1704a8054027SBarry 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
17053b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1706a8054027SBarry Smith 
1707a8054027SBarry Smith    Options Database Keys:
1708a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1709a8054027SBarry Smith 
1710a8054027SBarry Smith    Notes:
1711a8054027SBarry Smith    The default is 1
1712a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1713a8054027SBarry Smith 
1714a8054027SBarry Smith    Level: intermediate
1715a8054027SBarry Smith 
1716a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1717a8054027SBarry Smith 
1718a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1719a8054027SBarry Smith 
1720a8054027SBarry Smith @*/
17217087cfbeSBarry Smith PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1722a8054027SBarry Smith {
1723a8054027SBarry Smith   PetscFunctionBegin;
17240700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1725a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1726a8054027SBarry Smith   PetscFunctionReturn(0);
1727a8054027SBarry Smith }
1728a8054027SBarry Smith 
1729a8054027SBarry Smith #undef __FUNCT__
1730e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1731e35cf81dSBarry Smith /*@
1732e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1733e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1734e35cf81dSBarry Smith 
17353f9fe445SBarry Smith    Logically Collective on SNES
1736e35cf81dSBarry Smith 
1737e35cf81dSBarry Smith    Input Parameters:
1738e35cf81dSBarry Smith +  snes - the SNES context
1739e35cf81dSBarry 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
1740fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1741e35cf81dSBarry Smith 
1742e35cf81dSBarry Smith    Options Database Keys:
1743e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1744e35cf81dSBarry Smith 
1745e35cf81dSBarry Smith    Notes:
1746e35cf81dSBarry Smith    The default is 1
1747e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1748fe3ffe1eSBarry 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
1749fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1750e35cf81dSBarry Smith 
1751e35cf81dSBarry Smith    Level: intermediate
1752e35cf81dSBarry Smith 
1753e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1754e35cf81dSBarry Smith 
1755e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1756e35cf81dSBarry Smith 
1757e35cf81dSBarry Smith @*/
17587087cfbeSBarry Smith PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
1759e35cf81dSBarry Smith {
1760e35cf81dSBarry Smith   PetscFunctionBegin;
17610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1762e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1763e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1764c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1765e35cf81dSBarry Smith   snes->lagjacobian = lag;
1766e35cf81dSBarry Smith   PetscFunctionReturn(0);
1767e35cf81dSBarry Smith }
1768e35cf81dSBarry Smith 
1769e35cf81dSBarry Smith #undef __FUNCT__
1770e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1771e35cf81dSBarry Smith /*@
1772e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1773e35cf81dSBarry Smith 
17743f9fe445SBarry Smith    Not Collective
1775e35cf81dSBarry Smith 
1776e35cf81dSBarry Smith    Input Parameter:
1777e35cf81dSBarry Smith .  snes - the SNES context
1778e35cf81dSBarry Smith 
1779e35cf81dSBarry Smith    Output Parameter:
1780e35cf81dSBarry 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
1781e35cf81dSBarry Smith          the Jacobian is built etc.
1782e35cf81dSBarry Smith 
1783e35cf81dSBarry Smith    Options Database Keys:
1784e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1785e35cf81dSBarry Smith 
1786e35cf81dSBarry Smith    Notes:
1787e35cf81dSBarry Smith    The default is 1
1788e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1789e35cf81dSBarry Smith 
1790e35cf81dSBarry Smith    Level: intermediate
1791e35cf81dSBarry Smith 
1792e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1793e35cf81dSBarry Smith 
1794e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1795e35cf81dSBarry Smith 
1796e35cf81dSBarry Smith @*/
17977087cfbeSBarry Smith PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
1798e35cf81dSBarry Smith {
1799e35cf81dSBarry Smith   PetscFunctionBegin;
18000700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1801e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1802e35cf81dSBarry Smith   PetscFunctionReturn(0);
1803e35cf81dSBarry Smith }
1804e35cf81dSBarry Smith 
1805e35cf81dSBarry Smith #undef __FUNCT__
18064a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
18079b94acceSBarry Smith /*@
1808d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
18099b94acceSBarry Smith 
18103f9fe445SBarry Smith    Logically Collective on SNES
1811c7afd0dbSLois Curfman McInnes 
18129b94acceSBarry Smith    Input Parameters:
1813c7afd0dbSLois Curfman McInnes +  snes - the SNES context
181470441072SBarry Smith .  abstol - absolute convergence tolerance
181533174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
181633174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
181733174efeSLois Curfman McInnes            of the change in the solution between steps
181833174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1819c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1820fee21e36SBarry Smith 
182133174efeSLois Curfman McInnes    Options Database Keys:
182270441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1823c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1824c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1825c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1826c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
18279b94acceSBarry Smith 
1828d7a720efSLois Curfman McInnes    Notes:
18299b94acceSBarry Smith    The default maximum number of iterations is 50.
18309b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
18319b94acceSBarry Smith 
183236851e7fSLois Curfman McInnes    Level: intermediate
183336851e7fSLois Curfman McInnes 
183433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
18359b94acceSBarry Smith 
18362492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
18379b94acceSBarry Smith @*/
18387087cfbeSBarry Smith PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
18399b94acceSBarry Smith {
18403a40ed3dSBarry Smith   PetscFunctionBegin;
18410700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1842c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
1843c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
1844c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
1845c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
1846c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
1847c5eb9154SBarry Smith 
184870441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1849d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1850d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1851d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1852d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
18533a40ed3dSBarry Smith   PetscFunctionReturn(0);
18549b94acceSBarry Smith }
18559b94acceSBarry Smith 
18564a2ae208SSatish Balay #undef __FUNCT__
18574a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
18589b94acceSBarry Smith /*@
185933174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
186033174efeSLois Curfman McInnes 
1861c7afd0dbSLois Curfman McInnes    Not Collective
1862c7afd0dbSLois Curfman McInnes 
186333174efeSLois Curfman McInnes    Input Parameters:
1864c7afd0dbSLois Curfman McInnes +  snes - the SNES context
186585385478SLisandro Dalcin .  atol - absolute convergence tolerance
186633174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
186733174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
186833174efeSLois Curfman McInnes            of the change in the solution between steps
186933174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1870c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1871fee21e36SBarry Smith 
187233174efeSLois Curfman McInnes    Notes:
187333174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
187433174efeSLois Curfman McInnes 
187536851e7fSLois Curfman McInnes    Level: intermediate
187636851e7fSLois Curfman McInnes 
187733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
187833174efeSLois Curfman McInnes 
187933174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
188033174efeSLois Curfman McInnes @*/
18817087cfbeSBarry Smith PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
188233174efeSLois Curfman McInnes {
18833a40ed3dSBarry Smith   PetscFunctionBegin;
18840700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
188585385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
188633174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
188733174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
188833174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
188933174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
18903a40ed3dSBarry Smith   PetscFunctionReturn(0);
189133174efeSLois Curfman McInnes }
189233174efeSLois Curfman McInnes 
18934a2ae208SSatish Balay #undef __FUNCT__
18944a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
189533174efeSLois Curfman McInnes /*@
18969b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
18979b94acceSBarry Smith 
18983f9fe445SBarry Smith    Logically Collective on SNES
1899fee21e36SBarry Smith 
1900c7afd0dbSLois Curfman McInnes    Input Parameters:
1901c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1902c7afd0dbSLois Curfman McInnes -  tol - tolerance
1903c7afd0dbSLois Curfman McInnes 
19049b94acceSBarry Smith    Options Database Key:
1905c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
19069b94acceSBarry Smith 
190736851e7fSLois Curfman McInnes    Level: intermediate
190836851e7fSLois Curfman McInnes 
19099b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
19109b94acceSBarry Smith 
19112492ecdbSBarry Smith .seealso: SNESSetTolerances()
19129b94acceSBarry Smith @*/
19137087cfbeSBarry Smith PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
19149b94acceSBarry Smith {
19153a40ed3dSBarry Smith   PetscFunctionBegin;
19160700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1917c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
19189b94acceSBarry Smith   snes->deltatol = tol;
19193a40ed3dSBarry Smith   PetscFunctionReturn(0);
19209b94acceSBarry Smith }
19219b94acceSBarry Smith 
1922df9fa365SBarry Smith /*
1923df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1924df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1925df9fa365SBarry Smith    macros instead of functions
1926df9fa365SBarry Smith */
19274a2ae208SSatish Balay #undef __FUNCT__
1928a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
19297087cfbeSBarry Smith PetscErrorCode  SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1930ce1608b8SBarry Smith {
1931dfbe8321SBarry Smith   PetscErrorCode ierr;
1932ce1608b8SBarry Smith 
1933ce1608b8SBarry Smith   PetscFunctionBegin;
19340700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1935a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1936ce1608b8SBarry Smith   PetscFunctionReturn(0);
1937ce1608b8SBarry Smith }
1938ce1608b8SBarry Smith 
19394a2ae208SSatish Balay #undef __FUNCT__
1940a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
19417087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1942df9fa365SBarry Smith {
1943dfbe8321SBarry Smith   PetscErrorCode ierr;
1944df9fa365SBarry Smith 
1945df9fa365SBarry Smith   PetscFunctionBegin;
1946a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1947df9fa365SBarry Smith   PetscFunctionReturn(0);
1948df9fa365SBarry Smith }
1949df9fa365SBarry Smith 
19504a2ae208SSatish Balay #undef __FUNCT__
1951a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
19526bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGDestroy(PetscDrawLG *draw)
1953df9fa365SBarry Smith {
1954dfbe8321SBarry Smith   PetscErrorCode ierr;
1955df9fa365SBarry Smith 
1956df9fa365SBarry Smith   PetscFunctionBegin;
1957a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1958df9fa365SBarry Smith   PetscFunctionReturn(0);
1959df9fa365SBarry Smith }
1960df9fa365SBarry Smith 
19617087cfbeSBarry Smith extern PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1962b271bb04SBarry Smith #undef __FUNCT__
1963b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
19647087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1965b271bb04SBarry Smith {
1966b271bb04SBarry Smith   PetscDrawLG      lg;
1967b271bb04SBarry Smith   PetscErrorCode   ierr;
1968b271bb04SBarry Smith   PetscReal        x,y,per;
1969b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1970b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1971b271bb04SBarry Smith   PetscDraw        draw;
1972b271bb04SBarry Smith   PetscFunctionBegin;
1973b271bb04SBarry Smith   if (!monctx) {
1974b271bb04SBarry Smith     MPI_Comm    comm;
1975b271bb04SBarry Smith 
1976b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1977b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1978b271bb04SBarry Smith   }
1979b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1980b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1981b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1982b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1983b271bb04SBarry Smith   x = (PetscReal) n;
1984b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1985b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1986b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1987b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1988b271bb04SBarry Smith   }
1989b271bb04SBarry Smith 
1990b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1991b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1992b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1993b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1994b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1995b271bb04SBarry Smith   x = (PetscReal) n;
1996b271bb04SBarry Smith   y = 100.0*per;
1997b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1998b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1999b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2000b271bb04SBarry Smith   }
2001b271bb04SBarry Smith 
2002b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
2003b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2004b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2005b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
2006b271bb04SBarry Smith   x = (PetscReal) n;
2007b271bb04SBarry Smith   y = (prev - rnorm)/prev;
2008b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2009b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2010b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2011b271bb04SBarry Smith   }
2012b271bb04SBarry Smith 
2013b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
2014b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2015b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2016b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
2017b271bb04SBarry Smith   x = (PetscReal) n;
2018b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
2019b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
2020b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2021b271bb04SBarry Smith   }
2022b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2023b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2024b271bb04SBarry Smith   }
2025b271bb04SBarry Smith   prev = rnorm;
2026b271bb04SBarry Smith   PetscFunctionReturn(0);
2027b271bb04SBarry Smith }
2028b271bb04SBarry Smith 
2029b271bb04SBarry Smith #undef __FUNCT__
2030b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
20317087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2032b271bb04SBarry Smith {
2033b271bb04SBarry Smith   PetscErrorCode ierr;
2034b271bb04SBarry Smith 
2035b271bb04SBarry Smith   PetscFunctionBegin;
2036b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2037b271bb04SBarry Smith   PetscFunctionReturn(0);
2038b271bb04SBarry Smith }
2039b271bb04SBarry Smith 
2040b271bb04SBarry Smith #undef __FUNCT__
2041b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
20426bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGRangeDestroy(PetscDrawLG *draw)
2043b271bb04SBarry Smith {
2044b271bb04SBarry Smith   PetscErrorCode ierr;
2045b271bb04SBarry Smith 
2046b271bb04SBarry Smith   PetscFunctionBegin;
2047b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2048b271bb04SBarry Smith   PetscFunctionReturn(0);
2049b271bb04SBarry Smith }
2050b271bb04SBarry Smith 
20517a03ce2fSLisandro Dalcin #undef __FUNCT__
20527a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor"
20537a03ce2fSLisandro Dalcin /*
20547a03ce2fSLisandro Dalcin      Runs the user provided monitor routines, if they exists.
20557a03ce2fSLisandro Dalcin */
20567a03ce2fSLisandro Dalcin PetscErrorCode  SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm)
20577a03ce2fSLisandro Dalcin {
20587a03ce2fSLisandro Dalcin   PetscErrorCode ierr;
20597a03ce2fSLisandro Dalcin   PetscInt       i,n = snes->numbermonitors;
20607a03ce2fSLisandro Dalcin 
20617a03ce2fSLisandro Dalcin   PetscFunctionBegin;
20627a03ce2fSLisandro Dalcin   for (i=0; i<n; i++) {
20637a03ce2fSLisandro Dalcin     ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr);
20647a03ce2fSLisandro Dalcin   }
20657a03ce2fSLisandro Dalcin   PetscFunctionReturn(0);
20667a03ce2fSLisandro Dalcin }
20677a03ce2fSLisandro Dalcin 
20689b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
20699b94acceSBarry Smith 
20704a2ae208SSatish Balay #undef __FUNCT__
2071a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
20729b94acceSBarry Smith /*@C
2073a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
20749b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
20759b94acceSBarry Smith    progress.
20769b94acceSBarry Smith 
20773f9fe445SBarry Smith    Logically Collective on SNES
2078fee21e36SBarry Smith 
2079c7afd0dbSLois Curfman McInnes    Input Parameters:
2080c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2081c7afd0dbSLois Curfman McInnes .  func - monitoring routine
2082b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
2083e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
2084b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2085b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
20869b94acceSBarry Smith 
2087c7afd0dbSLois Curfman McInnes    Calling sequence of func:
2088a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
2089c7afd0dbSLois Curfman McInnes 
2090c7afd0dbSLois Curfman McInnes +    snes - the SNES context
2091c7afd0dbSLois Curfman McInnes .    its - iteration number
2092c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
209340a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
20949b94acceSBarry Smith 
20959665c990SLois Curfman McInnes    Options Database Keys:
2096a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
2097a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
2098a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
2099cca6129bSJed Brown -    -snes_monitor_cancel - cancels all monitors that have
2100c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
2101a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
2102c7afd0dbSLois Curfman McInnes                             does not cancel those set via
2103c7afd0dbSLois Curfman McInnes                             the options database.
21049665c990SLois Curfman McInnes 
2105639f9d9dSBarry Smith    Notes:
21066bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
2107a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
21086bc08f3fSLois Curfman McInnes    order in which they were set.
2109639f9d9dSBarry Smith 
2110025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
2111025f1a04SBarry Smith 
211236851e7fSLois Curfman McInnes    Level: intermediate
211336851e7fSLois Curfman McInnes 
21149b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
21159b94acceSBarry Smith 
2116a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
21179b94acceSBarry Smith @*/
2118c2efdce3SBarry Smith PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
21199b94acceSBarry Smith {
2120b90d0a6eSBarry Smith   PetscInt i;
2121b90d0a6eSBarry Smith 
21223a40ed3dSBarry Smith   PetscFunctionBegin;
21230700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
212417186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2125b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
2126b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
2127b90d0a6eSBarry Smith 
2128b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
2129b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
2130b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
2131b90d0a6eSBarry Smith         PetscErrorCode          ierr;
2132b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
2133b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
2134b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
2135c2efdce3SBarry Smith           ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr);
2136b90d0a6eSBarry Smith           PetscFunctionReturn(0);
2137b90d0a6eSBarry Smith         }
2138b90d0a6eSBarry Smith       }
2139b90d0a6eSBarry Smith     }
2140b90d0a6eSBarry Smith   }
2141b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
2142b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
2143639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
21443a40ed3dSBarry Smith   PetscFunctionReturn(0);
21459b94acceSBarry Smith }
21469b94acceSBarry Smith 
21474a2ae208SSatish Balay #undef __FUNCT__
2148a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
21495cd90555SBarry Smith /*@C
2150a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
21515cd90555SBarry Smith 
21523f9fe445SBarry Smith    Logically Collective on SNES
2153c7afd0dbSLois Curfman McInnes 
21545cd90555SBarry Smith    Input Parameters:
21555cd90555SBarry Smith .  snes - the SNES context
21565cd90555SBarry Smith 
21571a480d89SAdministrator    Options Database Key:
2158a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
2159a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
2160c7afd0dbSLois Curfman McInnes     set via the options database
21615cd90555SBarry Smith 
21625cd90555SBarry Smith    Notes:
21635cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
21645cd90555SBarry Smith 
216536851e7fSLois Curfman McInnes    Level: intermediate
216636851e7fSLois Curfman McInnes 
21675cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
21685cd90555SBarry Smith 
2169a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
21705cd90555SBarry Smith @*/
21717087cfbeSBarry Smith PetscErrorCode  SNESMonitorCancel(SNES snes)
21725cd90555SBarry Smith {
2173d952e501SBarry Smith   PetscErrorCode ierr;
2174d952e501SBarry Smith   PetscInt       i;
2175d952e501SBarry Smith 
21765cd90555SBarry Smith   PetscFunctionBegin;
21770700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2178d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2179d952e501SBarry Smith     if (snes->monitordestroy[i]) {
21803c4aec1bSBarry Smith       ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr);
2181d952e501SBarry Smith     }
2182d952e501SBarry Smith   }
21835cd90555SBarry Smith   snes->numbermonitors = 0;
21845cd90555SBarry Smith   PetscFunctionReturn(0);
21855cd90555SBarry Smith }
21865cd90555SBarry Smith 
21874a2ae208SSatish Balay #undef __FUNCT__
21884a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
21899b94acceSBarry Smith /*@C
21909b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
21919b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
21929b94acceSBarry Smith 
21933f9fe445SBarry Smith    Logically Collective on SNES
2194fee21e36SBarry Smith 
2195c7afd0dbSLois Curfman McInnes    Input Parameters:
2196c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2197c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
21987f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
21997f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
22009b94acceSBarry Smith 
2201c7afd0dbSLois Curfman McInnes    Calling sequence of func:
220206ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2203c7afd0dbSLois Curfman McInnes 
2204c7afd0dbSLois Curfman McInnes +    snes - the SNES context
220506ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2206c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2207184914b5SBarry Smith .    reason - reason for convergence/divergence
2208c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
22094b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
22104b27c08aSLois Curfman McInnes -    f - 2-norm of function
22119b94acceSBarry Smith 
221236851e7fSLois Curfman McInnes    Level: advanced
221336851e7fSLois Curfman McInnes 
22149b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
22159b94acceSBarry Smith 
221685385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
22179b94acceSBarry Smith @*/
22187087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
22199b94acceSBarry Smith {
22207f7931b9SBarry Smith   PetscErrorCode ierr;
22217f7931b9SBarry Smith 
22223a40ed3dSBarry Smith   PetscFunctionBegin;
22230700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
222485385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
22257f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
22267f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
22277f7931b9SBarry Smith   }
222885385478SLisandro Dalcin   snes->ops->converged        = func;
22297f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
223085385478SLisandro Dalcin   snes->cnvP                  = cctx;
22313a40ed3dSBarry Smith   PetscFunctionReturn(0);
22329b94acceSBarry Smith }
22339b94acceSBarry Smith 
22344a2ae208SSatish Balay #undef __FUNCT__
22354a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
223652baeb72SSatish Balay /*@
2237184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2238184914b5SBarry Smith 
2239184914b5SBarry Smith    Not Collective
2240184914b5SBarry Smith 
2241184914b5SBarry Smith    Input Parameter:
2242184914b5SBarry Smith .  snes - the SNES context
2243184914b5SBarry Smith 
2244184914b5SBarry Smith    Output Parameter:
22454d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2246184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2247184914b5SBarry Smith 
2248184914b5SBarry Smith    Level: intermediate
2249184914b5SBarry Smith 
2250184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2251184914b5SBarry Smith 
2252184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2253184914b5SBarry Smith 
225485385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2255184914b5SBarry Smith @*/
22567087cfbeSBarry Smith PetscErrorCode  SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2257184914b5SBarry Smith {
2258184914b5SBarry Smith   PetscFunctionBegin;
22590700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22604482741eSBarry Smith   PetscValidPointer(reason,2);
2261184914b5SBarry Smith   *reason = snes->reason;
2262184914b5SBarry Smith   PetscFunctionReturn(0);
2263184914b5SBarry Smith }
2264184914b5SBarry Smith 
22654a2ae208SSatish Balay #undef __FUNCT__
22664a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2267c9005455SLois Curfman McInnes /*@
2268c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2269c9005455SLois Curfman McInnes 
22703f9fe445SBarry Smith    Logically Collective on SNES
2271fee21e36SBarry Smith 
2272c7afd0dbSLois Curfman McInnes    Input Parameters:
2273c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
22748c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2275cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2276758f92a0SBarry Smith .  na  - size of a and its
227764731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2278758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2279c7afd0dbSLois Curfman McInnes 
2280308dcc3eSBarry Smith    Notes:
2281308dcc3eSBarry Smith    If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2282308dcc3eSBarry Smith    default array of length 10000 is allocated.
2283308dcc3eSBarry Smith 
2284c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2285c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2286c9005455SLois Curfman McInnes    during the section of code that is being timed.
2287c9005455SLois Curfman McInnes 
228836851e7fSLois Curfman McInnes    Level: intermediate
228936851e7fSLois Curfman McInnes 
2290c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2291758f92a0SBarry Smith 
229208405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2293758f92a0SBarry Smith 
2294c9005455SLois Curfman McInnes @*/
22957087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool  reset)
2296c9005455SLois Curfman McInnes {
2297308dcc3eSBarry Smith   PetscErrorCode ierr;
2298308dcc3eSBarry Smith 
22993a40ed3dSBarry Smith   PetscFunctionBegin;
23000700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23014482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2302a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2303308dcc3eSBarry Smith   if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) {
2304308dcc3eSBarry Smith     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2305308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr);
2306308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr);
2307308dcc3eSBarry Smith     snes->conv_malloc   = PETSC_TRUE;
2308308dcc3eSBarry Smith   }
2309c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2310758f92a0SBarry Smith   snes->conv_hist_its   = its;
2311758f92a0SBarry Smith   snes->conv_hist_max   = na;
2312a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2313758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2314758f92a0SBarry Smith   PetscFunctionReturn(0);
2315758f92a0SBarry Smith }
2316758f92a0SBarry Smith 
2317308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2318c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
2319c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
2320308dcc3eSBarry Smith EXTERN_C_BEGIN
2321308dcc3eSBarry Smith #undef __FUNCT__
2322308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab"
2323308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
2324308dcc3eSBarry Smith {
2325308dcc3eSBarry Smith   mxArray        *mat;
2326308dcc3eSBarry Smith   PetscInt       i;
2327308dcc3eSBarry Smith   PetscReal      *ar;
2328308dcc3eSBarry Smith 
2329308dcc3eSBarry Smith   PetscFunctionBegin;
2330308dcc3eSBarry Smith   mat  = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
2331308dcc3eSBarry Smith   ar   = (PetscReal*) mxGetData(mat);
2332308dcc3eSBarry Smith   for (i=0; i<snes->conv_hist_len; i++) {
2333308dcc3eSBarry Smith     ar[i] = snes->conv_hist[i];
2334308dcc3eSBarry Smith   }
2335308dcc3eSBarry Smith   PetscFunctionReturn(mat);
2336308dcc3eSBarry Smith }
2337308dcc3eSBarry Smith EXTERN_C_END
2338308dcc3eSBarry Smith #endif
2339308dcc3eSBarry Smith 
2340308dcc3eSBarry Smith 
23414a2ae208SSatish Balay #undef __FUNCT__
23424a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
23430c4c9dddSBarry Smith /*@C
2344758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2345758f92a0SBarry Smith 
23463f9fe445SBarry Smith    Not Collective
2347758f92a0SBarry Smith 
2348758f92a0SBarry Smith    Input Parameter:
2349758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2350758f92a0SBarry Smith 
2351758f92a0SBarry Smith    Output Parameters:
2352758f92a0SBarry Smith .  a   - array to hold history
2353758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2354758f92a0SBarry Smith          negative if not converged) for each solve.
2355758f92a0SBarry Smith -  na  - size of a and its
2356758f92a0SBarry Smith 
2357758f92a0SBarry Smith    Notes:
2358758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2359758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2360758f92a0SBarry Smith 
2361758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2362758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2363758f92a0SBarry Smith    during the section of code that is being timed.
2364758f92a0SBarry Smith 
2365758f92a0SBarry Smith    Level: intermediate
2366758f92a0SBarry Smith 
2367758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2368758f92a0SBarry Smith 
2369758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2370758f92a0SBarry Smith 
2371758f92a0SBarry Smith @*/
23727087cfbeSBarry Smith PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2373758f92a0SBarry Smith {
2374758f92a0SBarry Smith   PetscFunctionBegin;
23750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2376758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2377758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2378758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
23793a40ed3dSBarry Smith   PetscFunctionReturn(0);
2380c9005455SLois Curfman McInnes }
2381c9005455SLois Curfman McInnes 
2382e74ef692SMatthew Knepley #undef __FUNCT__
2383e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2384ac226902SBarry Smith /*@C
238576b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
2386eec8f646SJed Brown   at the beginning of every iteration of the nonlinear solve. Specifically
23877e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
238876b2cf59SMatthew Knepley 
23893f9fe445SBarry Smith   Logically Collective on SNES
239076b2cf59SMatthew Knepley 
239176b2cf59SMatthew Knepley   Input Parameters:
239276b2cf59SMatthew Knepley . snes - The nonlinear solver context
239376b2cf59SMatthew Knepley . func - The function
239476b2cf59SMatthew Knepley 
239576b2cf59SMatthew Knepley   Calling sequence of func:
2396b5d30489SBarry Smith . func (SNES snes, PetscInt step);
239776b2cf59SMatthew Knepley 
239876b2cf59SMatthew Knepley . step - The current step of the iteration
239976b2cf59SMatthew Knepley 
2400fe97e370SBarry Smith   Level: advanced
2401fe97e370SBarry Smith 
2402fe97e370SBarry 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()
2403fe97e370SBarry Smith         This is not used by most users.
240476b2cf59SMatthew Knepley 
240576b2cf59SMatthew Knepley .keywords: SNES, update
2406b5d30489SBarry Smith 
240785385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
240876b2cf59SMatthew Knepley @*/
24097087cfbeSBarry Smith PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
241076b2cf59SMatthew Knepley {
241176b2cf59SMatthew Knepley   PetscFunctionBegin;
24120700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2413e7788613SBarry Smith   snes->ops->update = func;
241476b2cf59SMatthew Knepley   PetscFunctionReturn(0);
241576b2cf59SMatthew Knepley }
241676b2cf59SMatthew Knepley 
2417e74ef692SMatthew Knepley #undef __FUNCT__
2418e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
241976b2cf59SMatthew Knepley /*@
242076b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
242176b2cf59SMatthew Knepley 
242276b2cf59SMatthew Knepley   Not collective
242376b2cf59SMatthew Knepley 
242476b2cf59SMatthew Knepley   Input Parameters:
242576b2cf59SMatthew Knepley . snes - The nonlinear solver context
242676b2cf59SMatthew Knepley . step - The current step of the iteration
242776b2cf59SMatthew Knepley 
2428205452f4SMatthew Knepley   Level: intermediate
2429205452f4SMatthew Knepley 
243076b2cf59SMatthew Knepley .keywords: SNES, update
2431a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
243276b2cf59SMatthew Knepley @*/
24337087cfbeSBarry Smith PetscErrorCode  SNESDefaultUpdate(SNES snes, PetscInt step)
243476b2cf59SMatthew Knepley {
243576b2cf59SMatthew Knepley   PetscFunctionBegin;
243676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
243776b2cf59SMatthew Knepley }
243876b2cf59SMatthew Knepley 
24394a2ae208SSatish Balay #undef __FUNCT__
24404a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
24419b94acceSBarry Smith /*
24429b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
24439b94acceSBarry Smith    positive parameter delta.
24449b94acceSBarry Smith 
24459b94acceSBarry Smith     Input Parameters:
2446c7afd0dbSLois Curfman McInnes +   snes - the SNES context
24479b94acceSBarry Smith .   y - approximate solution of linear system
24489b94acceSBarry Smith .   fnorm - 2-norm of current function
2449c7afd0dbSLois Curfman McInnes -   delta - trust region size
24509b94acceSBarry Smith 
24519b94acceSBarry Smith     Output Parameters:
2452c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
24539b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
24549b94acceSBarry Smith     region, and exceeds zero otherwise.
2455c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
24569b94acceSBarry Smith 
24579b94acceSBarry Smith     Note:
24584b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
24599b94acceSBarry Smith     is set to be the maximum allowable step size.
24609b94acceSBarry Smith 
24619b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
24629b94acceSBarry Smith */
2463dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
24649b94acceSBarry Smith {
2465064f8208SBarry Smith   PetscReal      nrm;
2466ea709b57SSatish Balay   PetscScalar    cnorm;
2467dfbe8321SBarry Smith   PetscErrorCode ierr;
24683a40ed3dSBarry Smith 
24693a40ed3dSBarry Smith   PetscFunctionBegin;
24700700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24710700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2472c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2473184914b5SBarry Smith 
2474064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2475064f8208SBarry Smith   if (nrm > *delta) {
2476064f8208SBarry Smith      nrm = *delta/nrm;
2477064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2478064f8208SBarry Smith      cnorm = nrm;
24792dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
24809b94acceSBarry Smith      *ynorm = *delta;
24819b94acceSBarry Smith   } else {
24829b94acceSBarry Smith      *gpnorm = 0.0;
2483064f8208SBarry Smith      *ynorm = nrm;
24849b94acceSBarry Smith   }
24853a40ed3dSBarry Smith   PetscFunctionReturn(0);
24869b94acceSBarry Smith }
24879b94acceSBarry Smith 
24884a2ae208SSatish Balay #undef __FUNCT__
24894a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
24906ce558aeSBarry Smith /*@C
2491f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2492f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
24939b94acceSBarry Smith 
2494c7afd0dbSLois Curfman McInnes    Collective on SNES
2495c7afd0dbSLois Curfman McInnes 
2496b2002411SLois Curfman McInnes    Input Parameters:
2497c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2498f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
249985385478SLisandro Dalcin -  x - the solution vector.
25009b94acceSBarry Smith 
2501b2002411SLois Curfman McInnes    Notes:
25028ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
25038ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
25048ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
25058ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
25068ddd3da0SLois Curfman McInnes 
250736851e7fSLois Curfman McInnes    Level: beginner
250836851e7fSLois Curfman McInnes 
25099b94acceSBarry Smith .keywords: SNES, nonlinear, solve
25109b94acceSBarry Smith 
251185385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
25129b94acceSBarry Smith @*/
25137087cfbeSBarry Smith PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
25149b94acceSBarry Smith {
2515dfbe8321SBarry Smith   PetscErrorCode ierr;
2516ace3abfcSBarry Smith   PetscBool      flg;
2517eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2518eabae89aSBarry Smith   PetscViewer    viewer;
2519efd51863SBarry Smith   PetscInt       grid;
2520052efed2SBarry Smith 
25213a40ed3dSBarry Smith   PetscFunctionBegin;
25220700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25230700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2524f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
25250700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
252685385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
252785385478SLisandro Dalcin 
2528efd51863SBarry Smith   for (grid=0; grid<snes->gridsequence+1; grid++) {
2529efd51863SBarry Smith 
253085385478SLisandro Dalcin     /* set solution vector */
2531efd51863SBarry Smith     if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);}
25326bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
253385385478SLisandro Dalcin     snes->vec_sol = x;
253485385478SLisandro Dalcin     /* set afine vector if provided */
253585385478SLisandro Dalcin     if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
25366bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
253785385478SLisandro Dalcin     snes->vec_rhs = b;
253885385478SLisandro Dalcin 
253970e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
25403f149594SLisandro Dalcin 
2541d25893d9SBarry Smith     if (!grid && snes->ops->computeinitialguess) {
2542d25893d9SBarry Smith       ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr);
2543d25893d9SBarry Smith     }
2544d25893d9SBarry Smith 
2545abc0a331SBarry Smith     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
254650ffb88aSMatthew Knepley     snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2547d5e45103SBarry Smith 
25483f149594SLisandro Dalcin     ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
25494936397dSBarry Smith     ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
255085385478SLisandro Dalcin     ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
25514936397dSBarry Smith     if (snes->domainerror){
25524936397dSBarry Smith       snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
25534936397dSBarry Smith       snes->domainerror = PETSC_FALSE;
25544936397dSBarry Smith     }
255517186662SBarry Smith     if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
25563f149594SLisandro Dalcin 
25577adad957SLisandro Dalcin     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2558eabae89aSBarry Smith     if (flg && !PetscPreLoadingOn) {
25597adad957SLisandro Dalcin       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2560eabae89aSBarry Smith       ierr = SNESView(snes,viewer);CHKERRQ(ierr);
25616bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2562eabae89aSBarry Smith     }
2563eabae89aSBarry Smith 
256490d69ab7SBarry Smith     flg  = PETSC_FALSE;
2565acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2566da9b6338SBarry Smith     if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
25675968eb51SBarry Smith     if (snes->printreason) {
25685968eb51SBarry Smith       if (snes->reason > 0) {
25697adad957SLisandro Dalcin         ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
25705968eb51SBarry Smith       } else {
25717adad957SLisandro Dalcin         ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
25725968eb51SBarry Smith       }
25735968eb51SBarry Smith     }
25745968eb51SBarry Smith 
2575e113a28aSBarry Smith     if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
2576efd51863SBarry Smith     if (grid <  snes->gridsequence) {
2577efd51863SBarry Smith       DM  fine;
2578efd51863SBarry Smith       Vec xnew;
2579efd51863SBarry Smith       Mat interp;
2580efd51863SBarry Smith 
2581efd51863SBarry Smith       ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr);
2582efd51863SBarry Smith       ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr);
2583efd51863SBarry Smith       ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr);
2584efd51863SBarry Smith       ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr);
2585efd51863SBarry Smith       ierr = MatDestroy(&interp);CHKERRQ(ierr);
2586efd51863SBarry Smith       x    = xnew;
2587efd51863SBarry Smith 
2588efd51863SBarry Smith       ierr = SNESReset(snes);CHKERRQ(ierr);
2589efd51863SBarry Smith       ierr = SNESSetDM(snes,fine);CHKERRQ(ierr);
2590efd51863SBarry Smith       ierr = DMDestroy(&fine);CHKERRQ(ierr);
2591efd51863SBarry Smith     }
2592efd51863SBarry Smith   }
25933a40ed3dSBarry Smith   PetscFunctionReturn(0);
25949b94acceSBarry Smith }
25959b94acceSBarry Smith 
25969b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
25979b94acceSBarry Smith 
25984a2ae208SSatish Balay #undef __FUNCT__
25994a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
260082bf6240SBarry Smith /*@C
26014b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
26029b94acceSBarry Smith 
2603fee21e36SBarry Smith    Collective on SNES
2604fee21e36SBarry Smith 
2605c7afd0dbSLois Curfman McInnes    Input Parameters:
2606c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2607454a90a3SBarry Smith -  type - a known method
2608c7afd0dbSLois Curfman McInnes 
2609c7afd0dbSLois Curfman McInnes    Options Database Key:
2610454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2611c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2612ae12b187SLois Curfman McInnes 
26139b94acceSBarry Smith    Notes:
2614e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
26154b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2616c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
26174b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2618c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
26199b94acceSBarry Smith 
2620ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2621ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2622ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2623ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2624ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2625ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2626ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2627ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2628ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2629b0a32e0cSBarry Smith   appropriate method.
263036851e7fSLois Curfman McInnes 
263136851e7fSLois Curfman McInnes   Level: intermediate
2632a703fe33SLois Curfman McInnes 
2633454a90a3SBarry Smith .keywords: SNES, set, type
2634435da068SBarry Smith 
2635435da068SBarry Smith .seealso: SNESType, SNESCreate()
2636435da068SBarry Smith 
26379b94acceSBarry Smith @*/
26387087cfbeSBarry Smith PetscErrorCode  SNESSetType(SNES snes,const SNESType type)
26399b94acceSBarry Smith {
2640dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
2641ace3abfcSBarry Smith   PetscBool      match;
26423a40ed3dSBarry Smith 
26433a40ed3dSBarry Smith   PetscFunctionBegin;
26440700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26454482741eSBarry Smith   PetscValidCharPointer(type,2);
264682bf6240SBarry Smith 
26476831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
26480f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
264992ff6ae8SBarry Smith 
26504b91b6eaSBarry Smith   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2651e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
265275396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
265375396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
265475396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
265575396ef9SLisandro Dalcin   snes->ops->setup          = 0;
265675396ef9SLisandro Dalcin   snes->ops->solve          = 0;
265775396ef9SLisandro Dalcin   snes->ops->view           = 0;
265875396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
265975396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
266075396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
266175396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
2662454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
266303bfa161SLisandro Dalcin   ierr = (*r)(snes);CHKERRQ(ierr);
26649fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
26659fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
26669fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr);
26679fb22e1aSBarry Smith   }
26689fb22e1aSBarry Smith #endif
26693a40ed3dSBarry Smith   PetscFunctionReturn(0);
26709b94acceSBarry Smith }
26719b94acceSBarry Smith 
2672a847f771SSatish Balay 
26739b94acceSBarry Smith /* --------------------------------------------------------------------- */
26744a2ae208SSatish Balay #undef __FUNCT__
26754a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
267652baeb72SSatish Balay /*@
26779b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2678f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
26799b94acceSBarry Smith 
2680fee21e36SBarry Smith    Not Collective
2681fee21e36SBarry Smith 
268236851e7fSLois Curfman McInnes    Level: advanced
268336851e7fSLois Curfman McInnes 
26849b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
26859b94acceSBarry Smith 
26869b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
26879b94acceSBarry Smith @*/
26887087cfbeSBarry Smith PetscErrorCode  SNESRegisterDestroy(void)
26899b94acceSBarry Smith {
2690dfbe8321SBarry Smith   PetscErrorCode ierr;
269182bf6240SBarry Smith 
26923a40ed3dSBarry Smith   PetscFunctionBegin;
26931441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
26944c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
26953a40ed3dSBarry Smith   PetscFunctionReturn(0);
26969b94acceSBarry Smith }
26979b94acceSBarry Smith 
26984a2ae208SSatish Balay #undef __FUNCT__
26994a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
27009b94acceSBarry Smith /*@C
27019a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
27029b94acceSBarry Smith 
2703c7afd0dbSLois Curfman McInnes    Not Collective
2704c7afd0dbSLois Curfman McInnes 
27059b94acceSBarry Smith    Input Parameter:
27064b0e389bSBarry Smith .  snes - nonlinear solver context
27079b94acceSBarry Smith 
27089b94acceSBarry Smith    Output Parameter:
27093a7fca6bSBarry Smith .  type - SNES method (a character string)
27109b94acceSBarry Smith 
271136851e7fSLois Curfman McInnes    Level: intermediate
271236851e7fSLois Curfman McInnes 
2713454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
27149b94acceSBarry Smith @*/
27157087cfbeSBarry Smith PetscErrorCode  SNESGetType(SNES snes,const SNESType *type)
27169b94acceSBarry Smith {
27173a40ed3dSBarry Smith   PetscFunctionBegin;
27180700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27194482741eSBarry Smith   PetscValidPointer(type,2);
27207adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
27213a40ed3dSBarry Smith   PetscFunctionReturn(0);
27229b94acceSBarry Smith }
27239b94acceSBarry Smith 
27244a2ae208SSatish Balay #undef __FUNCT__
27254a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
272652baeb72SSatish Balay /*@
27279b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
27289b94acceSBarry Smith    stored.
27299b94acceSBarry Smith 
2730c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2731c7afd0dbSLois Curfman McInnes 
27329b94acceSBarry Smith    Input Parameter:
27339b94acceSBarry Smith .  snes - the SNES context
27349b94acceSBarry Smith 
27359b94acceSBarry Smith    Output Parameter:
27369b94acceSBarry Smith .  x - the solution
27379b94acceSBarry Smith 
273870e92668SMatthew Knepley    Level: intermediate
273936851e7fSLois Curfman McInnes 
27409b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
27419b94acceSBarry Smith 
274285385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
27439b94acceSBarry Smith @*/
27447087cfbeSBarry Smith PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
27459b94acceSBarry Smith {
27463a40ed3dSBarry Smith   PetscFunctionBegin;
27470700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27484482741eSBarry Smith   PetscValidPointer(x,2);
274985385478SLisandro Dalcin   *x = snes->vec_sol;
275070e92668SMatthew Knepley   PetscFunctionReturn(0);
275170e92668SMatthew Knepley }
275270e92668SMatthew Knepley 
275370e92668SMatthew Knepley #undef __FUNCT__
27544a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
275552baeb72SSatish Balay /*@
27569b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
27579b94acceSBarry Smith    stored.
27589b94acceSBarry Smith 
2759c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2760c7afd0dbSLois Curfman McInnes 
27619b94acceSBarry Smith    Input Parameter:
27629b94acceSBarry Smith .  snes - the SNES context
27639b94acceSBarry Smith 
27649b94acceSBarry Smith    Output Parameter:
27659b94acceSBarry Smith .  x - the solution update
27669b94acceSBarry Smith 
276736851e7fSLois Curfman McInnes    Level: advanced
276836851e7fSLois Curfman McInnes 
27699b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
27709b94acceSBarry Smith 
277185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
27729b94acceSBarry Smith @*/
27737087cfbeSBarry Smith PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
27749b94acceSBarry Smith {
27753a40ed3dSBarry Smith   PetscFunctionBegin;
27760700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27774482741eSBarry Smith   PetscValidPointer(x,2);
277885385478SLisandro Dalcin   *x = snes->vec_sol_update;
27793a40ed3dSBarry Smith   PetscFunctionReturn(0);
27809b94acceSBarry Smith }
27819b94acceSBarry Smith 
27824a2ae208SSatish Balay #undef __FUNCT__
27834a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
27849b94acceSBarry Smith /*@C
27853638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
27869b94acceSBarry Smith 
2787c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2788c7afd0dbSLois Curfman McInnes 
27899b94acceSBarry Smith    Input Parameter:
27909b94acceSBarry Smith .  snes - the SNES context
27919b94acceSBarry Smith 
27929b94acceSBarry Smith    Output Parameter:
27937bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
279470e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
279570e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
27969b94acceSBarry Smith 
279736851e7fSLois Curfman McInnes    Level: advanced
279836851e7fSLois Curfman McInnes 
2799a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
28009b94acceSBarry Smith 
28014b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
28029b94acceSBarry Smith @*/
28037087cfbeSBarry Smith PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
28049b94acceSBarry Smith {
28053a40ed3dSBarry Smith   PetscFunctionBegin;
28060700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
280785385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2808e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
280970e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
28103a40ed3dSBarry Smith   PetscFunctionReturn(0);
28119b94acceSBarry Smith }
28129b94acceSBarry Smith 
28134a2ae208SSatish Balay #undef __FUNCT__
28144a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
28153c7409f5SSatish Balay /*@C
28163c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2817d850072dSLois Curfman McInnes    SNES options in the database.
28183c7409f5SSatish Balay 
28193f9fe445SBarry Smith    Logically Collective on SNES
2820fee21e36SBarry Smith 
2821c7afd0dbSLois Curfman McInnes    Input Parameter:
2822c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2823c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2824c7afd0dbSLois Curfman McInnes 
2825d850072dSLois Curfman McInnes    Notes:
2826a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2827c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2828d850072dSLois Curfman McInnes 
282936851e7fSLois Curfman McInnes    Level: advanced
283036851e7fSLois Curfman McInnes 
28313c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2832a86d99e1SLois Curfman McInnes 
2833a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
28343c7409f5SSatish Balay @*/
28357087cfbeSBarry Smith PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
28363c7409f5SSatish Balay {
2837dfbe8321SBarry Smith   PetscErrorCode ierr;
28383c7409f5SSatish Balay 
28393a40ed3dSBarry Smith   PetscFunctionBegin;
28400700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2841639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
28421cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
284394b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
28443a40ed3dSBarry Smith   PetscFunctionReturn(0);
28453c7409f5SSatish Balay }
28463c7409f5SSatish Balay 
28474a2ae208SSatish Balay #undef __FUNCT__
28484a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
28493c7409f5SSatish Balay /*@C
2850f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2851d850072dSLois Curfman McInnes    SNES options in the database.
28523c7409f5SSatish Balay 
28533f9fe445SBarry Smith    Logically Collective on SNES
2854fee21e36SBarry Smith 
2855c7afd0dbSLois Curfman McInnes    Input Parameters:
2856c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2857c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2858c7afd0dbSLois Curfman McInnes 
2859d850072dSLois Curfman McInnes    Notes:
2860a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2861c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2862d850072dSLois Curfman McInnes 
286336851e7fSLois Curfman McInnes    Level: advanced
286436851e7fSLois Curfman McInnes 
28653c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2866a86d99e1SLois Curfman McInnes 
2867a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
28683c7409f5SSatish Balay @*/
28697087cfbeSBarry Smith PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
28703c7409f5SSatish Balay {
2871dfbe8321SBarry Smith   PetscErrorCode ierr;
28723c7409f5SSatish Balay 
28733a40ed3dSBarry Smith   PetscFunctionBegin;
28740700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2875639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
28761cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
287794b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
28783a40ed3dSBarry Smith   PetscFunctionReturn(0);
28793c7409f5SSatish Balay }
28803c7409f5SSatish Balay 
28814a2ae208SSatish Balay #undef __FUNCT__
28824a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
28839ab63eb5SSatish Balay /*@C
28843c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
28853c7409f5SSatish Balay    SNES options in the database.
28863c7409f5SSatish Balay 
2887c7afd0dbSLois Curfman McInnes    Not Collective
2888c7afd0dbSLois Curfman McInnes 
28893c7409f5SSatish Balay    Input Parameter:
28903c7409f5SSatish Balay .  snes - the SNES context
28913c7409f5SSatish Balay 
28923c7409f5SSatish Balay    Output Parameter:
28933c7409f5SSatish Balay .  prefix - pointer to the prefix string used
28943c7409f5SSatish Balay 
28954ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
28969ab63eb5SSatish Balay    sufficient length to hold the prefix.
28979ab63eb5SSatish Balay 
289836851e7fSLois Curfman McInnes    Level: advanced
289936851e7fSLois Curfman McInnes 
29003c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2901a86d99e1SLois Curfman McInnes 
2902a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
29033c7409f5SSatish Balay @*/
29047087cfbeSBarry Smith PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
29053c7409f5SSatish Balay {
2906dfbe8321SBarry Smith   PetscErrorCode ierr;
29073c7409f5SSatish Balay 
29083a40ed3dSBarry Smith   PetscFunctionBegin;
29090700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2910639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
29113a40ed3dSBarry Smith   PetscFunctionReturn(0);
29123c7409f5SSatish Balay }
29133c7409f5SSatish Balay 
2914b2002411SLois Curfman McInnes 
29154a2ae208SSatish Balay #undef __FUNCT__
29164a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
29173cea93caSBarry Smith /*@C
29183cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
29193cea93caSBarry Smith 
29207f6c08e0SMatthew Knepley   Level: advanced
29213cea93caSBarry Smith @*/
29227087cfbeSBarry Smith PetscErrorCode  SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2923b2002411SLois Curfman McInnes {
2924e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2925dfbe8321SBarry Smith   PetscErrorCode ierr;
2926b2002411SLois Curfman McInnes 
2927b2002411SLois Curfman McInnes   PetscFunctionBegin;
2928b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2929c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2930b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2931b2002411SLois Curfman McInnes }
2932da9b6338SBarry Smith 
2933da9b6338SBarry Smith #undef __FUNCT__
2934da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
29357087cfbeSBarry Smith PetscErrorCode  SNESTestLocalMin(SNES snes)
2936da9b6338SBarry Smith {
2937dfbe8321SBarry Smith   PetscErrorCode ierr;
293877431f27SBarry Smith   PetscInt       N,i,j;
2939da9b6338SBarry Smith   Vec            u,uh,fh;
2940da9b6338SBarry Smith   PetscScalar    value;
2941da9b6338SBarry Smith   PetscReal      norm;
2942da9b6338SBarry Smith 
2943da9b6338SBarry Smith   PetscFunctionBegin;
2944da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2945da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2946da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2947da9b6338SBarry Smith 
2948da9b6338SBarry Smith   /* currently only works for sequential */
2949da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2950da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2951da9b6338SBarry Smith   for (i=0; i<N; i++) {
2952da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
295377431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2954da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2955ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2956da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
29573ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2958da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
295977431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2960da9b6338SBarry Smith       value = -value;
2961da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2962da9b6338SBarry Smith     }
2963da9b6338SBarry Smith   }
29646bf464f9SBarry Smith   ierr = VecDestroy(&uh);CHKERRQ(ierr);
29656bf464f9SBarry Smith   ierr = VecDestroy(&fh);CHKERRQ(ierr);
2966da9b6338SBarry Smith   PetscFunctionReturn(0);
2967da9b6338SBarry Smith }
296871f87433Sdalcinl 
296971f87433Sdalcinl #undef __FUNCT__
2970fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
297171f87433Sdalcinl /*@
2972fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
297371f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
297471f87433Sdalcinl    Newton method.
297571f87433Sdalcinl 
29763f9fe445SBarry Smith    Logically Collective on SNES
297771f87433Sdalcinl 
297871f87433Sdalcinl    Input Parameters:
297971f87433Sdalcinl +  snes - SNES context
298071f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
298171f87433Sdalcinl 
298264ba62caSBarry Smith     Options Database:
298364ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
298464ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
298564ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
298664ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
298764ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
298864ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
298964ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
299064ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
299164ba62caSBarry Smith 
299271f87433Sdalcinl    Notes:
299371f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
299471f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
299571f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
299671f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
299771f87433Sdalcinl    solver.
299871f87433Sdalcinl 
299971f87433Sdalcinl    Level: advanced
300071f87433Sdalcinl 
300171f87433Sdalcinl    Reference:
300271f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
300371f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
300471f87433Sdalcinl 
300571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
300671f87433Sdalcinl 
3007fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
300871f87433Sdalcinl @*/
30097087cfbeSBarry Smith PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool  flag)
301071f87433Sdalcinl {
301171f87433Sdalcinl   PetscFunctionBegin;
30120700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3013acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flag,2);
301471f87433Sdalcinl   snes->ksp_ewconv = flag;
301571f87433Sdalcinl   PetscFunctionReturn(0);
301671f87433Sdalcinl }
301771f87433Sdalcinl 
301871f87433Sdalcinl #undef __FUNCT__
3019fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
302071f87433Sdalcinl /*@
3021fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
302271f87433Sdalcinl    for computing relative tolerance for linear solvers within an
302371f87433Sdalcinl    inexact Newton method.
302471f87433Sdalcinl 
302571f87433Sdalcinl    Not Collective
302671f87433Sdalcinl 
302771f87433Sdalcinl    Input Parameter:
302871f87433Sdalcinl .  snes - SNES context
302971f87433Sdalcinl 
303071f87433Sdalcinl    Output Parameter:
303171f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
303271f87433Sdalcinl 
303371f87433Sdalcinl    Notes:
303471f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
303571f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
303671f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
303771f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
303871f87433Sdalcinl    solver.
303971f87433Sdalcinl 
304071f87433Sdalcinl    Level: advanced
304171f87433Sdalcinl 
304271f87433Sdalcinl    Reference:
304371f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
304471f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
304571f87433Sdalcinl 
304671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
304771f87433Sdalcinl 
3048fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
304971f87433Sdalcinl @*/
30507087cfbeSBarry Smith PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
305171f87433Sdalcinl {
305271f87433Sdalcinl   PetscFunctionBegin;
30530700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
305471f87433Sdalcinl   PetscValidPointer(flag,2);
305571f87433Sdalcinl   *flag = snes->ksp_ewconv;
305671f87433Sdalcinl   PetscFunctionReturn(0);
305771f87433Sdalcinl }
305871f87433Sdalcinl 
305971f87433Sdalcinl #undef __FUNCT__
3060fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
306171f87433Sdalcinl /*@
3062fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
306371f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
306471f87433Sdalcinl    Newton method.
306571f87433Sdalcinl 
30663f9fe445SBarry Smith    Logically Collective on SNES
306771f87433Sdalcinl 
306871f87433Sdalcinl    Input Parameters:
306971f87433Sdalcinl +    snes - SNES context
307071f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
307171f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
307271f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
307371f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
307471f87433Sdalcinl              (0 <= gamma2 <= 1)
307571f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
307671f87433Sdalcinl .    alpha2 - power for safeguard
307771f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
307871f87433Sdalcinl 
307971f87433Sdalcinl    Note:
308071f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
308171f87433Sdalcinl 
308271f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
308371f87433Sdalcinl 
308471f87433Sdalcinl    Level: advanced
308571f87433Sdalcinl 
308671f87433Sdalcinl    Reference:
308771f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
308871f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
308971f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
309071f87433Sdalcinl 
309171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
309271f87433Sdalcinl 
3093fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
309471f87433Sdalcinl @*/
30957087cfbeSBarry Smith PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
309671f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
309771f87433Sdalcinl {
3098fa9f3622SBarry Smith   SNESKSPEW *kctx;
309971f87433Sdalcinl   PetscFunctionBegin;
31000700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3101fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3102e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
3103c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
3104c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
3105c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
3106c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
3107c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
3108c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
3109c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
311071f87433Sdalcinl 
311171f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
311271f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
311371f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
311471f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
311571f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
311671f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
311771f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
311871f87433Sdalcinl 
311971f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
3120e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
312171f87433Sdalcinl   }
312271f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
3123e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
312471f87433Sdalcinl   }
312571f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
3126e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
312771f87433Sdalcinl   }
312871f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
3129e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
313071f87433Sdalcinl   }
313171f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
3132e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
313371f87433Sdalcinl   }
313471f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
3135e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
313671f87433Sdalcinl   }
313771f87433Sdalcinl   PetscFunctionReturn(0);
313871f87433Sdalcinl }
313971f87433Sdalcinl 
314071f87433Sdalcinl #undef __FUNCT__
3141fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
314271f87433Sdalcinl /*@
3143fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
314471f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
314571f87433Sdalcinl    Newton method.
314671f87433Sdalcinl 
314771f87433Sdalcinl    Not Collective
314871f87433Sdalcinl 
314971f87433Sdalcinl    Input Parameters:
315071f87433Sdalcinl      snes - SNES context
315171f87433Sdalcinl 
315271f87433Sdalcinl    Output Parameters:
315371f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
315471f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
315571f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
315671f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
315771f87433Sdalcinl              (0 <= gamma2 <= 1)
315871f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
315971f87433Sdalcinl .    alpha2 - power for safeguard
316071f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
316171f87433Sdalcinl 
316271f87433Sdalcinl    Level: advanced
316371f87433Sdalcinl 
316471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
316571f87433Sdalcinl 
3166fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
316771f87433Sdalcinl @*/
31687087cfbeSBarry Smith PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
316971f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
317071f87433Sdalcinl {
3171fa9f3622SBarry Smith   SNESKSPEW *kctx;
317271f87433Sdalcinl   PetscFunctionBegin;
31730700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3174fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3175e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
317671f87433Sdalcinl   if(version)   *version   = kctx->version;
317771f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
317871f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
317971f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
318071f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
318171f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
318271f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
318371f87433Sdalcinl   PetscFunctionReturn(0);
318471f87433Sdalcinl }
318571f87433Sdalcinl 
318671f87433Sdalcinl #undef __FUNCT__
3187fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
3188fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
318971f87433Sdalcinl {
319071f87433Sdalcinl   PetscErrorCode ierr;
3191fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
319271f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
319371f87433Sdalcinl 
319471f87433Sdalcinl   PetscFunctionBegin;
3195e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
319671f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
319771f87433Sdalcinl     rtol = kctx->rtol_0;
319871f87433Sdalcinl   } else {
319971f87433Sdalcinl     if (kctx->version == 1) {
320071f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
320171f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
320271f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
320371f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
320471f87433Sdalcinl     } else if (kctx->version == 2) {
320571f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
320671f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
320771f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
320871f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
320971f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
321071f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
321171f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
321271f87433Sdalcinl       stol = PetscMax(rtol,stol);
321371f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
321471f87433Sdalcinl       /* safeguard: avoid oversolving */
321571f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
321671f87433Sdalcinl       stol = PetscMax(rtol,stol);
321771f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
3218e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
321971f87433Sdalcinl   }
322071f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
322171f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
322271f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
322371f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
322471f87433Sdalcinl   PetscFunctionReturn(0);
322571f87433Sdalcinl }
322671f87433Sdalcinl 
322771f87433Sdalcinl #undef __FUNCT__
3228fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
3229fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
323071f87433Sdalcinl {
323171f87433Sdalcinl   PetscErrorCode ierr;
3232fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
323371f87433Sdalcinl   PCSide         pcside;
323471f87433Sdalcinl   Vec            lres;
323571f87433Sdalcinl 
323671f87433Sdalcinl   PetscFunctionBegin;
3237e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
323871f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
323971f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
324071f87433Sdalcinl   if (kctx->version == 1) {
3241b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
324271f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
324371f87433Sdalcinl       /* KSP residual is true linear residual */
324471f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
324571f87433Sdalcinl     } else {
324671f87433Sdalcinl       /* KSP residual is preconditioned residual */
324771f87433Sdalcinl       /* compute true linear residual norm */
324871f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
324971f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
325071f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
325171f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
32526bf464f9SBarry Smith       ierr = VecDestroy(&lres);CHKERRQ(ierr);
325371f87433Sdalcinl     }
325471f87433Sdalcinl   }
325571f87433Sdalcinl   PetscFunctionReturn(0);
325671f87433Sdalcinl }
325771f87433Sdalcinl 
325871f87433Sdalcinl #undef __FUNCT__
325971f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
326071f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
326171f87433Sdalcinl {
326271f87433Sdalcinl   PetscErrorCode ierr;
326371f87433Sdalcinl 
326471f87433Sdalcinl   PetscFunctionBegin;
3265fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
326671f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3267fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
326871f87433Sdalcinl   PetscFunctionReturn(0);
326971f87433Sdalcinl }
32706c699258SBarry Smith 
32716c699258SBarry Smith #undef __FUNCT__
32726c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
32736c699258SBarry Smith /*@
32746c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
32756c699258SBarry Smith 
32763f9fe445SBarry Smith    Logically Collective on SNES
32776c699258SBarry Smith 
32786c699258SBarry Smith    Input Parameters:
32796c699258SBarry Smith +  snes - the preconditioner context
32806c699258SBarry Smith -  dm - the dm
32816c699258SBarry Smith 
32826c699258SBarry Smith    Level: intermediate
32836c699258SBarry Smith 
32846c699258SBarry Smith 
32856c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
32866c699258SBarry Smith @*/
32877087cfbeSBarry Smith PetscErrorCode  SNESSetDM(SNES snes,DM dm)
32886c699258SBarry Smith {
32896c699258SBarry Smith   PetscErrorCode ierr;
3290345fed2cSBarry Smith   KSP            ksp;
32916c699258SBarry Smith 
32926c699258SBarry Smith   PetscFunctionBegin;
32930700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3294d0660788SBarry Smith   if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);}
32956bf464f9SBarry Smith   ierr = DMDestroy(&snes->dm);CHKERRQ(ierr);
32966c699258SBarry Smith   snes->dm = dm;
3297345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3298345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3299f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
33006c699258SBarry Smith   PetscFunctionReturn(0);
33016c699258SBarry Smith }
33026c699258SBarry Smith 
33036c699258SBarry Smith #undef __FUNCT__
33046c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
33056c699258SBarry Smith /*@
33066c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
33076c699258SBarry Smith 
33083f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
33096c699258SBarry Smith 
33106c699258SBarry Smith    Input Parameter:
33116c699258SBarry Smith . snes - the preconditioner context
33126c699258SBarry Smith 
33136c699258SBarry Smith    Output Parameter:
33146c699258SBarry Smith .  dm - the dm
33156c699258SBarry Smith 
33166c699258SBarry Smith    Level: intermediate
33176c699258SBarry Smith 
33186c699258SBarry Smith 
33196c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
33206c699258SBarry Smith @*/
33217087cfbeSBarry Smith PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
33226c699258SBarry Smith {
33236c699258SBarry Smith   PetscFunctionBegin;
33240700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33256c699258SBarry Smith   *dm = snes->dm;
33266c699258SBarry Smith   PetscFunctionReturn(0);
33276c699258SBarry Smith }
33280807856dSBarry Smith 
332969b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3330c6db04a5SJed Brown #include <mex.h>
333169b4f73cSBarry Smith 
33328f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
33338f6e6473SBarry Smith 
33340807856dSBarry Smith #undef __FUNCT__
33350807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab"
33360807856dSBarry Smith /*
33370807856dSBarry Smith    SNESComputeFunction_Matlab - Calls the function that has been set with
33380807856dSBarry Smith                          SNESSetFunctionMatlab().
33390807856dSBarry Smith 
33400807856dSBarry Smith    Collective on SNES
33410807856dSBarry Smith 
33420807856dSBarry Smith    Input Parameters:
33430807856dSBarry Smith +  snes - the SNES context
33440807856dSBarry Smith -  x - input vector
33450807856dSBarry Smith 
33460807856dSBarry Smith    Output Parameter:
33470807856dSBarry Smith .  y - function vector, as set by SNESSetFunction()
33480807856dSBarry Smith 
33490807856dSBarry Smith    Notes:
33500807856dSBarry Smith    SNESComputeFunction() is typically used within nonlinear solvers
33510807856dSBarry Smith    implementations, so most users would not generally call this routine
33520807856dSBarry Smith    themselves.
33530807856dSBarry Smith 
33540807856dSBarry Smith    Level: developer
33550807856dSBarry Smith 
33560807856dSBarry Smith .keywords: SNES, nonlinear, compute, function
33570807856dSBarry Smith 
33580807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
335961b2408cSBarry Smith */
33607087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx)
33610807856dSBarry Smith {
3362e650e774SBarry Smith   PetscErrorCode    ierr;
33638f6e6473SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
33648f6e6473SBarry Smith   int               nlhs = 1,nrhs = 5;
33658f6e6473SBarry Smith   mxArray	    *plhs[1],*prhs[5];
336691621f2eSBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
3367e650e774SBarry Smith 
33680807856dSBarry Smith   PetscFunctionBegin;
33690807856dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33700807856dSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
33710807856dSBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
33720807856dSBarry Smith   PetscCheckSameComm(snes,1,x,2);
33730807856dSBarry Smith   PetscCheckSameComm(snes,1,y,3);
33740807856dSBarry Smith 
33750807856dSBarry Smith   /* call Matlab function in ctx with arguments x and y */
3376e650e774SBarry Smith 
337791621f2eSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3378e650e774SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3379e650e774SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
338091621f2eSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
338191621f2eSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
338291621f2eSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
33838f6e6473SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
33848f6e6473SBarry Smith   prhs[4] =  sctx->ctx;
3385b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr);
3386e650e774SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3387e650e774SBarry Smith   mxDestroyArray(prhs[0]);
3388e650e774SBarry Smith   mxDestroyArray(prhs[1]);
3389e650e774SBarry Smith   mxDestroyArray(prhs[2]);
33908f6e6473SBarry Smith   mxDestroyArray(prhs[3]);
3391e650e774SBarry Smith   mxDestroyArray(plhs[0]);
33920807856dSBarry Smith   PetscFunctionReturn(0);
33930807856dSBarry Smith }
33940807856dSBarry Smith 
33950807856dSBarry Smith 
33960807856dSBarry Smith #undef __FUNCT__
33970807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab"
339861b2408cSBarry Smith /*
33990807856dSBarry Smith    SNESSetFunctionMatlab - Sets the function evaluation routine and function
34000807856dSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3401e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
34020807856dSBarry Smith 
34030807856dSBarry Smith    Logically Collective on SNES
34040807856dSBarry Smith 
34050807856dSBarry Smith    Input Parameters:
34060807856dSBarry Smith +  snes - the SNES context
34070807856dSBarry Smith .  r - vector to store function value
34080807856dSBarry Smith -  func - function evaluation routine
34090807856dSBarry Smith 
34100807856dSBarry Smith    Calling sequence of func:
341161b2408cSBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
34120807856dSBarry Smith 
34130807856dSBarry Smith 
34140807856dSBarry Smith    Notes:
34150807856dSBarry Smith    The Newton-like methods typically solve linear systems of the form
34160807856dSBarry Smith $      f'(x) x = -f(x),
34170807856dSBarry Smith    where f'(x) denotes the Jacobian matrix and f(x) is the function.
34180807856dSBarry Smith 
34190807856dSBarry Smith    Level: beginner
34200807856dSBarry Smith 
34210807856dSBarry Smith .keywords: SNES, nonlinear, set, function
34220807856dSBarry Smith 
34230807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
342461b2408cSBarry Smith */
34257087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx)
34260807856dSBarry Smith {
34270807856dSBarry Smith   PetscErrorCode    ierr;
34288f6e6473SBarry Smith   SNESMatlabContext *sctx;
34290807856dSBarry Smith 
34300807856dSBarry Smith   PetscFunctionBegin;
34318f6e6473SBarry Smith   /* currently sctx is memory bleed */
34328f6e6473SBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
34338f6e6473SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
34348f6e6473SBarry Smith   /*
34358f6e6473SBarry Smith      This should work, but it doesn't
34368f6e6473SBarry Smith   sctx->ctx = ctx;
34378f6e6473SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
34388f6e6473SBarry Smith   */
34398f6e6473SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
34408f6e6473SBarry Smith   ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr);
34410807856dSBarry Smith   PetscFunctionReturn(0);
34420807856dSBarry Smith }
344369b4f73cSBarry Smith 
344461b2408cSBarry Smith #undef __FUNCT__
344561b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab"
344661b2408cSBarry Smith /*
344761b2408cSBarry Smith    SNESComputeJacobian_Matlab - Calls the function that has been set with
344861b2408cSBarry Smith                          SNESSetJacobianMatlab().
344961b2408cSBarry Smith 
345061b2408cSBarry Smith    Collective on SNES
345161b2408cSBarry Smith 
345261b2408cSBarry Smith    Input Parameters:
345361b2408cSBarry Smith +  snes - the SNES context
345461b2408cSBarry Smith .  x - input vector
345561b2408cSBarry Smith .  A, B - the matrices
345661b2408cSBarry Smith -  ctx - user context
345761b2408cSBarry Smith 
345861b2408cSBarry Smith    Output Parameter:
345961b2408cSBarry Smith .  flag - structure of the matrix
346061b2408cSBarry Smith 
346161b2408cSBarry Smith    Level: developer
346261b2408cSBarry Smith 
346361b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function
346461b2408cSBarry Smith 
346561b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
346661b2408cSBarry Smith @*/
34677087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx)
346861b2408cSBarry Smith {
346961b2408cSBarry Smith   PetscErrorCode    ierr;
347061b2408cSBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
347161b2408cSBarry Smith   int               nlhs = 2,nrhs = 6;
347261b2408cSBarry Smith   mxArray	    *plhs[2],*prhs[6];
347361b2408cSBarry Smith   long long int     lx = 0,lA = 0,ls = 0, lB = 0;
347461b2408cSBarry Smith 
347561b2408cSBarry Smith   PetscFunctionBegin;
347661b2408cSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
347761b2408cSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
347861b2408cSBarry Smith 
347961b2408cSBarry Smith   /* call Matlab function in ctx with arguments x and y */
348061b2408cSBarry Smith 
348161b2408cSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
348261b2408cSBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
348361b2408cSBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
348461b2408cSBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
348561b2408cSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
348661b2408cSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
348761b2408cSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
348861b2408cSBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
348961b2408cSBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
349061b2408cSBarry Smith   prhs[5] =  sctx->ctx;
3491b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr);
349261b2408cSBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
349361b2408cSBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
349461b2408cSBarry Smith   mxDestroyArray(prhs[0]);
349561b2408cSBarry Smith   mxDestroyArray(prhs[1]);
349661b2408cSBarry Smith   mxDestroyArray(prhs[2]);
349761b2408cSBarry Smith   mxDestroyArray(prhs[3]);
349861b2408cSBarry Smith   mxDestroyArray(prhs[4]);
349961b2408cSBarry Smith   mxDestroyArray(plhs[0]);
350061b2408cSBarry Smith   mxDestroyArray(plhs[1]);
350161b2408cSBarry Smith   PetscFunctionReturn(0);
350261b2408cSBarry Smith }
350361b2408cSBarry Smith 
350461b2408cSBarry Smith 
350561b2408cSBarry Smith #undef __FUNCT__
350661b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab"
350761b2408cSBarry Smith /*
350861b2408cSBarry Smith    SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
350961b2408cSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3510e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
351161b2408cSBarry Smith 
351261b2408cSBarry Smith    Logically Collective on SNES
351361b2408cSBarry Smith 
351461b2408cSBarry Smith    Input Parameters:
351561b2408cSBarry Smith +  snes - the SNES context
351661b2408cSBarry Smith .  A,B - Jacobian matrices
351761b2408cSBarry Smith .  func - function evaluation routine
351861b2408cSBarry Smith -  ctx - user context
351961b2408cSBarry Smith 
352061b2408cSBarry Smith    Calling sequence of func:
352161b2408cSBarry Smith $    flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx);
352261b2408cSBarry Smith 
352361b2408cSBarry Smith 
352461b2408cSBarry Smith    Level: developer
352561b2408cSBarry Smith 
352661b2408cSBarry Smith .keywords: SNES, nonlinear, set, function
352761b2408cSBarry Smith 
352861b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
352961b2408cSBarry Smith */
35307087cfbeSBarry Smith PetscErrorCode  SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx)
353161b2408cSBarry Smith {
353261b2408cSBarry Smith   PetscErrorCode    ierr;
353361b2408cSBarry Smith   SNESMatlabContext *sctx;
353461b2408cSBarry Smith 
353561b2408cSBarry Smith   PetscFunctionBegin;
353661b2408cSBarry Smith   /* currently sctx is memory bleed */
353761b2408cSBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
353861b2408cSBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
353961b2408cSBarry Smith   /*
354061b2408cSBarry Smith      This should work, but it doesn't
354161b2408cSBarry Smith   sctx->ctx = ctx;
354261b2408cSBarry Smith   mexMakeArrayPersistent(sctx->ctx);
354361b2408cSBarry Smith   */
354461b2408cSBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
354561b2408cSBarry Smith   ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
354661b2408cSBarry Smith   PetscFunctionReturn(0);
354761b2408cSBarry Smith }
354869b4f73cSBarry Smith 
3549f9eb7ae2SShri Abhyankar #undef __FUNCT__
3550f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab"
3551f9eb7ae2SShri Abhyankar /*
3552f9eb7ae2SShri Abhyankar    SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab().
3553f9eb7ae2SShri Abhyankar 
3554f9eb7ae2SShri Abhyankar    Collective on SNES
3555f9eb7ae2SShri Abhyankar 
3556f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction()
3557f9eb7ae2SShri Abhyankar @*/
35587087cfbeSBarry Smith PetscErrorCode  SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx)
3559f9eb7ae2SShri Abhyankar {
3560f9eb7ae2SShri Abhyankar   PetscErrorCode  ierr;
356148f37e70SShri Abhyankar   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
3562f9eb7ae2SShri Abhyankar   int             nlhs = 1,nrhs = 6;
3563f9eb7ae2SShri Abhyankar   mxArray	  *plhs[1],*prhs[6];
3564f9eb7ae2SShri Abhyankar   long long int   lx = 0,ls = 0;
3565f9eb7ae2SShri Abhyankar   Vec             x=snes->vec_sol;
3566f9eb7ae2SShri Abhyankar 
3567f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3568f9eb7ae2SShri Abhyankar   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3569f9eb7ae2SShri Abhyankar 
3570f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3571f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3572f9eb7ae2SShri Abhyankar   prhs[0] =  mxCreateDoubleScalar((double)ls);
3573f9eb7ae2SShri Abhyankar   prhs[1] =  mxCreateDoubleScalar((double)it);
3574f9eb7ae2SShri Abhyankar   prhs[2] =  mxCreateDoubleScalar((double)fnorm);
3575f9eb7ae2SShri Abhyankar   prhs[3] =  mxCreateDoubleScalar((double)lx);
3576f9eb7ae2SShri Abhyankar   prhs[4] =  mxCreateString(sctx->funcname);
3577f9eb7ae2SShri Abhyankar   prhs[5] =  sctx->ctx;
3578f9eb7ae2SShri Abhyankar   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr);
3579f9eb7ae2SShri Abhyankar   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3580f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[0]);
3581f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[1]);
3582f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[2]);
3583f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[3]);
3584f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[4]);
3585f9eb7ae2SShri Abhyankar   mxDestroyArray(plhs[0]);
3586f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3587f9eb7ae2SShri Abhyankar }
3588f9eb7ae2SShri Abhyankar 
3589f9eb7ae2SShri Abhyankar 
3590f9eb7ae2SShri Abhyankar #undef __FUNCT__
3591f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab"
3592f9eb7ae2SShri Abhyankar /*
3593e3c5b3baSBarry Smith    SNESMonitorSetMatlab - Sets the monitor function from MATLAB
3594f9eb7ae2SShri Abhyankar 
3595f9eb7ae2SShri Abhyankar    Level: developer
3596f9eb7ae2SShri Abhyankar 
3597f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function
3598f9eb7ae2SShri Abhyankar 
3599f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
3600f9eb7ae2SShri Abhyankar */
36017087cfbeSBarry Smith PetscErrorCode  SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx)
3602f9eb7ae2SShri Abhyankar {
3603f9eb7ae2SShri Abhyankar   PetscErrorCode    ierr;
3604f9eb7ae2SShri Abhyankar   SNESMatlabContext *sctx;
3605f9eb7ae2SShri Abhyankar 
3606f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3607f9eb7ae2SShri Abhyankar   /* currently sctx is memory bleed */
3608f9eb7ae2SShri Abhyankar   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
3609f9eb7ae2SShri Abhyankar   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3610f9eb7ae2SShri Abhyankar   /*
3611f9eb7ae2SShri Abhyankar      This should work, but it doesn't
3612f9eb7ae2SShri Abhyankar   sctx->ctx = ctx;
3613f9eb7ae2SShri Abhyankar   mexMakeArrayPersistent(sctx->ctx);
3614f9eb7ae2SShri Abhyankar   */
3615f9eb7ae2SShri Abhyankar   sctx->ctx = mxDuplicateArray(ctx);
3616f9eb7ae2SShri Abhyankar   ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3617f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3618f9eb7ae2SShri Abhyankar }
3619f9eb7ae2SShri Abhyankar 
362069b4f73cSBarry Smith #endif
3621