xref: /petsc/src/snes/interface/snes.c (revision b07ff4145c9560608b2d7b6645acf0de7a23a7ca)
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"
518*b07ff414SBarry 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 {
5361b2093e4SBarry Smith   PetscErrorCode ierr;
537*b07ff414SBarry Smith   KSP            ksp;
5381b2093e4SBarry Smith 
5393a40ed3dSBarry Smith   PetscFunctionBegin;
5400700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
541*b07ff414SBarry Smith   ierr       = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
542*b07ff414SBarry Smith   ierr       = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr);
5439b94acceSBarry Smith   snes->user = usrP;
5443a40ed3dSBarry Smith   PetscFunctionReturn(0);
5459b94acceSBarry Smith }
54674679c65SBarry Smith 
5474a2ae208SSatish Balay #undef __FUNCT__
5484a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
549*b07ff414SBarry Smith /*@
5509b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
5519b94acceSBarry Smith    nonlinear solvers.
5529b94acceSBarry Smith 
553c7afd0dbSLois Curfman McInnes    Not Collective
554c7afd0dbSLois Curfman McInnes 
5559b94acceSBarry Smith    Input Parameter:
5569b94acceSBarry Smith .  snes - SNES context
5579b94acceSBarry Smith 
5589b94acceSBarry Smith    Output Parameter:
5599b94acceSBarry Smith .  usrP - user context
5609b94acceSBarry Smith 
56136851e7fSLois Curfman McInnes    Level: intermediate
56236851e7fSLois Curfman McInnes 
5639b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
5649b94acceSBarry Smith 
5659b94acceSBarry Smith .seealso: SNESSetApplicationContext()
5669b94acceSBarry Smith @*/
5677087cfbeSBarry Smith PetscErrorCode  SNESGetApplicationContext(SNES snes,void **usrP)
5689b94acceSBarry Smith {
5693a40ed3dSBarry Smith   PetscFunctionBegin;
5700700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5719b94acceSBarry Smith   *usrP = snes->user;
5723a40ed3dSBarry Smith   PetscFunctionReturn(0);
5739b94acceSBarry Smith }
57474679c65SBarry Smith 
5754a2ae208SSatish Balay #undef __FUNCT__
5764a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
5779b94acceSBarry Smith /*@
578c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
579c8228a4eSBarry Smith    at this time.
5809b94acceSBarry Smith 
581c7afd0dbSLois Curfman McInnes    Not Collective
582c7afd0dbSLois Curfman McInnes 
5839b94acceSBarry Smith    Input Parameter:
5849b94acceSBarry Smith .  snes - SNES context
5859b94acceSBarry Smith 
5869b94acceSBarry Smith    Output Parameter:
5879b94acceSBarry Smith .  iter - iteration number
5889b94acceSBarry Smith 
589c8228a4eSBarry Smith    Notes:
590c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
591c8228a4eSBarry Smith 
592c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
59308405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
59408405cd6SLois Curfman McInnes .vb
59508405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
59608405cd6SLois Curfman McInnes       if (!(it % 2)) {
59708405cd6SLois Curfman McInnes         [compute Jacobian here]
59808405cd6SLois Curfman McInnes       }
59908405cd6SLois Curfman McInnes .ve
600c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
60108405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
602c8228a4eSBarry Smith 
60336851e7fSLois Curfman McInnes    Level: intermediate
60436851e7fSLois Curfman McInnes 
6052b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
6062b668275SBarry Smith 
607b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
6089b94acceSBarry Smith @*/
6097087cfbeSBarry Smith PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt* iter)
6109b94acceSBarry Smith {
6113a40ed3dSBarry Smith   PetscFunctionBegin;
6120700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6134482741eSBarry Smith   PetscValidIntPointer(iter,2);
6149b94acceSBarry Smith   *iter = snes->iter;
6153a40ed3dSBarry Smith   PetscFunctionReturn(0);
6169b94acceSBarry Smith }
61774679c65SBarry Smith 
6184a2ae208SSatish Balay #undef __FUNCT__
6194a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
6209b94acceSBarry Smith /*@
6219b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
6229b94acceSBarry Smith    with SNESSSetFunction().
6239b94acceSBarry Smith 
624c7afd0dbSLois Curfman McInnes    Collective on SNES
625c7afd0dbSLois Curfman McInnes 
6269b94acceSBarry Smith    Input Parameter:
6279b94acceSBarry Smith .  snes - SNES context
6289b94acceSBarry Smith 
6299b94acceSBarry Smith    Output Parameter:
6309b94acceSBarry Smith .  fnorm - 2-norm of function
6319b94acceSBarry Smith 
63236851e7fSLois Curfman McInnes    Level: intermediate
63336851e7fSLois Curfman McInnes 
6349b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
635a86d99e1SLois Curfman McInnes 
636b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
6379b94acceSBarry Smith @*/
6387087cfbeSBarry Smith PetscErrorCode  SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
6399b94acceSBarry Smith {
6403a40ed3dSBarry Smith   PetscFunctionBegin;
6410700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6424482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
6439b94acceSBarry Smith   *fnorm = snes->norm;
6443a40ed3dSBarry Smith   PetscFunctionReturn(0);
6459b94acceSBarry Smith }
64674679c65SBarry Smith 
6474a2ae208SSatish Balay #undef __FUNCT__
648b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
6499b94acceSBarry Smith /*@
650b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
6519b94acceSBarry Smith    attempted by the nonlinear solver.
6529b94acceSBarry Smith 
653c7afd0dbSLois Curfman McInnes    Not Collective
654c7afd0dbSLois Curfman McInnes 
6559b94acceSBarry Smith    Input Parameter:
6569b94acceSBarry Smith .  snes - SNES context
6579b94acceSBarry Smith 
6589b94acceSBarry Smith    Output Parameter:
6599b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
6609b94acceSBarry Smith 
661c96a6f78SLois Curfman McInnes    Notes:
662c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
663c96a6f78SLois Curfman McInnes 
66436851e7fSLois Curfman McInnes    Level: intermediate
66536851e7fSLois Curfman McInnes 
6669b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
66758ebbce7SBarry Smith 
668e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
66958ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
6709b94acceSBarry Smith @*/
6717087cfbeSBarry Smith PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
6729b94acceSBarry Smith {
6733a40ed3dSBarry Smith   PetscFunctionBegin;
6740700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6754482741eSBarry Smith   PetscValidIntPointer(nfails,2);
67650ffb88aSMatthew Knepley   *nfails = snes->numFailures;
67750ffb88aSMatthew Knepley   PetscFunctionReturn(0);
67850ffb88aSMatthew Knepley }
67950ffb88aSMatthew Knepley 
68050ffb88aSMatthew Knepley #undef __FUNCT__
681b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
68250ffb88aSMatthew Knepley /*@
683b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
68450ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
68550ffb88aSMatthew Knepley 
68650ffb88aSMatthew Knepley    Not Collective
68750ffb88aSMatthew Knepley 
68850ffb88aSMatthew Knepley    Input Parameters:
68950ffb88aSMatthew Knepley +  snes     - SNES context
69050ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
69150ffb88aSMatthew Knepley 
69250ffb88aSMatthew Knepley    Level: intermediate
69350ffb88aSMatthew Knepley 
69450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
69558ebbce7SBarry Smith 
696e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
69758ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
69850ffb88aSMatthew Knepley @*/
6997087cfbeSBarry Smith PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
70050ffb88aSMatthew Knepley {
70150ffb88aSMatthew Knepley   PetscFunctionBegin;
7020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
70350ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
70450ffb88aSMatthew Knepley   PetscFunctionReturn(0);
70550ffb88aSMatthew Knepley }
70650ffb88aSMatthew Knepley 
70750ffb88aSMatthew Knepley #undef __FUNCT__
708b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
70950ffb88aSMatthew Knepley /*@
710b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
71150ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
71250ffb88aSMatthew Knepley 
71350ffb88aSMatthew Knepley    Not Collective
71450ffb88aSMatthew Knepley 
71550ffb88aSMatthew Knepley    Input Parameter:
71650ffb88aSMatthew Knepley .  snes     - SNES context
71750ffb88aSMatthew Knepley 
71850ffb88aSMatthew Knepley    Output Parameter:
71950ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
72050ffb88aSMatthew Knepley 
72150ffb88aSMatthew Knepley    Level: intermediate
72250ffb88aSMatthew Knepley 
72350ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
72458ebbce7SBarry Smith 
725e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
72658ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
72758ebbce7SBarry Smith 
72850ffb88aSMatthew Knepley @*/
7297087cfbeSBarry Smith PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
73050ffb88aSMatthew Knepley {
73150ffb88aSMatthew Knepley   PetscFunctionBegin;
7320700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7334482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
73450ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
7353a40ed3dSBarry Smith   PetscFunctionReturn(0);
7369b94acceSBarry Smith }
737a847f771SSatish Balay 
7384a2ae208SSatish Balay #undef __FUNCT__
7392541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
7402541af92SBarry Smith /*@
7412541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
7422541af92SBarry Smith      done by SNES.
7432541af92SBarry Smith 
7442541af92SBarry Smith    Not Collective
7452541af92SBarry Smith 
7462541af92SBarry Smith    Input Parameter:
7472541af92SBarry Smith .  snes     - SNES context
7482541af92SBarry Smith 
7492541af92SBarry Smith    Output Parameter:
7502541af92SBarry Smith .  nfuncs - number of evaluations
7512541af92SBarry Smith 
7522541af92SBarry Smith    Level: intermediate
7532541af92SBarry Smith 
7542541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
75558ebbce7SBarry Smith 
756e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
7572541af92SBarry Smith @*/
7587087cfbeSBarry Smith PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
7592541af92SBarry Smith {
7602541af92SBarry Smith   PetscFunctionBegin;
7610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7622541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
7632541af92SBarry Smith   *nfuncs = snes->nfuncs;
7642541af92SBarry Smith   PetscFunctionReturn(0);
7652541af92SBarry Smith }
7662541af92SBarry Smith 
7672541af92SBarry Smith #undef __FUNCT__
7683d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
7693d4c4710SBarry Smith /*@
7703d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
7713d4c4710SBarry Smith    linear solvers.
7723d4c4710SBarry Smith 
7733d4c4710SBarry Smith    Not Collective
7743d4c4710SBarry Smith 
7753d4c4710SBarry Smith    Input Parameter:
7763d4c4710SBarry Smith .  snes - SNES context
7773d4c4710SBarry Smith 
7783d4c4710SBarry Smith    Output Parameter:
7793d4c4710SBarry Smith .  nfails - number of failed solves
7803d4c4710SBarry Smith 
7813d4c4710SBarry Smith    Notes:
7823d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
7833d4c4710SBarry Smith 
7843d4c4710SBarry Smith    Level: intermediate
7853d4c4710SBarry Smith 
7863d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
78758ebbce7SBarry Smith 
788e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
7893d4c4710SBarry Smith @*/
7907087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
7913d4c4710SBarry Smith {
7923d4c4710SBarry Smith   PetscFunctionBegin;
7930700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7943d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
7953d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
7963d4c4710SBarry Smith   PetscFunctionReturn(0);
7973d4c4710SBarry Smith }
7983d4c4710SBarry Smith 
7993d4c4710SBarry Smith #undef __FUNCT__
8003d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
8013d4c4710SBarry Smith /*@
8023d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
8033d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
8043d4c4710SBarry Smith 
8053f9fe445SBarry Smith    Logically Collective on SNES
8063d4c4710SBarry Smith 
8073d4c4710SBarry Smith    Input Parameters:
8083d4c4710SBarry Smith +  snes     - SNES context
8093d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
8103d4c4710SBarry Smith 
8113d4c4710SBarry Smith    Level: intermediate
8123d4c4710SBarry Smith 
813a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
8143d4c4710SBarry Smith 
8153d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
8163d4c4710SBarry Smith 
81758ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
8183d4c4710SBarry Smith @*/
8197087cfbeSBarry Smith PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
8203d4c4710SBarry Smith {
8213d4c4710SBarry Smith   PetscFunctionBegin;
8220700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
823c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
8243d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
8253d4c4710SBarry Smith   PetscFunctionReturn(0);
8263d4c4710SBarry Smith }
8273d4c4710SBarry Smith 
8283d4c4710SBarry Smith #undef __FUNCT__
8293d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
8303d4c4710SBarry Smith /*@
8313d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
8323d4c4710SBarry Smith      are allowed before SNES terminates
8333d4c4710SBarry Smith 
8343d4c4710SBarry Smith    Not Collective
8353d4c4710SBarry Smith 
8363d4c4710SBarry Smith    Input Parameter:
8373d4c4710SBarry Smith .  snes     - SNES context
8383d4c4710SBarry Smith 
8393d4c4710SBarry Smith    Output Parameter:
8403d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
8413d4c4710SBarry Smith 
8423d4c4710SBarry Smith    Level: intermediate
8433d4c4710SBarry Smith 
8443d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
8453d4c4710SBarry Smith 
8463d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
8473d4c4710SBarry Smith 
848e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
8493d4c4710SBarry Smith @*/
8507087cfbeSBarry Smith PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
8513d4c4710SBarry Smith {
8523d4c4710SBarry Smith   PetscFunctionBegin;
8530700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8543d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
8553d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
8563d4c4710SBarry Smith   PetscFunctionReturn(0);
8573d4c4710SBarry Smith }
8583d4c4710SBarry Smith 
8593d4c4710SBarry Smith #undef __FUNCT__
860b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
861c96a6f78SLois Curfman McInnes /*@
862b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
863c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
864c96a6f78SLois Curfman McInnes 
865c7afd0dbSLois Curfman McInnes    Not Collective
866c7afd0dbSLois Curfman McInnes 
867c96a6f78SLois Curfman McInnes    Input Parameter:
868c96a6f78SLois Curfman McInnes .  snes - SNES context
869c96a6f78SLois Curfman McInnes 
870c96a6f78SLois Curfman McInnes    Output Parameter:
871c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
872c96a6f78SLois Curfman McInnes 
873c96a6f78SLois Curfman McInnes    Notes:
874c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
875c96a6f78SLois Curfman McInnes 
87636851e7fSLois Curfman McInnes    Level: intermediate
87736851e7fSLois Curfman McInnes 
878c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
8792b668275SBarry Smith 
8808c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
881c96a6f78SLois Curfman McInnes @*/
8827087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
883c96a6f78SLois Curfman McInnes {
8843a40ed3dSBarry Smith   PetscFunctionBegin;
8850700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8864482741eSBarry Smith   PetscValidIntPointer(lits,2);
887c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
8883a40ed3dSBarry Smith   PetscFunctionReturn(0);
889c96a6f78SLois Curfman McInnes }
890c96a6f78SLois Curfman McInnes 
8914a2ae208SSatish Balay #undef __FUNCT__
89294b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
89352baeb72SSatish Balay /*@
89494b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
8959b94acceSBarry Smith 
89694b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
897c7afd0dbSLois Curfman McInnes 
8989b94acceSBarry Smith    Input Parameter:
8999b94acceSBarry Smith .  snes - the SNES context
9009b94acceSBarry Smith 
9019b94acceSBarry Smith    Output Parameter:
90294b7f48cSBarry Smith .  ksp - the KSP context
9039b94acceSBarry Smith 
9049b94acceSBarry Smith    Notes:
90594b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
9069b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
9072999313aSBarry Smith    PC contexts as well.
9089b94acceSBarry Smith 
90936851e7fSLois Curfman McInnes    Level: beginner
91036851e7fSLois Curfman McInnes 
91194b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9129b94acceSBarry Smith 
9132999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9149b94acceSBarry Smith @*/
9157087cfbeSBarry Smith PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
9169b94acceSBarry Smith {
9171cee3971SBarry Smith   PetscErrorCode ierr;
9181cee3971SBarry Smith 
9193a40ed3dSBarry Smith   PetscFunctionBegin;
9200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9214482741eSBarry Smith   PetscValidPointer(ksp,2);
9221cee3971SBarry Smith 
9231cee3971SBarry Smith   if (!snes->ksp) {
9241cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
9251cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
9261cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
9271cee3971SBarry Smith   }
92894b7f48cSBarry Smith   *ksp = snes->ksp;
9293a40ed3dSBarry Smith   PetscFunctionReturn(0);
9309b94acceSBarry Smith }
93182bf6240SBarry Smith 
9324a2ae208SSatish Balay #undef __FUNCT__
9332999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
9342999313aSBarry Smith /*@
9352999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
9362999313aSBarry Smith 
9372999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
9382999313aSBarry Smith 
9392999313aSBarry Smith    Input Parameters:
9402999313aSBarry Smith +  snes - the SNES context
9412999313aSBarry Smith -  ksp - the KSP context
9422999313aSBarry Smith 
9432999313aSBarry Smith    Notes:
9442999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
9452999313aSBarry Smith    so this routine is rarely needed.
9462999313aSBarry Smith 
9472999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
9482999313aSBarry Smith    decreased by one.
9492999313aSBarry Smith 
9502999313aSBarry Smith    Level: developer
9512999313aSBarry Smith 
9522999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9532999313aSBarry Smith 
9542999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9552999313aSBarry Smith @*/
9567087cfbeSBarry Smith PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
9572999313aSBarry Smith {
9582999313aSBarry Smith   PetscErrorCode ierr;
9592999313aSBarry Smith 
9602999313aSBarry Smith   PetscFunctionBegin;
9610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9620700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
9632999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
9647dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
965906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
9662999313aSBarry Smith   snes->ksp = ksp;
9672999313aSBarry Smith   PetscFunctionReturn(0);
9682999313aSBarry Smith }
9692999313aSBarry Smith 
9707adad957SLisandro Dalcin #if 0
9712999313aSBarry Smith #undef __FUNCT__
9724a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
9736849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
974e24b481bSBarry Smith {
975e24b481bSBarry Smith   PetscFunctionBegin;
976e24b481bSBarry Smith   PetscFunctionReturn(0);
977e24b481bSBarry Smith }
9787adad957SLisandro Dalcin #endif
979e24b481bSBarry Smith 
9809b94acceSBarry Smith /* -----------------------------------------------------------*/
9814a2ae208SSatish Balay #undef __FUNCT__
9824a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
98352baeb72SSatish Balay /*@
9849b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
9859b94acceSBarry Smith 
986c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
987c7afd0dbSLois Curfman McInnes 
988c7afd0dbSLois Curfman McInnes    Input Parameters:
989906ed7ccSBarry Smith .  comm - MPI communicator
9909b94acceSBarry Smith 
9919b94acceSBarry Smith    Output Parameter:
9929b94acceSBarry Smith .  outsnes - the new SNES context
9939b94acceSBarry Smith 
994c7afd0dbSLois Curfman McInnes    Options Database Keys:
995c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
996c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
997c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
998c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
999c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
1000c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
1001c1f60f51SBarry Smith 
100236851e7fSLois Curfman McInnes    Level: beginner
100336851e7fSLois Curfman McInnes 
10049b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
10059b94acceSBarry Smith 
1006a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
1007a8054027SBarry Smith 
10089b94acceSBarry Smith @*/
10097087cfbeSBarry Smith PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
10109b94acceSBarry Smith {
1011dfbe8321SBarry Smith   PetscErrorCode      ierr;
10129b94acceSBarry Smith   SNES                snes;
1013fa9f3622SBarry Smith   SNESKSPEW           *kctx;
101437fcc0dbSBarry Smith 
10153a40ed3dSBarry Smith   PetscFunctionBegin;
1016ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
10178ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
10188ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
10198ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
10208ba1e511SMatthew Knepley #endif
10218ba1e511SMatthew Knepley 
10220700a824SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
10237adad957SLisandro Dalcin 
102485385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
10259b94acceSBarry Smith   snes->max_its           = 50;
10269750a799SBarry Smith   snes->max_funcs	  = 10000;
10279b94acceSBarry Smith   snes->norm		  = 0.0;
1028b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
1029b4874afaSBarry Smith   snes->ttol              = 0.0;
103070441072SBarry Smith   snes->abstol		  = 1.e-50;
10319b94acceSBarry Smith   snes->xtol		  = 1.e-8;
10324b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
10339b94acceSBarry Smith   snes->nfuncs            = 0;
103450ffb88aSMatthew Knepley   snes->numFailures       = 0;
103550ffb88aSMatthew Knepley   snes->maxFailures       = 1;
10367a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1037e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1038a8054027SBarry Smith   snes->lagpreconditioner = 1;
1039639f9d9dSBarry Smith   snes->numbermonitors    = 0;
10409b94acceSBarry Smith   snes->data              = 0;
10414dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1042186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
10436f24a144SLois Curfman McInnes   snes->nwork             = 0;
104458c9b817SLisandro Dalcin   snes->work              = 0;
104558c9b817SLisandro Dalcin   snes->nvwork            = 0;
104658c9b817SLisandro Dalcin   snes->vwork             = 0;
1047758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1048758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1049758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1050758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1051758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1052184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
10539b94acceSBarry Smith 
10543d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
10553d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
10563d4c4710SBarry Smith 
10579b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
105838f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
10599b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
10609b94acceSBarry Smith   kctx->version     = 2;
10619b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
10629b94acceSBarry Smith                              this was too large for some test cases */
106375567043SBarry Smith   kctx->rtol_last   = 0.0;
10649b94acceSBarry Smith   kctx->rtol_max    = .9;
10659b94acceSBarry Smith   kctx->gamma       = 1.0;
106671f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
106771f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
10689b94acceSBarry Smith   kctx->threshold   = .1;
106975567043SBarry Smith   kctx->lresid_last = 0.0;
107075567043SBarry Smith   kctx->norm_last   = 0.0;
10719b94acceSBarry Smith 
10729b94acceSBarry Smith   *outsnes = snes;
10733a40ed3dSBarry Smith   PetscFunctionReturn(0);
10749b94acceSBarry Smith }
10759b94acceSBarry Smith 
10764a2ae208SSatish Balay #undef __FUNCT__
10774a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
10789b94acceSBarry Smith /*@C
10799b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
10809b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
10819b94acceSBarry Smith    equations.
10829b94acceSBarry Smith 
10833f9fe445SBarry Smith    Logically Collective on SNES
1084fee21e36SBarry Smith 
1085c7afd0dbSLois Curfman McInnes    Input Parameters:
1086c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1087c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1088de044059SHong Zhang .  func - function evaluation routine
1089c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1090c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
10919b94acceSBarry Smith 
1092c7afd0dbSLois Curfman McInnes    Calling sequence of func:
10938d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1094c7afd0dbSLois Curfman McInnes 
1095313e4042SLois Curfman McInnes .  f - function vector
1096c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
10979b94acceSBarry Smith 
10989b94acceSBarry Smith    Notes:
10999b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
11009b94acceSBarry Smith $      f'(x) x = -f(x),
1101c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
11029b94acceSBarry Smith 
110336851e7fSLois Curfman McInnes    Level: beginner
110436851e7fSLois Curfman McInnes 
11059b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
11069b94acceSBarry Smith 
1107a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
11089b94acceSBarry Smith @*/
11097087cfbeSBarry Smith PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
11109b94acceSBarry Smith {
111185385478SLisandro Dalcin   PetscErrorCode ierr;
11123a40ed3dSBarry Smith   PetscFunctionBegin;
11130700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1114d2a683ecSLisandro Dalcin   if (r) {
1115d2a683ecSLisandro Dalcin     PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1116d2a683ecSLisandro Dalcin     PetscCheckSameComm(snes,1,r,2);
111785385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
11186bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
111985385478SLisandro Dalcin     snes->vec_func = r;
1120d2a683ecSLisandro Dalcin   } else if (!snes->vec_func && snes->dm) {
1121d2a683ecSLisandro Dalcin     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1122d2a683ecSLisandro Dalcin   }
1123d2a683ecSLisandro Dalcin   if (func) snes->ops->computefunction = func;
1124d2a683ecSLisandro Dalcin   if (ctx)  snes->funP                 = ctx;
11253a40ed3dSBarry Smith   PetscFunctionReturn(0);
11269b94acceSBarry Smith }
11279b94acceSBarry Smith 
1128d25893d9SBarry Smith #undef __FUNCT__
1129d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess"
1130d25893d9SBarry Smith /*@C
1131d25893d9SBarry Smith    SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem
1132d25893d9SBarry Smith 
1133d25893d9SBarry Smith    Logically Collective on SNES
1134d25893d9SBarry Smith 
1135d25893d9SBarry Smith    Input Parameters:
1136d25893d9SBarry Smith +  snes - the SNES context
1137d25893d9SBarry Smith .  func - function evaluation routine
1138d25893d9SBarry Smith -  ctx - [optional] user-defined context for private data for the
1139d25893d9SBarry Smith          function evaluation routine (may be PETSC_NULL)
1140d25893d9SBarry Smith 
1141d25893d9SBarry Smith    Calling sequence of func:
1142d25893d9SBarry Smith $    func (SNES snes,Vec x,void *ctx);
1143d25893d9SBarry Smith 
1144d25893d9SBarry Smith .  f - function vector
1145d25893d9SBarry Smith -  ctx - optional user-defined function context
1146d25893d9SBarry Smith 
1147d25893d9SBarry Smith    Level: intermediate
1148d25893d9SBarry Smith 
1149d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function
1150d25893d9SBarry Smith 
1151d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
1152d25893d9SBarry Smith @*/
1153d25893d9SBarry Smith PetscErrorCode  SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx)
1154d25893d9SBarry Smith {
1155d25893d9SBarry Smith   PetscFunctionBegin;
1156d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1157d25893d9SBarry Smith   if (func) snes->ops->computeinitialguess = func;
1158d25893d9SBarry Smith   if (ctx)  snes->initialguessP            = ctx;
1159d25893d9SBarry Smith   PetscFunctionReturn(0);
1160d25893d9SBarry Smith }
1161d25893d9SBarry Smith 
11623ab0aad5SBarry Smith /* --------------------------------------------------------------- */
11633ab0aad5SBarry Smith #undef __FUNCT__
11641096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
11651096aae1SMatthew Knepley /*@C
11661096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
11671096aae1SMatthew Knepley    it assumes a zero right hand side.
11681096aae1SMatthew Knepley 
11693f9fe445SBarry Smith    Logically Collective on SNES
11701096aae1SMatthew Knepley 
11711096aae1SMatthew Knepley    Input Parameter:
11721096aae1SMatthew Knepley .  snes - the SNES context
11731096aae1SMatthew Knepley 
11741096aae1SMatthew Knepley    Output Parameter:
1175bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
11761096aae1SMatthew Knepley 
11771096aae1SMatthew Knepley    Level: intermediate
11781096aae1SMatthew Knepley 
11791096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
11801096aae1SMatthew Knepley 
118185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
11821096aae1SMatthew Knepley @*/
11837087cfbeSBarry Smith PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
11841096aae1SMatthew Knepley {
11851096aae1SMatthew Knepley   PetscFunctionBegin;
11860700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11871096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
118885385478SLisandro Dalcin   *rhs = snes->vec_rhs;
11891096aae1SMatthew Knepley   PetscFunctionReturn(0);
11901096aae1SMatthew Knepley }
11911096aae1SMatthew Knepley 
11921096aae1SMatthew Knepley #undef __FUNCT__
11934a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
11949b94acceSBarry Smith /*@
119536851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
11969b94acceSBarry Smith                          SNESSetFunction().
11979b94acceSBarry Smith 
1198c7afd0dbSLois Curfman McInnes    Collective on SNES
1199c7afd0dbSLois Curfman McInnes 
12009b94acceSBarry Smith    Input Parameters:
1201c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1202c7afd0dbSLois Curfman McInnes -  x - input vector
12039b94acceSBarry Smith 
12049b94acceSBarry Smith    Output Parameter:
12053638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
12069b94acceSBarry Smith 
12071bffabb2SLois Curfman McInnes    Notes:
120836851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
120936851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
121036851e7fSLois Curfman McInnes    themselves.
121136851e7fSLois Curfman McInnes 
121236851e7fSLois Curfman McInnes    Level: developer
121336851e7fSLois Curfman McInnes 
12149b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
12159b94acceSBarry Smith 
1216a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
12179b94acceSBarry Smith @*/
12187087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
12199b94acceSBarry Smith {
1220dfbe8321SBarry Smith   PetscErrorCode ierr;
12219b94acceSBarry Smith 
12223a40ed3dSBarry Smith   PetscFunctionBegin;
12230700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12240700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
12250700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1226c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1227c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1228184914b5SBarry Smith 
1229d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1230e7788613SBarry Smith   if (snes->ops->computefunction) {
1231d64ed03dSBarry Smith     PetscStackPush("SNES user function");
123239d508bbSBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1233d64ed03dSBarry Smith     PetscStackPop;
123485385478SLisandro Dalcin   } else if (snes->vec_rhs) {
12351096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
123617186662SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
123785385478SLisandro Dalcin   if (snes->vec_rhs) {
123885385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
12393ab0aad5SBarry Smith   }
1240ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1241d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
12423a40ed3dSBarry Smith   PetscFunctionReturn(0);
12439b94acceSBarry Smith }
12449b94acceSBarry Smith 
12454a2ae208SSatish Balay #undef __FUNCT__
12464a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
124762fef451SLois Curfman McInnes /*@
124862fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
124962fef451SLois Curfman McInnes    set with SNESSetJacobian().
125062fef451SLois Curfman McInnes 
1251c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1252c7afd0dbSLois Curfman McInnes 
125362fef451SLois Curfman McInnes    Input Parameters:
1254c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1255c7afd0dbSLois Curfman McInnes -  x - input vector
125662fef451SLois Curfman McInnes 
125762fef451SLois Curfman McInnes    Output Parameters:
1258c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
125962fef451SLois Curfman McInnes .  B - optional preconditioning matrix
12602b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1261fee21e36SBarry Smith 
1262e35cf81dSBarry Smith   Options Database Keys:
1263e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1264693365a8SJed Brown .    -snes_lag_jacobian <lag>
1265693365a8SJed Brown .    -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences
1266693365a8SJed Brown .    -snes_compare_explicit_draw  - Compare the computed Jacobian to the finite difference Jacobian and draw the result
1267693365a8SJed Brown .    -snes_compare_explicit_contour  - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result
1268693365a8SJed Brown -    -snes_compare_operator  - Make the comparison options above use the operator instead of the preconditioning matrix
1269e35cf81dSBarry Smith 
127062fef451SLois Curfman McInnes    Notes:
127162fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
127262fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
127362fef451SLois Curfman McInnes 
127494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1275dc5a77f8SLois Curfman McInnes    flag parameter.
127662fef451SLois Curfman McInnes 
127736851e7fSLois Curfman McInnes    Level: developer
127836851e7fSLois Curfman McInnes 
127962fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
128062fef451SLois Curfman McInnes 
1281e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
128262fef451SLois Curfman McInnes @*/
12837087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
12849b94acceSBarry Smith {
1285dfbe8321SBarry Smith   PetscErrorCode ierr;
1286ace3abfcSBarry Smith   PetscBool      flag;
12873a40ed3dSBarry Smith 
12883a40ed3dSBarry Smith   PetscFunctionBegin;
12890700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12900700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
12914482741eSBarry Smith   PetscValidPointer(flg,5);
1292c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1293e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1294ebd3b9afSBarry Smith 
1295ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1296ebd3b9afSBarry Smith 
1297fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1298fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1299fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1300fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1301e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1302e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1303ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1304ebd3b9afSBarry Smith     if (flag) {
1305ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1306ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1307ebd3b9afSBarry Smith     }
1308e35cf81dSBarry Smith     PetscFunctionReturn(0);
1309e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1310e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1311e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1312ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1313ebd3b9afSBarry Smith     if (flag) {
1314ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1315ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1316ebd3b9afSBarry Smith     }
1317e35cf81dSBarry Smith     PetscFunctionReturn(0);
1318e35cf81dSBarry Smith   }
1319e35cf81dSBarry Smith 
1320c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1321e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1322d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1323e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1324d64ed03dSBarry Smith   PetscStackPop;
1325d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1326a8054027SBarry Smith 
13273b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
13283b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
13293b4f5425SBarry Smith     snes->lagpreconditioner = -1;
13303b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1331a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1332a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1333a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1334a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1335a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1336a8054027SBarry Smith   }
1337a8054027SBarry Smith 
13386d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
13390700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
13400700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
1341693365a8SJed Brown   {
1342693365a8SJed Brown     PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE;
1343693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr);
1344693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr);
1345693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr);
1346693365a8SJed Brown     ierr  = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr);
1347693365a8SJed Brown     if (flag || flag_draw || flag_contour) {
1348693365a8SJed Brown       Mat Bexp_mine = PETSC_NULL,Bexp,FDexp;
1349693365a8SJed Brown       MatStructure mstruct;
1350693365a8SJed Brown       PetscViewer vdraw,vstdout;
13516b3a5b13SJed Brown       PetscBool flg;
1352693365a8SJed Brown       if (flag_operator) {
1353693365a8SJed Brown         ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr);
1354693365a8SJed Brown         Bexp = Bexp_mine;
1355693365a8SJed Brown       } else {
1356693365a8SJed Brown         /* See if the preconditioning matrix can be viewed and added directly */
1357693365a8SJed Brown         ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr);
1358693365a8SJed Brown         if (flg) Bexp = *B;
1359693365a8SJed Brown         else {
1360693365a8SJed Brown           /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */
1361693365a8SJed Brown           ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr);
1362693365a8SJed Brown           Bexp = Bexp_mine;
1363693365a8SJed Brown         }
1364693365a8SJed Brown       }
1365693365a8SJed Brown       ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr);
1366693365a8SJed Brown       ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr);
1367693365a8SJed Brown       ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr);
1368693365a8SJed Brown       if (flag_draw || flag_contour) {
1369693365a8SJed Brown         ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr);
1370693365a8SJed Brown         if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);}
1371693365a8SJed Brown       } else vdraw = PETSC_NULL;
1372693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr);
1373693365a8SJed Brown       if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);}
1374693365a8SJed Brown       if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);}
1375693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr);
1376693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1377693365a8SJed Brown       if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);}
1378693365a8SJed Brown       ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
1379693365a8SJed Brown       ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr);
1380693365a8SJed Brown       if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);}
1381693365a8SJed Brown       if (vdraw) {              /* Always use contour for the difference */
1382693365a8SJed Brown         ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
1383693365a8SJed Brown         ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);
1384693365a8SJed Brown         ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);
1385693365a8SJed Brown       }
1386693365a8SJed Brown       if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);}
1387693365a8SJed Brown       ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr);
1388693365a8SJed Brown       ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr);
1389693365a8SJed Brown       ierr = MatDestroy(&FDexp);CHKERRQ(ierr);
1390693365a8SJed Brown     }
1391693365a8SJed Brown   }
13923a40ed3dSBarry Smith   PetscFunctionReturn(0);
13939b94acceSBarry Smith }
13949b94acceSBarry Smith 
13954a2ae208SSatish Balay #undef __FUNCT__
13964a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
13979b94acceSBarry Smith /*@C
13989b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1399044dda88SLois Curfman McInnes    location to store the matrix.
14009b94acceSBarry Smith 
14013f9fe445SBarry Smith    Logically Collective on SNES and Mat
1402c7afd0dbSLois Curfman McInnes 
14039b94acceSBarry Smith    Input Parameters:
1404c7afd0dbSLois Curfman McInnes +  snes - the SNES context
14059b94acceSBarry Smith .  A - Jacobian matrix
14069b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
1407efd51863SBarry Smith .  func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value)
1408c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1409efd51863SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value)
14109b94acceSBarry Smith 
14119b94acceSBarry Smith    Calling sequence of func:
14128d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
14139b94acceSBarry Smith 
1414c7afd0dbSLois Curfman McInnes +  x - input vector
14159b94acceSBarry Smith .  A - Jacobian matrix
14169b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1417ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
14182b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1419c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
14209b94acceSBarry Smith 
14219b94acceSBarry Smith    Notes:
142294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
14232cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1424ac21db08SLois Curfman McInnes 
1425ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
14269b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
14279b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
14289b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
14299b94acceSBarry Smith    throughout the global iterations.
14309b94acceSBarry Smith 
143116913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
143216913363SBarry Smith    each matrix.
143316913363SBarry Smith 
1434a8a26c1eSJed Brown    If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument
1435a8a26c1eSJed Brown    must be a MatFDColoring.
1436a8a26c1eSJed Brown 
143736851e7fSLois Curfman McInnes    Level: beginner
143836851e7fSLois Curfman McInnes 
14399b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
14409b94acceSBarry Smith 
14413ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
14429b94acceSBarry Smith @*/
14437087cfbeSBarry Smith PetscErrorCode  SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
14449b94acceSBarry Smith {
1445dfbe8321SBarry Smith   PetscErrorCode ierr;
14463a7fca6bSBarry Smith 
14473a40ed3dSBarry Smith   PetscFunctionBegin;
14480700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14490700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
14500700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1451c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
145206975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1453e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
14543a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
14553a7fca6bSBarry Smith   if (A) {
14567dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
14576bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
14589b94acceSBarry Smith     snes->jacobian = A;
14593a7fca6bSBarry Smith   }
14603a7fca6bSBarry Smith   if (B) {
14617dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
14626bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
14639b94acceSBarry Smith     snes->jacobian_pre = B;
14643a7fca6bSBarry Smith   }
14653a40ed3dSBarry Smith   PetscFunctionReturn(0);
14669b94acceSBarry Smith }
146762fef451SLois Curfman McInnes 
14684a2ae208SSatish Balay #undef __FUNCT__
14694a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1470c2aafc4cSSatish Balay /*@C
1471b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1472b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1473b4fd4287SBarry Smith 
1474c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1475c7afd0dbSLois Curfman McInnes 
1476b4fd4287SBarry Smith    Input Parameter:
1477b4fd4287SBarry Smith .  snes - the nonlinear solver context
1478b4fd4287SBarry Smith 
1479b4fd4287SBarry Smith    Output Parameters:
1480c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1481b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
148270e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
148370e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1484fee21e36SBarry Smith 
148536851e7fSLois Curfman McInnes    Level: advanced
148636851e7fSLois Curfman McInnes 
1487b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1488b4fd4287SBarry Smith @*/
14897087cfbeSBarry Smith PetscErrorCode  SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1490b4fd4287SBarry Smith {
14913a40ed3dSBarry Smith   PetscFunctionBegin;
14920700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1493b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1494b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1495e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
149670e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
14973a40ed3dSBarry Smith   PetscFunctionReturn(0);
1498b4fd4287SBarry Smith }
1499b4fd4287SBarry Smith 
15009b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
15019b94acceSBarry Smith 
15024a2ae208SSatish Balay #undef __FUNCT__
15034a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
15049b94acceSBarry Smith /*@
15059b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1506272ac6f2SLois Curfman McInnes    of a nonlinear solver.
15079b94acceSBarry Smith 
1508fee21e36SBarry Smith    Collective on SNES
1509fee21e36SBarry Smith 
1510c7afd0dbSLois Curfman McInnes    Input Parameters:
151170e92668SMatthew Knepley .  snes - the SNES context
1512c7afd0dbSLois Curfman McInnes 
1513272ac6f2SLois Curfman McInnes    Notes:
1514272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1515272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1516272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1517272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1518272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1519272ac6f2SLois Curfman McInnes 
152036851e7fSLois Curfman McInnes    Level: advanced
152136851e7fSLois Curfman McInnes 
15229b94acceSBarry Smith .keywords: SNES, nonlinear, setup
15239b94acceSBarry Smith 
15249b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
15259b94acceSBarry Smith @*/
15267087cfbeSBarry Smith PetscErrorCode  SNESSetUp(SNES snes)
15279b94acceSBarry Smith {
1528dfbe8321SBarry Smith   PetscErrorCode ierr;
15293a40ed3dSBarry Smith 
15303a40ed3dSBarry Smith   PetscFunctionBegin;
15310700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
15324dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
15339b94acceSBarry Smith 
15347adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
153585385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
153685385478SLisandro Dalcin   }
153785385478SLisandro Dalcin 
1538efd51863SBarry Smith   if (!snes->vec_func && snes->dm && !snes->vec_rhs) {
1539efd51863SBarry Smith     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1540efd51863SBarry Smith   }
154117186662SBarry Smith   if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
154217186662SBarry Smith   if (!snes->ops->computefunction && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
154317186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
154458c9b817SLisandro Dalcin   if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
154558c9b817SLisandro Dalcin 
154658c9b817SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
154758c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_rhs, &snes->vec_func);CHKERRQ(ierr);
154858c9b817SLisandro Dalcin   }
154958c9b817SLisandro Dalcin   if (!snes->vec_sol_update /* && snes->vec_sol */) {
155058c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr);
155158c9b817SLisandro Dalcin     ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr);
155258c9b817SLisandro Dalcin   }
155358c9b817SLisandro Dalcin 
1554ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1555ef8dffc7SBarry Smith     Mat           J;
1556ef8dffc7SBarry Smith     ISColoring    coloring;
1557ef8dffc7SBarry Smith     MatFDColoring fd;
1558a703fe33SLois Curfman McInnes 
1559ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1560ef8dffc7SBarry Smith     ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
1561ef8dffc7SBarry Smith     ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr);
1562ef8dffc7SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr);
1563ef8dffc7SBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr);
15646bf464f9SBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
1565efd51863SBarry Smith   } else if (snes->dm && !snes->jacobian_pre){
1566efd51863SBarry Smith     Mat J;
1567efd51863SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1568efd51863SBarry Smith     ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1569efd51863SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1570ef8dffc7SBarry Smith   }
1571efd51863SBarry Smith 
1572b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1573b710008aSBarry Smith 
1574d25893d9SBarry Smith   if (snes->ops->usercompute && !snes->user) {
1575d25893d9SBarry Smith     ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr);
1576d25893d9SBarry Smith   }
1577d25893d9SBarry Smith 
1578410397dcSLisandro Dalcin   if (snes->ops->setup) {
1579410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1580410397dcSLisandro Dalcin   }
158158c9b817SLisandro Dalcin 
15827aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
15833a40ed3dSBarry Smith   PetscFunctionReturn(0);
15849b94acceSBarry Smith }
15859b94acceSBarry Smith 
15864a2ae208SSatish Balay #undef __FUNCT__
158737596af1SLisandro Dalcin #define __FUNCT__ "SNESReset"
158837596af1SLisandro Dalcin /*@
158937596af1SLisandro Dalcin    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats
159037596af1SLisandro Dalcin 
159137596af1SLisandro Dalcin    Collective on SNES
159237596af1SLisandro Dalcin 
159337596af1SLisandro Dalcin    Input Parameter:
159437596af1SLisandro Dalcin .  snes - iterative context obtained from SNESCreate()
159537596af1SLisandro Dalcin 
1596d25893d9SBarry Smith    Level: intermediate
1597d25893d9SBarry Smith 
1598d25893d9SBarry Smith    Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext()
159937596af1SLisandro Dalcin 
160037596af1SLisandro Dalcin .keywords: SNES, destroy
160137596af1SLisandro Dalcin 
160237596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
160337596af1SLisandro Dalcin @*/
160437596af1SLisandro Dalcin PetscErrorCode  SNESReset(SNES snes)
160537596af1SLisandro Dalcin {
160637596af1SLisandro Dalcin   PetscErrorCode ierr;
160737596af1SLisandro Dalcin 
160837596af1SLisandro Dalcin   PetscFunctionBegin;
160937596af1SLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1610d25893d9SBarry Smith   if (snes->ops->userdestroy && snes->user) {
1611d25893d9SBarry Smith     ierr       = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr);
1612d25893d9SBarry Smith     snes->user = PETSC_NULL;
1613d25893d9SBarry Smith   }
1614d25893d9SBarry Smith 
161537596af1SLisandro Dalcin   if (snes->ops->reset) {
161637596af1SLisandro Dalcin     ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr);
161737596af1SLisandro Dalcin   }
161837596af1SLisandro Dalcin   if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);}
16196bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
16206bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
16216bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr);
16226bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
16236bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
16246bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
162537596af1SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
162637596af1SLisandro Dalcin   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
162737596af1SLisandro Dalcin   snes->nwork = snes->nvwork = 0;
162837596af1SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
162937596af1SLisandro Dalcin   PetscFunctionReturn(0);
163037596af1SLisandro Dalcin }
163137596af1SLisandro Dalcin 
163237596af1SLisandro Dalcin #undef __FUNCT__
16334a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
163452baeb72SSatish Balay /*@
16359b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
16369b94acceSBarry Smith    with SNESCreate().
16379b94acceSBarry Smith 
1638c7afd0dbSLois Curfman McInnes    Collective on SNES
1639c7afd0dbSLois Curfman McInnes 
16409b94acceSBarry Smith    Input Parameter:
16419b94acceSBarry Smith .  snes - the SNES context
16429b94acceSBarry Smith 
164336851e7fSLois Curfman McInnes    Level: beginner
164436851e7fSLois Curfman McInnes 
16459b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
16469b94acceSBarry Smith 
164763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
16489b94acceSBarry Smith @*/
16496bf464f9SBarry Smith PetscErrorCode  SNESDestroy(SNES *snes)
16509b94acceSBarry Smith {
16516849ba73SBarry Smith   PetscErrorCode ierr;
16523a40ed3dSBarry Smith 
16533a40ed3dSBarry Smith   PetscFunctionBegin;
16546bf464f9SBarry Smith   if (!*snes) PetscFunctionReturn(0);
16556bf464f9SBarry Smith   PetscValidHeaderSpecific((*snes),SNES_CLASSID,1);
16566bf464f9SBarry Smith   if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);}
1657d4bb536fSBarry Smith 
16586bf464f9SBarry Smith   ierr = SNESReset((*snes));CHKERRQ(ierr);
16596b8b9a38SLisandro Dalcin 
1660be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
16616bf464f9SBarry Smith   ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr);
16626bf464f9SBarry Smith   if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);}
16636d4c513bSLisandro Dalcin 
16646bf464f9SBarry Smith   ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr);
16656bf464f9SBarry Smith   ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr);
16666b8b9a38SLisandro Dalcin 
16676bf464f9SBarry Smith   ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr);
16686bf464f9SBarry Smith   if ((*snes)->ops->convergeddestroy) {
16696bf464f9SBarry Smith     ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr);
16706b8b9a38SLisandro Dalcin   }
16716bf464f9SBarry Smith   if ((*snes)->conv_malloc) {
16726bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr);
16736bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr);
167458c9b817SLisandro Dalcin   }
16756bf464f9SBarry Smith   ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr);
1676a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
16773a40ed3dSBarry Smith   PetscFunctionReturn(0);
16789b94acceSBarry Smith }
16799b94acceSBarry Smith 
16809b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
16819b94acceSBarry Smith 
16824a2ae208SSatish Balay #undef __FUNCT__
1683a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1684a8054027SBarry Smith /*@
1685a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1686a8054027SBarry Smith 
16873f9fe445SBarry Smith    Logically Collective on SNES
1688a8054027SBarry Smith 
1689a8054027SBarry Smith    Input Parameters:
1690a8054027SBarry Smith +  snes - the SNES context
1691a8054027SBarry 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
16923b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1693a8054027SBarry Smith 
1694a8054027SBarry Smith    Options Database Keys:
1695a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1696a8054027SBarry Smith 
1697a8054027SBarry Smith    Notes:
1698a8054027SBarry Smith    The default is 1
1699a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1700a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1701a8054027SBarry Smith 
1702a8054027SBarry Smith    Level: intermediate
1703a8054027SBarry Smith 
1704a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1705a8054027SBarry Smith 
1706e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1707a8054027SBarry Smith 
1708a8054027SBarry Smith @*/
17097087cfbeSBarry Smith PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1710a8054027SBarry Smith {
1711a8054027SBarry Smith   PetscFunctionBegin;
17120700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1713e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1714e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1715c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1716a8054027SBarry Smith   snes->lagpreconditioner = lag;
1717a8054027SBarry Smith   PetscFunctionReturn(0);
1718a8054027SBarry Smith }
1719a8054027SBarry Smith 
1720a8054027SBarry Smith #undef __FUNCT__
1721efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence"
1722efd51863SBarry Smith /*@
1723efd51863SBarry Smith    SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does
1724efd51863SBarry Smith 
1725efd51863SBarry Smith    Logically Collective on SNES
1726efd51863SBarry Smith 
1727efd51863SBarry Smith    Input Parameters:
1728efd51863SBarry Smith +  snes - the SNES context
1729efd51863SBarry Smith -  steps - the number of refinements to do, defaults to 0
1730efd51863SBarry Smith 
1731efd51863SBarry Smith    Options Database Keys:
1732efd51863SBarry Smith .    -snes_grid_sequence <steps>
1733efd51863SBarry Smith 
1734efd51863SBarry Smith    Level: intermediate
1735efd51863SBarry Smith 
1736efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1737efd51863SBarry Smith 
1738efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1739efd51863SBarry Smith 
1740efd51863SBarry Smith @*/
1741efd51863SBarry Smith PetscErrorCode  SNESSetGridSequence(SNES snes,PetscInt steps)
1742efd51863SBarry Smith {
1743efd51863SBarry Smith   PetscFunctionBegin;
1744efd51863SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1745efd51863SBarry Smith   PetscValidLogicalCollectiveInt(snes,steps,2);
1746efd51863SBarry Smith   snes->gridsequence = steps;
1747efd51863SBarry Smith   PetscFunctionReturn(0);
1748efd51863SBarry Smith }
1749efd51863SBarry Smith 
1750efd51863SBarry Smith #undef __FUNCT__
1751a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1752a8054027SBarry Smith /*@
1753a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1754a8054027SBarry Smith 
17553f9fe445SBarry Smith    Not Collective
1756a8054027SBarry Smith 
1757a8054027SBarry Smith    Input Parameter:
1758a8054027SBarry Smith .  snes - the SNES context
1759a8054027SBarry Smith 
1760a8054027SBarry Smith    Output Parameter:
1761a8054027SBarry 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
17623b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1763a8054027SBarry Smith 
1764a8054027SBarry Smith    Options Database Keys:
1765a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1766a8054027SBarry Smith 
1767a8054027SBarry Smith    Notes:
1768a8054027SBarry Smith    The default is 1
1769a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1770a8054027SBarry Smith 
1771a8054027SBarry Smith    Level: intermediate
1772a8054027SBarry Smith 
1773a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1774a8054027SBarry Smith 
1775a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1776a8054027SBarry Smith 
1777a8054027SBarry Smith @*/
17787087cfbeSBarry Smith PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1779a8054027SBarry Smith {
1780a8054027SBarry Smith   PetscFunctionBegin;
17810700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1782a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1783a8054027SBarry Smith   PetscFunctionReturn(0);
1784a8054027SBarry Smith }
1785a8054027SBarry Smith 
1786a8054027SBarry Smith #undef __FUNCT__
1787e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1788e35cf81dSBarry Smith /*@
1789e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1790e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1791e35cf81dSBarry Smith 
17923f9fe445SBarry Smith    Logically Collective on SNES
1793e35cf81dSBarry Smith 
1794e35cf81dSBarry Smith    Input Parameters:
1795e35cf81dSBarry Smith +  snes - the SNES context
1796e35cf81dSBarry 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
1797fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1798e35cf81dSBarry Smith 
1799e35cf81dSBarry Smith    Options Database Keys:
1800e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1801e35cf81dSBarry Smith 
1802e35cf81dSBarry Smith    Notes:
1803e35cf81dSBarry Smith    The default is 1
1804e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1805fe3ffe1eSBarry 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
1806fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1807e35cf81dSBarry Smith 
1808e35cf81dSBarry Smith    Level: intermediate
1809e35cf81dSBarry Smith 
1810e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1811e35cf81dSBarry Smith 
1812e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1813e35cf81dSBarry Smith 
1814e35cf81dSBarry Smith @*/
18157087cfbeSBarry Smith PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
1816e35cf81dSBarry Smith {
1817e35cf81dSBarry Smith   PetscFunctionBegin;
18180700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1819e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1820e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1821c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1822e35cf81dSBarry Smith   snes->lagjacobian = lag;
1823e35cf81dSBarry Smith   PetscFunctionReturn(0);
1824e35cf81dSBarry Smith }
1825e35cf81dSBarry Smith 
1826e35cf81dSBarry Smith #undef __FUNCT__
1827e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1828e35cf81dSBarry Smith /*@
1829e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1830e35cf81dSBarry Smith 
18313f9fe445SBarry Smith    Not Collective
1832e35cf81dSBarry Smith 
1833e35cf81dSBarry Smith    Input Parameter:
1834e35cf81dSBarry Smith .  snes - the SNES context
1835e35cf81dSBarry Smith 
1836e35cf81dSBarry Smith    Output Parameter:
1837e35cf81dSBarry 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
1838e35cf81dSBarry Smith          the Jacobian is built etc.
1839e35cf81dSBarry Smith 
1840e35cf81dSBarry Smith    Options Database Keys:
1841e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1842e35cf81dSBarry Smith 
1843e35cf81dSBarry Smith    Notes:
1844e35cf81dSBarry Smith    The default is 1
1845e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1846e35cf81dSBarry Smith 
1847e35cf81dSBarry Smith    Level: intermediate
1848e35cf81dSBarry Smith 
1849e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1850e35cf81dSBarry Smith 
1851e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1852e35cf81dSBarry Smith 
1853e35cf81dSBarry Smith @*/
18547087cfbeSBarry Smith PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
1855e35cf81dSBarry Smith {
1856e35cf81dSBarry Smith   PetscFunctionBegin;
18570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1858e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1859e35cf81dSBarry Smith   PetscFunctionReturn(0);
1860e35cf81dSBarry Smith }
1861e35cf81dSBarry Smith 
1862e35cf81dSBarry Smith #undef __FUNCT__
18634a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
18649b94acceSBarry Smith /*@
1865d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
18669b94acceSBarry Smith 
18673f9fe445SBarry Smith    Logically Collective on SNES
1868c7afd0dbSLois Curfman McInnes 
18699b94acceSBarry Smith    Input Parameters:
1870c7afd0dbSLois Curfman McInnes +  snes - the SNES context
187170441072SBarry Smith .  abstol - absolute convergence tolerance
187233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
187333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
187433174efeSLois Curfman McInnes            of the change in the solution between steps
187533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1876c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1877fee21e36SBarry Smith 
187833174efeSLois Curfman McInnes    Options Database Keys:
187970441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1880c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1881c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1882c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1883c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
18849b94acceSBarry Smith 
1885d7a720efSLois Curfman McInnes    Notes:
18869b94acceSBarry Smith    The default maximum number of iterations is 50.
18879b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
18889b94acceSBarry Smith 
188936851e7fSLois Curfman McInnes    Level: intermediate
189036851e7fSLois Curfman McInnes 
189133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
18929b94acceSBarry Smith 
18932492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
18949b94acceSBarry Smith @*/
18957087cfbeSBarry Smith PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
18969b94acceSBarry Smith {
18973a40ed3dSBarry Smith   PetscFunctionBegin;
18980700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1899c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
1900c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
1901c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
1902c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
1903c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
1904c5eb9154SBarry Smith 
190570441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1906d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1907d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1908d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1909d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
19103a40ed3dSBarry Smith   PetscFunctionReturn(0);
19119b94acceSBarry Smith }
19129b94acceSBarry Smith 
19134a2ae208SSatish Balay #undef __FUNCT__
19144a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
19159b94acceSBarry Smith /*@
191633174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
191733174efeSLois Curfman McInnes 
1918c7afd0dbSLois Curfman McInnes    Not Collective
1919c7afd0dbSLois Curfman McInnes 
192033174efeSLois Curfman McInnes    Input Parameters:
1921c7afd0dbSLois Curfman McInnes +  snes - the SNES context
192285385478SLisandro Dalcin .  atol - absolute convergence tolerance
192333174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
192433174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
192533174efeSLois Curfman McInnes            of the change in the solution between steps
192633174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1927c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1928fee21e36SBarry Smith 
192933174efeSLois Curfman McInnes    Notes:
193033174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
193133174efeSLois Curfman McInnes 
193236851e7fSLois Curfman McInnes    Level: intermediate
193336851e7fSLois Curfman McInnes 
193433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
193533174efeSLois Curfman McInnes 
193633174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
193733174efeSLois Curfman McInnes @*/
19387087cfbeSBarry Smith PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
193933174efeSLois Curfman McInnes {
19403a40ed3dSBarry Smith   PetscFunctionBegin;
19410700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
194285385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
194333174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
194433174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
194533174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
194633174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
19473a40ed3dSBarry Smith   PetscFunctionReturn(0);
194833174efeSLois Curfman McInnes }
194933174efeSLois Curfman McInnes 
19504a2ae208SSatish Balay #undef __FUNCT__
19514a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
195233174efeSLois Curfman McInnes /*@
19539b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
19549b94acceSBarry Smith 
19553f9fe445SBarry Smith    Logically Collective on SNES
1956fee21e36SBarry Smith 
1957c7afd0dbSLois Curfman McInnes    Input Parameters:
1958c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1959c7afd0dbSLois Curfman McInnes -  tol - tolerance
1960c7afd0dbSLois Curfman McInnes 
19619b94acceSBarry Smith    Options Database Key:
1962c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
19639b94acceSBarry Smith 
196436851e7fSLois Curfman McInnes    Level: intermediate
196536851e7fSLois Curfman McInnes 
19669b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
19679b94acceSBarry Smith 
19682492ecdbSBarry Smith .seealso: SNESSetTolerances()
19699b94acceSBarry Smith @*/
19707087cfbeSBarry Smith PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
19719b94acceSBarry Smith {
19723a40ed3dSBarry Smith   PetscFunctionBegin;
19730700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1974c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
19759b94acceSBarry Smith   snes->deltatol = tol;
19763a40ed3dSBarry Smith   PetscFunctionReturn(0);
19779b94acceSBarry Smith }
19789b94acceSBarry Smith 
1979df9fa365SBarry Smith /*
1980df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1981df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1982df9fa365SBarry Smith    macros instead of functions
1983df9fa365SBarry Smith */
19844a2ae208SSatish Balay #undef __FUNCT__
1985a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
19867087cfbeSBarry Smith PetscErrorCode  SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1987ce1608b8SBarry Smith {
1988dfbe8321SBarry Smith   PetscErrorCode ierr;
1989ce1608b8SBarry Smith 
1990ce1608b8SBarry Smith   PetscFunctionBegin;
19910700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1992a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1993ce1608b8SBarry Smith   PetscFunctionReturn(0);
1994ce1608b8SBarry Smith }
1995ce1608b8SBarry Smith 
19964a2ae208SSatish Balay #undef __FUNCT__
1997a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
19987087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1999df9fa365SBarry Smith {
2000dfbe8321SBarry Smith   PetscErrorCode ierr;
2001df9fa365SBarry Smith 
2002df9fa365SBarry Smith   PetscFunctionBegin;
2003a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2004df9fa365SBarry Smith   PetscFunctionReturn(0);
2005df9fa365SBarry Smith }
2006df9fa365SBarry Smith 
20074a2ae208SSatish Balay #undef __FUNCT__
2008a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
20096bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGDestroy(PetscDrawLG *draw)
2010df9fa365SBarry Smith {
2011dfbe8321SBarry Smith   PetscErrorCode ierr;
2012df9fa365SBarry Smith 
2013df9fa365SBarry Smith   PetscFunctionBegin;
2014a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2015df9fa365SBarry Smith   PetscFunctionReturn(0);
2016df9fa365SBarry Smith }
2017df9fa365SBarry Smith 
20187087cfbeSBarry Smith extern PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
2019b271bb04SBarry Smith #undef __FUNCT__
2020b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
20217087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
2022b271bb04SBarry Smith {
2023b271bb04SBarry Smith   PetscDrawLG      lg;
2024b271bb04SBarry Smith   PetscErrorCode   ierr;
2025b271bb04SBarry Smith   PetscReal        x,y,per;
2026b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
2027b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
2028b271bb04SBarry Smith   PetscDraw        draw;
2029b271bb04SBarry Smith   PetscFunctionBegin;
2030b271bb04SBarry Smith   if (!monctx) {
2031b271bb04SBarry Smith     MPI_Comm    comm;
2032b271bb04SBarry Smith 
2033b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
2034b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
2035b271bb04SBarry Smith   }
2036b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
2037b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2038b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2039b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
2040b271bb04SBarry Smith   x = (PetscReal) n;
2041b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
2042b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2043b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2044b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2045b271bb04SBarry Smith   }
2046b271bb04SBarry Smith 
2047b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
2048b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2049b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2050b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
2051b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
2052b271bb04SBarry Smith   x = (PetscReal) n;
2053b271bb04SBarry Smith   y = 100.0*per;
2054b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2055b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2056b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2057b271bb04SBarry Smith   }
2058b271bb04SBarry Smith 
2059b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
2060b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2061b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2062b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
2063b271bb04SBarry Smith   x = (PetscReal) n;
2064b271bb04SBarry Smith   y = (prev - rnorm)/prev;
2065b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2066b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2067b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2068b271bb04SBarry Smith   }
2069b271bb04SBarry Smith 
2070b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
2071b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2072b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2073b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
2074b271bb04SBarry Smith   x = (PetscReal) n;
2075b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
2076b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
2077b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2078b271bb04SBarry Smith   }
2079b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2080b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2081b271bb04SBarry Smith   }
2082b271bb04SBarry Smith   prev = rnorm;
2083b271bb04SBarry Smith   PetscFunctionReturn(0);
2084b271bb04SBarry Smith }
2085b271bb04SBarry Smith 
2086b271bb04SBarry Smith #undef __FUNCT__
2087b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
20887087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2089b271bb04SBarry Smith {
2090b271bb04SBarry Smith   PetscErrorCode ierr;
2091b271bb04SBarry Smith 
2092b271bb04SBarry Smith   PetscFunctionBegin;
2093b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2094b271bb04SBarry Smith   PetscFunctionReturn(0);
2095b271bb04SBarry Smith }
2096b271bb04SBarry Smith 
2097b271bb04SBarry Smith #undef __FUNCT__
2098b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
20996bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGRangeDestroy(PetscDrawLG *draw)
2100b271bb04SBarry Smith {
2101b271bb04SBarry Smith   PetscErrorCode ierr;
2102b271bb04SBarry Smith 
2103b271bb04SBarry Smith   PetscFunctionBegin;
2104b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2105b271bb04SBarry Smith   PetscFunctionReturn(0);
2106b271bb04SBarry Smith }
2107b271bb04SBarry Smith 
21087a03ce2fSLisandro Dalcin #undef __FUNCT__
21097a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor"
21107a03ce2fSLisandro Dalcin /*
21117a03ce2fSLisandro Dalcin      Runs the user provided monitor routines, if they exists.
21127a03ce2fSLisandro Dalcin */
21137a03ce2fSLisandro Dalcin PetscErrorCode  SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm)
21147a03ce2fSLisandro Dalcin {
21157a03ce2fSLisandro Dalcin   PetscErrorCode ierr;
21167a03ce2fSLisandro Dalcin   PetscInt       i,n = snes->numbermonitors;
21177a03ce2fSLisandro Dalcin 
21187a03ce2fSLisandro Dalcin   PetscFunctionBegin;
21197a03ce2fSLisandro Dalcin   for (i=0; i<n; i++) {
21207a03ce2fSLisandro Dalcin     ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr);
21217a03ce2fSLisandro Dalcin   }
21227a03ce2fSLisandro Dalcin   PetscFunctionReturn(0);
21237a03ce2fSLisandro Dalcin }
21247a03ce2fSLisandro Dalcin 
21259b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
21269b94acceSBarry Smith 
21274a2ae208SSatish Balay #undef __FUNCT__
2128a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
21299b94acceSBarry Smith /*@C
2130a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
21319b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
21329b94acceSBarry Smith    progress.
21339b94acceSBarry Smith 
21343f9fe445SBarry Smith    Logically Collective on SNES
2135fee21e36SBarry Smith 
2136c7afd0dbSLois Curfman McInnes    Input Parameters:
2137c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2138c7afd0dbSLois Curfman McInnes .  func - monitoring routine
2139b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
2140e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
2141b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2142b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
21439b94acceSBarry Smith 
2144c7afd0dbSLois Curfman McInnes    Calling sequence of func:
2145a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
2146c7afd0dbSLois Curfman McInnes 
2147c7afd0dbSLois Curfman McInnes +    snes - the SNES context
2148c7afd0dbSLois Curfman McInnes .    its - iteration number
2149c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
215040a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
21519b94acceSBarry Smith 
21529665c990SLois Curfman McInnes    Options Database Keys:
2153a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
2154a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
2155a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
2156cca6129bSJed Brown -    -snes_monitor_cancel - cancels all monitors that have
2157c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
2158a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
2159c7afd0dbSLois Curfman McInnes                             does not cancel those set via
2160c7afd0dbSLois Curfman McInnes                             the options database.
21619665c990SLois Curfman McInnes 
2162639f9d9dSBarry Smith    Notes:
21636bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
2164a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
21656bc08f3fSLois Curfman McInnes    order in which they were set.
2166639f9d9dSBarry Smith 
2167025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
2168025f1a04SBarry Smith 
216936851e7fSLois Curfman McInnes    Level: intermediate
217036851e7fSLois Curfman McInnes 
21719b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
21729b94acceSBarry Smith 
2173a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
21749b94acceSBarry Smith @*/
2175c2efdce3SBarry Smith PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
21769b94acceSBarry Smith {
2177b90d0a6eSBarry Smith   PetscInt i;
2178b90d0a6eSBarry Smith 
21793a40ed3dSBarry Smith   PetscFunctionBegin;
21800700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
218117186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2182b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
2183b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
2184b90d0a6eSBarry Smith 
2185b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
2186b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
2187b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
2188b90d0a6eSBarry Smith         PetscErrorCode          ierr;
2189b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
2190b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
2191b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
2192c2efdce3SBarry Smith           ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr);
2193b90d0a6eSBarry Smith           PetscFunctionReturn(0);
2194b90d0a6eSBarry Smith         }
2195b90d0a6eSBarry Smith       }
2196b90d0a6eSBarry Smith     }
2197b90d0a6eSBarry Smith   }
2198b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
2199b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
2200639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
22013a40ed3dSBarry Smith   PetscFunctionReturn(0);
22029b94acceSBarry Smith }
22039b94acceSBarry Smith 
22044a2ae208SSatish Balay #undef __FUNCT__
2205a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
22065cd90555SBarry Smith /*@C
2207a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
22085cd90555SBarry Smith 
22093f9fe445SBarry Smith    Logically Collective on SNES
2210c7afd0dbSLois Curfman McInnes 
22115cd90555SBarry Smith    Input Parameters:
22125cd90555SBarry Smith .  snes - the SNES context
22135cd90555SBarry Smith 
22141a480d89SAdministrator    Options Database Key:
2215a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
2216a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
2217c7afd0dbSLois Curfman McInnes     set via the options database
22185cd90555SBarry Smith 
22195cd90555SBarry Smith    Notes:
22205cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
22215cd90555SBarry Smith 
222236851e7fSLois Curfman McInnes    Level: intermediate
222336851e7fSLois Curfman McInnes 
22245cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
22255cd90555SBarry Smith 
2226a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
22275cd90555SBarry Smith @*/
22287087cfbeSBarry Smith PetscErrorCode  SNESMonitorCancel(SNES snes)
22295cd90555SBarry Smith {
2230d952e501SBarry Smith   PetscErrorCode ierr;
2231d952e501SBarry Smith   PetscInt       i;
2232d952e501SBarry Smith 
22335cd90555SBarry Smith   PetscFunctionBegin;
22340700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2235d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2236d952e501SBarry Smith     if (snes->monitordestroy[i]) {
22373c4aec1bSBarry Smith       ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr);
2238d952e501SBarry Smith     }
2239d952e501SBarry Smith   }
22405cd90555SBarry Smith   snes->numbermonitors = 0;
22415cd90555SBarry Smith   PetscFunctionReturn(0);
22425cd90555SBarry Smith }
22435cd90555SBarry Smith 
22444a2ae208SSatish Balay #undef __FUNCT__
22454a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
22469b94acceSBarry Smith /*@C
22479b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
22489b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
22499b94acceSBarry Smith 
22503f9fe445SBarry Smith    Logically Collective on SNES
2251fee21e36SBarry Smith 
2252c7afd0dbSLois Curfman McInnes    Input Parameters:
2253c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2254c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
22557f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
22567f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
22579b94acceSBarry Smith 
2258c7afd0dbSLois Curfman McInnes    Calling sequence of func:
225906ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2260c7afd0dbSLois Curfman McInnes 
2261c7afd0dbSLois Curfman McInnes +    snes - the SNES context
226206ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2263c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2264184914b5SBarry Smith .    reason - reason for convergence/divergence
2265c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
22664b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
22674b27c08aSLois Curfman McInnes -    f - 2-norm of function
22689b94acceSBarry Smith 
226936851e7fSLois Curfman McInnes    Level: advanced
227036851e7fSLois Curfman McInnes 
22719b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
22729b94acceSBarry Smith 
227385385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
22749b94acceSBarry Smith @*/
22757087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
22769b94acceSBarry Smith {
22777f7931b9SBarry Smith   PetscErrorCode ierr;
22787f7931b9SBarry Smith 
22793a40ed3dSBarry Smith   PetscFunctionBegin;
22800700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
228185385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
22827f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
22837f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
22847f7931b9SBarry Smith   }
228585385478SLisandro Dalcin   snes->ops->converged        = func;
22867f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
228785385478SLisandro Dalcin   snes->cnvP                  = cctx;
22883a40ed3dSBarry Smith   PetscFunctionReturn(0);
22899b94acceSBarry Smith }
22909b94acceSBarry Smith 
22914a2ae208SSatish Balay #undef __FUNCT__
22924a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
229352baeb72SSatish Balay /*@
2294184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2295184914b5SBarry Smith 
2296184914b5SBarry Smith    Not Collective
2297184914b5SBarry Smith 
2298184914b5SBarry Smith    Input Parameter:
2299184914b5SBarry Smith .  snes - the SNES context
2300184914b5SBarry Smith 
2301184914b5SBarry Smith    Output Parameter:
23024d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2303184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2304184914b5SBarry Smith 
2305184914b5SBarry Smith    Level: intermediate
2306184914b5SBarry Smith 
2307184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2308184914b5SBarry Smith 
2309184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2310184914b5SBarry Smith 
231185385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2312184914b5SBarry Smith @*/
23137087cfbeSBarry Smith PetscErrorCode  SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2314184914b5SBarry Smith {
2315184914b5SBarry Smith   PetscFunctionBegin;
23160700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23174482741eSBarry Smith   PetscValidPointer(reason,2);
2318184914b5SBarry Smith   *reason = snes->reason;
2319184914b5SBarry Smith   PetscFunctionReturn(0);
2320184914b5SBarry Smith }
2321184914b5SBarry Smith 
23224a2ae208SSatish Balay #undef __FUNCT__
23234a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2324c9005455SLois Curfman McInnes /*@
2325c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2326c9005455SLois Curfman McInnes 
23273f9fe445SBarry Smith    Logically Collective on SNES
2328fee21e36SBarry Smith 
2329c7afd0dbSLois Curfman McInnes    Input Parameters:
2330c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
23318c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2332cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2333758f92a0SBarry Smith .  na  - size of a and its
233464731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2335758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2336c7afd0dbSLois Curfman McInnes 
2337308dcc3eSBarry Smith    Notes:
2338308dcc3eSBarry Smith    If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2339308dcc3eSBarry Smith    default array of length 10000 is allocated.
2340308dcc3eSBarry Smith 
2341c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2342c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2343c9005455SLois Curfman McInnes    during the section of code that is being timed.
2344c9005455SLois Curfman McInnes 
234536851e7fSLois Curfman McInnes    Level: intermediate
234636851e7fSLois Curfman McInnes 
2347c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2348758f92a0SBarry Smith 
234908405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2350758f92a0SBarry Smith 
2351c9005455SLois Curfman McInnes @*/
23527087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool  reset)
2353c9005455SLois Curfman McInnes {
2354308dcc3eSBarry Smith   PetscErrorCode ierr;
2355308dcc3eSBarry Smith 
23563a40ed3dSBarry Smith   PetscFunctionBegin;
23570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23584482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2359a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2360308dcc3eSBarry Smith   if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) {
2361308dcc3eSBarry Smith     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2362308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr);
2363308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr);
2364308dcc3eSBarry Smith     snes->conv_malloc   = PETSC_TRUE;
2365308dcc3eSBarry Smith   }
2366c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2367758f92a0SBarry Smith   snes->conv_hist_its   = its;
2368758f92a0SBarry Smith   snes->conv_hist_max   = na;
2369a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2370758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2371758f92a0SBarry Smith   PetscFunctionReturn(0);
2372758f92a0SBarry Smith }
2373758f92a0SBarry Smith 
2374308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2375c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
2376c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
2377308dcc3eSBarry Smith EXTERN_C_BEGIN
2378308dcc3eSBarry Smith #undef __FUNCT__
2379308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab"
2380308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
2381308dcc3eSBarry Smith {
2382308dcc3eSBarry Smith   mxArray        *mat;
2383308dcc3eSBarry Smith   PetscInt       i;
2384308dcc3eSBarry Smith   PetscReal      *ar;
2385308dcc3eSBarry Smith 
2386308dcc3eSBarry Smith   PetscFunctionBegin;
2387308dcc3eSBarry Smith   mat  = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
2388308dcc3eSBarry Smith   ar   = (PetscReal*) mxGetData(mat);
2389308dcc3eSBarry Smith   for (i=0; i<snes->conv_hist_len; i++) {
2390308dcc3eSBarry Smith     ar[i] = snes->conv_hist[i];
2391308dcc3eSBarry Smith   }
2392308dcc3eSBarry Smith   PetscFunctionReturn(mat);
2393308dcc3eSBarry Smith }
2394308dcc3eSBarry Smith EXTERN_C_END
2395308dcc3eSBarry Smith #endif
2396308dcc3eSBarry Smith 
2397308dcc3eSBarry Smith 
23984a2ae208SSatish Balay #undef __FUNCT__
23994a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
24000c4c9dddSBarry Smith /*@C
2401758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2402758f92a0SBarry Smith 
24033f9fe445SBarry Smith    Not Collective
2404758f92a0SBarry Smith 
2405758f92a0SBarry Smith    Input Parameter:
2406758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2407758f92a0SBarry Smith 
2408758f92a0SBarry Smith    Output Parameters:
2409758f92a0SBarry Smith .  a   - array to hold history
2410758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2411758f92a0SBarry Smith          negative if not converged) for each solve.
2412758f92a0SBarry Smith -  na  - size of a and its
2413758f92a0SBarry Smith 
2414758f92a0SBarry Smith    Notes:
2415758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2416758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2417758f92a0SBarry Smith 
2418758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2419758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2420758f92a0SBarry Smith    during the section of code that is being timed.
2421758f92a0SBarry Smith 
2422758f92a0SBarry Smith    Level: intermediate
2423758f92a0SBarry Smith 
2424758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2425758f92a0SBarry Smith 
2426758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2427758f92a0SBarry Smith 
2428758f92a0SBarry Smith @*/
24297087cfbeSBarry Smith PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2430758f92a0SBarry Smith {
2431758f92a0SBarry Smith   PetscFunctionBegin;
24320700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2433758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2434758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2435758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
24363a40ed3dSBarry Smith   PetscFunctionReturn(0);
2437c9005455SLois Curfman McInnes }
2438c9005455SLois Curfman McInnes 
2439e74ef692SMatthew Knepley #undef __FUNCT__
2440e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2441ac226902SBarry Smith /*@C
244276b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
2443eec8f646SJed Brown   at the beginning of every iteration of the nonlinear solve. Specifically
24447e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
244576b2cf59SMatthew Knepley 
24463f9fe445SBarry Smith   Logically Collective on SNES
244776b2cf59SMatthew Knepley 
244876b2cf59SMatthew Knepley   Input Parameters:
244976b2cf59SMatthew Knepley . snes - The nonlinear solver context
245076b2cf59SMatthew Knepley . func - The function
245176b2cf59SMatthew Knepley 
245276b2cf59SMatthew Knepley   Calling sequence of func:
2453b5d30489SBarry Smith . func (SNES snes, PetscInt step);
245476b2cf59SMatthew Knepley 
245576b2cf59SMatthew Knepley . step - The current step of the iteration
245676b2cf59SMatthew Knepley 
2457fe97e370SBarry Smith   Level: advanced
2458fe97e370SBarry Smith 
2459fe97e370SBarry 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()
2460fe97e370SBarry Smith         This is not used by most users.
246176b2cf59SMatthew Knepley 
246276b2cf59SMatthew Knepley .keywords: SNES, update
2463b5d30489SBarry Smith 
246485385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
246576b2cf59SMatthew Knepley @*/
24667087cfbeSBarry Smith PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
246776b2cf59SMatthew Knepley {
246876b2cf59SMatthew Knepley   PetscFunctionBegin;
24690700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2470e7788613SBarry Smith   snes->ops->update = func;
247176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
247276b2cf59SMatthew Knepley }
247376b2cf59SMatthew Knepley 
2474e74ef692SMatthew Knepley #undef __FUNCT__
2475e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
247676b2cf59SMatthew Knepley /*@
247776b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
247876b2cf59SMatthew Knepley 
247976b2cf59SMatthew Knepley   Not collective
248076b2cf59SMatthew Knepley 
248176b2cf59SMatthew Knepley   Input Parameters:
248276b2cf59SMatthew Knepley . snes - The nonlinear solver context
248376b2cf59SMatthew Knepley . step - The current step of the iteration
248476b2cf59SMatthew Knepley 
2485205452f4SMatthew Knepley   Level: intermediate
2486205452f4SMatthew Knepley 
248776b2cf59SMatthew Knepley .keywords: SNES, update
2488a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
248976b2cf59SMatthew Knepley @*/
24907087cfbeSBarry Smith PetscErrorCode  SNESDefaultUpdate(SNES snes, PetscInt step)
249176b2cf59SMatthew Knepley {
249276b2cf59SMatthew Knepley   PetscFunctionBegin;
249376b2cf59SMatthew Knepley   PetscFunctionReturn(0);
249476b2cf59SMatthew Knepley }
249576b2cf59SMatthew Knepley 
24964a2ae208SSatish Balay #undef __FUNCT__
24974a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
24989b94acceSBarry Smith /*
24999b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
25009b94acceSBarry Smith    positive parameter delta.
25019b94acceSBarry Smith 
25029b94acceSBarry Smith     Input Parameters:
2503c7afd0dbSLois Curfman McInnes +   snes - the SNES context
25049b94acceSBarry Smith .   y - approximate solution of linear system
25059b94acceSBarry Smith .   fnorm - 2-norm of current function
2506c7afd0dbSLois Curfman McInnes -   delta - trust region size
25079b94acceSBarry Smith 
25089b94acceSBarry Smith     Output Parameters:
2509c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
25109b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
25119b94acceSBarry Smith     region, and exceeds zero otherwise.
2512c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
25139b94acceSBarry Smith 
25149b94acceSBarry Smith     Note:
25154b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
25169b94acceSBarry Smith     is set to be the maximum allowable step size.
25179b94acceSBarry Smith 
25189b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
25199b94acceSBarry Smith */
2520dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
25219b94acceSBarry Smith {
2522064f8208SBarry Smith   PetscReal      nrm;
2523ea709b57SSatish Balay   PetscScalar    cnorm;
2524dfbe8321SBarry Smith   PetscErrorCode ierr;
25253a40ed3dSBarry Smith 
25263a40ed3dSBarry Smith   PetscFunctionBegin;
25270700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25280700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2529c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2530184914b5SBarry Smith 
2531064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2532064f8208SBarry Smith   if (nrm > *delta) {
2533064f8208SBarry Smith      nrm = *delta/nrm;
2534064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2535064f8208SBarry Smith      cnorm = nrm;
25362dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
25379b94acceSBarry Smith      *ynorm = *delta;
25389b94acceSBarry Smith   } else {
25399b94acceSBarry Smith      *gpnorm = 0.0;
2540064f8208SBarry Smith      *ynorm = nrm;
25419b94acceSBarry Smith   }
25423a40ed3dSBarry Smith   PetscFunctionReturn(0);
25439b94acceSBarry Smith }
25449b94acceSBarry Smith 
25454a2ae208SSatish Balay #undef __FUNCT__
25464a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
25476ce558aeSBarry Smith /*@C
2548f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2549f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
25509b94acceSBarry Smith 
2551c7afd0dbSLois Curfman McInnes    Collective on SNES
2552c7afd0dbSLois Curfman McInnes 
2553b2002411SLois Curfman McInnes    Input Parameters:
2554c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2555f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
255685385478SLisandro Dalcin -  x - the solution vector.
25579b94acceSBarry Smith 
2558b2002411SLois Curfman McInnes    Notes:
25598ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
25608ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
25618ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
25628ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
25638ddd3da0SLois Curfman McInnes 
256436851e7fSLois Curfman McInnes    Level: beginner
256536851e7fSLois Curfman McInnes 
25669b94acceSBarry Smith .keywords: SNES, nonlinear, solve
25679b94acceSBarry Smith 
256885385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
25699b94acceSBarry Smith @*/
25707087cfbeSBarry Smith PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
25719b94acceSBarry Smith {
2572dfbe8321SBarry Smith   PetscErrorCode ierr;
2573ace3abfcSBarry Smith   PetscBool      flg;
2574eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2575eabae89aSBarry Smith   PetscViewer    viewer;
2576efd51863SBarry Smith   PetscInt       grid;
2577052efed2SBarry Smith 
25783a40ed3dSBarry Smith   PetscFunctionBegin;
25790700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25800700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2581f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
25820700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
258385385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
258485385478SLisandro Dalcin 
2585efd51863SBarry Smith   for (grid=0; grid<snes->gridsequence+1; grid++) {
2586efd51863SBarry Smith 
258785385478SLisandro Dalcin     /* set solution vector */
2588efd51863SBarry Smith     if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);}
25896bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
259085385478SLisandro Dalcin     snes->vec_sol = x;
259185385478SLisandro Dalcin     /* set afine vector if provided */
259285385478SLisandro Dalcin     if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
25936bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
259485385478SLisandro Dalcin     snes->vec_rhs = b;
259585385478SLisandro Dalcin 
259670e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
25973f149594SLisandro Dalcin 
2598d25893d9SBarry Smith     if (!grid && snes->ops->computeinitialguess) {
2599d25893d9SBarry Smith       ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr);
2600d25893d9SBarry Smith     }
2601d25893d9SBarry Smith 
2602abc0a331SBarry Smith     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
260350ffb88aSMatthew Knepley     snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2604d5e45103SBarry Smith 
26053f149594SLisandro Dalcin     ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
26064936397dSBarry Smith     ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
260785385478SLisandro Dalcin     ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
26084936397dSBarry Smith     if (snes->domainerror){
26094936397dSBarry Smith       snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
26104936397dSBarry Smith       snes->domainerror = PETSC_FALSE;
26114936397dSBarry Smith     }
261217186662SBarry Smith     if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
26133f149594SLisandro Dalcin 
26147adad957SLisandro Dalcin     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2615eabae89aSBarry Smith     if (flg && !PetscPreLoadingOn) {
26167adad957SLisandro Dalcin       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2617eabae89aSBarry Smith       ierr = SNESView(snes,viewer);CHKERRQ(ierr);
26186bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2619eabae89aSBarry Smith     }
2620eabae89aSBarry Smith 
262190d69ab7SBarry Smith     flg  = PETSC_FALSE;
2622acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2623da9b6338SBarry Smith     if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
26245968eb51SBarry Smith     if (snes->printreason) {
26255968eb51SBarry Smith       if (snes->reason > 0) {
26267adad957SLisandro Dalcin         ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
26275968eb51SBarry Smith       } else {
26287adad957SLisandro Dalcin         ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
26295968eb51SBarry Smith       }
26305968eb51SBarry Smith     }
26315968eb51SBarry Smith 
2632e113a28aSBarry Smith     if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
2633efd51863SBarry Smith     if (grid <  snes->gridsequence) {
2634efd51863SBarry Smith       DM  fine;
2635efd51863SBarry Smith       Vec xnew;
2636efd51863SBarry Smith       Mat interp;
2637efd51863SBarry Smith 
2638efd51863SBarry Smith       ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr);
2639efd51863SBarry Smith       ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr);
2640efd51863SBarry Smith       ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr);
2641efd51863SBarry Smith       ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr);
2642efd51863SBarry Smith       ierr = MatDestroy(&interp);CHKERRQ(ierr);
2643efd51863SBarry Smith       x    = xnew;
2644efd51863SBarry Smith 
2645efd51863SBarry Smith       ierr = SNESReset(snes);CHKERRQ(ierr);
2646efd51863SBarry Smith       ierr = SNESSetDM(snes,fine);CHKERRQ(ierr);
2647efd51863SBarry Smith       ierr = DMDestroy(&fine);CHKERRQ(ierr);
2648efd51863SBarry Smith     }
2649efd51863SBarry Smith   }
26503a40ed3dSBarry Smith   PetscFunctionReturn(0);
26519b94acceSBarry Smith }
26529b94acceSBarry Smith 
26539b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
26549b94acceSBarry Smith 
26554a2ae208SSatish Balay #undef __FUNCT__
26564a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
265782bf6240SBarry Smith /*@C
26584b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
26599b94acceSBarry Smith 
2660fee21e36SBarry Smith    Collective on SNES
2661fee21e36SBarry Smith 
2662c7afd0dbSLois Curfman McInnes    Input Parameters:
2663c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2664454a90a3SBarry Smith -  type - a known method
2665c7afd0dbSLois Curfman McInnes 
2666c7afd0dbSLois Curfman McInnes    Options Database Key:
2667454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2668c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2669ae12b187SLois Curfman McInnes 
26709b94acceSBarry Smith    Notes:
2671e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
26724b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2673c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
26744b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2675c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
26769b94acceSBarry Smith 
2677ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2678ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2679ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2680ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2681ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2682ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2683ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2684ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2685ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2686b0a32e0cSBarry Smith   appropriate method.
268736851e7fSLois Curfman McInnes 
268836851e7fSLois Curfman McInnes   Level: intermediate
2689a703fe33SLois Curfman McInnes 
2690454a90a3SBarry Smith .keywords: SNES, set, type
2691435da068SBarry Smith 
2692435da068SBarry Smith .seealso: SNESType, SNESCreate()
2693435da068SBarry Smith 
26949b94acceSBarry Smith @*/
26957087cfbeSBarry Smith PetscErrorCode  SNESSetType(SNES snes,const SNESType type)
26969b94acceSBarry Smith {
2697dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
2698ace3abfcSBarry Smith   PetscBool      match;
26993a40ed3dSBarry Smith 
27003a40ed3dSBarry Smith   PetscFunctionBegin;
27010700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27024482741eSBarry Smith   PetscValidCharPointer(type,2);
270382bf6240SBarry Smith 
27046831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
27050f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
270692ff6ae8SBarry Smith 
27074b91b6eaSBarry Smith   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2708e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
270975396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
271075396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
271175396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
271275396ef9SLisandro Dalcin   snes->ops->setup          = 0;
271375396ef9SLisandro Dalcin   snes->ops->solve          = 0;
271475396ef9SLisandro Dalcin   snes->ops->view           = 0;
271575396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
271675396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
271775396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
271875396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
2719454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
272003bfa161SLisandro Dalcin   ierr = (*r)(snes);CHKERRQ(ierr);
27219fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
27229fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
27239fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr);
27249fb22e1aSBarry Smith   }
27259fb22e1aSBarry Smith #endif
27263a40ed3dSBarry Smith   PetscFunctionReturn(0);
27279b94acceSBarry Smith }
27289b94acceSBarry Smith 
2729a847f771SSatish Balay 
27309b94acceSBarry Smith /* --------------------------------------------------------------------- */
27314a2ae208SSatish Balay #undef __FUNCT__
27324a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
273352baeb72SSatish Balay /*@
27349b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2735f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
27369b94acceSBarry Smith 
2737fee21e36SBarry Smith    Not Collective
2738fee21e36SBarry Smith 
273936851e7fSLois Curfman McInnes    Level: advanced
274036851e7fSLois Curfman McInnes 
27419b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
27429b94acceSBarry Smith 
27439b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
27449b94acceSBarry Smith @*/
27457087cfbeSBarry Smith PetscErrorCode  SNESRegisterDestroy(void)
27469b94acceSBarry Smith {
2747dfbe8321SBarry Smith   PetscErrorCode ierr;
274882bf6240SBarry Smith 
27493a40ed3dSBarry Smith   PetscFunctionBegin;
27501441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
27514c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
27523a40ed3dSBarry Smith   PetscFunctionReturn(0);
27539b94acceSBarry Smith }
27549b94acceSBarry Smith 
27554a2ae208SSatish Balay #undef __FUNCT__
27564a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
27579b94acceSBarry Smith /*@C
27589a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
27599b94acceSBarry Smith 
2760c7afd0dbSLois Curfman McInnes    Not Collective
2761c7afd0dbSLois Curfman McInnes 
27629b94acceSBarry Smith    Input Parameter:
27634b0e389bSBarry Smith .  snes - nonlinear solver context
27649b94acceSBarry Smith 
27659b94acceSBarry Smith    Output Parameter:
27663a7fca6bSBarry Smith .  type - SNES method (a character string)
27679b94acceSBarry Smith 
276836851e7fSLois Curfman McInnes    Level: intermediate
276936851e7fSLois Curfman McInnes 
2770454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
27719b94acceSBarry Smith @*/
27727087cfbeSBarry Smith PetscErrorCode  SNESGetType(SNES snes,const SNESType *type)
27739b94acceSBarry Smith {
27743a40ed3dSBarry Smith   PetscFunctionBegin;
27750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27764482741eSBarry Smith   PetscValidPointer(type,2);
27777adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
27783a40ed3dSBarry Smith   PetscFunctionReturn(0);
27799b94acceSBarry Smith }
27809b94acceSBarry Smith 
27814a2ae208SSatish Balay #undef __FUNCT__
27824a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
278352baeb72SSatish Balay /*@
27849b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
27859b94acceSBarry Smith    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:
27939b94acceSBarry Smith .  x - the solution
27949b94acceSBarry Smith 
279570e92668SMatthew Knepley    Level: intermediate
279636851e7fSLois Curfman McInnes 
27979b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
27989b94acceSBarry Smith 
279985385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
28009b94acceSBarry Smith @*/
28017087cfbeSBarry Smith PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
28029b94acceSBarry Smith {
28033a40ed3dSBarry Smith   PetscFunctionBegin;
28040700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
28054482741eSBarry Smith   PetscValidPointer(x,2);
280685385478SLisandro Dalcin   *x = snes->vec_sol;
280770e92668SMatthew Knepley   PetscFunctionReturn(0);
280870e92668SMatthew Knepley }
280970e92668SMatthew Knepley 
281070e92668SMatthew Knepley #undef __FUNCT__
28114a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
281252baeb72SSatish Balay /*@
28139b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
28149b94acceSBarry Smith    stored.
28159b94acceSBarry Smith 
2816c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2817c7afd0dbSLois Curfman McInnes 
28189b94acceSBarry Smith    Input Parameter:
28199b94acceSBarry Smith .  snes - the SNES context
28209b94acceSBarry Smith 
28219b94acceSBarry Smith    Output Parameter:
28229b94acceSBarry Smith .  x - the solution update
28239b94acceSBarry Smith 
282436851e7fSLois Curfman McInnes    Level: advanced
282536851e7fSLois Curfman McInnes 
28269b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
28279b94acceSBarry Smith 
282885385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
28299b94acceSBarry Smith @*/
28307087cfbeSBarry Smith PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
28319b94acceSBarry Smith {
28323a40ed3dSBarry Smith   PetscFunctionBegin;
28330700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
28344482741eSBarry Smith   PetscValidPointer(x,2);
283585385478SLisandro Dalcin   *x = snes->vec_sol_update;
28363a40ed3dSBarry Smith   PetscFunctionReturn(0);
28379b94acceSBarry Smith }
28389b94acceSBarry Smith 
28394a2ae208SSatish Balay #undef __FUNCT__
28404a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
28419b94acceSBarry Smith /*@C
28423638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
28439b94acceSBarry Smith 
2844c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2845c7afd0dbSLois Curfman McInnes 
28469b94acceSBarry Smith    Input Parameter:
28479b94acceSBarry Smith .  snes - the SNES context
28489b94acceSBarry Smith 
28499b94acceSBarry Smith    Output Parameter:
28507bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
285170e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
285270e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
28539b94acceSBarry Smith 
285436851e7fSLois Curfman McInnes    Level: advanced
285536851e7fSLois Curfman McInnes 
2856a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
28579b94acceSBarry Smith 
28584b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
28599b94acceSBarry Smith @*/
28607087cfbeSBarry Smith PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
28619b94acceSBarry Smith {
28623a40ed3dSBarry Smith   PetscFunctionBegin;
28630700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
286485385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2865e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
286670e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
28673a40ed3dSBarry Smith   PetscFunctionReturn(0);
28689b94acceSBarry Smith }
28699b94acceSBarry Smith 
28704a2ae208SSatish Balay #undef __FUNCT__
28714a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
28723c7409f5SSatish Balay /*@C
28733c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2874d850072dSLois Curfman McInnes    SNES options in the database.
28753c7409f5SSatish Balay 
28763f9fe445SBarry Smith    Logically Collective on SNES
2877fee21e36SBarry Smith 
2878c7afd0dbSLois Curfman McInnes    Input Parameter:
2879c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2880c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2881c7afd0dbSLois Curfman McInnes 
2882d850072dSLois Curfman McInnes    Notes:
2883a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2884c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2885d850072dSLois Curfman McInnes 
288636851e7fSLois Curfman McInnes    Level: advanced
288736851e7fSLois Curfman McInnes 
28883c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2889a86d99e1SLois Curfman McInnes 
2890a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
28913c7409f5SSatish Balay @*/
28927087cfbeSBarry Smith PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
28933c7409f5SSatish Balay {
2894dfbe8321SBarry Smith   PetscErrorCode ierr;
28953c7409f5SSatish Balay 
28963a40ed3dSBarry Smith   PetscFunctionBegin;
28970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2898639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
28991cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
290094b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
29013a40ed3dSBarry Smith   PetscFunctionReturn(0);
29023c7409f5SSatish Balay }
29033c7409f5SSatish Balay 
29044a2ae208SSatish Balay #undef __FUNCT__
29054a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
29063c7409f5SSatish Balay /*@C
2907f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2908d850072dSLois Curfman McInnes    SNES options in the database.
29093c7409f5SSatish Balay 
29103f9fe445SBarry Smith    Logically Collective on SNES
2911fee21e36SBarry Smith 
2912c7afd0dbSLois Curfman McInnes    Input Parameters:
2913c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2914c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2915c7afd0dbSLois Curfman McInnes 
2916d850072dSLois Curfman McInnes    Notes:
2917a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2918c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2919d850072dSLois Curfman McInnes 
292036851e7fSLois Curfman McInnes    Level: advanced
292136851e7fSLois Curfman McInnes 
29223c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2923a86d99e1SLois Curfman McInnes 
2924a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
29253c7409f5SSatish Balay @*/
29267087cfbeSBarry Smith PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
29273c7409f5SSatish Balay {
2928dfbe8321SBarry Smith   PetscErrorCode ierr;
29293c7409f5SSatish Balay 
29303a40ed3dSBarry Smith   PetscFunctionBegin;
29310700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2932639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
29331cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
293494b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
29353a40ed3dSBarry Smith   PetscFunctionReturn(0);
29363c7409f5SSatish Balay }
29373c7409f5SSatish Balay 
29384a2ae208SSatish Balay #undef __FUNCT__
29394a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
29409ab63eb5SSatish Balay /*@C
29413c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
29423c7409f5SSatish Balay    SNES options in the database.
29433c7409f5SSatish Balay 
2944c7afd0dbSLois Curfman McInnes    Not Collective
2945c7afd0dbSLois Curfman McInnes 
29463c7409f5SSatish Balay    Input Parameter:
29473c7409f5SSatish Balay .  snes - the SNES context
29483c7409f5SSatish Balay 
29493c7409f5SSatish Balay    Output Parameter:
29503c7409f5SSatish Balay .  prefix - pointer to the prefix string used
29513c7409f5SSatish Balay 
29524ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
29539ab63eb5SSatish Balay    sufficient length to hold the prefix.
29549ab63eb5SSatish Balay 
295536851e7fSLois Curfman McInnes    Level: advanced
295636851e7fSLois Curfman McInnes 
29573c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2958a86d99e1SLois Curfman McInnes 
2959a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
29603c7409f5SSatish Balay @*/
29617087cfbeSBarry Smith PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
29623c7409f5SSatish Balay {
2963dfbe8321SBarry Smith   PetscErrorCode ierr;
29643c7409f5SSatish Balay 
29653a40ed3dSBarry Smith   PetscFunctionBegin;
29660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2967639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
29683a40ed3dSBarry Smith   PetscFunctionReturn(0);
29693c7409f5SSatish Balay }
29703c7409f5SSatish Balay 
2971b2002411SLois Curfman McInnes 
29724a2ae208SSatish Balay #undef __FUNCT__
29734a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
29743cea93caSBarry Smith /*@C
29753cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
29763cea93caSBarry Smith 
29777f6c08e0SMatthew Knepley   Level: advanced
29783cea93caSBarry Smith @*/
29797087cfbeSBarry Smith PetscErrorCode  SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2980b2002411SLois Curfman McInnes {
2981e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2982dfbe8321SBarry Smith   PetscErrorCode ierr;
2983b2002411SLois Curfman McInnes 
2984b2002411SLois Curfman McInnes   PetscFunctionBegin;
2985b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2986c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2987b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2988b2002411SLois Curfman McInnes }
2989da9b6338SBarry Smith 
2990da9b6338SBarry Smith #undef __FUNCT__
2991da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
29927087cfbeSBarry Smith PetscErrorCode  SNESTestLocalMin(SNES snes)
2993da9b6338SBarry Smith {
2994dfbe8321SBarry Smith   PetscErrorCode ierr;
299577431f27SBarry Smith   PetscInt       N,i,j;
2996da9b6338SBarry Smith   Vec            u,uh,fh;
2997da9b6338SBarry Smith   PetscScalar    value;
2998da9b6338SBarry Smith   PetscReal      norm;
2999da9b6338SBarry Smith 
3000da9b6338SBarry Smith   PetscFunctionBegin;
3001da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
3002da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
3003da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
3004da9b6338SBarry Smith 
3005da9b6338SBarry Smith   /* currently only works for sequential */
3006da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
3007da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
3008da9b6338SBarry Smith   for (i=0; i<N; i++) {
3009da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
301077431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
3011da9b6338SBarry Smith     for (j=-10; j<11; j++) {
3012ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
3013da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
30143ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
3015da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
301677431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
3017da9b6338SBarry Smith       value = -value;
3018da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
3019da9b6338SBarry Smith     }
3020da9b6338SBarry Smith   }
30216bf464f9SBarry Smith   ierr = VecDestroy(&uh);CHKERRQ(ierr);
30226bf464f9SBarry Smith   ierr = VecDestroy(&fh);CHKERRQ(ierr);
3023da9b6338SBarry Smith   PetscFunctionReturn(0);
3024da9b6338SBarry Smith }
302571f87433Sdalcinl 
302671f87433Sdalcinl #undef __FUNCT__
3027fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
302871f87433Sdalcinl /*@
3029fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
303071f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
303171f87433Sdalcinl    Newton method.
303271f87433Sdalcinl 
30333f9fe445SBarry Smith    Logically Collective on SNES
303471f87433Sdalcinl 
303571f87433Sdalcinl    Input Parameters:
303671f87433Sdalcinl +  snes - SNES context
303771f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
303871f87433Sdalcinl 
303964ba62caSBarry Smith     Options Database:
304064ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
304164ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
304264ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
304364ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
304464ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
304564ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
304664ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
304764ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
304864ba62caSBarry Smith 
304971f87433Sdalcinl    Notes:
305071f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
305171f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
305271f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
305371f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
305471f87433Sdalcinl    solver.
305571f87433Sdalcinl 
305671f87433Sdalcinl    Level: advanced
305771f87433Sdalcinl 
305871f87433Sdalcinl    Reference:
305971f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
306071f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
306171f87433Sdalcinl 
306271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
306371f87433Sdalcinl 
3064fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
306571f87433Sdalcinl @*/
30667087cfbeSBarry Smith PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool  flag)
306771f87433Sdalcinl {
306871f87433Sdalcinl   PetscFunctionBegin;
30690700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3070acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flag,2);
307171f87433Sdalcinl   snes->ksp_ewconv = flag;
307271f87433Sdalcinl   PetscFunctionReturn(0);
307371f87433Sdalcinl }
307471f87433Sdalcinl 
307571f87433Sdalcinl #undef __FUNCT__
3076fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
307771f87433Sdalcinl /*@
3078fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
307971f87433Sdalcinl    for computing relative tolerance for linear solvers within an
308071f87433Sdalcinl    inexact Newton method.
308171f87433Sdalcinl 
308271f87433Sdalcinl    Not Collective
308371f87433Sdalcinl 
308471f87433Sdalcinl    Input Parameter:
308571f87433Sdalcinl .  snes - SNES context
308671f87433Sdalcinl 
308771f87433Sdalcinl    Output Parameter:
308871f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
308971f87433Sdalcinl 
309071f87433Sdalcinl    Notes:
309171f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
309271f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
309371f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
309471f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
309571f87433Sdalcinl    solver.
309671f87433Sdalcinl 
309771f87433Sdalcinl    Level: advanced
309871f87433Sdalcinl 
309971f87433Sdalcinl    Reference:
310071f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
310171f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
310271f87433Sdalcinl 
310371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
310471f87433Sdalcinl 
3105fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
310671f87433Sdalcinl @*/
31077087cfbeSBarry Smith PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
310871f87433Sdalcinl {
310971f87433Sdalcinl   PetscFunctionBegin;
31100700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
311171f87433Sdalcinl   PetscValidPointer(flag,2);
311271f87433Sdalcinl   *flag = snes->ksp_ewconv;
311371f87433Sdalcinl   PetscFunctionReturn(0);
311471f87433Sdalcinl }
311571f87433Sdalcinl 
311671f87433Sdalcinl #undef __FUNCT__
3117fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
311871f87433Sdalcinl /*@
3119fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
312071f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
312171f87433Sdalcinl    Newton method.
312271f87433Sdalcinl 
31233f9fe445SBarry Smith    Logically Collective on SNES
312471f87433Sdalcinl 
312571f87433Sdalcinl    Input Parameters:
312671f87433Sdalcinl +    snes - SNES context
312771f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
312871f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
312971f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
313071f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
313171f87433Sdalcinl              (0 <= gamma2 <= 1)
313271f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
313371f87433Sdalcinl .    alpha2 - power for safeguard
313471f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
313571f87433Sdalcinl 
313671f87433Sdalcinl    Note:
313771f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
313871f87433Sdalcinl 
313971f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
314071f87433Sdalcinl 
314171f87433Sdalcinl    Level: advanced
314271f87433Sdalcinl 
314371f87433Sdalcinl    Reference:
314471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
314571f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
314671f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
314771f87433Sdalcinl 
314871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
314971f87433Sdalcinl 
3150fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
315171f87433Sdalcinl @*/
31527087cfbeSBarry Smith PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
315371f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
315471f87433Sdalcinl {
3155fa9f3622SBarry Smith   SNESKSPEW *kctx;
315671f87433Sdalcinl   PetscFunctionBegin;
31570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3158fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3159e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
3160c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
3161c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
3162c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
3163c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
3164c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
3165c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
3166c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
316771f87433Sdalcinl 
316871f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
316971f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
317071f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
317171f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
317271f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
317371f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
317471f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
317571f87433Sdalcinl 
317671f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
3177e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
317871f87433Sdalcinl   }
317971f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
3180e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
318171f87433Sdalcinl   }
318271f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
3183e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
318471f87433Sdalcinl   }
318571f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
3186e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
318771f87433Sdalcinl   }
318871f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
3189e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
319071f87433Sdalcinl   }
319171f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
3192e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
319371f87433Sdalcinl   }
319471f87433Sdalcinl   PetscFunctionReturn(0);
319571f87433Sdalcinl }
319671f87433Sdalcinl 
319771f87433Sdalcinl #undef __FUNCT__
3198fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
319971f87433Sdalcinl /*@
3200fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
320171f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
320271f87433Sdalcinl    Newton method.
320371f87433Sdalcinl 
320471f87433Sdalcinl    Not Collective
320571f87433Sdalcinl 
320671f87433Sdalcinl    Input Parameters:
320771f87433Sdalcinl      snes - SNES context
320871f87433Sdalcinl 
320971f87433Sdalcinl    Output Parameters:
321071f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
321171f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
321271f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
321371f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
321471f87433Sdalcinl              (0 <= gamma2 <= 1)
321571f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
321671f87433Sdalcinl .    alpha2 - power for safeguard
321771f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
321871f87433Sdalcinl 
321971f87433Sdalcinl    Level: advanced
322071f87433Sdalcinl 
322171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
322271f87433Sdalcinl 
3223fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
322471f87433Sdalcinl @*/
32257087cfbeSBarry Smith PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
322671f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
322771f87433Sdalcinl {
3228fa9f3622SBarry Smith   SNESKSPEW *kctx;
322971f87433Sdalcinl   PetscFunctionBegin;
32300700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3231fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3232e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
323371f87433Sdalcinl   if(version)   *version   = kctx->version;
323471f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
323571f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
323671f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
323771f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
323871f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
323971f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
324071f87433Sdalcinl   PetscFunctionReturn(0);
324171f87433Sdalcinl }
324271f87433Sdalcinl 
324371f87433Sdalcinl #undef __FUNCT__
3244fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
3245fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
324671f87433Sdalcinl {
324771f87433Sdalcinl   PetscErrorCode ierr;
3248fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
324971f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
325071f87433Sdalcinl 
325171f87433Sdalcinl   PetscFunctionBegin;
3252e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
325371f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
325471f87433Sdalcinl     rtol = kctx->rtol_0;
325571f87433Sdalcinl   } else {
325671f87433Sdalcinl     if (kctx->version == 1) {
325771f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
325871f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
325971f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
326071f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
326171f87433Sdalcinl     } else if (kctx->version == 2) {
326271f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
326371f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
326471f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
326571f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
326671f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
326771f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
326871f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
326971f87433Sdalcinl       stol = PetscMax(rtol,stol);
327071f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
327171f87433Sdalcinl       /* safeguard: avoid oversolving */
327271f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
327371f87433Sdalcinl       stol = PetscMax(rtol,stol);
327471f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
3275e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
327671f87433Sdalcinl   }
327771f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
327871f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
327971f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
328071f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
328171f87433Sdalcinl   PetscFunctionReturn(0);
328271f87433Sdalcinl }
328371f87433Sdalcinl 
328471f87433Sdalcinl #undef __FUNCT__
3285fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
3286fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
328771f87433Sdalcinl {
328871f87433Sdalcinl   PetscErrorCode ierr;
3289fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
329071f87433Sdalcinl   PCSide         pcside;
329171f87433Sdalcinl   Vec            lres;
329271f87433Sdalcinl 
329371f87433Sdalcinl   PetscFunctionBegin;
3294e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
329571f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
329671f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
329771f87433Sdalcinl   if (kctx->version == 1) {
3298b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
329971f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
330071f87433Sdalcinl       /* KSP residual is true linear residual */
330171f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
330271f87433Sdalcinl     } else {
330371f87433Sdalcinl       /* KSP residual is preconditioned residual */
330471f87433Sdalcinl       /* compute true linear residual norm */
330571f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
330671f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
330771f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
330871f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
33096bf464f9SBarry Smith       ierr = VecDestroy(&lres);CHKERRQ(ierr);
331071f87433Sdalcinl     }
331171f87433Sdalcinl   }
331271f87433Sdalcinl   PetscFunctionReturn(0);
331371f87433Sdalcinl }
331471f87433Sdalcinl 
331571f87433Sdalcinl #undef __FUNCT__
331671f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
331771f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
331871f87433Sdalcinl {
331971f87433Sdalcinl   PetscErrorCode ierr;
332071f87433Sdalcinl 
332171f87433Sdalcinl   PetscFunctionBegin;
3322fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
332371f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3324fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
332571f87433Sdalcinl   PetscFunctionReturn(0);
332671f87433Sdalcinl }
33276c699258SBarry Smith 
33286c699258SBarry Smith #undef __FUNCT__
33296c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
33306c699258SBarry Smith /*@
33316c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
33326c699258SBarry Smith 
33333f9fe445SBarry Smith    Logically Collective on SNES
33346c699258SBarry Smith 
33356c699258SBarry Smith    Input Parameters:
33366c699258SBarry Smith +  snes - the preconditioner context
33376c699258SBarry Smith -  dm - the dm
33386c699258SBarry Smith 
33396c699258SBarry Smith    Level: intermediate
33406c699258SBarry Smith 
33416c699258SBarry Smith 
33426c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
33436c699258SBarry Smith @*/
33447087cfbeSBarry Smith PetscErrorCode  SNESSetDM(SNES snes,DM dm)
33456c699258SBarry Smith {
33466c699258SBarry Smith   PetscErrorCode ierr;
3347345fed2cSBarry Smith   KSP            ksp;
33486c699258SBarry Smith 
33496c699258SBarry Smith   PetscFunctionBegin;
33500700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3351d0660788SBarry Smith   if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);}
33526bf464f9SBarry Smith   ierr = DMDestroy(&snes->dm);CHKERRQ(ierr);
33536c699258SBarry Smith   snes->dm = dm;
3354345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3355345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3356f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
33576c699258SBarry Smith   PetscFunctionReturn(0);
33586c699258SBarry Smith }
33596c699258SBarry Smith 
33606c699258SBarry Smith #undef __FUNCT__
33616c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
33626c699258SBarry Smith /*@
33636c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
33646c699258SBarry Smith 
33653f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
33666c699258SBarry Smith 
33676c699258SBarry Smith    Input Parameter:
33686c699258SBarry Smith . snes - the preconditioner context
33696c699258SBarry Smith 
33706c699258SBarry Smith    Output Parameter:
33716c699258SBarry Smith .  dm - the dm
33726c699258SBarry Smith 
33736c699258SBarry Smith    Level: intermediate
33746c699258SBarry Smith 
33756c699258SBarry Smith 
33766c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
33776c699258SBarry Smith @*/
33787087cfbeSBarry Smith PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
33796c699258SBarry Smith {
33806c699258SBarry Smith   PetscFunctionBegin;
33810700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33826c699258SBarry Smith   *dm = snes->dm;
33836c699258SBarry Smith   PetscFunctionReturn(0);
33846c699258SBarry Smith }
33850807856dSBarry Smith 
338669b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3387c6db04a5SJed Brown #include <mex.h>
338869b4f73cSBarry Smith 
33898f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
33908f6e6473SBarry Smith 
33910807856dSBarry Smith #undef __FUNCT__
33920807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab"
33930807856dSBarry Smith /*
33940807856dSBarry Smith    SNESComputeFunction_Matlab - Calls the function that has been set with
33950807856dSBarry Smith                          SNESSetFunctionMatlab().
33960807856dSBarry Smith 
33970807856dSBarry Smith    Collective on SNES
33980807856dSBarry Smith 
33990807856dSBarry Smith    Input Parameters:
34000807856dSBarry Smith +  snes - the SNES context
34010807856dSBarry Smith -  x - input vector
34020807856dSBarry Smith 
34030807856dSBarry Smith    Output Parameter:
34040807856dSBarry Smith .  y - function vector, as set by SNESSetFunction()
34050807856dSBarry Smith 
34060807856dSBarry Smith    Notes:
34070807856dSBarry Smith    SNESComputeFunction() is typically used within nonlinear solvers
34080807856dSBarry Smith    implementations, so most users would not generally call this routine
34090807856dSBarry Smith    themselves.
34100807856dSBarry Smith 
34110807856dSBarry Smith    Level: developer
34120807856dSBarry Smith 
34130807856dSBarry Smith .keywords: SNES, nonlinear, compute, function
34140807856dSBarry Smith 
34150807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
341661b2408cSBarry Smith */
34177087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx)
34180807856dSBarry Smith {
3419e650e774SBarry Smith   PetscErrorCode    ierr;
34208f6e6473SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
34218f6e6473SBarry Smith   int               nlhs = 1,nrhs = 5;
34228f6e6473SBarry Smith   mxArray	    *plhs[1],*prhs[5];
342391621f2eSBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
3424e650e774SBarry Smith 
34250807856dSBarry Smith   PetscFunctionBegin;
34260807856dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34270807856dSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
34280807856dSBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
34290807856dSBarry Smith   PetscCheckSameComm(snes,1,x,2);
34300807856dSBarry Smith   PetscCheckSameComm(snes,1,y,3);
34310807856dSBarry Smith 
34320807856dSBarry Smith   /* call Matlab function in ctx with arguments x and y */
3433e650e774SBarry Smith 
343491621f2eSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3435e650e774SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3436e650e774SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
343791621f2eSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
343891621f2eSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
343991621f2eSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
34408f6e6473SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
34418f6e6473SBarry Smith   prhs[4] =  sctx->ctx;
3442b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr);
3443e650e774SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3444e650e774SBarry Smith   mxDestroyArray(prhs[0]);
3445e650e774SBarry Smith   mxDestroyArray(prhs[1]);
3446e650e774SBarry Smith   mxDestroyArray(prhs[2]);
34478f6e6473SBarry Smith   mxDestroyArray(prhs[3]);
3448e650e774SBarry Smith   mxDestroyArray(plhs[0]);
34490807856dSBarry Smith   PetscFunctionReturn(0);
34500807856dSBarry Smith }
34510807856dSBarry Smith 
34520807856dSBarry Smith 
34530807856dSBarry Smith #undef __FUNCT__
34540807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab"
345561b2408cSBarry Smith /*
34560807856dSBarry Smith    SNESSetFunctionMatlab - Sets the function evaluation routine and function
34570807856dSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3458e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
34590807856dSBarry Smith 
34600807856dSBarry Smith    Logically Collective on SNES
34610807856dSBarry Smith 
34620807856dSBarry Smith    Input Parameters:
34630807856dSBarry Smith +  snes - the SNES context
34640807856dSBarry Smith .  r - vector to store function value
34650807856dSBarry Smith -  func - function evaluation routine
34660807856dSBarry Smith 
34670807856dSBarry Smith    Calling sequence of func:
346861b2408cSBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
34690807856dSBarry Smith 
34700807856dSBarry Smith 
34710807856dSBarry Smith    Notes:
34720807856dSBarry Smith    The Newton-like methods typically solve linear systems of the form
34730807856dSBarry Smith $      f'(x) x = -f(x),
34740807856dSBarry Smith    where f'(x) denotes the Jacobian matrix and f(x) is the function.
34750807856dSBarry Smith 
34760807856dSBarry Smith    Level: beginner
34770807856dSBarry Smith 
34780807856dSBarry Smith .keywords: SNES, nonlinear, set, function
34790807856dSBarry Smith 
34800807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
348161b2408cSBarry Smith */
34827087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx)
34830807856dSBarry Smith {
34840807856dSBarry Smith   PetscErrorCode    ierr;
34858f6e6473SBarry Smith   SNESMatlabContext *sctx;
34860807856dSBarry Smith 
34870807856dSBarry Smith   PetscFunctionBegin;
34888f6e6473SBarry Smith   /* currently sctx is memory bleed */
34898f6e6473SBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
34908f6e6473SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
34918f6e6473SBarry Smith   /*
34928f6e6473SBarry Smith      This should work, but it doesn't
34938f6e6473SBarry Smith   sctx->ctx = ctx;
34948f6e6473SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
34958f6e6473SBarry Smith   */
34968f6e6473SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
34978f6e6473SBarry Smith   ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr);
34980807856dSBarry Smith   PetscFunctionReturn(0);
34990807856dSBarry Smith }
350069b4f73cSBarry Smith 
350161b2408cSBarry Smith #undef __FUNCT__
350261b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab"
350361b2408cSBarry Smith /*
350461b2408cSBarry Smith    SNESComputeJacobian_Matlab - Calls the function that has been set with
350561b2408cSBarry Smith                          SNESSetJacobianMatlab().
350661b2408cSBarry Smith 
350761b2408cSBarry Smith    Collective on SNES
350861b2408cSBarry Smith 
350961b2408cSBarry Smith    Input Parameters:
351061b2408cSBarry Smith +  snes - the SNES context
351161b2408cSBarry Smith .  x - input vector
351261b2408cSBarry Smith .  A, B - the matrices
351361b2408cSBarry Smith -  ctx - user context
351461b2408cSBarry Smith 
351561b2408cSBarry Smith    Output Parameter:
351661b2408cSBarry Smith .  flag - structure of the matrix
351761b2408cSBarry Smith 
351861b2408cSBarry Smith    Level: developer
351961b2408cSBarry Smith 
352061b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function
352161b2408cSBarry Smith 
352261b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
352361b2408cSBarry Smith @*/
35247087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx)
352561b2408cSBarry Smith {
352661b2408cSBarry Smith   PetscErrorCode    ierr;
352761b2408cSBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
352861b2408cSBarry Smith   int               nlhs = 2,nrhs = 6;
352961b2408cSBarry Smith   mxArray	    *plhs[2],*prhs[6];
353061b2408cSBarry Smith   long long int     lx = 0,lA = 0,ls = 0, lB = 0;
353161b2408cSBarry Smith 
353261b2408cSBarry Smith   PetscFunctionBegin;
353361b2408cSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
353461b2408cSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
353561b2408cSBarry Smith 
353661b2408cSBarry Smith   /* call Matlab function in ctx with arguments x and y */
353761b2408cSBarry Smith 
353861b2408cSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
353961b2408cSBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
354061b2408cSBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
354161b2408cSBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
354261b2408cSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
354361b2408cSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
354461b2408cSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
354561b2408cSBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
354661b2408cSBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
354761b2408cSBarry Smith   prhs[5] =  sctx->ctx;
3548b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr);
354961b2408cSBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
355061b2408cSBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
355161b2408cSBarry Smith   mxDestroyArray(prhs[0]);
355261b2408cSBarry Smith   mxDestroyArray(prhs[1]);
355361b2408cSBarry Smith   mxDestroyArray(prhs[2]);
355461b2408cSBarry Smith   mxDestroyArray(prhs[3]);
355561b2408cSBarry Smith   mxDestroyArray(prhs[4]);
355661b2408cSBarry Smith   mxDestroyArray(plhs[0]);
355761b2408cSBarry Smith   mxDestroyArray(plhs[1]);
355861b2408cSBarry Smith   PetscFunctionReturn(0);
355961b2408cSBarry Smith }
356061b2408cSBarry Smith 
356161b2408cSBarry Smith 
356261b2408cSBarry Smith #undef __FUNCT__
356361b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab"
356461b2408cSBarry Smith /*
356561b2408cSBarry Smith    SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
356661b2408cSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3567e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
356861b2408cSBarry Smith 
356961b2408cSBarry Smith    Logically Collective on SNES
357061b2408cSBarry Smith 
357161b2408cSBarry Smith    Input Parameters:
357261b2408cSBarry Smith +  snes - the SNES context
357361b2408cSBarry Smith .  A,B - Jacobian matrices
357461b2408cSBarry Smith .  func - function evaluation routine
357561b2408cSBarry Smith -  ctx - user context
357661b2408cSBarry Smith 
357761b2408cSBarry Smith    Calling sequence of func:
357861b2408cSBarry Smith $    flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx);
357961b2408cSBarry Smith 
358061b2408cSBarry Smith 
358161b2408cSBarry Smith    Level: developer
358261b2408cSBarry Smith 
358361b2408cSBarry Smith .keywords: SNES, nonlinear, set, function
358461b2408cSBarry Smith 
358561b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
358661b2408cSBarry Smith */
35877087cfbeSBarry Smith PetscErrorCode  SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx)
358861b2408cSBarry Smith {
358961b2408cSBarry Smith   PetscErrorCode    ierr;
359061b2408cSBarry Smith   SNESMatlabContext *sctx;
359161b2408cSBarry Smith 
359261b2408cSBarry Smith   PetscFunctionBegin;
359361b2408cSBarry Smith   /* currently sctx is memory bleed */
359461b2408cSBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
359561b2408cSBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
359661b2408cSBarry Smith   /*
359761b2408cSBarry Smith      This should work, but it doesn't
359861b2408cSBarry Smith   sctx->ctx = ctx;
359961b2408cSBarry Smith   mexMakeArrayPersistent(sctx->ctx);
360061b2408cSBarry Smith   */
360161b2408cSBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
360261b2408cSBarry Smith   ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
360361b2408cSBarry Smith   PetscFunctionReturn(0);
360461b2408cSBarry Smith }
360569b4f73cSBarry Smith 
3606f9eb7ae2SShri Abhyankar #undef __FUNCT__
3607f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab"
3608f9eb7ae2SShri Abhyankar /*
3609f9eb7ae2SShri Abhyankar    SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab().
3610f9eb7ae2SShri Abhyankar 
3611f9eb7ae2SShri Abhyankar    Collective on SNES
3612f9eb7ae2SShri Abhyankar 
3613f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction()
3614f9eb7ae2SShri Abhyankar @*/
36157087cfbeSBarry Smith PetscErrorCode  SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx)
3616f9eb7ae2SShri Abhyankar {
3617f9eb7ae2SShri Abhyankar   PetscErrorCode  ierr;
361848f37e70SShri Abhyankar   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
3619f9eb7ae2SShri Abhyankar   int             nlhs = 1,nrhs = 6;
3620f9eb7ae2SShri Abhyankar   mxArray	  *plhs[1],*prhs[6];
3621f9eb7ae2SShri Abhyankar   long long int   lx = 0,ls = 0;
3622f9eb7ae2SShri Abhyankar   Vec             x=snes->vec_sol;
3623f9eb7ae2SShri Abhyankar 
3624f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3625f9eb7ae2SShri Abhyankar   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3626f9eb7ae2SShri Abhyankar 
3627f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3628f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3629f9eb7ae2SShri Abhyankar   prhs[0] =  mxCreateDoubleScalar((double)ls);
3630f9eb7ae2SShri Abhyankar   prhs[1] =  mxCreateDoubleScalar((double)it);
3631f9eb7ae2SShri Abhyankar   prhs[2] =  mxCreateDoubleScalar((double)fnorm);
3632f9eb7ae2SShri Abhyankar   prhs[3] =  mxCreateDoubleScalar((double)lx);
3633f9eb7ae2SShri Abhyankar   prhs[4] =  mxCreateString(sctx->funcname);
3634f9eb7ae2SShri Abhyankar   prhs[5] =  sctx->ctx;
3635f9eb7ae2SShri Abhyankar   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr);
3636f9eb7ae2SShri Abhyankar   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3637f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[0]);
3638f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[1]);
3639f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[2]);
3640f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[3]);
3641f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[4]);
3642f9eb7ae2SShri Abhyankar   mxDestroyArray(plhs[0]);
3643f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3644f9eb7ae2SShri Abhyankar }
3645f9eb7ae2SShri Abhyankar 
3646f9eb7ae2SShri Abhyankar 
3647f9eb7ae2SShri Abhyankar #undef __FUNCT__
3648f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab"
3649f9eb7ae2SShri Abhyankar /*
3650e3c5b3baSBarry Smith    SNESMonitorSetMatlab - Sets the monitor function from MATLAB
3651f9eb7ae2SShri Abhyankar 
3652f9eb7ae2SShri Abhyankar    Level: developer
3653f9eb7ae2SShri Abhyankar 
3654f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function
3655f9eb7ae2SShri Abhyankar 
3656f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
3657f9eb7ae2SShri Abhyankar */
36587087cfbeSBarry Smith PetscErrorCode  SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx)
3659f9eb7ae2SShri Abhyankar {
3660f9eb7ae2SShri Abhyankar   PetscErrorCode    ierr;
3661f9eb7ae2SShri Abhyankar   SNESMatlabContext *sctx;
3662f9eb7ae2SShri Abhyankar 
3663f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3664f9eb7ae2SShri Abhyankar   /* currently sctx is memory bleed */
3665f9eb7ae2SShri Abhyankar   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
3666f9eb7ae2SShri Abhyankar   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3667f9eb7ae2SShri Abhyankar   /*
3668f9eb7ae2SShri Abhyankar      This should work, but it doesn't
3669f9eb7ae2SShri Abhyankar   sctx->ctx = ctx;
3670f9eb7ae2SShri Abhyankar   mexMakeArrayPersistent(sctx->ctx);
3671f9eb7ae2SShri Abhyankar   */
3672f9eb7ae2SShri Abhyankar   sctx->ctx = mxDuplicateArray(ctx);
3673f9eb7ae2SShri Abhyankar   ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3674f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3675f9eb7ae2SShri Abhyankar }
3676f9eb7ae2SShri Abhyankar 
367769b4f73cSBarry Smith #endif
3678