xref: /petsc/src/snes/interface/snes.c (revision d25893d9b3e4138a6f167d255fa9b6d7673262ec)
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 
488*d25893d9SBarry Smith #undef __FUNCT__
489*d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext"
490*d25893d9SBarry Smith /*@
491*d25893d9SBarry Smith    SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for
492*d25893d9SBarry Smith    the nonlinear solvers.
493*d25893d9SBarry Smith 
494*d25893d9SBarry Smith    Logically Collective on SNES
495*d25893d9SBarry Smith 
496*d25893d9SBarry Smith    Input Parameters:
497*d25893d9SBarry Smith +  snes - the SNES context
498*d25893d9SBarry Smith .  compute - function to compute the context
499*d25893d9SBarry Smith -  destroy - function to destroy the context
500*d25893d9SBarry Smith 
501*d25893d9SBarry Smith    Level: intermediate
502*d25893d9SBarry Smith 
503*d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context
504*d25893d9SBarry Smith 
505*d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext()
506*d25893d9SBarry Smith @*/
507*d25893d9SBarry Smith PetscErrorCode  SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**))
508*d25893d9SBarry Smith {
509*d25893d9SBarry Smith   PetscFunctionBegin;
510*d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
511*d25893d9SBarry Smith   snes->ops->usercompute = compute;
512*d25893d9SBarry Smith   snes->ops->userdestroy = destroy;
513*d25893d9SBarry Smith   PetscFunctionReturn(0);
514*d25893d9SBarry Smith }
515a847f771SSatish Balay 
5164a2ae208SSatish Balay #undef __FUNCT__
5174a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
5189b94acceSBarry Smith /*@
5199b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
5209b94acceSBarry Smith    the nonlinear solvers.
5219b94acceSBarry Smith 
5223f9fe445SBarry Smith    Logically Collective on SNES
523fee21e36SBarry Smith 
524c7afd0dbSLois Curfman McInnes    Input Parameters:
525c7afd0dbSLois Curfman McInnes +  snes - the SNES context
526c7afd0dbSLois Curfman McInnes -  usrP - optional user context
527c7afd0dbSLois Curfman McInnes 
52836851e7fSLois Curfman McInnes    Level: intermediate
52936851e7fSLois Curfman McInnes 
5309b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
5319b94acceSBarry Smith 
532*d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext()
5339b94acceSBarry Smith @*/
5347087cfbeSBarry Smith PetscErrorCode  SNESSetApplicationContext(SNES snes,void *usrP)
5359b94acceSBarry Smith {
5363a40ed3dSBarry Smith   PetscFunctionBegin;
5370700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5389b94acceSBarry Smith   snes->user		= usrP;
5393a40ed3dSBarry Smith   PetscFunctionReturn(0);
5409b94acceSBarry Smith }
54174679c65SBarry Smith 
5424a2ae208SSatish Balay #undef __FUNCT__
5434a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
5449b94acceSBarry Smith /*@C
5459b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
5469b94acceSBarry Smith    nonlinear solvers.
5479b94acceSBarry Smith 
548c7afd0dbSLois Curfman McInnes    Not Collective
549c7afd0dbSLois Curfman McInnes 
5509b94acceSBarry Smith    Input Parameter:
5519b94acceSBarry Smith .  snes - SNES context
5529b94acceSBarry Smith 
5539b94acceSBarry Smith    Output Parameter:
5549b94acceSBarry Smith .  usrP - user context
5559b94acceSBarry Smith 
55636851e7fSLois Curfman McInnes    Level: intermediate
55736851e7fSLois Curfman McInnes 
5589b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
5599b94acceSBarry Smith 
5609b94acceSBarry Smith .seealso: SNESSetApplicationContext()
5619b94acceSBarry Smith @*/
5627087cfbeSBarry Smith PetscErrorCode  SNESGetApplicationContext(SNES snes,void **usrP)
5639b94acceSBarry Smith {
5643a40ed3dSBarry Smith   PetscFunctionBegin;
5650700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5669b94acceSBarry Smith   *usrP = snes->user;
5673a40ed3dSBarry Smith   PetscFunctionReturn(0);
5689b94acceSBarry Smith }
56974679c65SBarry Smith 
5704a2ae208SSatish Balay #undef __FUNCT__
5714a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
5729b94acceSBarry Smith /*@
573c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
574c8228a4eSBarry Smith    at this time.
5759b94acceSBarry Smith 
576c7afd0dbSLois Curfman McInnes    Not Collective
577c7afd0dbSLois Curfman McInnes 
5789b94acceSBarry Smith    Input Parameter:
5799b94acceSBarry Smith .  snes - SNES context
5809b94acceSBarry Smith 
5819b94acceSBarry Smith    Output Parameter:
5829b94acceSBarry Smith .  iter - iteration number
5839b94acceSBarry Smith 
584c8228a4eSBarry Smith    Notes:
585c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
586c8228a4eSBarry Smith 
587c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
58808405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
58908405cd6SLois Curfman McInnes .vb
59008405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
59108405cd6SLois Curfman McInnes       if (!(it % 2)) {
59208405cd6SLois Curfman McInnes         [compute Jacobian here]
59308405cd6SLois Curfman McInnes       }
59408405cd6SLois Curfman McInnes .ve
595c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
59608405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
597c8228a4eSBarry Smith 
59836851e7fSLois Curfman McInnes    Level: intermediate
59936851e7fSLois Curfman McInnes 
6002b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
6012b668275SBarry Smith 
602b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
6039b94acceSBarry Smith @*/
6047087cfbeSBarry Smith PetscErrorCode  SNESGetIterationNumber(SNES snes,PetscInt* iter)
6059b94acceSBarry Smith {
6063a40ed3dSBarry Smith   PetscFunctionBegin;
6070700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6084482741eSBarry Smith   PetscValidIntPointer(iter,2);
6099b94acceSBarry Smith   *iter = snes->iter;
6103a40ed3dSBarry Smith   PetscFunctionReturn(0);
6119b94acceSBarry Smith }
61274679c65SBarry Smith 
6134a2ae208SSatish Balay #undef __FUNCT__
6144a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
6159b94acceSBarry Smith /*@
6169b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
6179b94acceSBarry Smith    with SNESSSetFunction().
6189b94acceSBarry Smith 
619c7afd0dbSLois Curfman McInnes    Collective on SNES
620c7afd0dbSLois Curfman McInnes 
6219b94acceSBarry Smith    Input Parameter:
6229b94acceSBarry Smith .  snes - SNES context
6239b94acceSBarry Smith 
6249b94acceSBarry Smith    Output Parameter:
6259b94acceSBarry Smith .  fnorm - 2-norm of function
6269b94acceSBarry Smith 
62736851e7fSLois Curfman McInnes    Level: intermediate
62836851e7fSLois Curfman McInnes 
6299b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
630a86d99e1SLois Curfman McInnes 
631b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
6329b94acceSBarry Smith @*/
6337087cfbeSBarry Smith PetscErrorCode  SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
6349b94acceSBarry Smith {
6353a40ed3dSBarry Smith   PetscFunctionBegin;
6360700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6374482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
6389b94acceSBarry Smith   *fnorm = snes->norm;
6393a40ed3dSBarry Smith   PetscFunctionReturn(0);
6409b94acceSBarry Smith }
64174679c65SBarry Smith 
6424a2ae208SSatish Balay #undef __FUNCT__
643b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
6449b94acceSBarry Smith /*@
645b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
6469b94acceSBarry Smith    attempted by the nonlinear solver.
6479b94acceSBarry Smith 
648c7afd0dbSLois Curfman McInnes    Not Collective
649c7afd0dbSLois Curfman McInnes 
6509b94acceSBarry Smith    Input Parameter:
6519b94acceSBarry Smith .  snes - SNES context
6529b94acceSBarry Smith 
6539b94acceSBarry Smith    Output Parameter:
6549b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
6559b94acceSBarry Smith 
656c96a6f78SLois Curfman McInnes    Notes:
657c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
658c96a6f78SLois Curfman McInnes 
65936851e7fSLois Curfman McInnes    Level: intermediate
66036851e7fSLois Curfman McInnes 
6619b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
66258ebbce7SBarry Smith 
663e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
66458ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
6659b94acceSBarry Smith @*/
6667087cfbeSBarry Smith PetscErrorCode  SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
6679b94acceSBarry Smith {
6683a40ed3dSBarry Smith   PetscFunctionBegin;
6690700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6704482741eSBarry Smith   PetscValidIntPointer(nfails,2);
67150ffb88aSMatthew Knepley   *nfails = snes->numFailures;
67250ffb88aSMatthew Knepley   PetscFunctionReturn(0);
67350ffb88aSMatthew Knepley }
67450ffb88aSMatthew Knepley 
67550ffb88aSMatthew Knepley #undef __FUNCT__
676b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
67750ffb88aSMatthew Knepley /*@
678b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
67950ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
68050ffb88aSMatthew Knepley 
68150ffb88aSMatthew Knepley    Not Collective
68250ffb88aSMatthew Knepley 
68350ffb88aSMatthew Knepley    Input Parameters:
68450ffb88aSMatthew Knepley +  snes     - SNES context
68550ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
68650ffb88aSMatthew Knepley 
68750ffb88aSMatthew Knepley    Level: intermediate
68850ffb88aSMatthew Knepley 
68950ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
69058ebbce7SBarry Smith 
691e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
69258ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
69350ffb88aSMatthew Knepley @*/
6947087cfbeSBarry Smith PetscErrorCode  SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
69550ffb88aSMatthew Knepley {
69650ffb88aSMatthew Knepley   PetscFunctionBegin;
6970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
69850ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
69950ffb88aSMatthew Knepley   PetscFunctionReturn(0);
70050ffb88aSMatthew Knepley }
70150ffb88aSMatthew Knepley 
70250ffb88aSMatthew Knepley #undef __FUNCT__
703b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
70450ffb88aSMatthew Knepley /*@
705b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
70650ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
70750ffb88aSMatthew Knepley 
70850ffb88aSMatthew Knepley    Not Collective
70950ffb88aSMatthew Knepley 
71050ffb88aSMatthew Knepley    Input Parameter:
71150ffb88aSMatthew Knepley .  snes     - SNES context
71250ffb88aSMatthew Knepley 
71350ffb88aSMatthew Knepley    Output Parameter:
71450ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
71550ffb88aSMatthew Knepley 
71650ffb88aSMatthew Knepley    Level: intermediate
71750ffb88aSMatthew Knepley 
71850ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
71958ebbce7SBarry Smith 
720e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
72158ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
72258ebbce7SBarry Smith 
72350ffb88aSMatthew Knepley @*/
7247087cfbeSBarry Smith PetscErrorCode  SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
72550ffb88aSMatthew Knepley {
72650ffb88aSMatthew Knepley   PetscFunctionBegin;
7270700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7284482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
72950ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
7303a40ed3dSBarry Smith   PetscFunctionReturn(0);
7319b94acceSBarry Smith }
732a847f771SSatish Balay 
7334a2ae208SSatish Balay #undef __FUNCT__
7342541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
7352541af92SBarry Smith /*@
7362541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
7372541af92SBarry Smith      done by SNES.
7382541af92SBarry Smith 
7392541af92SBarry Smith    Not Collective
7402541af92SBarry Smith 
7412541af92SBarry Smith    Input Parameter:
7422541af92SBarry Smith .  snes     - SNES context
7432541af92SBarry Smith 
7442541af92SBarry Smith    Output Parameter:
7452541af92SBarry Smith .  nfuncs - number of evaluations
7462541af92SBarry Smith 
7472541af92SBarry Smith    Level: intermediate
7482541af92SBarry Smith 
7492541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
75058ebbce7SBarry Smith 
751e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
7522541af92SBarry Smith @*/
7537087cfbeSBarry Smith PetscErrorCode  SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
7542541af92SBarry Smith {
7552541af92SBarry Smith   PetscFunctionBegin;
7560700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7572541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
7582541af92SBarry Smith   *nfuncs = snes->nfuncs;
7592541af92SBarry Smith   PetscFunctionReturn(0);
7602541af92SBarry Smith }
7612541af92SBarry Smith 
7622541af92SBarry Smith #undef __FUNCT__
7633d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
7643d4c4710SBarry Smith /*@
7653d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
7663d4c4710SBarry Smith    linear solvers.
7673d4c4710SBarry Smith 
7683d4c4710SBarry Smith    Not Collective
7693d4c4710SBarry Smith 
7703d4c4710SBarry Smith    Input Parameter:
7713d4c4710SBarry Smith .  snes - SNES context
7723d4c4710SBarry Smith 
7733d4c4710SBarry Smith    Output Parameter:
7743d4c4710SBarry Smith .  nfails - number of failed solves
7753d4c4710SBarry Smith 
7763d4c4710SBarry Smith    Notes:
7773d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
7783d4c4710SBarry Smith 
7793d4c4710SBarry Smith    Level: intermediate
7803d4c4710SBarry Smith 
7813d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
78258ebbce7SBarry Smith 
783e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
7843d4c4710SBarry Smith @*/
7857087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
7863d4c4710SBarry Smith {
7873d4c4710SBarry Smith   PetscFunctionBegin;
7880700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7893d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
7903d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
7913d4c4710SBarry Smith   PetscFunctionReturn(0);
7923d4c4710SBarry Smith }
7933d4c4710SBarry Smith 
7943d4c4710SBarry Smith #undef __FUNCT__
7953d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
7963d4c4710SBarry Smith /*@
7973d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
7983d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
7993d4c4710SBarry Smith 
8003f9fe445SBarry Smith    Logically Collective on SNES
8013d4c4710SBarry Smith 
8023d4c4710SBarry Smith    Input Parameters:
8033d4c4710SBarry Smith +  snes     - SNES context
8043d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
8053d4c4710SBarry Smith 
8063d4c4710SBarry Smith    Level: intermediate
8073d4c4710SBarry Smith 
808a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
8093d4c4710SBarry Smith 
8103d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
8113d4c4710SBarry Smith 
81258ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
8133d4c4710SBarry Smith @*/
8147087cfbeSBarry Smith PetscErrorCode  SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
8153d4c4710SBarry Smith {
8163d4c4710SBarry Smith   PetscFunctionBegin;
8170700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
818c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
8193d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
8203d4c4710SBarry Smith   PetscFunctionReturn(0);
8213d4c4710SBarry Smith }
8223d4c4710SBarry Smith 
8233d4c4710SBarry Smith #undef __FUNCT__
8243d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
8253d4c4710SBarry Smith /*@
8263d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
8273d4c4710SBarry Smith      are allowed before SNES terminates
8283d4c4710SBarry Smith 
8293d4c4710SBarry Smith    Not Collective
8303d4c4710SBarry Smith 
8313d4c4710SBarry Smith    Input Parameter:
8323d4c4710SBarry Smith .  snes     - SNES context
8333d4c4710SBarry Smith 
8343d4c4710SBarry Smith    Output Parameter:
8353d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
8363d4c4710SBarry Smith 
8373d4c4710SBarry Smith    Level: intermediate
8383d4c4710SBarry Smith 
8393d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
8403d4c4710SBarry Smith 
8413d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
8423d4c4710SBarry Smith 
843e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
8443d4c4710SBarry Smith @*/
8457087cfbeSBarry Smith PetscErrorCode  SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
8463d4c4710SBarry Smith {
8473d4c4710SBarry Smith   PetscFunctionBegin;
8480700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8493d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
8503d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
8513d4c4710SBarry Smith   PetscFunctionReturn(0);
8523d4c4710SBarry Smith }
8533d4c4710SBarry Smith 
8543d4c4710SBarry Smith #undef __FUNCT__
855b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
856c96a6f78SLois Curfman McInnes /*@
857b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
858c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
859c96a6f78SLois Curfman McInnes 
860c7afd0dbSLois Curfman McInnes    Not Collective
861c7afd0dbSLois Curfman McInnes 
862c96a6f78SLois Curfman McInnes    Input Parameter:
863c96a6f78SLois Curfman McInnes .  snes - SNES context
864c96a6f78SLois Curfman McInnes 
865c96a6f78SLois Curfman McInnes    Output Parameter:
866c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
867c96a6f78SLois Curfman McInnes 
868c96a6f78SLois Curfman McInnes    Notes:
869c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
870c96a6f78SLois Curfman McInnes 
87136851e7fSLois Curfman McInnes    Level: intermediate
87236851e7fSLois Curfman McInnes 
873c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
8742b668275SBarry Smith 
8758c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
876c96a6f78SLois Curfman McInnes @*/
8777087cfbeSBarry Smith PetscErrorCode  SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
878c96a6f78SLois Curfman McInnes {
8793a40ed3dSBarry Smith   PetscFunctionBegin;
8800700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8814482741eSBarry Smith   PetscValidIntPointer(lits,2);
882c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
8833a40ed3dSBarry Smith   PetscFunctionReturn(0);
884c96a6f78SLois Curfman McInnes }
885c96a6f78SLois Curfman McInnes 
8864a2ae208SSatish Balay #undef __FUNCT__
88794b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
88852baeb72SSatish Balay /*@
88994b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
8909b94acceSBarry Smith 
89194b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
892c7afd0dbSLois Curfman McInnes 
8939b94acceSBarry Smith    Input Parameter:
8949b94acceSBarry Smith .  snes - the SNES context
8959b94acceSBarry Smith 
8969b94acceSBarry Smith    Output Parameter:
89794b7f48cSBarry Smith .  ksp - the KSP context
8989b94acceSBarry Smith 
8999b94acceSBarry Smith    Notes:
90094b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
9019b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
9022999313aSBarry Smith    PC contexts as well.
9039b94acceSBarry Smith 
90436851e7fSLois Curfman McInnes    Level: beginner
90536851e7fSLois Curfman McInnes 
90694b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9079b94acceSBarry Smith 
9082999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9099b94acceSBarry Smith @*/
9107087cfbeSBarry Smith PetscErrorCode  SNESGetKSP(SNES snes,KSP *ksp)
9119b94acceSBarry Smith {
9121cee3971SBarry Smith   PetscErrorCode ierr;
9131cee3971SBarry Smith 
9143a40ed3dSBarry Smith   PetscFunctionBegin;
9150700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9164482741eSBarry Smith   PetscValidPointer(ksp,2);
9171cee3971SBarry Smith 
9181cee3971SBarry Smith   if (!snes->ksp) {
9191cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
9201cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
9211cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
9221cee3971SBarry Smith   }
92394b7f48cSBarry Smith   *ksp = snes->ksp;
9243a40ed3dSBarry Smith   PetscFunctionReturn(0);
9259b94acceSBarry Smith }
92682bf6240SBarry Smith 
9274a2ae208SSatish Balay #undef __FUNCT__
9282999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
9292999313aSBarry Smith /*@
9302999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
9312999313aSBarry Smith 
9322999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
9332999313aSBarry Smith 
9342999313aSBarry Smith    Input Parameters:
9352999313aSBarry Smith +  snes - the SNES context
9362999313aSBarry Smith -  ksp - the KSP context
9372999313aSBarry Smith 
9382999313aSBarry Smith    Notes:
9392999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
9402999313aSBarry Smith    so this routine is rarely needed.
9412999313aSBarry Smith 
9422999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
9432999313aSBarry Smith    decreased by one.
9442999313aSBarry Smith 
9452999313aSBarry Smith    Level: developer
9462999313aSBarry Smith 
9472999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9482999313aSBarry Smith 
9492999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9502999313aSBarry Smith @*/
9517087cfbeSBarry Smith PetscErrorCode  SNESSetKSP(SNES snes,KSP ksp)
9522999313aSBarry Smith {
9532999313aSBarry Smith   PetscErrorCode ierr;
9542999313aSBarry Smith 
9552999313aSBarry Smith   PetscFunctionBegin;
9560700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9570700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
9582999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
9597dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
960906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
9612999313aSBarry Smith   snes->ksp = ksp;
9622999313aSBarry Smith   PetscFunctionReturn(0);
9632999313aSBarry Smith }
9642999313aSBarry Smith 
9657adad957SLisandro Dalcin #if 0
9662999313aSBarry Smith #undef __FUNCT__
9674a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
9686849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
969e24b481bSBarry Smith {
970e24b481bSBarry Smith   PetscFunctionBegin;
971e24b481bSBarry Smith   PetscFunctionReturn(0);
972e24b481bSBarry Smith }
9737adad957SLisandro Dalcin #endif
974e24b481bSBarry Smith 
9759b94acceSBarry Smith /* -----------------------------------------------------------*/
9764a2ae208SSatish Balay #undef __FUNCT__
9774a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
97852baeb72SSatish Balay /*@
9799b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
9809b94acceSBarry Smith 
981c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
982c7afd0dbSLois Curfman McInnes 
983c7afd0dbSLois Curfman McInnes    Input Parameters:
984906ed7ccSBarry Smith .  comm - MPI communicator
9859b94acceSBarry Smith 
9869b94acceSBarry Smith    Output Parameter:
9879b94acceSBarry Smith .  outsnes - the new SNES context
9889b94acceSBarry Smith 
989c7afd0dbSLois Curfman McInnes    Options Database Keys:
990c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
991c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
992c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
993c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
994c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
995c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
996c1f60f51SBarry Smith 
99736851e7fSLois Curfman McInnes    Level: beginner
99836851e7fSLois Curfman McInnes 
9999b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
10009b94acceSBarry Smith 
1001a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
1002a8054027SBarry Smith 
10039b94acceSBarry Smith @*/
10047087cfbeSBarry Smith PetscErrorCode  SNESCreate(MPI_Comm comm,SNES *outsnes)
10059b94acceSBarry Smith {
1006dfbe8321SBarry Smith   PetscErrorCode      ierr;
10079b94acceSBarry Smith   SNES                snes;
1008fa9f3622SBarry Smith   SNESKSPEW           *kctx;
100937fcc0dbSBarry Smith 
10103a40ed3dSBarry Smith   PetscFunctionBegin;
1011ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
10128ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
10138ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
10148ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
10158ba1e511SMatthew Knepley #endif
10168ba1e511SMatthew Knepley 
10170700a824SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
10187adad957SLisandro Dalcin 
101985385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
10209b94acceSBarry Smith   snes->max_its           = 50;
10219750a799SBarry Smith   snes->max_funcs	  = 10000;
10229b94acceSBarry Smith   snes->norm		  = 0.0;
1023b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
1024b4874afaSBarry Smith   snes->ttol              = 0.0;
102570441072SBarry Smith   snes->abstol		  = 1.e-50;
10269b94acceSBarry Smith   snes->xtol		  = 1.e-8;
10274b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
10289b94acceSBarry Smith   snes->nfuncs            = 0;
102950ffb88aSMatthew Knepley   snes->numFailures       = 0;
103050ffb88aSMatthew Knepley   snes->maxFailures       = 1;
10317a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1032e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1033a8054027SBarry Smith   snes->lagpreconditioner = 1;
1034639f9d9dSBarry Smith   snes->numbermonitors    = 0;
10359b94acceSBarry Smith   snes->data              = 0;
10364dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1037186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
10386f24a144SLois Curfman McInnes   snes->nwork             = 0;
103958c9b817SLisandro Dalcin   snes->work              = 0;
104058c9b817SLisandro Dalcin   snes->nvwork            = 0;
104158c9b817SLisandro Dalcin   snes->vwork             = 0;
1042758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1043758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1044758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1045758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1046758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1047184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
10489b94acceSBarry Smith 
10493d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
10503d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
10513d4c4710SBarry Smith 
10529b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
105338f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
10549b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
10559b94acceSBarry Smith   kctx->version     = 2;
10569b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
10579b94acceSBarry Smith                              this was too large for some test cases */
105875567043SBarry Smith   kctx->rtol_last   = 0.0;
10599b94acceSBarry Smith   kctx->rtol_max    = .9;
10609b94acceSBarry Smith   kctx->gamma       = 1.0;
106171f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
106271f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
10639b94acceSBarry Smith   kctx->threshold   = .1;
106475567043SBarry Smith   kctx->lresid_last = 0.0;
106575567043SBarry Smith   kctx->norm_last   = 0.0;
10669b94acceSBarry Smith 
10679b94acceSBarry Smith   *outsnes = snes;
10683a40ed3dSBarry Smith   PetscFunctionReturn(0);
10699b94acceSBarry Smith }
10709b94acceSBarry Smith 
10714a2ae208SSatish Balay #undef __FUNCT__
10724a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
10739b94acceSBarry Smith /*@C
10749b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
10759b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
10769b94acceSBarry Smith    equations.
10779b94acceSBarry Smith 
10783f9fe445SBarry Smith    Logically Collective on SNES
1079fee21e36SBarry Smith 
1080c7afd0dbSLois Curfman McInnes    Input Parameters:
1081c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1082c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1083de044059SHong Zhang .  func - function evaluation routine
1084c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1085c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
10869b94acceSBarry Smith 
1087c7afd0dbSLois Curfman McInnes    Calling sequence of func:
10888d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1089c7afd0dbSLois Curfman McInnes 
1090313e4042SLois Curfman McInnes .  f - function vector
1091c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
10929b94acceSBarry Smith 
10939b94acceSBarry Smith    Notes:
10949b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
10959b94acceSBarry Smith $      f'(x) x = -f(x),
1096c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
10979b94acceSBarry Smith 
109836851e7fSLois Curfman McInnes    Level: beginner
109936851e7fSLois Curfman McInnes 
11009b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
11019b94acceSBarry Smith 
1102a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
11039b94acceSBarry Smith @*/
11047087cfbeSBarry Smith PetscErrorCode  SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
11059b94acceSBarry Smith {
110685385478SLisandro Dalcin   PetscErrorCode ierr;
11073a40ed3dSBarry Smith   PetscFunctionBegin;
11080700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1109d2a683ecSLisandro Dalcin   if (r) {
1110d2a683ecSLisandro Dalcin     PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1111d2a683ecSLisandro Dalcin     PetscCheckSameComm(snes,1,r,2);
111285385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
11136bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
111485385478SLisandro Dalcin     snes->vec_func = r;
1115d2a683ecSLisandro Dalcin   } else if (!snes->vec_func && snes->dm) {
1116d2a683ecSLisandro Dalcin     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1117d2a683ecSLisandro Dalcin   }
1118d2a683ecSLisandro Dalcin   if (func) snes->ops->computefunction = func;
1119d2a683ecSLisandro Dalcin   if (ctx)  snes->funP                 = ctx;
11203a40ed3dSBarry Smith   PetscFunctionReturn(0);
11219b94acceSBarry Smith }
11229b94acceSBarry Smith 
1123*d25893d9SBarry Smith #undef __FUNCT__
1124*d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess"
1125*d25893d9SBarry Smith /*@C
1126*d25893d9SBarry Smith    SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem
1127*d25893d9SBarry Smith 
1128*d25893d9SBarry Smith    Logically Collective on SNES
1129*d25893d9SBarry Smith 
1130*d25893d9SBarry Smith    Input Parameters:
1131*d25893d9SBarry Smith +  snes - the SNES context
1132*d25893d9SBarry Smith .  func - function evaluation routine
1133*d25893d9SBarry Smith -  ctx - [optional] user-defined context for private data for the
1134*d25893d9SBarry Smith          function evaluation routine (may be PETSC_NULL)
1135*d25893d9SBarry Smith 
1136*d25893d9SBarry Smith    Calling sequence of func:
1137*d25893d9SBarry Smith $    func (SNES snes,Vec x,void *ctx);
1138*d25893d9SBarry Smith 
1139*d25893d9SBarry Smith .  f - function vector
1140*d25893d9SBarry Smith -  ctx - optional user-defined function context
1141*d25893d9SBarry Smith 
1142*d25893d9SBarry Smith    Level: intermediate
1143*d25893d9SBarry Smith 
1144*d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function
1145*d25893d9SBarry Smith 
1146*d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
1147*d25893d9SBarry Smith @*/
1148*d25893d9SBarry Smith PetscErrorCode  SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx)
1149*d25893d9SBarry Smith {
1150*d25893d9SBarry Smith   PetscFunctionBegin;
1151*d25893d9SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1152*d25893d9SBarry Smith   if (func) snes->ops->computeinitialguess = func;
1153*d25893d9SBarry Smith   if (ctx)  snes->initialguessP            = ctx;
1154*d25893d9SBarry Smith   PetscFunctionReturn(0);
1155*d25893d9SBarry Smith }
1156*d25893d9SBarry Smith 
11573ab0aad5SBarry Smith /* --------------------------------------------------------------- */
11583ab0aad5SBarry Smith #undef __FUNCT__
11591096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
11601096aae1SMatthew Knepley /*@C
11611096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
11621096aae1SMatthew Knepley    it assumes a zero right hand side.
11631096aae1SMatthew Knepley 
11643f9fe445SBarry Smith    Logically Collective on SNES
11651096aae1SMatthew Knepley 
11661096aae1SMatthew Knepley    Input Parameter:
11671096aae1SMatthew Knepley .  snes - the SNES context
11681096aae1SMatthew Knepley 
11691096aae1SMatthew Knepley    Output Parameter:
1170bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
11711096aae1SMatthew Knepley 
11721096aae1SMatthew Knepley    Level: intermediate
11731096aae1SMatthew Knepley 
11741096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
11751096aae1SMatthew Knepley 
117685385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
11771096aae1SMatthew Knepley @*/
11787087cfbeSBarry Smith PetscErrorCode  SNESGetRhs(SNES snes,Vec *rhs)
11791096aae1SMatthew Knepley {
11801096aae1SMatthew Knepley   PetscFunctionBegin;
11810700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11821096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
118385385478SLisandro Dalcin   *rhs = snes->vec_rhs;
11841096aae1SMatthew Knepley   PetscFunctionReturn(0);
11851096aae1SMatthew Knepley }
11861096aae1SMatthew Knepley 
11871096aae1SMatthew Knepley #undef __FUNCT__
11884a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
11899b94acceSBarry Smith /*@
119036851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
11919b94acceSBarry Smith                          SNESSetFunction().
11929b94acceSBarry Smith 
1193c7afd0dbSLois Curfman McInnes    Collective on SNES
1194c7afd0dbSLois Curfman McInnes 
11959b94acceSBarry Smith    Input Parameters:
1196c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1197c7afd0dbSLois Curfman McInnes -  x - input vector
11989b94acceSBarry Smith 
11999b94acceSBarry Smith    Output Parameter:
12003638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
12019b94acceSBarry Smith 
12021bffabb2SLois Curfman McInnes    Notes:
120336851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
120436851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
120536851e7fSLois Curfman McInnes    themselves.
120636851e7fSLois Curfman McInnes 
120736851e7fSLois Curfman McInnes    Level: developer
120836851e7fSLois Curfman McInnes 
12099b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
12109b94acceSBarry Smith 
1211a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
12129b94acceSBarry Smith @*/
12137087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction(SNES snes,Vec x,Vec y)
12149b94acceSBarry Smith {
1215dfbe8321SBarry Smith   PetscErrorCode ierr;
12169b94acceSBarry Smith 
12173a40ed3dSBarry Smith   PetscFunctionBegin;
12180700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12190700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
12200700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1221c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1222c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1223184914b5SBarry Smith 
1224d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1225e7788613SBarry Smith   if (snes->ops->computefunction) {
1226d64ed03dSBarry Smith     PetscStackPush("SNES user function");
122739d508bbSBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1228d64ed03dSBarry Smith     PetscStackPop;
122985385478SLisandro Dalcin   } else if (snes->vec_rhs) {
12301096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
123117186662SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
123285385478SLisandro Dalcin   if (snes->vec_rhs) {
123385385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
12343ab0aad5SBarry Smith   }
1235ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1236d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
12373a40ed3dSBarry Smith   PetscFunctionReturn(0);
12389b94acceSBarry Smith }
12399b94acceSBarry Smith 
12404a2ae208SSatish Balay #undef __FUNCT__
12414a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
124262fef451SLois Curfman McInnes /*@
124362fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
124462fef451SLois Curfman McInnes    set with SNESSetJacobian().
124562fef451SLois Curfman McInnes 
1246c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1247c7afd0dbSLois Curfman McInnes 
124862fef451SLois Curfman McInnes    Input Parameters:
1249c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1250c7afd0dbSLois Curfman McInnes -  x - input vector
125162fef451SLois Curfman McInnes 
125262fef451SLois Curfman McInnes    Output Parameters:
1253c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
125462fef451SLois Curfman McInnes .  B - optional preconditioning matrix
12552b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1256fee21e36SBarry Smith 
1257e35cf81dSBarry Smith   Options Database Keys:
1258e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1259e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1260e35cf81dSBarry Smith 
126162fef451SLois Curfman McInnes    Notes:
126262fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
126362fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
126462fef451SLois Curfman McInnes 
126594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1266dc5a77f8SLois Curfman McInnes    flag parameter.
126762fef451SLois Curfman McInnes 
126836851e7fSLois Curfman McInnes    Level: developer
126936851e7fSLois Curfman McInnes 
127062fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
127162fef451SLois Curfman McInnes 
1272e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
127362fef451SLois Curfman McInnes @*/
12747087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
12759b94acceSBarry Smith {
1276dfbe8321SBarry Smith   PetscErrorCode ierr;
1277ace3abfcSBarry Smith   PetscBool      flag;
12783a40ed3dSBarry Smith 
12793a40ed3dSBarry Smith   PetscFunctionBegin;
12800700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12810700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
12824482741eSBarry Smith   PetscValidPointer(flg,5);
1283c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1284e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1285ebd3b9afSBarry Smith 
1286ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1287ebd3b9afSBarry Smith 
1288fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1289fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1290fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1291fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1292e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1293e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1294ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1295ebd3b9afSBarry Smith     if (flag) {
1296ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1297ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1298ebd3b9afSBarry Smith     }
1299e35cf81dSBarry Smith     PetscFunctionReturn(0);
1300e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1301e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1302e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);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   }
1310e35cf81dSBarry Smith 
1311c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1312e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1313d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1314e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1315d64ed03dSBarry Smith   PetscStackPop;
1316d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1317a8054027SBarry Smith 
13183b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
13193b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
13203b4f5425SBarry Smith     snes->lagpreconditioner = -1;
13213b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1322a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1323a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1324a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1325a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1326a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1327a8054027SBarry Smith   }
1328a8054027SBarry Smith 
13296d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
13300700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
13310700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
13323a40ed3dSBarry Smith   PetscFunctionReturn(0);
13339b94acceSBarry Smith }
13349b94acceSBarry Smith 
13354a2ae208SSatish Balay #undef __FUNCT__
13364a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
13379b94acceSBarry Smith /*@C
13389b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1339044dda88SLois Curfman McInnes    location to store the matrix.
13409b94acceSBarry Smith 
13413f9fe445SBarry Smith    Logically Collective on SNES and Mat
1342c7afd0dbSLois Curfman McInnes 
13439b94acceSBarry Smith    Input Parameters:
1344c7afd0dbSLois Curfman McInnes +  snes - the SNES context
13459b94acceSBarry Smith .  A - Jacobian matrix
13469b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
1347efd51863SBarry Smith .  func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value)
1348c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1349efd51863SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value)
13509b94acceSBarry Smith 
13519b94acceSBarry Smith    Calling sequence of func:
13528d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
13539b94acceSBarry Smith 
1354c7afd0dbSLois Curfman McInnes +  x - input vector
13559b94acceSBarry Smith .  A - Jacobian matrix
13569b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1357ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
13582b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1359c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
13609b94acceSBarry Smith 
13619b94acceSBarry Smith    Notes:
136294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
13632cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1364ac21db08SLois Curfman McInnes 
1365ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
13669b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
13679b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
13689b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
13699b94acceSBarry Smith    throughout the global iterations.
13709b94acceSBarry Smith 
137116913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
137216913363SBarry Smith    each matrix.
137316913363SBarry Smith 
1374a8a26c1eSJed Brown    If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument
1375a8a26c1eSJed Brown    must be a MatFDColoring.
1376a8a26c1eSJed Brown 
137736851e7fSLois Curfman McInnes    Level: beginner
137836851e7fSLois Curfman McInnes 
13799b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
13809b94acceSBarry Smith 
13813ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
13829b94acceSBarry Smith @*/
13837087cfbeSBarry Smith PetscErrorCode  SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
13849b94acceSBarry Smith {
1385dfbe8321SBarry Smith   PetscErrorCode ierr;
13863a7fca6bSBarry Smith 
13873a40ed3dSBarry Smith   PetscFunctionBegin;
13880700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13890700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
13900700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1391c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
139206975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1393e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
13943a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
13953a7fca6bSBarry Smith   if (A) {
13967dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
13976bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
13989b94acceSBarry Smith     snes->jacobian = A;
13993a7fca6bSBarry Smith   }
14003a7fca6bSBarry Smith   if (B) {
14017dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
14026bf464f9SBarry Smith     ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
14039b94acceSBarry Smith     snes->jacobian_pre = B;
14043a7fca6bSBarry Smith   }
14053a40ed3dSBarry Smith   PetscFunctionReturn(0);
14069b94acceSBarry Smith }
140762fef451SLois Curfman McInnes 
14084a2ae208SSatish Balay #undef __FUNCT__
14094a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1410c2aafc4cSSatish Balay /*@C
1411b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1412b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1413b4fd4287SBarry Smith 
1414c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1415c7afd0dbSLois Curfman McInnes 
1416b4fd4287SBarry Smith    Input Parameter:
1417b4fd4287SBarry Smith .  snes - the nonlinear solver context
1418b4fd4287SBarry Smith 
1419b4fd4287SBarry Smith    Output Parameters:
1420c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1421b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
142270e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
142370e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1424fee21e36SBarry Smith 
142536851e7fSLois Curfman McInnes    Level: advanced
142636851e7fSLois Curfman McInnes 
1427b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1428b4fd4287SBarry Smith @*/
14297087cfbeSBarry Smith PetscErrorCode  SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1430b4fd4287SBarry Smith {
14313a40ed3dSBarry Smith   PetscFunctionBegin;
14320700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1433b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1434b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1435e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
143670e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
14373a40ed3dSBarry Smith   PetscFunctionReturn(0);
1438b4fd4287SBarry Smith }
1439b4fd4287SBarry Smith 
14409b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
14419b94acceSBarry Smith 
14424a2ae208SSatish Balay #undef __FUNCT__
14434a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
14449b94acceSBarry Smith /*@
14459b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1446272ac6f2SLois Curfman McInnes    of a nonlinear solver.
14479b94acceSBarry Smith 
1448fee21e36SBarry Smith    Collective on SNES
1449fee21e36SBarry Smith 
1450c7afd0dbSLois Curfman McInnes    Input Parameters:
145170e92668SMatthew Knepley .  snes - the SNES context
1452c7afd0dbSLois Curfman McInnes 
1453272ac6f2SLois Curfman McInnes    Notes:
1454272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1455272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1456272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1457272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1458272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1459272ac6f2SLois Curfman McInnes 
146036851e7fSLois Curfman McInnes    Level: advanced
146136851e7fSLois Curfman McInnes 
14629b94acceSBarry Smith .keywords: SNES, nonlinear, setup
14639b94acceSBarry Smith 
14649b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
14659b94acceSBarry Smith @*/
14667087cfbeSBarry Smith PetscErrorCode  SNESSetUp(SNES snes)
14679b94acceSBarry Smith {
1468dfbe8321SBarry Smith   PetscErrorCode ierr;
14693a40ed3dSBarry Smith 
14703a40ed3dSBarry Smith   PetscFunctionBegin;
14710700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14724dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
14739b94acceSBarry Smith 
14747adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
147585385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
147685385478SLisandro Dalcin   }
147785385478SLisandro Dalcin 
1478efd51863SBarry Smith   if (!snes->vec_func && snes->dm && !snes->vec_rhs) {
1479efd51863SBarry Smith     ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr);
1480efd51863SBarry Smith   }
148117186662SBarry Smith   if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
148217186662SBarry Smith   if (!snes->ops->computefunction && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
148317186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
148458c9b817SLisandro Dalcin   if (snes->vec_rhs  == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector");
148558c9b817SLisandro Dalcin 
148658c9b817SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
148758c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_rhs, &snes->vec_func);CHKERRQ(ierr);
148858c9b817SLisandro Dalcin   }
148958c9b817SLisandro Dalcin   if (!snes->vec_sol_update /* && snes->vec_sol */) {
149058c9b817SLisandro Dalcin     ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr);
149158c9b817SLisandro Dalcin     ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr);
149258c9b817SLisandro Dalcin   }
149358c9b817SLisandro Dalcin 
1494ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1495ef8dffc7SBarry Smith     Mat           J;
1496ef8dffc7SBarry Smith     ISColoring    coloring;
1497ef8dffc7SBarry Smith     MatFDColoring fd;
1498a703fe33SLois Curfman McInnes 
1499ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1500ef8dffc7SBarry Smith     ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
1501ef8dffc7SBarry Smith     ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr);
1502ef8dffc7SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr);
1503ef8dffc7SBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr);
15046bf464f9SBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
1505efd51863SBarry Smith   } else if (snes->dm && !snes->jacobian_pre){
1506efd51863SBarry Smith     Mat J;
1507efd51863SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1508efd51863SBarry Smith     ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1509efd51863SBarry Smith     ierr = MatDestroy(&J);CHKERRQ(ierr);
1510ef8dffc7SBarry Smith   }
1511efd51863SBarry Smith 
1512b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1513b710008aSBarry Smith 
1514*d25893d9SBarry Smith   if (snes->ops->usercompute && !snes->user) {
1515*d25893d9SBarry Smith     ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr);
1516*d25893d9SBarry Smith   }
1517*d25893d9SBarry Smith 
1518410397dcSLisandro Dalcin   if (snes->ops->setup) {
1519410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1520410397dcSLisandro Dalcin   }
152158c9b817SLisandro Dalcin 
15227aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
15233a40ed3dSBarry Smith   PetscFunctionReturn(0);
15249b94acceSBarry Smith }
15259b94acceSBarry Smith 
15264a2ae208SSatish Balay #undef __FUNCT__
152737596af1SLisandro Dalcin #define __FUNCT__ "SNESReset"
152837596af1SLisandro Dalcin /*@
152937596af1SLisandro Dalcin    SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats
153037596af1SLisandro Dalcin 
153137596af1SLisandro Dalcin    Collective on SNES
153237596af1SLisandro Dalcin 
153337596af1SLisandro Dalcin    Input Parameter:
153437596af1SLisandro Dalcin .  snes - iterative context obtained from SNESCreate()
153537596af1SLisandro Dalcin 
1536*d25893d9SBarry Smith    Level: intermediate
1537*d25893d9SBarry Smith 
1538*d25893d9SBarry Smith    Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext()
153937596af1SLisandro Dalcin 
154037596af1SLisandro Dalcin .keywords: SNES, destroy
154137596af1SLisandro Dalcin 
154237596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
154337596af1SLisandro Dalcin @*/
154437596af1SLisandro Dalcin PetscErrorCode  SNESReset(SNES snes)
154537596af1SLisandro Dalcin {
154637596af1SLisandro Dalcin   PetscErrorCode ierr;
154737596af1SLisandro Dalcin 
154837596af1SLisandro Dalcin   PetscFunctionBegin;
154937596af1SLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1550*d25893d9SBarry Smith   if (snes->ops->userdestroy && snes->user) {
1551*d25893d9SBarry Smith     ierr       = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr);
1552*d25893d9SBarry Smith     snes->user = PETSC_NULL;
1553*d25893d9SBarry Smith   }
1554*d25893d9SBarry Smith 
155537596af1SLisandro Dalcin   if (snes->ops->reset) {
155637596af1SLisandro Dalcin     ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr);
155737596af1SLisandro Dalcin   }
155837596af1SLisandro Dalcin   if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);}
15596bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
15606bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
15616bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr);
15626bf464f9SBarry Smith   ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr);
15636bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr);
15646bf464f9SBarry Smith   ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr);
156537596af1SLisandro Dalcin   if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);}
156637596af1SLisandro Dalcin   if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);}
156737596af1SLisandro Dalcin   snes->nwork = snes->nvwork = 0;
156837596af1SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
156937596af1SLisandro Dalcin   PetscFunctionReturn(0);
157037596af1SLisandro Dalcin }
157137596af1SLisandro Dalcin 
157237596af1SLisandro Dalcin #undef __FUNCT__
15734a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
157452baeb72SSatish Balay /*@
15759b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
15769b94acceSBarry Smith    with SNESCreate().
15779b94acceSBarry Smith 
1578c7afd0dbSLois Curfman McInnes    Collective on SNES
1579c7afd0dbSLois Curfman McInnes 
15809b94acceSBarry Smith    Input Parameter:
15819b94acceSBarry Smith .  snes - the SNES context
15829b94acceSBarry Smith 
158336851e7fSLois Curfman McInnes    Level: beginner
158436851e7fSLois Curfman McInnes 
15859b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
15869b94acceSBarry Smith 
158763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
15889b94acceSBarry Smith @*/
15896bf464f9SBarry Smith PetscErrorCode  SNESDestroy(SNES *snes)
15909b94acceSBarry Smith {
15916849ba73SBarry Smith   PetscErrorCode ierr;
15923a40ed3dSBarry Smith 
15933a40ed3dSBarry Smith   PetscFunctionBegin;
15946bf464f9SBarry Smith   if (!*snes) PetscFunctionReturn(0);
15956bf464f9SBarry Smith   PetscValidHeaderSpecific((*snes),SNES_CLASSID,1);
15966bf464f9SBarry Smith   if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);}
1597d4bb536fSBarry Smith 
15986bf464f9SBarry Smith   ierr = SNESReset((*snes));CHKERRQ(ierr);
15996b8b9a38SLisandro Dalcin 
1600be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
16016bf464f9SBarry Smith   ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr);
16026bf464f9SBarry Smith   if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);}
16036d4c513bSLisandro Dalcin 
16046bf464f9SBarry Smith   ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr);
16056bf464f9SBarry Smith   ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr);
16066b8b9a38SLisandro Dalcin 
16076bf464f9SBarry Smith   ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr);
16086bf464f9SBarry Smith   if ((*snes)->ops->convergeddestroy) {
16096bf464f9SBarry Smith     ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr);
16106b8b9a38SLisandro Dalcin   }
16116bf464f9SBarry Smith   if ((*snes)->conv_malloc) {
16126bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr);
16136bf464f9SBarry Smith     ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr);
161458c9b817SLisandro Dalcin   }
16156bf464f9SBarry Smith   ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr);
1616a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
16173a40ed3dSBarry Smith   PetscFunctionReturn(0);
16189b94acceSBarry Smith }
16199b94acceSBarry Smith 
16209b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
16219b94acceSBarry Smith 
16224a2ae208SSatish Balay #undef __FUNCT__
1623a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1624a8054027SBarry Smith /*@
1625a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1626a8054027SBarry Smith 
16273f9fe445SBarry Smith    Logically Collective on SNES
1628a8054027SBarry Smith 
1629a8054027SBarry Smith    Input Parameters:
1630a8054027SBarry Smith +  snes - the SNES context
1631a8054027SBarry 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
16323b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1633a8054027SBarry Smith 
1634a8054027SBarry Smith    Options Database Keys:
1635a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1636a8054027SBarry Smith 
1637a8054027SBarry Smith    Notes:
1638a8054027SBarry Smith    The default is 1
1639a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1640a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1641a8054027SBarry Smith 
1642a8054027SBarry Smith    Level: intermediate
1643a8054027SBarry Smith 
1644a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1645a8054027SBarry Smith 
1646e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1647a8054027SBarry Smith 
1648a8054027SBarry Smith @*/
16497087cfbeSBarry Smith PetscErrorCode  SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1650a8054027SBarry Smith {
1651a8054027SBarry Smith   PetscFunctionBegin;
16520700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1653e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1654e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1655c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1656a8054027SBarry Smith   snes->lagpreconditioner = lag;
1657a8054027SBarry Smith   PetscFunctionReturn(0);
1658a8054027SBarry Smith }
1659a8054027SBarry Smith 
1660a8054027SBarry Smith #undef __FUNCT__
1661efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence"
1662efd51863SBarry Smith /*@
1663efd51863SBarry Smith    SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does
1664efd51863SBarry Smith 
1665efd51863SBarry Smith    Logically Collective on SNES
1666efd51863SBarry Smith 
1667efd51863SBarry Smith    Input Parameters:
1668efd51863SBarry Smith +  snes - the SNES context
1669efd51863SBarry Smith -  steps - the number of refinements to do, defaults to 0
1670efd51863SBarry Smith 
1671efd51863SBarry Smith    Options Database Keys:
1672efd51863SBarry Smith .    -snes_grid_sequence <steps>
1673efd51863SBarry Smith 
1674efd51863SBarry Smith    Level: intermediate
1675efd51863SBarry Smith 
1676efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1677efd51863SBarry Smith 
1678efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1679efd51863SBarry Smith 
1680efd51863SBarry Smith @*/
1681efd51863SBarry Smith PetscErrorCode  SNESSetGridSequence(SNES snes,PetscInt steps)
1682efd51863SBarry Smith {
1683efd51863SBarry Smith   PetscFunctionBegin;
1684efd51863SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1685efd51863SBarry Smith   PetscValidLogicalCollectiveInt(snes,steps,2);
1686efd51863SBarry Smith   snes->gridsequence = steps;
1687efd51863SBarry Smith   PetscFunctionReturn(0);
1688efd51863SBarry Smith }
1689efd51863SBarry Smith 
1690efd51863SBarry Smith #undef __FUNCT__
1691a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1692a8054027SBarry Smith /*@
1693a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1694a8054027SBarry Smith 
16953f9fe445SBarry Smith    Not Collective
1696a8054027SBarry Smith 
1697a8054027SBarry Smith    Input Parameter:
1698a8054027SBarry Smith .  snes - the SNES context
1699a8054027SBarry Smith 
1700a8054027SBarry Smith    Output Parameter:
1701a8054027SBarry 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
17023b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1703a8054027SBarry Smith 
1704a8054027SBarry Smith    Options Database Keys:
1705a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1706a8054027SBarry Smith 
1707a8054027SBarry Smith    Notes:
1708a8054027SBarry Smith    The default is 1
1709a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1710a8054027SBarry Smith 
1711a8054027SBarry Smith    Level: intermediate
1712a8054027SBarry Smith 
1713a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1714a8054027SBarry Smith 
1715a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1716a8054027SBarry Smith 
1717a8054027SBarry Smith @*/
17187087cfbeSBarry Smith PetscErrorCode  SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1719a8054027SBarry Smith {
1720a8054027SBarry Smith   PetscFunctionBegin;
17210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1722a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1723a8054027SBarry Smith   PetscFunctionReturn(0);
1724a8054027SBarry Smith }
1725a8054027SBarry Smith 
1726a8054027SBarry Smith #undef __FUNCT__
1727e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1728e35cf81dSBarry Smith /*@
1729e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1730e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1731e35cf81dSBarry Smith 
17323f9fe445SBarry Smith    Logically Collective on SNES
1733e35cf81dSBarry Smith 
1734e35cf81dSBarry Smith    Input Parameters:
1735e35cf81dSBarry Smith +  snes - the SNES context
1736e35cf81dSBarry 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
1737fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1738e35cf81dSBarry Smith 
1739e35cf81dSBarry Smith    Options Database Keys:
1740e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1741e35cf81dSBarry Smith 
1742e35cf81dSBarry Smith    Notes:
1743e35cf81dSBarry Smith    The default is 1
1744e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1745fe3ffe1eSBarry 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
1746fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1747e35cf81dSBarry Smith 
1748e35cf81dSBarry Smith    Level: intermediate
1749e35cf81dSBarry Smith 
1750e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1751e35cf81dSBarry Smith 
1752e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1753e35cf81dSBarry Smith 
1754e35cf81dSBarry Smith @*/
17557087cfbeSBarry Smith PetscErrorCode  SNESSetLagJacobian(SNES snes,PetscInt lag)
1756e35cf81dSBarry Smith {
1757e35cf81dSBarry Smith   PetscFunctionBegin;
17580700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1759e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1760e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1761c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1762e35cf81dSBarry Smith   snes->lagjacobian = lag;
1763e35cf81dSBarry Smith   PetscFunctionReturn(0);
1764e35cf81dSBarry Smith }
1765e35cf81dSBarry Smith 
1766e35cf81dSBarry Smith #undef __FUNCT__
1767e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1768e35cf81dSBarry Smith /*@
1769e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1770e35cf81dSBarry Smith 
17713f9fe445SBarry Smith    Not Collective
1772e35cf81dSBarry Smith 
1773e35cf81dSBarry Smith    Input Parameter:
1774e35cf81dSBarry Smith .  snes - the SNES context
1775e35cf81dSBarry Smith 
1776e35cf81dSBarry Smith    Output Parameter:
1777e35cf81dSBarry 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
1778e35cf81dSBarry Smith          the Jacobian is built etc.
1779e35cf81dSBarry Smith 
1780e35cf81dSBarry Smith    Options Database Keys:
1781e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1782e35cf81dSBarry Smith 
1783e35cf81dSBarry Smith    Notes:
1784e35cf81dSBarry Smith    The default is 1
1785e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1786e35cf81dSBarry Smith 
1787e35cf81dSBarry Smith    Level: intermediate
1788e35cf81dSBarry Smith 
1789e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1790e35cf81dSBarry Smith 
1791e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1792e35cf81dSBarry Smith 
1793e35cf81dSBarry Smith @*/
17947087cfbeSBarry Smith PetscErrorCode  SNESGetLagJacobian(SNES snes,PetscInt *lag)
1795e35cf81dSBarry Smith {
1796e35cf81dSBarry Smith   PetscFunctionBegin;
17970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1798e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1799e35cf81dSBarry Smith   PetscFunctionReturn(0);
1800e35cf81dSBarry Smith }
1801e35cf81dSBarry Smith 
1802e35cf81dSBarry Smith #undef __FUNCT__
18034a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
18049b94acceSBarry Smith /*@
1805d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
18069b94acceSBarry Smith 
18073f9fe445SBarry Smith    Logically Collective on SNES
1808c7afd0dbSLois Curfman McInnes 
18099b94acceSBarry Smith    Input Parameters:
1810c7afd0dbSLois Curfman McInnes +  snes - the SNES context
181170441072SBarry Smith .  abstol - absolute convergence tolerance
181233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
181333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
181433174efeSLois Curfman McInnes            of the change in the solution between steps
181533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1816c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1817fee21e36SBarry Smith 
181833174efeSLois Curfman McInnes    Options Database Keys:
181970441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1820c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1821c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1822c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1823c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
18249b94acceSBarry Smith 
1825d7a720efSLois Curfman McInnes    Notes:
18269b94acceSBarry Smith    The default maximum number of iterations is 50.
18279b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
18289b94acceSBarry Smith 
182936851e7fSLois Curfman McInnes    Level: intermediate
183036851e7fSLois Curfman McInnes 
183133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
18329b94acceSBarry Smith 
18332492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
18349b94acceSBarry Smith @*/
18357087cfbeSBarry Smith PetscErrorCode  SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
18369b94acceSBarry Smith {
18373a40ed3dSBarry Smith   PetscFunctionBegin;
18380700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1839c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
1840c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
1841c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
1842c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
1843c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
1844c5eb9154SBarry Smith 
184570441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1846d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1847d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1848d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1849d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
18503a40ed3dSBarry Smith   PetscFunctionReturn(0);
18519b94acceSBarry Smith }
18529b94acceSBarry Smith 
18534a2ae208SSatish Balay #undef __FUNCT__
18544a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
18559b94acceSBarry Smith /*@
185633174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
185733174efeSLois Curfman McInnes 
1858c7afd0dbSLois Curfman McInnes    Not Collective
1859c7afd0dbSLois Curfman McInnes 
186033174efeSLois Curfman McInnes    Input Parameters:
1861c7afd0dbSLois Curfman McInnes +  snes - the SNES context
186285385478SLisandro Dalcin .  atol - absolute convergence tolerance
186333174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
186433174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
186533174efeSLois Curfman McInnes            of the change in the solution between steps
186633174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1867c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1868fee21e36SBarry Smith 
186933174efeSLois Curfman McInnes    Notes:
187033174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
187133174efeSLois Curfman McInnes 
187236851e7fSLois Curfman McInnes    Level: intermediate
187336851e7fSLois Curfman McInnes 
187433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
187533174efeSLois Curfman McInnes 
187633174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
187733174efeSLois Curfman McInnes @*/
18787087cfbeSBarry Smith PetscErrorCode  SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
187933174efeSLois Curfman McInnes {
18803a40ed3dSBarry Smith   PetscFunctionBegin;
18810700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
188285385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
188333174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
188433174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
188533174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
188633174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
18873a40ed3dSBarry Smith   PetscFunctionReturn(0);
188833174efeSLois Curfman McInnes }
188933174efeSLois Curfman McInnes 
18904a2ae208SSatish Balay #undef __FUNCT__
18914a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
189233174efeSLois Curfman McInnes /*@
18939b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
18949b94acceSBarry Smith 
18953f9fe445SBarry Smith    Logically Collective on SNES
1896fee21e36SBarry Smith 
1897c7afd0dbSLois Curfman McInnes    Input Parameters:
1898c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1899c7afd0dbSLois Curfman McInnes -  tol - tolerance
1900c7afd0dbSLois Curfman McInnes 
19019b94acceSBarry Smith    Options Database Key:
1902c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
19039b94acceSBarry Smith 
190436851e7fSLois Curfman McInnes    Level: intermediate
190536851e7fSLois Curfman McInnes 
19069b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
19079b94acceSBarry Smith 
19082492ecdbSBarry Smith .seealso: SNESSetTolerances()
19099b94acceSBarry Smith @*/
19107087cfbeSBarry Smith PetscErrorCode  SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
19119b94acceSBarry Smith {
19123a40ed3dSBarry Smith   PetscFunctionBegin;
19130700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1914c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
19159b94acceSBarry Smith   snes->deltatol = tol;
19163a40ed3dSBarry Smith   PetscFunctionReturn(0);
19179b94acceSBarry Smith }
19189b94acceSBarry Smith 
1919df9fa365SBarry Smith /*
1920df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1921df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1922df9fa365SBarry Smith    macros instead of functions
1923df9fa365SBarry Smith */
19244a2ae208SSatish Balay #undef __FUNCT__
1925a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
19267087cfbeSBarry Smith PetscErrorCode  SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1927ce1608b8SBarry Smith {
1928dfbe8321SBarry Smith   PetscErrorCode ierr;
1929ce1608b8SBarry Smith 
1930ce1608b8SBarry Smith   PetscFunctionBegin;
19310700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1932a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1933ce1608b8SBarry Smith   PetscFunctionReturn(0);
1934ce1608b8SBarry Smith }
1935ce1608b8SBarry Smith 
19364a2ae208SSatish Balay #undef __FUNCT__
1937a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
19387087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1939df9fa365SBarry Smith {
1940dfbe8321SBarry Smith   PetscErrorCode ierr;
1941df9fa365SBarry Smith 
1942df9fa365SBarry Smith   PetscFunctionBegin;
1943a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1944df9fa365SBarry Smith   PetscFunctionReturn(0);
1945df9fa365SBarry Smith }
1946df9fa365SBarry Smith 
19474a2ae208SSatish Balay #undef __FUNCT__
1948a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
19496bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGDestroy(PetscDrawLG *draw)
1950df9fa365SBarry Smith {
1951dfbe8321SBarry Smith   PetscErrorCode ierr;
1952df9fa365SBarry Smith 
1953df9fa365SBarry Smith   PetscFunctionBegin;
1954a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1955df9fa365SBarry Smith   PetscFunctionReturn(0);
1956df9fa365SBarry Smith }
1957df9fa365SBarry Smith 
19587087cfbeSBarry Smith extern PetscErrorCode  SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1959b271bb04SBarry Smith #undef __FUNCT__
1960b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
19617087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1962b271bb04SBarry Smith {
1963b271bb04SBarry Smith   PetscDrawLG      lg;
1964b271bb04SBarry Smith   PetscErrorCode   ierr;
1965b271bb04SBarry Smith   PetscReal        x,y,per;
1966b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1967b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1968b271bb04SBarry Smith   PetscDraw        draw;
1969b271bb04SBarry Smith   PetscFunctionBegin;
1970b271bb04SBarry Smith   if (!monctx) {
1971b271bb04SBarry Smith     MPI_Comm    comm;
1972b271bb04SBarry Smith 
1973b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1974b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1975b271bb04SBarry Smith   }
1976b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1977b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1978b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1979b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1980b271bb04SBarry Smith   x = (PetscReal) n;
1981b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1982b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1983b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1984b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1985b271bb04SBarry Smith   }
1986b271bb04SBarry Smith 
1987b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1988b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1989b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1990b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1991b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1992b271bb04SBarry Smith   x = (PetscReal) n;
1993b271bb04SBarry Smith   y = 100.0*per;
1994b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1995b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1996b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1997b271bb04SBarry Smith   }
1998b271bb04SBarry Smith 
1999b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
2000b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2001b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2002b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
2003b271bb04SBarry Smith   x = (PetscReal) n;
2004b271bb04SBarry Smith   y = (prev - rnorm)/prev;
2005b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2006b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2007b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2008b271bb04SBarry Smith   }
2009b271bb04SBarry Smith 
2010b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
2011b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
2012b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
2013b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
2014b271bb04SBarry Smith   x = (PetscReal) n;
2015b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
2016b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
2017b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2018b271bb04SBarry Smith   }
2019b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
2020b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2021b271bb04SBarry Smith   }
2022b271bb04SBarry Smith   prev = rnorm;
2023b271bb04SBarry Smith   PetscFunctionReturn(0);
2024b271bb04SBarry Smith }
2025b271bb04SBarry Smith 
2026b271bb04SBarry Smith #undef __FUNCT__
2027b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
20287087cfbeSBarry Smith PetscErrorCode  SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2029b271bb04SBarry Smith {
2030b271bb04SBarry Smith   PetscErrorCode ierr;
2031b271bb04SBarry Smith 
2032b271bb04SBarry Smith   PetscFunctionBegin;
2033b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
2034b271bb04SBarry Smith   PetscFunctionReturn(0);
2035b271bb04SBarry Smith }
2036b271bb04SBarry Smith 
2037b271bb04SBarry Smith #undef __FUNCT__
2038b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
20396bf464f9SBarry Smith PetscErrorCode  SNESMonitorLGRangeDestroy(PetscDrawLG *draw)
2040b271bb04SBarry Smith {
2041b271bb04SBarry Smith   PetscErrorCode ierr;
2042b271bb04SBarry Smith 
2043b271bb04SBarry Smith   PetscFunctionBegin;
2044b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
2045b271bb04SBarry Smith   PetscFunctionReturn(0);
2046b271bb04SBarry Smith }
2047b271bb04SBarry Smith 
20487a03ce2fSLisandro Dalcin #undef __FUNCT__
20497a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor"
20507a03ce2fSLisandro Dalcin /*
20517a03ce2fSLisandro Dalcin      Runs the user provided monitor routines, if they exists.
20527a03ce2fSLisandro Dalcin */
20537a03ce2fSLisandro Dalcin PetscErrorCode  SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm)
20547a03ce2fSLisandro Dalcin {
20557a03ce2fSLisandro Dalcin   PetscErrorCode ierr;
20567a03ce2fSLisandro Dalcin   PetscInt       i,n = snes->numbermonitors;
20577a03ce2fSLisandro Dalcin 
20587a03ce2fSLisandro Dalcin   PetscFunctionBegin;
20597a03ce2fSLisandro Dalcin   for (i=0; i<n; i++) {
20607a03ce2fSLisandro Dalcin     ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr);
20617a03ce2fSLisandro Dalcin   }
20627a03ce2fSLisandro Dalcin   PetscFunctionReturn(0);
20637a03ce2fSLisandro Dalcin }
20647a03ce2fSLisandro Dalcin 
20659b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
20669b94acceSBarry Smith 
20674a2ae208SSatish Balay #undef __FUNCT__
2068a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
20699b94acceSBarry Smith /*@C
2070a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
20719b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
20729b94acceSBarry Smith    progress.
20739b94acceSBarry Smith 
20743f9fe445SBarry Smith    Logically Collective on SNES
2075fee21e36SBarry Smith 
2076c7afd0dbSLois Curfman McInnes    Input Parameters:
2077c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2078c7afd0dbSLois Curfman McInnes .  func - monitoring routine
2079b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
2080e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
2081b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
2082b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
20839b94acceSBarry Smith 
2084c7afd0dbSLois Curfman McInnes    Calling sequence of func:
2085a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
2086c7afd0dbSLois Curfman McInnes 
2087c7afd0dbSLois Curfman McInnes +    snes - the SNES context
2088c7afd0dbSLois Curfman McInnes .    its - iteration number
2089c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
209040a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
20919b94acceSBarry Smith 
20929665c990SLois Curfman McInnes    Options Database Keys:
2093a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
2094a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
2095a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
2096cca6129bSJed Brown -    -snes_monitor_cancel - cancels all monitors that have
2097c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
2098a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
2099c7afd0dbSLois Curfman McInnes                             does not cancel those set via
2100c7afd0dbSLois Curfman McInnes                             the options database.
21019665c990SLois Curfman McInnes 
2102639f9d9dSBarry Smith    Notes:
21036bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
2104a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
21056bc08f3fSLois Curfman McInnes    order in which they were set.
2106639f9d9dSBarry Smith 
2107025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
2108025f1a04SBarry Smith 
210936851e7fSLois Curfman McInnes    Level: intermediate
211036851e7fSLois Curfman McInnes 
21119b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
21129b94acceSBarry Smith 
2113a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
21149b94acceSBarry Smith @*/
2115c2efdce3SBarry Smith PetscErrorCode  SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
21169b94acceSBarry Smith {
2117b90d0a6eSBarry Smith   PetscInt i;
2118b90d0a6eSBarry Smith 
21193a40ed3dSBarry Smith   PetscFunctionBegin;
21200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
212117186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2122b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
2123b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
2124b90d0a6eSBarry Smith 
2125b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
2126b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
2127b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
2128b90d0a6eSBarry Smith         PetscErrorCode          ierr;
2129b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
2130b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
2131b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
2132c2efdce3SBarry Smith           ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr);
2133b90d0a6eSBarry Smith           PetscFunctionReturn(0);
2134b90d0a6eSBarry Smith         }
2135b90d0a6eSBarry Smith       }
2136b90d0a6eSBarry Smith     }
2137b90d0a6eSBarry Smith   }
2138b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
2139b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
2140639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
21413a40ed3dSBarry Smith   PetscFunctionReturn(0);
21429b94acceSBarry Smith }
21439b94acceSBarry Smith 
21444a2ae208SSatish Balay #undef __FUNCT__
2145a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
21465cd90555SBarry Smith /*@C
2147a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
21485cd90555SBarry Smith 
21493f9fe445SBarry Smith    Logically Collective on SNES
2150c7afd0dbSLois Curfman McInnes 
21515cd90555SBarry Smith    Input Parameters:
21525cd90555SBarry Smith .  snes - the SNES context
21535cd90555SBarry Smith 
21541a480d89SAdministrator    Options Database Key:
2155a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
2156a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
2157c7afd0dbSLois Curfman McInnes     set via the options database
21585cd90555SBarry Smith 
21595cd90555SBarry Smith    Notes:
21605cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
21615cd90555SBarry Smith 
216236851e7fSLois Curfman McInnes    Level: intermediate
216336851e7fSLois Curfman McInnes 
21645cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
21655cd90555SBarry Smith 
2166a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
21675cd90555SBarry Smith @*/
21687087cfbeSBarry Smith PetscErrorCode  SNESMonitorCancel(SNES snes)
21695cd90555SBarry Smith {
2170d952e501SBarry Smith   PetscErrorCode ierr;
2171d952e501SBarry Smith   PetscInt       i;
2172d952e501SBarry Smith 
21735cd90555SBarry Smith   PetscFunctionBegin;
21740700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2175d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2176d952e501SBarry Smith     if (snes->monitordestroy[i]) {
21773c4aec1bSBarry Smith       ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr);
2178d952e501SBarry Smith     }
2179d952e501SBarry Smith   }
21805cd90555SBarry Smith   snes->numbermonitors = 0;
21815cd90555SBarry Smith   PetscFunctionReturn(0);
21825cd90555SBarry Smith }
21835cd90555SBarry Smith 
21844a2ae208SSatish Balay #undef __FUNCT__
21854a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
21869b94acceSBarry Smith /*@C
21879b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
21889b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
21899b94acceSBarry Smith 
21903f9fe445SBarry Smith    Logically Collective on SNES
2191fee21e36SBarry Smith 
2192c7afd0dbSLois Curfman McInnes    Input Parameters:
2193c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2194c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
21957f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
21967f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
21979b94acceSBarry Smith 
2198c7afd0dbSLois Curfman McInnes    Calling sequence of func:
219906ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2200c7afd0dbSLois Curfman McInnes 
2201c7afd0dbSLois Curfman McInnes +    snes - the SNES context
220206ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2203c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2204184914b5SBarry Smith .    reason - reason for convergence/divergence
2205c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
22064b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
22074b27c08aSLois Curfman McInnes -    f - 2-norm of function
22089b94acceSBarry Smith 
220936851e7fSLois Curfman McInnes    Level: advanced
221036851e7fSLois Curfman McInnes 
22119b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
22129b94acceSBarry Smith 
221385385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
22149b94acceSBarry Smith @*/
22157087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
22169b94acceSBarry Smith {
22177f7931b9SBarry Smith   PetscErrorCode ierr;
22187f7931b9SBarry Smith 
22193a40ed3dSBarry Smith   PetscFunctionBegin;
22200700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
222185385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
22227f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
22237f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
22247f7931b9SBarry Smith   }
222585385478SLisandro Dalcin   snes->ops->converged        = func;
22267f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
222785385478SLisandro Dalcin   snes->cnvP                  = cctx;
22283a40ed3dSBarry Smith   PetscFunctionReturn(0);
22299b94acceSBarry Smith }
22309b94acceSBarry Smith 
22314a2ae208SSatish Balay #undef __FUNCT__
22324a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
223352baeb72SSatish Balay /*@
2234184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2235184914b5SBarry Smith 
2236184914b5SBarry Smith    Not Collective
2237184914b5SBarry Smith 
2238184914b5SBarry Smith    Input Parameter:
2239184914b5SBarry Smith .  snes - the SNES context
2240184914b5SBarry Smith 
2241184914b5SBarry Smith    Output Parameter:
22424d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2243184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2244184914b5SBarry Smith 
2245184914b5SBarry Smith    Level: intermediate
2246184914b5SBarry Smith 
2247184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2248184914b5SBarry Smith 
2249184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2250184914b5SBarry Smith 
225185385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2252184914b5SBarry Smith @*/
22537087cfbeSBarry Smith PetscErrorCode  SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2254184914b5SBarry Smith {
2255184914b5SBarry Smith   PetscFunctionBegin;
22560700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22574482741eSBarry Smith   PetscValidPointer(reason,2);
2258184914b5SBarry Smith   *reason = snes->reason;
2259184914b5SBarry Smith   PetscFunctionReturn(0);
2260184914b5SBarry Smith }
2261184914b5SBarry Smith 
22624a2ae208SSatish Balay #undef __FUNCT__
22634a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2264c9005455SLois Curfman McInnes /*@
2265c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2266c9005455SLois Curfman McInnes 
22673f9fe445SBarry Smith    Logically Collective on SNES
2268fee21e36SBarry Smith 
2269c7afd0dbSLois Curfman McInnes    Input Parameters:
2270c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
22718c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2272cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2273758f92a0SBarry Smith .  na  - size of a and its
227464731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2275758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2276c7afd0dbSLois Curfman McInnes 
2277308dcc3eSBarry Smith    Notes:
2278308dcc3eSBarry Smith    If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a
2279308dcc3eSBarry Smith    default array of length 10000 is allocated.
2280308dcc3eSBarry Smith 
2281c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2282c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2283c9005455SLois Curfman McInnes    during the section of code that is being timed.
2284c9005455SLois Curfman McInnes 
228536851e7fSLois Curfman McInnes    Level: intermediate
228636851e7fSLois Curfman McInnes 
2287c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2288758f92a0SBarry Smith 
228908405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2290758f92a0SBarry Smith 
2291c9005455SLois Curfman McInnes @*/
22927087cfbeSBarry Smith PetscErrorCode  SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool  reset)
2293c9005455SLois Curfman McInnes {
2294308dcc3eSBarry Smith   PetscErrorCode ierr;
2295308dcc3eSBarry Smith 
22963a40ed3dSBarry Smith   PetscFunctionBegin;
22970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22984482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2299a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2300308dcc3eSBarry Smith   if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) {
2301308dcc3eSBarry Smith     if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2302308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr);
2303308dcc3eSBarry Smith     ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr);
2304308dcc3eSBarry Smith     snes->conv_malloc   = PETSC_TRUE;
2305308dcc3eSBarry Smith   }
2306c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2307758f92a0SBarry Smith   snes->conv_hist_its   = its;
2308758f92a0SBarry Smith   snes->conv_hist_max   = na;
2309a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2310758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2311758f92a0SBarry Smith   PetscFunctionReturn(0);
2312758f92a0SBarry Smith }
2313758f92a0SBarry Smith 
2314308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2315c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
2316c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
2317308dcc3eSBarry Smith EXTERN_C_BEGIN
2318308dcc3eSBarry Smith #undef __FUNCT__
2319308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab"
2320308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes)
2321308dcc3eSBarry Smith {
2322308dcc3eSBarry Smith   mxArray        *mat;
2323308dcc3eSBarry Smith   PetscInt       i;
2324308dcc3eSBarry Smith   PetscReal      *ar;
2325308dcc3eSBarry Smith 
2326308dcc3eSBarry Smith   PetscFunctionBegin;
2327308dcc3eSBarry Smith   mat  = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL);
2328308dcc3eSBarry Smith   ar   = (PetscReal*) mxGetData(mat);
2329308dcc3eSBarry Smith   for (i=0; i<snes->conv_hist_len; i++) {
2330308dcc3eSBarry Smith     ar[i] = snes->conv_hist[i];
2331308dcc3eSBarry Smith   }
2332308dcc3eSBarry Smith   PetscFunctionReturn(mat);
2333308dcc3eSBarry Smith }
2334308dcc3eSBarry Smith EXTERN_C_END
2335308dcc3eSBarry Smith #endif
2336308dcc3eSBarry Smith 
2337308dcc3eSBarry Smith 
23384a2ae208SSatish Balay #undef __FUNCT__
23394a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
23400c4c9dddSBarry Smith /*@C
2341758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2342758f92a0SBarry Smith 
23433f9fe445SBarry Smith    Not Collective
2344758f92a0SBarry Smith 
2345758f92a0SBarry Smith    Input Parameter:
2346758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2347758f92a0SBarry Smith 
2348758f92a0SBarry Smith    Output Parameters:
2349758f92a0SBarry Smith .  a   - array to hold history
2350758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2351758f92a0SBarry Smith          negative if not converged) for each solve.
2352758f92a0SBarry Smith -  na  - size of a and its
2353758f92a0SBarry Smith 
2354758f92a0SBarry Smith    Notes:
2355758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2356758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2357758f92a0SBarry Smith 
2358758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2359758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2360758f92a0SBarry Smith    during the section of code that is being timed.
2361758f92a0SBarry Smith 
2362758f92a0SBarry Smith    Level: intermediate
2363758f92a0SBarry Smith 
2364758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2365758f92a0SBarry Smith 
2366758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2367758f92a0SBarry Smith 
2368758f92a0SBarry Smith @*/
23697087cfbeSBarry Smith PetscErrorCode  SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2370758f92a0SBarry Smith {
2371758f92a0SBarry Smith   PetscFunctionBegin;
23720700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2373758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2374758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2375758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
23763a40ed3dSBarry Smith   PetscFunctionReturn(0);
2377c9005455SLois Curfman McInnes }
2378c9005455SLois Curfman McInnes 
2379e74ef692SMatthew Knepley #undef __FUNCT__
2380e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2381ac226902SBarry Smith /*@C
238276b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
2383eec8f646SJed Brown   at the beginning of every iteration of the nonlinear solve. Specifically
23847e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
238576b2cf59SMatthew Knepley 
23863f9fe445SBarry Smith   Logically Collective on SNES
238776b2cf59SMatthew Knepley 
238876b2cf59SMatthew Knepley   Input Parameters:
238976b2cf59SMatthew Knepley . snes - The nonlinear solver context
239076b2cf59SMatthew Knepley . func - The function
239176b2cf59SMatthew Knepley 
239276b2cf59SMatthew Knepley   Calling sequence of func:
2393b5d30489SBarry Smith . func (SNES snes, PetscInt step);
239476b2cf59SMatthew Knepley 
239576b2cf59SMatthew Knepley . step - The current step of the iteration
239676b2cf59SMatthew Knepley 
2397fe97e370SBarry Smith   Level: advanced
2398fe97e370SBarry Smith 
2399fe97e370SBarry 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()
2400fe97e370SBarry Smith         This is not used by most users.
240176b2cf59SMatthew Knepley 
240276b2cf59SMatthew Knepley .keywords: SNES, update
2403b5d30489SBarry Smith 
240485385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
240576b2cf59SMatthew Knepley @*/
24067087cfbeSBarry Smith PetscErrorCode  SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
240776b2cf59SMatthew Knepley {
240876b2cf59SMatthew Knepley   PetscFunctionBegin;
24090700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2410e7788613SBarry Smith   snes->ops->update = func;
241176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
241276b2cf59SMatthew Knepley }
241376b2cf59SMatthew Knepley 
2414e74ef692SMatthew Knepley #undef __FUNCT__
2415e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
241676b2cf59SMatthew Knepley /*@
241776b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
241876b2cf59SMatthew Knepley 
241976b2cf59SMatthew Knepley   Not collective
242076b2cf59SMatthew Knepley 
242176b2cf59SMatthew Knepley   Input Parameters:
242276b2cf59SMatthew Knepley . snes - The nonlinear solver context
242376b2cf59SMatthew Knepley . step - The current step of the iteration
242476b2cf59SMatthew Knepley 
2425205452f4SMatthew Knepley   Level: intermediate
2426205452f4SMatthew Knepley 
242776b2cf59SMatthew Knepley .keywords: SNES, update
2428a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
242976b2cf59SMatthew Knepley @*/
24307087cfbeSBarry Smith PetscErrorCode  SNESDefaultUpdate(SNES snes, PetscInt step)
243176b2cf59SMatthew Knepley {
243276b2cf59SMatthew Knepley   PetscFunctionBegin;
243376b2cf59SMatthew Knepley   PetscFunctionReturn(0);
243476b2cf59SMatthew Knepley }
243576b2cf59SMatthew Knepley 
24364a2ae208SSatish Balay #undef __FUNCT__
24374a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
24389b94acceSBarry Smith /*
24399b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
24409b94acceSBarry Smith    positive parameter delta.
24419b94acceSBarry Smith 
24429b94acceSBarry Smith     Input Parameters:
2443c7afd0dbSLois Curfman McInnes +   snes - the SNES context
24449b94acceSBarry Smith .   y - approximate solution of linear system
24459b94acceSBarry Smith .   fnorm - 2-norm of current function
2446c7afd0dbSLois Curfman McInnes -   delta - trust region size
24479b94acceSBarry Smith 
24489b94acceSBarry Smith     Output Parameters:
2449c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
24509b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
24519b94acceSBarry Smith     region, and exceeds zero otherwise.
2452c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
24539b94acceSBarry Smith 
24549b94acceSBarry Smith     Note:
24554b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
24569b94acceSBarry Smith     is set to be the maximum allowable step size.
24579b94acceSBarry Smith 
24589b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
24599b94acceSBarry Smith */
2460dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
24619b94acceSBarry Smith {
2462064f8208SBarry Smith   PetscReal      nrm;
2463ea709b57SSatish Balay   PetscScalar    cnorm;
2464dfbe8321SBarry Smith   PetscErrorCode ierr;
24653a40ed3dSBarry Smith 
24663a40ed3dSBarry Smith   PetscFunctionBegin;
24670700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24680700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2469c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2470184914b5SBarry Smith 
2471064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2472064f8208SBarry Smith   if (nrm > *delta) {
2473064f8208SBarry Smith      nrm = *delta/nrm;
2474064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2475064f8208SBarry Smith      cnorm = nrm;
24762dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
24779b94acceSBarry Smith      *ynorm = *delta;
24789b94acceSBarry Smith   } else {
24799b94acceSBarry Smith      *gpnorm = 0.0;
2480064f8208SBarry Smith      *ynorm = nrm;
24819b94acceSBarry Smith   }
24823a40ed3dSBarry Smith   PetscFunctionReturn(0);
24839b94acceSBarry Smith }
24849b94acceSBarry Smith 
24854a2ae208SSatish Balay #undef __FUNCT__
24864a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
24876ce558aeSBarry Smith /*@C
2488f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2489f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
24909b94acceSBarry Smith 
2491c7afd0dbSLois Curfman McInnes    Collective on SNES
2492c7afd0dbSLois Curfman McInnes 
2493b2002411SLois Curfman McInnes    Input Parameters:
2494c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2495f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
249685385478SLisandro Dalcin -  x - the solution vector.
24979b94acceSBarry Smith 
2498b2002411SLois Curfman McInnes    Notes:
24998ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
25008ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
25018ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
25028ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
25038ddd3da0SLois Curfman McInnes 
250436851e7fSLois Curfman McInnes    Level: beginner
250536851e7fSLois Curfman McInnes 
25069b94acceSBarry Smith .keywords: SNES, nonlinear, solve
25079b94acceSBarry Smith 
250885385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
25099b94acceSBarry Smith @*/
25107087cfbeSBarry Smith PetscErrorCode  SNESSolve(SNES snes,Vec b,Vec x)
25119b94acceSBarry Smith {
2512dfbe8321SBarry Smith   PetscErrorCode ierr;
2513ace3abfcSBarry Smith   PetscBool      flg;
2514eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2515eabae89aSBarry Smith   PetscViewer    viewer;
2516efd51863SBarry Smith   PetscInt       grid;
2517052efed2SBarry Smith 
25183a40ed3dSBarry Smith   PetscFunctionBegin;
25190700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25200700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2521f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
25220700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
252385385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
252485385478SLisandro Dalcin 
2525efd51863SBarry Smith   for (grid=0; grid<snes->gridsequence+1; grid++) {
2526efd51863SBarry Smith 
252785385478SLisandro Dalcin     /* set solution vector */
2528efd51863SBarry Smith     if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);}
25296bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr);
253085385478SLisandro Dalcin     snes->vec_sol = x;
253185385478SLisandro Dalcin     /* set afine vector if provided */
253285385478SLisandro Dalcin     if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
25336bf464f9SBarry Smith     ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr);
253485385478SLisandro Dalcin     snes->vec_rhs = b;
253585385478SLisandro Dalcin 
253670e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
25373f149594SLisandro Dalcin 
2538*d25893d9SBarry Smith     if (!grid && snes->ops->computeinitialguess) {
2539*d25893d9SBarry Smith       ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr);
2540*d25893d9SBarry Smith     }
2541*d25893d9SBarry Smith 
2542abc0a331SBarry Smith     if (snes->conv_hist_reset) snes->conv_hist_len = 0;
254350ffb88aSMatthew Knepley     snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2544d5e45103SBarry Smith 
25453f149594SLisandro Dalcin     ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
25464936397dSBarry Smith     ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
254785385478SLisandro Dalcin     ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
25484936397dSBarry Smith     if (snes->domainerror){
25494936397dSBarry Smith       snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
25504936397dSBarry Smith       snes->domainerror = PETSC_FALSE;
25514936397dSBarry Smith     }
255217186662SBarry Smith     if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
25533f149594SLisandro Dalcin 
25547adad957SLisandro Dalcin     ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2555eabae89aSBarry Smith     if (flg && !PetscPreLoadingOn) {
25567adad957SLisandro Dalcin       ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2557eabae89aSBarry Smith       ierr = SNESView(snes,viewer);CHKERRQ(ierr);
25586bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2559eabae89aSBarry Smith     }
2560eabae89aSBarry Smith 
256190d69ab7SBarry Smith     flg  = PETSC_FALSE;
2562acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2563da9b6338SBarry Smith     if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
25645968eb51SBarry Smith     if (snes->printreason) {
25655968eb51SBarry Smith       if (snes->reason > 0) {
25667adad957SLisandro Dalcin         ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
25675968eb51SBarry Smith       } else {
25687adad957SLisandro Dalcin         ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
25695968eb51SBarry Smith       }
25705968eb51SBarry Smith     }
25715968eb51SBarry Smith 
2572e113a28aSBarry Smith     if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
2573efd51863SBarry Smith     if (grid <  snes->gridsequence) {
2574efd51863SBarry Smith       DM  fine;
2575efd51863SBarry Smith       Vec xnew;
2576efd51863SBarry Smith       Mat interp;
2577efd51863SBarry Smith 
2578efd51863SBarry Smith       ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr);
2579efd51863SBarry Smith       ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr);
2580efd51863SBarry Smith       ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr);
2581efd51863SBarry Smith       ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr);
2582efd51863SBarry Smith       ierr = MatDestroy(&interp);CHKERRQ(ierr);
2583efd51863SBarry Smith       x    = xnew;
2584efd51863SBarry Smith 
2585efd51863SBarry Smith       ierr = SNESReset(snes);CHKERRQ(ierr);
2586efd51863SBarry Smith       ierr = SNESSetDM(snes,fine);CHKERRQ(ierr);
2587efd51863SBarry Smith       ierr = DMDestroy(&fine);CHKERRQ(ierr);
2588efd51863SBarry Smith     }
2589efd51863SBarry Smith   }
25903a40ed3dSBarry Smith   PetscFunctionReturn(0);
25919b94acceSBarry Smith }
25929b94acceSBarry Smith 
25939b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
25949b94acceSBarry Smith 
25954a2ae208SSatish Balay #undef __FUNCT__
25964a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
259782bf6240SBarry Smith /*@C
25984b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
25999b94acceSBarry Smith 
2600fee21e36SBarry Smith    Collective on SNES
2601fee21e36SBarry Smith 
2602c7afd0dbSLois Curfman McInnes    Input Parameters:
2603c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2604454a90a3SBarry Smith -  type - a known method
2605c7afd0dbSLois Curfman McInnes 
2606c7afd0dbSLois Curfman McInnes    Options Database Key:
2607454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2608c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2609ae12b187SLois Curfman McInnes 
26109b94acceSBarry Smith    Notes:
2611e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
26124b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2613c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
26144b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2615c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
26169b94acceSBarry Smith 
2617ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2618ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2619ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2620ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2621ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2622ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2623ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2624ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2625ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2626b0a32e0cSBarry Smith   appropriate method.
262736851e7fSLois Curfman McInnes 
262836851e7fSLois Curfman McInnes   Level: intermediate
2629a703fe33SLois Curfman McInnes 
2630454a90a3SBarry Smith .keywords: SNES, set, type
2631435da068SBarry Smith 
2632435da068SBarry Smith .seealso: SNESType, SNESCreate()
2633435da068SBarry Smith 
26349b94acceSBarry Smith @*/
26357087cfbeSBarry Smith PetscErrorCode  SNESSetType(SNES snes,const SNESType type)
26369b94acceSBarry Smith {
2637dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
2638ace3abfcSBarry Smith   PetscBool      match;
26393a40ed3dSBarry Smith 
26403a40ed3dSBarry Smith   PetscFunctionBegin;
26410700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
26424482741eSBarry Smith   PetscValidCharPointer(type,2);
264382bf6240SBarry Smith 
26446831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
26450f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
264692ff6ae8SBarry Smith 
26474b91b6eaSBarry Smith   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2648e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
264975396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
265075396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
265175396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
265275396ef9SLisandro Dalcin   snes->ops->setup          = 0;
265375396ef9SLisandro Dalcin   snes->ops->solve          = 0;
265475396ef9SLisandro Dalcin   snes->ops->view           = 0;
265575396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
265675396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
265775396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
265875396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
2659454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
266003bfa161SLisandro Dalcin   ierr = (*r)(snes);CHKERRQ(ierr);
26619fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS)
26629fb22e1aSBarry Smith   if (PetscAMSPublishAll) {
26639fb22e1aSBarry Smith     ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr);
26649fb22e1aSBarry Smith   }
26659fb22e1aSBarry Smith #endif
26663a40ed3dSBarry Smith   PetscFunctionReturn(0);
26679b94acceSBarry Smith }
26689b94acceSBarry Smith 
2669a847f771SSatish Balay 
26709b94acceSBarry Smith /* --------------------------------------------------------------------- */
26714a2ae208SSatish Balay #undef __FUNCT__
26724a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
267352baeb72SSatish Balay /*@
26749b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2675f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
26769b94acceSBarry Smith 
2677fee21e36SBarry Smith    Not Collective
2678fee21e36SBarry Smith 
267936851e7fSLois Curfman McInnes    Level: advanced
268036851e7fSLois Curfman McInnes 
26819b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
26829b94acceSBarry Smith 
26839b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
26849b94acceSBarry Smith @*/
26857087cfbeSBarry Smith PetscErrorCode  SNESRegisterDestroy(void)
26869b94acceSBarry Smith {
2687dfbe8321SBarry Smith   PetscErrorCode ierr;
268882bf6240SBarry Smith 
26893a40ed3dSBarry Smith   PetscFunctionBegin;
26901441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
26914c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
26923a40ed3dSBarry Smith   PetscFunctionReturn(0);
26939b94acceSBarry Smith }
26949b94acceSBarry Smith 
26954a2ae208SSatish Balay #undef __FUNCT__
26964a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
26979b94acceSBarry Smith /*@C
26989a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
26999b94acceSBarry Smith 
2700c7afd0dbSLois Curfman McInnes    Not Collective
2701c7afd0dbSLois Curfman McInnes 
27029b94acceSBarry Smith    Input Parameter:
27034b0e389bSBarry Smith .  snes - nonlinear solver context
27049b94acceSBarry Smith 
27059b94acceSBarry Smith    Output Parameter:
27063a7fca6bSBarry Smith .  type - SNES method (a character string)
27079b94acceSBarry Smith 
270836851e7fSLois Curfman McInnes    Level: intermediate
270936851e7fSLois Curfman McInnes 
2710454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
27119b94acceSBarry Smith @*/
27127087cfbeSBarry Smith PetscErrorCode  SNESGetType(SNES snes,const SNESType *type)
27139b94acceSBarry Smith {
27143a40ed3dSBarry Smith   PetscFunctionBegin;
27150700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27164482741eSBarry Smith   PetscValidPointer(type,2);
27177adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
27183a40ed3dSBarry Smith   PetscFunctionReturn(0);
27199b94acceSBarry Smith }
27209b94acceSBarry Smith 
27214a2ae208SSatish Balay #undef __FUNCT__
27224a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
272352baeb72SSatish Balay /*@
27249b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
27259b94acceSBarry Smith    stored.
27269b94acceSBarry Smith 
2727c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2728c7afd0dbSLois Curfman McInnes 
27299b94acceSBarry Smith    Input Parameter:
27309b94acceSBarry Smith .  snes - the SNES context
27319b94acceSBarry Smith 
27329b94acceSBarry Smith    Output Parameter:
27339b94acceSBarry Smith .  x - the solution
27349b94acceSBarry Smith 
273570e92668SMatthew Knepley    Level: intermediate
273636851e7fSLois Curfman McInnes 
27379b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
27389b94acceSBarry Smith 
273985385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
27409b94acceSBarry Smith @*/
27417087cfbeSBarry Smith PetscErrorCode  SNESGetSolution(SNES snes,Vec *x)
27429b94acceSBarry Smith {
27433a40ed3dSBarry Smith   PetscFunctionBegin;
27440700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27454482741eSBarry Smith   PetscValidPointer(x,2);
274685385478SLisandro Dalcin   *x = snes->vec_sol;
274770e92668SMatthew Knepley   PetscFunctionReturn(0);
274870e92668SMatthew Knepley }
274970e92668SMatthew Knepley 
275070e92668SMatthew Knepley #undef __FUNCT__
27514a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
275252baeb72SSatish Balay /*@
27539b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
27549b94acceSBarry Smith    stored.
27559b94acceSBarry Smith 
2756c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2757c7afd0dbSLois Curfman McInnes 
27589b94acceSBarry Smith    Input Parameter:
27599b94acceSBarry Smith .  snes - the SNES context
27609b94acceSBarry Smith 
27619b94acceSBarry Smith    Output Parameter:
27629b94acceSBarry Smith .  x - the solution update
27639b94acceSBarry Smith 
276436851e7fSLois Curfman McInnes    Level: advanced
276536851e7fSLois Curfman McInnes 
27669b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
27679b94acceSBarry Smith 
276885385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
27699b94acceSBarry Smith @*/
27707087cfbeSBarry Smith PetscErrorCode  SNESGetSolutionUpdate(SNES snes,Vec *x)
27719b94acceSBarry Smith {
27723a40ed3dSBarry Smith   PetscFunctionBegin;
27730700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27744482741eSBarry Smith   PetscValidPointer(x,2);
277585385478SLisandro Dalcin   *x = snes->vec_sol_update;
27763a40ed3dSBarry Smith   PetscFunctionReturn(0);
27779b94acceSBarry Smith }
27789b94acceSBarry Smith 
27794a2ae208SSatish Balay #undef __FUNCT__
27804a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
27819b94acceSBarry Smith /*@C
27823638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
27839b94acceSBarry Smith 
2784c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2785c7afd0dbSLois Curfman McInnes 
27869b94acceSBarry Smith    Input Parameter:
27879b94acceSBarry Smith .  snes - the SNES context
27889b94acceSBarry Smith 
27899b94acceSBarry Smith    Output Parameter:
27907bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
279170e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
279270e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
27939b94acceSBarry Smith 
279436851e7fSLois Curfman McInnes    Level: advanced
279536851e7fSLois Curfman McInnes 
2796a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
27979b94acceSBarry Smith 
27984b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
27999b94acceSBarry Smith @*/
28007087cfbeSBarry Smith PetscErrorCode  SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
28019b94acceSBarry Smith {
28023a40ed3dSBarry Smith   PetscFunctionBegin;
28030700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
280485385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2805e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
280670e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
28073a40ed3dSBarry Smith   PetscFunctionReturn(0);
28089b94acceSBarry Smith }
28099b94acceSBarry Smith 
28104a2ae208SSatish Balay #undef __FUNCT__
28114a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
28123c7409f5SSatish Balay /*@C
28133c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2814d850072dSLois Curfman McInnes    SNES options in the database.
28153c7409f5SSatish Balay 
28163f9fe445SBarry Smith    Logically Collective on SNES
2817fee21e36SBarry Smith 
2818c7afd0dbSLois Curfman McInnes    Input Parameter:
2819c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2820c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2821c7afd0dbSLois Curfman McInnes 
2822d850072dSLois Curfman McInnes    Notes:
2823a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2824c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2825d850072dSLois Curfman McInnes 
282636851e7fSLois Curfman McInnes    Level: advanced
282736851e7fSLois Curfman McInnes 
28283c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2829a86d99e1SLois Curfman McInnes 
2830a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
28313c7409f5SSatish Balay @*/
28327087cfbeSBarry Smith PetscErrorCode  SNESSetOptionsPrefix(SNES snes,const char prefix[])
28333c7409f5SSatish Balay {
2834dfbe8321SBarry Smith   PetscErrorCode ierr;
28353c7409f5SSatish Balay 
28363a40ed3dSBarry Smith   PetscFunctionBegin;
28370700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2838639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
28391cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
284094b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
28413a40ed3dSBarry Smith   PetscFunctionReturn(0);
28423c7409f5SSatish Balay }
28433c7409f5SSatish Balay 
28444a2ae208SSatish Balay #undef __FUNCT__
28454a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
28463c7409f5SSatish Balay /*@C
2847f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2848d850072dSLois Curfman McInnes    SNES options in the database.
28493c7409f5SSatish Balay 
28503f9fe445SBarry Smith    Logically Collective on SNES
2851fee21e36SBarry Smith 
2852c7afd0dbSLois Curfman McInnes    Input Parameters:
2853c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2854c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2855c7afd0dbSLois Curfman McInnes 
2856d850072dSLois Curfman McInnes    Notes:
2857a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2858c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2859d850072dSLois Curfman McInnes 
286036851e7fSLois Curfman McInnes    Level: advanced
286136851e7fSLois Curfman McInnes 
28623c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2863a86d99e1SLois Curfman McInnes 
2864a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
28653c7409f5SSatish Balay @*/
28667087cfbeSBarry Smith PetscErrorCode  SNESAppendOptionsPrefix(SNES snes,const char prefix[])
28673c7409f5SSatish Balay {
2868dfbe8321SBarry Smith   PetscErrorCode ierr;
28693c7409f5SSatish Balay 
28703a40ed3dSBarry Smith   PetscFunctionBegin;
28710700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2872639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
28731cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
287494b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
28753a40ed3dSBarry Smith   PetscFunctionReturn(0);
28763c7409f5SSatish Balay }
28773c7409f5SSatish Balay 
28784a2ae208SSatish Balay #undef __FUNCT__
28794a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
28809ab63eb5SSatish Balay /*@C
28813c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
28823c7409f5SSatish Balay    SNES options in the database.
28833c7409f5SSatish Balay 
2884c7afd0dbSLois Curfman McInnes    Not Collective
2885c7afd0dbSLois Curfman McInnes 
28863c7409f5SSatish Balay    Input Parameter:
28873c7409f5SSatish Balay .  snes - the SNES context
28883c7409f5SSatish Balay 
28893c7409f5SSatish Balay    Output Parameter:
28903c7409f5SSatish Balay .  prefix - pointer to the prefix string used
28913c7409f5SSatish Balay 
28924ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
28939ab63eb5SSatish Balay    sufficient length to hold the prefix.
28949ab63eb5SSatish Balay 
289536851e7fSLois Curfman McInnes    Level: advanced
289636851e7fSLois Curfman McInnes 
28973c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2898a86d99e1SLois Curfman McInnes 
2899a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
29003c7409f5SSatish Balay @*/
29017087cfbeSBarry Smith PetscErrorCode  SNESGetOptionsPrefix(SNES snes,const char *prefix[])
29023c7409f5SSatish Balay {
2903dfbe8321SBarry Smith   PetscErrorCode ierr;
29043c7409f5SSatish Balay 
29053a40ed3dSBarry Smith   PetscFunctionBegin;
29060700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2907639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
29083a40ed3dSBarry Smith   PetscFunctionReturn(0);
29093c7409f5SSatish Balay }
29103c7409f5SSatish Balay 
2911b2002411SLois Curfman McInnes 
29124a2ae208SSatish Balay #undef __FUNCT__
29134a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
29143cea93caSBarry Smith /*@C
29153cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
29163cea93caSBarry Smith 
29177f6c08e0SMatthew Knepley   Level: advanced
29183cea93caSBarry Smith @*/
29197087cfbeSBarry Smith PetscErrorCode  SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2920b2002411SLois Curfman McInnes {
2921e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2922dfbe8321SBarry Smith   PetscErrorCode ierr;
2923b2002411SLois Curfman McInnes 
2924b2002411SLois Curfman McInnes   PetscFunctionBegin;
2925b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2926c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2927b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2928b2002411SLois Curfman McInnes }
2929da9b6338SBarry Smith 
2930da9b6338SBarry Smith #undef __FUNCT__
2931da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
29327087cfbeSBarry Smith PetscErrorCode  SNESTestLocalMin(SNES snes)
2933da9b6338SBarry Smith {
2934dfbe8321SBarry Smith   PetscErrorCode ierr;
293577431f27SBarry Smith   PetscInt       N,i,j;
2936da9b6338SBarry Smith   Vec            u,uh,fh;
2937da9b6338SBarry Smith   PetscScalar    value;
2938da9b6338SBarry Smith   PetscReal      norm;
2939da9b6338SBarry Smith 
2940da9b6338SBarry Smith   PetscFunctionBegin;
2941da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2942da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2943da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2944da9b6338SBarry Smith 
2945da9b6338SBarry Smith   /* currently only works for sequential */
2946da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2947da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2948da9b6338SBarry Smith   for (i=0; i<N; i++) {
2949da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
295077431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2951da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2952ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2953da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
29543ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2955da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
295677431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2957da9b6338SBarry Smith       value = -value;
2958da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2959da9b6338SBarry Smith     }
2960da9b6338SBarry Smith   }
29616bf464f9SBarry Smith   ierr = VecDestroy(&uh);CHKERRQ(ierr);
29626bf464f9SBarry Smith   ierr = VecDestroy(&fh);CHKERRQ(ierr);
2963da9b6338SBarry Smith   PetscFunctionReturn(0);
2964da9b6338SBarry Smith }
296571f87433Sdalcinl 
296671f87433Sdalcinl #undef __FUNCT__
2967fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
296871f87433Sdalcinl /*@
2969fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
297071f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
297171f87433Sdalcinl    Newton method.
297271f87433Sdalcinl 
29733f9fe445SBarry Smith    Logically Collective on SNES
297471f87433Sdalcinl 
297571f87433Sdalcinl    Input Parameters:
297671f87433Sdalcinl +  snes - SNES context
297771f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
297871f87433Sdalcinl 
297964ba62caSBarry Smith     Options Database:
298064ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
298164ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
298264ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
298364ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
298464ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
298564ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
298664ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
298764ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
298864ba62caSBarry Smith 
298971f87433Sdalcinl    Notes:
299071f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
299171f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
299271f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
299371f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
299471f87433Sdalcinl    solver.
299571f87433Sdalcinl 
299671f87433Sdalcinl    Level: advanced
299771f87433Sdalcinl 
299871f87433Sdalcinl    Reference:
299971f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
300071f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
300171f87433Sdalcinl 
300271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
300371f87433Sdalcinl 
3004fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
300571f87433Sdalcinl @*/
30067087cfbeSBarry Smith PetscErrorCode  SNESKSPSetUseEW(SNES snes,PetscBool  flag)
300771f87433Sdalcinl {
300871f87433Sdalcinl   PetscFunctionBegin;
30090700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3010acfcf0e5SJed Brown   PetscValidLogicalCollectiveBool(snes,flag,2);
301171f87433Sdalcinl   snes->ksp_ewconv = flag;
301271f87433Sdalcinl   PetscFunctionReturn(0);
301371f87433Sdalcinl }
301471f87433Sdalcinl 
301571f87433Sdalcinl #undef __FUNCT__
3016fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
301771f87433Sdalcinl /*@
3018fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
301971f87433Sdalcinl    for computing relative tolerance for linear solvers within an
302071f87433Sdalcinl    inexact Newton method.
302171f87433Sdalcinl 
302271f87433Sdalcinl    Not Collective
302371f87433Sdalcinl 
302471f87433Sdalcinl    Input Parameter:
302571f87433Sdalcinl .  snes - SNES context
302671f87433Sdalcinl 
302771f87433Sdalcinl    Output Parameter:
302871f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
302971f87433Sdalcinl 
303071f87433Sdalcinl    Notes:
303171f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
303271f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
303371f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
303471f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
303571f87433Sdalcinl    solver.
303671f87433Sdalcinl 
303771f87433Sdalcinl    Level: advanced
303871f87433Sdalcinl 
303971f87433Sdalcinl    Reference:
304071f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
304171f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
304271f87433Sdalcinl 
304371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
304471f87433Sdalcinl 
3045fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
304671f87433Sdalcinl @*/
30477087cfbeSBarry Smith PetscErrorCode  SNESKSPGetUseEW(SNES snes, PetscBool  *flag)
304871f87433Sdalcinl {
304971f87433Sdalcinl   PetscFunctionBegin;
30500700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
305171f87433Sdalcinl   PetscValidPointer(flag,2);
305271f87433Sdalcinl   *flag = snes->ksp_ewconv;
305371f87433Sdalcinl   PetscFunctionReturn(0);
305471f87433Sdalcinl }
305571f87433Sdalcinl 
305671f87433Sdalcinl #undef __FUNCT__
3057fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
305871f87433Sdalcinl /*@
3059fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
306071f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
306171f87433Sdalcinl    Newton method.
306271f87433Sdalcinl 
30633f9fe445SBarry Smith    Logically Collective on SNES
306471f87433Sdalcinl 
306571f87433Sdalcinl    Input Parameters:
306671f87433Sdalcinl +    snes - SNES context
306771f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
306871f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
306971f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
307071f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
307171f87433Sdalcinl              (0 <= gamma2 <= 1)
307271f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
307371f87433Sdalcinl .    alpha2 - power for safeguard
307471f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
307571f87433Sdalcinl 
307671f87433Sdalcinl    Note:
307771f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
307871f87433Sdalcinl 
307971f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
308071f87433Sdalcinl 
308171f87433Sdalcinl    Level: advanced
308271f87433Sdalcinl 
308371f87433Sdalcinl    Reference:
308471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
308571f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
308671f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
308771f87433Sdalcinl 
308871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
308971f87433Sdalcinl 
3090fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
309171f87433Sdalcinl @*/
30927087cfbeSBarry Smith PetscErrorCode  SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
309371f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
309471f87433Sdalcinl {
3095fa9f3622SBarry Smith   SNESKSPEW *kctx;
309671f87433Sdalcinl   PetscFunctionBegin;
30970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3098fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3099e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
3100c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
3101c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
3102c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
3103c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
3104c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
3105c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
3106c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
310771f87433Sdalcinl 
310871f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
310971f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
311071f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
311171f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
311271f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
311371f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
311471f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
311571f87433Sdalcinl 
311671f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
3117e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
311871f87433Sdalcinl   }
311971f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
3120e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
312171f87433Sdalcinl   }
312271f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
3123e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
312471f87433Sdalcinl   }
312571f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
3126e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
312771f87433Sdalcinl   }
312871f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
3129e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
313071f87433Sdalcinl   }
313171f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
3132e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
313371f87433Sdalcinl   }
313471f87433Sdalcinl   PetscFunctionReturn(0);
313571f87433Sdalcinl }
313671f87433Sdalcinl 
313771f87433Sdalcinl #undef __FUNCT__
3138fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
313971f87433Sdalcinl /*@
3140fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
314171f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
314271f87433Sdalcinl    Newton method.
314371f87433Sdalcinl 
314471f87433Sdalcinl    Not Collective
314571f87433Sdalcinl 
314671f87433Sdalcinl    Input Parameters:
314771f87433Sdalcinl      snes - SNES context
314871f87433Sdalcinl 
314971f87433Sdalcinl    Output Parameters:
315071f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
315171f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
315271f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
315371f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
315471f87433Sdalcinl              (0 <= gamma2 <= 1)
315571f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
315671f87433Sdalcinl .    alpha2 - power for safeguard
315771f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
315871f87433Sdalcinl 
315971f87433Sdalcinl    Level: advanced
316071f87433Sdalcinl 
316171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
316271f87433Sdalcinl 
3163fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
316471f87433Sdalcinl @*/
31657087cfbeSBarry Smith PetscErrorCode  SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
316671f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
316771f87433Sdalcinl {
3168fa9f3622SBarry Smith   SNESKSPEW *kctx;
316971f87433Sdalcinl   PetscFunctionBegin;
31700700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3171fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
3172e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
317371f87433Sdalcinl   if(version)   *version   = kctx->version;
317471f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
317571f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
317671f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
317771f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
317871f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
317971f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
318071f87433Sdalcinl   PetscFunctionReturn(0);
318171f87433Sdalcinl }
318271f87433Sdalcinl 
318371f87433Sdalcinl #undef __FUNCT__
3184fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
3185fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
318671f87433Sdalcinl {
318771f87433Sdalcinl   PetscErrorCode ierr;
3188fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
318971f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
319071f87433Sdalcinl 
319171f87433Sdalcinl   PetscFunctionBegin;
3192e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
319371f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
319471f87433Sdalcinl     rtol = kctx->rtol_0;
319571f87433Sdalcinl   } else {
319671f87433Sdalcinl     if (kctx->version == 1) {
319771f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
319871f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
319971f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
320071f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
320171f87433Sdalcinl     } else if (kctx->version == 2) {
320271f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
320371f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
320471f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
320571f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
320671f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
320771f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
320871f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
320971f87433Sdalcinl       stol = PetscMax(rtol,stol);
321071f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
321171f87433Sdalcinl       /* safeguard: avoid oversolving */
321271f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
321371f87433Sdalcinl       stol = PetscMax(rtol,stol);
321471f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
3215e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
321671f87433Sdalcinl   }
321771f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
321871f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
321971f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
322071f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
322171f87433Sdalcinl   PetscFunctionReturn(0);
322271f87433Sdalcinl }
322371f87433Sdalcinl 
322471f87433Sdalcinl #undef __FUNCT__
3225fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
3226fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
322771f87433Sdalcinl {
322871f87433Sdalcinl   PetscErrorCode ierr;
3229fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
323071f87433Sdalcinl   PCSide         pcside;
323171f87433Sdalcinl   Vec            lres;
323271f87433Sdalcinl 
323371f87433Sdalcinl   PetscFunctionBegin;
3234e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
323571f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
323671f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
323771f87433Sdalcinl   if (kctx->version == 1) {
3238b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
323971f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
324071f87433Sdalcinl       /* KSP residual is true linear residual */
324171f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
324271f87433Sdalcinl     } else {
324371f87433Sdalcinl       /* KSP residual is preconditioned residual */
324471f87433Sdalcinl       /* compute true linear residual norm */
324571f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
324671f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
324771f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
324871f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
32496bf464f9SBarry Smith       ierr = VecDestroy(&lres);CHKERRQ(ierr);
325071f87433Sdalcinl     }
325171f87433Sdalcinl   }
325271f87433Sdalcinl   PetscFunctionReturn(0);
325371f87433Sdalcinl }
325471f87433Sdalcinl 
325571f87433Sdalcinl #undef __FUNCT__
325671f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
325771f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
325871f87433Sdalcinl {
325971f87433Sdalcinl   PetscErrorCode ierr;
326071f87433Sdalcinl 
326171f87433Sdalcinl   PetscFunctionBegin;
3262fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
326371f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3264fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
326571f87433Sdalcinl   PetscFunctionReturn(0);
326671f87433Sdalcinl }
32676c699258SBarry Smith 
32686c699258SBarry Smith #undef __FUNCT__
32696c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
32706c699258SBarry Smith /*@
32716c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
32726c699258SBarry Smith 
32733f9fe445SBarry Smith    Logically Collective on SNES
32746c699258SBarry Smith 
32756c699258SBarry Smith    Input Parameters:
32766c699258SBarry Smith +  snes - the preconditioner context
32776c699258SBarry Smith -  dm - the dm
32786c699258SBarry Smith 
32796c699258SBarry Smith    Level: intermediate
32806c699258SBarry Smith 
32816c699258SBarry Smith 
32826c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
32836c699258SBarry Smith @*/
32847087cfbeSBarry Smith PetscErrorCode  SNESSetDM(SNES snes,DM dm)
32856c699258SBarry Smith {
32866c699258SBarry Smith   PetscErrorCode ierr;
3287345fed2cSBarry Smith   KSP            ksp;
32886c699258SBarry Smith 
32896c699258SBarry Smith   PetscFunctionBegin;
32900700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3291d0660788SBarry Smith   if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);}
32926bf464f9SBarry Smith   ierr = DMDestroy(&snes->dm);CHKERRQ(ierr);
32936c699258SBarry Smith   snes->dm = dm;
3294345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3295345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3296f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
32976c699258SBarry Smith   PetscFunctionReturn(0);
32986c699258SBarry Smith }
32996c699258SBarry Smith 
33006c699258SBarry Smith #undef __FUNCT__
33016c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
33026c699258SBarry Smith /*@
33036c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
33046c699258SBarry Smith 
33053f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
33066c699258SBarry Smith 
33076c699258SBarry Smith    Input Parameter:
33086c699258SBarry Smith . snes - the preconditioner context
33096c699258SBarry Smith 
33106c699258SBarry Smith    Output Parameter:
33116c699258SBarry Smith .  dm - the dm
33126c699258SBarry Smith 
33136c699258SBarry Smith    Level: intermediate
33146c699258SBarry Smith 
33156c699258SBarry Smith 
33166c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
33176c699258SBarry Smith @*/
33187087cfbeSBarry Smith PetscErrorCode  SNESGetDM(SNES snes,DM *dm)
33196c699258SBarry Smith {
33206c699258SBarry Smith   PetscFunctionBegin;
33210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33226c699258SBarry Smith   *dm = snes->dm;
33236c699258SBarry Smith   PetscFunctionReturn(0);
33246c699258SBarry Smith }
33250807856dSBarry Smith 
332669b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3327c6db04a5SJed Brown #include <mex.h>
332869b4f73cSBarry Smith 
33298f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
33308f6e6473SBarry Smith 
33310807856dSBarry Smith #undef __FUNCT__
33320807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab"
33330807856dSBarry Smith /*
33340807856dSBarry Smith    SNESComputeFunction_Matlab - Calls the function that has been set with
33350807856dSBarry Smith                          SNESSetFunctionMatlab().
33360807856dSBarry Smith 
33370807856dSBarry Smith    Collective on SNES
33380807856dSBarry Smith 
33390807856dSBarry Smith    Input Parameters:
33400807856dSBarry Smith +  snes - the SNES context
33410807856dSBarry Smith -  x - input vector
33420807856dSBarry Smith 
33430807856dSBarry Smith    Output Parameter:
33440807856dSBarry Smith .  y - function vector, as set by SNESSetFunction()
33450807856dSBarry Smith 
33460807856dSBarry Smith    Notes:
33470807856dSBarry Smith    SNESComputeFunction() is typically used within nonlinear solvers
33480807856dSBarry Smith    implementations, so most users would not generally call this routine
33490807856dSBarry Smith    themselves.
33500807856dSBarry Smith 
33510807856dSBarry Smith    Level: developer
33520807856dSBarry Smith 
33530807856dSBarry Smith .keywords: SNES, nonlinear, compute, function
33540807856dSBarry Smith 
33550807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
335661b2408cSBarry Smith */
33577087cfbeSBarry Smith PetscErrorCode  SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx)
33580807856dSBarry Smith {
3359e650e774SBarry Smith   PetscErrorCode    ierr;
33608f6e6473SBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
33618f6e6473SBarry Smith   int               nlhs = 1,nrhs = 5;
33628f6e6473SBarry Smith   mxArray	    *plhs[1],*prhs[5];
336391621f2eSBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
3364e650e774SBarry Smith 
33650807856dSBarry Smith   PetscFunctionBegin;
33660807856dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33670807856dSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
33680807856dSBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
33690807856dSBarry Smith   PetscCheckSameComm(snes,1,x,2);
33700807856dSBarry Smith   PetscCheckSameComm(snes,1,y,3);
33710807856dSBarry Smith 
33720807856dSBarry Smith   /* call Matlab function in ctx with arguments x and y */
3373e650e774SBarry Smith 
337491621f2eSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3375e650e774SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3376e650e774SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
337791621f2eSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
337891621f2eSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
337991621f2eSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
33808f6e6473SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
33818f6e6473SBarry Smith   prhs[4] =  sctx->ctx;
3382b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr);
3383e650e774SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3384e650e774SBarry Smith   mxDestroyArray(prhs[0]);
3385e650e774SBarry Smith   mxDestroyArray(prhs[1]);
3386e650e774SBarry Smith   mxDestroyArray(prhs[2]);
33878f6e6473SBarry Smith   mxDestroyArray(prhs[3]);
3388e650e774SBarry Smith   mxDestroyArray(plhs[0]);
33890807856dSBarry Smith   PetscFunctionReturn(0);
33900807856dSBarry Smith }
33910807856dSBarry Smith 
33920807856dSBarry Smith 
33930807856dSBarry Smith #undef __FUNCT__
33940807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab"
339561b2408cSBarry Smith /*
33960807856dSBarry Smith    SNESSetFunctionMatlab - Sets the function evaluation routine and function
33970807856dSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3398e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
33990807856dSBarry Smith 
34000807856dSBarry Smith    Logically Collective on SNES
34010807856dSBarry Smith 
34020807856dSBarry Smith    Input Parameters:
34030807856dSBarry Smith +  snes - the SNES context
34040807856dSBarry Smith .  r - vector to store function value
34050807856dSBarry Smith -  func - function evaluation routine
34060807856dSBarry Smith 
34070807856dSBarry Smith    Calling sequence of func:
340861b2408cSBarry Smith $    func (SNES snes,Vec x,Vec f,void *ctx);
34090807856dSBarry Smith 
34100807856dSBarry Smith 
34110807856dSBarry Smith    Notes:
34120807856dSBarry Smith    The Newton-like methods typically solve linear systems of the form
34130807856dSBarry Smith $      f'(x) x = -f(x),
34140807856dSBarry Smith    where f'(x) denotes the Jacobian matrix and f(x) is the function.
34150807856dSBarry Smith 
34160807856dSBarry Smith    Level: beginner
34170807856dSBarry Smith 
34180807856dSBarry Smith .keywords: SNES, nonlinear, set, function
34190807856dSBarry Smith 
34200807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
342161b2408cSBarry Smith */
34227087cfbeSBarry Smith PetscErrorCode  SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx)
34230807856dSBarry Smith {
34240807856dSBarry Smith   PetscErrorCode    ierr;
34258f6e6473SBarry Smith   SNESMatlabContext *sctx;
34260807856dSBarry Smith 
34270807856dSBarry Smith   PetscFunctionBegin;
34288f6e6473SBarry Smith   /* currently sctx is memory bleed */
34298f6e6473SBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
34308f6e6473SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
34318f6e6473SBarry Smith   /*
34328f6e6473SBarry Smith      This should work, but it doesn't
34338f6e6473SBarry Smith   sctx->ctx = ctx;
34348f6e6473SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
34358f6e6473SBarry Smith   */
34368f6e6473SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
34378f6e6473SBarry Smith   ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr);
34380807856dSBarry Smith   PetscFunctionReturn(0);
34390807856dSBarry Smith }
344069b4f73cSBarry Smith 
344161b2408cSBarry Smith #undef __FUNCT__
344261b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab"
344361b2408cSBarry Smith /*
344461b2408cSBarry Smith    SNESComputeJacobian_Matlab - Calls the function that has been set with
344561b2408cSBarry Smith                          SNESSetJacobianMatlab().
344661b2408cSBarry Smith 
344761b2408cSBarry Smith    Collective on SNES
344861b2408cSBarry Smith 
344961b2408cSBarry Smith    Input Parameters:
345061b2408cSBarry Smith +  snes - the SNES context
345161b2408cSBarry Smith .  x - input vector
345261b2408cSBarry Smith .  A, B - the matrices
345361b2408cSBarry Smith -  ctx - user context
345461b2408cSBarry Smith 
345561b2408cSBarry Smith    Output Parameter:
345661b2408cSBarry Smith .  flag - structure of the matrix
345761b2408cSBarry Smith 
345861b2408cSBarry Smith    Level: developer
345961b2408cSBarry Smith 
346061b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function
346161b2408cSBarry Smith 
346261b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction()
346361b2408cSBarry Smith @*/
34647087cfbeSBarry Smith PetscErrorCode  SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx)
346561b2408cSBarry Smith {
346661b2408cSBarry Smith   PetscErrorCode    ierr;
346761b2408cSBarry Smith   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
346861b2408cSBarry Smith   int               nlhs = 2,nrhs = 6;
346961b2408cSBarry Smith   mxArray	    *plhs[2],*prhs[6];
347061b2408cSBarry Smith   long long int     lx = 0,lA = 0,ls = 0, lB = 0;
347161b2408cSBarry Smith 
347261b2408cSBarry Smith   PetscFunctionBegin;
347361b2408cSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
347461b2408cSBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
347561b2408cSBarry Smith 
347661b2408cSBarry Smith   /* call Matlab function in ctx with arguments x and y */
347761b2408cSBarry Smith 
347861b2408cSBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
347961b2408cSBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
348061b2408cSBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
348161b2408cSBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
348261b2408cSBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
348361b2408cSBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
348461b2408cSBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
348561b2408cSBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
348661b2408cSBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
348761b2408cSBarry Smith   prhs[5] =  sctx->ctx;
3488b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr);
348961b2408cSBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
349061b2408cSBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
349161b2408cSBarry Smith   mxDestroyArray(prhs[0]);
349261b2408cSBarry Smith   mxDestroyArray(prhs[1]);
349361b2408cSBarry Smith   mxDestroyArray(prhs[2]);
349461b2408cSBarry Smith   mxDestroyArray(prhs[3]);
349561b2408cSBarry Smith   mxDestroyArray(prhs[4]);
349661b2408cSBarry Smith   mxDestroyArray(plhs[0]);
349761b2408cSBarry Smith   mxDestroyArray(plhs[1]);
349861b2408cSBarry Smith   PetscFunctionReturn(0);
349961b2408cSBarry Smith }
350061b2408cSBarry Smith 
350161b2408cSBarry Smith 
350261b2408cSBarry Smith #undef __FUNCT__
350361b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab"
350461b2408cSBarry Smith /*
350561b2408cSBarry Smith    SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
350661b2408cSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
3507e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
350861b2408cSBarry Smith 
350961b2408cSBarry Smith    Logically Collective on SNES
351061b2408cSBarry Smith 
351161b2408cSBarry Smith    Input Parameters:
351261b2408cSBarry Smith +  snes - the SNES context
351361b2408cSBarry Smith .  A,B - Jacobian matrices
351461b2408cSBarry Smith .  func - function evaluation routine
351561b2408cSBarry Smith -  ctx - user context
351661b2408cSBarry Smith 
351761b2408cSBarry Smith    Calling sequence of func:
351861b2408cSBarry Smith $    flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx);
351961b2408cSBarry Smith 
352061b2408cSBarry Smith 
352161b2408cSBarry Smith    Level: developer
352261b2408cSBarry Smith 
352361b2408cSBarry Smith .keywords: SNES, nonlinear, set, function
352461b2408cSBarry Smith 
352561b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
352661b2408cSBarry Smith */
35277087cfbeSBarry Smith PetscErrorCode  SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx)
352861b2408cSBarry Smith {
352961b2408cSBarry Smith   PetscErrorCode    ierr;
353061b2408cSBarry Smith   SNESMatlabContext *sctx;
353161b2408cSBarry Smith 
353261b2408cSBarry Smith   PetscFunctionBegin;
353361b2408cSBarry Smith   /* currently sctx is memory bleed */
353461b2408cSBarry Smith   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
353561b2408cSBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
353661b2408cSBarry Smith   /*
353761b2408cSBarry Smith      This should work, but it doesn't
353861b2408cSBarry Smith   sctx->ctx = ctx;
353961b2408cSBarry Smith   mexMakeArrayPersistent(sctx->ctx);
354061b2408cSBarry Smith   */
354161b2408cSBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
354261b2408cSBarry Smith   ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
354361b2408cSBarry Smith   PetscFunctionReturn(0);
354461b2408cSBarry Smith }
354569b4f73cSBarry Smith 
3546f9eb7ae2SShri Abhyankar #undef __FUNCT__
3547f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab"
3548f9eb7ae2SShri Abhyankar /*
3549f9eb7ae2SShri Abhyankar    SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab().
3550f9eb7ae2SShri Abhyankar 
3551f9eb7ae2SShri Abhyankar    Collective on SNES
3552f9eb7ae2SShri Abhyankar 
3553f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction()
3554f9eb7ae2SShri Abhyankar @*/
35557087cfbeSBarry Smith PetscErrorCode  SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx)
3556f9eb7ae2SShri Abhyankar {
3557f9eb7ae2SShri Abhyankar   PetscErrorCode  ierr;
355848f37e70SShri Abhyankar   SNESMatlabContext *sctx = (SNESMatlabContext *)ctx;
3559f9eb7ae2SShri Abhyankar   int             nlhs = 1,nrhs = 6;
3560f9eb7ae2SShri Abhyankar   mxArray	  *plhs[1],*prhs[6];
3561f9eb7ae2SShri Abhyankar   long long int   lx = 0,ls = 0;
3562f9eb7ae2SShri Abhyankar   Vec             x=snes->vec_sol;
3563f9eb7ae2SShri Abhyankar 
3564f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3565f9eb7ae2SShri Abhyankar   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
3566f9eb7ae2SShri Abhyankar 
3567f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3568f9eb7ae2SShri Abhyankar   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3569f9eb7ae2SShri Abhyankar   prhs[0] =  mxCreateDoubleScalar((double)ls);
3570f9eb7ae2SShri Abhyankar   prhs[1] =  mxCreateDoubleScalar((double)it);
3571f9eb7ae2SShri Abhyankar   prhs[2] =  mxCreateDoubleScalar((double)fnorm);
3572f9eb7ae2SShri Abhyankar   prhs[3] =  mxCreateDoubleScalar((double)lx);
3573f9eb7ae2SShri Abhyankar   prhs[4] =  mxCreateString(sctx->funcname);
3574f9eb7ae2SShri Abhyankar   prhs[5] =  sctx->ctx;
3575f9eb7ae2SShri Abhyankar   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr);
3576f9eb7ae2SShri Abhyankar   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3577f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[0]);
3578f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[1]);
3579f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[2]);
3580f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[3]);
3581f9eb7ae2SShri Abhyankar   mxDestroyArray(prhs[4]);
3582f9eb7ae2SShri Abhyankar   mxDestroyArray(plhs[0]);
3583f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3584f9eb7ae2SShri Abhyankar }
3585f9eb7ae2SShri Abhyankar 
3586f9eb7ae2SShri Abhyankar 
3587f9eb7ae2SShri Abhyankar #undef __FUNCT__
3588f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab"
3589f9eb7ae2SShri Abhyankar /*
3590e3c5b3baSBarry Smith    SNESMonitorSetMatlab - Sets the monitor function from MATLAB
3591f9eb7ae2SShri Abhyankar 
3592f9eb7ae2SShri Abhyankar    Level: developer
3593f9eb7ae2SShri Abhyankar 
3594f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function
3595f9eb7ae2SShri Abhyankar 
3596f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
3597f9eb7ae2SShri Abhyankar */
35987087cfbeSBarry Smith PetscErrorCode  SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx)
3599f9eb7ae2SShri Abhyankar {
3600f9eb7ae2SShri Abhyankar   PetscErrorCode    ierr;
3601f9eb7ae2SShri Abhyankar   SNESMatlabContext *sctx;
3602f9eb7ae2SShri Abhyankar 
3603f9eb7ae2SShri Abhyankar   PetscFunctionBegin;
3604f9eb7ae2SShri Abhyankar   /* currently sctx is memory bleed */
3605f9eb7ae2SShri Abhyankar   ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr);
3606f9eb7ae2SShri Abhyankar   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3607f9eb7ae2SShri Abhyankar   /*
3608f9eb7ae2SShri Abhyankar      This should work, but it doesn't
3609f9eb7ae2SShri Abhyankar   sctx->ctx = ctx;
3610f9eb7ae2SShri Abhyankar   mexMakeArrayPersistent(sctx->ctx);
3611f9eb7ae2SShri Abhyankar   */
3612f9eb7ae2SShri Abhyankar   sctx->ctx = mxDuplicateArray(ctx);
3613f9eb7ae2SShri Abhyankar   ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3614f9eb7ae2SShri Abhyankar   PetscFunctionReturn(0);
3615f9eb7ae2SShri Abhyankar }
3616f9eb7ae2SShri Abhyankar 
361769b4f73cSBarry Smith #endif
3618