xref: /petsc/src/snes/interface/snes.c (revision c5eb91543d2ee8daf880d93389b892228ddada03)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
29b94acceSBarry Smith 
37c4f633dSBarry Smith #include "private/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
68ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
78ba1e511SMatthew Knepley 
88ba1e511SMatthew Knepley /* Logging support */
90700a824SBarry Smith PetscClassId PETSCSNES_DLLEXPORT SNES_CLASSID;
10166c7f25SBarry Smith PetscLogEvent  SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval;
11a09944afSBarry Smith 
12a09944afSBarry Smith #undef __FUNCT__
13e113a28aSBarry Smith #define __FUNCT__ "SNESSetErrorIfNotConverged"
14e113a28aSBarry Smith /*@
15e113a28aSBarry Smith    SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged.
16e113a28aSBarry Smith 
173f9fe445SBarry Smith    Logically Collective on SNES
18e113a28aSBarry Smith 
19e113a28aSBarry Smith    Input Parameters:
20e113a28aSBarry Smith +  snes - iterative context obtained from SNESCreate()
21e113a28aSBarry Smith -  flg - PETSC_TRUE indicates you want the error generated
22e113a28aSBarry Smith 
23e113a28aSBarry Smith    Options database keys:
24e113a28aSBarry Smith .  -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false)
25e113a28aSBarry Smith 
26e113a28aSBarry Smith    Level: intermediate
27e113a28aSBarry Smith 
28e113a28aSBarry Smith    Notes:
29e113a28aSBarry Smith     Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve()
30e113a28aSBarry Smith     to determine if it has converged.
31e113a28aSBarry Smith 
32e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
33e113a28aSBarry Smith 
34e113a28aSBarry Smith .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
35e113a28aSBarry Smith @*/
36e113a28aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetErrorIfNotConverged(SNES snes,PetscTruth flg)
37e113a28aSBarry Smith {
38e113a28aSBarry Smith   PetscFunctionBegin;
39e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
40*c5eb9154SBarry Smith   PetscValidLogicalCollectiveTruth(snes,flg,2);
41e113a28aSBarry Smith   snes->errorifnotconverged = flg;
42e113a28aSBarry Smith   PetscFunctionReturn(0);
43e113a28aSBarry Smith }
44e113a28aSBarry Smith 
45e113a28aSBarry Smith #undef __FUNCT__
46e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged"
47e113a28aSBarry Smith /*@
48e113a28aSBarry Smith    SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge?
49e113a28aSBarry Smith 
50e113a28aSBarry Smith    Not Collective
51e113a28aSBarry Smith 
52e113a28aSBarry Smith    Input Parameter:
53e113a28aSBarry Smith .  snes - iterative context obtained from SNESCreate()
54e113a28aSBarry Smith 
55e113a28aSBarry Smith    Output Parameter:
56e113a28aSBarry Smith .  flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE
57e113a28aSBarry Smith 
58e113a28aSBarry Smith    Level: intermediate
59e113a28aSBarry Smith 
60e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
61e113a28aSBarry Smith 
62e113a28aSBarry Smith .seealso:  SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
63e113a28aSBarry Smith @*/
64e113a28aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetErrorIfNotConverged(SNES snes,PetscTruth *flag)
65e113a28aSBarry Smith {
66e113a28aSBarry Smith   PetscFunctionBegin;
67e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
68e113a28aSBarry Smith   PetscValidPointer(flag,2);
69e113a28aSBarry Smith   *flag = snes->errorifnotconverged;
70e113a28aSBarry Smith   PetscFunctionReturn(0);
71e113a28aSBarry Smith }
72e113a28aSBarry Smith 
73e113a28aSBarry Smith #undef __FUNCT__
744936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
75e725d27bSBarry Smith /*@
764936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
774936397dSBarry Smith      in the functions domain. For example, negative pressure.
784936397dSBarry Smith 
793f9fe445SBarry Smith    Logically Collective on SNES
804936397dSBarry Smith 
814936397dSBarry Smith    Input Parameters:
824936397dSBarry Smith .  SNES - the SNES context
834936397dSBarry Smith 
8428529972SSatish Balay    Level: advanced
854936397dSBarry Smith 
864936397dSBarry Smith .keywords: SNES, view
874936397dSBarry Smith 
884936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
894936397dSBarry Smith @*/
904936397dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunctionDomainError(SNES snes)
914936397dSBarry Smith {
924936397dSBarry Smith   PetscFunctionBegin;
930700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
944936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
954936397dSBarry Smith   PetscFunctionReturn(0);
964936397dSBarry Smith }
974936397dSBarry Smith 
984936397dSBarry Smith #undef __FUNCT__
994a2ae208SSatish Balay #define __FUNCT__ "SNESView"
1007e2c5f70SBarry Smith /*@C
1019b94acceSBarry Smith    SNESView - Prints the SNES data structure.
1029b94acceSBarry Smith 
1034c49b128SBarry Smith    Collective on SNES
104fee21e36SBarry Smith 
105c7afd0dbSLois Curfman McInnes    Input Parameters:
106c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
107c7afd0dbSLois Curfman McInnes -  viewer - visualization context
108c7afd0dbSLois Curfman McInnes 
1099b94acceSBarry Smith    Options Database Key:
110c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
1119b94acceSBarry Smith 
1129b94acceSBarry Smith    Notes:
1139b94acceSBarry Smith    The available visualization contexts include
114b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
115b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
116c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
117c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
118c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
1199b94acceSBarry Smith 
1203e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
121b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
1229b94acceSBarry Smith 
12336851e7fSLois Curfman McInnes    Level: beginner
12436851e7fSLois Curfman McInnes 
1259b94acceSBarry Smith .keywords: SNES, view
1269b94acceSBarry Smith 
127b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1289b94acceSBarry Smith @*/
12963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer)
1309b94acceSBarry Smith {
131fa9f3622SBarry Smith   SNESKSPEW           *kctx;
132dfbe8321SBarry Smith   PetscErrorCode      ierr;
13394b7f48cSBarry Smith   KSP                 ksp;
134a313700dSBarry Smith   const SNESType      type;
13532077d6dSBarry Smith   PetscTruth          iascii,isstring;
1369b94acceSBarry Smith 
1373a40ed3dSBarry Smith   PetscFunctionBegin;
1380700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1393050cee2SBarry Smith   if (!viewer) {
1407adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
1413050cee2SBarry Smith   }
1420700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
143c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
14474679c65SBarry Smith 
1452692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1462692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
14732077d6dSBarry Smith   if (iascii) {
1487adad957SLisandro Dalcin     if (((PetscObject)snes)->prefix) {
1497adad957SLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",((PetscObject)snes)->prefix);CHKERRQ(ierr);
1503a7fca6bSBarry Smith     } else {
151b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
1523a7fca6bSBarry Smith     }
153454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
154454a90a3SBarry Smith     if (type) {
155b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
156184914b5SBarry Smith     } else {
157b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
158184914b5SBarry Smith     }
159e7788613SBarry Smith     if (snes->ops->view) {
160b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
161e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
162b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1630ef38995SBarry Smith     }
16477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
165a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
16670441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
16777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
16877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1699b94acceSBarry Smith     if (snes->ksp_ewconv) {
170fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1719b94acceSBarry Smith       if (kctx) {
17277431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
173a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
174a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1759b94acceSBarry Smith       }
1769b94acceSBarry Smith     }
177eb1f6c34SBarry Smith     if (snes->lagpreconditioner == -1) {
178eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is never rebuilt\n");CHKERRQ(ierr);
179eb1f6c34SBarry Smith     } else if (snes->lagpreconditioner > 1) {
180eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr);
181eb1f6c34SBarry Smith     }
182eb1f6c34SBarry Smith     if (snes->lagjacobian == -1) {
183eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is never rebuilt\n");CHKERRQ(ierr);
184eb1f6c34SBarry Smith     } else if (snes->lagjacobian > 1) {
185eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is rebuilt every %D Newton iterations\n",snes->lagjacobian);CHKERRQ(ierr);
186eb1f6c34SBarry Smith     }
1870f5bd95cSBarry Smith   } else if (isstring) {
188454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
189b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
19019bcc07fSBarry Smith   }
19194b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
192b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
19394b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
194b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1953a40ed3dSBarry Smith   PetscFunctionReturn(0);
1969b94acceSBarry Smith }
1979b94acceSBarry Smith 
19876b2cf59SMatthew Knepley /*
19976b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
20076b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
20176b2cf59SMatthew Knepley */
20276b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
203a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
2046849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
20576b2cf59SMatthew Knepley 
206e74ef692SMatthew Knepley #undef __FUNCT__
207e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
208ac226902SBarry Smith /*@C
20976b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
21076b2cf59SMatthew Knepley 
21176b2cf59SMatthew Knepley   Not Collective
21276b2cf59SMatthew Knepley 
21376b2cf59SMatthew Knepley   Input Parameter:
21476b2cf59SMatthew Knepley . snescheck - function that checks for options
21576b2cf59SMatthew Knepley 
21676b2cf59SMatthew Knepley   Level: developer
21776b2cf59SMatthew Knepley 
21876b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
21976b2cf59SMatthew Knepley @*/
22063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
22176b2cf59SMatthew Knepley {
22276b2cf59SMatthew Knepley   PetscFunctionBegin;
22376b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
224e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
22576b2cf59SMatthew Knepley   }
22676b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
22776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
22876b2cf59SMatthew Knepley }
22976b2cf59SMatthew Knepley 
230aa3661deSLisandro Dalcin EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
231aa3661deSLisandro Dalcin 
232aa3661deSLisandro Dalcin #undef __FUNCT__
233aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private"
234d3462f78SMatthew Knepley static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscTruth hasOperator, PetscInt version)
235aa3661deSLisandro Dalcin {
236aa3661deSLisandro Dalcin   Mat            J;
237aa3661deSLisandro Dalcin   KSP            ksp;
238aa3661deSLisandro Dalcin   PC             pc;
239aa3661deSLisandro Dalcin   PetscTruth     match;
240aa3661deSLisandro Dalcin   PetscErrorCode ierr;
241aa3661deSLisandro Dalcin 
242aa3661deSLisandro Dalcin   PetscFunctionBegin;
2430700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
244aa3661deSLisandro Dalcin 
24598613b67SLisandro Dalcin   if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
24698613b67SLisandro Dalcin     Mat A = snes->jacobian, B = snes->jacobian_pre;
24798613b67SLisandro Dalcin     ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr);
24898613b67SLisandro Dalcin   }
24998613b67SLisandro Dalcin 
250aa3661deSLisandro Dalcin   if (version == 1) {
251aa3661deSLisandro Dalcin     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
25298613b67SLisandro Dalcin     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
253aa3661deSLisandro Dalcin     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
254aa3661deSLisandro Dalcin   } else if (version == 2) {
255e32f2f54SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
25665460251SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE) && !defined(PETSC_USE_SCALAR_LONG_DOUBLE) && !defined(PETSC_USE_SCALAR_INT)
257aa3661deSLisandro Dalcin     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr);
258aa3661deSLisandro Dalcin #else
259e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
260aa3661deSLisandro Dalcin #endif
261aa3661deSLisandro Dalcin   } else {
262e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2");
263aa3661deSLisandro Dalcin   }
264aa3661deSLisandro Dalcin 
265aa3661deSLisandro Dalcin   ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr);
266d3462f78SMatthew Knepley   if (hasOperator) {
267aa3661deSLisandro Dalcin     /* This version replaces the user provided Jacobian matrix with a
268aa3661deSLisandro Dalcin        matrix-free version but still employs the user-provided preconditioner matrix. */
269aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
270aa3661deSLisandro Dalcin   } else {
271aa3661deSLisandro Dalcin     /* This version replaces both the user-provided Jacobian and the user-
272aa3661deSLisandro Dalcin        provided preconditioner matrix with the default matrix free version. */
273aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
274aa3661deSLisandro Dalcin     /* Force no preconditioner */
275aa3661deSLisandro Dalcin     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
276aa3661deSLisandro Dalcin     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
277aa3661deSLisandro Dalcin     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr);
278aa3661deSLisandro Dalcin     if (!match) {
279aa3661deSLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
280aa3661deSLisandro Dalcin       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
281aa3661deSLisandro Dalcin     }
282aa3661deSLisandro Dalcin   }
283aa3661deSLisandro Dalcin   ierr = MatDestroy(J);CHKERRQ(ierr);
284aa3661deSLisandro Dalcin 
285aa3661deSLisandro Dalcin   PetscFunctionReturn(0);
286aa3661deSLisandro Dalcin }
287aa3661deSLisandro Dalcin 
2884a2ae208SSatish Balay #undef __FUNCT__
2894a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
2909b94acceSBarry Smith /*@
29194b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
2929b94acceSBarry Smith 
293c7afd0dbSLois Curfman McInnes    Collective on SNES
294c7afd0dbSLois Curfman McInnes 
2959b94acceSBarry Smith    Input Parameter:
2969b94acceSBarry Smith .  snes - the SNES context
2979b94acceSBarry Smith 
29836851e7fSLois Curfman McInnes    Options Database Keys:
2996831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
30082738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
30182738288SBarry Smith                 of the change in the solution between steps
30270441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
303b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
304b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
305b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
30650ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
307ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
308a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
309e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
310b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
3112492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
31282738288SBarry Smith                                solver; hence iterations will continue until max_it
3131fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
31482738288SBarry Smith                                of convergence test
315e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
316e8105e01SRichard Katz                                        filename given prints to stdout
317a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
318a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
319a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
320a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
321e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
3225968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
323fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
32482738288SBarry Smith 
32582738288SBarry Smith     Options Database for Eisenstat-Walker method:
326fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
3274b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
32836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
32936851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
33036851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
33136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
33236851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
33336851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
33482738288SBarry Smith 
33511ca99fdSLois Curfman McInnes    Notes:
33611ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
33711ca99fdSLois Curfman McInnes    the users manual.
33883e2fdc7SBarry Smith 
33936851e7fSLois Curfman McInnes    Level: beginner
34036851e7fSLois Curfman McInnes 
3419b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
3429b94acceSBarry Smith 
34369ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
3449b94acceSBarry Smith @*/
34563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
3469b94acceSBarry Smith {
347aa3661deSLisandro Dalcin   PetscTruth              flg,mf,mf_operator;
348aa3661deSLisandro Dalcin   PetscInt                i,indx,lag,mf_version;
349aa3661deSLisandro Dalcin   MatStructure            matflag;
35085385478SLisandro Dalcin   const char              *deft = SNESLS;
35185385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
35285385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
353e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
35423d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
35585385478SLisandro Dalcin   PetscErrorCode          ierr;
3569b94acceSBarry Smith 
3573a40ed3dSBarry Smith   PetscFunctionBegin;
3580700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
359ca161407SBarry Smith 
360186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
361cce0b1b2SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
3627adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
363b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
364d64ed03dSBarry Smith     if (flg) {
365186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
3667adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
367186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
368d64ed03dSBarry Smith     }
36990d69ab7SBarry Smith     /* not used here, but called so will go into help messaage */
370909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
37193c39befSBarry Smith 
37257034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
37357034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
374186905e3SBarry Smith 
37557034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
376b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
377b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
37850ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
379ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
380e113a28aSBarry Smith     ierr = PetscOptionsTruth("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr);
38185385478SLisandro Dalcin 
382a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
383a8054027SBarry Smith     if (flg) {
384a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
385a8054027SBarry Smith     }
386e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
387e35cf81dSBarry Smith     if (flg) {
388e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
389e35cf81dSBarry Smith     }
390a8054027SBarry Smith 
39185385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
39285385478SLisandro Dalcin     if (flg) {
39385385478SLisandro Dalcin       switch (indx) {
3947f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
3957f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
39685385478SLisandro Dalcin       }
39785385478SLisandro Dalcin     }
39885385478SLisandro Dalcin 
39990d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
400186905e3SBarry Smith 
40185385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
40285385478SLisandro Dalcin 
403fa9f3622SBarry Smith     ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
404186905e3SBarry Smith 
405fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
406fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
407fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
408fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
409fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
410fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
411fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
412186905e3SBarry Smith 
41390d69ab7SBarry Smith     flg  = PETSC_FALSE;
41490d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
415a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
416eabae89aSBarry Smith 
417a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
418e8105e01SRichard Katz     if (flg) {
419050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
42023d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
421e8105e01SRichard Katz     }
422eabae89aSBarry Smith 
423b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
424b271bb04SBarry Smith     if (flg) {
425b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
426b271bb04SBarry Smith     }
427b271bb04SBarry Smith 
428a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
429eabae89aSBarry Smith     if (flg) {
430050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
431f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
432e8105e01SRichard Katz     }
433eabae89aSBarry Smith 
434a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
435eabae89aSBarry Smith     if (flg) {
436050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
43723d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
438eabae89aSBarry Smith     }
439eabae89aSBarry Smith 
44090d69ab7SBarry Smith     flg  = PETSC_FALSE;
44190d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
442a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
44390d69ab7SBarry Smith     flg  = PETSC_FALSE;
44490d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
445a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
44690d69ab7SBarry Smith     flg  = PETSC_FALSE;
44790d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
448a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
44990d69ab7SBarry Smith     flg  = PETSC_FALSE;
45090d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
451a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
45290d69ab7SBarry Smith     flg  = PETSC_FALSE;
45390d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
454b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
455e24b481bSBarry Smith 
45690d69ab7SBarry Smith     flg  = PETSC_FALSE;
45790d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
4584b27c08aSLois Curfman McInnes     if (flg) {
459186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
460ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
4619b94acceSBarry Smith     }
462639f9d9dSBarry Smith 
463aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
464aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
465aa3661deSLisandro Dalcin     ierr = PetscOptionsTruth("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
466aa3661deSLisandro Dalcin     if (flg && mf_operator) mf = PETSC_TRUE;
467aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
468aa3661deSLisandro Dalcin     ierr = PetscOptionsTruth("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
469aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
470aa3661deSLisandro Dalcin     mf_version = 1;
471aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
472aa3661deSLisandro Dalcin 
47376b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
47476b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
47576b2cf59SMatthew Knepley     }
47676b2cf59SMatthew Knepley 
477e7788613SBarry Smith     if (snes->ops->setfromoptions) {
478e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
479639f9d9dSBarry Smith     }
4805d973c19SBarry Smith 
4815d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
4825d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr);
483b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4844bbc92c1SBarry Smith 
485aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
4861cee3971SBarry Smith 
4871cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
488aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
489aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
49085385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
49193993e2dSLois Curfman McInnes 
4923a40ed3dSBarry Smith   PetscFunctionReturn(0);
4939b94acceSBarry Smith }
4949b94acceSBarry Smith 
495a847f771SSatish Balay 
4964a2ae208SSatish Balay #undef __FUNCT__
4974a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
4989b94acceSBarry Smith /*@
4999b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
5009b94acceSBarry Smith    the nonlinear solvers.
5019b94acceSBarry Smith 
5023f9fe445SBarry Smith    Logically Collective on SNES
503fee21e36SBarry Smith 
504c7afd0dbSLois Curfman McInnes    Input Parameters:
505c7afd0dbSLois Curfman McInnes +  snes - the SNES context
506c7afd0dbSLois Curfman McInnes -  usrP - optional user context
507c7afd0dbSLois Curfman McInnes 
50836851e7fSLois Curfman McInnes    Level: intermediate
50936851e7fSLois Curfman McInnes 
5109b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
5119b94acceSBarry Smith 
5129b94acceSBarry Smith .seealso: SNESGetApplicationContext()
5139b94acceSBarry Smith @*/
51463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
5159b94acceSBarry Smith {
5163a40ed3dSBarry Smith   PetscFunctionBegin;
5170700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5189b94acceSBarry Smith   snes->user		= usrP;
5193a40ed3dSBarry Smith   PetscFunctionReturn(0);
5209b94acceSBarry Smith }
52174679c65SBarry Smith 
5224a2ae208SSatish Balay #undef __FUNCT__
5234a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
5249b94acceSBarry Smith /*@C
5259b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
5269b94acceSBarry Smith    nonlinear solvers.
5279b94acceSBarry Smith 
528c7afd0dbSLois Curfman McInnes    Not Collective
529c7afd0dbSLois Curfman McInnes 
5309b94acceSBarry Smith    Input Parameter:
5319b94acceSBarry Smith .  snes - SNES context
5329b94acceSBarry Smith 
5339b94acceSBarry Smith    Output Parameter:
5349b94acceSBarry Smith .  usrP - user context
5359b94acceSBarry Smith 
53636851e7fSLois Curfman McInnes    Level: intermediate
53736851e7fSLois Curfman McInnes 
5389b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
5399b94acceSBarry Smith 
5409b94acceSBarry Smith .seealso: SNESSetApplicationContext()
5419b94acceSBarry Smith @*/
54263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
5439b94acceSBarry Smith {
5443a40ed3dSBarry Smith   PetscFunctionBegin;
5450700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5469b94acceSBarry Smith   *usrP = snes->user;
5473a40ed3dSBarry Smith   PetscFunctionReturn(0);
5489b94acceSBarry Smith }
54974679c65SBarry Smith 
5504a2ae208SSatish Balay #undef __FUNCT__
5514a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
5529b94acceSBarry Smith /*@
553c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
554c8228a4eSBarry Smith    at this time.
5559b94acceSBarry Smith 
556c7afd0dbSLois Curfman McInnes    Not Collective
557c7afd0dbSLois Curfman McInnes 
5589b94acceSBarry Smith    Input Parameter:
5599b94acceSBarry Smith .  snes - SNES context
5609b94acceSBarry Smith 
5619b94acceSBarry Smith    Output Parameter:
5629b94acceSBarry Smith .  iter - iteration number
5639b94acceSBarry Smith 
564c8228a4eSBarry Smith    Notes:
565c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
566c8228a4eSBarry Smith 
567c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
56808405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
56908405cd6SLois Curfman McInnes .vb
57008405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
57108405cd6SLois Curfman McInnes       if (!(it % 2)) {
57208405cd6SLois Curfman McInnes         [compute Jacobian here]
57308405cd6SLois Curfman McInnes       }
57408405cd6SLois Curfman McInnes .ve
575c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
57608405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
577c8228a4eSBarry Smith 
57836851e7fSLois Curfman McInnes    Level: intermediate
57936851e7fSLois Curfman McInnes 
5802b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
5812b668275SBarry Smith 
582b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
5839b94acceSBarry Smith @*/
58463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
5859b94acceSBarry Smith {
5863a40ed3dSBarry Smith   PetscFunctionBegin;
5870700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5884482741eSBarry Smith   PetscValidIntPointer(iter,2);
5899b94acceSBarry Smith   *iter = snes->iter;
5903a40ed3dSBarry Smith   PetscFunctionReturn(0);
5919b94acceSBarry Smith }
59274679c65SBarry Smith 
5934a2ae208SSatish Balay #undef __FUNCT__
5944a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
5959b94acceSBarry Smith /*@
5969b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
5979b94acceSBarry Smith    with SNESSSetFunction().
5989b94acceSBarry Smith 
599c7afd0dbSLois Curfman McInnes    Collective on SNES
600c7afd0dbSLois Curfman McInnes 
6019b94acceSBarry Smith    Input Parameter:
6029b94acceSBarry Smith .  snes - SNES context
6039b94acceSBarry Smith 
6049b94acceSBarry Smith    Output Parameter:
6059b94acceSBarry Smith .  fnorm - 2-norm of function
6069b94acceSBarry Smith 
60736851e7fSLois Curfman McInnes    Level: intermediate
60836851e7fSLois Curfman McInnes 
6099b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
610a86d99e1SLois Curfman McInnes 
611b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
6129b94acceSBarry Smith @*/
61371f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
6149b94acceSBarry Smith {
6153a40ed3dSBarry Smith   PetscFunctionBegin;
6160700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6174482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
6189b94acceSBarry Smith   *fnorm = snes->norm;
6193a40ed3dSBarry Smith   PetscFunctionReturn(0);
6209b94acceSBarry Smith }
62174679c65SBarry Smith 
6224a2ae208SSatish Balay #undef __FUNCT__
623b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
6249b94acceSBarry Smith /*@
625b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
6269b94acceSBarry Smith    attempted by the nonlinear solver.
6279b94acceSBarry Smith 
628c7afd0dbSLois Curfman McInnes    Not Collective
629c7afd0dbSLois Curfman McInnes 
6309b94acceSBarry Smith    Input Parameter:
6319b94acceSBarry Smith .  snes - SNES context
6329b94acceSBarry Smith 
6339b94acceSBarry Smith    Output Parameter:
6349b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
6359b94acceSBarry Smith 
636c96a6f78SLois Curfman McInnes    Notes:
637c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
638c96a6f78SLois Curfman McInnes 
63936851e7fSLois Curfman McInnes    Level: intermediate
64036851e7fSLois Curfman McInnes 
6419b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
64258ebbce7SBarry Smith 
643e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
64458ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
6459b94acceSBarry Smith @*/
646b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
6479b94acceSBarry Smith {
6483a40ed3dSBarry Smith   PetscFunctionBegin;
6490700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6504482741eSBarry Smith   PetscValidIntPointer(nfails,2);
65150ffb88aSMatthew Knepley   *nfails = snes->numFailures;
65250ffb88aSMatthew Knepley   PetscFunctionReturn(0);
65350ffb88aSMatthew Knepley }
65450ffb88aSMatthew Knepley 
65550ffb88aSMatthew Knepley #undef __FUNCT__
656b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
65750ffb88aSMatthew Knepley /*@
658b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
65950ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
66050ffb88aSMatthew Knepley 
66150ffb88aSMatthew Knepley    Not Collective
66250ffb88aSMatthew Knepley 
66350ffb88aSMatthew Knepley    Input Parameters:
66450ffb88aSMatthew Knepley +  snes     - SNES context
66550ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
66650ffb88aSMatthew Knepley 
66750ffb88aSMatthew Knepley    Level: intermediate
66850ffb88aSMatthew Knepley 
66950ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
67058ebbce7SBarry Smith 
671e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
67258ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
67350ffb88aSMatthew Knepley @*/
674b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
67550ffb88aSMatthew Knepley {
67650ffb88aSMatthew Knepley   PetscFunctionBegin;
6770700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
67850ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
67950ffb88aSMatthew Knepley   PetscFunctionReturn(0);
68050ffb88aSMatthew Knepley }
68150ffb88aSMatthew Knepley 
68250ffb88aSMatthew Knepley #undef __FUNCT__
683b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
68450ffb88aSMatthew Knepley /*@
685b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
68650ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
68750ffb88aSMatthew Knepley 
68850ffb88aSMatthew Knepley    Not Collective
68950ffb88aSMatthew Knepley 
69050ffb88aSMatthew Knepley    Input Parameter:
69150ffb88aSMatthew Knepley .  snes     - SNES context
69250ffb88aSMatthew Knepley 
69350ffb88aSMatthew Knepley    Output Parameter:
69450ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
69550ffb88aSMatthew Knepley 
69650ffb88aSMatthew Knepley    Level: intermediate
69750ffb88aSMatthew Knepley 
69850ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
69958ebbce7SBarry Smith 
700e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
70158ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
70258ebbce7SBarry Smith 
70350ffb88aSMatthew Knepley @*/
704b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
70550ffb88aSMatthew Knepley {
70650ffb88aSMatthew Knepley   PetscFunctionBegin;
7070700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7084482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
70950ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
7103a40ed3dSBarry Smith   PetscFunctionReturn(0);
7119b94acceSBarry Smith }
712a847f771SSatish Balay 
7134a2ae208SSatish Balay #undef __FUNCT__
7142541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
7152541af92SBarry Smith /*@
7162541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
7172541af92SBarry Smith      done by SNES.
7182541af92SBarry Smith 
7192541af92SBarry Smith    Not Collective
7202541af92SBarry Smith 
7212541af92SBarry Smith    Input Parameter:
7222541af92SBarry Smith .  snes     - SNES context
7232541af92SBarry Smith 
7242541af92SBarry Smith    Output Parameter:
7252541af92SBarry Smith .  nfuncs - number of evaluations
7262541af92SBarry Smith 
7272541af92SBarry Smith    Level: intermediate
7282541af92SBarry Smith 
7292541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
73058ebbce7SBarry Smith 
731e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
7322541af92SBarry Smith @*/
7332541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
7342541af92SBarry Smith {
7352541af92SBarry Smith   PetscFunctionBegin;
7360700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7372541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
7382541af92SBarry Smith   *nfuncs = snes->nfuncs;
7392541af92SBarry Smith   PetscFunctionReturn(0);
7402541af92SBarry Smith }
7412541af92SBarry Smith 
7422541af92SBarry Smith #undef __FUNCT__
7433d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
7443d4c4710SBarry Smith /*@
7453d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
7463d4c4710SBarry Smith    linear solvers.
7473d4c4710SBarry Smith 
7483d4c4710SBarry Smith    Not Collective
7493d4c4710SBarry Smith 
7503d4c4710SBarry Smith    Input Parameter:
7513d4c4710SBarry Smith .  snes - SNES context
7523d4c4710SBarry Smith 
7533d4c4710SBarry Smith    Output Parameter:
7543d4c4710SBarry Smith .  nfails - number of failed solves
7553d4c4710SBarry Smith 
7563d4c4710SBarry Smith    Notes:
7573d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
7583d4c4710SBarry Smith 
7593d4c4710SBarry Smith    Level: intermediate
7603d4c4710SBarry Smith 
7613d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
76258ebbce7SBarry Smith 
763e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
7643d4c4710SBarry Smith @*/
7653d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
7663d4c4710SBarry Smith {
7673d4c4710SBarry Smith   PetscFunctionBegin;
7680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7693d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
7703d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
7713d4c4710SBarry Smith   PetscFunctionReturn(0);
7723d4c4710SBarry Smith }
7733d4c4710SBarry Smith 
7743d4c4710SBarry Smith #undef __FUNCT__
7753d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
7763d4c4710SBarry Smith /*@
7773d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
7783d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
7793d4c4710SBarry Smith 
7803f9fe445SBarry Smith    Logically Collective on SNES
7813d4c4710SBarry Smith 
7823d4c4710SBarry Smith    Input Parameters:
7833d4c4710SBarry Smith +  snes     - SNES context
7843d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
7853d4c4710SBarry Smith 
7863d4c4710SBarry Smith    Level: intermediate
7873d4c4710SBarry Smith 
788a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
7893d4c4710SBarry Smith 
7903d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
7913d4c4710SBarry Smith 
79258ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
7933d4c4710SBarry Smith @*/
7943d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
7953d4c4710SBarry Smith {
7963d4c4710SBarry Smith   PetscFunctionBegin;
7970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
798*c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxFails,2);
7993d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
8003d4c4710SBarry Smith   PetscFunctionReturn(0);
8013d4c4710SBarry Smith }
8023d4c4710SBarry Smith 
8033d4c4710SBarry Smith #undef __FUNCT__
8043d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
8053d4c4710SBarry Smith /*@
8063d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
8073d4c4710SBarry Smith      are allowed before SNES terminates
8083d4c4710SBarry Smith 
8093d4c4710SBarry Smith    Not Collective
8103d4c4710SBarry Smith 
8113d4c4710SBarry Smith    Input Parameter:
8123d4c4710SBarry Smith .  snes     - SNES context
8133d4c4710SBarry Smith 
8143d4c4710SBarry Smith    Output Parameter:
8153d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
8163d4c4710SBarry Smith 
8173d4c4710SBarry Smith    Level: intermediate
8183d4c4710SBarry Smith 
8193d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
8203d4c4710SBarry Smith 
8213d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
8223d4c4710SBarry Smith 
823e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
8243d4c4710SBarry Smith @*/
8253d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
8263d4c4710SBarry Smith {
8273d4c4710SBarry Smith   PetscFunctionBegin;
8280700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8293d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
8303d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
8313d4c4710SBarry Smith   PetscFunctionReturn(0);
8323d4c4710SBarry Smith }
8333d4c4710SBarry Smith 
8343d4c4710SBarry Smith #undef __FUNCT__
835b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
836c96a6f78SLois Curfman McInnes /*@
837b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
838c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
839c96a6f78SLois Curfman McInnes 
840c7afd0dbSLois Curfman McInnes    Not Collective
841c7afd0dbSLois Curfman McInnes 
842c96a6f78SLois Curfman McInnes    Input Parameter:
843c96a6f78SLois Curfman McInnes .  snes - SNES context
844c96a6f78SLois Curfman McInnes 
845c96a6f78SLois Curfman McInnes    Output Parameter:
846c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
847c96a6f78SLois Curfman McInnes 
848c96a6f78SLois Curfman McInnes    Notes:
849c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
850c96a6f78SLois Curfman McInnes 
85136851e7fSLois Curfman McInnes    Level: intermediate
85236851e7fSLois Curfman McInnes 
853c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
8542b668275SBarry Smith 
8558c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
856c96a6f78SLois Curfman McInnes @*/
857b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
858c96a6f78SLois Curfman McInnes {
8593a40ed3dSBarry Smith   PetscFunctionBegin;
8600700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8614482741eSBarry Smith   PetscValidIntPointer(lits,2);
862c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
8633a40ed3dSBarry Smith   PetscFunctionReturn(0);
864c96a6f78SLois Curfman McInnes }
865c96a6f78SLois Curfman McInnes 
8664a2ae208SSatish Balay #undef __FUNCT__
86794b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
86852baeb72SSatish Balay /*@
86994b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
8709b94acceSBarry Smith 
87194b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
872c7afd0dbSLois Curfman McInnes 
8739b94acceSBarry Smith    Input Parameter:
8749b94acceSBarry Smith .  snes - the SNES context
8759b94acceSBarry Smith 
8769b94acceSBarry Smith    Output Parameter:
87794b7f48cSBarry Smith .  ksp - the KSP context
8789b94acceSBarry Smith 
8799b94acceSBarry Smith    Notes:
88094b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
8819b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
8822999313aSBarry Smith    PC contexts as well.
8839b94acceSBarry Smith 
88436851e7fSLois Curfman McInnes    Level: beginner
88536851e7fSLois Curfman McInnes 
88694b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
8879b94acceSBarry Smith 
8882999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
8899b94acceSBarry Smith @*/
89063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
8919b94acceSBarry Smith {
8921cee3971SBarry Smith   PetscErrorCode ierr;
8931cee3971SBarry Smith 
8943a40ed3dSBarry Smith   PetscFunctionBegin;
8950700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8964482741eSBarry Smith   PetscValidPointer(ksp,2);
8971cee3971SBarry Smith 
8981cee3971SBarry Smith   if (!snes->ksp) {
8991cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
9001cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
9011cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
9021cee3971SBarry Smith   }
90394b7f48cSBarry Smith   *ksp = snes->ksp;
9043a40ed3dSBarry Smith   PetscFunctionReturn(0);
9059b94acceSBarry Smith }
90682bf6240SBarry Smith 
9074a2ae208SSatish Balay #undef __FUNCT__
9082999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
9092999313aSBarry Smith /*@
9102999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
9112999313aSBarry Smith 
9122999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
9132999313aSBarry Smith 
9142999313aSBarry Smith    Input Parameters:
9152999313aSBarry Smith +  snes - the SNES context
9162999313aSBarry Smith -  ksp - the KSP context
9172999313aSBarry Smith 
9182999313aSBarry Smith    Notes:
9192999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
9202999313aSBarry Smith    so this routine is rarely needed.
9212999313aSBarry Smith 
9222999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
9232999313aSBarry Smith    decreased by one.
9242999313aSBarry Smith 
9252999313aSBarry Smith    Level: developer
9262999313aSBarry Smith 
9272999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9282999313aSBarry Smith 
9292999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9302999313aSBarry Smith @*/
9312999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
9322999313aSBarry Smith {
9332999313aSBarry Smith   PetscErrorCode ierr;
9342999313aSBarry Smith 
9352999313aSBarry Smith   PetscFunctionBegin;
9360700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9370700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
9382999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
9397dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
940906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
9412999313aSBarry Smith   snes->ksp = ksp;
9422999313aSBarry Smith   PetscFunctionReturn(0);
9432999313aSBarry Smith }
9442999313aSBarry Smith 
9457adad957SLisandro Dalcin #if 0
9462999313aSBarry Smith #undef __FUNCT__
9474a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
9486849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
949e24b481bSBarry Smith {
950e24b481bSBarry Smith   PetscFunctionBegin;
951e24b481bSBarry Smith   PetscFunctionReturn(0);
952e24b481bSBarry Smith }
9537adad957SLisandro Dalcin #endif
954e24b481bSBarry Smith 
9559b94acceSBarry Smith /* -----------------------------------------------------------*/
9564a2ae208SSatish Balay #undef __FUNCT__
9574a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
95852baeb72SSatish Balay /*@
9599b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
9609b94acceSBarry Smith 
961c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
962c7afd0dbSLois Curfman McInnes 
963c7afd0dbSLois Curfman McInnes    Input Parameters:
964906ed7ccSBarry Smith .  comm - MPI communicator
9659b94acceSBarry Smith 
9669b94acceSBarry Smith    Output Parameter:
9679b94acceSBarry Smith .  outsnes - the new SNES context
9689b94acceSBarry Smith 
969c7afd0dbSLois Curfman McInnes    Options Database Keys:
970c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
971c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
972c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
973c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
974c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
975c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
976c1f60f51SBarry Smith 
97736851e7fSLois Curfman McInnes    Level: beginner
97836851e7fSLois Curfman McInnes 
9799b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
9809b94acceSBarry Smith 
981a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
982a8054027SBarry Smith 
9839b94acceSBarry Smith @*/
98463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
9859b94acceSBarry Smith {
986dfbe8321SBarry Smith   PetscErrorCode      ierr;
9879b94acceSBarry Smith   SNES                snes;
988fa9f3622SBarry Smith   SNESKSPEW           *kctx;
98937fcc0dbSBarry Smith 
9903a40ed3dSBarry Smith   PetscFunctionBegin;
991ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
9928ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
9938ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
9948ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
9958ba1e511SMatthew Knepley #endif
9968ba1e511SMatthew Knepley 
9970700a824SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
9987adad957SLisandro Dalcin 
99985385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
10009b94acceSBarry Smith   snes->max_its           = 50;
10019750a799SBarry Smith   snes->max_funcs	  = 10000;
10029b94acceSBarry Smith   snes->norm		  = 0.0;
1003b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
1004b4874afaSBarry Smith   snes->ttol              = 0.0;
100570441072SBarry Smith   snes->abstol		  = 1.e-50;
10069b94acceSBarry Smith   snes->xtol		  = 1.e-8;
10074b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
10089b94acceSBarry Smith   snes->nfuncs            = 0;
100950ffb88aSMatthew Knepley   snes->numFailures       = 0;
101050ffb88aSMatthew Knepley   snes->maxFailures       = 1;
10117a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1012e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1013a8054027SBarry Smith   snes->lagpreconditioner = 1;
1014639f9d9dSBarry Smith   snes->numbermonitors    = 0;
10159b94acceSBarry Smith   snes->data              = 0;
10164dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1017186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
10186f24a144SLois Curfman McInnes   snes->vwork             = 0;
10196f24a144SLois Curfman McInnes   snes->nwork             = 0;
1020758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1021758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1022758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1023758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1024758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1025184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
10269b94acceSBarry Smith 
10273d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
10283d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
10293d4c4710SBarry Smith 
10309b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
103138f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
10329b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
10339b94acceSBarry Smith   kctx->version     = 2;
10349b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
10359b94acceSBarry Smith                              this was too large for some test cases */
103675567043SBarry Smith   kctx->rtol_last   = 0.0;
10379b94acceSBarry Smith   kctx->rtol_max    = .9;
10389b94acceSBarry Smith   kctx->gamma       = 1.0;
103971f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
104071f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
10419b94acceSBarry Smith   kctx->threshold   = .1;
104275567043SBarry Smith   kctx->lresid_last = 0.0;
104375567043SBarry Smith   kctx->norm_last   = 0.0;
10449b94acceSBarry Smith 
10459b94acceSBarry Smith   *outsnes = snes;
104600036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
10473a40ed3dSBarry Smith   PetscFunctionReturn(0);
10489b94acceSBarry Smith }
10499b94acceSBarry Smith 
10504a2ae208SSatish Balay #undef __FUNCT__
10514a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
10529b94acceSBarry Smith /*@C
10539b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
10549b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
10559b94acceSBarry Smith    equations.
10569b94acceSBarry Smith 
10573f9fe445SBarry Smith    Logically Collective on SNES
1058fee21e36SBarry Smith 
1059c7afd0dbSLois Curfman McInnes    Input Parameters:
1060c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1061c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1062de044059SHong Zhang .  func - function evaluation routine
1063c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1064c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
10659b94acceSBarry Smith 
1066c7afd0dbSLois Curfman McInnes    Calling sequence of func:
10678d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1068c7afd0dbSLois Curfman McInnes 
1069313e4042SLois Curfman McInnes .  f - function vector
1070c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
10719b94acceSBarry Smith 
10729b94acceSBarry Smith    Notes:
10739b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
10749b94acceSBarry Smith $      f'(x) x = -f(x),
1075c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
10769b94acceSBarry Smith 
107736851e7fSLois Curfman McInnes    Level: beginner
107836851e7fSLois Curfman McInnes 
10799b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
10809b94acceSBarry Smith 
1081a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
10829b94acceSBarry Smith @*/
108363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
10849b94acceSBarry Smith {
108585385478SLisandro Dalcin   PetscErrorCode ierr;
10863a40ed3dSBarry Smith   PetscFunctionBegin;
10870700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1088ef8dffc7SBarry Smith   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1089ef8dffc7SBarry Smith   if (r) PetscCheckSameComm(snes,1,r,2);
1090ef8dffc7SBarry Smith   if (!r && snes->dm) {
1091ef8dffc7SBarry Smith     ierr = DMCreateGlobalVector(snes->dm,&r);CHKERRQ(ierr);
1092ef8dffc7SBarry Smith   } else {
109385385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
1094ef8dffc7SBarry Smith   }
109585385478SLisandro Dalcin   if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); }
1096e7788613SBarry Smith   snes->ops->computefunction = func;
109785385478SLisandro Dalcin   snes->vec_func             = r;
10989b94acceSBarry Smith   snes->funP                 = ctx;
10993a40ed3dSBarry Smith   PetscFunctionReturn(0);
11009b94acceSBarry Smith }
11019b94acceSBarry Smith 
11023ab0aad5SBarry Smith /* --------------------------------------------------------------- */
11033ab0aad5SBarry Smith #undef __FUNCT__
11041096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
11051096aae1SMatthew Knepley /*@C
11061096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
11071096aae1SMatthew Knepley    it assumes a zero right hand side.
11081096aae1SMatthew Knepley 
11093f9fe445SBarry Smith    Logically Collective on SNES
11101096aae1SMatthew Knepley 
11111096aae1SMatthew Knepley    Input Parameter:
11121096aae1SMatthew Knepley .  snes - the SNES context
11131096aae1SMatthew Knepley 
11141096aae1SMatthew Knepley    Output Parameter:
1115bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
11161096aae1SMatthew Knepley 
11171096aae1SMatthew Knepley    Level: intermediate
11181096aae1SMatthew Knepley 
11191096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
11201096aae1SMatthew Knepley 
112185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
11221096aae1SMatthew Knepley @*/
11231096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
11241096aae1SMatthew Knepley {
11251096aae1SMatthew Knepley   PetscFunctionBegin;
11260700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11271096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
112885385478SLisandro Dalcin   *rhs = snes->vec_rhs;
11291096aae1SMatthew Knepley   PetscFunctionReturn(0);
11301096aae1SMatthew Knepley }
11311096aae1SMatthew Knepley 
11321096aae1SMatthew Knepley #undef __FUNCT__
11334a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
11349b94acceSBarry Smith /*@
113536851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
11369b94acceSBarry Smith                          SNESSetFunction().
11379b94acceSBarry Smith 
1138c7afd0dbSLois Curfman McInnes    Collective on SNES
1139c7afd0dbSLois Curfman McInnes 
11409b94acceSBarry Smith    Input Parameters:
1141c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1142c7afd0dbSLois Curfman McInnes -  x - input vector
11439b94acceSBarry Smith 
11449b94acceSBarry Smith    Output Parameter:
11453638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
11469b94acceSBarry Smith 
11471bffabb2SLois Curfman McInnes    Notes:
114836851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
114936851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
115036851e7fSLois Curfman McInnes    themselves.
115136851e7fSLois Curfman McInnes 
115236851e7fSLois Curfman McInnes    Level: developer
115336851e7fSLois Curfman McInnes 
11549b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
11559b94acceSBarry Smith 
1156a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
11579b94acceSBarry Smith @*/
115863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
11599b94acceSBarry Smith {
1160dfbe8321SBarry Smith   PetscErrorCode ierr;
11619b94acceSBarry Smith 
11623a40ed3dSBarry Smith   PetscFunctionBegin;
11630700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11640700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
11650700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1166c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1167c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1168184914b5SBarry Smith 
1169d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1170e7788613SBarry Smith   if (snes->ops->computefunction) {
1171d64ed03dSBarry Smith     PetscStackPush("SNES user function");
1172e9a2bbcdSBarry Smith     CHKMEMQ;
1173e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
1174e9a2bbcdSBarry Smith     CHKMEMQ;
1175d64ed03dSBarry Smith     PetscStackPop;
1176d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
117719717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
117819717074SBarry Smith     }
1179d5e45103SBarry Smith     CHKERRQ(ierr);
118085385478SLisandro Dalcin   } else if (snes->vec_rhs) {
11811096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
118217186662SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
118385385478SLisandro Dalcin   if (snes->vec_rhs) {
118485385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
11853ab0aad5SBarry Smith   }
1186ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1187d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
11883a40ed3dSBarry Smith   PetscFunctionReturn(0);
11899b94acceSBarry Smith }
11909b94acceSBarry Smith 
11914a2ae208SSatish Balay #undef __FUNCT__
11924a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
119362fef451SLois Curfman McInnes /*@
119462fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
119562fef451SLois Curfman McInnes    set with SNESSetJacobian().
119662fef451SLois Curfman McInnes 
1197c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1198c7afd0dbSLois Curfman McInnes 
119962fef451SLois Curfman McInnes    Input Parameters:
1200c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1201c7afd0dbSLois Curfman McInnes -  x - input vector
120262fef451SLois Curfman McInnes 
120362fef451SLois Curfman McInnes    Output Parameters:
1204c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
120562fef451SLois Curfman McInnes .  B - optional preconditioning matrix
12062b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1207fee21e36SBarry Smith 
1208e35cf81dSBarry Smith   Options Database Keys:
1209e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1210e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1211e35cf81dSBarry Smith 
121262fef451SLois Curfman McInnes    Notes:
121362fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
121462fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
121562fef451SLois Curfman McInnes 
121694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1217dc5a77f8SLois Curfman McInnes    flag parameter.
121862fef451SLois Curfman McInnes 
121936851e7fSLois Curfman McInnes    Level: developer
122036851e7fSLois Curfman McInnes 
122162fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
122262fef451SLois Curfman McInnes 
1223e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
122462fef451SLois Curfman McInnes @*/
122563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
12269b94acceSBarry Smith {
1227dfbe8321SBarry Smith   PetscErrorCode ierr;
1228ebd3b9afSBarry Smith   PetscTruth     flag;
12293a40ed3dSBarry Smith 
12303a40ed3dSBarry Smith   PetscFunctionBegin;
12310700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12320700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
12334482741eSBarry Smith   PetscValidPointer(flg,5);
1234c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1235e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1236ebd3b9afSBarry Smith 
1237ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1238ebd3b9afSBarry Smith 
1239fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1240fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1241fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1242fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1243e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1244e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1245ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1246ebd3b9afSBarry Smith     if (flag) {
1247ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1248ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1249ebd3b9afSBarry Smith     }
1250e35cf81dSBarry Smith     PetscFunctionReturn(0);
1251e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1252e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1253e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1254ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1255ebd3b9afSBarry Smith     if (flag) {
1256ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1257ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1258ebd3b9afSBarry Smith     }
1259e35cf81dSBarry Smith     PetscFunctionReturn(0);
1260e35cf81dSBarry Smith   }
1261e35cf81dSBarry Smith 
1262c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1263e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1264d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1265dc67937bSBarry Smith   CHKMEMQ;
1266e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1267dc67937bSBarry Smith   CHKMEMQ;
1268d64ed03dSBarry Smith   PetscStackPop;
1269d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1270a8054027SBarry Smith 
12713b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
12723b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
12733b4f5425SBarry Smith     snes->lagpreconditioner = -1;
12743b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1275a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1276a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1277a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1278a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1279a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1280a8054027SBarry Smith   }
1281a8054027SBarry Smith 
12826d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
12830700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
12840700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
12853a40ed3dSBarry Smith   PetscFunctionReturn(0);
12869b94acceSBarry Smith }
12879b94acceSBarry Smith 
12884a2ae208SSatish Balay #undef __FUNCT__
12894a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
12909b94acceSBarry Smith /*@C
12919b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1292044dda88SLois Curfman McInnes    location to store the matrix.
12939b94acceSBarry Smith 
12943f9fe445SBarry Smith    Logically Collective on SNES and Mat
1295c7afd0dbSLois Curfman McInnes 
12969b94acceSBarry Smith    Input Parameters:
1297c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12989b94acceSBarry Smith .  A - Jacobian matrix
12999b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
13009b94acceSBarry Smith .  func - Jacobian evaluation routine
1301c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
13022cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
13039b94acceSBarry Smith 
13049b94acceSBarry Smith    Calling sequence of func:
13058d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
13069b94acceSBarry Smith 
1307c7afd0dbSLois Curfman McInnes +  x - input vector
13089b94acceSBarry Smith .  A - Jacobian matrix
13099b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1310ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
13112b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1312c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
13139b94acceSBarry Smith 
13149b94acceSBarry Smith    Notes:
131594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
13162cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1317ac21db08SLois Curfman McInnes 
1318ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
13199b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
13209b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
13219b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
13229b94acceSBarry Smith    throughout the global iterations.
13239b94acceSBarry Smith 
132416913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
132516913363SBarry Smith    each matrix.
132616913363SBarry Smith 
132736851e7fSLois Curfman McInnes    Level: beginner
132836851e7fSLois Curfman McInnes 
13299b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
13309b94acceSBarry Smith 
13313ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
13329b94acceSBarry Smith @*/
133363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
13349b94acceSBarry Smith {
1335dfbe8321SBarry Smith   PetscErrorCode ierr;
13363a7fca6bSBarry Smith 
13373a40ed3dSBarry Smith   PetscFunctionBegin;
13380700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13390700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
13400700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1341c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
134206975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1343e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
13443a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
13453a7fca6bSBarry Smith   if (A) {
13467dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
13473a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13489b94acceSBarry Smith     snes->jacobian = A;
13493a7fca6bSBarry Smith   }
13503a7fca6bSBarry Smith   if (B) {
13517dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
13523a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13539b94acceSBarry Smith     snes->jacobian_pre = B;
13543a7fca6bSBarry Smith   }
13553a40ed3dSBarry Smith   PetscFunctionReturn(0);
13569b94acceSBarry Smith }
135762fef451SLois Curfman McInnes 
13584a2ae208SSatish Balay #undef __FUNCT__
13594a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1360c2aafc4cSSatish Balay /*@C
1361b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1362b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1363b4fd4287SBarry Smith 
1364c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1365c7afd0dbSLois Curfman McInnes 
1366b4fd4287SBarry Smith    Input Parameter:
1367b4fd4287SBarry Smith .  snes - the nonlinear solver context
1368b4fd4287SBarry Smith 
1369b4fd4287SBarry Smith    Output Parameters:
1370c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1371b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
137270e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
137370e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1374fee21e36SBarry Smith 
137536851e7fSLois Curfman McInnes    Level: advanced
137636851e7fSLois Curfman McInnes 
1377b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1378b4fd4287SBarry Smith @*/
137970e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1380b4fd4287SBarry Smith {
13813a40ed3dSBarry Smith   PetscFunctionBegin;
13820700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1383b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1384b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1385e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
138670e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
13873a40ed3dSBarry Smith   PetscFunctionReturn(0);
1388b4fd4287SBarry Smith }
1389b4fd4287SBarry Smith 
13909b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13919b94acceSBarry Smith 
13924a2ae208SSatish Balay #undef __FUNCT__
13934a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
13949b94acceSBarry Smith /*@
13959b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1396272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13979b94acceSBarry Smith 
1398fee21e36SBarry Smith    Collective on SNES
1399fee21e36SBarry Smith 
1400c7afd0dbSLois Curfman McInnes    Input Parameters:
140170e92668SMatthew Knepley .  snes - the SNES context
1402c7afd0dbSLois Curfman McInnes 
1403272ac6f2SLois Curfman McInnes    Notes:
1404272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1405272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1406272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1407272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1408272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1409272ac6f2SLois Curfman McInnes 
141036851e7fSLois Curfman McInnes    Level: advanced
141136851e7fSLois Curfman McInnes 
14129b94acceSBarry Smith .keywords: SNES, nonlinear, setup
14139b94acceSBarry Smith 
14149b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
14159b94acceSBarry Smith @*/
141670e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
14179b94acceSBarry Smith {
1418dfbe8321SBarry Smith   PetscErrorCode ierr;
14193a40ed3dSBarry Smith 
14203a40ed3dSBarry Smith   PetscFunctionBegin;
14210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14224dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
14239b94acceSBarry Smith 
14247adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
142585385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
142685385478SLisandro Dalcin   }
142785385478SLisandro Dalcin 
142817186662SBarry Smith   if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
142917186662SBarry Smith   if (!snes->ops->computefunction && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
143017186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1431ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1432ef8dffc7SBarry Smith     Mat           J;
1433ef8dffc7SBarry Smith     ISColoring    coloring;
1434ef8dffc7SBarry Smith     MatFDColoring fd;
1435a703fe33SLois Curfman McInnes 
1436ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1437ef8dffc7SBarry Smith     ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
1438ef8dffc7SBarry Smith     ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr);
1439ef8dffc7SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr);
1440ef8dffc7SBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr);
1441ef8dffc7SBarry Smith     ierr = ISColoringDestroy(coloring);CHKERRQ(ierr);
1442ef8dffc7SBarry Smith   }
1443b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1444b710008aSBarry Smith 
1445410397dcSLisandro Dalcin   if (snes->ops->setup) {
1446410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1447410397dcSLisandro Dalcin   }
14487aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
14493a40ed3dSBarry Smith   PetscFunctionReturn(0);
14509b94acceSBarry Smith }
14519b94acceSBarry Smith 
14524a2ae208SSatish Balay #undef __FUNCT__
14534a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
145452baeb72SSatish Balay /*@
14559b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
14569b94acceSBarry Smith    with SNESCreate().
14579b94acceSBarry Smith 
1458c7afd0dbSLois Curfman McInnes    Collective on SNES
1459c7afd0dbSLois Curfman McInnes 
14609b94acceSBarry Smith    Input Parameter:
14619b94acceSBarry Smith .  snes - the SNES context
14629b94acceSBarry Smith 
146336851e7fSLois Curfman McInnes    Level: beginner
146436851e7fSLois Curfman McInnes 
14659b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14669b94acceSBarry Smith 
146763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14689b94acceSBarry Smith @*/
146963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
14709b94acceSBarry Smith {
14716849ba73SBarry Smith   PetscErrorCode ierr;
14723a40ed3dSBarry Smith 
14733a40ed3dSBarry Smith   PetscFunctionBegin;
14740700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14757adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1476d4bb536fSBarry Smith 
1477be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
14780f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
14796c699258SBarry Smith 
14806c699258SBarry Smith   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
1481e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
148285385478SLisandro Dalcin 
148385385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
148485385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
148585385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
14863a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
14873a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
14881cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
148985385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1490522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1491a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
14927f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1493a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
14943a40ed3dSBarry Smith   PetscFunctionReturn(0);
14959b94acceSBarry Smith }
14969b94acceSBarry Smith 
14979b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14989b94acceSBarry Smith 
14994a2ae208SSatish Balay #undef __FUNCT__
1500a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1501a8054027SBarry Smith /*@
1502a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1503a8054027SBarry Smith 
15043f9fe445SBarry Smith    Logically Collective on SNES
1505a8054027SBarry Smith 
1506a8054027SBarry Smith    Input Parameters:
1507a8054027SBarry Smith +  snes - the SNES context
1508a8054027SBarry 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
15093b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1510a8054027SBarry Smith 
1511a8054027SBarry Smith    Options Database Keys:
1512a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1513a8054027SBarry Smith 
1514a8054027SBarry Smith    Notes:
1515a8054027SBarry Smith    The default is 1
1516a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1517a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1518a8054027SBarry Smith 
1519a8054027SBarry Smith    Level: intermediate
1520a8054027SBarry Smith 
1521a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1522a8054027SBarry Smith 
1523e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1524a8054027SBarry Smith 
1525a8054027SBarry Smith @*/
1526a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1527a8054027SBarry Smith {
1528a8054027SBarry Smith   PetscFunctionBegin;
15290700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1530e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1531e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1532*c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1533a8054027SBarry Smith   snes->lagpreconditioner = lag;
1534a8054027SBarry Smith   PetscFunctionReturn(0);
1535a8054027SBarry Smith }
1536a8054027SBarry Smith 
1537a8054027SBarry Smith #undef __FUNCT__
1538a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1539a8054027SBarry Smith /*@
1540a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1541a8054027SBarry Smith 
15423f9fe445SBarry Smith    Not Collective
1543a8054027SBarry Smith 
1544a8054027SBarry Smith    Input Parameter:
1545a8054027SBarry Smith .  snes - the SNES context
1546a8054027SBarry Smith 
1547a8054027SBarry Smith    Output Parameter:
1548a8054027SBarry 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
15493b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1550a8054027SBarry Smith 
1551a8054027SBarry Smith    Options Database Keys:
1552a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1553a8054027SBarry Smith 
1554a8054027SBarry Smith    Notes:
1555a8054027SBarry Smith    The default is 1
1556a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1557a8054027SBarry Smith 
1558a8054027SBarry Smith    Level: intermediate
1559a8054027SBarry Smith 
1560a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1561a8054027SBarry Smith 
1562a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1563a8054027SBarry Smith 
1564a8054027SBarry Smith @*/
1565a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1566a8054027SBarry Smith {
1567a8054027SBarry Smith   PetscFunctionBegin;
15680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1569a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1570a8054027SBarry Smith   PetscFunctionReturn(0);
1571a8054027SBarry Smith }
1572a8054027SBarry Smith 
1573a8054027SBarry Smith #undef __FUNCT__
1574e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1575e35cf81dSBarry Smith /*@
1576e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1577e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1578e35cf81dSBarry Smith 
15793f9fe445SBarry Smith    Logically Collective on SNES
1580e35cf81dSBarry Smith 
1581e35cf81dSBarry Smith    Input Parameters:
1582e35cf81dSBarry Smith +  snes - the SNES context
1583e35cf81dSBarry 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
1584fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1585e35cf81dSBarry Smith 
1586e35cf81dSBarry Smith    Options Database Keys:
1587e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1588e35cf81dSBarry Smith 
1589e35cf81dSBarry Smith    Notes:
1590e35cf81dSBarry Smith    The default is 1
1591e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1592fe3ffe1eSBarry 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
1593fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1594e35cf81dSBarry Smith 
1595e35cf81dSBarry Smith    Level: intermediate
1596e35cf81dSBarry Smith 
1597e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1598e35cf81dSBarry Smith 
1599e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1600e35cf81dSBarry Smith 
1601e35cf81dSBarry Smith @*/
1602e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1603e35cf81dSBarry Smith {
1604e35cf81dSBarry Smith   PetscFunctionBegin;
16050700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1606e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1607e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1608*c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,lag,2);
1609e35cf81dSBarry Smith   snes->lagjacobian = lag;
1610e35cf81dSBarry Smith   PetscFunctionReturn(0);
1611e35cf81dSBarry Smith }
1612e35cf81dSBarry Smith 
1613e35cf81dSBarry Smith #undef __FUNCT__
1614e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1615e35cf81dSBarry Smith /*@
1616e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1617e35cf81dSBarry Smith 
16183f9fe445SBarry Smith    Not Collective
1619e35cf81dSBarry Smith 
1620e35cf81dSBarry Smith    Input Parameter:
1621e35cf81dSBarry Smith .  snes - the SNES context
1622e35cf81dSBarry Smith 
1623e35cf81dSBarry Smith    Output Parameter:
1624e35cf81dSBarry Smith .   lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
1625e35cf81dSBarry Smith          the Jacobian is built etc.
1626e35cf81dSBarry Smith 
1627e35cf81dSBarry Smith    Options Database Keys:
1628e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1629e35cf81dSBarry Smith 
1630e35cf81dSBarry Smith    Notes:
1631e35cf81dSBarry Smith    The default is 1
1632e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1633e35cf81dSBarry Smith 
1634e35cf81dSBarry Smith    Level: intermediate
1635e35cf81dSBarry Smith 
1636e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1637e35cf81dSBarry Smith 
1638e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1639e35cf81dSBarry Smith 
1640e35cf81dSBarry Smith @*/
1641e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1642e35cf81dSBarry Smith {
1643e35cf81dSBarry Smith   PetscFunctionBegin;
16440700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1645e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1646e35cf81dSBarry Smith   PetscFunctionReturn(0);
1647e35cf81dSBarry Smith }
1648e35cf81dSBarry Smith 
1649e35cf81dSBarry Smith #undef __FUNCT__
16504a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
16519b94acceSBarry Smith /*@
1652d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
16539b94acceSBarry Smith 
16543f9fe445SBarry Smith    Logically Collective on SNES
1655c7afd0dbSLois Curfman McInnes 
16569b94acceSBarry Smith    Input Parameters:
1657c7afd0dbSLois Curfman McInnes +  snes - the SNES context
165870441072SBarry Smith .  abstol - absolute convergence tolerance
165933174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
166033174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
166133174efeSLois Curfman McInnes            of the change in the solution between steps
166233174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1663c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1664fee21e36SBarry Smith 
166533174efeSLois Curfman McInnes    Options Database Keys:
166670441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1667c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1668c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1669c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1670c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
16719b94acceSBarry Smith 
1672d7a720efSLois Curfman McInnes    Notes:
16739b94acceSBarry Smith    The default maximum number of iterations is 50.
16749b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
16759b94acceSBarry Smith 
167636851e7fSLois Curfman McInnes    Level: intermediate
167736851e7fSLois Curfman McInnes 
167833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
16799b94acceSBarry Smith 
16802492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
16819b94acceSBarry Smith @*/
168263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
16839b94acceSBarry Smith {
16843a40ed3dSBarry Smith   PetscFunctionBegin;
16850700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1686*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,abstol,2);
1687*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol,3);
1688*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,stol,4);
1689*c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxit,5);
1690*c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,maxf,6);
1691*c5eb9154SBarry Smith 
169270441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1693d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1694d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1695d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1696d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
16973a40ed3dSBarry Smith   PetscFunctionReturn(0);
16989b94acceSBarry Smith }
16999b94acceSBarry Smith 
17004a2ae208SSatish Balay #undef __FUNCT__
17014a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
17029b94acceSBarry Smith /*@
170333174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
170433174efeSLois Curfman McInnes 
1705c7afd0dbSLois Curfman McInnes    Not Collective
1706c7afd0dbSLois Curfman McInnes 
170733174efeSLois Curfman McInnes    Input Parameters:
1708c7afd0dbSLois Curfman McInnes +  snes - the SNES context
170985385478SLisandro Dalcin .  atol - absolute convergence tolerance
171033174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
171133174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
171233174efeSLois Curfman McInnes            of the change in the solution between steps
171333174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1714c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1715fee21e36SBarry Smith 
171633174efeSLois Curfman McInnes    Notes:
171733174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
171833174efeSLois Curfman McInnes 
171936851e7fSLois Curfman McInnes    Level: intermediate
172036851e7fSLois Curfman McInnes 
172133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
172233174efeSLois Curfman McInnes 
172333174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
172433174efeSLois Curfman McInnes @*/
172585385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
172633174efeSLois Curfman McInnes {
17273a40ed3dSBarry Smith   PetscFunctionBegin;
17280700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
172985385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
173033174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
173133174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
173233174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
173333174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
17343a40ed3dSBarry Smith   PetscFunctionReturn(0);
173533174efeSLois Curfman McInnes }
173633174efeSLois Curfman McInnes 
17374a2ae208SSatish Balay #undef __FUNCT__
17384a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
173933174efeSLois Curfman McInnes /*@
17409b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
17419b94acceSBarry Smith 
17423f9fe445SBarry Smith    Logically Collective on SNES
1743fee21e36SBarry Smith 
1744c7afd0dbSLois Curfman McInnes    Input Parameters:
1745c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1746c7afd0dbSLois Curfman McInnes -  tol - tolerance
1747c7afd0dbSLois Curfman McInnes 
17489b94acceSBarry Smith    Options Database Key:
1749c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
17509b94acceSBarry Smith 
175136851e7fSLois Curfman McInnes    Level: intermediate
175236851e7fSLois Curfman McInnes 
17539b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
17549b94acceSBarry Smith 
17552492ecdbSBarry Smith .seealso: SNESSetTolerances()
17569b94acceSBarry Smith @*/
175763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
17589b94acceSBarry Smith {
17593a40ed3dSBarry Smith   PetscFunctionBegin;
17600700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1761*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,tol,2);
17629b94acceSBarry Smith   snes->deltatol = tol;
17633a40ed3dSBarry Smith   PetscFunctionReturn(0);
17649b94acceSBarry Smith }
17659b94acceSBarry Smith 
1766df9fa365SBarry Smith /*
1767df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1768df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1769df9fa365SBarry Smith    macros instead of functions
1770df9fa365SBarry Smith */
17714a2ae208SSatish Balay #undef __FUNCT__
1772a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1773a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1774ce1608b8SBarry Smith {
1775dfbe8321SBarry Smith   PetscErrorCode ierr;
1776ce1608b8SBarry Smith 
1777ce1608b8SBarry Smith   PetscFunctionBegin;
17780700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1779a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1780ce1608b8SBarry Smith   PetscFunctionReturn(0);
1781ce1608b8SBarry Smith }
1782ce1608b8SBarry Smith 
17834a2ae208SSatish Balay #undef __FUNCT__
1784a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1785a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1786df9fa365SBarry Smith {
1787dfbe8321SBarry Smith   PetscErrorCode ierr;
1788df9fa365SBarry Smith 
1789df9fa365SBarry Smith   PetscFunctionBegin;
1790a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1791df9fa365SBarry Smith   PetscFunctionReturn(0);
1792df9fa365SBarry Smith }
1793df9fa365SBarry Smith 
17944a2ae208SSatish Balay #undef __FUNCT__
1795a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1796a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1797df9fa365SBarry Smith {
1798dfbe8321SBarry Smith   PetscErrorCode ierr;
1799df9fa365SBarry Smith 
1800df9fa365SBarry Smith   PetscFunctionBegin;
1801a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1802df9fa365SBarry Smith   PetscFunctionReturn(0);
1803df9fa365SBarry Smith }
1804df9fa365SBarry Smith 
1805b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1806b271bb04SBarry Smith #undef __FUNCT__
1807b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
1808b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1809b271bb04SBarry Smith {
1810b271bb04SBarry Smith   PetscDrawLG      lg;
1811b271bb04SBarry Smith   PetscErrorCode   ierr;
1812b271bb04SBarry Smith   PetscReal        x,y,per;
1813b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1814b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1815b271bb04SBarry Smith   PetscDraw        draw;
1816b271bb04SBarry Smith   PetscFunctionBegin;
1817b271bb04SBarry Smith   if (!monctx) {
1818b271bb04SBarry Smith     MPI_Comm    comm;
1819b271bb04SBarry Smith 
1820b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1821b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1822b271bb04SBarry Smith   }
1823b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1824b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1825b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1826b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1827b271bb04SBarry Smith   x = (PetscReal) n;
1828b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1829b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1830b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1831b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1832b271bb04SBarry Smith   }
1833b271bb04SBarry Smith 
1834b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1835b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1836b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1837b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1838b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1839b271bb04SBarry Smith   x = (PetscReal) n;
1840b271bb04SBarry Smith   y = 100.0*per;
1841b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1842b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1843b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1844b271bb04SBarry Smith   }
1845b271bb04SBarry Smith 
1846b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1847b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1848b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1849b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1850b271bb04SBarry Smith   x = (PetscReal) n;
1851b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1852b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1853b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1854b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1855b271bb04SBarry Smith   }
1856b271bb04SBarry Smith 
1857b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1858b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1859b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1860b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1861b271bb04SBarry Smith   x = (PetscReal) n;
1862b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1863b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1864b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1865b271bb04SBarry Smith   }
1866b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1867b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1868b271bb04SBarry Smith   }
1869b271bb04SBarry Smith   prev = rnorm;
1870b271bb04SBarry Smith   PetscFunctionReturn(0);
1871b271bb04SBarry Smith }
1872b271bb04SBarry Smith 
1873b271bb04SBarry Smith #undef __FUNCT__
1874b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
1875b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1876b271bb04SBarry Smith {
1877b271bb04SBarry Smith   PetscErrorCode ierr;
1878b271bb04SBarry Smith 
1879b271bb04SBarry Smith   PetscFunctionBegin;
1880b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1881b271bb04SBarry Smith   PetscFunctionReturn(0);
1882b271bb04SBarry Smith }
1883b271bb04SBarry Smith 
1884b271bb04SBarry Smith #undef __FUNCT__
1885b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
1886b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw)
1887b271bb04SBarry Smith {
1888b271bb04SBarry Smith   PetscErrorCode ierr;
1889b271bb04SBarry Smith 
1890b271bb04SBarry Smith   PetscFunctionBegin;
1891b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1892b271bb04SBarry Smith   PetscFunctionReturn(0);
1893b271bb04SBarry Smith }
1894b271bb04SBarry Smith 
18959b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
18969b94acceSBarry Smith 
18974a2ae208SSatish Balay #undef __FUNCT__
1898a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
18999b94acceSBarry Smith /*@C
1900a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
19019b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
19029b94acceSBarry Smith    progress.
19039b94acceSBarry Smith 
19043f9fe445SBarry Smith    Logically Collective on SNES
1905fee21e36SBarry Smith 
1906c7afd0dbSLois Curfman McInnes    Input Parameters:
1907c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1908c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1909b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1910e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1911b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1912b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
19139b94acceSBarry Smith 
1914c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1915a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1916c7afd0dbSLois Curfman McInnes 
1917c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1918c7afd0dbSLois Curfman McInnes .    its - iteration number
1919c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
192040a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
19219b94acceSBarry Smith 
19229665c990SLois Curfman McInnes    Options Database Keys:
1923a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1924a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1925a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1926a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1927c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1928a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1929c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1930c7afd0dbSLois Curfman McInnes                             the options database.
19319665c990SLois Curfman McInnes 
1932639f9d9dSBarry Smith    Notes:
19336bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1934a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
19356bc08f3fSLois Curfman McInnes    order in which they were set.
1936639f9d9dSBarry Smith 
1937025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1938025f1a04SBarry Smith 
193936851e7fSLois Curfman McInnes    Level: intermediate
194036851e7fSLois Curfman McInnes 
19419b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
19429b94acceSBarry Smith 
1943a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
19449b94acceSBarry Smith @*/
1945b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
19469b94acceSBarry Smith {
1947b90d0a6eSBarry Smith   PetscInt i;
1948b90d0a6eSBarry Smith 
19493a40ed3dSBarry Smith   PetscFunctionBegin;
19500700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
195117186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1952b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1953b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1954b90d0a6eSBarry Smith 
1955b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1956b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1957b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1958b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1959b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1960b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1961b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1962b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1963b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1964b90d0a6eSBarry Smith         }
1965b90d0a6eSBarry Smith       }
1966b90d0a6eSBarry Smith     }
1967b90d0a6eSBarry Smith   }
1968b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1969b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1970639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
19713a40ed3dSBarry Smith   PetscFunctionReturn(0);
19729b94acceSBarry Smith }
19739b94acceSBarry Smith 
19744a2ae208SSatish Balay #undef __FUNCT__
1975a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
19765cd90555SBarry Smith /*@C
1977a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
19785cd90555SBarry Smith 
19793f9fe445SBarry Smith    Logically Collective on SNES
1980c7afd0dbSLois Curfman McInnes 
19815cd90555SBarry Smith    Input Parameters:
19825cd90555SBarry Smith .  snes - the SNES context
19835cd90555SBarry Smith 
19841a480d89SAdministrator    Options Database Key:
1985a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1986a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1987c7afd0dbSLois Curfman McInnes     set via the options database
19885cd90555SBarry Smith 
19895cd90555SBarry Smith    Notes:
19905cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
19915cd90555SBarry Smith 
199236851e7fSLois Curfman McInnes    Level: intermediate
199336851e7fSLois Curfman McInnes 
19945cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
19955cd90555SBarry Smith 
1996a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
19975cd90555SBarry Smith @*/
1998a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
19995cd90555SBarry Smith {
2000d952e501SBarry Smith   PetscErrorCode ierr;
2001d952e501SBarry Smith   PetscInt       i;
2002d952e501SBarry Smith 
20035cd90555SBarry Smith   PetscFunctionBegin;
20040700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2005d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
2006d952e501SBarry Smith     if (snes->monitordestroy[i]) {
2007d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
2008d952e501SBarry Smith     }
2009d952e501SBarry Smith   }
20105cd90555SBarry Smith   snes->numbermonitors = 0;
20115cd90555SBarry Smith   PetscFunctionReturn(0);
20125cd90555SBarry Smith }
20135cd90555SBarry Smith 
20144a2ae208SSatish Balay #undef __FUNCT__
20154a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
20169b94acceSBarry Smith /*@C
20179b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
20189b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
20199b94acceSBarry Smith 
20203f9fe445SBarry Smith    Logically Collective on SNES
2021fee21e36SBarry Smith 
2022c7afd0dbSLois Curfman McInnes    Input Parameters:
2023c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2024c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
20257f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
20267f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
20279b94acceSBarry Smith 
2028c7afd0dbSLois Curfman McInnes    Calling sequence of func:
202906ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2030c7afd0dbSLois Curfman McInnes 
2031c7afd0dbSLois Curfman McInnes +    snes - the SNES context
203206ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2033c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2034184914b5SBarry Smith .    reason - reason for convergence/divergence
2035c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
20364b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
20374b27c08aSLois Curfman McInnes -    f - 2-norm of function
20389b94acceSBarry Smith 
203936851e7fSLois Curfman McInnes    Level: advanced
204036851e7fSLois Curfman McInnes 
20419b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
20429b94acceSBarry Smith 
204385385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
20449b94acceSBarry Smith @*/
20457f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
20469b94acceSBarry Smith {
20477f7931b9SBarry Smith   PetscErrorCode ierr;
20487f7931b9SBarry Smith 
20493a40ed3dSBarry Smith   PetscFunctionBegin;
20500700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
205185385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
20527f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
20537f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
20547f7931b9SBarry Smith   }
205585385478SLisandro Dalcin   snes->ops->converged        = func;
20567f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
205785385478SLisandro Dalcin   snes->cnvP                  = cctx;
20583a40ed3dSBarry Smith   PetscFunctionReturn(0);
20599b94acceSBarry Smith }
20609b94acceSBarry Smith 
20614a2ae208SSatish Balay #undef __FUNCT__
20624a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
206352baeb72SSatish Balay /*@
2064184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2065184914b5SBarry Smith 
2066184914b5SBarry Smith    Not Collective
2067184914b5SBarry Smith 
2068184914b5SBarry Smith    Input Parameter:
2069184914b5SBarry Smith .  snes - the SNES context
2070184914b5SBarry Smith 
2071184914b5SBarry Smith    Output Parameter:
20724d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2073184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2074184914b5SBarry Smith 
2075184914b5SBarry Smith    Level: intermediate
2076184914b5SBarry Smith 
2077184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2078184914b5SBarry Smith 
2079184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2080184914b5SBarry Smith 
208185385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2082184914b5SBarry Smith @*/
208363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2084184914b5SBarry Smith {
2085184914b5SBarry Smith   PetscFunctionBegin;
20860700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
20874482741eSBarry Smith   PetscValidPointer(reason,2);
2088184914b5SBarry Smith   *reason = snes->reason;
2089184914b5SBarry Smith   PetscFunctionReturn(0);
2090184914b5SBarry Smith }
2091184914b5SBarry Smith 
20924a2ae208SSatish Balay #undef __FUNCT__
20934a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2094c9005455SLois Curfman McInnes /*@
2095c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2096c9005455SLois Curfman McInnes 
20973f9fe445SBarry Smith    Logically Collective on SNES
2098fee21e36SBarry Smith 
2099c7afd0dbSLois Curfman McInnes    Input Parameters:
2100c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
21018c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2102cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2103758f92a0SBarry Smith .  na  - size of a and its
210464731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2105758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2106c7afd0dbSLois Curfman McInnes 
2107c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2108c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2109c9005455SLois Curfman McInnes    during the section of code that is being timed.
2110c9005455SLois Curfman McInnes 
211136851e7fSLois Curfman McInnes    Level: intermediate
211236851e7fSLois Curfman McInnes 
2113c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2114758f92a0SBarry Smith 
211508405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2116758f92a0SBarry Smith 
2117c9005455SLois Curfman McInnes @*/
2118a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
2119c9005455SLois Curfman McInnes {
21203a40ed3dSBarry Smith   PetscFunctionBegin;
21210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
21224482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2123a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2124c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2125758f92a0SBarry Smith   snes->conv_hist_its   = its;
2126758f92a0SBarry Smith   snes->conv_hist_max   = na;
2127a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2128758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2129758f92a0SBarry Smith   PetscFunctionReturn(0);
2130758f92a0SBarry Smith }
2131758f92a0SBarry Smith 
21324a2ae208SSatish Balay #undef __FUNCT__
21334a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
21340c4c9dddSBarry Smith /*@C
2135758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2136758f92a0SBarry Smith 
21373f9fe445SBarry Smith    Not Collective
2138758f92a0SBarry Smith 
2139758f92a0SBarry Smith    Input Parameter:
2140758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2141758f92a0SBarry Smith 
2142758f92a0SBarry Smith    Output Parameters:
2143758f92a0SBarry Smith .  a   - array to hold history
2144758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2145758f92a0SBarry Smith          negative if not converged) for each solve.
2146758f92a0SBarry Smith -  na  - size of a and its
2147758f92a0SBarry Smith 
2148758f92a0SBarry Smith    Notes:
2149758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2150758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2151758f92a0SBarry Smith 
2152758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2153758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2154758f92a0SBarry Smith    during the section of code that is being timed.
2155758f92a0SBarry Smith 
2156758f92a0SBarry Smith    Level: intermediate
2157758f92a0SBarry Smith 
2158758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2159758f92a0SBarry Smith 
2160758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2161758f92a0SBarry Smith 
2162758f92a0SBarry Smith @*/
216363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2164758f92a0SBarry Smith {
2165758f92a0SBarry Smith   PetscFunctionBegin;
21660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2167758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2168758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2169758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
21703a40ed3dSBarry Smith   PetscFunctionReturn(0);
2171c9005455SLois Curfman McInnes }
2172c9005455SLois Curfman McInnes 
2173e74ef692SMatthew Knepley #undef __FUNCT__
2174e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2175ac226902SBarry Smith /*@C
217676b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
21777e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
21787e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
217976b2cf59SMatthew Knepley 
21803f9fe445SBarry Smith   Logically Collective on SNES
218176b2cf59SMatthew Knepley 
218276b2cf59SMatthew Knepley   Input Parameters:
218376b2cf59SMatthew Knepley . snes - The nonlinear solver context
218476b2cf59SMatthew Knepley . func - The function
218576b2cf59SMatthew Knepley 
218676b2cf59SMatthew Knepley   Calling sequence of func:
2187b5d30489SBarry Smith . func (SNES snes, PetscInt step);
218876b2cf59SMatthew Knepley 
218976b2cf59SMatthew Knepley . step - The current step of the iteration
219076b2cf59SMatthew Knepley 
2191fe97e370SBarry Smith   Level: advanced
2192fe97e370SBarry Smith 
2193fe97e370SBarry 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()
2194fe97e370SBarry Smith         This is not used by most users.
219576b2cf59SMatthew Knepley 
219676b2cf59SMatthew Knepley .keywords: SNES, update
2197b5d30489SBarry Smith 
219885385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
219976b2cf59SMatthew Knepley @*/
220063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
220176b2cf59SMatthew Knepley {
220276b2cf59SMatthew Knepley   PetscFunctionBegin;
22030700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2204e7788613SBarry Smith   snes->ops->update = func;
220576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
220676b2cf59SMatthew Knepley }
220776b2cf59SMatthew Knepley 
2208e74ef692SMatthew Knepley #undef __FUNCT__
2209e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
221076b2cf59SMatthew Knepley /*@
221176b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
221276b2cf59SMatthew Knepley 
221376b2cf59SMatthew Knepley   Not collective
221476b2cf59SMatthew Knepley 
221576b2cf59SMatthew Knepley   Input Parameters:
221676b2cf59SMatthew Knepley . snes - The nonlinear solver context
221776b2cf59SMatthew Knepley . step - The current step of the iteration
221876b2cf59SMatthew Knepley 
2219205452f4SMatthew Knepley   Level: intermediate
2220205452f4SMatthew Knepley 
222176b2cf59SMatthew Knepley .keywords: SNES, update
2222a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
222376b2cf59SMatthew Knepley @*/
222463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
222576b2cf59SMatthew Knepley {
222676b2cf59SMatthew Knepley   PetscFunctionBegin;
222776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
222876b2cf59SMatthew Knepley }
222976b2cf59SMatthew Knepley 
22304a2ae208SSatish Balay #undef __FUNCT__
22314a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
22329b94acceSBarry Smith /*
22339b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
22349b94acceSBarry Smith    positive parameter delta.
22359b94acceSBarry Smith 
22369b94acceSBarry Smith     Input Parameters:
2237c7afd0dbSLois Curfman McInnes +   snes - the SNES context
22389b94acceSBarry Smith .   y - approximate solution of linear system
22399b94acceSBarry Smith .   fnorm - 2-norm of current function
2240c7afd0dbSLois Curfman McInnes -   delta - trust region size
22419b94acceSBarry Smith 
22429b94acceSBarry Smith     Output Parameters:
2243c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
22449b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
22459b94acceSBarry Smith     region, and exceeds zero otherwise.
2246c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
22479b94acceSBarry Smith 
22489b94acceSBarry Smith     Note:
22494b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
22509b94acceSBarry Smith     is set to be the maximum allowable step size.
22519b94acceSBarry Smith 
22529b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
22539b94acceSBarry Smith */
2254dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
22559b94acceSBarry Smith {
2256064f8208SBarry Smith   PetscReal      nrm;
2257ea709b57SSatish Balay   PetscScalar    cnorm;
2258dfbe8321SBarry Smith   PetscErrorCode ierr;
22593a40ed3dSBarry Smith 
22603a40ed3dSBarry Smith   PetscFunctionBegin;
22610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22620700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2263c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2264184914b5SBarry Smith 
2265064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2266064f8208SBarry Smith   if (nrm > *delta) {
2267064f8208SBarry Smith      nrm = *delta/nrm;
2268064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2269064f8208SBarry Smith      cnorm = nrm;
22702dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
22719b94acceSBarry Smith      *ynorm = *delta;
22729b94acceSBarry Smith   } else {
22739b94acceSBarry Smith      *gpnorm = 0.0;
2274064f8208SBarry Smith      *ynorm = nrm;
22759b94acceSBarry Smith   }
22763a40ed3dSBarry Smith   PetscFunctionReturn(0);
22779b94acceSBarry Smith }
22789b94acceSBarry Smith 
22794a2ae208SSatish Balay #undef __FUNCT__
22804a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
22816ce558aeSBarry Smith /*@C
2282f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2283f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
22849b94acceSBarry Smith 
2285c7afd0dbSLois Curfman McInnes    Collective on SNES
2286c7afd0dbSLois Curfman McInnes 
2287b2002411SLois Curfman McInnes    Input Parameters:
2288c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2289f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
229085385478SLisandro Dalcin -  x - the solution vector.
22919b94acceSBarry Smith 
2292b2002411SLois Curfman McInnes    Notes:
22938ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
22948ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
22958ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
22968ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
22978ddd3da0SLois Curfman McInnes 
229836851e7fSLois Curfman McInnes    Level: beginner
229936851e7fSLois Curfman McInnes 
23009b94acceSBarry Smith .keywords: SNES, nonlinear, solve
23019b94acceSBarry Smith 
230285385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
23039b94acceSBarry Smith @*/
2304f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
23059b94acceSBarry Smith {
2306dfbe8321SBarry Smith   PetscErrorCode ierr;
2307f1af5d2fSBarry Smith   PetscTruth     flg;
2308eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2309eabae89aSBarry Smith   PetscViewer    viewer;
2310052efed2SBarry Smith 
23113a40ed3dSBarry Smith   PetscFunctionBegin;
23120700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23130700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2314f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
23150700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
231685385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
231785385478SLisandro Dalcin 
231885385478SLisandro Dalcin   /* set solution vector */
231985385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
232085385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
232185385478SLisandro Dalcin   snes->vec_sol = x;
232285385478SLisandro Dalcin   /* set afine vector if provided */
232385385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
232485385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
232585385478SLisandro Dalcin   snes->vec_rhs = b;
232685385478SLisandro Dalcin 
232785385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
232885385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func);CHKERRQ(ierr);
232970e92668SMatthew Knepley   }
23303f149594SLisandro Dalcin 
233170e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
23323f149594SLisandro Dalcin 
2333abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
233450ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2335d5e45103SBarry Smith 
23363f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
23374936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
233885385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
23394936397dSBarry Smith   if (snes->domainerror){
23404936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
23414936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
23424936397dSBarry Smith   }
234317186662SBarry Smith   if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
23443f149594SLisandro Dalcin 
23457adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2346eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
23477adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2348eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2349eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2350eabae89aSBarry Smith   }
2351eabae89aSBarry Smith 
235290d69ab7SBarry Smith   flg  = PETSC_FALSE;
235390d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2354da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
23555968eb51SBarry Smith   if (snes->printreason) {
23565968eb51SBarry Smith     if (snes->reason > 0) {
23577adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
23585968eb51SBarry Smith     } else {
23597adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
23605968eb51SBarry Smith     }
23615968eb51SBarry Smith   }
23625968eb51SBarry Smith 
2363e113a28aSBarry Smith   if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
23643a40ed3dSBarry Smith   PetscFunctionReturn(0);
23659b94acceSBarry Smith }
23669b94acceSBarry Smith 
23679b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
23689b94acceSBarry Smith 
23694a2ae208SSatish Balay #undef __FUNCT__
23704a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
237182bf6240SBarry Smith /*@C
23724b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
23739b94acceSBarry Smith 
2374fee21e36SBarry Smith    Collective on SNES
2375fee21e36SBarry Smith 
2376c7afd0dbSLois Curfman McInnes    Input Parameters:
2377c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2378454a90a3SBarry Smith -  type - a known method
2379c7afd0dbSLois Curfman McInnes 
2380c7afd0dbSLois Curfman McInnes    Options Database Key:
2381454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2382c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2383ae12b187SLois Curfman McInnes 
23849b94acceSBarry Smith    Notes:
2385e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
23864b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2387c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23884b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2389c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23909b94acceSBarry Smith 
2391ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2392ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2393ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2394ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2395ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2396ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2397ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2398ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2399ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2400b0a32e0cSBarry Smith   appropriate method.
240136851e7fSLois Curfman McInnes 
240236851e7fSLois Curfman McInnes   Level: intermediate
2403a703fe33SLois Curfman McInnes 
2404454a90a3SBarry Smith .keywords: SNES, set, type
2405435da068SBarry Smith 
2406435da068SBarry Smith .seealso: SNESType, SNESCreate()
2407435da068SBarry Smith 
24089b94acceSBarry Smith @*/
2409a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
24109b94acceSBarry Smith {
2411dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
24126831982aSBarry Smith   PetscTruth     match;
24133a40ed3dSBarry Smith 
24143a40ed3dSBarry Smith   PetscFunctionBegin;
24150700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24164482741eSBarry Smith   PetscValidCharPointer(type,2);
241782bf6240SBarry Smith 
24186831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
24190f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
242092ff6ae8SBarry Smith 
24217adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2422e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
242375396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
242475396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
242575396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
242675396ef9SLisandro Dalcin   snes->ops->setup          = 0;
242775396ef9SLisandro Dalcin   snes->ops->solve          = 0;
242875396ef9SLisandro Dalcin   snes->ops->view           = 0;
242975396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
243075396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
243175396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
243275396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
24333a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2434454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
24353a40ed3dSBarry Smith   PetscFunctionReturn(0);
24369b94acceSBarry Smith }
24379b94acceSBarry Smith 
2438a847f771SSatish Balay 
24399b94acceSBarry Smith /* --------------------------------------------------------------------- */
24404a2ae208SSatish Balay #undef __FUNCT__
24414a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
244252baeb72SSatish Balay /*@
24439b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2444f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
24459b94acceSBarry Smith 
2446fee21e36SBarry Smith    Not Collective
2447fee21e36SBarry Smith 
244836851e7fSLois Curfman McInnes    Level: advanced
244936851e7fSLois Curfman McInnes 
24509b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
24519b94acceSBarry Smith 
24529b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
24539b94acceSBarry Smith @*/
245463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
24559b94acceSBarry Smith {
2456dfbe8321SBarry Smith   PetscErrorCode ierr;
245782bf6240SBarry Smith 
24583a40ed3dSBarry Smith   PetscFunctionBegin;
24591441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
24604c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
24613a40ed3dSBarry Smith   PetscFunctionReturn(0);
24629b94acceSBarry Smith }
24639b94acceSBarry Smith 
24644a2ae208SSatish Balay #undef __FUNCT__
24654a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
24669b94acceSBarry Smith /*@C
24679a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
24689b94acceSBarry Smith 
2469c7afd0dbSLois Curfman McInnes    Not Collective
2470c7afd0dbSLois Curfman McInnes 
24719b94acceSBarry Smith    Input Parameter:
24724b0e389bSBarry Smith .  snes - nonlinear solver context
24739b94acceSBarry Smith 
24749b94acceSBarry Smith    Output Parameter:
24753a7fca6bSBarry Smith .  type - SNES method (a character string)
24769b94acceSBarry Smith 
247736851e7fSLois Curfman McInnes    Level: intermediate
247836851e7fSLois Curfman McInnes 
2479454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
24809b94acceSBarry Smith @*/
2481a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
24829b94acceSBarry Smith {
24833a40ed3dSBarry Smith   PetscFunctionBegin;
24840700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24854482741eSBarry Smith   PetscValidPointer(type,2);
24867adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
24873a40ed3dSBarry Smith   PetscFunctionReturn(0);
24889b94acceSBarry Smith }
24899b94acceSBarry Smith 
24904a2ae208SSatish Balay #undef __FUNCT__
24914a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
249252baeb72SSatish Balay /*@
24939b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
24949b94acceSBarry Smith    stored.
24959b94acceSBarry Smith 
2496c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2497c7afd0dbSLois Curfman McInnes 
24989b94acceSBarry Smith    Input Parameter:
24999b94acceSBarry Smith .  snes - the SNES context
25009b94acceSBarry Smith 
25019b94acceSBarry Smith    Output Parameter:
25029b94acceSBarry Smith .  x - the solution
25039b94acceSBarry Smith 
250470e92668SMatthew Knepley    Level: intermediate
250536851e7fSLois Curfman McInnes 
25069b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
25079b94acceSBarry Smith 
250885385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
25099b94acceSBarry Smith @*/
251063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
25119b94acceSBarry Smith {
25123a40ed3dSBarry Smith   PetscFunctionBegin;
25130700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25144482741eSBarry Smith   PetscValidPointer(x,2);
251585385478SLisandro Dalcin   *x = snes->vec_sol;
251670e92668SMatthew Knepley   PetscFunctionReturn(0);
251770e92668SMatthew Knepley }
251870e92668SMatthew Knepley 
251970e92668SMatthew Knepley #undef __FUNCT__
25204a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
252152baeb72SSatish Balay /*@
25229b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
25239b94acceSBarry Smith    stored.
25249b94acceSBarry Smith 
2525c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2526c7afd0dbSLois Curfman McInnes 
25279b94acceSBarry Smith    Input Parameter:
25289b94acceSBarry Smith .  snes - the SNES context
25299b94acceSBarry Smith 
25309b94acceSBarry Smith    Output Parameter:
25319b94acceSBarry Smith .  x - the solution update
25329b94acceSBarry Smith 
253336851e7fSLois Curfman McInnes    Level: advanced
253436851e7fSLois Curfman McInnes 
25359b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
25369b94acceSBarry Smith 
253785385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
25389b94acceSBarry Smith @*/
253963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
25409b94acceSBarry Smith {
25413a40ed3dSBarry Smith   PetscFunctionBegin;
25420700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25434482741eSBarry Smith   PetscValidPointer(x,2);
254485385478SLisandro Dalcin   *x = snes->vec_sol_update;
25453a40ed3dSBarry Smith   PetscFunctionReturn(0);
25469b94acceSBarry Smith }
25479b94acceSBarry Smith 
25484a2ae208SSatish Balay #undef __FUNCT__
25494a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
25509b94acceSBarry Smith /*@C
25513638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
25529b94acceSBarry Smith 
2553c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2554c7afd0dbSLois Curfman McInnes 
25559b94acceSBarry Smith    Input Parameter:
25569b94acceSBarry Smith .  snes - the SNES context
25579b94acceSBarry Smith 
25589b94acceSBarry Smith    Output Parameter:
25597bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
256070e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
256170e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
25629b94acceSBarry Smith 
256336851e7fSLois Curfman McInnes    Level: advanced
256436851e7fSLois Curfman McInnes 
2565a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
25669b94acceSBarry Smith 
25674b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
25689b94acceSBarry Smith @*/
256970e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
25709b94acceSBarry Smith {
25713a40ed3dSBarry Smith   PetscFunctionBegin;
25720700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
257385385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2574e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
257570e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
25763a40ed3dSBarry Smith   PetscFunctionReturn(0);
25779b94acceSBarry Smith }
25789b94acceSBarry Smith 
25794a2ae208SSatish Balay #undef __FUNCT__
25804a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
25813c7409f5SSatish Balay /*@C
25823c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2583d850072dSLois Curfman McInnes    SNES options in the database.
25843c7409f5SSatish Balay 
25853f9fe445SBarry Smith    Logically Collective on SNES
2586fee21e36SBarry Smith 
2587c7afd0dbSLois Curfman McInnes    Input Parameter:
2588c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2589c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2590c7afd0dbSLois Curfman McInnes 
2591d850072dSLois Curfman McInnes    Notes:
2592a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2593c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2594d850072dSLois Curfman McInnes 
259536851e7fSLois Curfman McInnes    Level: advanced
259636851e7fSLois Curfman McInnes 
25973c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2598a86d99e1SLois Curfman McInnes 
2599a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
26003c7409f5SSatish Balay @*/
260163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
26023c7409f5SSatish Balay {
2603dfbe8321SBarry Smith   PetscErrorCode ierr;
26043c7409f5SSatish Balay 
26053a40ed3dSBarry Smith   PetscFunctionBegin;
26060700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2607639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26081cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
260994b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
26103a40ed3dSBarry Smith   PetscFunctionReturn(0);
26113c7409f5SSatish Balay }
26123c7409f5SSatish Balay 
26134a2ae208SSatish Balay #undef __FUNCT__
26144a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
26153c7409f5SSatish Balay /*@C
2616f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2617d850072dSLois Curfman McInnes    SNES options in the database.
26183c7409f5SSatish Balay 
26193f9fe445SBarry Smith    Logically Collective on SNES
2620fee21e36SBarry Smith 
2621c7afd0dbSLois Curfman McInnes    Input Parameters:
2622c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2623c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2624c7afd0dbSLois Curfman McInnes 
2625d850072dSLois Curfman McInnes    Notes:
2626a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2627c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2628d850072dSLois Curfman McInnes 
262936851e7fSLois Curfman McInnes    Level: advanced
263036851e7fSLois Curfman McInnes 
26313c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2632a86d99e1SLois Curfman McInnes 
2633a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
26343c7409f5SSatish Balay @*/
263563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
26363c7409f5SSatish Balay {
2637dfbe8321SBarry Smith   PetscErrorCode ierr;
26383c7409f5SSatish Balay 
26393a40ed3dSBarry Smith   PetscFunctionBegin;
26400700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2641639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26421cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
264394b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
26443a40ed3dSBarry Smith   PetscFunctionReturn(0);
26453c7409f5SSatish Balay }
26463c7409f5SSatish Balay 
26474a2ae208SSatish Balay #undef __FUNCT__
26484a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
26499ab63eb5SSatish Balay /*@C
26503c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
26513c7409f5SSatish Balay    SNES options in the database.
26523c7409f5SSatish Balay 
2653c7afd0dbSLois Curfman McInnes    Not Collective
2654c7afd0dbSLois Curfman McInnes 
26553c7409f5SSatish Balay    Input Parameter:
26563c7409f5SSatish Balay .  snes - the SNES context
26573c7409f5SSatish Balay 
26583c7409f5SSatish Balay    Output Parameter:
26593c7409f5SSatish Balay .  prefix - pointer to the prefix string used
26603c7409f5SSatish Balay 
26614ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
26629ab63eb5SSatish Balay    sufficient length to hold the prefix.
26639ab63eb5SSatish Balay 
266436851e7fSLois Curfman McInnes    Level: advanced
266536851e7fSLois Curfman McInnes 
26663c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2667a86d99e1SLois Curfman McInnes 
2668a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
26693c7409f5SSatish Balay @*/
2670e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
26713c7409f5SSatish Balay {
2672dfbe8321SBarry Smith   PetscErrorCode ierr;
26733c7409f5SSatish Balay 
26743a40ed3dSBarry Smith   PetscFunctionBegin;
26750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2676639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26773a40ed3dSBarry Smith   PetscFunctionReturn(0);
26783c7409f5SSatish Balay }
26793c7409f5SSatish Balay 
2680b2002411SLois Curfman McInnes 
26814a2ae208SSatish Balay #undef __FUNCT__
26824a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
26833cea93caSBarry Smith /*@C
26843cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
26853cea93caSBarry Smith 
26867f6c08e0SMatthew Knepley   Level: advanced
26873cea93caSBarry Smith @*/
268863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2689b2002411SLois Curfman McInnes {
2690e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2691dfbe8321SBarry Smith   PetscErrorCode ierr;
2692b2002411SLois Curfman McInnes 
2693b2002411SLois Curfman McInnes   PetscFunctionBegin;
2694b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2695c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2696b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2697b2002411SLois Curfman McInnes }
2698da9b6338SBarry Smith 
2699da9b6338SBarry Smith #undef __FUNCT__
2700da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
270163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2702da9b6338SBarry Smith {
2703dfbe8321SBarry Smith   PetscErrorCode ierr;
270477431f27SBarry Smith   PetscInt       N,i,j;
2705da9b6338SBarry Smith   Vec            u,uh,fh;
2706da9b6338SBarry Smith   PetscScalar    value;
2707da9b6338SBarry Smith   PetscReal      norm;
2708da9b6338SBarry Smith 
2709da9b6338SBarry Smith   PetscFunctionBegin;
2710da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2711da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2712da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2713da9b6338SBarry Smith 
2714da9b6338SBarry Smith   /* currently only works for sequential */
2715da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2716da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2717da9b6338SBarry Smith   for (i=0; i<N; i++) {
2718da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
271977431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2720da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2721ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2722da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
27233ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2724da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
272577431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2726da9b6338SBarry Smith       value = -value;
2727da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2728da9b6338SBarry Smith     }
2729da9b6338SBarry Smith   }
2730da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2731da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2732da9b6338SBarry Smith   PetscFunctionReturn(0);
2733da9b6338SBarry Smith }
273471f87433Sdalcinl 
273571f87433Sdalcinl #undef __FUNCT__
2736fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
273771f87433Sdalcinl /*@
2738fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
273971f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
274071f87433Sdalcinl    Newton method.
274171f87433Sdalcinl 
27423f9fe445SBarry Smith    Logically Collective on SNES
274371f87433Sdalcinl 
274471f87433Sdalcinl    Input Parameters:
274571f87433Sdalcinl +  snes - SNES context
274671f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
274771f87433Sdalcinl 
274864ba62caSBarry Smith     Options Database:
274964ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
275064ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
275164ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
275264ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
275364ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
275464ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
275564ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
275664ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
275764ba62caSBarry Smith 
275871f87433Sdalcinl    Notes:
275971f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
276071f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
276171f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
276271f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
276371f87433Sdalcinl    solver.
276471f87433Sdalcinl 
276571f87433Sdalcinl    Level: advanced
276671f87433Sdalcinl 
276771f87433Sdalcinl    Reference:
276871f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
276971f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
277071f87433Sdalcinl 
277171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
277271f87433Sdalcinl 
2773fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
277471f87433Sdalcinl @*/
2775fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
277671f87433Sdalcinl {
277771f87433Sdalcinl   PetscFunctionBegin;
27780700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2779*c5eb9154SBarry Smith   PetscValidLogicalCollectiveTruth(snes,flag,2);
278071f87433Sdalcinl   snes->ksp_ewconv = flag;
278171f87433Sdalcinl   PetscFunctionReturn(0);
278271f87433Sdalcinl }
278371f87433Sdalcinl 
278471f87433Sdalcinl #undef __FUNCT__
2785fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
278671f87433Sdalcinl /*@
2787fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
278871f87433Sdalcinl    for computing relative tolerance for linear solvers within an
278971f87433Sdalcinl    inexact Newton method.
279071f87433Sdalcinl 
279171f87433Sdalcinl    Not Collective
279271f87433Sdalcinl 
279371f87433Sdalcinl    Input Parameter:
279471f87433Sdalcinl .  snes - SNES context
279571f87433Sdalcinl 
279671f87433Sdalcinl    Output Parameter:
279771f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
279871f87433Sdalcinl 
279971f87433Sdalcinl    Notes:
280071f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
280171f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
280271f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
280371f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
280471f87433Sdalcinl    solver.
280571f87433Sdalcinl 
280671f87433Sdalcinl    Level: advanced
280771f87433Sdalcinl 
280871f87433Sdalcinl    Reference:
280971f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
281071f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
281171f87433Sdalcinl 
281271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
281371f87433Sdalcinl 
2814fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
281571f87433Sdalcinl @*/
2816fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
281771f87433Sdalcinl {
281871f87433Sdalcinl   PetscFunctionBegin;
28190700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
282071f87433Sdalcinl   PetscValidPointer(flag,2);
282171f87433Sdalcinl   *flag = snes->ksp_ewconv;
282271f87433Sdalcinl   PetscFunctionReturn(0);
282371f87433Sdalcinl }
282471f87433Sdalcinl 
282571f87433Sdalcinl #undef __FUNCT__
2826fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
282771f87433Sdalcinl /*@
2828fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
282971f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
283071f87433Sdalcinl    Newton method.
283171f87433Sdalcinl 
28323f9fe445SBarry Smith    Logically Collective on SNES
283371f87433Sdalcinl 
283471f87433Sdalcinl    Input Parameters:
283571f87433Sdalcinl +    snes - SNES context
283671f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
283771f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
283871f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
283971f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
284071f87433Sdalcinl              (0 <= gamma2 <= 1)
284171f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
284271f87433Sdalcinl .    alpha2 - power for safeguard
284371f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
284471f87433Sdalcinl 
284571f87433Sdalcinl    Note:
284671f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
284771f87433Sdalcinl 
284871f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
284971f87433Sdalcinl 
285071f87433Sdalcinl    Level: advanced
285171f87433Sdalcinl 
285271f87433Sdalcinl    Reference:
285371f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
285471f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
285571f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
285671f87433Sdalcinl 
285771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
285871f87433Sdalcinl 
2859fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
286071f87433Sdalcinl @*/
2861fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
286271f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
286371f87433Sdalcinl {
2864fa9f3622SBarry Smith   SNESKSPEW *kctx;
286571f87433Sdalcinl   PetscFunctionBegin;
28660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2867fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
2868e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
2869*c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(snes,version,2);
2870*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_0,3);
2871*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,rtol_max,4);
2872*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,gamma,5);
2873*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha,6);
2874*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,alpha2,7);
2875*c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(snes,threshold,8);
287671f87433Sdalcinl 
287771f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
287871f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
287971f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
288071f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
288171f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
288271f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
288371f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
288471f87433Sdalcinl 
288571f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
2886e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
288771f87433Sdalcinl   }
288871f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
2889e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
289071f87433Sdalcinl   }
289171f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
2892e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
289371f87433Sdalcinl   }
289471f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
2895e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
289671f87433Sdalcinl   }
289771f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
2898e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
289971f87433Sdalcinl   }
290071f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
2901e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
290271f87433Sdalcinl   }
290371f87433Sdalcinl   PetscFunctionReturn(0);
290471f87433Sdalcinl }
290571f87433Sdalcinl 
290671f87433Sdalcinl #undef __FUNCT__
2907fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
290871f87433Sdalcinl /*@
2909fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
291071f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
291171f87433Sdalcinl    Newton method.
291271f87433Sdalcinl 
291371f87433Sdalcinl    Not Collective
291471f87433Sdalcinl 
291571f87433Sdalcinl    Input Parameters:
291671f87433Sdalcinl      snes - SNES context
291771f87433Sdalcinl 
291871f87433Sdalcinl    Output Parameters:
291971f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
292071f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
292171f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
292271f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
292371f87433Sdalcinl              (0 <= gamma2 <= 1)
292471f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
292571f87433Sdalcinl .    alpha2 - power for safeguard
292671f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
292771f87433Sdalcinl 
292871f87433Sdalcinl    Level: advanced
292971f87433Sdalcinl 
293071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
293171f87433Sdalcinl 
2932fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
293371f87433Sdalcinl @*/
2934fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
293571f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
293671f87433Sdalcinl {
2937fa9f3622SBarry Smith   SNESKSPEW *kctx;
293871f87433Sdalcinl   PetscFunctionBegin;
29390700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2940fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
2941e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
294271f87433Sdalcinl   if(version)   *version   = kctx->version;
294371f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
294471f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
294571f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
294671f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
294771f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
294871f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
294971f87433Sdalcinl   PetscFunctionReturn(0);
295071f87433Sdalcinl }
295171f87433Sdalcinl 
295271f87433Sdalcinl #undef __FUNCT__
2953fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2954fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
295571f87433Sdalcinl {
295671f87433Sdalcinl   PetscErrorCode ierr;
2957fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
295871f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
295971f87433Sdalcinl 
296071f87433Sdalcinl   PetscFunctionBegin;
2961e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
296271f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
296371f87433Sdalcinl     rtol = kctx->rtol_0;
296471f87433Sdalcinl   } else {
296571f87433Sdalcinl     if (kctx->version == 1) {
296671f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
296771f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
296871f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
296971f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
297071f87433Sdalcinl     } else if (kctx->version == 2) {
297171f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
297271f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
297371f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
297471f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
297571f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
297671f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
297771f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
297871f87433Sdalcinl       stol = PetscMax(rtol,stol);
297971f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
298071f87433Sdalcinl       /* safeguard: avoid oversolving */
298171f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
298271f87433Sdalcinl       stol = PetscMax(rtol,stol);
298371f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
2984e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
298571f87433Sdalcinl   }
298671f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
298771f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
298871f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
298971f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
299071f87433Sdalcinl   PetscFunctionReturn(0);
299171f87433Sdalcinl }
299271f87433Sdalcinl 
299371f87433Sdalcinl #undef __FUNCT__
2994fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2995fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
299671f87433Sdalcinl {
299771f87433Sdalcinl   PetscErrorCode ierr;
2998fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
299971f87433Sdalcinl   PCSide         pcside;
300071f87433Sdalcinl   Vec            lres;
300171f87433Sdalcinl 
300271f87433Sdalcinl   PetscFunctionBegin;
3003e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
300471f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
300571f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
300671f87433Sdalcinl   if (kctx->version == 1) {
3007b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
300871f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
300971f87433Sdalcinl       /* KSP residual is true linear residual */
301071f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
301171f87433Sdalcinl     } else {
301271f87433Sdalcinl       /* KSP residual is preconditioned residual */
301371f87433Sdalcinl       /* compute true linear residual norm */
301471f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
301571f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
301671f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
301771f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
301871f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
301971f87433Sdalcinl     }
302071f87433Sdalcinl   }
302171f87433Sdalcinl   PetscFunctionReturn(0);
302271f87433Sdalcinl }
302371f87433Sdalcinl 
302471f87433Sdalcinl #undef __FUNCT__
302571f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
302671f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
302771f87433Sdalcinl {
302871f87433Sdalcinl   PetscErrorCode ierr;
302971f87433Sdalcinl 
303071f87433Sdalcinl   PetscFunctionBegin;
3031fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
303271f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3033fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
303471f87433Sdalcinl   PetscFunctionReturn(0);
303571f87433Sdalcinl }
30366c699258SBarry Smith 
30376c699258SBarry Smith #undef __FUNCT__
30386c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
30396c699258SBarry Smith /*@
30406c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
30416c699258SBarry Smith 
30423f9fe445SBarry Smith    Logically Collective on SNES
30436c699258SBarry Smith 
30446c699258SBarry Smith    Input Parameters:
30456c699258SBarry Smith +  snes - the preconditioner context
30466c699258SBarry Smith -  dm - the dm
30476c699258SBarry Smith 
30486c699258SBarry Smith    Level: intermediate
30496c699258SBarry Smith 
30506c699258SBarry Smith 
30516c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
30526c699258SBarry Smith @*/
30536c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetDM(SNES snes,DM dm)
30546c699258SBarry Smith {
30556c699258SBarry Smith   PetscErrorCode ierr;
3056345fed2cSBarry Smith   KSP            ksp;
30576c699258SBarry Smith 
30586c699258SBarry Smith   PetscFunctionBegin;
30590700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
306070663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
30616c699258SBarry Smith   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
30626c699258SBarry Smith   snes->dm = dm;
3063345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3064345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3065f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
30666c699258SBarry Smith   PetscFunctionReturn(0);
30676c699258SBarry Smith }
30686c699258SBarry Smith 
30696c699258SBarry Smith #undef __FUNCT__
30706c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
30716c699258SBarry Smith /*@
30726c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
30736c699258SBarry Smith 
30743f9fe445SBarry Smith    Not Collective but DM obtained is parallel on SNES
30756c699258SBarry Smith 
30766c699258SBarry Smith    Input Parameter:
30776c699258SBarry Smith . snes - the preconditioner context
30786c699258SBarry Smith 
30796c699258SBarry Smith    Output Parameter:
30806c699258SBarry Smith .  dm - the dm
30816c699258SBarry Smith 
30826c699258SBarry Smith    Level: intermediate
30836c699258SBarry Smith 
30846c699258SBarry Smith 
30856c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
30866c699258SBarry Smith @*/
30876c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetDM(SNES snes,DM *dm)
30886c699258SBarry Smith {
30896c699258SBarry Smith   PetscFunctionBegin;
30900700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
30916c699258SBarry Smith   *dm = snes->dm;
30926c699258SBarry Smith   PetscFunctionReturn(0);
30936c699258SBarry Smith }
3094