xref: /petsc/src/snes/interface/snes.c (revision e113a28a487f568249929c8f6c82686b04bc17a4)
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__
13*e113a28aSBarry Smith #define __FUNCT__ "SNESSetErrorIfNotConverged"
14*e113a28aSBarry Smith /*@
15*e113a28aSBarry Smith    SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged.
16*e113a28aSBarry Smith 
17*e113a28aSBarry Smith    Collective on SNES
18*e113a28aSBarry Smith 
19*e113a28aSBarry Smith    Input Parameters:
20*e113a28aSBarry Smith +  snes - iterative context obtained from SNESCreate()
21*e113a28aSBarry Smith -  flg - PETSC_TRUE indicates you want the error generated
22*e113a28aSBarry Smith 
23*e113a28aSBarry Smith    Options database keys:
24*e113a28aSBarry Smith .  -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false)
25*e113a28aSBarry Smith 
26*e113a28aSBarry Smith    Level: intermediate
27*e113a28aSBarry Smith 
28*e113a28aSBarry Smith    Notes:
29*e113a28aSBarry Smith     Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve()
30*e113a28aSBarry Smith     to determine if it has converged.
31*e113a28aSBarry Smith 
32*e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
33*e113a28aSBarry Smith 
34*e113a28aSBarry Smith .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
35*e113a28aSBarry Smith @*/
36*e113a28aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetErrorIfNotConverged(SNES snes,PetscTruth flg)
37*e113a28aSBarry Smith {
38*e113a28aSBarry Smith   PetscFunctionBegin;
39*e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
40*e113a28aSBarry Smith   snes->errorifnotconverged = flg;
41*e113a28aSBarry Smith   PetscFunctionReturn(0);
42*e113a28aSBarry Smith }
43*e113a28aSBarry Smith 
44*e113a28aSBarry Smith #undef __FUNCT__
45*e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged"
46*e113a28aSBarry Smith /*@
47*e113a28aSBarry Smith    SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge?
48*e113a28aSBarry Smith 
49*e113a28aSBarry Smith    Not Collective
50*e113a28aSBarry Smith 
51*e113a28aSBarry Smith    Input Parameter:
52*e113a28aSBarry Smith .  snes - iterative context obtained from SNESCreate()
53*e113a28aSBarry Smith 
54*e113a28aSBarry Smith    Output Parameter:
55*e113a28aSBarry Smith .  flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE
56*e113a28aSBarry Smith 
57*e113a28aSBarry Smith    Level: intermediate
58*e113a28aSBarry Smith 
59*e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero
60*e113a28aSBarry Smith 
61*e113a28aSBarry Smith .seealso:  SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged()
62*e113a28aSBarry Smith @*/
63*e113a28aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetErrorIfNotConverged(SNES snes,PetscTruth *flag)
64*e113a28aSBarry Smith {
65*e113a28aSBarry Smith   PetscFunctionBegin;
66*e113a28aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
67*e113a28aSBarry Smith   PetscValidPointer(flag,2);
68*e113a28aSBarry Smith   *flag = snes->errorifnotconverged;
69*e113a28aSBarry Smith   PetscFunctionReturn(0);
70*e113a28aSBarry Smith }
71*e113a28aSBarry Smith 
72*e113a28aSBarry Smith #undef __FUNCT__
734936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
74e725d27bSBarry Smith /*@
754936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
764936397dSBarry Smith      in the functions domain. For example, negative pressure.
774936397dSBarry Smith 
784936397dSBarry Smith    Collective on SNES
794936397dSBarry Smith 
804936397dSBarry Smith    Input Parameters:
814936397dSBarry Smith .  SNES - the SNES context
824936397dSBarry Smith 
8328529972SSatish Balay    Level: advanced
844936397dSBarry Smith 
854936397dSBarry Smith .keywords: SNES, view
864936397dSBarry Smith 
874936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
884936397dSBarry Smith @*/
894936397dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunctionDomainError(SNES snes)
904936397dSBarry Smith {
914936397dSBarry Smith   PetscFunctionBegin;
920700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
934936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
944936397dSBarry Smith   PetscFunctionReturn(0);
954936397dSBarry Smith }
964936397dSBarry Smith 
974936397dSBarry Smith #undef __FUNCT__
984a2ae208SSatish Balay #define __FUNCT__ "SNESView"
997e2c5f70SBarry Smith /*@C
1009b94acceSBarry Smith    SNESView - Prints the SNES data structure.
1019b94acceSBarry Smith 
1024c49b128SBarry Smith    Collective on SNES
103fee21e36SBarry Smith 
104c7afd0dbSLois Curfman McInnes    Input Parameters:
105c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
106c7afd0dbSLois Curfman McInnes -  viewer - visualization context
107c7afd0dbSLois Curfman McInnes 
1089b94acceSBarry Smith    Options Database Key:
109c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
1109b94acceSBarry Smith 
1119b94acceSBarry Smith    Notes:
1129b94acceSBarry Smith    The available visualization contexts include
113b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
114b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
115c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
116c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
117c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
1189b94acceSBarry Smith 
1193e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
120b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
1219b94acceSBarry Smith 
12236851e7fSLois Curfman McInnes    Level: beginner
12336851e7fSLois Curfman McInnes 
1249b94acceSBarry Smith .keywords: SNES, view
1259b94acceSBarry Smith 
126b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1279b94acceSBarry Smith @*/
12863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer)
1299b94acceSBarry Smith {
130fa9f3622SBarry Smith   SNESKSPEW           *kctx;
131dfbe8321SBarry Smith   PetscErrorCode      ierr;
13294b7f48cSBarry Smith   KSP                 ksp;
133a313700dSBarry Smith   const SNESType      type;
13432077d6dSBarry Smith   PetscTruth          iascii,isstring;
1359b94acceSBarry Smith 
1363a40ed3dSBarry Smith   PetscFunctionBegin;
1370700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1383050cee2SBarry Smith   if (!viewer) {
1397adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
1403050cee2SBarry Smith   }
1410700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
142c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
14374679c65SBarry Smith 
1442692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1452692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
14632077d6dSBarry Smith   if (iascii) {
1477adad957SLisandro Dalcin     if (((PetscObject)snes)->prefix) {
1487adad957SLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",((PetscObject)snes)->prefix);CHKERRQ(ierr);
1493a7fca6bSBarry Smith     } else {
150b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
1513a7fca6bSBarry Smith     }
152454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
153454a90a3SBarry Smith     if (type) {
154b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
155184914b5SBarry Smith     } else {
156b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
157184914b5SBarry Smith     }
158e7788613SBarry Smith     if (snes->ops->view) {
159b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
160e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
161b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1620ef38995SBarry Smith     }
16377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
164a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
16570441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
16677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
16777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1689b94acceSBarry Smith     if (snes->ksp_ewconv) {
169fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1709b94acceSBarry Smith       if (kctx) {
17177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
172a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
173a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1749b94acceSBarry Smith       }
1759b94acceSBarry Smith     }
176eb1f6c34SBarry Smith     if (snes->lagpreconditioner == -1) {
177eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is never rebuilt\n");CHKERRQ(ierr);
178eb1f6c34SBarry Smith     } else if (snes->lagpreconditioner > 1) {
179eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr);
180eb1f6c34SBarry Smith     }
181eb1f6c34SBarry Smith     if (snes->lagjacobian == -1) {
182eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is never rebuilt\n");CHKERRQ(ierr);
183eb1f6c34SBarry Smith     } else if (snes->lagjacobian > 1) {
184eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is rebuilt every %D Newton iterations\n",snes->lagjacobian);CHKERRQ(ierr);
185eb1f6c34SBarry Smith     }
1860f5bd95cSBarry Smith   } else if (isstring) {
187454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
188b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
18919bcc07fSBarry Smith   }
19094b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
191b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
19294b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
193b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1943a40ed3dSBarry Smith   PetscFunctionReturn(0);
1959b94acceSBarry Smith }
1969b94acceSBarry Smith 
19776b2cf59SMatthew Knepley /*
19876b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
19976b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
20076b2cf59SMatthew Knepley */
20176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
202a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
2036849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
20476b2cf59SMatthew Knepley 
205e74ef692SMatthew Knepley #undef __FUNCT__
206e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
207ac226902SBarry Smith /*@C
20876b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
20976b2cf59SMatthew Knepley 
21076b2cf59SMatthew Knepley   Not Collective
21176b2cf59SMatthew Knepley 
21276b2cf59SMatthew Knepley   Input Parameter:
21376b2cf59SMatthew Knepley . snescheck - function that checks for options
21476b2cf59SMatthew Knepley 
21576b2cf59SMatthew Knepley   Level: developer
21676b2cf59SMatthew Knepley 
21776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
21876b2cf59SMatthew Knepley @*/
21963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
22076b2cf59SMatthew Knepley {
22176b2cf59SMatthew Knepley   PetscFunctionBegin;
22276b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
223e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
22476b2cf59SMatthew Knepley   }
22576b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
22676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
22776b2cf59SMatthew Knepley }
22876b2cf59SMatthew Knepley 
229aa3661deSLisandro Dalcin EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
230aa3661deSLisandro Dalcin 
231aa3661deSLisandro Dalcin #undef __FUNCT__
232aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private"
233d3462f78SMatthew Knepley static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscTruth hasOperator, PetscInt version)
234aa3661deSLisandro Dalcin {
235aa3661deSLisandro Dalcin   Mat            J;
236aa3661deSLisandro Dalcin   KSP            ksp;
237aa3661deSLisandro Dalcin   PC             pc;
238aa3661deSLisandro Dalcin   PetscTruth     match;
239aa3661deSLisandro Dalcin   PetscErrorCode ierr;
240aa3661deSLisandro Dalcin 
241aa3661deSLisandro Dalcin   PetscFunctionBegin;
2420700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
243aa3661deSLisandro Dalcin 
24498613b67SLisandro Dalcin   if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
24598613b67SLisandro Dalcin     Mat A = snes->jacobian, B = snes->jacobian_pre;
24698613b67SLisandro Dalcin     ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr);
24798613b67SLisandro Dalcin   }
24898613b67SLisandro Dalcin 
249aa3661deSLisandro Dalcin   if (version == 1) {
250aa3661deSLisandro Dalcin     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
25198613b67SLisandro Dalcin     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
252aa3661deSLisandro Dalcin     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
253aa3661deSLisandro Dalcin   } else if (version == 2) {
254e32f2f54SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
25565460251SBarry 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)
256aa3661deSLisandro Dalcin     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr);
257aa3661deSLisandro Dalcin #else
258e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
259aa3661deSLisandro Dalcin #endif
260aa3661deSLisandro Dalcin   } else {
261e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2");
262aa3661deSLisandro Dalcin   }
263aa3661deSLisandro Dalcin 
264aa3661deSLisandro Dalcin   ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr);
265d3462f78SMatthew Knepley   if (hasOperator) {
266aa3661deSLisandro Dalcin     /* This version replaces the user provided Jacobian matrix with a
267aa3661deSLisandro Dalcin        matrix-free version but still employs the user-provided preconditioner matrix. */
268aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
269aa3661deSLisandro Dalcin   } else {
270aa3661deSLisandro Dalcin     /* This version replaces both the user-provided Jacobian and the user-
271aa3661deSLisandro Dalcin        provided preconditioner matrix with the default matrix free version. */
272aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
273aa3661deSLisandro Dalcin     /* Force no preconditioner */
274aa3661deSLisandro Dalcin     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
275aa3661deSLisandro Dalcin     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
276aa3661deSLisandro Dalcin     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr);
277aa3661deSLisandro Dalcin     if (!match) {
278aa3661deSLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
279aa3661deSLisandro Dalcin       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
280aa3661deSLisandro Dalcin     }
281aa3661deSLisandro Dalcin   }
282aa3661deSLisandro Dalcin   ierr = MatDestroy(J);CHKERRQ(ierr);
283aa3661deSLisandro Dalcin 
284aa3661deSLisandro Dalcin   PetscFunctionReturn(0);
285aa3661deSLisandro Dalcin }
286aa3661deSLisandro Dalcin 
2874a2ae208SSatish Balay #undef __FUNCT__
2884a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
2899b94acceSBarry Smith /*@
29094b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
2919b94acceSBarry Smith 
292c7afd0dbSLois Curfman McInnes    Collective on SNES
293c7afd0dbSLois Curfman McInnes 
2949b94acceSBarry Smith    Input Parameter:
2959b94acceSBarry Smith .  snes - the SNES context
2969b94acceSBarry Smith 
29736851e7fSLois Curfman McInnes    Options Database Keys:
2986831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
29982738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
30082738288SBarry Smith                 of the change in the solution between steps
30170441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
302b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
303b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
304b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
30550ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
306ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
307a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
308e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
309b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
3102492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
31182738288SBarry Smith                                solver; hence iterations will continue until max_it
3121fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
31382738288SBarry Smith                                of convergence test
314e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
315e8105e01SRichard Katz                                        filename given prints to stdout
316a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
317a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
318a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
319a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
320e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
3215968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
322fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
32382738288SBarry Smith 
32482738288SBarry Smith     Options Database for Eisenstat-Walker method:
325fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
3264b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
32736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
32836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
32936851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
33036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
33136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
33236851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
33382738288SBarry Smith 
33411ca99fdSLois Curfman McInnes    Notes:
33511ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
33611ca99fdSLois Curfman McInnes    the users manual.
33783e2fdc7SBarry Smith 
33836851e7fSLois Curfman McInnes    Level: beginner
33936851e7fSLois Curfman McInnes 
3409b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
3419b94acceSBarry Smith 
34269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
3439b94acceSBarry Smith @*/
34463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
3459b94acceSBarry Smith {
346aa3661deSLisandro Dalcin   PetscTruth              flg,mf,mf_operator;
347aa3661deSLisandro Dalcin   PetscInt                i,indx,lag,mf_version;
348aa3661deSLisandro Dalcin   MatStructure            matflag;
34985385478SLisandro Dalcin   const char              *deft = SNESLS;
35085385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
35185385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
352e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
35323d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
35485385478SLisandro Dalcin   PetscErrorCode          ierr;
3559b94acceSBarry Smith 
3563a40ed3dSBarry Smith   PetscFunctionBegin;
3570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
358ca161407SBarry Smith 
359186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
360cce0b1b2SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
3617adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
362b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
363d64ed03dSBarry Smith     if (flg) {
364186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
3657adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
366186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
367d64ed03dSBarry Smith     }
36890d69ab7SBarry Smith     /* not used here, but called so will go into help messaage */
369909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
37093c39befSBarry Smith 
37157034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
37257034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
373186905e3SBarry Smith 
37457034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
375b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
376b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
37750ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
378ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
379*e113a28aSBarry Smith     ierr = PetscOptionsTruth("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr);
38085385478SLisandro Dalcin 
381a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
382a8054027SBarry Smith     if (flg) {
383a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
384a8054027SBarry Smith     }
385e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
386e35cf81dSBarry Smith     if (flg) {
387e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
388e35cf81dSBarry Smith     }
389a8054027SBarry Smith 
39085385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
39185385478SLisandro Dalcin     if (flg) {
39285385478SLisandro Dalcin       switch (indx) {
3937f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
3947f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
39585385478SLisandro Dalcin       }
39685385478SLisandro Dalcin     }
39785385478SLisandro Dalcin 
39890d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
399186905e3SBarry Smith 
40085385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
40185385478SLisandro Dalcin 
402fa9f3622SBarry Smith     ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
403186905e3SBarry Smith 
404fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
405fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
406fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
407fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
408fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
409fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
410fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
411186905e3SBarry Smith 
41290d69ab7SBarry Smith     flg  = PETSC_FALSE;
41390d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
414a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
415eabae89aSBarry Smith 
416a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
417e8105e01SRichard Katz     if (flg) {
418050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
41923d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
420e8105e01SRichard Katz     }
421eabae89aSBarry Smith 
422b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
423b271bb04SBarry Smith     if (flg) {
424b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
425b271bb04SBarry Smith     }
426b271bb04SBarry Smith 
427a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
428eabae89aSBarry Smith     if (flg) {
429050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
430f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
431e8105e01SRichard Katz     }
432eabae89aSBarry Smith 
433a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
434eabae89aSBarry Smith     if (flg) {
435050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
43623d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
437eabae89aSBarry Smith     }
438eabae89aSBarry Smith 
43990d69ab7SBarry Smith     flg  = PETSC_FALSE;
44090d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
441a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
44290d69ab7SBarry Smith     flg  = PETSC_FALSE;
44390d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
444a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
44590d69ab7SBarry Smith     flg  = PETSC_FALSE;
44690d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
447a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
44890d69ab7SBarry Smith     flg  = PETSC_FALSE;
44990d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
450a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
45190d69ab7SBarry Smith     flg  = PETSC_FALSE;
45290d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
453b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
454e24b481bSBarry Smith 
45590d69ab7SBarry Smith     flg  = PETSC_FALSE;
45690d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
4574b27c08aSLois Curfman McInnes     if (flg) {
458186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
459ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
4609b94acceSBarry Smith     }
461639f9d9dSBarry Smith 
462aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
463aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
464aa3661deSLisandro Dalcin     ierr = PetscOptionsTruth("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
465aa3661deSLisandro Dalcin     if (flg && mf_operator) mf = PETSC_TRUE;
466aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
467aa3661deSLisandro Dalcin     ierr = PetscOptionsTruth("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
468aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
469aa3661deSLisandro Dalcin     mf_version = 1;
470aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
471aa3661deSLisandro Dalcin 
47276b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
47376b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
47476b2cf59SMatthew Knepley     }
47576b2cf59SMatthew Knepley 
476e7788613SBarry Smith     if (snes->ops->setfromoptions) {
477e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
478639f9d9dSBarry Smith     }
479b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4804bbc92c1SBarry Smith 
481aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
4821cee3971SBarry Smith 
4831cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
484aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
485aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
48685385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
48793993e2dSLois Curfman McInnes 
4883a40ed3dSBarry Smith   PetscFunctionReturn(0);
4899b94acceSBarry Smith }
4909b94acceSBarry Smith 
491a847f771SSatish Balay 
4924a2ae208SSatish Balay #undef __FUNCT__
4934a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
4949b94acceSBarry Smith /*@
4959b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
4969b94acceSBarry Smith    the nonlinear solvers.
4979b94acceSBarry Smith 
498fee21e36SBarry Smith    Collective on SNES
499fee21e36SBarry Smith 
500c7afd0dbSLois Curfman McInnes    Input Parameters:
501c7afd0dbSLois Curfman McInnes +  snes - the SNES context
502c7afd0dbSLois Curfman McInnes -  usrP - optional user context
503c7afd0dbSLois Curfman McInnes 
50436851e7fSLois Curfman McInnes    Level: intermediate
50536851e7fSLois Curfman McInnes 
5069b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
5079b94acceSBarry Smith 
5089b94acceSBarry Smith .seealso: SNESGetApplicationContext()
5099b94acceSBarry Smith @*/
51063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
5119b94acceSBarry Smith {
5123a40ed3dSBarry Smith   PetscFunctionBegin;
5130700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5149b94acceSBarry Smith   snes->user		= usrP;
5153a40ed3dSBarry Smith   PetscFunctionReturn(0);
5169b94acceSBarry Smith }
51774679c65SBarry Smith 
5184a2ae208SSatish Balay #undef __FUNCT__
5194a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
5209b94acceSBarry Smith /*@C
5219b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
5229b94acceSBarry Smith    nonlinear solvers.
5239b94acceSBarry Smith 
524c7afd0dbSLois Curfman McInnes    Not Collective
525c7afd0dbSLois Curfman McInnes 
5269b94acceSBarry Smith    Input Parameter:
5279b94acceSBarry Smith .  snes - SNES context
5289b94acceSBarry Smith 
5299b94acceSBarry Smith    Output Parameter:
5309b94acceSBarry Smith .  usrP - user context
5319b94acceSBarry Smith 
53236851e7fSLois Curfman McInnes    Level: intermediate
53336851e7fSLois Curfman McInnes 
5349b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
5359b94acceSBarry Smith 
5369b94acceSBarry Smith .seealso: SNESSetApplicationContext()
5379b94acceSBarry Smith @*/
53863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
5399b94acceSBarry Smith {
5403a40ed3dSBarry Smith   PetscFunctionBegin;
5410700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5429b94acceSBarry Smith   *usrP = snes->user;
5433a40ed3dSBarry Smith   PetscFunctionReturn(0);
5449b94acceSBarry Smith }
54574679c65SBarry Smith 
5464a2ae208SSatish Balay #undef __FUNCT__
5474a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
5489b94acceSBarry Smith /*@
549c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
550c8228a4eSBarry Smith    at this time.
5519b94acceSBarry Smith 
552c7afd0dbSLois Curfman McInnes    Not Collective
553c7afd0dbSLois Curfman McInnes 
5549b94acceSBarry Smith    Input Parameter:
5559b94acceSBarry Smith .  snes - SNES context
5569b94acceSBarry Smith 
5579b94acceSBarry Smith    Output Parameter:
5589b94acceSBarry Smith .  iter - iteration number
5599b94acceSBarry Smith 
560c8228a4eSBarry Smith    Notes:
561c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
562c8228a4eSBarry Smith 
563c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
56408405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
56508405cd6SLois Curfman McInnes .vb
56608405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
56708405cd6SLois Curfman McInnes       if (!(it % 2)) {
56808405cd6SLois Curfman McInnes         [compute Jacobian here]
56908405cd6SLois Curfman McInnes       }
57008405cd6SLois Curfman McInnes .ve
571c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
57208405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
573c8228a4eSBarry Smith 
57436851e7fSLois Curfman McInnes    Level: intermediate
57536851e7fSLois Curfman McInnes 
5762b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
5772b668275SBarry Smith 
578b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
5799b94acceSBarry Smith @*/
58063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
5819b94acceSBarry Smith {
5823a40ed3dSBarry Smith   PetscFunctionBegin;
5830700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5844482741eSBarry Smith   PetscValidIntPointer(iter,2);
5859b94acceSBarry Smith   *iter = snes->iter;
5863a40ed3dSBarry Smith   PetscFunctionReturn(0);
5879b94acceSBarry Smith }
58874679c65SBarry Smith 
5894a2ae208SSatish Balay #undef __FUNCT__
5904a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
5919b94acceSBarry Smith /*@
5929b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
5939b94acceSBarry Smith    with SNESSSetFunction().
5949b94acceSBarry Smith 
595c7afd0dbSLois Curfman McInnes    Collective on SNES
596c7afd0dbSLois Curfman McInnes 
5979b94acceSBarry Smith    Input Parameter:
5989b94acceSBarry Smith .  snes - SNES context
5999b94acceSBarry Smith 
6009b94acceSBarry Smith    Output Parameter:
6019b94acceSBarry Smith .  fnorm - 2-norm of function
6029b94acceSBarry Smith 
60336851e7fSLois Curfman McInnes    Level: intermediate
60436851e7fSLois Curfman McInnes 
6059b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
606a86d99e1SLois Curfman McInnes 
607b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
6089b94acceSBarry Smith @*/
60971f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
6109b94acceSBarry Smith {
6113a40ed3dSBarry Smith   PetscFunctionBegin;
6120700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6134482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
6149b94acceSBarry Smith   *fnorm = snes->norm;
6153a40ed3dSBarry Smith   PetscFunctionReturn(0);
6169b94acceSBarry Smith }
61774679c65SBarry Smith 
6184a2ae208SSatish Balay #undef __FUNCT__
619b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
6209b94acceSBarry Smith /*@
621b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
6229b94acceSBarry Smith    attempted by the nonlinear solver.
6239b94acceSBarry Smith 
624c7afd0dbSLois Curfman McInnes    Not Collective
625c7afd0dbSLois Curfman McInnes 
6269b94acceSBarry Smith    Input Parameter:
6279b94acceSBarry Smith .  snes - SNES context
6289b94acceSBarry Smith 
6299b94acceSBarry Smith    Output Parameter:
6309b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
6319b94acceSBarry Smith 
632c96a6f78SLois Curfman McInnes    Notes:
633c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
634c96a6f78SLois Curfman McInnes 
63536851e7fSLois Curfman McInnes    Level: intermediate
63636851e7fSLois Curfman McInnes 
6379b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
63858ebbce7SBarry Smith 
639e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
64058ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
6419b94acceSBarry Smith @*/
642b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
6439b94acceSBarry Smith {
6443a40ed3dSBarry Smith   PetscFunctionBegin;
6450700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6464482741eSBarry Smith   PetscValidIntPointer(nfails,2);
64750ffb88aSMatthew Knepley   *nfails = snes->numFailures;
64850ffb88aSMatthew Knepley   PetscFunctionReturn(0);
64950ffb88aSMatthew Knepley }
65050ffb88aSMatthew Knepley 
65150ffb88aSMatthew Knepley #undef __FUNCT__
652b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
65350ffb88aSMatthew Knepley /*@
654b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
65550ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
65650ffb88aSMatthew Knepley 
65750ffb88aSMatthew Knepley    Not Collective
65850ffb88aSMatthew Knepley 
65950ffb88aSMatthew Knepley    Input Parameters:
66050ffb88aSMatthew Knepley +  snes     - SNES context
66150ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
66250ffb88aSMatthew Knepley 
66350ffb88aSMatthew Knepley    Level: intermediate
66450ffb88aSMatthew Knepley 
66550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
66658ebbce7SBarry Smith 
667e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
66858ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
66950ffb88aSMatthew Knepley @*/
670b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
67150ffb88aSMatthew Knepley {
67250ffb88aSMatthew Knepley   PetscFunctionBegin;
6730700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
67450ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
67550ffb88aSMatthew Knepley   PetscFunctionReturn(0);
67650ffb88aSMatthew Knepley }
67750ffb88aSMatthew Knepley 
67850ffb88aSMatthew Knepley #undef __FUNCT__
679b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
68050ffb88aSMatthew Knepley /*@
681b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
68250ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
68350ffb88aSMatthew Knepley 
68450ffb88aSMatthew Knepley    Not Collective
68550ffb88aSMatthew Knepley 
68650ffb88aSMatthew Knepley    Input Parameter:
68750ffb88aSMatthew Knepley .  snes     - SNES context
68850ffb88aSMatthew Knepley 
68950ffb88aSMatthew Knepley    Output Parameter:
69050ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
69150ffb88aSMatthew Knepley 
69250ffb88aSMatthew Knepley    Level: intermediate
69350ffb88aSMatthew Knepley 
69450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
69558ebbce7SBarry Smith 
696e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
69758ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
69858ebbce7SBarry Smith 
69950ffb88aSMatthew Knepley @*/
700b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
70150ffb88aSMatthew Knepley {
70250ffb88aSMatthew Knepley   PetscFunctionBegin;
7030700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7044482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
70550ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
7063a40ed3dSBarry Smith   PetscFunctionReturn(0);
7079b94acceSBarry Smith }
708a847f771SSatish Balay 
7094a2ae208SSatish Balay #undef __FUNCT__
7102541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
7112541af92SBarry Smith /*@
7122541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
7132541af92SBarry Smith      done by SNES.
7142541af92SBarry Smith 
7152541af92SBarry Smith    Not Collective
7162541af92SBarry Smith 
7172541af92SBarry Smith    Input Parameter:
7182541af92SBarry Smith .  snes     - SNES context
7192541af92SBarry Smith 
7202541af92SBarry Smith    Output Parameter:
7212541af92SBarry Smith .  nfuncs - number of evaluations
7222541af92SBarry Smith 
7232541af92SBarry Smith    Level: intermediate
7242541af92SBarry Smith 
7252541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
72658ebbce7SBarry Smith 
727e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
7282541af92SBarry Smith @*/
7292541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
7302541af92SBarry Smith {
7312541af92SBarry Smith   PetscFunctionBegin;
7320700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7332541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
7342541af92SBarry Smith   *nfuncs = snes->nfuncs;
7352541af92SBarry Smith   PetscFunctionReturn(0);
7362541af92SBarry Smith }
7372541af92SBarry Smith 
7382541af92SBarry Smith #undef __FUNCT__
7393d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
7403d4c4710SBarry Smith /*@
7413d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
7423d4c4710SBarry Smith    linear solvers.
7433d4c4710SBarry Smith 
7443d4c4710SBarry Smith    Not Collective
7453d4c4710SBarry Smith 
7463d4c4710SBarry Smith    Input Parameter:
7473d4c4710SBarry Smith .  snes - SNES context
7483d4c4710SBarry Smith 
7493d4c4710SBarry Smith    Output Parameter:
7503d4c4710SBarry Smith .  nfails - number of failed solves
7513d4c4710SBarry Smith 
7523d4c4710SBarry Smith    Notes:
7533d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
7543d4c4710SBarry Smith 
7553d4c4710SBarry Smith    Level: intermediate
7563d4c4710SBarry Smith 
7573d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
75858ebbce7SBarry Smith 
759e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
7603d4c4710SBarry Smith @*/
7613d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
7623d4c4710SBarry Smith {
7633d4c4710SBarry Smith   PetscFunctionBegin;
7640700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7653d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
7663d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
7673d4c4710SBarry Smith   PetscFunctionReturn(0);
7683d4c4710SBarry Smith }
7693d4c4710SBarry Smith 
7703d4c4710SBarry Smith #undef __FUNCT__
7713d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
7723d4c4710SBarry Smith /*@
7733d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
7743d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
7753d4c4710SBarry Smith 
7763d4c4710SBarry Smith    Collective on SNES
7773d4c4710SBarry Smith 
7783d4c4710SBarry Smith    Input Parameters:
7793d4c4710SBarry Smith +  snes     - SNES context
7803d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
7813d4c4710SBarry Smith 
7823d4c4710SBarry Smith    Level: intermediate
7833d4c4710SBarry Smith 
784a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
7853d4c4710SBarry Smith 
7863d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
7873d4c4710SBarry Smith 
78858ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
7893d4c4710SBarry Smith @*/
7903d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
7913d4c4710SBarry Smith {
7923d4c4710SBarry Smith   PetscFunctionBegin;
7930700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7943d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
7953d4c4710SBarry Smith   PetscFunctionReturn(0);
7963d4c4710SBarry Smith }
7973d4c4710SBarry Smith 
7983d4c4710SBarry Smith #undef __FUNCT__
7993d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
8003d4c4710SBarry Smith /*@
8013d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
8023d4c4710SBarry Smith      are allowed before SNES terminates
8033d4c4710SBarry Smith 
8043d4c4710SBarry Smith    Not Collective
8053d4c4710SBarry Smith 
8063d4c4710SBarry Smith    Input Parameter:
8073d4c4710SBarry Smith .  snes     - SNES context
8083d4c4710SBarry Smith 
8093d4c4710SBarry Smith    Output Parameter:
8103d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
8113d4c4710SBarry Smith 
8123d4c4710SBarry Smith    Level: intermediate
8133d4c4710SBarry Smith 
8143d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
8153d4c4710SBarry Smith 
8163d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
8173d4c4710SBarry Smith 
818e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
8193d4c4710SBarry Smith @*/
8203d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
8213d4c4710SBarry Smith {
8223d4c4710SBarry Smith   PetscFunctionBegin;
8230700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8243d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
8253d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
8263d4c4710SBarry Smith   PetscFunctionReturn(0);
8273d4c4710SBarry Smith }
8283d4c4710SBarry Smith 
8293d4c4710SBarry Smith #undef __FUNCT__
830b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
831c96a6f78SLois Curfman McInnes /*@
832b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
833c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
834c96a6f78SLois Curfman McInnes 
835c7afd0dbSLois Curfman McInnes    Not Collective
836c7afd0dbSLois Curfman McInnes 
837c96a6f78SLois Curfman McInnes    Input Parameter:
838c96a6f78SLois Curfman McInnes .  snes - SNES context
839c96a6f78SLois Curfman McInnes 
840c96a6f78SLois Curfman McInnes    Output Parameter:
841c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
842c96a6f78SLois Curfman McInnes 
843c96a6f78SLois Curfman McInnes    Notes:
844c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
845c96a6f78SLois Curfman McInnes 
84636851e7fSLois Curfman McInnes    Level: intermediate
84736851e7fSLois Curfman McInnes 
848c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
8492b668275SBarry Smith 
8508c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
851c96a6f78SLois Curfman McInnes @*/
852b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
853c96a6f78SLois Curfman McInnes {
8543a40ed3dSBarry Smith   PetscFunctionBegin;
8550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8564482741eSBarry Smith   PetscValidIntPointer(lits,2);
857c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
8583a40ed3dSBarry Smith   PetscFunctionReturn(0);
859c96a6f78SLois Curfman McInnes }
860c96a6f78SLois Curfman McInnes 
8614a2ae208SSatish Balay #undef __FUNCT__
86294b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
86352baeb72SSatish Balay /*@
86494b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
8659b94acceSBarry Smith 
86694b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
867c7afd0dbSLois Curfman McInnes 
8689b94acceSBarry Smith    Input Parameter:
8699b94acceSBarry Smith .  snes - the SNES context
8709b94acceSBarry Smith 
8719b94acceSBarry Smith    Output Parameter:
87294b7f48cSBarry Smith .  ksp - the KSP context
8739b94acceSBarry Smith 
8749b94acceSBarry Smith    Notes:
87594b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
8769b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
8772999313aSBarry Smith    PC contexts as well.
8789b94acceSBarry Smith 
87936851e7fSLois Curfman McInnes    Level: beginner
88036851e7fSLois Curfman McInnes 
88194b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
8829b94acceSBarry Smith 
8832999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
8849b94acceSBarry Smith @*/
88563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
8869b94acceSBarry Smith {
8871cee3971SBarry Smith   PetscErrorCode ierr;
8881cee3971SBarry Smith 
8893a40ed3dSBarry Smith   PetscFunctionBegin;
8900700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8914482741eSBarry Smith   PetscValidPointer(ksp,2);
8921cee3971SBarry Smith 
8931cee3971SBarry Smith   if (!snes->ksp) {
8941cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
8951cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
8961cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
8971cee3971SBarry Smith   }
89894b7f48cSBarry Smith   *ksp = snes->ksp;
8993a40ed3dSBarry Smith   PetscFunctionReturn(0);
9009b94acceSBarry Smith }
90182bf6240SBarry Smith 
9024a2ae208SSatish Balay #undef __FUNCT__
9032999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
9042999313aSBarry Smith /*@
9052999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
9062999313aSBarry Smith 
9072999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
9082999313aSBarry Smith 
9092999313aSBarry Smith    Input Parameters:
9102999313aSBarry Smith +  snes - the SNES context
9112999313aSBarry Smith -  ksp - the KSP context
9122999313aSBarry Smith 
9132999313aSBarry Smith    Notes:
9142999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
9152999313aSBarry Smith    so this routine is rarely needed.
9162999313aSBarry Smith 
9172999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
9182999313aSBarry Smith    decreased by one.
9192999313aSBarry Smith 
9202999313aSBarry Smith    Level: developer
9212999313aSBarry Smith 
9222999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
9232999313aSBarry Smith 
9242999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
9252999313aSBarry Smith @*/
9262999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
9272999313aSBarry Smith {
9282999313aSBarry Smith   PetscErrorCode ierr;
9292999313aSBarry Smith 
9302999313aSBarry Smith   PetscFunctionBegin;
9310700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
9320700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
9332999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
9347dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
935906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
9362999313aSBarry Smith   snes->ksp = ksp;
9372999313aSBarry Smith   PetscFunctionReturn(0);
9382999313aSBarry Smith }
9392999313aSBarry Smith 
9407adad957SLisandro Dalcin #if 0
9412999313aSBarry Smith #undef __FUNCT__
9424a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
9436849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
944e24b481bSBarry Smith {
945e24b481bSBarry Smith   PetscFunctionBegin;
946e24b481bSBarry Smith   PetscFunctionReturn(0);
947e24b481bSBarry Smith }
9487adad957SLisandro Dalcin #endif
949e24b481bSBarry Smith 
9509b94acceSBarry Smith /* -----------------------------------------------------------*/
9514a2ae208SSatish Balay #undef __FUNCT__
9524a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
95352baeb72SSatish Balay /*@
9549b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
9559b94acceSBarry Smith 
956c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
957c7afd0dbSLois Curfman McInnes 
958c7afd0dbSLois Curfman McInnes    Input Parameters:
959906ed7ccSBarry Smith .  comm - MPI communicator
9609b94acceSBarry Smith 
9619b94acceSBarry Smith    Output Parameter:
9629b94acceSBarry Smith .  outsnes - the new SNES context
9639b94acceSBarry Smith 
964c7afd0dbSLois Curfman McInnes    Options Database Keys:
965c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
966c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
967c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
968c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
969c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
970c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
971c1f60f51SBarry Smith 
97236851e7fSLois Curfman McInnes    Level: beginner
97336851e7fSLois Curfman McInnes 
9749b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
9759b94acceSBarry Smith 
976a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
977a8054027SBarry Smith 
9789b94acceSBarry Smith @*/
97963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
9809b94acceSBarry Smith {
981dfbe8321SBarry Smith   PetscErrorCode      ierr;
9829b94acceSBarry Smith   SNES                snes;
983fa9f3622SBarry Smith   SNESKSPEW           *kctx;
98437fcc0dbSBarry Smith 
9853a40ed3dSBarry Smith   PetscFunctionBegin;
986ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
9878ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
9888ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
9898ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
9908ba1e511SMatthew Knepley #endif
9918ba1e511SMatthew Knepley 
9920700a824SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
9937adad957SLisandro Dalcin 
99485385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
9959b94acceSBarry Smith   snes->max_its           = 50;
9969750a799SBarry Smith   snes->max_funcs	  = 10000;
9979b94acceSBarry Smith   snes->norm		  = 0.0;
998b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
999b4874afaSBarry Smith   snes->ttol              = 0.0;
100070441072SBarry Smith   snes->abstol		  = 1.e-50;
10019b94acceSBarry Smith   snes->xtol		  = 1.e-8;
10024b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
10039b94acceSBarry Smith   snes->nfuncs            = 0;
100450ffb88aSMatthew Knepley   snes->numFailures       = 0;
100550ffb88aSMatthew Knepley   snes->maxFailures       = 1;
10067a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
1007e35cf81dSBarry Smith   snes->lagjacobian       = 1;
1008a8054027SBarry Smith   snes->lagpreconditioner = 1;
1009639f9d9dSBarry Smith   snes->numbermonitors    = 0;
10109b94acceSBarry Smith   snes->data              = 0;
10114dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
1012186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
10136f24a144SLois Curfman McInnes   snes->vwork             = 0;
10146f24a144SLois Curfman McInnes   snes->nwork             = 0;
1015758f92a0SBarry Smith   snes->conv_hist_len     = 0;
1016758f92a0SBarry Smith   snes->conv_hist_max     = 0;
1017758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
1018758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
1019758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
1020184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
10219b94acceSBarry Smith 
10223d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
10233d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
10243d4c4710SBarry Smith 
10259b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
102638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
10279b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
10289b94acceSBarry Smith   kctx->version     = 2;
10299b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
10309b94acceSBarry Smith                              this was too large for some test cases */
103175567043SBarry Smith   kctx->rtol_last   = 0.0;
10329b94acceSBarry Smith   kctx->rtol_max    = .9;
10339b94acceSBarry Smith   kctx->gamma       = 1.0;
103471f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
103571f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
10369b94acceSBarry Smith   kctx->threshold   = .1;
103775567043SBarry Smith   kctx->lresid_last = 0.0;
103875567043SBarry Smith   kctx->norm_last   = 0.0;
10399b94acceSBarry Smith 
10409b94acceSBarry Smith   *outsnes = snes;
104100036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
10423a40ed3dSBarry Smith   PetscFunctionReturn(0);
10439b94acceSBarry Smith }
10449b94acceSBarry Smith 
10454a2ae208SSatish Balay #undef __FUNCT__
10464a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
10479b94acceSBarry Smith /*@C
10489b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
10499b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
10509b94acceSBarry Smith    equations.
10519b94acceSBarry Smith 
1052fee21e36SBarry Smith    Collective on SNES
1053fee21e36SBarry Smith 
1054c7afd0dbSLois Curfman McInnes    Input Parameters:
1055c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1056c7afd0dbSLois Curfman McInnes .  r - vector to store function value
1057de044059SHong Zhang .  func - function evaluation routine
1058c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1059c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
10609b94acceSBarry Smith 
1061c7afd0dbSLois Curfman McInnes    Calling sequence of func:
10628d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1063c7afd0dbSLois Curfman McInnes 
1064313e4042SLois Curfman McInnes .  f - function vector
1065c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
10669b94acceSBarry Smith 
10679b94acceSBarry Smith    Notes:
10689b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
10699b94acceSBarry Smith $      f'(x) x = -f(x),
1070c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
10719b94acceSBarry Smith 
107236851e7fSLois Curfman McInnes    Level: beginner
107336851e7fSLois Curfman McInnes 
10749b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
10759b94acceSBarry Smith 
1076a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
10779b94acceSBarry Smith @*/
107863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
10799b94acceSBarry Smith {
108085385478SLisandro Dalcin   PetscErrorCode ierr;
10813a40ed3dSBarry Smith   PetscFunctionBegin;
10820700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1083ef8dffc7SBarry Smith   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1084ef8dffc7SBarry Smith   if (r) PetscCheckSameComm(snes,1,r,2);
1085ef8dffc7SBarry Smith   if (!r && snes->dm) {
1086ef8dffc7SBarry Smith     ierr = DMCreateGlobalVector(snes->dm,&r);CHKERRQ(ierr);
1087ef8dffc7SBarry Smith   } else {
108885385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
1089ef8dffc7SBarry Smith   }
109085385478SLisandro Dalcin   if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); }
1091e7788613SBarry Smith   snes->ops->computefunction = func;
109285385478SLisandro Dalcin   snes->vec_func             = r;
10939b94acceSBarry Smith   snes->funP                 = ctx;
10943a40ed3dSBarry Smith   PetscFunctionReturn(0);
10959b94acceSBarry Smith }
10969b94acceSBarry Smith 
10973ab0aad5SBarry Smith /* --------------------------------------------------------------- */
10983ab0aad5SBarry Smith #undef __FUNCT__
10991096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
11001096aae1SMatthew Knepley /*@C
11011096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
11021096aae1SMatthew Knepley    it assumes a zero right hand side.
11031096aae1SMatthew Knepley 
11041096aae1SMatthew Knepley    Collective on SNES
11051096aae1SMatthew Knepley 
11061096aae1SMatthew Knepley    Input Parameter:
11071096aae1SMatthew Knepley .  snes - the SNES context
11081096aae1SMatthew Knepley 
11091096aae1SMatthew Knepley    Output Parameter:
1110bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
11111096aae1SMatthew Knepley 
11121096aae1SMatthew Knepley    Level: intermediate
11131096aae1SMatthew Knepley 
11141096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
11151096aae1SMatthew Knepley 
111685385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
11171096aae1SMatthew Knepley @*/
11181096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
11191096aae1SMatthew Knepley {
11201096aae1SMatthew Knepley   PetscFunctionBegin;
11210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11221096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
112385385478SLisandro Dalcin   *rhs = snes->vec_rhs;
11241096aae1SMatthew Knepley   PetscFunctionReturn(0);
11251096aae1SMatthew Knepley }
11261096aae1SMatthew Knepley 
11271096aae1SMatthew Knepley #undef __FUNCT__
11284a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
11299b94acceSBarry Smith /*@
113036851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
11319b94acceSBarry Smith                          SNESSetFunction().
11329b94acceSBarry Smith 
1133c7afd0dbSLois Curfman McInnes    Collective on SNES
1134c7afd0dbSLois Curfman McInnes 
11359b94acceSBarry Smith    Input Parameters:
1136c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1137c7afd0dbSLois Curfman McInnes -  x - input vector
11389b94acceSBarry Smith 
11399b94acceSBarry Smith    Output Parameter:
11403638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
11419b94acceSBarry Smith 
11421bffabb2SLois Curfman McInnes    Notes:
114336851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
114436851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
114536851e7fSLois Curfman McInnes    themselves.
114636851e7fSLois Curfman McInnes 
114736851e7fSLois Curfman McInnes    Level: developer
114836851e7fSLois Curfman McInnes 
11499b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
11509b94acceSBarry Smith 
1151a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
11529b94acceSBarry Smith @*/
115363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
11549b94acceSBarry Smith {
1155dfbe8321SBarry Smith   PetscErrorCode ierr;
11569b94acceSBarry Smith 
11573a40ed3dSBarry Smith   PetscFunctionBegin;
11580700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11590700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
11600700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1161c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1162c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1163184914b5SBarry Smith 
1164d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1165e7788613SBarry Smith   if (snes->ops->computefunction) {
1166d64ed03dSBarry Smith     PetscStackPush("SNES user function");
1167e9a2bbcdSBarry Smith     CHKMEMQ;
1168e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
1169e9a2bbcdSBarry Smith     CHKMEMQ;
1170d64ed03dSBarry Smith     PetscStackPop;
1171d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
117219717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
117319717074SBarry Smith     }
1174d5e45103SBarry Smith     CHKERRQ(ierr);
117585385478SLisandro Dalcin   } else if (snes->vec_rhs) {
11761096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
117717186662SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
117885385478SLisandro Dalcin   if (snes->vec_rhs) {
117985385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
11803ab0aad5SBarry Smith   }
1181ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1182d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
11833a40ed3dSBarry Smith   PetscFunctionReturn(0);
11849b94acceSBarry Smith }
11859b94acceSBarry Smith 
11864a2ae208SSatish Balay #undef __FUNCT__
11874a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
118862fef451SLois Curfman McInnes /*@
118962fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
119062fef451SLois Curfman McInnes    set with SNESSetJacobian().
119162fef451SLois Curfman McInnes 
1192c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1193c7afd0dbSLois Curfman McInnes 
119462fef451SLois Curfman McInnes    Input Parameters:
1195c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1196c7afd0dbSLois Curfman McInnes -  x - input vector
119762fef451SLois Curfman McInnes 
119862fef451SLois Curfman McInnes    Output Parameters:
1199c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
120062fef451SLois Curfman McInnes .  B - optional preconditioning matrix
12012b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1202fee21e36SBarry Smith 
1203e35cf81dSBarry Smith   Options Database Keys:
1204e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1205e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1206e35cf81dSBarry Smith 
120762fef451SLois Curfman McInnes    Notes:
120862fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
120962fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
121062fef451SLois Curfman McInnes 
121194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1212dc5a77f8SLois Curfman McInnes    flag parameter.
121362fef451SLois Curfman McInnes 
121436851e7fSLois Curfman McInnes    Level: developer
121536851e7fSLois Curfman McInnes 
121662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
121762fef451SLois Curfman McInnes 
1218e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
121962fef451SLois Curfman McInnes @*/
122063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
12219b94acceSBarry Smith {
1222dfbe8321SBarry Smith   PetscErrorCode ierr;
1223ebd3b9afSBarry Smith   PetscTruth     flag;
12243a40ed3dSBarry Smith 
12253a40ed3dSBarry Smith   PetscFunctionBegin;
12260700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12270700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
12284482741eSBarry Smith   PetscValidPointer(flg,5);
1229c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1230e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1231ebd3b9afSBarry Smith 
1232ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1233ebd3b9afSBarry Smith 
1234fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1235fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1236fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1237fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1238e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1239e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1240ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1241ebd3b9afSBarry Smith     if (flag) {
1242ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1243ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1244ebd3b9afSBarry Smith     }
1245e35cf81dSBarry Smith     PetscFunctionReturn(0);
1246e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1247e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1248e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1249ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1250ebd3b9afSBarry Smith     if (flag) {
1251ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1252ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1253ebd3b9afSBarry Smith     }
1254e35cf81dSBarry Smith     PetscFunctionReturn(0);
1255e35cf81dSBarry Smith   }
1256e35cf81dSBarry Smith 
1257c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1258e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1259d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1260dc67937bSBarry Smith   CHKMEMQ;
1261e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1262dc67937bSBarry Smith   CHKMEMQ;
1263d64ed03dSBarry Smith   PetscStackPop;
1264d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1265a8054027SBarry Smith 
12663b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
12673b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
12683b4f5425SBarry Smith     snes->lagpreconditioner = -1;
12693b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1270a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1271a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1272a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1273a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1274a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1275a8054027SBarry Smith   }
1276a8054027SBarry Smith 
12776d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
12780700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
12790700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
12803a40ed3dSBarry Smith   PetscFunctionReturn(0);
12819b94acceSBarry Smith }
12829b94acceSBarry Smith 
12834a2ae208SSatish Balay #undef __FUNCT__
12844a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
12859b94acceSBarry Smith /*@C
12869b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1287044dda88SLois Curfman McInnes    location to store the matrix.
12889b94acceSBarry Smith 
1289c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1290c7afd0dbSLois Curfman McInnes 
12919b94acceSBarry Smith    Input Parameters:
1292c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12939b94acceSBarry Smith .  A - Jacobian matrix
12949b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
12959b94acceSBarry Smith .  func - Jacobian evaluation routine
1296c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
12972cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
12989b94acceSBarry Smith 
12999b94acceSBarry Smith    Calling sequence of func:
13008d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
13019b94acceSBarry Smith 
1302c7afd0dbSLois Curfman McInnes +  x - input vector
13039b94acceSBarry Smith .  A - Jacobian matrix
13049b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1305ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
13062b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1307c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
13089b94acceSBarry Smith 
13099b94acceSBarry Smith    Notes:
131094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
13112cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1312ac21db08SLois Curfman McInnes 
1313ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
13149b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
13159b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
13169b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
13179b94acceSBarry Smith    throughout the global iterations.
13189b94acceSBarry Smith 
131916913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
132016913363SBarry Smith    each matrix.
132116913363SBarry Smith 
132236851e7fSLois Curfman McInnes    Level: beginner
132336851e7fSLois Curfman McInnes 
13249b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
13259b94acceSBarry Smith 
13263ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
13279b94acceSBarry Smith @*/
132863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
13299b94acceSBarry Smith {
1330dfbe8321SBarry Smith   PetscErrorCode ierr;
13313a7fca6bSBarry Smith 
13323a40ed3dSBarry Smith   PetscFunctionBegin;
13330700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13340700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
13350700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1336c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
133706975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1338e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
13393a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
13403a7fca6bSBarry Smith   if (A) {
13417dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
13423a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13439b94acceSBarry Smith     snes->jacobian = A;
13443a7fca6bSBarry Smith   }
13453a7fca6bSBarry Smith   if (B) {
13467dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
13473a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13489b94acceSBarry Smith     snes->jacobian_pre = B;
13493a7fca6bSBarry Smith   }
13503a40ed3dSBarry Smith   PetscFunctionReturn(0);
13519b94acceSBarry Smith }
135262fef451SLois Curfman McInnes 
13534a2ae208SSatish Balay #undef __FUNCT__
13544a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1355c2aafc4cSSatish Balay /*@C
1356b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1357b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1358b4fd4287SBarry Smith 
1359c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1360c7afd0dbSLois Curfman McInnes 
1361b4fd4287SBarry Smith    Input Parameter:
1362b4fd4287SBarry Smith .  snes - the nonlinear solver context
1363b4fd4287SBarry Smith 
1364b4fd4287SBarry Smith    Output Parameters:
1365c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1366b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
136770e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
136870e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1369fee21e36SBarry Smith 
137036851e7fSLois Curfman McInnes    Level: advanced
137136851e7fSLois Curfman McInnes 
1372b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1373b4fd4287SBarry Smith @*/
137470e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1375b4fd4287SBarry Smith {
13763a40ed3dSBarry Smith   PetscFunctionBegin;
13770700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1378b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1379b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1380e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
138170e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
13823a40ed3dSBarry Smith   PetscFunctionReturn(0);
1383b4fd4287SBarry Smith }
1384b4fd4287SBarry Smith 
13859b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13869b94acceSBarry Smith 
13874a2ae208SSatish Balay #undef __FUNCT__
13884a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
13899b94acceSBarry Smith /*@
13909b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1391272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13929b94acceSBarry Smith 
1393fee21e36SBarry Smith    Collective on SNES
1394fee21e36SBarry Smith 
1395c7afd0dbSLois Curfman McInnes    Input Parameters:
139670e92668SMatthew Knepley .  snes - the SNES context
1397c7afd0dbSLois Curfman McInnes 
1398272ac6f2SLois Curfman McInnes    Notes:
1399272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1400272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1401272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1402272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1403272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1404272ac6f2SLois Curfman McInnes 
140536851e7fSLois Curfman McInnes    Level: advanced
140636851e7fSLois Curfman McInnes 
14079b94acceSBarry Smith .keywords: SNES, nonlinear, setup
14089b94acceSBarry Smith 
14099b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
14109b94acceSBarry Smith @*/
141170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
14129b94acceSBarry Smith {
1413dfbe8321SBarry Smith   PetscErrorCode ierr;
14143a40ed3dSBarry Smith 
14153a40ed3dSBarry Smith   PetscFunctionBegin;
14160700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14174dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
14189b94acceSBarry Smith 
14197adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
142085385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
142185385478SLisandro Dalcin   }
142285385478SLisandro Dalcin 
142317186662SBarry Smith   if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
142417186662SBarry Smith   if (!snes->ops->computefunction && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
142517186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1426ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1427ef8dffc7SBarry Smith     Mat           J;
1428ef8dffc7SBarry Smith     ISColoring    coloring;
1429ef8dffc7SBarry Smith     MatFDColoring fd;
1430a703fe33SLois Curfman McInnes 
1431ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1432ef8dffc7SBarry Smith     ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
1433ef8dffc7SBarry Smith     ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr);
1434ef8dffc7SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr);
1435ef8dffc7SBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr);
1436ef8dffc7SBarry Smith     ierr = ISColoringDestroy(coloring);CHKERRQ(ierr);
1437ef8dffc7SBarry Smith   }
1438b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1439b710008aSBarry Smith 
1440410397dcSLisandro Dalcin   if (snes->ops->setup) {
1441410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1442410397dcSLisandro Dalcin   }
14437aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
14443a40ed3dSBarry Smith   PetscFunctionReturn(0);
14459b94acceSBarry Smith }
14469b94acceSBarry Smith 
14474a2ae208SSatish Balay #undef __FUNCT__
14484a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
144952baeb72SSatish Balay /*@
14509b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
14519b94acceSBarry Smith    with SNESCreate().
14529b94acceSBarry Smith 
1453c7afd0dbSLois Curfman McInnes    Collective on SNES
1454c7afd0dbSLois Curfman McInnes 
14559b94acceSBarry Smith    Input Parameter:
14569b94acceSBarry Smith .  snes - the SNES context
14579b94acceSBarry Smith 
145836851e7fSLois Curfman McInnes    Level: beginner
145936851e7fSLois Curfman McInnes 
14609b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14619b94acceSBarry Smith 
146263a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14639b94acceSBarry Smith @*/
146463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
14659b94acceSBarry Smith {
14666849ba73SBarry Smith   PetscErrorCode ierr;
14673a40ed3dSBarry Smith 
14683a40ed3dSBarry Smith   PetscFunctionBegin;
14690700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14707adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1471d4bb536fSBarry Smith 
1472be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
14730f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
14746c699258SBarry Smith 
14756c699258SBarry Smith   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
1476e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
147785385478SLisandro Dalcin 
147885385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
147985385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
148085385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
14813a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
14823a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
14831cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
148485385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1485522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1486a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
14877f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1488a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
14893a40ed3dSBarry Smith   PetscFunctionReturn(0);
14909b94acceSBarry Smith }
14919b94acceSBarry Smith 
14929b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14939b94acceSBarry Smith 
14944a2ae208SSatish Balay #undef __FUNCT__
1495a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1496a8054027SBarry Smith /*@
1497a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1498a8054027SBarry Smith 
1499a8054027SBarry Smith    Collective on SNES
1500a8054027SBarry Smith 
1501a8054027SBarry Smith    Input Parameters:
1502a8054027SBarry Smith +  snes - the SNES context
1503a8054027SBarry 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
15043b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1505a8054027SBarry Smith 
1506a8054027SBarry Smith    Options Database Keys:
1507a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1508a8054027SBarry Smith 
1509a8054027SBarry Smith    Notes:
1510a8054027SBarry Smith    The default is 1
1511a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1512a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1513a8054027SBarry Smith 
1514a8054027SBarry Smith    Level: intermediate
1515a8054027SBarry Smith 
1516a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1517a8054027SBarry Smith 
1518e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1519a8054027SBarry Smith 
1520a8054027SBarry Smith @*/
1521a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1522a8054027SBarry Smith {
1523a8054027SBarry Smith   PetscFunctionBegin;
15240700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1525e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1526e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1527a8054027SBarry Smith   snes->lagpreconditioner = lag;
1528a8054027SBarry Smith   PetscFunctionReturn(0);
1529a8054027SBarry Smith }
1530a8054027SBarry Smith 
1531a8054027SBarry Smith #undef __FUNCT__
1532a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1533a8054027SBarry Smith /*@
1534a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1535a8054027SBarry Smith 
1536a8054027SBarry Smith    Collective on SNES
1537a8054027SBarry Smith 
1538a8054027SBarry Smith    Input Parameter:
1539a8054027SBarry Smith .  snes - the SNES context
1540a8054027SBarry Smith 
1541a8054027SBarry Smith    Output Parameter:
1542a8054027SBarry 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
15433b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1544a8054027SBarry Smith 
1545a8054027SBarry Smith    Options Database Keys:
1546a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1547a8054027SBarry Smith 
1548a8054027SBarry Smith    Notes:
1549a8054027SBarry Smith    The default is 1
1550a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1551a8054027SBarry Smith 
1552a8054027SBarry Smith    Level: intermediate
1553a8054027SBarry Smith 
1554a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1555a8054027SBarry Smith 
1556a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1557a8054027SBarry Smith 
1558a8054027SBarry Smith @*/
1559a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1560a8054027SBarry Smith {
1561a8054027SBarry Smith   PetscFunctionBegin;
15620700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1563a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1564a8054027SBarry Smith   PetscFunctionReturn(0);
1565a8054027SBarry Smith }
1566a8054027SBarry Smith 
1567a8054027SBarry Smith #undef __FUNCT__
1568e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1569e35cf81dSBarry Smith /*@
1570e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1571e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1572e35cf81dSBarry Smith 
1573e35cf81dSBarry Smith    Collective on SNES
1574e35cf81dSBarry Smith 
1575e35cf81dSBarry Smith    Input Parameters:
1576e35cf81dSBarry Smith +  snes - the SNES context
1577e35cf81dSBarry 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
1578fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1579e35cf81dSBarry Smith 
1580e35cf81dSBarry Smith    Options Database Keys:
1581e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1582e35cf81dSBarry Smith 
1583e35cf81dSBarry Smith    Notes:
1584e35cf81dSBarry Smith    The default is 1
1585e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1586fe3ffe1eSBarry 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
1587fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1588e35cf81dSBarry Smith 
1589e35cf81dSBarry Smith    Level: intermediate
1590e35cf81dSBarry Smith 
1591e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1592e35cf81dSBarry Smith 
1593e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1594e35cf81dSBarry Smith 
1595e35cf81dSBarry Smith @*/
1596e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1597e35cf81dSBarry Smith {
1598e35cf81dSBarry Smith   PetscFunctionBegin;
15990700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1600e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1601e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1602e35cf81dSBarry Smith   snes->lagjacobian = lag;
1603e35cf81dSBarry Smith   PetscFunctionReturn(0);
1604e35cf81dSBarry Smith }
1605e35cf81dSBarry Smith 
1606e35cf81dSBarry Smith #undef __FUNCT__
1607e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1608e35cf81dSBarry Smith /*@
1609e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1610e35cf81dSBarry Smith 
1611e35cf81dSBarry Smith    Collective on SNES
1612e35cf81dSBarry Smith 
1613e35cf81dSBarry Smith    Input Parameter:
1614e35cf81dSBarry Smith .  snes - the SNES context
1615e35cf81dSBarry Smith 
1616e35cf81dSBarry Smith    Output Parameter:
1617e35cf81dSBarry 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
1618e35cf81dSBarry Smith          the Jacobian is built etc.
1619e35cf81dSBarry Smith 
1620e35cf81dSBarry Smith    Options Database Keys:
1621e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1622e35cf81dSBarry Smith 
1623e35cf81dSBarry Smith    Notes:
1624e35cf81dSBarry Smith    The default is 1
1625e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1626e35cf81dSBarry Smith 
1627e35cf81dSBarry Smith    Level: intermediate
1628e35cf81dSBarry Smith 
1629e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1630e35cf81dSBarry Smith 
1631e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1632e35cf81dSBarry Smith 
1633e35cf81dSBarry Smith @*/
1634e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1635e35cf81dSBarry Smith {
1636e35cf81dSBarry Smith   PetscFunctionBegin;
16370700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1638e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1639e35cf81dSBarry Smith   PetscFunctionReturn(0);
1640e35cf81dSBarry Smith }
1641e35cf81dSBarry Smith 
1642e35cf81dSBarry Smith #undef __FUNCT__
16434a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
16449b94acceSBarry Smith /*@
1645d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
16469b94acceSBarry Smith 
1647c7afd0dbSLois Curfman McInnes    Collective on SNES
1648c7afd0dbSLois Curfman McInnes 
16499b94acceSBarry Smith    Input Parameters:
1650c7afd0dbSLois Curfman McInnes +  snes - the SNES context
165170441072SBarry Smith .  abstol - absolute convergence tolerance
165233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
165333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
165433174efeSLois Curfman McInnes            of the change in the solution between steps
165533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1656c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1657fee21e36SBarry Smith 
165833174efeSLois Curfman McInnes    Options Database Keys:
165970441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1660c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1661c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1662c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1663c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
16649b94acceSBarry Smith 
1665d7a720efSLois Curfman McInnes    Notes:
16669b94acceSBarry Smith    The default maximum number of iterations is 50.
16679b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
16689b94acceSBarry Smith 
166936851e7fSLois Curfman McInnes    Level: intermediate
167036851e7fSLois Curfman McInnes 
167133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
16729b94acceSBarry Smith 
16732492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
16749b94acceSBarry Smith @*/
167563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
16769b94acceSBarry Smith {
16773a40ed3dSBarry Smith   PetscFunctionBegin;
16780700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
167970441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1680d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1681d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1682d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1683d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
16843a40ed3dSBarry Smith   PetscFunctionReturn(0);
16859b94acceSBarry Smith }
16869b94acceSBarry Smith 
16874a2ae208SSatish Balay #undef __FUNCT__
16884a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
16899b94acceSBarry Smith /*@
169033174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
169133174efeSLois Curfman McInnes 
1692c7afd0dbSLois Curfman McInnes    Not Collective
1693c7afd0dbSLois Curfman McInnes 
169433174efeSLois Curfman McInnes    Input Parameters:
1695c7afd0dbSLois Curfman McInnes +  snes - the SNES context
169685385478SLisandro Dalcin .  atol - absolute convergence tolerance
169733174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
169833174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
169933174efeSLois Curfman McInnes            of the change in the solution between steps
170033174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1701c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1702fee21e36SBarry Smith 
170333174efeSLois Curfman McInnes    Notes:
170433174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
170533174efeSLois Curfman McInnes 
170636851e7fSLois Curfman McInnes    Level: intermediate
170736851e7fSLois Curfman McInnes 
170833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
170933174efeSLois Curfman McInnes 
171033174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
171133174efeSLois Curfman McInnes @*/
171285385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
171333174efeSLois Curfman McInnes {
17143a40ed3dSBarry Smith   PetscFunctionBegin;
17150700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
171685385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
171733174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
171833174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
171933174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
172033174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
17213a40ed3dSBarry Smith   PetscFunctionReturn(0);
172233174efeSLois Curfman McInnes }
172333174efeSLois Curfman McInnes 
17244a2ae208SSatish Balay #undef __FUNCT__
17254a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
172633174efeSLois Curfman McInnes /*@
17279b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
17289b94acceSBarry Smith 
1729fee21e36SBarry Smith    Collective on SNES
1730fee21e36SBarry Smith 
1731c7afd0dbSLois Curfman McInnes    Input Parameters:
1732c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1733c7afd0dbSLois Curfman McInnes -  tol - tolerance
1734c7afd0dbSLois Curfman McInnes 
17359b94acceSBarry Smith    Options Database Key:
1736c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
17379b94acceSBarry Smith 
173836851e7fSLois Curfman McInnes    Level: intermediate
173936851e7fSLois Curfman McInnes 
17409b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
17419b94acceSBarry Smith 
17422492ecdbSBarry Smith .seealso: SNESSetTolerances()
17439b94acceSBarry Smith @*/
174463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
17459b94acceSBarry Smith {
17463a40ed3dSBarry Smith   PetscFunctionBegin;
17470700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
17489b94acceSBarry Smith   snes->deltatol = tol;
17493a40ed3dSBarry Smith   PetscFunctionReturn(0);
17509b94acceSBarry Smith }
17519b94acceSBarry Smith 
1752df9fa365SBarry Smith /*
1753df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1754df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1755df9fa365SBarry Smith    macros instead of functions
1756df9fa365SBarry Smith */
17574a2ae208SSatish Balay #undef __FUNCT__
1758a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1759a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1760ce1608b8SBarry Smith {
1761dfbe8321SBarry Smith   PetscErrorCode ierr;
1762ce1608b8SBarry Smith 
1763ce1608b8SBarry Smith   PetscFunctionBegin;
17640700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1765a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1766ce1608b8SBarry Smith   PetscFunctionReturn(0);
1767ce1608b8SBarry Smith }
1768ce1608b8SBarry Smith 
17694a2ae208SSatish Balay #undef __FUNCT__
1770a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1771a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1772df9fa365SBarry Smith {
1773dfbe8321SBarry Smith   PetscErrorCode ierr;
1774df9fa365SBarry Smith 
1775df9fa365SBarry Smith   PetscFunctionBegin;
1776a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1777df9fa365SBarry Smith   PetscFunctionReturn(0);
1778df9fa365SBarry Smith }
1779df9fa365SBarry Smith 
17804a2ae208SSatish Balay #undef __FUNCT__
1781a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1782a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1783df9fa365SBarry Smith {
1784dfbe8321SBarry Smith   PetscErrorCode ierr;
1785df9fa365SBarry Smith 
1786df9fa365SBarry Smith   PetscFunctionBegin;
1787a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1788df9fa365SBarry Smith   PetscFunctionReturn(0);
1789df9fa365SBarry Smith }
1790df9fa365SBarry Smith 
1791b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1792b271bb04SBarry Smith #undef __FUNCT__
1793b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
1794b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1795b271bb04SBarry Smith {
1796b271bb04SBarry Smith   PetscDrawLG      lg;
1797b271bb04SBarry Smith   PetscErrorCode   ierr;
1798b271bb04SBarry Smith   PetscReal        x,y,per;
1799b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1800b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1801b271bb04SBarry Smith   PetscDraw        draw;
1802b271bb04SBarry Smith   PetscFunctionBegin;
1803b271bb04SBarry Smith   if (!monctx) {
1804b271bb04SBarry Smith     MPI_Comm    comm;
1805b271bb04SBarry Smith 
1806b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1807b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1808b271bb04SBarry Smith   }
1809b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1810b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1811b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1812b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1813b271bb04SBarry Smith   x = (PetscReal) n;
1814b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1815b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1816b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1817b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1818b271bb04SBarry Smith   }
1819b271bb04SBarry Smith 
1820b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1821b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1822b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1823b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1824b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1825b271bb04SBarry Smith   x = (PetscReal) n;
1826b271bb04SBarry Smith   y = 100.0*per;
1827b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1828b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1829b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1830b271bb04SBarry Smith   }
1831b271bb04SBarry Smith 
1832b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1833b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1834b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1835b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1836b271bb04SBarry Smith   x = (PetscReal) n;
1837b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1838b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1839b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1840b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1841b271bb04SBarry Smith   }
1842b271bb04SBarry Smith 
1843b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1844b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1845b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1846b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1847b271bb04SBarry Smith   x = (PetscReal) n;
1848b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1849b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1850b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1851b271bb04SBarry Smith   }
1852b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1853b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1854b271bb04SBarry Smith   }
1855b271bb04SBarry Smith   prev = rnorm;
1856b271bb04SBarry Smith   PetscFunctionReturn(0);
1857b271bb04SBarry Smith }
1858b271bb04SBarry Smith 
1859b271bb04SBarry Smith #undef __FUNCT__
1860b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
1861b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1862b271bb04SBarry Smith {
1863b271bb04SBarry Smith   PetscErrorCode ierr;
1864b271bb04SBarry Smith 
1865b271bb04SBarry Smith   PetscFunctionBegin;
1866b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1867b271bb04SBarry Smith   PetscFunctionReturn(0);
1868b271bb04SBarry Smith }
1869b271bb04SBarry Smith 
1870b271bb04SBarry Smith #undef __FUNCT__
1871b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
1872b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw)
1873b271bb04SBarry Smith {
1874b271bb04SBarry Smith   PetscErrorCode ierr;
1875b271bb04SBarry Smith 
1876b271bb04SBarry Smith   PetscFunctionBegin;
1877b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1878b271bb04SBarry Smith   PetscFunctionReturn(0);
1879b271bb04SBarry Smith }
1880b271bb04SBarry Smith 
18819b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
18829b94acceSBarry Smith 
18834a2ae208SSatish Balay #undef __FUNCT__
1884a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
18859b94acceSBarry Smith /*@C
1886a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
18879b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
18889b94acceSBarry Smith    progress.
18899b94acceSBarry Smith 
1890fee21e36SBarry Smith    Collective on SNES
1891fee21e36SBarry Smith 
1892c7afd0dbSLois Curfman McInnes    Input Parameters:
1893c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1894c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1895b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1896e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1897b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1898b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
18999b94acceSBarry Smith 
1900c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1901a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1902c7afd0dbSLois Curfman McInnes 
1903c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1904c7afd0dbSLois Curfman McInnes .    its - iteration number
1905c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
190640a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
19079b94acceSBarry Smith 
19089665c990SLois Curfman McInnes    Options Database Keys:
1909a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1910a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1911a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1912a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1913c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1914a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1915c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1916c7afd0dbSLois Curfman McInnes                             the options database.
19179665c990SLois Curfman McInnes 
1918639f9d9dSBarry Smith    Notes:
19196bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1920a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
19216bc08f3fSLois Curfman McInnes    order in which they were set.
1922639f9d9dSBarry Smith 
1923025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1924025f1a04SBarry Smith 
192536851e7fSLois Curfman McInnes    Level: intermediate
192636851e7fSLois Curfman McInnes 
19279b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
19289b94acceSBarry Smith 
1929a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
19309b94acceSBarry Smith @*/
1931b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
19329b94acceSBarry Smith {
1933b90d0a6eSBarry Smith   PetscInt i;
1934b90d0a6eSBarry Smith 
19353a40ed3dSBarry Smith   PetscFunctionBegin;
19360700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
193717186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1938b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1939b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1940b90d0a6eSBarry Smith 
1941b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1942b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1943b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1944b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1945b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1946b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1947b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1948b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1949b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1950b90d0a6eSBarry Smith         }
1951b90d0a6eSBarry Smith       }
1952b90d0a6eSBarry Smith     }
1953b90d0a6eSBarry Smith   }
1954b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1955b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1956639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
19573a40ed3dSBarry Smith   PetscFunctionReturn(0);
19589b94acceSBarry Smith }
19599b94acceSBarry Smith 
19604a2ae208SSatish Balay #undef __FUNCT__
1961a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
19625cd90555SBarry Smith /*@C
1963a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
19645cd90555SBarry Smith 
1965c7afd0dbSLois Curfman McInnes    Collective on SNES
1966c7afd0dbSLois Curfman McInnes 
19675cd90555SBarry Smith    Input Parameters:
19685cd90555SBarry Smith .  snes - the SNES context
19695cd90555SBarry Smith 
19701a480d89SAdministrator    Options Database Key:
1971a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1972a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1973c7afd0dbSLois Curfman McInnes     set via the options database
19745cd90555SBarry Smith 
19755cd90555SBarry Smith    Notes:
19765cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
19775cd90555SBarry Smith 
197836851e7fSLois Curfman McInnes    Level: intermediate
197936851e7fSLois Curfman McInnes 
19805cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
19815cd90555SBarry Smith 
1982a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
19835cd90555SBarry Smith @*/
1984a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
19855cd90555SBarry Smith {
1986d952e501SBarry Smith   PetscErrorCode ierr;
1987d952e501SBarry Smith   PetscInt       i;
1988d952e501SBarry Smith 
19895cd90555SBarry Smith   PetscFunctionBegin;
19900700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1991d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1992d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1993d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1994d952e501SBarry Smith     }
1995d952e501SBarry Smith   }
19965cd90555SBarry Smith   snes->numbermonitors = 0;
19975cd90555SBarry Smith   PetscFunctionReturn(0);
19985cd90555SBarry Smith }
19995cd90555SBarry Smith 
20004a2ae208SSatish Balay #undef __FUNCT__
20014a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
20029b94acceSBarry Smith /*@C
20039b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
20049b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
20059b94acceSBarry Smith 
2006fee21e36SBarry Smith    Collective on SNES
2007fee21e36SBarry Smith 
2008c7afd0dbSLois Curfman McInnes    Input Parameters:
2009c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2010c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
20117f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
20127f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
20139b94acceSBarry Smith 
2014c7afd0dbSLois Curfman McInnes    Calling sequence of func:
201506ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
2016c7afd0dbSLois Curfman McInnes 
2017c7afd0dbSLois Curfman McInnes +    snes - the SNES context
201806ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
2019c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
2020184914b5SBarry Smith .    reason - reason for convergence/divergence
2021c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
20224b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
20234b27c08aSLois Curfman McInnes -    f - 2-norm of function
20249b94acceSBarry Smith 
202536851e7fSLois Curfman McInnes    Level: advanced
202636851e7fSLois Curfman McInnes 
20279b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
20289b94acceSBarry Smith 
202985385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
20309b94acceSBarry Smith @*/
20317f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
20329b94acceSBarry Smith {
20337f7931b9SBarry Smith   PetscErrorCode ierr;
20347f7931b9SBarry Smith 
20353a40ed3dSBarry Smith   PetscFunctionBegin;
20360700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
203785385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
20387f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
20397f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
20407f7931b9SBarry Smith   }
204185385478SLisandro Dalcin   snes->ops->converged        = func;
20427f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
204385385478SLisandro Dalcin   snes->cnvP                  = cctx;
20443a40ed3dSBarry Smith   PetscFunctionReturn(0);
20459b94acceSBarry Smith }
20469b94acceSBarry Smith 
20474a2ae208SSatish Balay #undef __FUNCT__
20484a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
204952baeb72SSatish Balay /*@
2050184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2051184914b5SBarry Smith 
2052184914b5SBarry Smith    Not Collective
2053184914b5SBarry Smith 
2054184914b5SBarry Smith    Input Parameter:
2055184914b5SBarry Smith .  snes - the SNES context
2056184914b5SBarry Smith 
2057184914b5SBarry Smith    Output Parameter:
20584d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2059184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2060184914b5SBarry Smith 
2061184914b5SBarry Smith    Level: intermediate
2062184914b5SBarry Smith 
2063184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2064184914b5SBarry Smith 
2065184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2066184914b5SBarry Smith 
206785385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2068184914b5SBarry Smith @*/
206963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2070184914b5SBarry Smith {
2071184914b5SBarry Smith   PetscFunctionBegin;
20720700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
20734482741eSBarry Smith   PetscValidPointer(reason,2);
2074184914b5SBarry Smith   *reason = snes->reason;
2075184914b5SBarry Smith   PetscFunctionReturn(0);
2076184914b5SBarry Smith }
2077184914b5SBarry Smith 
20784a2ae208SSatish Balay #undef __FUNCT__
20794a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2080c9005455SLois Curfman McInnes /*@
2081c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2082c9005455SLois Curfman McInnes 
2083fee21e36SBarry Smith    Collective on SNES
2084fee21e36SBarry Smith 
2085c7afd0dbSLois Curfman McInnes    Input Parameters:
2086c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
20878c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2088cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2089758f92a0SBarry Smith .  na  - size of a and its
209064731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2091758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2092c7afd0dbSLois Curfman McInnes 
2093c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2094c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2095c9005455SLois Curfman McInnes    during the section of code that is being timed.
2096c9005455SLois Curfman McInnes 
209736851e7fSLois Curfman McInnes    Level: intermediate
209836851e7fSLois Curfman McInnes 
2099c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2100758f92a0SBarry Smith 
210108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2102758f92a0SBarry Smith 
2103c9005455SLois Curfman McInnes @*/
2104a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
2105c9005455SLois Curfman McInnes {
21063a40ed3dSBarry Smith   PetscFunctionBegin;
21070700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
21084482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2109a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2110c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2111758f92a0SBarry Smith   snes->conv_hist_its   = its;
2112758f92a0SBarry Smith   snes->conv_hist_max   = na;
2113a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2114758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2115758f92a0SBarry Smith   PetscFunctionReturn(0);
2116758f92a0SBarry Smith }
2117758f92a0SBarry Smith 
21184a2ae208SSatish Balay #undef __FUNCT__
21194a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
21200c4c9dddSBarry Smith /*@C
2121758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2122758f92a0SBarry Smith 
2123758f92a0SBarry Smith    Collective on SNES
2124758f92a0SBarry Smith 
2125758f92a0SBarry Smith    Input Parameter:
2126758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2127758f92a0SBarry Smith 
2128758f92a0SBarry Smith    Output Parameters:
2129758f92a0SBarry Smith .  a   - array to hold history
2130758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2131758f92a0SBarry Smith          negative if not converged) for each solve.
2132758f92a0SBarry Smith -  na  - size of a and its
2133758f92a0SBarry Smith 
2134758f92a0SBarry Smith    Notes:
2135758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2136758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2137758f92a0SBarry Smith 
2138758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2139758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2140758f92a0SBarry Smith    during the section of code that is being timed.
2141758f92a0SBarry Smith 
2142758f92a0SBarry Smith    Level: intermediate
2143758f92a0SBarry Smith 
2144758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2145758f92a0SBarry Smith 
2146758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2147758f92a0SBarry Smith 
2148758f92a0SBarry Smith @*/
214963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2150758f92a0SBarry Smith {
2151758f92a0SBarry Smith   PetscFunctionBegin;
21520700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2153758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2154758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2155758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
21563a40ed3dSBarry Smith   PetscFunctionReturn(0);
2157c9005455SLois Curfman McInnes }
2158c9005455SLois Curfman McInnes 
2159e74ef692SMatthew Knepley #undef __FUNCT__
2160e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2161ac226902SBarry Smith /*@C
216276b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
21637e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
21647e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
216576b2cf59SMatthew Knepley 
216676b2cf59SMatthew Knepley   Collective on SNES
216776b2cf59SMatthew Knepley 
216876b2cf59SMatthew Knepley   Input Parameters:
216976b2cf59SMatthew Knepley . snes - The nonlinear solver context
217076b2cf59SMatthew Knepley . func - The function
217176b2cf59SMatthew Knepley 
217276b2cf59SMatthew Knepley   Calling sequence of func:
2173b5d30489SBarry Smith . func (SNES snes, PetscInt step);
217476b2cf59SMatthew Knepley 
217576b2cf59SMatthew Knepley . step - The current step of the iteration
217676b2cf59SMatthew Knepley 
2177fe97e370SBarry Smith   Level: advanced
2178fe97e370SBarry Smith 
2179fe97e370SBarry 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()
2180fe97e370SBarry Smith         This is not used by most users.
218176b2cf59SMatthew Knepley 
218276b2cf59SMatthew Knepley .keywords: SNES, update
2183b5d30489SBarry Smith 
218485385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
218576b2cf59SMatthew Knepley @*/
218663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
218776b2cf59SMatthew Knepley {
218876b2cf59SMatthew Knepley   PetscFunctionBegin;
21890700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2190e7788613SBarry Smith   snes->ops->update = func;
219176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
219276b2cf59SMatthew Knepley }
219376b2cf59SMatthew Knepley 
2194e74ef692SMatthew Knepley #undef __FUNCT__
2195e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
219676b2cf59SMatthew Knepley /*@
219776b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
219876b2cf59SMatthew Knepley 
219976b2cf59SMatthew Knepley   Not collective
220076b2cf59SMatthew Knepley 
220176b2cf59SMatthew Knepley   Input Parameters:
220276b2cf59SMatthew Knepley . snes - The nonlinear solver context
220376b2cf59SMatthew Knepley . step - The current step of the iteration
220476b2cf59SMatthew Knepley 
2205205452f4SMatthew Knepley   Level: intermediate
2206205452f4SMatthew Knepley 
220776b2cf59SMatthew Knepley .keywords: SNES, update
2208a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
220976b2cf59SMatthew Knepley @*/
221063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
221176b2cf59SMatthew Knepley {
221276b2cf59SMatthew Knepley   PetscFunctionBegin;
221376b2cf59SMatthew Knepley   PetscFunctionReturn(0);
221476b2cf59SMatthew Knepley }
221576b2cf59SMatthew Knepley 
22164a2ae208SSatish Balay #undef __FUNCT__
22174a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
22189b94acceSBarry Smith /*
22199b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
22209b94acceSBarry Smith    positive parameter delta.
22219b94acceSBarry Smith 
22229b94acceSBarry Smith     Input Parameters:
2223c7afd0dbSLois Curfman McInnes +   snes - the SNES context
22249b94acceSBarry Smith .   y - approximate solution of linear system
22259b94acceSBarry Smith .   fnorm - 2-norm of current function
2226c7afd0dbSLois Curfman McInnes -   delta - trust region size
22279b94acceSBarry Smith 
22289b94acceSBarry Smith     Output Parameters:
2229c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
22309b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
22319b94acceSBarry Smith     region, and exceeds zero otherwise.
2232c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
22339b94acceSBarry Smith 
22349b94acceSBarry Smith     Note:
22354b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
22369b94acceSBarry Smith     is set to be the maximum allowable step size.
22379b94acceSBarry Smith 
22389b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
22399b94acceSBarry Smith */
2240dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
22419b94acceSBarry Smith {
2242064f8208SBarry Smith   PetscReal      nrm;
2243ea709b57SSatish Balay   PetscScalar    cnorm;
2244dfbe8321SBarry Smith   PetscErrorCode ierr;
22453a40ed3dSBarry Smith 
22463a40ed3dSBarry Smith   PetscFunctionBegin;
22470700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22480700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2249c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2250184914b5SBarry Smith 
2251064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2252064f8208SBarry Smith   if (nrm > *delta) {
2253064f8208SBarry Smith      nrm = *delta/nrm;
2254064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2255064f8208SBarry Smith      cnorm = nrm;
22562dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
22579b94acceSBarry Smith      *ynorm = *delta;
22589b94acceSBarry Smith   } else {
22599b94acceSBarry Smith      *gpnorm = 0.0;
2260064f8208SBarry Smith      *ynorm = nrm;
22619b94acceSBarry Smith   }
22623a40ed3dSBarry Smith   PetscFunctionReturn(0);
22639b94acceSBarry Smith }
22649b94acceSBarry Smith 
22654a2ae208SSatish Balay #undef __FUNCT__
22664a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
22676ce558aeSBarry Smith /*@C
2268f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2269f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
22709b94acceSBarry Smith 
2271c7afd0dbSLois Curfman McInnes    Collective on SNES
2272c7afd0dbSLois Curfman McInnes 
2273b2002411SLois Curfman McInnes    Input Parameters:
2274c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2275f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
227685385478SLisandro Dalcin -  x - the solution vector.
22779b94acceSBarry Smith 
2278b2002411SLois Curfman McInnes    Notes:
22798ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
22808ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
22818ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
22828ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
22838ddd3da0SLois Curfman McInnes 
228436851e7fSLois Curfman McInnes    Level: beginner
228536851e7fSLois Curfman McInnes 
22869b94acceSBarry Smith .keywords: SNES, nonlinear, solve
22879b94acceSBarry Smith 
228885385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
22899b94acceSBarry Smith @*/
2290f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
22919b94acceSBarry Smith {
2292dfbe8321SBarry Smith   PetscErrorCode ierr;
2293f1af5d2fSBarry Smith   PetscTruth     flg;
2294eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2295eabae89aSBarry Smith   PetscViewer    viewer;
2296052efed2SBarry Smith 
22973a40ed3dSBarry Smith   PetscFunctionBegin;
22980700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22990700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2300f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
23010700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
230285385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
230385385478SLisandro Dalcin 
230485385478SLisandro Dalcin   /* set solution vector */
230585385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
230685385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
230785385478SLisandro Dalcin   snes->vec_sol = x;
230885385478SLisandro Dalcin   /* set afine vector if provided */
230985385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
231085385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
231185385478SLisandro Dalcin   snes->vec_rhs = b;
231285385478SLisandro Dalcin 
231385385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
231485385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func);CHKERRQ(ierr);
231570e92668SMatthew Knepley   }
23163f149594SLisandro Dalcin 
231770e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
23183f149594SLisandro Dalcin 
2319abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
232050ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2321d5e45103SBarry Smith 
23223f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
23234936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
232485385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
23254936397dSBarry Smith   if (snes->domainerror){
23264936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
23274936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
23284936397dSBarry Smith   }
232917186662SBarry Smith   if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
23303f149594SLisandro Dalcin 
23317adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2332eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
23337adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2334eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2335eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2336eabae89aSBarry Smith   }
2337eabae89aSBarry Smith 
233890d69ab7SBarry Smith   flg  = PETSC_FALSE;
233990d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2340da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
23415968eb51SBarry Smith   if (snes->printreason) {
23425968eb51SBarry Smith     if (snes->reason > 0) {
23437adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
23445968eb51SBarry Smith     } else {
23457adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
23465968eb51SBarry Smith     }
23475968eb51SBarry Smith   }
23485968eb51SBarry Smith 
2349*e113a28aSBarry Smith   if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged");
23503a40ed3dSBarry Smith   PetscFunctionReturn(0);
23519b94acceSBarry Smith }
23529b94acceSBarry Smith 
23539b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
23549b94acceSBarry Smith 
23554a2ae208SSatish Balay #undef __FUNCT__
23564a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
235782bf6240SBarry Smith /*@C
23584b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
23599b94acceSBarry Smith 
2360fee21e36SBarry Smith    Collective on SNES
2361fee21e36SBarry Smith 
2362c7afd0dbSLois Curfman McInnes    Input Parameters:
2363c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2364454a90a3SBarry Smith -  type - a known method
2365c7afd0dbSLois Curfman McInnes 
2366c7afd0dbSLois Curfman McInnes    Options Database Key:
2367454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2368c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2369ae12b187SLois Curfman McInnes 
23709b94acceSBarry Smith    Notes:
2371e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
23724b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2373c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23744b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2375c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23769b94acceSBarry Smith 
2377ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2378ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2379ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2380ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2381ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2382ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2383ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2384ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2385ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2386b0a32e0cSBarry Smith   appropriate method.
238736851e7fSLois Curfman McInnes 
238836851e7fSLois Curfman McInnes   Level: intermediate
2389a703fe33SLois Curfman McInnes 
2390454a90a3SBarry Smith .keywords: SNES, set, type
2391435da068SBarry Smith 
2392435da068SBarry Smith .seealso: SNESType, SNESCreate()
2393435da068SBarry Smith 
23949b94acceSBarry Smith @*/
2395a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
23969b94acceSBarry Smith {
2397dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
23986831982aSBarry Smith   PetscTruth     match;
23993a40ed3dSBarry Smith 
24003a40ed3dSBarry Smith   PetscFunctionBegin;
24010700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24024482741eSBarry Smith   PetscValidCharPointer(type,2);
240382bf6240SBarry Smith 
24046831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
24050f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
240692ff6ae8SBarry Smith 
24077adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2408e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
240975396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
241075396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
241175396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
241275396ef9SLisandro Dalcin   snes->ops->setup          = 0;
241375396ef9SLisandro Dalcin   snes->ops->solve          = 0;
241475396ef9SLisandro Dalcin   snes->ops->view           = 0;
241575396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
241675396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
241775396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
241875396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
24193a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2420454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
24213a40ed3dSBarry Smith   PetscFunctionReturn(0);
24229b94acceSBarry Smith }
24239b94acceSBarry Smith 
2424a847f771SSatish Balay 
24259b94acceSBarry Smith /* --------------------------------------------------------------------- */
24264a2ae208SSatish Balay #undef __FUNCT__
24274a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
242852baeb72SSatish Balay /*@
24299b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2430f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
24319b94acceSBarry Smith 
2432fee21e36SBarry Smith    Not Collective
2433fee21e36SBarry Smith 
243436851e7fSLois Curfman McInnes    Level: advanced
243536851e7fSLois Curfman McInnes 
24369b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
24379b94acceSBarry Smith 
24389b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
24399b94acceSBarry Smith @*/
244063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
24419b94acceSBarry Smith {
2442dfbe8321SBarry Smith   PetscErrorCode ierr;
244382bf6240SBarry Smith 
24443a40ed3dSBarry Smith   PetscFunctionBegin;
24451441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
24464c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
24473a40ed3dSBarry Smith   PetscFunctionReturn(0);
24489b94acceSBarry Smith }
24499b94acceSBarry Smith 
24504a2ae208SSatish Balay #undef __FUNCT__
24514a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
24529b94acceSBarry Smith /*@C
24539a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
24549b94acceSBarry Smith 
2455c7afd0dbSLois Curfman McInnes    Not Collective
2456c7afd0dbSLois Curfman McInnes 
24579b94acceSBarry Smith    Input Parameter:
24584b0e389bSBarry Smith .  snes - nonlinear solver context
24599b94acceSBarry Smith 
24609b94acceSBarry Smith    Output Parameter:
24613a7fca6bSBarry Smith .  type - SNES method (a character string)
24629b94acceSBarry Smith 
246336851e7fSLois Curfman McInnes    Level: intermediate
246436851e7fSLois Curfman McInnes 
2465454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
24669b94acceSBarry Smith @*/
2467a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
24689b94acceSBarry Smith {
24693a40ed3dSBarry Smith   PetscFunctionBegin;
24700700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24714482741eSBarry Smith   PetscValidPointer(type,2);
24727adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
24733a40ed3dSBarry Smith   PetscFunctionReturn(0);
24749b94acceSBarry Smith }
24759b94acceSBarry Smith 
24764a2ae208SSatish Balay #undef __FUNCT__
24774a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
247852baeb72SSatish Balay /*@
24799b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
24809b94acceSBarry Smith    stored.
24819b94acceSBarry Smith 
2482c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2483c7afd0dbSLois Curfman McInnes 
24849b94acceSBarry Smith    Input Parameter:
24859b94acceSBarry Smith .  snes - the SNES context
24869b94acceSBarry Smith 
24879b94acceSBarry Smith    Output Parameter:
24889b94acceSBarry Smith .  x - the solution
24899b94acceSBarry Smith 
249070e92668SMatthew Knepley    Level: intermediate
249136851e7fSLois Curfman McInnes 
24929b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
24939b94acceSBarry Smith 
249485385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
24959b94acceSBarry Smith @*/
249663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
24979b94acceSBarry Smith {
24983a40ed3dSBarry Smith   PetscFunctionBegin;
24990700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25004482741eSBarry Smith   PetscValidPointer(x,2);
250185385478SLisandro Dalcin   *x = snes->vec_sol;
250270e92668SMatthew Knepley   PetscFunctionReturn(0);
250370e92668SMatthew Knepley }
250470e92668SMatthew Knepley 
250570e92668SMatthew Knepley #undef __FUNCT__
25064a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
250752baeb72SSatish Balay /*@
25089b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
25099b94acceSBarry Smith    stored.
25109b94acceSBarry Smith 
2511c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2512c7afd0dbSLois Curfman McInnes 
25139b94acceSBarry Smith    Input Parameter:
25149b94acceSBarry Smith .  snes - the SNES context
25159b94acceSBarry Smith 
25169b94acceSBarry Smith    Output Parameter:
25179b94acceSBarry Smith .  x - the solution update
25189b94acceSBarry Smith 
251936851e7fSLois Curfman McInnes    Level: advanced
252036851e7fSLois Curfman McInnes 
25219b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
25229b94acceSBarry Smith 
252385385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
25249b94acceSBarry Smith @*/
252563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
25269b94acceSBarry Smith {
25273a40ed3dSBarry Smith   PetscFunctionBegin;
25280700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25294482741eSBarry Smith   PetscValidPointer(x,2);
253085385478SLisandro Dalcin   *x = snes->vec_sol_update;
25313a40ed3dSBarry Smith   PetscFunctionReturn(0);
25329b94acceSBarry Smith }
25339b94acceSBarry Smith 
25344a2ae208SSatish Balay #undef __FUNCT__
25354a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
25369b94acceSBarry Smith /*@C
25373638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
25389b94acceSBarry Smith 
2539c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2540c7afd0dbSLois Curfman McInnes 
25419b94acceSBarry Smith    Input Parameter:
25429b94acceSBarry Smith .  snes - the SNES context
25439b94acceSBarry Smith 
25449b94acceSBarry Smith    Output Parameter:
25457bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
254670e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
254770e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
25489b94acceSBarry Smith 
254936851e7fSLois Curfman McInnes    Level: advanced
255036851e7fSLois Curfman McInnes 
2551a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
25529b94acceSBarry Smith 
25534b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
25549b94acceSBarry Smith @*/
255570e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
25569b94acceSBarry Smith {
25573a40ed3dSBarry Smith   PetscFunctionBegin;
25580700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
255985385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2560e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
256170e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
25623a40ed3dSBarry Smith   PetscFunctionReturn(0);
25639b94acceSBarry Smith }
25649b94acceSBarry Smith 
25654a2ae208SSatish Balay #undef __FUNCT__
25664a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
25673c7409f5SSatish Balay /*@C
25683c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2569d850072dSLois Curfman McInnes    SNES options in the database.
25703c7409f5SSatish Balay 
2571fee21e36SBarry Smith    Collective on SNES
2572fee21e36SBarry Smith 
2573c7afd0dbSLois Curfman McInnes    Input Parameter:
2574c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2575c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2576c7afd0dbSLois Curfman McInnes 
2577d850072dSLois Curfman McInnes    Notes:
2578a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2579c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2580d850072dSLois Curfman McInnes 
258136851e7fSLois Curfman McInnes    Level: advanced
258236851e7fSLois Curfman McInnes 
25833c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2584a86d99e1SLois Curfman McInnes 
2585a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
25863c7409f5SSatish Balay @*/
258763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
25883c7409f5SSatish Balay {
2589dfbe8321SBarry Smith   PetscErrorCode ierr;
25903c7409f5SSatish Balay 
25913a40ed3dSBarry Smith   PetscFunctionBegin;
25920700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2593639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25941cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
259594b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25963a40ed3dSBarry Smith   PetscFunctionReturn(0);
25973c7409f5SSatish Balay }
25983c7409f5SSatish Balay 
25994a2ae208SSatish Balay #undef __FUNCT__
26004a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
26013c7409f5SSatish Balay /*@C
2602f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2603d850072dSLois Curfman McInnes    SNES options in the database.
26043c7409f5SSatish Balay 
2605fee21e36SBarry Smith    Collective on SNES
2606fee21e36SBarry Smith 
2607c7afd0dbSLois Curfman McInnes    Input Parameters:
2608c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2609c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2610c7afd0dbSLois Curfman McInnes 
2611d850072dSLois Curfman McInnes    Notes:
2612a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2613c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2614d850072dSLois Curfman McInnes 
261536851e7fSLois Curfman McInnes    Level: advanced
261636851e7fSLois Curfman McInnes 
26173c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2618a86d99e1SLois Curfman McInnes 
2619a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
26203c7409f5SSatish Balay @*/
262163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
26223c7409f5SSatish Balay {
2623dfbe8321SBarry Smith   PetscErrorCode ierr;
26243c7409f5SSatish Balay 
26253a40ed3dSBarry Smith   PetscFunctionBegin;
26260700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2627639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26281cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
262994b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
26303a40ed3dSBarry Smith   PetscFunctionReturn(0);
26313c7409f5SSatish Balay }
26323c7409f5SSatish Balay 
26334a2ae208SSatish Balay #undef __FUNCT__
26344a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
26359ab63eb5SSatish Balay /*@C
26363c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
26373c7409f5SSatish Balay    SNES options in the database.
26383c7409f5SSatish Balay 
2639c7afd0dbSLois Curfman McInnes    Not Collective
2640c7afd0dbSLois Curfman McInnes 
26413c7409f5SSatish Balay    Input Parameter:
26423c7409f5SSatish Balay .  snes - the SNES context
26433c7409f5SSatish Balay 
26443c7409f5SSatish Balay    Output Parameter:
26453c7409f5SSatish Balay .  prefix - pointer to the prefix string used
26463c7409f5SSatish Balay 
26474ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
26489ab63eb5SSatish Balay    sufficient length to hold the prefix.
26499ab63eb5SSatish Balay 
265036851e7fSLois Curfman McInnes    Level: advanced
265136851e7fSLois Curfman McInnes 
26523c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2653a86d99e1SLois Curfman McInnes 
2654a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
26553c7409f5SSatish Balay @*/
2656e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
26573c7409f5SSatish Balay {
2658dfbe8321SBarry Smith   PetscErrorCode ierr;
26593c7409f5SSatish Balay 
26603a40ed3dSBarry Smith   PetscFunctionBegin;
26610700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2662639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26633a40ed3dSBarry Smith   PetscFunctionReturn(0);
26643c7409f5SSatish Balay }
26653c7409f5SSatish Balay 
2666b2002411SLois Curfman McInnes 
26674a2ae208SSatish Balay #undef __FUNCT__
26684a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
26693cea93caSBarry Smith /*@C
26703cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
26713cea93caSBarry Smith 
26727f6c08e0SMatthew Knepley   Level: advanced
26733cea93caSBarry Smith @*/
267463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2675b2002411SLois Curfman McInnes {
2676e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2677dfbe8321SBarry Smith   PetscErrorCode ierr;
2678b2002411SLois Curfman McInnes 
2679b2002411SLois Curfman McInnes   PetscFunctionBegin;
2680b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2681c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2682b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2683b2002411SLois Curfman McInnes }
2684da9b6338SBarry Smith 
2685da9b6338SBarry Smith #undef __FUNCT__
2686da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
268763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2688da9b6338SBarry Smith {
2689dfbe8321SBarry Smith   PetscErrorCode ierr;
269077431f27SBarry Smith   PetscInt       N,i,j;
2691da9b6338SBarry Smith   Vec            u,uh,fh;
2692da9b6338SBarry Smith   PetscScalar    value;
2693da9b6338SBarry Smith   PetscReal      norm;
2694da9b6338SBarry Smith 
2695da9b6338SBarry Smith   PetscFunctionBegin;
2696da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2697da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2698da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2699da9b6338SBarry Smith 
2700da9b6338SBarry Smith   /* currently only works for sequential */
2701da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2702da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2703da9b6338SBarry Smith   for (i=0; i<N; i++) {
2704da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
270577431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2706da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2707ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2708da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
27093ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2710da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
271177431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2712da9b6338SBarry Smith       value = -value;
2713da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2714da9b6338SBarry Smith     }
2715da9b6338SBarry Smith   }
2716da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2717da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2718da9b6338SBarry Smith   PetscFunctionReturn(0);
2719da9b6338SBarry Smith }
272071f87433Sdalcinl 
272171f87433Sdalcinl #undef __FUNCT__
2722fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
272371f87433Sdalcinl /*@
2724fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
272571f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
272671f87433Sdalcinl    Newton method.
272771f87433Sdalcinl 
272871f87433Sdalcinl    Collective on SNES
272971f87433Sdalcinl 
273071f87433Sdalcinl    Input Parameters:
273171f87433Sdalcinl +  snes - SNES context
273271f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
273371f87433Sdalcinl 
273464ba62caSBarry Smith     Options Database:
273564ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
273664ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
273764ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
273864ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
273964ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
274064ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
274164ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
274264ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
274364ba62caSBarry Smith 
274471f87433Sdalcinl    Notes:
274571f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
274671f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
274771f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
274871f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
274971f87433Sdalcinl    solver.
275071f87433Sdalcinl 
275171f87433Sdalcinl    Level: advanced
275271f87433Sdalcinl 
275371f87433Sdalcinl    Reference:
275471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
275571f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
275671f87433Sdalcinl 
275771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
275871f87433Sdalcinl 
2759fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
276071f87433Sdalcinl @*/
2761fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
276271f87433Sdalcinl {
276371f87433Sdalcinl   PetscFunctionBegin;
27640700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
276571f87433Sdalcinl   snes->ksp_ewconv = flag;
276671f87433Sdalcinl   PetscFunctionReturn(0);
276771f87433Sdalcinl }
276871f87433Sdalcinl 
276971f87433Sdalcinl #undef __FUNCT__
2770fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
277171f87433Sdalcinl /*@
2772fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
277371f87433Sdalcinl    for computing relative tolerance for linear solvers within an
277471f87433Sdalcinl    inexact Newton method.
277571f87433Sdalcinl 
277671f87433Sdalcinl    Not Collective
277771f87433Sdalcinl 
277871f87433Sdalcinl    Input Parameter:
277971f87433Sdalcinl .  snes - SNES context
278071f87433Sdalcinl 
278171f87433Sdalcinl    Output Parameter:
278271f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
278371f87433Sdalcinl 
278471f87433Sdalcinl    Notes:
278571f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
278671f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
278771f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
278871f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
278971f87433Sdalcinl    solver.
279071f87433Sdalcinl 
279171f87433Sdalcinl    Level: advanced
279271f87433Sdalcinl 
279371f87433Sdalcinl    Reference:
279471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
279571f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
279671f87433Sdalcinl 
279771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
279871f87433Sdalcinl 
2799fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
280071f87433Sdalcinl @*/
2801fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
280271f87433Sdalcinl {
280371f87433Sdalcinl   PetscFunctionBegin;
28040700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
280571f87433Sdalcinl   PetscValidPointer(flag,2);
280671f87433Sdalcinl   *flag = snes->ksp_ewconv;
280771f87433Sdalcinl   PetscFunctionReturn(0);
280871f87433Sdalcinl }
280971f87433Sdalcinl 
281071f87433Sdalcinl #undef __FUNCT__
2811fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
281271f87433Sdalcinl /*@
2813fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
281471f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
281571f87433Sdalcinl    Newton method.
281671f87433Sdalcinl 
281771f87433Sdalcinl    Collective on SNES
281871f87433Sdalcinl 
281971f87433Sdalcinl    Input Parameters:
282071f87433Sdalcinl +    snes - SNES context
282171f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
282271f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
282371f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
282471f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
282571f87433Sdalcinl              (0 <= gamma2 <= 1)
282671f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
282771f87433Sdalcinl .    alpha2 - power for safeguard
282871f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
282971f87433Sdalcinl 
283071f87433Sdalcinl    Note:
283171f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
283271f87433Sdalcinl 
283371f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
283471f87433Sdalcinl 
283571f87433Sdalcinl    Level: advanced
283671f87433Sdalcinl 
283771f87433Sdalcinl    Reference:
283871f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
283971f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
284071f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
284171f87433Sdalcinl 
284271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
284371f87433Sdalcinl 
2844fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
284571f87433Sdalcinl @*/
2846fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
284771f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
284871f87433Sdalcinl {
2849fa9f3622SBarry Smith   SNESKSPEW *kctx;
285071f87433Sdalcinl   PetscFunctionBegin;
28510700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2852fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
2853e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
285471f87433Sdalcinl 
285571f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
285671f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
285771f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
285871f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
285971f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
286071f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
286171f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
286271f87433Sdalcinl 
286371f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
2864e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
286571f87433Sdalcinl   }
286671f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
2867e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
286871f87433Sdalcinl   }
286971f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
2870e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
287171f87433Sdalcinl   }
287271f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
2873e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
287471f87433Sdalcinl   }
287571f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
2876e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
287771f87433Sdalcinl   }
287871f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
2879e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
288071f87433Sdalcinl   }
288171f87433Sdalcinl   PetscFunctionReturn(0);
288271f87433Sdalcinl }
288371f87433Sdalcinl 
288471f87433Sdalcinl #undef __FUNCT__
2885fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
288671f87433Sdalcinl /*@
2887fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
288871f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
288971f87433Sdalcinl    Newton method.
289071f87433Sdalcinl 
289171f87433Sdalcinl    Not Collective
289271f87433Sdalcinl 
289371f87433Sdalcinl    Input Parameters:
289471f87433Sdalcinl      snes - SNES context
289571f87433Sdalcinl 
289671f87433Sdalcinl    Output Parameters:
289771f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
289871f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
289971f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
290071f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
290171f87433Sdalcinl              (0 <= gamma2 <= 1)
290271f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
290371f87433Sdalcinl .    alpha2 - power for safeguard
290471f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
290571f87433Sdalcinl 
290671f87433Sdalcinl    Level: advanced
290771f87433Sdalcinl 
290871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
290971f87433Sdalcinl 
2910fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
291171f87433Sdalcinl @*/
2912fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
291371f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
291471f87433Sdalcinl {
2915fa9f3622SBarry Smith   SNESKSPEW *kctx;
291671f87433Sdalcinl   PetscFunctionBegin;
29170700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2918fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
2919e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
292071f87433Sdalcinl   if(version)   *version   = kctx->version;
292171f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
292271f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
292371f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
292471f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
292571f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
292671f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
292771f87433Sdalcinl   PetscFunctionReturn(0);
292871f87433Sdalcinl }
292971f87433Sdalcinl 
293071f87433Sdalcinl #undef __FUNCT__
2931fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2932fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
293371f87433Sdalcinl {
293471f87433Sdalcinl   PetscErrorCode ierr;
2935fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
293671f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
293771f87433Sdalcinl 
293871f87433Sdalcinl   PetscFunctionBegin;
2939e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
294071f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
294171f87433Sdalcinl     rtol = kctx->rtol_0;
294271f87433Sdalcinl   } else {
294371f87433Sdalcinl     if (kctx->version == 1) {
294471f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
294571f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
294671f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
294771f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
294871f87433Sdalcinl     } else if (kctx->version == 2) {
294971f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
295071f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
295171f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
295271f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
295371f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
295471f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
295571f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
295671f87433Sdalcinl       stol = PetscMax(rtol,stol);
295771f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
295871f87433Sdalcinl       /* safeguard: avoid oversolving */
295971f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
296071f87433Sdalcinl       stol = PetscMax(rtol,stol);
296171f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
2962e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
296371f87433Sdalcinl   }
296471f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
296571f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
296671f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
296771f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
296871f87433Sdalcinl   PetscFunctionReturn(0);
296971f87433Sdalcinl }
297071f87433Sdalcinl 
297171f87433Sdalcinl #undef __FUNCT__
2972fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2973fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
297471f87433Sdalcinl {
297571f87433Sdalcinl   PetscErrorCode ierr;
2976fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
297771f87433Sdalcinl   PCSide         pcside;
297871f87433Sdalcinl   Vec            lres;
297971f87433Sdalcinl 
298071f87433Sdalcinl   PetscFunctionBegin;
2981e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
298271f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
298371f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
298471f87433Sdalcinl   if (kctx->version == 1) {
2985b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
298671f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
298771f87433Sdalcinl       /* KSP residual is true linear residual */
298871f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
298971f87433Sdalcinl     } else {
299071f87433Sdalcinl       /* KSP residual is preconditioned residual */
299171f87433Sdalcinl       /* compute true linear residual norm */
299271f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
299371f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
299471f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
299571f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
299671f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
299771f87433Sdalcinl     }
299871f87433Sdalcinl   }
299971f87433Sdalcinl   PetscFunctionReturn(0);
300071f87433Sdalcinl }
300171f87433Sdalcinl 
300271f87433Sdalcinl #undef __FUNCT__
300371f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
300471f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
300571f87433Sdalcinl {
300671f87433Sdalcinl   PetscErrorCode ierr;
300771f87433Sdalcinl 
300871f87433Sdalcinl   PetscFunctionBegin;
3009fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
301071f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
3011fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
301271f87433Sdalcinl   PetscFunctionReturn(0);
301371f87433Sdalcinl }
30146c699258SBarry Smith 
30156c699258SBarry Smith #undef __FUNCT__
30166c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
30176c699258SBarry Smith /*@
30186c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
30196c699258SBarry Smith 
30206c699258SBarry Smith    Collective on SNES
30216c699258SBarry Smith 
30226c699258SBarry Smith    Input Parameters:
30236c699258SBarry Smith +  snes - the preconditioner context
30246c699258SBarry Smith -  dm - the dm
30256c699258SBarry Smith 
30266c699258SBarry Smith    Level: intermediate
30276c699258SBarry Smith 
30286c699258SBarry Smith 
30296c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
30306c699258SBarry Smith @*/
30316c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetDM(SNES snes,DM dm)
30326c699258SBarry Smith {
30336c699258SBarry Smith   PetscErrorCode ierr;
3034345fed2cSBarry Smith   KSP            ksp;
30356c699258SBarry Smith 
30366c699258SBarry Smith   PetscFunctionBegin;
30370700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
303870663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
30396c699258SBarry Smith   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
30406c699258SBarry Smith   snes->dm = dm;
3041345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
3042345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
3043f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
30446c699258SBarry Smith   PetscFunctionReturn(0);
30456c699258SBarry Smith }
30466c699258SBarry Smith 
30476c699258SBarry Smith #undef __FUNCT__
30486c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
30496c699258SBarry Smith /*@
30506c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
30516c699258SBarry Smith 
30526c699258SBarry Smith    Collective on SNES
30536c699258SBarry Smith 
30546c699258SBarry Smith    Input Parameter:
30556c699258SBarry Smith . snes - the preconditioner context
30566c699258SBarry Smith 
30576c699258SBarry Smith    Output Parameter:
30586c699258SBarry Smith .  dm - the dm
30596c699258SBarry Smith 
30606c699258SBarry Smith    Level: intermediate
30616c699258SBarry Smith 
30626c699258SBarry Smith 
30636c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
30646c699258SBarry Smith @*/
30656c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetDM(SNES snes,DM *dm)
30666c699258SBarry Smith {
30676c699258SBarry Smith   PetscFunctionBegin;
30680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
30696c699258SBarry Smith   *dm = snes->dm;
30706c699258SBarry Smith   PetscFunctionReturn(0);
30716c699258SBarry Smith }
3072