xref: /petsc/src/snes/interface/snes.c (revision eb1f6c344b3152462f2c01dcaa9f1b610adbbed5)
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 */
9166c7f25SBarry Smith PetscCookie PETSCSNES_DLLEXPORT SNES_COOKIE;
10166c7f25SBarry Smith PetscLogEvent  SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval;
11a09944afSBarry Smith 
12a09944afSBarry Smith #undef __FUNCT__
134936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
14e725d27bSBarry Smith /*@
154936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
164936397dSBarry Smith      in the functions domain. For example, negative pressure.
174936397dSBarry Smith 
184936397dSBarry Smith    Collective on SNES
194936397dSBarry Smith 
204936397dSBarry Smith    Input Parameters:
214936397dSBarry Smith .  SNES - the SNES context
224936397dSBarry Smith 
2328529972SSatish Balay    Level: advanced
244936397dSBarry Smith 
254936397dSBarry Smith .keywords: SNES, view
264936397dSBarry Smith 
274936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
284936397dSBarry Smith @*/
294936397dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunctionDomainError(SNES snes)
304936397dSBarry Smith {
314936397dSBarry Smith   PetscFunctionBegin;
324936397dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
334936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
344936397dSBarry Smith   PetscFunctionReturn(0);
354936397dSBarry Smith }
364936397dSBarry Smith 
374936397dSBarry Smith #undef __FUNCT__
384a2ae208SSatish Balay #define __FUNCT__ "SNESView"
397e2c5f70SBarry Smith /*@C
409b94acceSBarry Smith    SNESView - Prints the SNES data structure.
419b94acceSBarry Smith 
424c49b128SBarry Smith    Collective on SNES
43fee21e36SBarry Smith 
44c7afd0dbSLois Curfman McInnes    Input Parameters:
45c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
46c7afd0dbSLois Curfman McInnes -  viewer - visualization context
47c7afd0dbSLois Curfman McInnes 
489b94acceSBarry Smith    Options Database Key:
49c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
509b94acceSBarry Smith 
519b94acceSBarry Smith    Notes:
529b94acceSBarry Smith    The available visualization contexts include
53b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
54b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
55c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
56c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
57c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
589b94acceSBarry Smith 
593e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
60b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
619b94acceSBarry Smith 
6236851e7fSLois Curfman McInnes    Level: beginner
6336851e7fSLois Curfman McInnes 
649b94acceSBarry Smith .keywords: SNES, view
659b94acceSBarry Smith 
66b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
679b94acceSBarry Smith @*/
6863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer)
699b94acceSBarry Smith {
70fa9f3622SBarry Smith   SNESKSPEW           *kctx;
71dfbe8321SBarry Smith   PetscErrorCode      ierr;
7294b7f48cSBarry Smith   KSP                 ksp;
73a313700dSBarry Smith   const SNESType      type;
7432077d6dSBarry Smith   PetscTruth          iascii,isstring;
759b94acceSBarry Smith 
763a40ed3dSBarry Smith   PetscFunctionBegin;
774482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
783050cee2SBarry Smith   if (!viewer) {
797adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
803050cee2SBarry Smith   }
814482741eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
82c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
8374679c65SBarry Smith 
8432077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
85b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
8632077d6dSBarry Smith   if (iascii) {
877adad957SLisandro Dalcin     if (((PetscObject)snes)->prefix) {
887adad957SLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",((PetscObject)snes)->prefix);CHKERRQ(ierr);
893a7fca6bSBarry Smith     } else {
90b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
913a7fca6bSBarry Smith     }
92454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
93454a90a3SBarry Smith     if (type) {
94b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
95184914b5SBarry Smith     } else {
96b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
97184914b5SBarry Smith     }
98e7788613SBarry Smith     if (snes->ops->view) {
99b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
100e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
101b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1020ef38995SBarry Smith     }
10377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
104a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
10570441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
10677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
10777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1089b94acceSBarry Smith     if (snes->ksp_ewconv) {
109fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1109b94acceSBarry Smith       if (kctx) {
11177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
112a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
113a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1149b94acceSBarry Smith       }
1159b94acceSBarry Smith     }
116*eb1f6c34SBarry Smith     if (snes->lagpreconditioner == -1) {
117*eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is never rebuilt\n");CHKERRQ(ierr);
118*eb1f6c34SBarry Smith     } else if (snes->lagpreconditioner > 1) {
119*eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr);
120*eb1f6c34SBarry Smith     }
121*eb1f6c34SBarry Smith     if (snes->lagjacobian == -1) {
122*eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is never rebuilt\n");CHKERRQ(ierr);
123*eb1f6c34SBarry Smith     } else if (snes->lagjacobian > 1) {
124*eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is rebuilt every %D Newton iterations\n",snes->lagjacobian);CHKERRQ(ierr);
125*eb1f6c34SBarry Smith     }
1260f5bd95cSBarry Smith   } else if (isstring) {
127454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
128b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
12919bcc07fSBarry Smith   }
13094b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
131b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
13294b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
133b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1343a40ed3dSBarry Smith   PetscFunctionReturn(0);
1359b94acceSBarry Smith }
1369b94acceSBarry Smith 
13776b2cf59SMatthew Knepley /*
13876b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
13976b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
14076b2cf59SMatthew Knepley */
14176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
142a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
1436849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
14476b2cf59SMatthew Knepley 
145e74ef692SMatthew Knepley #undef __FUNCT__
146e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
147ac226902SBarry Smith /*@C
14876b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
14976b2cf59SMatthew Knepley 
15076b2cf59SMatthew Knepley   Not Collective
15176b2cf59SMatthew Knepley 
15276b2cf59SMatthew Knepley   Input Parameter:
15376b2cf59SMatthew Knepley . snescheck - function that checks for options
15476b2cf59SMatthew Knepley 
15576b2cf59SMatthew Knepley   Level: developer
15676b2cf59SMatthew Knepley 
15776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
15876b2cf59SMatthew Knepley @*/
15963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
16076b2cf59SMatthew Knepley {
16176b2cf59SMatthew Knepley   PetscFunctionBegin;
16276b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
16377431f27SBarry Smith     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
16476b2cf59SMatthew Knepley   }
16576b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
16676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
16776b2cf59SMatthew Knepley }
16876b2cf59SMatthew Knepley 
169aa3661deSLisandro Dalcin EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
170aa3661deSLisandro Dalcin 
171aa3661deSLisandro Dalcin #undef __FUNCT__
172aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private"
173d3462f78SMatthew Knepley static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscTruth hasOperator, PetscInt version)
174aa3661deSLisandro Dalcin {
175aa3661deSLisandro Dalcin   Mat            J;
176aa3661deSLisandro Dalcin   KSP            ksp;
177aa3661deSLisandro Dalcin   PC             pc;
178aa3661deSLisandro Dalcin   PetscTruth     match;
179aa3661deSLisandro Dalcin   PetscErrorCode ierr;
180aa3661deSLisandro Dalcin 
181aa3661deSLisandro Dalcin   PetscFunctionBegin;
182aa3661deSLisandro Dalcin   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
183aa3661deSLisandro Dalcin 
18498613b67SLisandro Dalcin   if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
18598613b67SLisandro Dalcin     Mat A = snes->jacobian, B = snes->jacobian_pre;
18698613b67SLisandro Dalcin     ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr);
18798613b67SLisandro Dalcin   }
18898613b67SLisandro Dalcin 
189aa3661deSLisandro Dalcin   if (version == 1) {
190aa3661deSLisandro Dalcin     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
19198613b67SLisandro Dalcin     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
192aa3661deSLisandro Dalcin     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
193aa3661deSLisandro Dalcin   } else if (version == 2) {
194aa3661deSLisandro Dalcin     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
19565460251SBarry 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)
196aa3661deSLisandro Dalcin     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr);
197aa3661deSLisandro Dalcin #else
198aa3661deSLisandro Dalcin     SETERRQ(PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
199aa3661deSLisandro Dalcin #endif
200aa3661deSLisandro Dalcin   } else {
201aa3661deSLisandro Dalcin     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2");
202aa3661deSLisandro Dalcin   }
203aa3661deSLisandro Dalcin 
204aa3661deSLisandro Dalcin   ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr);
205d3462f78SMatthew Knepley   if (hasOperator) {
206aa3661deSLisandro Dalcin     /* This version replaces the user provided Jacobian matrix with a
207aa3661deSLisandro Dalcin        matrix-free version but still employs the user-provided preconditioner matrix. */
208aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
209aa3661deSLisandro Dalcin   } else {
210aa3661deSLisandro Dalcin     /* This version replaces both the user-provided Jacobian and the user-
211aa3661deSLisandro Dalcin        provided preconditioner matrix with the default matrix free version. */
212aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
213aa3661deSLisandro Dalcin     /* Force no preconditioner */
214aa3661deSLisandro Dalcin     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
215aa3661deSLisandro Dalcin     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
216aa3661deSLisandro Dalcin     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr);
217aa3661deSLisandro Dalcin     if (!match) {
218aa3661deSLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
219aa3661deSLisandro Dalcin       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
220aa3661deSLisandro Dalcin     }
221aa3661deSLisandro Dalcin   }
222aa3661deSLisandro Dalcin   ierr = MatDestroy(J);CHKERRQ(ierr);
223aa3661deSLisandro Dalcin 
224aa3661deSLisandro Dalcin   PetscFunctionReturn(0);
225aa3661deSLisandro Dalcin }
226aa3661deSLisandro Dalcin 
2274a2ae208SSatish Balay #undef __FUNCT__
2284a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
2299b94acceSBarry Smith /*@
23094b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
2319b94acceSBarry Smith 
232c7afd0dbSLois Curfman McInnes    Collective on SNES
233c7afd0dbSLois Curfman McInnes 
2349b94acceSBarry Smith    Input Parameter:
2359b94acceSBarry Smith .  snes - the SNES context
2369b94acceSBarry Smith 
23736851e7fSLois Curfman McInnes    Options Database Keys:
2386831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
23982738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
24082738288SBarry Smith                 of the change in the solution between steps
24170441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
242b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
243b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
244b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
24550ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
246ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
247a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
248e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
249b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
2502492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
25182738288SBarry Smith                                solver; hence iterations will continue until max_it
2521fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
25382738288SBarry Smith                                of convergence test
254e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
255e8105e01SRichard Katz                                        filename given prints to stdout
256a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
257a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
258a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
259a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
260e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
2615968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
262fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
26382738288SBarry Smith 
26482738288SBarry Smith     Options Database for Eisenstat-Walker method:
265fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
2664b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
26736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
26836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
26936851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
27036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
27136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
27236851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
27382738288SBarry Smith 
27411ca99fdSLois Curfman McInnes    Notes:
27511ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
27611ca99fdSLois Curfman McInnes    the users manual.
27783e2fdc7SBarry Smith 
27836851e7fSLois Curfman McInnes    Level: beginner
27936851e7fSLois Curfman McInnes 
2809b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2819b94acceSBarry Smith 
28269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
2839b94acceSBarry Smith @*/
28463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
2859b94acceSBarry Smith {
286aa3661deSLisandro Dalcin   PetscTruth              flg,mf,mf_operator;
287aa3661deSLisandro Dalcin   PetscInt                i,indx,lag,mf_version;
288aa3661deSLisandro Dalcin   MatStructure            matflag;
28985385478SLisandro Dalcin   const char              *deft = SNESLS;
29085385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
29185385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
292e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
29323d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
29485385478SLisandro Dalcin   PetscErrorCode          ierr;
2959b94acceSBarry Smith 
2963a40ed3dSBarry Smith   PetscFunctionBegin;
2974482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
298ca161407SBarry Smith 
299186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
300cce0b1b2SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
3017adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
302b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
303d64ed03dSBarry Smith     if (flg) {
304186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
3057adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
306186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
307d64ed03dSBarry Smith     }
30890d69ab7SBarry Smith     /* not used here, but called so will go into help messaage */
309909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
31093c39befSBarry Smith 
31157034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
31257034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
313186905e3SBarry Smith 
31457034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
315b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
316b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
31750ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
318ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
31985385478SLisandro Dalcin 
320a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
321a8054027SBarry Smith     if (flg) {
322a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
323a8054027SBarry Smith     }
324e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
325e35cf81dSBarry Smith     if (flg) {
326e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
327e35cf81dSBarry Smith     }
328a8054027SBarry Smith 
32985385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
33085385478SLisandro Dalcin     if (flg) {
33185385478SLisandro Dalcin       switch (indx) {
3327f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
3337f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
33485385478SLisandro Dalcin       }
33585385478SLisandro Dalcin     }
33685385478SLisandro Dalcin 
33790d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
338186905e3SBarry Smith 
33985385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
34085385478SLisandro Dalcin 
341fa9f3622SBarry Smith     ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
342186905e3SBarry Smith 
343fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
344fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
345fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
346fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
347fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
348fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
349fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
350186905e3SBarry Smith 
35190d69ab7SBarry Smith     flg  = PETSC_FALSE;
35290d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
353a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
354eabae89aSBarry Smith 
355a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
356e8105e01SRichard Katz     if (flg) {
357050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
35823d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
359e8105e01SRichard Katz     }
360eabae89aSBarry Smith 
361b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
362b271bb04SBarry Smith     if (flg) {
363b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
364b271bb04SBarry Smith     }
365b271bb04SBarry Smith 
366a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
367eabae89aSBarry Smith     if (flg) {
368050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
369f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
370e8105e01SRichard Katz     }
371eabae89aSBarry Smith 
372a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
373eabae89aSBarry Smith     if (flg) {
374050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
37523d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
376eabae89aSBarry Smith     }
377eabae89aSBarry Smith 
37890d69ab7SBarry Smith     flg  = PETSC_FALSE;
37990d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
380a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
38190d69ab7SBarry Smith     flg  = PETSC_FALSE;
38290d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
383a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
38490d69ab7SBarry Smith     flg  = PETSC_FALSE;
38590d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
386a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
38790d69ab7SBarry Smith     flg  = PETSC_FALSE;
38890d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
389a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
39090d69ab7SBarry Smith     flg  = PETSC_FALSE;
39190d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
392b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
393e24b481bSBarry Smith 
39490d69ab7SBarry Smith     flg  = PETSC_FALSE;
39590d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
3964b27c08aSLois Curfman McInnes     if (flg) {
397186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
398ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
3999b94acceSBarry Smith     }
400639f9d9dSBarry Smith 
401aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
402aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
403aa3661deSLisandro Dalcin     ierr = PetscOptionsTruth("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
404aa3661deSLisandro Dalcin     if (flg && mf_operator) mf = PETSC_TRUE;
405aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
406aa3661deSLisandro Dalcin     ierr = PetscOptionsTruth("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
407aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
408aa3661deSLisandro Dalcin     mf_version = 1;
409aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
410aa3661deSLisandro Dalcin 
41176b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
41276b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
41376b2cf59SMatthew Knepley     }
41476b2cf59SMatthew Knepley 
415e7788613SBarry Smith     if (snes->ops->setfromoptions) {
416e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
417639f9d9dSBarry Smith     }
418b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4194bbc92c1SBarry Smith 
420aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
4211cee3971SBarry Smith 
4221cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
423aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
424aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
42585385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
42693993e2dSLois Curfman McInnes 
4273a40ed3dSBarry Smith   PetscFunctionReturn(0);
4289b94acceSBarry Smith }
4299b94acceSBarry Smith 
430a847f771SSatish Balay 
4314a2ae208SSatish Balay #undef __FUNCT__
4324a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
4339b94acceSBarry Smith /*@
4349b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
4359b94acceSBarry Smith    the nonlinear solvers.
4369b94acceSBarry Smith 
437fee21e36SBarry Smith    Collective on SNES
438fee21e36SBarry Smith 
439c7afd0dbSLois Curfman McInnes    Input Parameters:
440c7afd0dbSLois Curfman McInnes +  snes - the SNES context
441c7afd0dbSLois Curfman McInnes -  usrP - optional user context
442c7afd0dbSLois Curfman McInnes 
44336851e7fSLois Curfman McInnes    Level: intermediate
44436851e7fSLois Curfman McInnes 
4459b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
4469b94acceSBarry Smith 
4479b94acceSBarry Smith .seealso: SNESGetApplicationContext()
4489b94acceSBarry Smith @*/
44963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
4509b94acceSBarry Smith {
4513a40ed3dSBarry Smith   PetscFunctionBegin;
4524482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4539b94acceSBarry Smith   snes->user		= usrP;
4543a40ed3dSBarry Smith   PetscFunctionReturn(0);
4559b94acceSBarry Smith }
45674679c65SBarry Smith 
4574a2ae208SSatish Balay #undef __FUNCT__
4584a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
4599b94acceSBarry Smith /*@C
4609b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
4619b94acceSBarry Smith    nonlinear solvers.
4629b94acceSBarry Smith 
463c7afd0dbSLois Curfman McInnes    Not Collective
464c7afd0dbSLois Curfman McInnes 
4659b94acceSBarry Smith    Input Parameter:
4669b94acceSBarry Smith .  snes - SNES context
4679b94acceSBarry Smith 
4689b94acceSBarry Smith    Output Parameter:
4699b94acceSBarry Smith .  usrP - user context
4709b94acceSBarry Smith 
47136851e7fSLois Curfman McInnes    Level: intermediate
47236851e7fSLois Curfman McInnes 
4739b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
4749b94acceSBarry Smith 
4759b94acceSBarry Smith .seealso: SNESSetApplicationContext()
4769b94acceSBarry Smith @*/
47763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
4789b94acceSBarry Smith {
4793a40ed3dSBarry Smith   PetscFunctionBegin;
4804482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4819b94acceSBarry Smith   *usrP = snes->user;
4823a40ed3dSBarry Smith   PetscFunctionReturn(0);
4839b94acceSBarry Smith }
48474679c65SBarry Smith 
4854a2ae208SSatish Balay #undef __FUNCT__
4864a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
4879b94acceSBarry Smith /*@
488c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
489c8228a4eSBarry Smith    at this time.
4909b94acceSBarry Smith 
491c7afd0dbSLois Curfman McInnes    Not Collective
492c7afd0dbSLois Curfman McInnes 
4939b94acceSBarry Smith    Input Parameter:
4949b94acceSBarry Smith .  snes - SNES context
4959b94acceSBarry Smith 
4969b94acceSBarry Smith    Output Parameter:
4979b94acceSBarry Smith .  iter - iteration number
4989b94acceSBarry Smith 
499c8228a4eSBarry Smith    Notes:
500c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
501c8228a4eSBarry Smith 
502c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
50308405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
50408405cd6SLois Curfman McInnes .vb
50508405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
50608405cd6SLois Curfman McInnes       if (!(it % 2)) {
50708405cd6SLois Curfman McInnes         [compute Jacobian here]
50808405cd6SLois Curfman McInnes       }
50908405cd6SLois Curfman McInnes .ve
510c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
51108405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
512c8228a4eSBarry Smith 
51336851e7fSLois Curfman McInnes    Level: intermediate
51436851e7fSLois Curfman McInnes 
5152b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
5162b668275SBarry Smith 
517b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
5189b94acceSBarry Smith @*/
51963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
5209b94acceSBarry Smith {
5213a40ed3dSBarry Smith   PetscFunctionBegin;
5224482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5234482741eSBarry Smith   PetscValidIntPointer(iter,2);
5249b94acceSBarry Smith   *iter = snes->iter;
5253a40ed3dSBarry Smith   PetscFunctionReturn(0);
5269b94acceSBarry Smith }
52774679c65SBarry Smith 
5284a2ae208SSatish Balay #undef __FUNCT__
5294a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
5309b94acceSBarry Smith /*@
5319b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
5329b94acceSBarry Smith    with SNESSSetFunction().
5339b94acceSBarry Smith 
534c7afd0dbSLois Curfman McInnes    Collective on SNES
535c7afd0dbSLois Curfman McInnes 
5369b94acceSBarry Smith    Input Parameter:
5379b94acceSBarry Smith .  snes - SNES context
5389b94acceSBarry Smith 
5399b94acceSBarry Smith    Output Parameter:
5409b94acceSBarry Smith .  fnorm - 2-norm of function
5419b94acceSBarry Smith 
54236851e7fSLois Curfman McInnes    Level: intermediate
54336851e7fSLois Curfman McInnes 
5449b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
545a86d99e1SLois Curfman McInnes 
546b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
5479b94acceSBarry Smith @*/
54871f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
5499b94acceSBarry Smith {
5503a40ed3dSBarry Smith   PetscFunctionBegin;
5514482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5524482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
5539b94acceSBarry Smith   *fnorm = snes->norm;
5543a40ed3dSBarry Smith   PetscFunctionReturn(0);
5559b94acceSBarry Smith }
55674679c65SBarry Smith 
5574a2ae208SSatish Balay #undef __FUNCT__
558b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
5599b94acceSBarry Smith /*@
560b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
5619b94acceSBarry Smith    attempted by the nonlinear solver.
5629b94acceSBarry Smith 
563c7afd0dbSLois Curfman McInnes    Not Collective
564c7afd0dbSLois Curfman McInnes 
5659b94acceSBarry Smith    Input Parameter:
5669b94acceSBarry Smith .  snes - SNES context
5679b94acceSBarry Smith 
5689b94acceSBarry Smith    Output Parameter:
5699b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
5709b94acceSBarry Smith 
571c96a6f78SLois Curfman McInnes    Notes:
572c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
573c96a6f78SLois Curfman McInnes 
57436851e7fSLois Curfman McInnes    Level: intermediate
57536851e7fSLois Curfman McInnes 
5769b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
57758ebbce7SBarry Smith 
578e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
57958ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
5809b94acceSBarry Smith @*/
581b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
5829b94acceSBarry Smith {
5833a40ed3dSBarry Smith   PetscFunctionBegin;
5844482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5854482741eSBarry Smith   PetscValidIntPointer(nfails,2);
58650ffb88aSMatthew Knepley   *nfails = snes->numFailures;
58750ffb88aSMatthew Knepley   PetscFunctionReturn(0);
58850ffb88aSMatthew Knepley }
58950ffb88aSMatthew Knepley 
59050ffb88aSMatthew Knepley #undef __FUNCT__
591b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
59250ffb88aSMatthew Knepley /*@
593b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
59450ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
59550ffb88aSMatthew Knepley 
59650ffb88aSMatthew Knepley    Not Collective
59750ffb88aSMatthew Knepley 
59850ffb88aSMatthew Knepley    Input Parameters:
59950ffb88aSMatthew Knepley +  snes     - SNES context
60050ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
60150ffb88aSMatthew Knepley 
60250ffb88aSMatthew Knepley    Level: intermediate
60350ffb88aSMatthew Knepley 
60450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
60558ebbce7SBarry Smith 
606e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
60758ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
60850ffb88aSMatthew Knepley @*/
609b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
61050ffb88aSMatthew Knepley {
61150ffb88aSMatthew Knepley   PetscFunctionBegin;
6124482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
61350ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
61450ffb88aSMatthew Knepley   PetscFunctionReturn(0);
61550ffb88aSMatthew Knepley }
61650ffb88aSMatthew Knepley 
61750ffb88aSMatthew Knepley #undef __FUNCT__
618b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
61950ffb88aSMatthew Knepley /*@
620b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
62150ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
62250ffb88aSMatthew Knepley 
62350ffb88aSMatthew Knepley    Not Collective
62450ffb88aSMatthew Knepley 
62550ffb88aSMatthew Knepley    Input Parameter:
62650ffb88aSMatthew Knepley .  snes     - SNES context
62750ffb88aSMatthew Knepley 
62850ffb88aSMatthew Knepley    Output Parameter:
62950ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
63050ffb88aSMatthew Knepley 
63150ffb88aSMatthew Knepley    Level: intermediate
63250ffb88aSMatthew Knepley 
63350ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
63458ebbce7SBarry Smith 
635e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
63658ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
63758ebbce7SBarry Smith 
63850ffb88aSMatthew Knepley @*/
639b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
64050ffb88aSMatthew Knepley {
64150ffb88aSMatthew Knepley   PetscFunctionBegin;
6424482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6434482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
64450ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
6453a40ed3dSBarry Smith   PetscFunctionReturn(0);
6469b94acceSBarry Smith }
647a847f771SSatish Balay 
6484a2ae208SSatish Balay #undef __FUNCT__
6492541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
6502541af92SBarry Smith /*@
6512541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
6522541af92SBarry Smith      done by SNES.
6532541af92SBarry Smith 
6542541af92SBarry Smith    Not Collective
6552541af92SBarry Smith 
6562541af92SBarry Smith    Input Parameter:
6572541af92SBarry Smith .  snes     - SNES context
6582541af92SBarry Smith 
6592541af92SBarry Smith    Output Parameter:
6602541af92SBarry Smith .  nfuncs - number of evaluations
6612541af92SBarry Smith 
6622541af92SBarry Smith    Level: intermediate
6632541af92SBarry Smith 
6642541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
66558ebbce7SBarry Smith 
666e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
6672541af92SBarry Smith @*/
6682541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
6692541af92SBarry Smith {
6702541af92SBarry Smith   PetscFunctionBegin;
6712541af92SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6722541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
6732541af92SBarry Smith   *nfuncs = snes->nfuncs;
6742541af92SBarry Smith   PetscFunctionReturn(0);
6752541af92SBarry Smith }
6762541af92SBarry Smith 
6772541af92SBarry Smith #undef __FUNCT__
6783d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
6793d4c4710SBarry Smith /*@
6803d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
6813d4c4710SBarry Smith    linear solvers.
6823d4c4710SBarry Smith 
6833d4c4710SBarry Smith    Not Collective
6843d4c4710SBarry Smith 
6853d4c4710SBarry Smith    Input Parameter:
6863d4c4710SBarry Smith .  snes - SNES context
6873d4c4710SBarry Smith 
6883d4c4710SBarry Smith    Output Parameter:
6893d4c4710SBarry Smith .  nfails - number of failed solves
6903d4c4710SBarry Smith 
6913d4c4710SBarry Smith    Notes:
6923d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
6933d4c4710SBarry Smith 
6943d4c4710SBarry Smith    Level: intermediate
6953d4c4710SBarry Smith 
6963d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
69758ebbce7SBarry Smith 
698e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
6993d4c4710SBarry Smith @*/
7003d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
7013d4c4710SBarry Smith {
7023d4c4710SBarry Smith   PetscFunctionBegin;
7033d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7043d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
7053d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
7063d4c4710SBarry Smith   PetscFunctionReturn(0);
7073d4c4710SBarry Smith }
7083d4c4710SBarry Smith 
7093d4c4710SBarry Smith #undef __FUNCT__
7103d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
7113d4c4710SBarry Smith /*@
7123d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
7133d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
7143d4c4710SBarry Smith 
7153d4c4710SBarry Smith    Collective on SNES
7163d4c4710SBarry Smith 
7173d4c4710SBarry Smith    Input Parameters:
7183d4c4710SBarry Smith +  snes     - SNES context
7193d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
7203d4c4710SBarry Smith 
7213d4c4710SBarry Smith    Level: intermediate
7223d4c4710SBarry Smith 
723a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
7243d4c4710SBarry Smith 
7253d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
7263d4c4710SBarry Smith 
72758ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
7283d4c4710SBarry Smith @*/
7293d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
7303d4c4710SBarry Smith {
7313d4c4710SBarry Smith   PetscFunctionBegin;
7323d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7333d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
7343d4c4710SBarry Smith   PetscFunctionReturn(0);
7353d4c4710SBarry Smith }
7363d4c4710SBarry Smith 
7373d4c4710SBarry Smith #undef __FUNCT__
7383d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
7393d4c4710SBarry Smith /*@
7403d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
7413d4c4710SBarry Smith      are allowed before SNES terminates
7423d4c4710SBarry Smith 
7433d4c4710SBarry Smith    Not Collective
7443d4c4710SBarry Smith 
7453d4c4710SBarry Smith    Input Parameter:
7463d4c4710SBarry Smith .  snes     - SNES context
7473d4c4710SBarry Smith 
7483d4c4710SBarry Smith    Output Parameter:
7493d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
7503d4c4710SBarry Smith 
7513d4c4710SBarry Smith    Level: intermediate
7523d4c4710SBarry Smith 
7533d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
7543d4c4710SBarry Smith 
7553d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
7563d4c4710SBarry Smith 
757e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
7583d4c4710SBarry Smith @*/
7593d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
7603d4c4710SBarry Smith {
7613d4c4710SBarry Smith   PetscFunctionBegin;
7623d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7633d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
7643d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
7653d4c4710SBarry Smith   PetscFunctionReturn(0);
7663d4c4710SBarry Smith }
7673d4c4710SBarry Smith 
7683d4c4710SBarry Smith #undef __FUNCT__
769b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
770c96a6f78SLois Curfman McInnes /*@
771b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
772c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
773c96a6f78SLois Curfman McInnes 
774c7afd0dbSLois Curfman McInnes    Not Collective
775c7afd0dbSLois Curfman McInnes 
776c96a6f78SLois Curfman McInnes    Input Parameter:
777c96a6f78SLois Curfman McInnes .  snes - SNES context
778c96a6f78SLois Curfman McInnes 
779c96a6f78SLois Curfman McInnes    Output Parameter:
780c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
781c96a6f78SLois Curfman McInnes 
782c96a6f78SLois Curfman McInnes    Notes:
783c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
784c96a6f78SLois Curfman McInnes 
78536851e7fSLois Curfman McInnes    Level: intermediate
78636851e7fSLois Curfman McInnes 
787c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
7882b668275SBarry Smith 
7898c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
790c96a6f78SLois Curfman McInnes @*/
791b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
792c96a6f78SLois Curfman McInnes {
7933a40ed3dSBarry Smith   PetscFunctionBegin;
7944482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7954482741eSBarry Smith   PetscValidIntPointer(lits,2);
796c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
7973a40ed3dSBarry Smith   PetscFunctionReturn(0);
798c96a6f78SLois Curfman McInnes }
799c96a6f78SLois Curfman McInnes 
8004a2ae208SSatish Balay #undef __FUNCT__
80194b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
80252baeb72SSatish Balay /*@
80394b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
8049b94acceSBarry Smith 
80594b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
806c7afd0dbSLois Curfman McInnes 
8079b94acceSBarry Smith    Input Parameter:
8089b94acceSBarry Smith .  snes - the SNES context
8099b94acceSBarry Smith 
8109b94acceSBarry Smith    Output Parameter:
81194b7f48cSBarry Smith .  ksp - the KSP context
8129b94acceSBarry Smith 
8139b94acceSBarry Smith    Notes:
81494b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
8159b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
8162999313aSBarry Smith    PC contexts as well.
8179b94acceSBarry Smith 
81836851e7fSLois Curfman McInnes    Level: beginner
81936851e7fSLois Curfman McInnes 
82094b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
8219b94acceSBarry Smith 
8222999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
8239b94acceSBarry Smith @*/
82463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
8259b94acceSBarry Smith {
8261cee3971SBarry Smith   PetscErrorCode ierr;
8271cee3971SBarry Smith 
8283a40ed3dSBarry Smith   PetscFunctionBegin;
8294482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8304482741eSBarry Smith   PetscValidPointer(ksp,2);
8311cee3971SBarry Smith 
8321cee3971SBarry Smith   if (!snes->ksp) {
8331cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
8341cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
8351cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
8361cee3971SBarry Smith   }
83794b7f48cSBarry Smith   *ksp = snes->ksp;
8383a40ed3dSBarry Smith   PetscFunctionReturn(0);
8399b94acceSBarry Smith }
84082bf6240SBarry Smith 
8414a2ae208SSatish Balay #undef __FUNCT__
8422999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
8432999313aSBarry Smith /*@
8442999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
8452999313aSBarry Smith 
8462999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
8472999313aSBarry Smith 
8482999313aSBarry Smith    Input Parameters:
8492999313aSBarry Smith +  snes - the SNES context
8502999313aSBarry Smith -  ksp - the KSP context
8512999313aSBarry Smith 
8522999313aSBarry Smith    Notes:
8532999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
8542999313aSBarry Smith    so this routine is rarely needed.
8552999313aSBarry Smith 
8562999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
8572999313aSBarry Smith    decreased by one.
8582999313aSBarry Smith 
8592999313aSBarry Smith    Level: developer
8602999313aSBarry Smith 
8612999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
8622999313aSBarry Smith 
8632999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
8642999313aSBarry Smith @*/
8652999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
8662999313aSBarry Smith {
8672999313aSBarry Smith   PetscErrorCode ierr;
8682999313aSBarry Smith 
8692999313aSBarry Smith   PetscFunctionBegin;
8702999313aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8712999313aSBarry Smith   PetscValidHeaderSpecific(ksp,KSP_COOKIE,2);
8722999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
8737dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
874906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
8752999313aSBarry Smith   snes->ksp = ksp;
8762999313aSBarry Smith   PetscFunctionReturn(0);
8772999313aSBarry Smith }
8782999313aSBarry Smith 
8797adad957SLisandro Dalcin #if 0
8802999313aSBarry Smith #undef __FUNCT__
8814a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
8826849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
883e24b481bSBarry Smith {
884e24b481bSBarry Smith   PetscFunctionBegin;
885e24b481bSBarry Smith   PetscFunctionReturn(0);
886e24b481bSBarry Smith }
8877adad957SLisandro Dalcin #endif
888e24b481bSBarry Smith 
8899b94acceSBarry Smith /* -----------------------------------------------------------*/
8904a2ae208SSatish Balay #undef __FUNCT__
8914a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
89252baeb72SSatish Balay /*@
8939b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
8949b94acceSBarry Smith 
895c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
896c7afd0dbSLois Curfman McInnes 
897c7afd0dbSLois Curfman McInnes    Input Parameters:
898906ed7ccSBarry Smith .  comm - MPI communicator
8999b94acceSBarry Smith 
9009b94acceSBarry Smith    Output Parameter:
9019b94acceSBarry Smith .  outsnes - the new SNES context
9029b94acceSBarry Smith 
903c7afd0dbSLois Curfman McInnes    Options Database Keys:
904c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
905c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
906c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
907c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
908c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
909c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
910c1f60f51SBarry Smith 
91136851e7fSLois Curfman McInnes    Level: beginner
91236851e7fSLois Curfman McInnes 
9139b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
9149b94acceSBarry Smith 
915a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
916a8054027SBarry Smith 
9179b94acceSBarry Smith @*/
91863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
9199b94acceSBarry Smith {
920dfbe8321SBarry Smith   PetscErrorCode      ierr;
9219b94acceSBarry Smith   SNES                snes;
922fa9f3622SBarry Smith   SNESKSPEW           *kctx;
92337fcc0dbSBarry Smith 
9243a40ed3dSBarry Smith   PetscFunctionBegin;
925ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
9268ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
9278ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
9288ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
9298ba1e511SMatthew Knepley #endif
9308ba1e511SMatthew Knepley 
931e7788613SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
9327adad957SLisandro Dalcin 
93385385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
9349b94acceSBarry Smith   snes->max_its           = 50;
9359750a799SBarry Smith   snes->max_funcs	  = 10000;
9369b94acceSBarry Smith   snes->norm		  = 0.0;
937b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
938b4874afaSBarry Smith   snes->ttol              = 0.0;
93970441072SBarry Smith   snes->abstol		  = 1.e-50;
9409b94acceSBarry Smith   snes->xtol		  = 1.e-8;
9414b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
9429b94acceSBarry Smith   snes->nfuncs            = 0;
94350ffb88aSMatthew Knepley   snes->numFailures       = 0;
94450ffb88aSMatthew Knepley   snes->maxFailures       = 1;
9457a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
946e35cf81dSBarry Smith   snes->lagjacobian       = 1;
947a8054027SBarry Smith   snes->lagpreconditioner = 1;
948639f9d9dSBarry Smith   snes->numbermonitors    = 0;
9499b94acceSBarry Smith   snes->data              = 0;
9504dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
951186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
9526f24a144SLois Curfman McInnes   snes->vwork             = 0;
9536f24a144SLois Curfman McInnes   snes->nwork             = 0;
954758f92a0SBarry Smith   snes->conv_hist_len     = 0;
955758f92a0SBarry Smith   snes->conv_hist_max     = 0;
956758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
957758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
958758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
959184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
9609b94acceSBarry Smith 
9613d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
9623d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
9633d4c4710SBarry Smith 
9649b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
96538f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
9669b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
9679b94acceSBarry Smith   kctx->version     = 2;
9689b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
9699b94acceSBarry Smith                              this was too large for some test cases */
97075567043SBarry Smith   kctx->rtol_last   = 0.0;
9719b94acceSBarry Smith   kctx->rtol_max    = .9;
9729b94acceSBarry Smith   kctx->gamma       = 1.0;
97371f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
97471f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
9759b94acceSBarry Smith   kctx->threshold   = .1;
97675567043SBarry Smith   kctx->lresid_last = 0.0;
97775567043SBarry Smith   kctx->norm_last   = 0.0;
9789b94acceSBarry Smith 
9799b94acceSBarry Smith   *outsnes = snes;
98000036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
9813a40ed3dSBarry Smith   PetscFunctionReturn(0);
9829b94acceSBarry Smith }
9839b94acceSBarry Smith 
9844a2ae208SSatish Balay #undef __FUNCT__
9854a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
9869b94acceSBarry Smith /*@C
9879b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
9889b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
9899b94acceSBarry Smith    equations.
9909b94acceSBarry Smith 
991fee21e36SBarry Smith    Collective on SNES
992fee21e36SBarry Smith 
993c7afd0dbSLois Curfman McInnes    Input Parameters:
994c7afd0dbSLois Curfman McInnes +  snes - the SNES context
995c7afd0dbSLois Curfman McInnes .  r - vector to store function value
996de044059SHong Zhang .  func - function evaluation routine
997c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
998c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
9999b94acceSBarry Smith 
1000c7afd0dbSLois Curfman McInnes    Calling sequence of func:
10018d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1002c7afd0dbSLois Curfman McInnes 
1003313e4042SLois Curfman McInnes .  f - function vector
1004c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
10059b94acceSBarry Smith 
10069b94acceSBarry Smith    Notes:
10079b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
10089b94acceSBarry Smith $      f'(x) x = -f(x),
1009c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
10109b94acceSBarry Smith 
101136851e7fSLois Curfman McInnes    Level: beginner
101236851e7fSLois Curfman McInnes 
10139b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
10149b94acceSBarry Smith 
1015a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
10169b94acceSBarry Smith @*/
101763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
10189b94acceSBarry Smith {
101985385478SLisandro Dalcin   PetscErrorCode ierr;
10203a40ed3dSBarry Smith   PetscFunctionBegin;
10214482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10224482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
1023c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
102485385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
102585385478SLisandro Dalcin   if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); }
1026e7788613SBarry Smith   snes->ops->computefunction = func;
102785385478SLisandro Dalcin   snes->vec_func             = r;
10289b94acceSBarry Smith   snes->funP                 = ctx;
10293a40ed3dSBarry Smith   PetscFunctionReturn(0);
10309b94acceSBarry Smith }
10319b94acceSBarry Smith 
10323ab0aad5SBarry Smith /* --------------------------------------------------------------- */
10333ab0aad5SBarry Smith #undef __FUNCT__
10341096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
10351096aae1SMatthew Knepley /*@C
10361096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
10371096aae1SMatthew Knepley    it assumes a zero right hand side.
10381096aae1SMatthew Knepley 
10391096aae1SMatthew Knepley    Collective on SNES
10401096aae1SMatthew Knepley 
10411096aae1SMatthew Knepley    Input Parameter:
10421096aae1SMatthew Knepley .  snes - the SNES context
10431096aae1SMatthew Knepley 
10441096aae1SMatthew Knepley    Output Parameter:
1045bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
10461096aae1SMatthew Knepley 
10471096aae1SMatthew Knepley    Level: intermediate
10481096aae1SMatthew Knepley 
10491096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
10501096aae1SMatthew Knepley 
105185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
10521096aae1SMatthew Knepley @*/
10531096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
10541096aae1SMatthew Knepley {
10551096aae1SMatthew Knepley   PetscFunctionBegin;
10561096aae1SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10571096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
105885385478SLisandro Dalcin   *rhs = snes->vec_rhs;
10591096aae1SMatthew Knepley   PetscFunctionReturn(0);
10601096aae1SMatthew Knepley }
10611096aae1SMatthew Knepley 
10621096aae1SMatthew Knepley #undef __FUNCT__
10634a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
10649b94acceSBarry Smith /*@
106536851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
10669b94acceSBarry Smith                          SNESSetFunction().
10679b94acceSBarry Smith 
1068c7afd0dbSLois Curfman McInnes    Collective on SNES
1069c7afd0dbSLois Curfman McInnes 
10709b94acceSBarry Smith    Input Parameters:
1071c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1072c7afd0dbSLois Curfman McInnes -  x - input vector
10739b94acceSBarry Smith 
10749b94acceSBarry Smith    Output Parameter:
10753638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
10769b94acceSBarry Smith 
10771bffabb2SLois Curfman McInnes    Notes:
107836851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
107936851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
108036851e7fSLois Curfman McInnes    themselves.
108136851e7fSLois Curfman McInnes 
108236851e7fSLois Curfman McInnes    Level: developer
108336851e7fSLois Curfman McInnes 
10849b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
10859b94acceSBarry Smith 
1086a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
10879b94acceSBarry Smith @*/
108863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
10899b94acceSBarry Smith {
1090dfbe8321SBarry Smith   PetscErrorCode ierr;
10919b94acceSBarry Smith 
10923a40ed3dSBarry Smith   PetscFunctionBegin;
10934482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10944482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
10954482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
1096c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1097c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1098184914b5SBarry Smith 
1099d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1100e7788613SBarry Smith   if (snes->ops->computefunction) {
1101d64ed03dSBarry Smith     PetscStackPush("SNES user function");
1102e9a2bbcdSBarry Smith     CHKMEMQ;
1103e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
1104e9a2bbcdSBarry Smith     CHKMEMQ;
1105d64ed03dSBarry Smith     PetscStackPop;
1106d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
110719717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
110819717074SBarry Smith     }
1109d5e45103SBarry Smith     CHKERRQ(ierr);
111085385478SLisandro Dalcin   } else if (snes->vec_rhs) {
11111096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
11121096aae1SMatthew Knepley   } else {
11131096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
11141096aae1SMatthew Knepley   }
111585385478SLisandro Dalcin   if (snes->vec_rhs) {
111685385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
11173ab0aad5SBarry Smith   }
1118ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1119d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
11203a40ed3dSBarry Smith   PetscFunctionReturn(0);
11219b94acceSBarry Smith }
11229b94acceSBarry Smith 
11234a2ae208SSatish Balay #undef __FUNCT__
11244a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
112562fef451SLois Curfman McInnes /*@
112662fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
112762fef451SLois Curfman McInnes    set with SNESSetJacobian().
112862fef451SLois Curfman McInnes 
1129c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1130c7afd0dbSLois Curfman McInnes 
113162fef451SLois Curfman McInnes    Input Parameters:
1132c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1133c7afd0dbSLois Curfman McInnes -  x - input vector
113462fef451SLois Curfman McInnes 
113562fef451SLois Curfman McInnes    Output Parameters:
1136c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
113762fef451SLois Curfman McInnes .  B - optional preconditioning matrix
11382b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1139fee21e36SBarry Smith 
1140e35cf81dSBarry Smith   Options Database Keys:
1141e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1142e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1143e35cf81dSBarry Smith 
114462fef451SLois Curfman McInnes    Notes:
114562fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
114662fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
114762fef451SLois Curfman McInnes 
114894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1149dc5a77f8SLois Curfman McInnes    flag parameter.
115062fef451SLois Curfman McInnes 
115136851e7fSLois Curfman McInnes    Level: developer
115236851e7fSLois Curfman McInnes 
115362fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
115462fef451SLois Curfman McInnes 
1155e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
115662fef451SLois Curfman McInnes @*/
115763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
11589b94acceSBarry Smith {
1159dfbe8321SBarry Smith   PetscErrorCode ierr;
1160ebd3b9afSBarry Smith   PetscTruth     flag;
11613a40ed3dSBarry Smith 
11623a40ed3dSBarry Smith   PetscFunctionBegin;
11634482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11644482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
11654482741eSBarry Smith   PetscValidPointer(flg,5);
1166c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1167e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1168ebd3b9afSBarry Smith 
1169ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1170ebd3b9afSBarry Smith 
1171fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1172fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1173fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1174fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1175e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1176e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1177ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1178ebd3b9afSBarry Smith     if (flag) {
1179ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1180ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1181ebd3b9afSBarry Smith     }
1182e35cf81dSBarry Smith     PetscFunctionReturn(0);
1183e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1184e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1185e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1186ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1187ebd3b9afSBarry Smith     if (flag) {
1188ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1189ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1190ebd3b9afSBarry Smith     }
1191e35cf81dSBarry Smith     PetscFunctionReturn(0);
1192e35cf81dSBarry Smith   }
1193e35cf81dSBarry Smith 
1194c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1195e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1196d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1197dc67937bSBarry Smith   CHKMEMQ;
1198e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1199dc67937bSBarry Smith   CHKMEMQ;
1200d64ed03dSBarry Smith   PetscStackPop;
1201d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1202a8054027SBarry Smith 
12033b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
12043b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
12053b4f5425SBarry Smith     snes->lagpreconditioner = -1;
12063b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1207a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1208a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1209a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1210a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1211a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1212a8054027SBarry Smith   }
1213a8054027SBarry Smith 
12146d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
12156ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
12166ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
12173a40ed3dSBarry Smith   PetscFunctionReturn(0);
12189b94acceSBarry Smith }
12199b94acceSBarry Smith 
12204a2ae208SSatish Balay #undef __FUNCT__
12214a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
12229b94acceSBarry Smith /*@C
12239b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1224044dda88SLois Curfman McInnes    location to store the matrix.
12259b94acceSBarry Smith 
1226c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1227c7afd0dbSLois Curfman McInnes 
12289b94acceSBarry Smith    Input Parameters:
1229c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12309b94acceSBarry Smith .  A - Jacobian matrix
12319b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
12329b94acceSBarry Smith .  func - Jacobian evaluation routine
1233c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
12342cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
12359b94acceSBarry Smith 
12369b94acceSBarry Smith    Calling sequence of func:
12378d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12389b94acceSBarry Smith 
1239c7afd0dbSLois Curfman McInnes +  x - input vector
12409b94acceSBarry Smith .  A - Jacobian matrix
12419b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1242ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
12432b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1244c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
12459b94acceSBarry Smith 
12469b94acceSBarry Smith    Notes:
124794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
12482cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1249ac21db08SLois Curfman McInnes 
1250ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
12519b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
12529b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12539b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12549b94acceSBarry Smith    throughout the global iterations.
12559b94acceSBarry Smith 
125616913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
125716913363SBarry Smith    each matrix.
125816913363SBarry Smith 
125936851e7fSLois Curfman McInnes    Level: beginner
126036851e7fSLois Curfman McInnes 
12619b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
12629b94acceSBarry Smith 
12633ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
12649b94acceSBarry Smith @*/
126563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
12669b94acceSBarry Smith {
1267dfbe8321SBarry Smith   PetscErrorCode ierr;
12683a7fca6bSBarry Smith 
12693a40ed3dSBarry Smith   PetscFunctionBegin;
12704482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12714482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
12724482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1273c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
127406975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1275e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
12763a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
12773a7fca6bSBarry Smith   if (A) {
12787dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
12793a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12809b94acceSBarry Smith     snes->jacobian = A;
12813a7fca6bSBarry Smith   }
12823a7fca6bSBarry Smith   if (B) {
12837dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
12843a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12859b94acceSBarry Smith     snes->jacobian_pre = B;
12863a7fca6bSBarry Smith   }
12873a40ed3dSBarry Smith   PetscFunctionReturn(0);
12889b94acceSBarry Smith }
128962fef451SLois Curfman McInnes 
12904a2ae208SSatish Balay #undef __FUNCT__
12914a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1292c2aafc4cSSatish Balay /*@C
1293b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1294b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1295b4fd4287SBarry Smith 
1296c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1297c7afd0dbSLois Curfman McInnes 
1298b4fd4287SBarry Smith    Input Parameter:
1299b4fd4287SBarry Smith .  snes - the nonlinear solver context
1300b4fd4287SBarry Smith 
1301b4fd4287SBarry Smith    Output Parameters:
1302c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1303b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
130470e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
130570e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1306fee21e36SBarry Smith 
130736851e7fSLois Curfman McInnes    Level: advanced
130836851e7fSLois Curfman McInnes 
1309b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1310b4fd4287SBarry Smith @*/
131170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1312b4fd4287SBarry Smith {
13133a40ed3dSBarry Smith   PetscFunctionBegin;
13144482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1315b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1316b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1317e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
131870e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
13193a40ed3dSBarry Smith   PetscFunctionReturn(0);
1320b4fd4287SBarry Smith }
1321b4fd4287SBarry Smith 
13229b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13239b94acceSBarry Smith 
13244a2ae208SSatish Balay #undef __FUNCT__
13254a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
13269b94acceSBarry Smith /*@
13279b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1328272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13299b94acceSBarry Smith 
1330fee21e36SBarry Smith    Collective on SNES
1331fee21e36SBarry Smith 
1332c7afd0dbSLois Curfman McInnes    Input Parameters:
133370e92668SMatthew Knepley .  snes - the SNES context
1334c7afd0dbSLois Curfman McInnes 
1335272ac6f2SLois Curfman McInnes    Notes:
1336272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1337272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1338272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1339272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1340272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1341272ac6f2SLois Curfman McInnes 
134236851e7fSLois Curfman McInnes    Level: advanced
134336851e7fSLois Curfman McInnes 
13449b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13459b94acceSBarry Smith 
13469b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
13479b94acceSBarry Smith @*/
134870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
13499b94acceSBarry Smith {
1350dfbe8321SBarry Smith   PetscErrorCode ierr;
13513a40ed3dSBarry Smith 
13523a40ed3dSBarry Smith   PetscFunctionBegin;
13534482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13544dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
13559b94acceSBarry Smith 
13567adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
135785385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
135885385478SLisandro Dalcin   }
135985385478SLisandro Dalcin 
136085385478SLisandro Dalcin   if (!snes->vec_func && !snes->vec_rhs) {
13611096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13621096aae1SMatthew Knepley   }
136385385478SLisandro Dalcin   if (!snes->ops->computefunction && !snes->vec_rhs) {
13641096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13651096aae1SMatthew Knepley   }
1366a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
136729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1368a8c6a408SBarry Smith   }
1369a703fe33SLois Curfman McInnes 
1370b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1371b710008aSBarry Smith 
1372410397dcSLisandro Dalcin   if (snes->ops->setup) {
1373410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1374410397dcSLisandro Dalcin   }
13757aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
13763a40ed3dSBarry Smith   PetscFunctionReturn(0);
13779b94acceSBarry Smith }
13789b94acceSBarry Smith 
13794a2ae208SSatish Balay #undef __FUNCT__
13804a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
138152baeb72SSatish Balay /*@
13829b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13839b94acceSBarry Smith    with SNESCreate().
13849b94acceSBarry Smith 
1385c7afd0dbSLois Curfman McInnes    Collective on SNES
1386c7afd0dbSLois Curfman McInnes 
13879b94acceSBarry Smith    Input Parameter:
13889b94acceSBarry Smith .  snes - the SNES context
13899b94acceSBarry Smith 
139036851e7fSLois Curfman McInnes    Level: beginner
139136851e7fSLois Curfman McInnes 
13929b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
13939b94acceSBarry Smith 
139463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
13959b94acceSBarry Smith @*/
139663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
13979b94acceSBarry Smith {
13986849ba73SBarry Smith   PetscErrorCode ierr;
13993a40ed3dSBarry Smith 
14003a40ed3dSBarry Smith   PetscFunctionBegin;
14014482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
14027adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1403d4bb536fSBarry Smith 
1404be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
14050f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1406e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
140785385478SLisandro Dalcin 
140885385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
140985385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
141085385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
14113a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
14123a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
14131cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
141485385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1415522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1416a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
14177f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1418a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
14193a40ed3dSBarry Smith   PetscFunctionReturn(0);
14209b94acceSBarry Smith }
14219b94acceSBarry Smith 
14229b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14239b94acceSBarry Smith 
14244a2ae208SSatish Balay #undef __FUNCT__
1425a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1426a8054027SBarry Smith /*@
1427a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1428a8054027SBarry Smith 
1429a8054027SBarry Smith    Collective on SNES
1430a8054027SBarry Smith 
1431a8054027SBarry Smith    Input Parameters:
1432a8054027SBarry Smith +  snes - the SNES context
1433a8054027SBarry 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
14343b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1435a8054027SBarry Smith 
1436a8054027SBarry Smith    Options Database Keys:
1437a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1438a8054027SBarry Smith 
1439a8054027SBarry Smith    Notes:
1440a8054027SBarry Smith    The default is 1
1441a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1442a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1443a8054027SBarry Smith 
1444a8054027SBarry Smith    Level: intermediate
1445a8054027SBarry Smith 
1446a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1447a8054027SBarry Smith 
1448e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1449a8054027SBarry Smith 
1450a8054027SBarry Smith @*/
1451a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1452a8054027SBarry Smith {
1453a8054027SBarry Smith   PetscFunctionBegin;
1454a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
14553b4f5425SBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1456a8054027SBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1457a8054027SBarry Smith   snes->lagpreconditioner = lag;
1458a8054027SBarry Smith   PetscFunctionReturn(0);
1459a8054027SBarry Smith }
1460a8054027SBarry Smith 
1461a8054027SBarry Smith #undef __FUNCT__
1462a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1463a8054027SBarry Smith /*@
1464a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1465a8054027SBarry Smith 
1466a8054027SBarry Smith    Collective on SNES
1467a8054027SBarry Smith 
1468a8054027SBarry Smith    Input Parameter:
1469a8054027SBarry Smith .  snes - the SNES context
1470a8054027SBarry Smith 
1471a8054027SBarry Smith    Output Parameter:
1472a8054027SBarry 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
14733b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1474a8054027SBarry Smith 
1475a8054027SBarry Smith    Options Database Keys:
1476a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1477a8054027SBarry Smith 
1478a8054027SBarry Smith    Notes:
1479a8054027SBarry Smith    The default is 1
1480a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1481a8054027SBarry Smith 
1482a8054027SBarry Smith    Level: intermediate
1483a8054027SBarry Smith 
1484a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1485a8054027SBarry Smith 
1486a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1487a8054027SBarry Smith 
1488a8054027SBarry Smith @*/
1489a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1490a8054027SBarry Smith {
1491a8054027SBarry Smith   PetscFunctionBegin;
1492a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1493a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1494a8054027SBarry Smith   PetscFunctionReturn(0);
1495a8054027SBarry Smith }
1496a8054027SBarry Smith 
1497a8054027SBarry Smith #undef __FUNCT__
1498e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1499e35cf81dSBarry Smith /*@
1500e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1501e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1502e35cf81dSBarry Smith 
1503e35cf81dSBarry Smith    Collective on SNES
1504e35cf81dSBarry Smith 
1505e35cf81dSBarry Smith    Input Parameters:
1506e35cf81dSBarry Smith +  snes - the SNES context
1507e35cf81dSBarry 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
1508fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1509e35cf81dSBarry Smith 
1510e35cf81dSBarry Smith    Options Database Keys:
1511e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1512e35cf81dSBarry Smith 
1513e35cf81dSBarry Smith    Notes:
1514e35cf81dSBarry Smith    The default is 1
1515e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1516fe3ffe1eSBarry 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
1517fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1518e35cf81dSBarry Smith 
1519e35cf81dSBarry Smith    Level: intermediate
1520e35cf81dSBarry Smith 
1521e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1522e35cf81dSBarry Smith 
1523e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1524e35cf81dSBarry Smith 
1525e35cf81dSBarry Smith @*/
1526e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1527e35cf81dSBarry Smith {
1528e35cf81dSBarry Smith   PetscFunctionBegin;
1529e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1530fe3ffe1eSBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1531e35cf81dSBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1532e35cf81dSBarry Smith   snes->lagjacobian = lag;
1533e35cf81dSBarry Smith   PetscFunctionReturn(0);
1534e35cf81dSBarry Smith }
1535e35cf81dSBarry Smith 
1536e35cf81dSBarry Smith #undef __FUNCT__
1537e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1538e35cf81dSBarry Smith /*@
1539e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1540e35cf81dSBarry Smith 
1541e35cf81dSBarry Smith    Collective on SNES
1542e35cf81dSBarry Smith 
1543e35cf81dSBarry Smith    Input Parameter:
1544e35cf81dSBarry Smith .  snes - the SNES context
1545e35cf81dSBarry Smith 
1546e35cf81dSBarry Smith    Output Parameter:
1547e35cf81dSBarry 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
1548e35cf81dSBarry Smith          the Jacobian is built etc.
1549e35cf81dSBarry Smith 
1550e35cf81dSBarry Smith    Options Database Keys:
1551e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1552e35cf81dSBarry Smith 
1553e35cf81dSBarry Smith    Notes:
1554e35cf81dSBarry Smith    The default is 1
1555e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1556e35cf81dSBarry Smith 
1557e35cf81dSBarry Smith    Level: intermediate
1558e35cf81dSBarry Smith 
1559e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1560e35cf81dSBarry Smith 
1561e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1562e35cf81dSBarry Smith 
1563e35cf81dSBarry Smith @*/
1564e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1565e35cf81dSBarry Smith {
1566e35cf81dSBarry Smith   PetscFunctionBegin;
1567e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1568e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1569e35cf81dSBarry Smith   PetscFunctionReturn(0);
1570e35cf81dSBarry Smith }
1571e35cf81dSBarry Smith 
1572e35cf81dSBarry Smith #undef __FUNCT__
15734a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15749b94acceSBarry Smith /*@
1575d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15769b94acceSBarry Smith 
1577c7afd0dbSLois Curfman McInnes    Collective on SNES
1578c7afd0dbSLois Curfman McInnes 
15799b94acceSBarry Smith    Input Parameters:
1580c7afd0dbSLois Curfman McInnes +  snes - the SNES context
158170441072SBarry Smith .  abstol - absolute convergence tolerance
158233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
158333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
158433174efeSLois Curfman McInnes            of the change in the solution between steps
158533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1586c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1587fee21e36SBarry Smith 
158833174efeSLois Curfman McInnes    Options Database Keys:
158970441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1590c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1591c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1592c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1593c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15949b94acceSBarry Smith 
1595d7a720efSLois Curfman McInnes    Notes:
15969b94acceSBarry Smith    The default maximum number of iterations is 50.
15979b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15989b94acceSBarry Smith 
159936851e7fSLois Curfman McInnes    Level: intermediate
160036851e7fSLois Curfman McInnes 
160133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
16029b94acceSBarry Smith 
16032492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
16049b94acceSBarry Smith @*/
160563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
16069b94acceSBarry Smith {
16073a40ed3dSBarry Smith   PetscFunctionBegin;
16084482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
160970441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1610d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1611d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1612d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1613d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
16143a40ed3dSBarry Smith   PetscFunctionReturn(0);
16159b94acceSBarry Smith }
16169b94acceSBarry Smith 
16174a2ae208SSatish Balay #undef __FUNCT__
16184a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
16199b94acceSBarry Smith /*@
162033174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
162133174efeSLois Curfman McInnes 
1622c7afd0dbSLois Curfman McInnes    Not Collective
1623c7afd0dbSLois Curfman McInnes 
162433174efeSLois Curfman McInnes    Input Parameters:
1625c7afd0dbSLois Curfman McInnes +  snes - the SNES context
162685385478SLisandro Dalcin .  atol - absolute convergence tolerance
162733174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
162833174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
162933174efeSLois Curfman McInnes            of the change in the solution between steps
163033174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1631c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1632fee21e36SBarry Smith 
163333174efeSLois Curfman McInnes    Notes:
163433174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
163533174efeSLois Curfman McInnes 
163636851e7fSLois Curfman McInnes    Level: intermediate
163736851e7fSLois Curfman McInnes 
163833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
163933174efeSLois Curfman McInnes 
164033174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
164133174efeSLois Curfman McInnes @*/
164285385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
164333174efeSLois Curfman McInnes {
16443a40ed3dSBarry Smith   PetscFunctionBegin;
16454482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
164685385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
164733174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
164833174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
164933174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
165033174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
16513a40ed3dSBarry Smith   PetscFunctionReturn(0);
165233174efeSLois Curfman McInnes }
165333174efeSLois Curfman McInnes 
16544a2ae208SSatish Balay #undef __FUNCT__
16554a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
165633174efeSLois Curfman McInnes /*@
16579b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16589b94acceSBarry Smith 
1659fee21e36SBarry Smith    Collective on SNES
1660fee21e36SBarry Smith 
1661c7afd0dbSLois Curfman McInnes    Input Parameters:
1662c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1663c7afd0dbSLois Curfman McInnes -  tol - tolerance
1664c7afd0dbSLois Curfman McInnes 
16659b94acceSBarry Smith    Options Database Key:
1666c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16679b94acceSBarry Smith 
166836851e7fSLois Curfman McInnes    Level: intermediate
166936851e7fSLois Curfman McInnes 
16709b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16719b94acceSBarry Smith 
16722492ecdbSBarry Smith .seealso: SNESSetTolerances()
16739b94acceSBarry Smith @*/
167463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16759b94acceSBarry Smith {
16763a40ed3dSBarry Smith   PetscFunctionBegin;
16774482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16789b94acceSBarry Smith   snes->deltatol = tol;
16793a40ed3dSBarry Smith   PetscFunctionReturn(0);
16809b94acceSBarry Smith }
16819b94acceSBarry Smith 
1682df9fa365SBarry Smith /*
1683df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1684df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1685df9fa365SBarry Smith    macros instead of functions
1686df9fa365SBarry Smith */
16874a2ae208SSatish Balay #undef __FUNCT__
1688a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1689a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1690ce1608b8SBarry Smith {
1691dfbe8321SBarry Smith   PetscErrorCode ierr;
1692ce1608b8SBarry Smith 
1693ce1608b8SBarry Smith   PetscFunctionBegin;
16944482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1695a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1696ce1608b8SBarry Smith   PetscFunctionReturn(0);
1697ce1608b8SBarry Smith }
1698ce1608b8SBarry Smith 
16994a2ae208SSatish Balay #undef __FUNCT__
1700a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1701a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1702df9fa365SBarry Smith {
1703dfbe8321SBarry Smith   PetscErrorCode ierr;
1704df9fa365SBarry Smith 
1705df9fa365SBarry Smith   PetscFunctionBegin;
1706a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1707df9fa365SBarry Smith   PetscFunctionReturn(0);
1708df9fa365SBarry Smith }
1709df9fa365SBarry Smith 
17104a2ae208SSatish Balay #undef __FUNCT__
1711a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1712a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1713df9fa365SBarry Smith {
1714dfbe8321SBarry Smith   PetscErrorCode ierr;
1715df9fa365SBarry Smith 
1716df9fa365SBarry Smith   PetscFunctionBegin;
1717a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1718df9fa365SBarry Smith   PetscFunctionReturn(0);
1719df9fa365SBarry Smith }
1720df9fa365SBarry Smith 
1721b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1722b271bb04SBarry Smith #undef __FUNCT__
1723b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
1724b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1725b271bb04SBarry Smith {
1726b271bb04SBarry Smith   PetscDrawLG      lg;
1727b271bb04SBarry Smith   PetscErrorCode   ierr;
1728b271bb04SBarry Smith   PetscReal        x,y,per;
1729b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1730b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1731b271bb04SBarry Smith   PetscDraw        draw;
1732b271bb04SBarry Smith   PetscFunctionBegin;
1733b271bb04SBarry Smith   if (!monctx) {
1734b271bb04SBarry Smith     MPI_Comm    comm;
1735b271bb04SBarry Smith 
1736b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1737b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1738b271bb04SBarry Smith   }
1739b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1740b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1741b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1742b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1743b271bb04SBarry Smith   x = (PetscReal) n;
1744b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1745b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1746b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1747b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1748b271bb04SBarry Smith   }
1749b271bb04SBarry Smith 
1750b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1751b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1752b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1753b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1754b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1755b271bb04SBarry Smith   x = (PetscReal) n;
1756b271bb04SBarry Smith   y = 100.0*per;
1757b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1758b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1759b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1760b271bb04SBarry Smith   }
1761b271bb04SBarry Smith 
1762b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1763b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1764b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1765b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1766b271bb04SBarry Smith   x = (PetscReal) n;
1767b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1768b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1769b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1770b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1771b271bb04SBarry Smith   }
1772b271bb04SBarry Smith 
1773b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1774b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1775b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1776b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1777b271bb04SBarry Smith   x = (PetscReal) n;
1778b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1779b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1780b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1781b271bb04SBarry Smith   }
1782b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1783b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1784b271bb04SBarry Smith   }
1785b271bb04SBarry Smith   prev = rnorm;
1786b271bb04SBarry Smith   PetscFunctionReturn(0);
1787b271bb04SBarry Smith }
1788b271bb04SBarry Smith 
1789b271bb04SBarry Smith #undef __FUNCT__
1790b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
1791b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1792b271bb04SBarry Smith {
1793b271bb04SBarry Smith   PetscErrorCode ierr;
1794b271bb04SBarry Smith 
1795b271bb04SBarry Smith   PetscFunctionBegin;
1796b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1797b271bb04SBarry Smith   PetscFunctionReturn(0);
1798b271bb04SBarry Smith }
1799b271bb04SBarry Smith 
1800b271bb04SBarry Smith #undef __FUNCT__
1801b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
1802b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw)
1803b271bb04SBarry Smith {
1804b271bb04SBarry Smith   PetscErrorCode ierr;
1805b271bb04SBarry Smith 
1806b271bb04SBarry Smith   PetscFunctionBegin;
1807b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1808b271bb04SBarry Smith   PetscFunctionReturn(0);
1809b271bb04SBarry Smith }
1810b271bb04SBarry Smith 
18119b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
18129b94acceSBarry Smith 
18134a2ae208SSatish Balay #undef __FUNCT__
1814a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
18159b94acceSBarry Smith /*@C
1816a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
18179b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
18189b94acceSBarry Smith    progress.
18199b94acceSBarry Smith 
1820fee21e36SBarry Smith    Collective on SNES
1821fee21e36SBarry Smith 
1822c7afd0dbSLois Curfman McInnes    Input Parameters:
1823c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1824c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1825b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1826e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1827b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1828b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
18299b94acceSBarry Smith 
1830c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1831a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1832c7afd0dbSLois Curfman McInnes 
1833c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1834c7afd0dbSLois Curfman McInnes .    its - iteration number
1835c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
183640a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
18379b94acceSBarry Smith 
18389665c990SLois Curfman McInnes    Options Database Keys:
1839a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1840a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1841a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1842a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1843c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1844a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1845c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1846c7afd0dbSLois Curfman McInnes                             the options database.
18479665c990SLois Curfman McInnes 
1848639f9d9dSBarry Smith    Notes:
18496bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1850a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
18516bc08f3fSLois Curfman McInnes    order in which they were set.
1852639f9d9dSBarry Smith 
1853025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1854025f1a04SBarry Smith 
185536851e7fSLois Curfman McInnes    Level: intermediate
185636851e7fSLois Curfman McInnes 
18579b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
18589b94acceSBarry Smith 
1859a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
18609b94acceSBarry Smith @*/
1861b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
18629b94acceSBarry Smith {
1863b90d0a6eSBarry Smith   PetscInt i;
1864b90d0a6eSBarry Smith 
18653a40ed3dSBarry Smith   PetscFunctionBegin;
18664482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1867639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
186829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1869639f9d9dSBarry Smith   }
1870b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1871b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1872b90d0a6eSBarry Smith 
1873b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1874b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1875b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1876b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1877b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1878b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1879b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1880b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1881b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1882b90d0a6eSBarry Smith         }
1883b90d0a6eSBarry Smith       }
1884b90d0a6eSBarry Smith     }
1885b90d0a6eSBarry Smith   }
1886b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1887b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1888639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
18893a40ed3dSBarry Smith   PetscFunctionReturn(0);
18909b94acceSBarry Smith }
18919b94acceSBarry Smith 
18924a2ae208SSatish Balay #undef __FUNCT__
1893a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
18945cd90555SBarry Smith /*@C
1895a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
18965cd90555SBarry Smith 
1897c7afd0dbSLois Curfman McInnes    Collective on SNES
1898c7afd0dbSLois Curfman McInnes 
18995cd90555SBarry Smith    Input Parameters:
19005cd90555SBarry Smith .  snes - the SNES context
19015cd90555SBarry Smith 
19021a480d89SAdministrator    Options Database Key:
1903a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1904a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1905c7afd0dbSLois Curfman McInnes     set via the options database
19065cd90555SBarry Smith 
19075cd90555SBarry Smith    Notes:
19085cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
19095cd90555SBarry Smith 
191036851e7fSLois Curfman McInnes    Level: intermediate
191136851e7fSLois Curfman McInnes 
19125cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
19135cd90555SBarry Smith 
1914a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
19155cd90555SBarry Smith @*/
1916a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
19175cd90555SBarry Smith {
1918d952e501SBarry Smith   PetscErrorCode ierr;
1919d952e501SBarry Smith   PetscInt       i;
1920d952e501SBarry Smith 
19215cd90555SBarry Smith   PetscFunctionBegin;
19224482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1923d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1924d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1925d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1926d952e501SBarry Smith     }
1927d952e501SBarry Smith   }
19285cd90555SBarry Smith   snes->numbermonitors = 0;
19295cd90555SBarry Smith   PetscFunctionReturn(0);
19305cd90555SBarry Smith }
19315cd90555SBarry Smith 
19324a2ae208SSatish Balay #undef __FUNCT__
19334a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
19349b94acceSBarry Smith /*@C
19359b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
19369b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
19379b94acceSBarry Smith 
1938fee21e36SBarry Smith    Collective on SNES
1939fee21e36SBarry Smith 
1940c7afd0dbSLois Curfman McInnes    Input Parameters:
1941c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1942c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
19437f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
19447f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
19459b94acceSBarry Smith 
1946c7afd0dbSLois Curfman McInnes    Calling sequence of func:
194706ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1948c7afd0dbSLois Curfman McInnes 
1949c7afd0dbSLois Curfman McInnes +    snes - the SNES context
195006ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1951c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1952184914b5SBarry Smith .    reason - reason for convergence/divergence
1953c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
19544b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
19554b27c08aSLois Curfman McInnes -    f - 2-norm of function
19569b94acceSBarry Smith 
195736851e7fSLois Curfman McInnes    Level: advanced
195836851e7fSLois Curfman McInnes 
19599b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
19609b94acceSBarry Smith 
196185385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
19629b94acceSBarry Smith @*/
19637f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
19649b94acceSBarry Smith {
19657f7931b9SBarry Smith   PetscErrorCode ierr;
19667f7931b9SBarry Smith 
19673a40ed3dSBarry Smith   PetscFunctionBegin;
19684482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
196985385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
19707f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
19717f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
19727f7931b9SBarry Smith   }
197385385478SLisandro Dalcin   snes->ops->converged        = func;
19747f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
197585385478SLisandro Dalcin   snes->cnvP                  = cctx;
19763a40ed3dSBarry Smith   PetscFunctionReturn(0);
19779b94acceSBarry Smith }
19789b94acceSBarry Smith 
19794a2ae208SSatish Balay #undef __FUNCT__
19804a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
198152baeb72SSatish Balay /*@
1982184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1983184914b5SBarry Smith 
1984184914b5SBarry Smith    Not Collective
1985184914b5SBarry Smith 
1986184914b5SBarry Smith    Input Parameter:
1987184914b5SBarry Smith .  snes - the SNES context
1988184914b5SBarry Smith 
1989184914b5SBarry Smith    Output Parameter:
19904d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
1991184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1992184914b5SBarry Smith 
1993184914b5SBarry Smith    Level: intermediate
1994184914b5SBarry Smith 
1995184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1996184914b5SBarry Smith 
1997184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1998184914b5SBarry Smith 
199985385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2000184914b5SBarry Smith @*/
200163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2002184914b5SBarry Smith {
2003184914b5SBarry Smith   PetscFunctionBegin;
20044482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20054482741eSBarry Smith   PetscValidPointer(reason,2);
2006184914b5SBarry Smith   *reason = snes->reason;
2007184914b5SBarry Smith   PetscFunctionReturn(0);
2008184914b5SBarry Smith }
2009184914b5SBarry Smith 
20104a2ae208SSatish Balay #undef __FUNCT__
20114a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2012c9005455SLois Curfman McInnes /*@
2013c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2014c9005455SLois Curfman McInnes 
2015fee21e36SBarry Smith    Collective on SNES
2016fee21e36SBarry Smith 
2017c7afd0dbSLois Curfman McInnes    Input Parameters:
2018c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
20198c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2020cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2021758f92a0SBarry Smith .  na  - size of a and its
202264731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2023758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2024c7afd0dbSLois Curfman McInnes 
2025c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2026c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2027c9005455SLois Curfman McInnes    during the section of code that is being timed.
2028c9005455SLois Curfman McInnes 
202936851e7fSLois Curfman McInnes    Level: intermediate
203036851e7fSLois Curfman McInnes 
2031c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2032758f92a0SBarry Smith 
203308405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2034758f92a0SBarry Smith 
2035c9005455SLois Curfman McInnes @*/
2036a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
2037c9005455SLois Curfman McInnes {
20383a40ed3dSBarry Smith   PetscFunctionBegin;
20394482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20404482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2041a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2042c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2043758f92a0SBarry Smith   snes->conv_hist_its   = its;
2044758f92a0SBarry Smith   snes->conv_hist_max   = na;
2045a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2046758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2047758f92a0SBarry Smith   PetscFunctionReturn(0);
2048758f92a0SBarry Smith }
2049758f92a0SBarry Smith 
20504a2ae208SSatish Balay #undef __FUNCT__
20514a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
20520c4c9dddSBarry Smith /*@C
2053758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2054758f92a0SBarry Smith 
2055758f92a0SBarry Smith    Collective on SNES
2056758f92a0SBarry Smith 
2057758f92a0SBarry Smith    Input Parameter:
2058758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2059758f92a0SBarry Smith 
2060758f92a0SBarry Smith    Output Parameters:
2061758f92a0SBarry Smith .  a   - array to hold history
2062758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2063758f92a0SBarry Smith          negative if not converged) for each solve.
2064758f92a0SBarry Smith -  na  - size of a and its
2065758f92a0SBarry Smith 
2066758f92a0SBarry Smith    Notes:
2067758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2068758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2069758f92a0SBarry Smith 
2070758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2071758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2072758f92a0SBarry Smith    during the section of code that is being timed.
2073758f92a0SBarry Smith 
2074758f92a0SBarry Smith    Level: intermediate
2075758f92a0SBarry Smith 
2076758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2077758f92a0SBarry Smith 
2078758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2079758f92a0SBarry Smith 
2080758f92a0SBarry Smith @*/
208163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2082758f92a0SBarry Smith {
2083758f92a0SBarry Smith   PetscFunctionBegin;
20844482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2085758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2086758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2087758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
20883a40ed3dSBarry Smith   PetscFunctionReturn(0);
2089c9005455SLois Curfman McInnes }
2090c9005455SLois Curfman McInnes 
2091e74ef692SMatthew Knepley #undef __FUNCT__
2092e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2093ac226902SBarry Smith /*@C
209476b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
20957e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
20967e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
209776b2cf59SMatthew Knepley 
209876b2cf59SMatthew Knepley   Collective on SNES
209976b2cf59SMatthew Knepley 
210076b2cf59SMatthew Knepley   Input Parameters:
210176b2cf59SMatthew Knepley . snes - The nonlinear solver context
210276b2cf59SMatthew Knepley . func - The function
210376b2cf59SMatthew Knepley 
210476b2cf59SMatthew Knepley   Calling sequence of func:
2105b5d30489SBarry Smith . func (SNES snes, PetscInt step);
210676b2cf59SMatthew Knepley 
210776b2cf59SMatthew Knepley . step - The current step of the iteration
210876b2cf59SMatthew Knepley 
2109fe97e370SBarry Smith   Level: advanced
2110fe97e370SBarry Smith 
2111fe97e370SBarry 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()
2112fe97e370SBarry Smith         This is not used by most users.
211376b2cf59SMatthew Knepley 
211476b2cf59SMatthew Knepley .keywords: SNES, update
2115b5d30489SBarry Smith 
211685385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
211776b2cf59SMatthew Knepley @*/
211863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
211976b2cf59SMatthew Knepley {
212076b2cf59SMatthew Knepley   PetscFunctionBegin;
21214482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
2122e7788613SBarry Smith   snes->ops->update = func;
212376b2cf59SMatthew Knepley   PetscFunctionReturn(0);
212476b2cf59SMatthew Knepley }
212576b2cf59SMatthew Knepley 
2126e74ef692SMatthew Knepley #undef __FUNCT__
2127e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
212876b2cf59SMatthew Knepley /*@
212976b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
213076b2cf59SMatthew Knepley 
213176b2cf59SMatthew Knepley   Not collective
213276b2cf59SMatthew Knepley 
213376b2cf59SMatthew Knepley   Input Parameters:
213476b2cf59SMatthew Knepley . snes - The nonlinear solver context
213576b2cf59SMatthew Knepley . step - The current step of the iteration
213676b2cf59SMatthew Knepley 
2137205452f4SMatthew Knepley   Level: intermediate
2138205452f4SMatthew Knepley 
213976b2cf59SMatthew Knepley .keywords: SNES, update
2140a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
214176b2cf59SMatthew Knepley @*/
214263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
214376b2cf59SMatthew Knepley {
214476b2cf59SMatthew Knepley   PetscFunctionBegin;
214576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
214676b2cf59SMatthew Knepley }
214776b2cf59SMatthew Knepley 
21484a2ae208SSatish Balay #undef __FUNCT__
21494a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
21509b94acceSBarry Smith /*
21519b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
21529b94acceSBarry Smith    positive parameter delta.
21539b94acceSBarry Smith 
21549b94acceSBarry Smith     Input Parameters:
2155c7afd0dbSLois Curfman McInnes +   snes - the SNES context
21569b94acceSBarry Smith .   y - approximate solution of linear system
21579b94acceSBarry Smith .   fnorm - 2-norm of current function
2158c7afd0dbSLois Curfman McInnes -   delta - trust region size
21599b94acceSBarry Smith 
21609b94acceSBarry Smith     Output Parameters:
2161c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
21629b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
21639b94acceSBarry Smith     region, and exceeds zero otherwise.
2164c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
21659b94acceSBarry Smith 
21669b94acceSBarry Smith     Note:
21674b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
21689b94acceSBarry Smith     is set to be the maximum allowable step size.
21699b94acceSBarry Smith 
21709b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
21719b94acceSBarry Smith */
2172dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
21739b94acceSBarry Smith {
2174064f8208SBarry Smith   PetscReal      nrm;
2175ea709b57SSatish Balay   PetscScalar    cnorm;
2176dfbe8321SBarry Smith   PetscErrorCode ierr;
21773a40ed3dSBarry Smith 
21783a40ed3dSBarry Smith   PetscFunctionBegin;
21794482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21804482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
2181c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2182184914b5SBarry Smith 
2183064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2184064f8208SBarry Smith   if (nrm > *delta) {
2185064f8208SBarry Smith      nrm = *delta/nrm;
2186064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2187064f8208SBarry Smith      cnorm = nrm;
21882dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
21899b94acceSBarry Smith      *ynorm = *delta;
21909b94acceSBarry Smith   } else {
21919b94acceSBarry Smith      *gpnorm = 0.0;
2192064f8208SBarry Smith      *ynorm = nrm;
21939b94acceSBarry Smith   }
21943a40ed3dSBarry Smith   PetscFunctionReturn(0);
21959b94acceSBarry Smith }
21969b94acceSBarry Smith 
21974a2ae208SSatish Balay #undef __FUNCT__
21984a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
21996ce558aeSBarry Smith /*@C
2200f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2201f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
22029b94acceSBarry Smith 
2203c7afd0dbSLois Curfman McInnes    Collective on SNES
2204c7afd0dbSLois Curfman McInnes 
2205b2002411SLois Curfman McInnes    Input Parameters:
2206c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2207f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
220885385478SLisandro Dalcin -  x - the solution vector.
22099b94acceSBarry Smith 
2210b2002411SLois Curfman McInnes    Notes:
22118ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
22128ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
22138ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
22148ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
22158ddd3da0SLois Curfman McInnes 
221636851e7fSLois Curfman McInnes    Level: beginner
221736851e7fSLois Curfman McInnes 
22189b94acceSBarry Smith .keywords: SNES, nonlinear, solve
22199b94acceSBarry Smith 
222085385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
22219b94acceSBarry Smith @*/
2222f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
22239b94acceSBarry Smith {
2224dfbe8321SBarry Smith   PetscErrorCode ierr;
2225f1af5d2fSBarry Smith   PetscTruth     flg;
2226eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2227eabae89aSBarry Smith   PetscViewer    viewer;
2228052efed2SBarry Smith 
22293a40ed3dSBarry Smith   PetscFunctionBegin;
22304482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2231f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2232f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
223385385478SLisandro Dalcin   if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2);
223485385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
223585385478SLisandro Dalcin 
223685385478SLisandro Dalcin   /* set solution vector */
223785385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
223885385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
223985385478SLisandro Dalcin   snes->vec_sol = x;
224085385478SLisandro Dalcin   /* set afine vector if provided */
224185385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
224285385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
224385385478SLisandro Dalcin   snes->vec_rhs = b;
224485385478SLisandro Dalcin 
224585385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
224685385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func);CHKERRQ(ierr);
224770e92668SMatthew Knepley   }
22483f149594SLisandro Dalcin 
224970e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
22503f149594SLisandro Dalcin 
2251abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
225250ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2253d5e45103SBarry Smith 
22543f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22554936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
225685385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22574936397dSBarry Smith   if (snes->domainerror){
22584936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
22594936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
22604936397dSBarry Smith   }
226185385478SLisandro Dalcin 
22623f149594SLisandro Dalcin   if (!snes->reason) {
22633f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
22643f149594SLisandro Dalcin   }
22653f149594SLisandro Dalcin 
22667adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2267eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
22687adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2269eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2270eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2271eabae89aSBarry Smith   }
2272eabae89aSBarry Smith 
227390d69ab7SBarry Smith   flg  = PETSC_FALSE;
227490d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2275da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
22765968eb51SBarry Smith   if (snes->printreason) {
22775968eb51SBarry Smith     if (snes->reason > 0) {
22787adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22795968eb51SBarry Smith     } else {
22807adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22815968eb51SBarry Smith     }
22825968eb51SBarry Smith   }
22835968eb51SBarry Smith 
22843a40ed3dSBarry Smith   PetscFunctionReturn(0);
22859b94acceSBarry Smith }
22869b94acceSBarry Smith 
22879b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
22889b94acceSBarry Smith 
22894a2ae208SSatish Balay #undef __FUNCT__
22904a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
229182bf6240SBarry Smith /*@C
22924b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
22939b94acceSBarry Smith 
2294fee21e36SBarry Smith    Collective on SNES
2295fee21e36SBarry Smith 
2296c7afd0dbSLois Curfman McInnes    Input Parameters:
2297c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2298454a90a3SBarry Smith -  type - a known method
2299c7afd0dbSLois Curfman McInnes 
2300c7afd0dbSLois Curfman McInnes    Options Database Key:
2301454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2302c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2303ae12b187SLois Curfman McInnes 
23049b94acceSBarry Smith    Notes:
2305e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
23064b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2307c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23084b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2309c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23109b94acceSBarry Smith 
2311ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2312ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2313ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2314ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2315ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2316ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2317ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2318ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2319ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2320b0a32e0cSBarry Smith   appropriate method.
232136851e7fSLois Curfman McInnes 
232236851e7fSLois Curfman McInnes   Level: intermediate
2323a703fe33SLois Curfman McInnes 
2324454a90a3SBarry Smith .keywords: SNES, set, type
2325435da068SBarry Smith 
2326435da068SBarry Smith .seealso: SNESType, SNESCreate()
2327435da068SBarry Smith 
23289b94acceSBarry Smith @*/
2329a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
23309b94acceSBarry Smith {
2331dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
23326831982aSBarry Smith   PetscTruth     match;
23333a40ed3dSBarry Smith 
23343a40ed3dSBarry Smith   PetscFunctionBegin;
23354482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23364482741eSBarry Smith   PetscValidCharPointer(type,2);
233782bf6240SBarry Smith 
23386831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
23390f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
234092ff6ae8SBarry Smith 
23417adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2342958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
234375396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
234475396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
234575396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
234675396ef9SLisandro Dalcin   snes->ops->setup          = 0;
234775396ef9SLisandro Dalcin   snes->ops->solve          = 0;
234875396ef9SLisandro Dalcin   snes->ops->view           = 0;
234975396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
235075396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
235175396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
235275396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
23533a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2354454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
23553a40ed3dSBarry Smith   PetscFunctionReturn(0);
23569b94acceSBarry Smith }
23579b94acceSBarry Smith 
2358a847f771SSatish Balay 
23599b94acceSBarry Smith /* --------------------------------------------------------------------- */
23604a2ae208SSatish Balay #undef __FUNCT__
23614a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
236252baeb72SSatish Balay /*@
23639b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2364f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
23659b94acceSBarry Smith 
2366fee21e36SBarry Smith    Not Collective
2367fee21e36SBarry Smith 
236836851e7fSLois Curfman McInnes    Level: advanced
236936851e7fSLois Curfman McInnes 
23709b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
23719b94acceSBarry Smith 
23729b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
23739b94acceSBarry Smith @*/
237463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
23759b94acceSBarry Smith {
2376dfbe8321SBarry Smith   PetscErrorCode ierr;
237782bf6240SBarry Smith 
23783a40ed3dSBarry Smith   PetscFunctionBegin;
23791441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
23804c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
23813a40ed3dSBarry Smith   PetscFunctionReturn(0);
23829b94acceSBarry Smith }
23839b94acceSBarry Smith 
23844a2ae208SSatish Balay #undef __FUNCT__
23854a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
23869b94acceSBarry Smith /*@C
23879a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
23889b94acceSBarry Smith 
2389c7afd0dbSLois Curfman McInnes    Not Collective
2390c7afd0dbSLois Curfman McInnes 
23919b94acceSBarry Smith    Input Parameter:
23924b0e389bSBarry Smith .  snes - nonlinear solver context
23939b94acceSBarry Smith 
23949b94acceSBarry Smith    Output Parameter:
23953a7fca6bSBarry Smith .  type - SNES method (a character string)
23969b94acceSBarry Smith 
239736851e7fSLois Curfman McInnes    Level: intermediate
239836851e7fSLois Curfman McInnes 
2399454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
24009b94acceSBarry Smith @*/
2401a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
24029b94acceSBarry Smith {
24033a40ed3dSBarry Smith   PetscFunctionBegin;
24044482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
24054482741eSBarry Smith   PetscValidPointer(type,2);
24067adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
24073a40ed3dSBarry Smith   PetscFunctionReturn(0);
24089b94acceSBarry Smith }
24099b94acceSBarry Smith 
24104a2ae208SSatish Balay #undef __FUNCT__
24114a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
241252baeb72SSatish Balay /*@
24139b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
24149b94acceSBarry Smith    stored.
24159b94acceSBarry Smith 
2416c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2417c7afd0dbSLois Curfman McInnes 
24189b94acceSBarry Smith    Input Parameter:
24199b94acceSBarry Smith .  snes - the SNES context
24209b94acceSBarry Smith 
24219b94acceSBarry Smith    Output Parameter:
24229b94acceSBarry Smith .  x - the solution
24239b94acceSBarry Smith 
242470e92668SMatthew Knepley    Level: intermediate
242536851e7fSLois Curfman McInnes 
24269b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
24279b94acceSBarry Smith 
242885385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
24299b94acceSBarry Smith @*/
243063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
24319b94acceSBarry Smith {
24323a40ed3dSBarry Smith   PetscFunctionBegin;
24334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
24344482741eSBarry Smith   PetscValidPointer(x,2);
243585385478SLisandro Dalcin   *x = snes->vec_sol;
243670e92668SMatthew Knepley   PetscFunctionReturn(0);
243770e92668SMatthew Knepley }
243870e92668SMatthew Knepley 
243970e92668SMatthew Knepley #undef __FUNCT__
24404a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
244152baeb72SSatish Balay /*@
24429b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
24439b94acceSBarry Smith    stored.
24449b94acceSBarry Smith 
2445c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2446c7afd0dbSLois Curfman McInnes 
24479b94acceSBarry Smith    Input Parameter:
24489b94acceSBarry Smith .  snes - the SNES context
24499b94acceSBarry Smith 
24509b94acceSBarry Smith    Output Parameter:
24519b94acceSBarry Smith .  x - the solution update
24529b94acceSBarry Smith 
245336851e7fSLois Curfman McInnes    Level: advanced
245436851e7fSLois Curfman McInnes 
24559b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
24569b94acceSBarry Smith 
245785385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
24589b94acceSBarry Smith @*/
245963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
24609b94acceSBarry Smith {
24613a40ed3dSBarry Smith   PetscFunctionBegin;
24624482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
24634482741eSBarry Smith   PetscValidPointer(x,2);
246485385478SLisandro Dalcin   *x = snes->vec_sol_update;
24653a40ed3dSBarry Smith   PetscFunctionReturn(0);
24669b94acceSBarry Smith }
24679b94acceSBarry Smith 
24684a2ae208SSatish Balay #undef __FUNCT__
24694a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
24709b94acceSBarry Smith /*@C
24713638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
24729b94acceSBarry Smith 
2473c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2474c7afd0dbSLois Curfman McInnes 
24759b94acceSBarry Smith    Input Parameter:
24769b94acceSBarry Smith .  snes - the SNES context
24779b94acceSBarry Smith 
24789b94acceSBarry Smith    Output Parameter:
24797bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
248070e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
248170e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
24829b94acceSBarry Smith 
248336851e7fSLois Curfman McInnes    Level: advanced
248436851e7fSLois Curfman McInnes 
2485a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
24869b94acceSBarry Smith 
24874b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
24889b94acceSBarry Smith @*/
248970e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
24909b94acceSBarry Smith {
24913a40ed3dSBarry Smith   PetscFunctionBegin;
24924482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
249385385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2494e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
249570e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
24963a40ed3dSBarry Smith   PetscFunctionReturn(0);
24979b94acceSBarry Smith }
24989b94acceSBarry Smith 
24994a2ae208SSatish Balay #undef __FUNCT__
25004a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
25013c7409f5SSatish Balay /*@C
25023c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2503d850072dSLois Curfman McInnes    SNES options in the database.
25043c7409f5SSatish Balay 
2505fee21e36SBarry Smith    Collective on SNES
2506fee21e36SBarry Smith 
2507c7afd0dbSLois Curfman McInnes    Input Parameter:
2508c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2509c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2510c7afd0dbSLois Curfman McInnes 
2511d850072dSLois Curfman McInnes    Notes:
2512a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2513c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2514d850072dSLois Curfman McInnes 
251536851e7fSLois Curfman McInnes    Level: advanced
251636851e7fSLois Curfman McInnes 
25173c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2518a86d99e1SLois Curfman McInnes 
2519a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
25203c7409f5SSatish Balay @*/
252163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
25223c7409f5SSatish Balay {
2523dfbe8321SBarry Smith   PetscErrorCode ierr;
25243c7409f5SSatish Balay 
25253a40ed3dSBarry Smith   PetscFunctionBegin;
25264482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2527639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25281cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
252994b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25303a40ed3dSBarry Smith   PetscFunctionReturn(0);
25313c7409f5SSatish Balay }
25323c7409f5SSatish Balay 
25334a2ae208SSatish Balay #undef __FUNCT__
25344a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
25353c7409f5SSatish Balay /*@C
2536f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2537d850072dSLois Curfman McInnes    SNES options in the database.
25383c7409f5SSatish Balay 
2539fee21e36SBarry Smith    Collective on SNES
2540fee21e36SBarry Smith 
2541c7afd0dbSLois Curfman McInnes    Input Parameters:
2542c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2543c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2544c7afd0dbSLois Curfman McInnes 
2545d850072dSLois Curfman McInnes    Notes:
2546a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2547c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2548d850072dSLois Curfman McInnes 
254936851e7fSLois Curfman McInnes    Level: advanced
255036851e7fSLois Curfman McInnes 
25513c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2552a86d99e1SLois Curfman McInnes 
2553a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
25543c7409f5SSatish Balay @*/
255563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
25563c7409f5SSatish Balay {
2557dfbe8321SBarry Smith   PetscErrorCode ierr;
25583c7409f5SSatish Balay 
25593a40ed3dSBarry Smith   PetscFunctionBegin;
25604482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2561639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25621cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
256394b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25643a40ed3dSBarry Smith   PetscFunctionReturn(0);
25653c7409f5SSatish Balay }
25663c7409f5SSatish Balay 
25674a2ae208SSatish Balay #undef __FUNCT__
25684a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
25699ab63eb5SSatish Balay /*@C
25703c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
25713c7409f5SSatish Balay    SNES options in the database.
25723c7409f5SSatish Balay 
2573c7afd0dbSLois Curfman McInnes    Not Collective
2574c7afd0dbSLois Curfman McInnes 
25753c7409f5SSatish Balay    Input Parameter:
25763c7409f5SSatish Balay .  snes - the SNES context
25773c7409f5SSatish Balay 
25783c7409f5SSatish Balay    Output Parameter:
25793c7409f5SSatish Balay .  prefix - pointer to the prefix string used
25803c7409f5SSatish Balay 
25814ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
25829ab63eb5SSatish Balay    sufficient length to hold the prefix.
25839ab63eb5SSatish Balay 
258436851e7fSLois Curfman McInnes    Level: advanced
258536851e7fSLois Curfman McInnes 
25863c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2587a86d99e1SLois Curfman McInnes 
2588a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
25893c7409f5SSatish Balay @*/
2590e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
25913c7409f5SSatish Balay {
2592dfbe8321SBarry Smith   PetscErrorCode ierr;
25933c7409f5SSatish Balay 
25943a40ed3dSBarry Smith   PetscFunctionBegin;
25954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2596639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25973a40ed3dSBarry Smith   PetscFunctionReturn(0);
25983c7409f5SSatish Balay }
25993c7409f5SSatish Balay 
2600b2002411SLois Curfman McInnes 
26014a2ae208SSatish Balay #undef __FUNCT__
26024a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
26033cea93caSBarry Smith /*@C
26043cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
26053cea93caSBarry Smith 
26067f6c08e0SMatthew Knepley   Level: advanced
26073cea93caSBarry Smith @*/
260863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2609b2002411SLois Curfman McInnes {
2610e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2611dfbe8321SBarry Smith   PetscErrorCode ierr;
2612b2002411SLois Curfman McInnes 
2613b2002411SLois Curfman McInnes   PetscFunctionBegin;
2614b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2615c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2616b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2617b2002411SLois Curfman McInnes }
2618da9b6338SBarry Smith 
2619da9b6338SBarry Smith #undef __FUNCT__
2620da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
262163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2622da9b6338SBarry Smith {
2623dfbe8321SBarry Smith   PetscErrorCode ierr;
262477431f27SBarry Smith   PetscInt       N,i,j;
2625da9b6338SBarry Smith   Vec            u,uh,fh;
2626da9b6338SBarry Smith   PetscScalar    value;
2627da9b6338SBarry Smith   PetscReal      norm;
2628da9b6338SBarry Smith 
2629da9b6338SBarry Smith   PetscFunctionBegin;
2630da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2631da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2632da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2633da9b6338SBarry Smith 
2634da9b6338SBarry Smith   /* currently only works for sequential */
2635da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2636da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2637da9b6338SBarry Smith   for (i=0; i<N; i++) {
2638da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
263977431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2640da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2641ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2642da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
26433ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2644da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
264577431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2646da9b6338SBarry Smith       value = -value;
2647da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2648da9b6338SBarry Smith     }
2649da9b6338SBarry Smith   }
2650da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2651da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2652da9b6338SBarry Smith   PetscFunctionReturn(0);
2653da9b6338SBarry Smith }
265471f87433Sdalcinl 
265571f87433Sdalcinl #undef __FUNCT__
2656fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
265771f87433Sdalcinl /*@
2658fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
265971f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
266071f87433Sdalcinl    Newton method.
266171f87433Sdalcinl 
266271f87433Sdalcinl    Collective on SNES
266371f87433Sdalcinl 
266471f87433Sdalcinl    Input Parameters:
266571f87433Sdalcinl +  snes - SNES context
266671f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
266771f87433Sdalcinl 
266864ba62caSBarry Smith     Options Database:
266964ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
267064ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
267164ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
267264ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
267364ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
267464ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
267564ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
267664ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
267764ba62caSBarry Smith 
267871f87433Sdalcinl    Notes:
267971f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
268071f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
268171f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
268271f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
268371f87433Sdalcinl    solver.
268471f87433Sdalcinl 
268571f87433Sdalcinl    Level: advanced
268671f87433Sdalcinl 
268771f87433Sdalcinl    Reference:
268871f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
268971f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
269071f87433Sdalcinl 
269171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
269271f87433Sdalcinl 
2693fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
269471f87433Sdalcinl @*/
2695fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
269671f87433Sdalcinl {
269771f87433Sdalcinl   PetscFunctionBegin;
269871f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
269971f87433Sdalcinl   snes->ksp_ewconv = flag;
270071f87433Sdalcinl   PetscFunctionReturn(0);
270171f87433Sdalcinl }
270271f87433Sdalcinl 
270371f87433Sdalcinl #undef __FUNCT__
2704fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
270571f87433Sdalcinl /*@
2706fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
270771f87433Sdalcinl    for computing relative tolerance for linear solvers within an
270871f87433Sdalcinl    inexact Newton method.
270971f87433Sdalcinl 
271071f87433Sdalcinl    Not Collective
271171f87433Sdalcinl 
271271f87433Sdalcinl    Input Parameter:
271371f87433Sdalcinl .  snes - SNES context
271471f87433Sdalcinl 
271571f87433Sdalcinl    Output Parameter:
271671f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
271771f87433Sdalcinl 
271871f87433Sdalcinl    Notes:
271971f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
272071f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
272171f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
272271f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
272371f87433Sdalcinl    solver.
272471f87433Sdalcinl 
272571f87433Sdalcinl    Level: advanced
272671f87433Sdalcinl 
272771f87433Sdalcinl    Reference:
272871f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
272971f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
273071f87433Sdalcinl 
273171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
273271f87433Sdalcinl 
2733fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
273471f87433Sdalcinl @*/
2735fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
273671f87433Sdalcinl {
273771f87433Sdalcinl   PetscFunctionBegin;
273871f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
273971f87433Sdalcinl   PetscValidPointer(flag,2);
274071f87433Sdalcinl   *flag = snes->ksp_ewconv;
274171f87433Sdalcinl   PetscFunctionReturn(0);
274271f87433Sdalcinl }
274371f87433Sdalcinl 
274471f87433Sdalcinl #undef __FUNCT__
2745fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
274671f87433Sdalcinl /*@
2747fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
274871f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
274971f87433Sdalcinl    Newton method.
275071f87433Sdalcinl 
275171f87433Sdalcinl    Collective on SNES
275271f87433Sdalcinl 
275371f87433Sdalcinl    Input Parameters:
275471f87433Sdalcinl +    snes - SNES context
275571f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
275671f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
275771f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
275871f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
275971f87433Sdalcinl              (0 <= gamma2 <= 1)
276071f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
276171f87433Sdalcinl .    alpha2 - power for safeguard
276271f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
276371f87433Sdalcinl 
276471f87433Sdalcinl    Note:
276571f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
276671f87433Sdalcinl 
276771f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
276871f87433Sdalcinl 
276971f87433Sdalcinl    Level: advanced
277071f87433Sdalcinl 
277171f87433Sdalcinl    Reference:
277271f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
277371f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
277471f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
277571f87433Sdalcinl 
277671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
277771f87433Sdalcinl 
2778fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
277971f87433Sdalcinl @*/
2780fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
278171f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
278271f87433Sdalcinl {
2783fa9f3622SBarry Smith   SNESKSPEW *kctx;
278471f87433Sdalcinl   PetscFunctionBegin;
278571f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2786fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
278771f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
278871f87433Sdalcinl 
278971f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
279071f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
279171f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
279271f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
279371f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
279471f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
279571f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
279671f87433Sdalcinl 
279771f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
279871f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
279971f87433Sdalcinl   }
280071f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
280171f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
280271f87433Sdalcinl   }
280371f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
280471f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
280571f87433Sdalcinl   }
280671f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
280771f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
280871f87433Sdalcinl   }
280971f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
281071f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
281171f87433Sdalcinl   }
281271f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
281371f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
281471f87433Sdalcinl   }
281571f87433Sdalcinl   PetscFunctionReturn(0);
281671f87433Sdalcinl }
281771f87433Sdalcinl 
281871f87433Sdalcinl #undef __FUNCT__
2819fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
282071f87433Sdalcinl /*@
2821fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
282271f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
282371f87433Sdalcinl    Newton method.
282471f87433Sdalcinl 
282571f87433Sdalcinl    Not Collective
282671f87433Sdalcinl 
282771f87433Sdalcinl    Input Parameters:
282871f87433Sdalcinl      snes - SNES context
282971f87433Sdalcinl 
283071f87433Sdalcinl    Output Parameters:
283171f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
283271f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
283371f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
283471f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
283571f87433Sdalcinl              (0 <= gamma2 <= 1)
283671f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
283771f87433Sdalcinl .    alpha2 - power for safeguard
283871f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
283971f87433Sdalcinl 
284071f87433Sdalcinl    Level: advanced
284171f87433Sdalcinl 
284271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
284371f87433Sdalcinl 
2844fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
284571f87433Sdalcinl @*/
2846fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(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;
285171f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2852fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
285371f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
285471f87433Sdalcinl   if(version)   *version   = kctx->version;
285571f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
285671f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
285771f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
285871f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
285971f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
286071f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
286171f87433Sdalcinl   PetscFunctionReturn(0);
286271f87433Sdalcinl }
286371f87433Sdalcinl 
286471f87433Sdalcinl #undef __FUNCT__
2865fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2866fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
286771f87433Sdalcinl {
286871f87433Sdalcinl   PetscErrorCode ierr;
2869fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
287071f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
287171f87433Sdalcinl 
287271f87433Sdalcinl   PetscFunctionBegin;
287371f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
287471f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
287571f87433Sdalcinl     rtol = kctx->rtol_0;
287671f87433Sdalcinl   } else {
287771f87433Sdalcinl     if (kctx->version == 1) {
287871f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
287971f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
288071f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
288171f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
288271f87433Sdalcinl     } else if (kctx->version == 2) {
288371f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
288471f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
288571f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
288671f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
288771f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
288871f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
288971f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
289071f87433Sdalcinl       stol = PetscMax(rtol,stol);
289171f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
289271f87433Sdalcinl       /* safeguard: avoid oversolving */
289371f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
289471f87433Sdalcinl       stol = PetscMax(rtol,stol);
289571f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
289671f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
289771f87433Sdalcinl   }
289871f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
289971f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
290071f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
290171f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
290271f87433Sdalcinl   PetscFunctionReturn(0);
290371f87433Sdalcinl }
290471f87433Sdalcinl 
290571f87433Sdalcinl #undef __FUNCT__
2906fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2907fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
290871f87433Sdalcinl {
290971f87433Sdalcinl   PetscErrorCode ierr;
2910fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
291171f87433Sdalcinl   PCSide         pcside;
291271f87433Sdalcinl   Vec            lres;
291371f87433Sdalcinl 
291471f87433Sdalcinl   PetscFunctionBegin;
291571f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
291671f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
291771f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
291871f87433Sdalcinl   if (kctx->version == 1) {
291971f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
292071f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
292171f87433Sdalcinl       /* KSP residual is true linear residual */
292271f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
292371f87433Sdalcinl     } else {
292471f87433Sdalcinl       /* KSP residual is preconditioned residual */
292571f87433Sdalcinl       /* compute true linear residual norm */
292671f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
292771f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
292871f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
292971f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
293071f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
293171f87433Sdalcinl     }
293271f87433Sdalcinl   }
293371f87433Sdalcinl   PetscFunctionReturn(0);
293471f87433Sdalcinl }
293571f87433Sdalcinl 
293671f87433Sdalcinl #undef __FUNCT__
293771f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
293871f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
293971f87433Sdalcinl {
294071f87433Sdalcinl   PetscErrorCode ierr;
294171f87433Sdalcinl 
294271f87433Sdalcinl   PetscFunctionBegin;
2943fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
294471f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2945fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
294671f87433Sdalcinl   PetscFunctionReturn(0);
294771f87433Sdalcinl }
2948