xref: /petsc/src/snes/interface/snes.c (revision 1691336300274d665bcc73bfd7e347487d02b683)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
29b94acceSBarry Smith 
3b9147fbbSdalcinl #include "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 */
963dd3a1aSKris Buschelman PetscCookie PETSCSNES_DLLEXPORT SNES_COOKIE = 0;
106849ba73SBarry Smith PetscEvent  SNES_Solve = 0, SNES_LineSearch = 0, SNES_FunctionEval = 0, SNES_JacobianEval = 0;
11a09944afSBarry Smith 
12a09944afSBarry Smith #undef __FUNCT__
134936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
144936397dSBarry Smith /*@C
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;
73e060cb09SBarry Smith   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     }
1160f5bd95cSBarry Smith   } else if (isstring) {
117454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
118b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
11919bcc07fSBarry Smith   }
12094b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
121b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
12294b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
123b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1243a40ed3dSBarry Smith   PetscFunctionReturn(0);
1259b94acceSBarry Smith }
1269b94acceSBarry Smith 
12776b2cf59SMatthew Knepley /*
12876b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
12976b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
13076b2cf59SMatthew Knepley */
13176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
132a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
1336849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
13476b2cf59SMatthew Knepley 
135e74ef692SMatthew Knepley #undef __FUNCT__
136e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
137ac226902SBarry Smith /*@C
13876b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
13976b2cf59SMatthew Knepley 
14076b2cf59SMatthew Knepley   Not Collective
14176b2cf59SMatthew Knepley 
14276b2cf59SMatthew Knepley   Input Parameter:
14376b2cf59SMatthew Knepley . snescheck - function that checks for options
14476b2cf59SMatthew Knepley 
14576b2cf59SMatthew Knepley   Level: developer
14676b2cf59SMatthew Knepley 
14776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
14876b2cf59SMatthew Knepley @*/
14963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
15076b2cf59SMatthew Knepley {
15176b2cf59SMatthew Knepley   PetscFunctionBegin;
15276b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
15377431f27SBarry Smith     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
15476b2cf59SMatthew Knepley   }
15576b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
15676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
15776b2cf59SMatthew Knepley }
15876b2cf59SMatthew Knepley 
1594a2ae208SSatish Balay #undef __FUNCT__
1604a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1619b94acceSBarry Smith /*@
16294b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
1639b94acceSBarry Smith 
164c7afd0dbSLois Curfman McInnes    Collective on SNES
165c7afd0dbSLois Curfman McInnes 
1669b94acceSBarry Smith    Input Parameter:
1679b94acceSBarry Smith .  snes - the SNES context
1689b94acceSBarry Smith 
16936851e7fSLois Curfman McInnes    Options Database Keys:
1706831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
17182738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
17282738288SBarry Smith                 of the change in the solution between steps
17370441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
174b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
175b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
176b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
17750ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
178b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
1792492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
18082738288SBarry Smith                                solver; hence iterations will continue until max_it
1811fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
18282738288SBarry Smith                                of convergence test
183e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
184e8105e01SRichard Katz                                        filename given prints to stdout
185a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
186a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
187a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
188a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
189e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
1905968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
191fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
19282738288SBarry Smith 
19382738288SBarry Smith     Options Database for Eisenstat-Walker method:
194fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
1954b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
19636851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
19736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
19836851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
19936851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
20036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
20136851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
20282738288SBarry Smith 
20311ca99fdSLois Curfman McInnes    Notes:
20411ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
20511ca99fdSLois Curfman McInnes    the users manual.
20683e2fdc7SBarry Smith 
20736851e7fSLois Curfman McInnes    Level: beginner
20836851e7fSLois Curfman McInnes 
2099b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2109b94acceSBarry Smith 
21169ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
2129b94acceSBarry Smith @*/
21363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
2149b94acceSBarry Smith {
215f1af5d2fSBarry Smith   PetscTruth              flg;
21685385478SLisandro Dalcin   PetscInt                i,indx;
21785385478SLisandro Dalcin   const char              *deft = SNESLS;
21885385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
21985385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
220e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
22123d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
22285385478SLisandro Dalcin   PetscErrorCode          ierr;
2239b94acceSBarry Smith 
2243a40ed3dSBarry Smith   PetscFunctionBegin;
2254482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
226ca161407SBarry Smith 
2277adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
228186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
2297adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
230b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
231d64ed03dSBarry Smith     if (flg) {
232186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
2337adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
234186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
235d64ed03dSBarry Smith     }
236909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
23793c39befSBarry Smith 
23887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
23970441072SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
240186905e3SBarry Smith 
24187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
242b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
243b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
24450ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
24585385478SLisandro Dalcin 
24685385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
24785385478SLisandro Dalcin     if (flg) {
24885385478SLisandro Dalcin       switch (indx) {
2497f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
2507f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
25185385478SLisandro Dalcin       }
25285385478SLisandro Dalcin     }
25385385478SLisandro Dalcin 
2545968eb51SBarry Smith     ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr);
2555968eb51SBarry Smith     if (flg) {
2565968eb51SBarry Smith       snes->printreason = PETSC_TRUE;
2575968eb51SBarry Smith     }
258186905e3SBarry Smith 
25985385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
26085385478SLisandro Dalcin 
261fa9f3622SBarry Smith     ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
262186905e3SBarry Smith 
263fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
264fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
265fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
266fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
267fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
268fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
269fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
270186905e3SBarry Smith 
271a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr);
272a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
273eabae89aSBarry Smith 
274a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
275e8105e01SRichard Katz     if (flg) {
2767adad957SLisandro Dalcin       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
27723d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
278e8105e01SRichard Katz     }
279eabae89aSBarry Smith 
280a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
281eabae89aSBarry Smith     if (flg) {
2827adad957SLisandro Dalcin       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
283f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
284e8105e01SRichard Katz     }
285eabae89aSBarry Smith 
286a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
287eabae89aSBarry Smith     if (flg) {
2887adad957SLisandro Dalcin       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
28923d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
290eabae89aSBarry Smith     }
291eabae89aSBarry Smith 
292a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr);
293a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
294a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr);
295a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
296a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr);
297a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
298a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr);
299a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
300e24b481bSBarry Smith 
301b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
3024b27c08aSLois Curfman McInnes     if (flg) {
303186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
304ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
3059b94acceSBarry Smith     }
306639f9d9dSBarry Smith 
30776b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
30876b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
30976b2cf59SMatthew Knepley     }
31076b2cf59SMatthew Knepley 
311e7788613SBarry Smith     if (snes->ops->setfromoptions) {
312e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
313639f9d9dSBarry Smith     }
314b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3154bbc92c1SBarry Smith 
31685385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
31793993e2dSLois Curfman McInnes 
3183a40ed3dSBarry Smith   PetscFunctionReturn(0);
3199b94acceSBarry Smith }
3209b94acceSBarry Smith 
321a847f771SSatish Balay 
3224a2ae208SSatish Balay #undef __FUNCT__
3234a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
3249b94acceSBarry Smith /*@
3259b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3269b94acceSBarry Smith    the nonlinear solvers.
3279b94acceSBarry Smith 
328fee21e36SBarry Smith    Collective on SNES
329fee21e36SBarry Smith 
330c7afd0dbSLois Curfman McInnes    Input Parameters:
331c7afd0dbSLois Curfman McInnes +  snes - the SNES context
332c7afd0dbSLois Curfman McInnes -  usrP - optional user context
333c7afd0dbSLois Curfman McInnes 
33436851e7fSLois Curfman McInnes    Level: intermediate
33536851e7fSLois Curfman McInnes 
3369b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3379b94acceSBarry Smith 
3389b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3399b94acceSBarry Smith @*/
34063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
3419b94acceSBarry Smith {
3423a40ed3dSBarry Smith   PetscFunctionBegin;
3434482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3449b94acceSBarry Smith   snes->user		= usrP;
3453a40ed3dSBarry Smith   PetscFunctionReturn(0);
3469b94acceSBarry Smith }
34774679c65SBarry Smith 
3484a2ae208SSatish Balay #undef __FUNCT__
3494a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3509b94acceSBarry Smith /*@C
3519b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3529b94acceSBarry Smith    nonlinear solvers.
3539b94acceSBarry Smith 
354c7afd0dbSLois Curfman McInnes    Not Collective
355c7afd0dbSLois Curfman McInnes 
3569b94acceSBarry Smith    Input Parameter:
3579b94acceSBarry Smith .  snes - SNES context
3589b94acceSBarry Smith 
3599b94acceSBarry Smith    Output Parameter:
3609b94acceSBarry Smith .  usrP - user context
3619b94acceSBarry Smith 
36236851e7fSLois Curfman McInnes    Level: intermediate
36336851e7fSLois Curfman McInnes 
3649b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3659b94acceSBarry Smith 
3669b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3679b94acceSBarry Smith @*/
36863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
3699b94acceSBarry Smith {
3703a40ed3dSBarry Smith   PetscFunctionBegin;
3714482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3729b94acceSBarry Smith   *usrP = snes->user;
3733a40ed3dSBarry Smith   PetscFunctionReturn(0);
3749b94acceSBarry Smith }
37574679c65SBarry Smith 
3764a2ae208SSatish Balay #undef __FUNCT__
3774a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3789b94acceSBarry Smith /*@
379c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
380c8228a4eSBarry Smith    at this time.
3819b94acceSBarry Smith 
382c7afd0dbSLois Curfman McInnes    Not Collective
383c7afd0dbSLois Curfman McInnes 
3849b94acceSBarry Smith    Input Parameter:
3859b94acceSBarry Smith .  snes - SNES context
3869b94acceSBarry Smith 
3879b94acceSBarry Smith    Output Parameter:
3889b94acceSBarry Smith .  iter - iteration number
3899b94acceSBarry Smith 
390c8228a4eSBarry Smith    Notes:
391c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
392c8228a4eSBarry Smith 
393c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
39408405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
39508405cd6SLois Curfman McInnes .vb
39608405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
39708405cd6SLois Curfman McInnes       if (!(it % 2)) {
39808405cd6SLois Curfman McInnes         [compute Jacobian here]
39908405cd6SLois Curfman McInnes       }
40008405cd6SLois Curfman McInnes .ve
401c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
40208405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
403c8228a4eSBarry Smith 
40436851e7fSLois Curfman McInnes    Level: intermediate
40536851e7fSLois Curfman McInnes 
4062b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
4072b668275SBarry Smith 
408b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
4099b94acceSBarry Smith @*/
41063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
4119b94acceSBarry Smith {
4123a40ed3dSBarry Smith   PetscFunctionBegin;
4134482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4144482741eSBarry Smith   PetscValidIntPointer(iter,2);
4159b94acceSBarry Smith   *iter = snes->iter;
4163a40ed3dSBarry Smith   PetscFunctionReturn(0);
4179b94acceSBarry Smith }
41874679c65SBarry Smith 
4194a2ae208SSatish Balay #undef __FUNCT__
4204a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
4219b94acceSBarry Smith /*@
4229b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4239b94acceSBarry Smith    with SNESSSetFunction().
4249b94acceSBarry Smith 
425c7afd0dbSLois Curfman McInnes    Collective on SNES
426c7afd0dbSLois Curfman McInnes 
4279b94acceSBarry Smith    Input Parameter:
4289b94acceSBarry Smith .  snes - SNES context
4299b94acceSBarry Smith 
4309b94acceSBarry Smith    Output Parameter:
4319b94acceSBarry Smith .  fnorm - 2-norm of function
4329b94acceSBarry Smith 
43336851e7fSLois Curfman McInnes    Level: intermediate
43436851e7fSLois Curfman McInnes 
4359b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
436a86d99e1SLois Curfman McInnes 
437b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
4389b94acceSBarry Smith @*/
43971f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
4409b94acceSBarry Smith {
4413a40ed3dSBarry Smith   PetscFunctionBegin;
4424482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4434482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
4449b94acceSBarry Smith   *fnorm = snes->norm;
4453a40ed3dSBarry Smith   PetscFunctionReturn(0);
4469b94acceSBarry Smith }
44774679c65SBarry Smith 
4484a2ae208SSatish Balay #undef __FUNCT__
449b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
4509b94acceSBarry Smith /*@
451b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
4529b94acceSBarry Smith    attempted by the nonlinear solver.
4539b94acceSBarry Smith 
454c7afd0dbSLois Curfman McInnes    Not Collective
455c7afd0dbSLois Curfman McInnes 
4569b94acceSBarry Smith    Input Parameter:
4579b94acceSBarry Smith .  snes - SNES context
4589b94acceSBarry Smith 
4599b94acceSBarry Smith    Output Parameter:
4609b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4619b94acceSBarry Smith 
462c96a6f78SLois Curfman McInnes    Notes:
463c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
464c96a6f78SLois Curfman McInnes 
46536851e7fSLois Curfman McInnes    Level: intermediate
46636851e7fSLois Curfman McInnes 
4679b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
46858ebbce7SBarry Smith 
46958ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
47058ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
4719b94acceSBarry Smith @*/
472b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
4739b94acceSBarry Smith {
4743a40ed3dSBarry Smith   PetscFunctionBegin;
4754482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4764482741eSBarry Smith   PetscValidIntPointer(nfails,2);
47750ffb88aSMatthew Knepley   *nfails = snes->numFailures;
47850ffb88aSMatthew Knepley   PetscFunctionReturn(0);
47950ffb88aSMatthew Knepley }
48050ffb88aSMatthew Knepley 
48150ffb88aSMatthew Knepley #undef __FUNCT__
482b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
48350ffb88aSMatthew Knepley /*@
484b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
48550ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
48650ffb88aSMatthew Knepley 
48750ffb88aSMatthew Knepley    Not Collective
48850ffb88aSMatthew Knepley 
48950ffb88aSMatthew Knepley    Input Parameters:
49050ffb88aSMatthew Knepley +  snes     - SNES context
49150ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
49250ffb88aSMatthew Knepley 
49350ffb88aSMatthew Knepley    Level: intermediate
49450ffb88aSMatthew Knepley 
49550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
49658ebbce7SBarry Smith 
49758ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
49858ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
49950ffb88aSMatthew Knepley @*/
500b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
50150ffb88aSMatthew Knepley {
50250ffb88aSMatthew Knepley   PetscFunctionBegin;
5034482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
50450ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
50550ffb88aSMatthew Knepley   PetscFunctionReturn(0);
50650ffb88aSMatthew Knepley }
50750ffb88aSMatthew Knepley 
50850ffb88aSMatthew Knepley #undef __FUNCT__
509b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
51050ffb88aSMatthew Knepley /*@
511b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
51250ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
51350ffb88aSMatthew Knepley 
51450ffb88aSMatthew Knepley    Not Collective
51550ffb88aSMatthew Knepley 
51650ffb88aSMatthew Knepley    Input Parameter:
51750ffb88aSMatthew Knepley .  snes     - SNES context
51850ffb88aSMatthew Knepley 
51950ffb88aSMatthew Knepley    Output Parameter:
52050ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
52150ffb88aSMatthew Knepley 
52250ffb88aSMatthew Knepley    Level: intermediate
52350ffb88aSMatthew Knepley 
52450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
52558ebbce7SBarry Smith 
52658ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
52758ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
52858ebbce7SBarry Smith 
52950ffb88aSMatthew Knepley @*/
530b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
53150ffb88aSMatthew Knepley {
53250ffb88aSMatthew Knepley   PetscFunctionBegin;
5334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5344482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
53550ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
5363a40ed3dSBarry Smith   PetscFunctionReturn(0);
5379b94acceSBarry Smith }
538a847f771SSatish Balay 
5394a2ae208SSatish Balay #undef __FUNCT__
5402541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
5412541af92SBarry Smith /*@
5422541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
5432541af92SBarry Smith      done by SNES.
5442541af92SBarry Smith 
5452541af92SBarry Smith    Not Collective
5462541af92SBarry Smith 
5472541af92SBarry Smith    Input Parameter:
5482541af92SBarry Smith .  snes     - SNES context
5492541af92SBarry Smith 
5502541af92SBarry Smith    Output Parameter:
5512541af92SBarry Smith .  nfuncs - number of evaluations
5522541af92SBarry Smith 
5532541af92SBarry Smith    Level: intermediate
5542541af92SBarry Smith 
5552541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
55658ebbce7SBarry Smith 
55758ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
5582541af92SBarry Smith @*/
5592541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
5602541af92SBarry Smith {
5612541af92SBarry Smith   PetscFunctionBegin;
5622541af92SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5632541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
5642541af92SBarry Smith   *nfuncs = snes->nfuncs;
5652541af92SBarry Smith   PetscFunctionReturn(0);
5662541af92SBarry Smith }
5672541af92SBarry Smith 
5682541af92SBarry Smith #undef __FUNCT__
5693d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
5703d4c4710SBarry Smith /*@
5713d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
5723d4c4710SBarry Smith    linear solvers.
5733d4c4710SBarry Smith 
5743d4c4710SBarry Smith    Not Collective
5753d4c4710SBarry Smith 
5763d4c4710SBarry Smith    Input Parameter:
5773d4c4710SBarry Smith .  snes - SNES context
5783d4c4710SBarry Smith 
5793d4c4710SBarry Smith    Output Parameter:
5803d4c4710SBarry Smith .  nfails - number of failed solves
5813d4c4710SBarry Smith 
5823d4c4710SBarry Smith    Notes:
5833d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
5843d4c4710SBarry Smith 
5853d4c4710SBarry Smith    Level: intermediate
5863d4c4710SBarry Smith 
5873d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
58858ebbce7SBarry Smith 
58958ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures()
5903d4c4710SBarry Smith @*/
5913d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
5923d4c4710SBarry Smith {
5933d4c4710SBarry Smith   PetscFunctionBegin;
5943d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5953d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
5963d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
5973d4c4710SBarry Smith   PetscFunctionReturn(0);
5983d4c4710SBarry Smith }
5993d4c4710SBarry Smith 
6003d4c4710SBarry Smith #undef __FUNCT__
6013d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
6023d4c4710SBarry Smith /*@
6033d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
6043d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
6053d4c4710SBarry Smith 
6063d4c4710SBarry Smith    Collective on SNES
6073d4c4710SBarry Smith 
6083d4c4710SBarry Smith    Input Parameters:
6093d4c4710SBarry Smith +  snes     - SNES context
6103d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
6113d4c4710SBarry Smith 
6123d4c4710SBarry Smith    Level: intermediate
6133d4c4710SBarry Smith 
6143d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
6153d4c4710SBarry Smith 
6163d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
6173d4c4710SBarry Smith 
61858ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
6193d4c4710SBarry Smith @*/
6203d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
6213d4c4710SBarry Smith {
6223d4c4710SBarry Smith   PetscFunctionBegin;
6233d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6243d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
6253d4c4710SBarry Smith   PetscFunctionReturn(0);
6263d4c4710SBarry Smith }
6273d4c4710SBarry Smith 
6283d4c4710SBarry Smith #undef __FUNCT__
6293d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
6303d4c4710SBarry Smith /*@
6313d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
6323d4c4710SBarry Smith      are allowed before SNES terminates
6333d4c4710SBarry Smith 
6343d4c4710SBarry Smith    Not Collective
6353d4c4710SBarry Smith 
6363d4c4710SBarry Smith    Input Parameter:
6373d4c4710SBarry Smith .  snes     - SNES context
6383d4c4710SBarry Smith 
6393d4c4710SBarry Smith    Output Parameter:
6403d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
6413d4c4710SBarry Smith 
6423d4c4710SBarry Smith    Level: intermediate
6433d4c4710SBarry Smith 
6443d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
6453d4c4710SBarry Smith 
6463d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
6473d4c4710SBarry Smith 
64858ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(),
6493d4c4710SBarry Smith @*/
6503d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
6513d4c4710SBarry Smith {
6523d4c4710SBarry Smith   PetscFunctionBegin;
6533d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6543d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
6553d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
6563d4c4710SBarry Smith   PetscFunctionReturn(0);
6573d4c4710SBarry Smith }
6583d4c4710SBarry Smith 
6593d4c4710SBarry Smith #undef __FUNCT__
660b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
661c96a6f78SLois Curfman McInnes /*@
662b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
663c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
664c96a6f78SLois Curfman McInnes 
665c7afd0dbSLois Curfman McInnes    Not Collective
666c7afd0dbSLois Curfman McInnes 
667c96a6f78SLois Curfman McInnes    Input Parameter:
668c96a6f78SLois Curfman McInnes .  snes - SNES context
669c96a6f78SLois Curfman McInnes 
670c96a6f78SLois Curfman McInnes    Output Parameter:
671c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
672c96a6f78SLois Curfman McInnes 
673c96a6f78SLois Curfman McInnes    Notes:
674c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
675c96a6f78SLois Curfman McInnes 
67636851e7fSLois Curfman McInnes    Level: intermediate
67736851e7fSLois Curfman McInnes 
678c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
6792b668275SBarry Smith 
68058ebbce7SBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm()S, NESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
681c96a6f78SLois Curfman McInnes @*/
682b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
683c96a6f78SLois Curfman McInnes {
6843a40ed3dSBarry Smith   PetscFunctionBegin;
6854482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6864482741eSBarry Smith   PetscValidIntPointer(lits,2);
687c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
6883a40ed3dSBarry Smith   PetscFunctionReturn(0);
689c96a6f78SLois Curfman McInnes }
690c96a6f78SLois Curfman McInnes 
6914a2ae208SSatish Balay #undef __FUNCT__
69294b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
69352baeb72SSatish Balay /*@
69494b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
6959b94acceSBarry Smith 
69694b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
697c7afd0dbSLois Curfman McInnes 
6989b94acceSBarry Smith    Input Parameter:
6999b94acceSBarry Smith .  snes - the SNES context
7009b94acceSBarry Smith 
7019b94acceSBarry Smith    Output Parameter:
70294b7f48cSBarry Smith .  ksp - the KSP context
7039b94acceSBarry Smith 
7049b94acceSBarry Smith    Notes:
70594b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
7069b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
7072999313aSBarry Smith    PC contexts as well.
7089b94acceSBarry Smith 
70936851e7fSLois Curfman McInnes    Level: beginner
71036851e7fSLois Curfman McInnes 
71194b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
7129b94acceSBarry Smith 
7132999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
7149b94acceSBarry Smith @*/
71563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
7169b94acceSBarry Smith {
7173a40ed3dSBarry Smith   PetscFunctionBegin;
7184482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7194482741eSBarry Smith   PetscValidPointer(ksp,2);
72094b7f48cSBarry Smith   *ksp = snes->ksp;
7213a40ed3dSBarry Smith   PetscFunctionReturn(0);
7229b94acceSBarry Smith }
72382bf6240SBarry Smith 
7244a2ae208SSatish Balay #undef __FUNCT__
7252999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
7262999313aSBarry Smith /*@
7272999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
7282999313aSBarry Smith 
7292999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
7302999313aSBarry Smith 
7312999313aSBarry Smith    Input Parameters:
7322999313aSBarry Smith +  snes - the SNES context
7332999313aSBarry Smith -  ksp - the KSP context
7342999313aSBarry Smith 
7352999313aSBarry Smith    Notes:
7362999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
7372999313aSBarry Smith    so this routine is rarely needed.
7382999313aSBarry Smith 
7392999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
7402999313aSBarry Smith    decreased by one.
7412999313aSBarry Smith 
7422999313aSBarry Smith    Level: developer
7432999313aSBarry Smith 
7442999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
7452999313aSBarry Smith 
7462999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
7472999313aSBarry Smith @*/
7482999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
7492999313aSBarry Smith {
7502999313aSBarry Smith   PetscErrorCode ierr;
7512999313aSBarry Smith 
7522999313aSBarry Smith   PetscFunctionBegin;
7532999313aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7542999313aSBarry Smith   PetscValidHeaderSpecific(ksp,KSP_COOKIE,2);
7552999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
7567dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
757906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
7582999313aSBarry Smith   snes->ksp = ksp;
7592999313aSBarry Smith   PetscFunctionReturn(0);
7602999313aSBarry Smith }
7612999313aSBarry Smith 
7627adad957SLisandro Dalcin #if 0
7632999313aSBarry Smith #undef __FUNCT__
7644a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
7656849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
766e24b481bSBarry Smith {
767e24b481bSBarry Smith   PetscFunctionBegin;
768e24b481bSBarry Smith   PetscFunctionReturn(0);
769e24b481bSBarry Smith }
7707adad957SLisandro Dalcin #endif
771e24b481bSBarry Smith 
7729b94acceSBarry Smith /* -----------------------------------------------------------*/
7734a2ae208SSatish Balay #undef __FUNCT__
7744a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
77552baeb72SSatish Balay /*@
7769b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
7779b94acceSBarry Smith 
778c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
779c7afd0dbSLois Curfman McInnes 
780c7afd0dbSLois Curfman McInnes    Input Parameters:
781906ed7ccSBarry Smith .  comm - MPI communicator
7829b94acceSBarry Smith 
7839b94acceSBarry Smith    Output Parameter:
7849b94acceSBarry Smith .  outsnes - the new SNES context
7859b94acceSBarry Smith 
786c7afd0dbSLois Curfman McInnes    Options Database Keys:
787c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
788c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
789c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
790c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
791c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
792c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
793c1f60f51SBarry Smith 
79436851e7fSLois Curfman McInnes    Level: beginner
79536851e7fSLois Curfman McInnes 
7969b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
7979b94acceSBarry Smith 
7984b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES
7999b94acceSBarry Smith @*/
80063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
8019b94acceSBarry Smith {
802dfbe8321SBarry Smith   PetscErrorCode      ierr;
8039b94acceSBarry Smith   SNES                snes;
804fa9f3622SBarry Smith   SNESKSPEW           *kctx;
80537fcc0dbSBarry Smith 
8063a40ed3dSBarry Smith   PetscFunctionBegin;
807ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
8088ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
8098ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
8108ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
8118ba1e511SMatthew Knepley #endif
8128ba1e511SMatthew Knepley 
813e7788613SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
8147adad957SLisandro Dalcin 
81585385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
8169b94acceSBarry Smith   snes->max_its           = 50;
8179750a799SBarry Smith   snes->max_funcs	  = 10000;
8189b94acceSBarry Smith   snes->norm		  = 0.0;
819b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
820b4874afaSBarry Smith   snes->ttol              = 0.0;
82170441072SBarry Smith   snes->abstol		  = 1.e-50;
8229b94acceSBarry Smith   snes->xtol		  = 1.e-8;
8234b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
8249b94acceSBarry Smith   snes->nfuncs            = 0;
82550ffb88aSMatthew Knepley   snes->numFailures       = 0;
82650ffb88aSMatthew Knepley   snes->maxFailures       = 1;
8277a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
828639f9d9dSBarry Smith   snes->numbermonitors    = 0;
8299b94acceSBarry Smith   snes->data              = 0;
8304dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
831186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
8326f24a144SLois Curfman McInnes   snes->vwork             = 0;
8336f24a144SLois Curfman McInnes   snes->nwork             = 0;
834758f92a0SBarry Smith   snes->conv_hist_len     = 0;
835758f92a0SBarry Smith   snes->conv_hist_max     = 0;
836758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
837758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
838758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
839184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
8409b94acceSBarry Smith 
8413d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
8423d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
8433d4c4710SBarry Smith 
8449b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
84538f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
8469b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
8479b94acceSBarry Smith   kctx->version     = 2;
8489b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
8499b94acceSBarry Smith                              this was too large for some test cases */
8509b94acceSBarry Smith   kctx->rtol_last   = 0;
8519b94acceSBarry Smith   kctx->rtol_max    = .9;
8529b94acceSBarry Smith   kctx->gamma       = 1.0;
85371f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
85471f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
8559b94acceSBarry Smith   kctx->threshold   = .1;
8569b94acceSBarry Smith   kctx->lresid_last = 0;
8579b94acceSBarry Smith   kctx->norm_last   = 0;
8589b94acceSBarry Smith 
85994b7f48cSBarry Smith   ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr);
86052e6d16bSBarry Smith   ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
8615334005bSBarry Smith 
8629b94acceSBarry Smith   *outsnes = snes;
86300036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
8643a40ed3dSBarry Smith   PetscFunctionReturn(0);
8659b94acceSBarry Smith }
8669b94acceSBarry Smith 
8674a2ae208SSatish Balay #undef __FUNCT__
8684a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
8699b94acceSBarry Smith /*@C
8709b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
8719b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
8729b94acceSBarry Smith    equations.
8739b94acceSBarry Smith 
874fee21e36SBarry Smith    Collective on SNES
875fee21e36SBarry Smith 
876c7afd0dbSLois Curfman McInnes    Input Parameters:
877c7afd0dbSLois Curfman McInnes +  snes - the SNES context
878c7afd0dbSLois Curfman McInnes .  r - vector to store function value
879de044059SHong Zhang .  func - function evaluation routine
880c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
881c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8829b94acceSBarry Smith 
883c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8848d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
885c7afd0dbSLois Curfman McInnes 
886313e4042SLois Curfman McInnes .  f - function vector
887c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
8889b94acceSBarry Smith 
8899b94acceSBarry Smith    Notes:
8909b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
8919b94acceSBarry Smith $      f'(x) x = -f(x),
892c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
8939b94acceSBarry Smith 
89436851e7fSLois Curfman McInnes    Level: beginner
89536851e7fSLois Curfman McInnes 
8969b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
8979b94acceSBarry Smith 
898a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
8999b94acceSBarry Smith @*/
90063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
9019b94acceSBarry Smith {
90285385478SLisandro Dalcin   PetscErrorCode ierr;
9033a40ed3dSBarry Smith   PetscFunctionBegin;
9044482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9054482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
906c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
90785385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
90885385478SLisandro Dalcin   if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); }
909e7788613SBarry Smith   snes->ops->computefunction = func;
91085385478SLisandro Dalcin   snes->vec_func             = r;
9119b94acceSBarry Smith   snes->funP                 = ctx;
9123a40ed3dSBarry Smith   PetscFunctionReturn(0);
9139b94acceSBarry Smith }
9149b94acceSBarry Smith 
9153ab0aad5SBarry Smith /* --------------------------------------------------------------- */
9163ab0aad5SBarry Smith #undef __FUNCT__
9171096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
9181096aae1SMatthew Knepley /*@C
9191096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
9201096aae1SMatthew Knepley    it assumes a zero right hand side.
9211096aae1SMatthew Knepley 
9221096aae1SMatthew Knepley    Collective on SNES
9231096aae1SMatthew Knepley 
9241096aae1SMatthew Knepley    Input Parameter:
9251096aae1SMatthew Knepley .  snes - the SNES context
9261096aae1SMatthew Knepley 
9271096aae1SMatthew Knepley    Output Parameter:
928bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
9291096aae1SMatthew Knepley 
9301096aae1SMatthew Knepley    Level: intermediate
9311096aae1SMatthew Knepley 
9321096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
9331096aae1SMatthew Knepley 
93485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
9351096aae1SMatthew Knepley @*/
9361096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
9371096aae1SMatthew Knepley {
9381096aae1SMatthew Knepley   PetscFunctionBegin;
9391096aae1SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9401096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
94185385478SLisandro Dalcin   *rhs = snes->vec_rhs;
9421096aae1SMatthew Knepley   PetscFunctionReturn(0);
9431096aae1SMatthew Knepley }
9441096aae1SMatthew Knepley 
9451096aae1SMatthew Knepley #undef __FUNCT__
9464a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
9479b94acceSBarry Smith /*@
94836851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
9499b94acceSBarry Smith                          SNESSetFunction().
9509b94acceSBarry Smith 
951c7afd0dbSLois Curfman McInnes    Collective on SNES
952c7afd0dbSLois Curfman McInnes 
9539b94acceSBarry Smith    Input Parameters:
954c7afd0dbSLois Curfman McInnes +  snes - the SNES context
955c7afd0dbSLois Curfman McInnes -  x - input vector
9569b94acceSBarry Smith 
9579b94acceSBarry Smith    Output Parameter:
9583638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
9599b94acceSBarry Smith 
9601bffabb2SLois Curfman McInnes    Notes:
96136851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
96236851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
96336851e7fSLois Curfman McInnes    themselves.
96436851e7fSLois Curfman McInnes 
96536851e7fSLois Curfman McInnes    Level: developer
96636851e7fSLois Curfman McInnes 
9679b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
9689b94acceSBarry Smith 
969a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
9709b94acceSBarry Smith @*/
97163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
9729b94acceSBarry Smith {
973dfbe8321SBarry Smith   PetscErrorCode ierr;
9749b94acceSBarry Smith 
9753a40ed3dSBarry Smith   PetscFunctionBegin;
9764482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9774482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
9784482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
979c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
980c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
981184914b5SBarry Smith 
982d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
983e7788613SBarry Smith   if (snes->ops->computefunction) {
984d64ed03dSBarry Smith     PetscStackPush("SNES user function");
985e9a2bbcdSBarry Smith     CHKMEMQ;
986e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
987e9a2bbcdSBarry Smith     CHKMEMQ;
988d64ed03dSBarry Smith     PetscStackPop;
989d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
99019717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
99119717074SBarry Smith     }
992d5e45103SBarry Smith     CHKERRQ(ierr);
99385385478SLisandro Dalcin   } else if (snes->vec_rhs) {
9941096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
9951096aae1SMatthew Knepley   } else {
9961096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
9971096aae1SMatthew Knepley   }
99885385478SLisandro Dalcin   if (snes->vec_rhs) {
99985385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
10003ab0aad5SBarry Smith   }
1001ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1002d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
10033a40ed3dSBarry Smith   PetscFunctionReturn(0);
10049b94acceSBarry Smith }
10059b94acceSBarry Smith 
10064a2ae208SSatish Balay #undef __FUNCT__
10074a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
100862fef451SLois Curfman McInnes /*@
100962fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
101062fef451SLois Curfman McInnes    set with SNESSetJacobian().
101162fef451SLois Curfman McInnes 
1012c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1013c7afd0dbSLois Curfman McInnes 
101462fef451SLois Curfman McInnes    Input Parameters:
1015c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1016c7afd0dbSLois Curfman McInnes -  x - input vector
101762fef451SLois Curfman McInnes 
101862fef451SLois Curfman McInnes    Output Parameters:
1019c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
102062fef451SLois Curfman McInnes .  B - optional preconditioning matrix
10212b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1022fee21e36SBarry Smith 
102362fef451SLois Curfman McInnes    Notes:
102462fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
102562fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
102662fef451SLois Curfman McInnes 
102794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1028dc5a77f8SLois Curfman McInnes    flag parameter.
102962fef451SLois Curfman McInnes 
103036851e7fSLois Curfman McInnes    Level: developer
103136851e7fSLois Curfman McInnes 
103262fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
103362fef451SLois Curfman McInnes 
10342b668275SBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure
103562fef451SLois Curfman McInnes @*/
103663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10379b94acceSBarry Smith {
1038dfbe8321SBarry Smith   PetscErrorCode ierr;
10393a40ed3dSBarry Smith 
10403a40ed3dSBarry Smith   PetscFunctionBegin;
10414482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10424482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
10434482741eSBarry Smith   PetscValidPointer(flg,5);
1044c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1045e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1046d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1047c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1048d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1049dc67937bSBarry Smith   CHKMEMQ;
1050e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1051dc67937bSBarry Smith   CHKMEMQ;
1052d64ed03dSBarry Smith   PetscStackPop;
1053d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
10546d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
10556ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
10566ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
10573a40ed3dSBarry Smith   PetscFunctionReturn(0);
10589b94acceSBarry Smith }
10599b94acceSBarry Smith 
10604a2ae208SSatish Balay #undef __FUNCT__
10614a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
10629b94acceSBarry Smith /*@C
10639b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1064044dda88SLois Curfman McInnes    location to store the matrix.
10659b94acceSBarry Smith 
1066c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1067c7afd0dbSLois Curfman McInnes 
10689b94acceSBarry Smith    Input Parameters:
1069c7afd0dbSLois Curfman McInnes +  snes - the SNES context
10709b94acceSBarry Smith .  A - Jacobian matrix
10719b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
10729b94acceSBarry Smith .  func - Jacobian evaluation routine
1073c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
10742cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
10759b94acceSBarry Smith 
10769b94acceSBarry Smith    Calling sequence of func:
10778d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
10789b94acceSBarry Smith 
1079c7afd0dbSLois Curfman McInnes +  x - input vector
10809b94acceSBarry Smith .  A - Jacobian matrix
10819b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1082ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
10832b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1084c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
10859b94acceSBarry Smith 
10869b94acceSBarry Smith    Notes:
108794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
10882cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1089ac21db08SLois Curfman McInnes 
1090ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
10919b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
10929b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
10939b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
10949b94acceSBarry Smith    throughout the global iterations.
10959b94acceSBarry Smith 
1096*16913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
1097*16913363SBarry Smith    each matrix.
1098*16913363SBarry Smith 
109936851e7fSLois Curfman McInnes    Level: beginner
110036851e7fSLois Curfman McInnes 
11019b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11029b94acceSBarry Smith 
11033ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
11049b94acceSBarry Smith @*/
110563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
11069b94acceSBarry Smith {
1107dfbe8321SBarry Smith   PetscErrorCode ierr;
11083a7fca6bSBarry Smith 
11093a40ed3dSBarry Smith   PetscFunctionBegin;
11104482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11114482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
11124482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1113c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1114c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
1115e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
11163a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
11173a7fca6bSBarry Smith   if (A) {
11187dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
11193a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
11209b94acceSBarry Smith     snes->jacobian = A;
11213a7fca6bSBarry Smith   }
11223a7fca6bSBarry Smith   if (B) {
11237dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
11243a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
11259b94acceSBarry Smith     snes->jacobian_pre = B;
11263a7fca6bSBarry Smith   }
11273a40ed3dSBarry Smith   PetscFunctionReturn(0);
11289b94acceSBarry Smith }
112962fef451SLois Curfman McInnes 
11304a2ae208SSatish Balay #undef __FUNCT__
11314a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1132c2aafc4cSSatish Balay /*@C
1133b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1134b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1135b4fd4287SBarry Smith 
1136c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1137c7afd0dbSLois Curfman McInnes 
1138b4fd4287SBarry Smith    Input Parameter:
1139b4fd4287SBarry Smith .  snes - the nonlinear solver context
1140b4fd4287SBarry Smith 
1141b4fd4287SBarry Smith    Output Parameters:
1142c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1143b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
114470e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
114570e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1146fee21e36SBarry Smith 
114736851e7fSLois Curfman McInnes    Level: advanced
114836851e7fSLois Curfman McInnes 
1149b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1150b4fd4287SBarry Smith @*/
115170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1152b4fd4287SBarry Smith {
11533a40ed3dSBarry Smith   PetscFunctionBegin;
11544482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1155b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1156b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1157e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
115870e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
11593a40ed3dSBarry Smith   PetscFunctionReturn(0);
1160b4fd4287SBarry Smith }
1161b4fd4287SBarry Smith 
11629b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
116363dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
11649b94acceSBarry Smith 
11654a2ae208SSatish Balay #undef __FUNCT__
11664a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
11679b94acceSBarry Smith /*@
11689b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1169272ac6f2SLois Curfman McInnes    of a nonlinear solver.
11709b94acceSBarry Smith 
1171fee21e36SBarry Smith    Collective on SNES
1172fee21e36SBarry Smith 
1173c7afd0dbSLois Curfman McInnes    Input Parameters:
117470e92668SMatthew Knepley .  snes - the SNES context
1175c7afd0dbSLois Curfman McInnes 
1176272ac6f2SLois Curfman McInnes    Notes:
1177272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1178272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1179272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1180272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1181272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1182272ac6f2SLois Curfman McInnes 
118336851e7fSLois Curfman McInnes    Level: advanced
118436851e7fSLois Curfman McInnes 
11859b94acceSBarry Smith .keywords: SNES, nonlinear, setup
11869b94acceSBarry Smith 
11879b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
11889b94acceSBarry Smith @*/
118970e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
11909b94acceSBarry Smith {
1191dfbe8321SBarry Smith   PetscErrorCode ierr;
119271f87433Sdalcinl   PetscTruth     flg;
11933a40ed3dSBarry Smith 
11943a40ed3dSBarry Smith   PetscFunctionBegin;
11954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11964dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
11979b94acceSBarry Smith 
11987adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
119985385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
120085385478SLisandro Dalcin   }
120185385478SLisandro Dalcin 
12027adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1203c1f60f51SBarry Smith   /*
1204c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1205dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1206c1f60f51SBarry Smith   */
1207c1f60f51SBarry Smith   if (flg) {
1208c1f60f51SBarry Smith     Mat J;
1209fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12103ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
1211ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12123a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
12133a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1214c1f60f51SBarry Smith   }
121545fc7adcSBarry Smith 
121603c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
12177adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
121845fc7adcSBarry Smith   if (flg) {
121945fc7adcSBarry Smith     Mat J;
122045fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
122175396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr);
122245fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
122345fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
122445fc7adcSBarry Smith   }
122532a4b47aSMatthew Knepley #endif
122645fc7adcSBarry Smith 
12277adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1228c1f60f51SBarry Smith   /*
1229dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1230c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1231c1f60f51SBarry Smith    */
123231615d04SLois Curfman McInnes   if (flg) {
1233272ac6f2SLois Curfman McInnes     Mat  J;
1234b5d62d44SBarry Smith     KSP ksp;
123594b7f48cSBarry Smith     PC   pc;
123675396ef9SLisandro Dalcin     /* create and set matrix-free operator */
1237fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12383ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
123975396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12403ec795f1SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
12413a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1242f3ef73ceSBarry Smith     /* force no preconditioner */
124394b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1244b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1245a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1246a9815358SBarry Smith     if (!flg) {
124775396ef9SLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
1248f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1249272ac6f2SLois Curfman McInnes     }
1250a9815358SBarry Smith   }
1251f3ef73ceSBarry Smith 
125285385478SLisandro Dalcin   if (!snes->vec_func && !snes->vec_rhs) {
12531096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12541096aae1SMatthew Knepley   }
125585385478SLisandro Dalcin   if (!snes->ops->computefunction && !snes->vec_rhs) {
12561096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12571096aae1SMatthew Knepley   }
125875396ef9SLisandro Dalcin   if (!snes->jacobian) {
125975396ef9SLisandro Dalcin     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
126075396ef9SLisandro Dalcin   }
1261a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
126229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1263a8c6a408SBarry Smith   }
1264a703fe33SLois Curfman McInnes 
1265410397dcSLisandro Dalcin   if (snes->ops->setup) {
1266410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1267410397dcSLisandro Dalcin   }
12687aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
12693a40ed3dSBarry Smith   PetscFunctionReturn(0);
12709b94acceSBarry Smith }
12719b94acceSBarry Smith 
12724a2ae208SSatish Balay #undef __FUNCT__
12734a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
127452baeb72SSatish Balay /*@
12759b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
12769b94acceSBarry Smith    with SNESCreate().
12779b94acceSBarry Smith 
1278c7afd0dbSLois Curfman McInnes    Collective on SNES
1279c7afd0dbSLois Curfman McInnes 
12809b94acceSBarry Smith    Input Parameter:
12819b94acceSBarry Smith .  snes - the SNES context
12829b94acceSBarry Smith 
128336851e7fSLois Curfman McInnes    Level: beginner
128436851e7fSLois Curfman McInnes 
12859b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
12869b94acceSBarry Smith 
128763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
12889b94acceSBarry Smith @*/
128963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
12909b94acceSBarry Smith {
12916849ba73SBarry Smith   PetscErrorCode ierr;
12923a40ed3dSBarry Smith 
12933a40ed3dSBarry Smith   PetscFunctionBegin;
12944482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12957adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1296d4bb536fSBarry Smith 
1297be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12980f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1299e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
130085385478SLisandro Dalcin 
130185385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
130285385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
130385385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
13043a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13053a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
130694b7f48cSBarry Smith   ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);
130785385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1308522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1309a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
13107f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1311a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
13123a40ed3dSBarry Smith   PetscFunctionReturn(0);
13139b94acceSBarry Smith }
13149b94acceSBarry Smith 
13159b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
13169b94acceSBarry Smith 
13174a2ae208SSatish Balay #undef __FUNCT__
13184a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
13199b94acceSBarry Smith /*@
1320d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
13219b94acceSBarry Smith 
1322c7afd0dbSLois Curfman McInnes    Collective on SNES
1323c7afd0dbSLois Curfman McInnes 
13249b94acceSBarry Smith    Input Parameters:
1325c7afd0dbSLois Curfman McInnes +  snes - the SNES context
132670441072SBarry Smith .  abstol - absolute convergence tolerance
132733174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
132833174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
132933174efeSLois Curfman McInnes            of the change in the solution between steps
133033174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1331c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1332fee21e36SBarry Smith 
133333174efeSLois Curfman McInnes    Options Database Keys:
133470441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1335c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1336c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1337c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1338c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
13399b94acceSBarry Smith 
1340d7a720efSLois Curfman McInnes    Notes:
13419b94acceSBarry Smith    The default maximum number of iterations is 50.
13429b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
13439b94acceSBarry Smith 
134436851e7fSLois Curfman McInnes    Level: intermediate
134536851e7fSLois Curfman McInnes 
134633174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
13479b94acceSBarry Smith 
13482492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
13499b94acceSBarry Smith @*/
135063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
13519b94acceSBarry Smith {
13523a40ed3dSBarry Smith   PetscFunctionBegin;
13534482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
135470441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1355d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1356d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1357d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1358d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
13593a40ed3dSBarry Smith   PetscFunctionReturn(0);
13609b94acceSBarry Smith }
13619b94acceSBarry Smith 
13624a2ae208SSatish Balay #undef __FUNCT__
13634a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
13649b94acceSBarry Smith /*@
136533174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
136633174efeSLois Curfman McInnes 
1367c7afd0dbSLois Curfman McInnes    Not Collective
1368c7afd0dbSLois Curfman McInnes 
136933174efeSLois Curfman McInnes    Input Parameters:
1370c7afd0dbSLois Curfman McInnes +  snes - the SNES context
137185385478SLisandro Dalcin .  atol - absolute convergence tolerance
137233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
137333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
137433174efeSLois Curfman McInnes            of the change in the solution between steps
137533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1376c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1377fee21e36SBarry Smith 
137833174efeSLois Curfman McInnes    Notes:
137933174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
138033174efeSLois Curfman McInnes 
138136851e7fSLois Curfman McInnes    Level: intermediate
138236851e7fSLois Curfman McInnes 
138333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
138433174efeSLois Curfman McInnes 
138533174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
138633174efeSLois Curfman McInnes @*/
138785385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
138833174efeSLois Curfman McInnes {
13893a40ed3dSBarry Smith   PetscFunctionBegin;
13904482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
139185385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
139233174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
139333174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
139433174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
139533174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
13963a40ed3dSBarry Smith   PetscFunctionReturn(0);
139733174efeSLois Curfman McInnes }
139833174efeSLois Curfman McInnes 
13994a2ae208SSatish Balay #undef __FUNCT__
14004a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
140133174efeSLois Curfman McInnes /*@
14029b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
14039b94acceSBarry Smith 
1404fee21e36SBarry Smith    Collective on SNES
1405fee21e36SBarry Smith 
1406c7afd0dbSLois Curfman McInnes    Input Parameters:
1407c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1408c7afd0dbSLois Curfman McInnes -  tol - tolerance
1409c7afd0dbSLois Curfman McInnes 
14109b94acceSBarry Smith    Options Database Key:
1411c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
14129b94acceSBarry Smith 
141336851e7fSLois Curfman McInnes    Level: intermediate
141436851e7fSLois Curfman McInnes 
14159b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
14169b94acceSBarry Smith 
14172492ecdbSBarry Smith .seealso: SNESSetTolerances()
14189b94acceSBarry Smith @*/
141963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
14209b94acceSBarry Smith {
14213a40ed3dSBarry Smith   PetscFunctionBegin;
14224482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
14239b94acceSBarry Smith   snes->deltatol = tol;
14243a40ed3dSBarry Smith   PetscFunctionReturn(0);
14259b94acceSBarry Smith }
14269b94acceSBarry Smith 
1427df9fa365SBarry Smith /*
1428df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1429df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1430df9fa365SBarry Smith    macros instead of functions
1431df9fa365SBarry Smith */
14324a2ae208SSatish Balay #undef __FUNCT__
1433a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1434a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1435ce1608b8SBarry Smith {
1436dfbe8321SBarry Smith   PetscErrorCode ierr;
1437ce1608b8SBarry Smith 
1438ce1608b8SBarry Smith   PetscFunctionBegin;
14394482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1440a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1441ce1608b8SBarry Smith   PetscFunctionReturn(0);
1442ce1608b8SBarry Smith }
1443ce1608b8SBarry Smith 
14444a2ae208SSatish Balay #undef __FUNCT__
1445a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1446a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1447df9fa365SBarry Smith {
1448dfbe8321SBarry Smith   PetscErrorCode ierr;
1449df9fa365SBarry Smith 
1450df9fa365SBarry Smith   PetscFunctionBegin;
1451a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1452df9fa365SBarry Smith   PetscFunctionReturn(0);
1453df9fa365SBarry Smith }
1454df9fa365SBarry Smith 
14554a2ae208SSatish Balay #undef __FUNCT__
1456a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1457a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1458df9fa365SBarry Smith {
1459dfbe8321SBarry Smith   PetscErrorCode ierr;
1460df9fa365SBarry Smith 
1461df9fa365SBarry Smith   PetscFunctionBegin;
1462a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1463df9fa365SBarry Smith   PetscFunctionReturn(0);
1464df9fa365SBarry Smith }
1465df9fa365SBarry Smith 
14669b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
14679b94acceSBarry Smith 
14684a2ae208SSatish Balay #undef __FUNCT__
1469a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
14709b94acceSBarry Smith /*@C
1471a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
14729b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
14739b94acceSBarry Smith    progress.
14749b94acceSBarry Smith 
1475fee21e36SBarry Smith    Collective on SNES
1476fee21e36SBarry Smith 
1477c7afd0dbSLois Curfman McInnes    Input Parameters:
1478c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1479c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1480b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1481e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1482b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1483b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
14849b94acceSBarry Smith 
1485c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1486a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1487c7afd0dbSLois Curfman McInnes 
1488c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1489c7afd0dbSLois Curfman McInnes .    its - iteration number
1490c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
149140a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
14929b94acceSBarry Smith 
14939665c990SLois Curfman McInnes    Options Database Keys:
1494a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1495a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1496a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1497a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1498c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1499a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1500c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1501c7afd0dbSLois Curfman McInnes                             the options database.
15029665c990SLois Curfman McInnes 
1503639f9d9dSBarry Smith    Notes:
15046bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1505a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
15066bc08f3fSLois Curfman McInnes    order in which they were set.
1507639f9d9dSBarry Smith 
1508025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1509025f1a04SBarry Smith 
151036851e7fSLois Curfman McInnes    Level: intermediate
151136851e7fSLois Curfman McInnes 
15129b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
15139b94acceSBarry Smith 
1514a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
15159b94acceSBarry Smith @*/
1516b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
15179b94acceSBarry Smith {
1518b90d0a6eSBarry Smith   PetscInt i;
1519b90d0a6eSBarry Smith 
15203a40ed3dSBarry Smith   PetscFunctionBegin;
15214482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1522639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
152329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1524639f9d9dSBarry Smith   }
1525b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1526b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1527b90d0a6eSBarry Smith 
1528b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1529b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1530b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1531b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1532b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1533b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1534b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1535b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1536b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1537b90d0a6eSBarry Smith         }
1538b90d0a6eSBarry Smith       }
1539b90d0a6eSBarry Smith     }
1540b90d0a6eSBarry Smith   }
1541b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1542b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1543639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
15443a40ed3dSBarry Smith   PetscFunctionReturn(0);
15459b94acceSBarry Smith }
15469b94acceSBarry Smith 
15474a2ae208SSatish Balay #undef __FUNCT__
1548a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
15495cd90555SBarry Smith /*@C
1550a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
15515cd90555SBarry Smith 
1552c7afd0dbSLois Curfman McInnes    Collective on SNES
1553c7afd0dbSLois Curfman McInnes 
15545cd90555SBarry Smith    Input Parameters:
15555cd90555SBarry Smith .  snes - the SNES context
15565cd90555SBarry Smith 
15571a480d89SAdministrator    Options Database Key:
1558a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1559a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1560c7afd0dbSLois Curfman McInnes     set via the options database
15615cd90555SBarry Smith 
15625cd90555SBarry Smith    Notes:
15635cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
15645cd90555SBarry Smith 
156536851e7fSLois Curfman McInnes    Level: intermediate
156636851e7fSLois Curfman McInnes 
15675cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
15685cd90555SBarry Smith 
1569a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
15705cd90555SBarry Smith @*/
1571a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
15725cd90555SBarry Smith {
1573d952e501SBarry Smith   PetscErrorCode ierr;
1574d952e501SBarry Smith   PetscInt       i;
1575d952e501SBarry Smith 
15765cd90555SBarry Smith   PetscFunctionBegin;
15774482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1578d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1579d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1580d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1581d952e501SBarry Smith     }
1582d952e501SBarry Smith   }
15835cd90555SBarry Smith   snes->numbermonitors = 0;
15845cd90555SBarry Smith   PetscFunctionReturn(0);
15855cd90555SBarry Smith }
15865cd90555SBarry Smith 
15874a2ae208SSatish Balay #undef __FUNCT__
15884a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
15899b94acceSBarry Smith /*@C
15909b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
15919b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
15929b94acceSBarry Smith 
1593fee21e36SBarry Smith    Collective on SNES
1594fee21e36SBarry Smith 
1595c7afd0dbSLois Curfman McInnes    Input Parameters:
1596c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1597c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
15987f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
15997f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
16009b94acceSBarry Smith 
1601c7afd0dbSLois Curfman McInnes    Calling sequence of func:
160206ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1603c7afd0dbSLois Curfman McInnes 
1604c7afd0dbSLois Curfman McInnes +    snes - the SNES context
160506ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1606c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1607184914b5SBarry Smith .    reason - reason for convergence/divergence
1608c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
16094b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
16104b27c08aSLois Curfman McInnes -    f - 2-norm of function
16119b94acceSBarry Smith 
161236851e7fSLois Curfman McInnes    Level: advanced
161336851e7fSLois Curfman McInnes 
16149b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
16159b94acceSBarry Smith 
161685385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
16179b94acceSBarry Smith @*/
16187f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
16199b94acceSBarry Smith {
16207f7931b9SBarry Smith   PetscErrorCode ierr;
16217f7931b9SBarry Smith 
16223a40ed3dSBarry Smith   PetscFunctionBegin;
16234482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
162485385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
16257f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
16267f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
16277f7931b9SBarry Smith   }
162885385478SLisandro Dalcin   snes->ops->converged        = func;
16297f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
163085385478SLisandro Dalcin   snes->cnvP                  = cctx;
16313a40ed3dSBarry Smith   PetscFunctionReturn(0);
16329b94acceSBarry Smith }
16339b94acceSBarry Smith 
16344a2ae208SSatish Balay #undef __FUNCT__
16354a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
163652baeb72SSatish Balay /*@
1637184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1638184914b5SBarry Smith 
1639184914b5SBarry Smith    Not Collective
1640184914b5SBarry Smith 
1641184914b5SBarry Smith    Input Parameter:
1642184914b5SBarry Smith .  snes - the SNES context
1643184914b5SBarry Smith 
1644184914b5SBarry Smith    Output Parameter:
1645e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1646184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1647184914b5SBarry Smith 
1648184914b5SBarry Smith    Level: intermediate
1649184914b5SBarry Smith 
1650184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1651184914b5SBarry Smith 
1652184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1653184914b5SBarry Smith 
165485385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
1655184914b5SBarry Smith @*/
165663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1657184914b5SBarry Smith {
1658184914b5SBarry Smith   PetscFunctionBegin;
16594482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16604482741eSBarry Smith   PetscValidPointer(reason,2);
1661184914b5SBarry Smith   *reason = snes->reason;
1662184914b5SBarry Smith   PetscFunctionReturn(0);
1663184914b5SBarry Smith }
1664184914b5SBarry Smith 
16654a2ae208SSatish Balay #undef __FUNCT__
16664a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1667c9005455SLois Curfman McInnes /*@
1668c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1669c9005455SLois Curfman McInnes 
1670fee21e36SBarry Smith    Collective on SNES
1671fee21e36SBarry Smith 
1672c7afd0dbSLois Curfman McInnes    Input Parameters:
1673c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1674c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1675cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1676758f92a0SBarry Smith .  na  - size of a and its
167764731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1678758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1679c7afd0dbSLois Curfman McInnes 
1680c9005455SLois Curfman McInnes    Notes:
16814b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1682c9005455SLois Curfman McInnes    at each step.
1683c9005455SLois Curfman McInnes 
1684c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1685c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1686c9005455SLois Curfman McInnes    during the section of code that is being timed.
1687c9005455SLois Curfman McInnes 
168836851e7fSLois Curfman McInnes    Level: intermediate
168936851e7fSLois Curfman McInnes 
1690c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1691758f92a0SBarry Smith 
169208405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1693758f92a0SBarry Smith 
1694c9005455SLois Curfman McInnes @*/
1695a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
1696c9005455SLois Curfman McInnes {
16973a40ed3dSBarry Smith   PetscFunctionBegin;
16984482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16994482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
1700a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
1701c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1702758f92a0SBarry Smith   snes->conv_hist_its   = its;
1703758f92a0SBarry Smith   snes->conv_hist_max   = na;
1704758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1705758f92a0SBarry Smith   PetscFunctionReturn(0);
1706758f92a0SBarry Smith }
1707758f92a0SBarry Smith 
17084a2ae208SSatish Balay #undef __FUNCT__
17094a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
17100c4c9dddSBarry Smith /*@C
1711758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1712758f92a0SBarry Smith 
1713758f92a0SBarry Smith    Collective on SNES
1714758f92a0SBarry Smith 
1715758f92a0SBarry Smith    Input Parameter:
1716758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1717758f92a0SBarry Smith 
1718758f92a0SBarry Smith    Output Parameters:
1719758f92a0SBarry Smith .  a   - array to hold history
1720758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1721758f92a0SBarry Smith          negative if not converged) for each solve.
1722758f92a0SBarry Smith -  na  - size of a and its
1723758f92a0SBarry Smith 
1724758f92a0SBarry Smith    Notes:
1725758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1726758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1727758f92a0SBarry Smith 
1728758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1729758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1730758f92a0SBarry Smith    during the section of code that is being timed.
1731758f92a0SBarry Smith 
1732758f92a0SBarry Smith    Level: intermediate
1733758f92a0SBarry Smith 
1734758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1735758f92a0SBarry Smith 
1736758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1737758f92a0SBarry Smith 
1738758f92a0SBarry Smith @*/
173963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
1740758f92a0SBarry Smith {
1741758f92a0SBarry Smith   PetscFunctionBegin;
17424482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1743758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1744758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1745758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
17463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1747c9005455SLois Curfman McInnes }
1748c9005455SLois Curfman McInnes 
1749e74ef692SMatthew Knepley #undef __FUNCT__
1750e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1751ac226902SBarry Smith /*@C
175276b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
17537e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
17547e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
175576b2cf59SMatthew Knepley 
175676b2cf59SMatthew Knepley   Collective on SNES
175776b2cf59SMatthew Knepley 
175876b2cf59SMatthew Knepley   Input Parameters:
175976b2cf59SMatthew Knepley . snes - The nonlinear solver context
176076b2cf59SMatthew Knepley . func - The function
176176b2cf59SMatthew Knepley 
176276b2cf59SMatthew Knepley   Calling sequence of func:
1763b5d30489SBarry Smith . func (SNES snes, PetscInt step);
176476b2cf59SMatthew Knepley 
176576b2cf59SMatthew Knepley . step - The current step of the iteration
176676b2cf59SMatthew Knepley 
176776b2cf59SMatthew Knepley   Level: intermediate
176876b2cf59SMatthew Knepley 
176976b2cf59SMatthew Knepley .keywords: SNES, update
1770b5d30489SBarry Smith 
177185385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
177276b2cf59SMatthew Knepley @*/
177363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
177476b2cf59SMatthew Knepley {
177576b2cf59SMatthew Knepley   PetscFunctionBegin;
17764482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
1777e7788613SBarry Smith   snes->ops->update = func;
177876b2cf59SMatthew Knepley   PetscFunctionReturn(0);
177976b2cf59SMatthew Knepley }
178076b2cf59SMatthew Knepley 
1781e74ef692SMatthew Knepley #undef __FUNCT__
1782e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
178376b2cf59SMatthew Knepley /*@
178476b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
178576b2cf59SMatthew Knepley 
178676b2cf59SMatthew Knepley   Not collective
178776b2cf59SMatthew Knepley 
178876b2cf59SMatthew Knepley   Input Parameters:
178976b2cf59SMatthew Knepley . snes - The nonlinear solver context
179076b2cf59SMatthew Knepley . step - The current step of the iteration
179176b2cf59SMatthew Knepley 
1792205452f4SMatthew Knepley   Level: intermediate
1793205452f4SMatthew Knepley 
179476b2cf59SMatthew Knepley .keywords: SNES, update
1795a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
179676b2cf59SMatthew Knepley @*/
179763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
179876b2cf59SMatthew Knepley {
179976b2cf59SMatthew Knepley   PetscFunctionBegin;
180076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
180176b2cf59SMatthew Knepley }
180276b2cf59SMatthew Knepley 
18034a2ae208SSatish Balay #undef __FUNCT__
18044a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
18059b94acceSBarry Smith /*
18069b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
18079b94acceSBarry Smith    positive parameter delta.
18089b94acceSBarry Smith 
18099b94acceSBarry Smith     Input Parameters:
1810c7afd0dbSLois Curfman McInnes +   snes - the SNES context
18119b94acceSBarry Smith .   y - approximate solution of linear system
18129b94acceSBarry Smith .   fnorm - 2-norm of current function
1813c7afd0dbSLois Curfman McInnes -   delta - trust region size
18149b94acceSBarry Smith 
18159b94acceSBarry Smith     Output Parameters:
1816c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
18179b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
18189b94acceSBarry Smith     region, and exceeds zero otherwise.
1819c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
18209b94acceSBarry Smith 
18219b94acceSBarry Smith     Note:
18224b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
18239b94acceSBarry Smith     is set to be the maximum allowable step size.
18249b94acceSBarry Smith 
18259b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
18269b94acceSBarry Smith */
1827dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
18289b94acceSBarry Smith {
1829064f8208SBarry Smith   PetscReal      nrm;
1830ea709b57SSatish Balay   PetscScalar    cnorm;
1831dfbe8321SBarry Smith   PetscErrorCode ierr;
18323a40ed3dSBarry Smith 
18333a40ed3dSBarry Smith   PetscFunctionBegin;
18344482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18354482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
1836c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
1837184914b5SBarry Smith 
1838064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
1839064f8208SBarry Smith   if (nrm > *delta) {
1840064f8208SBarry Smith      nrm = *delta/nrm;
1841064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
1842064f8208SBarry Smith      cnorm = nrm;
18432dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
18449b94acceSBarry Smith      *ynorm = *delta;
18459b94acceSBarry Smith   } else {
18469b94acceSBarry Smith      *gpnorm = 0.0;
1847064f8208SBarry Smith      *ynorm = nrm;
18489b94acceSBarry Smith   }
18493a40ed3dSBarry Smith   PetscFunctionReturn(0);
18509b94acceSBarry Smith }
18519b94acceSBarry Smith 
18524a2ae208SSatish Balay #undef __FUNCT__
18534a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
18546ce558aeSBarry Smith /*@C
1855f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
1856f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
18579b94acceSBarry Smith 
1858c7afd0dbSLois Curfman McInnes    Collective on SNES
1859c7afd0dbSLois Curfman McInnes 
1860b2002411SLois Curfman McInnes    Input Parameters:
1861c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1862f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
186385385478SLisandro Dalcin -  x - the solution vector.
18649b94acceSBarry Smith 
1865b2002411SLois Curfman McInnes    Notes:
18668ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
18678ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
18688ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
18698ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
18708ddd3da0SLois Curfman McInnes 
187136851e7fSLois Curfman McInnes    Level: beginner
187236851e7fSLois Curfman McInnes 
18739b94acceSBarry Smith .keywords: SNES, nonlinear, solve
18749b94acceSBarry Smith 
187585385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
18769b94acceSBarry Smith @*/
1877f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
18789b94acceSBarry Smith {
1879dfbe8321SBarry Smith   PetscErrorCode ierr;
1880f1af5d2fSBarry Smith   PetscTruth     flg;
1881eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
1882eabae89aSBarry Smith   PetscViewer    viewer;
1883052efed2SBarry Smith 
18843a40ed3dSBarry Smith   PetscFunctionBegin;
18854482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1886f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
1887f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
188885385478SLisandro Dalcin   if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2);
188985385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
189085385478SLisandro Dalcin 
189185385478SLisandro Dalcin   /* set solution vector */
189285385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
189385385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
189485385478SLisandro Dalcin   snes->vec_sol = x;
189585385478SLisandro Dalcin   /* set afine vector if provided */
189685385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
189785385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
189885385478SLisandro Dalcin   snes->vec_rhs = b;
189985385478SLisandro Dalcin 
190085385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
190185385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr);
190270e92668SMatthew Knepley   }
19033f149594SLisandro Dalcin 
190470e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
19053f149594SLisandro Dalcin 
1906abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
190750ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
1908d5e45103SBarry Smith 
19093f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
19104936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
191185385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
19124936397dSBarry Smith   if (snes->domainerror){
19134936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
19144936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
19154936397dSBarry Smith   }
191685385478SLisandro Dalcin 
19173f149594SLisandro Dalcin   if (!snes->reason) {
19183f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
19193f149594SLisandro Dalcin   }
19203f149594SLisandro Dalcin 
19217adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1922eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
19237adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
1924eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
1925eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
1926eabae89aSBarry Smith   }
1927eabae89aSBarry Smith 
19287adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
1929da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
19305968eb51SBarry Smith   if (snes->printreason) {
19315968eb51SBarry Smith     if (snes->reason > 0) {
19327adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
19335968eb51SBarry Smith     } else {
19347adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
19355968eb51SBarry Smith     }
19365968eb51SBarry Smith   }
19375968eb51SBarry Smith 
19383a40ed3dSBarry Smith   PetscFunctionReturn(0);
19399b94acceSBarry Smith }
19409b94acceSBarry Smith 
19419b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
19429b94acceSBarry Smith 
19434a2ae208SSatish Balay #undef __FUNCT__
19444a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
194582bf6240SBarry Smith /*@C
19464b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
19479b94acceSBarry Smith 
1948fee21e36SBarry Smith    Collective on SNES
1949fee21e36SBarry Smith 
1950c7afd0dbSLois Curfman McInnes    Input Parameters:
1951c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1952454a90a3SBarry Smith -  type - a known method
1953c7afd0dbSLois Curfman McInnes 
1954c7afd0dbSLois Curfman McInnes    Options Database Key:
1955454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1956c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1957ae12b187SLois Curfman McInnes 
19589b94acceSBarry Smith    Notes:
1959e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
19604b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
1961c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19624b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
1963c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19649b94acceSBarry Smith 
1965ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1966ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1967ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1968ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1969ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1970ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1971ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1972ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1973ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
1974b0a32e0cSBarry Smith   appropriate method.
197536851e7fSLois Curfman McInnes 
197636851e7fSLois Curfman McInnes   Level: intermediate
1977a703fe33SLois Curfman McInnes 
1978454a90a3SBarry Smith .keywords: SNES, set, type
1979435da068SBarry Smith 
1980435da068SBarry Smith .seealso: SNESType, SNESCreate()
1981435da068SBarry Smith 
19829b94acceSBarry Smith @*/
1983e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,SNESType type)
19849b94acceSBarry Smith {
1985dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
19866831982aSBarry Smith   PetscTruth     match;
19873a40ed3dSBarry Smith 
19883a40ed3dSBarry Smith   PetscFunctionBegin;
19894482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19904482741eSBarry Smith   PetscValidCharPointer(type,2);
199182bf6240SBarry Smith 
19926831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
19930f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
199492ff6ae8SBarry Smith 
19957adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
1996958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
199775396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
199875396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
199975396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
200075396ef9SLisandro Dalcin   snes->ops->setup          = 0;
200175396ef9SLisandro Dalcin   snes->ops->solve          = 0;
200275396ef9SLisandro Dalcin   snes->ops->view           = 0;
200375396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
200475396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
200575396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
200675396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
20073a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2008454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
20093a40ed3dSBarry Smith   PetscFunctionReturn(0);
20109b94acceSBarry Smith }
20119b94acceSBarry Smith 
2012a847f771SSatish Balay 
20139b94acceSBarry Smith /* --------------------------------------------------------------------- */
20144a2ae208SSatish Balay #undef __FUNCT__
20154a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
201652baeb72SSatish Balay /*@
20179b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2018f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
20199b94acceSBarry Smith 
2020fee21e36SBarry Smith    Not Collective
2021fee21e36SBarry Smith 
202236851e7fSLois Curfman McInnes    Level: advanced
202336851e7fSLois Curfman McInnes 
20249b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
20259b94acceSBarry Smith 
20269b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
20279b94acceSBarry Smith @*/
202863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
20299b94acceSBarry Smith {
2030dfbe8321SBarry Smith   PetscErrorCode ierr;
203182bf6240SBarry Smith 
20323a40ed3dSBarry Smith   PetscFunctionBegin;
20331441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
20344c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
20353a40ed3dSBarry Smith   PetscFunctionReturn(0);
20369b94acceSBarry Smith }
20379b94acceSBarry Smith 
20384a2ae208SSatish Balay #undef __FUNCT__
20394a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
20409b94acceSBarry Smith /*@C
20419a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
20429b94acceSBarry Smith 
2043c7afd0dbSLois Curfman McInnes    Not Collective
2044c7afd0dbSLois Curfman McInnes 
20459b94acceSBarry Smith    Input Parameter:
20464b0e389bSBarry Smith .  snes - nonlinear solver context
20479b94acceSBarry Smith 
20489b94acceSBarry Smith    Output Parameter:
20493a7fca6bSBarry Smith .  type - SNES method (a character string)
20509b94acceSBarry Smith 
205136851e7fSLois Curfman McInnes    Level: intermediate
205236851e7fSLois Curfman McInnes 
2053454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
20549b94acceSBarry Smith @*/
205563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,SNESType *type)
20569b94acceSBarry Smith {
20573a40ed3dSBarry Smith   PetscFunctionBegin;
20584482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20594482741eSBarry Smith   PetscValidPointer(type,2);
20607adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
20613a40ed3dSBarry Smith   PetscFunctionReturn(0);
20629b94acceSBarry Smith }
20639b94acceSBarry Smith 
20644a2ae208SSatish Balay #undef __FUNCT__
20654a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
206652baeb72SSatish Balay /*@
20679b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20689b94acceSBarry Smith    stored.
20699b94acceSBarry Smith 
2070c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2071c7afd0dbSLois Curfman McInnes 
20729b94acceSBarry Smith    Input Parameter:
20739b94acceSBarry Smith .  snes - the SNES context
20749b94acceSBarry Smith 
20759b94acceSBarry Smith    Output Parameter:
20769b94acceSBarry Smith .  x - the solution
20779b94acceSBarry Smith 
207870e92668SMatthew Knepley    Level: intermediate
207936851e7fSLois Curfman McInnes 
20809b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20819b94acceSBarry Smith 
208285385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
20839b94acceSBarry Smith @*/
208463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
20859b94acceSBarry Smith {
20863a40ed3dSBarry Smith   PetscFunctionBegin;
20874482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20884482741eSBarry Smith   PetscValidPointer(x,2);
208985385478SLisandro Dalcin   *x = snes->vec_sol;
209070e92668SMatthew Knepley   PetscFunctionReturn(0);
209170e92668SMatthew Knepley }
209270e92668SMatthew Knepley 
209370e92668SMatthew Knepley #undef __FUNCT__
20944a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
209552baeb72SSatish Balay /*@
20969b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
20979b94acceSBarry Smith    stored.
20989b94acceSBarry Smith 
2099c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2100c7afd0dbSLois Curfman McInnes 
21019b94acceSBarry Smith    Input Parameter:
21029b94acceSBarry Smith .  snes - the SNES context
21039b94acceSBarry Smith 
21049b94acceSBarry Smith    Output Parameter:
21059b94acceSBarry Smith .  x - the solution update
21069b94acceSBarry Smith 
210736851e7fSLois Curfman McInnes    Level: advanced
210836851e7fSLois Curfman McInnes 
21099b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
21109b94acceSBarry Smith 
211185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
21129b94acceSBarry Smith @*/
211363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
21149b94acceSBarry Smith {
21153a40ed3dSBarry Smith   PetscFunctionBegin;
21164482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21174482741eSBarry Smith   PetscValidPointer(x,2);
211885385478SLisandro Dalcin   *x = snes->vec_sol_update;
21193a40ed3dSBarry Smith   PetscFunctionReturn(0);
21209b94acceSBarry Smith }
21219b94acceSBarry Smith 
21224a2ae208SSatish Balay #undef __FUNCT__
21234a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
21249b94acceSBarry Smith /*@C
21253638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
21269b94acceSBarry Smith 
2127c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2128c7afd0dbSLois Curfman McInnes 
21299b94acceSBarry Smith    Input Parameter:
21309b94acceSBarry Smith .  snes - the SNES context
21319b94acceSBarry Smith 
21329b94acceSBarry Smith    Output Parameter:
21337bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
213470e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
213570e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
21369b94acceSBarry Smith 
213736851e7fSLois Curfman McInnes    Level: advanced
213836851e7fSLois Curfman McInnes 
2139a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
21409b94acceSBarry Smith 
21414b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
21429b94acceSBarry Smith @*/
214370e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
21449b94acceSBarry Smith {
21453a40ed3dSBarry Smith   PetscFunctionBegin;
21464482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
214785385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2148e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
214970e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
21503a40ed3dSBarry Smith   PetscFunctionReturn(0);
21519b94acceSBarry Smith }
21529b94acceSBarry Smith 
21534a2ae208SSatish Balay #undef __FUNCT__
21544a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
21553c7409f5SSatish Balay /*@C
21563c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2157d850072dSLois Curfman McInnes    SNES options in the database.
21583c7409f5SSatish Balay 
2159fee21e36SBarry Smith    Collective on SNES
2160fee21e36SBarry Smith 
2161c7afd0dbSLois Curfman McInnes    Input Parameter:
2162c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2163c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2164c7afd0dbSLois Curfman McInnes 
2165d850072dSLois Curfman McInnes    Notes:
2166a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2167c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2168d850072dSLois Curfman McInnes 
216936851e7fSLois Curfman McInnes    Level: advanced
217036851e7fSLois Curfman McInnes 
21713c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2172a86d99e1SLois Curfman McInnes 
2173a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
21743c7409f5SSatish Balay @*/
217563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
21763c7409f5SSatish Balay {
2177dfbe8321SBarry Smith   PetscErrorCode ierr;
21783c7409f5SSatish Balay 
21793a40ed3dSBarry Smith   PetscFunctionBegin;
21804482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2181639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
218294b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
21833a40ed3dSBarry Smith   PetscFunctionReturn(0);
21843c7409f5SSatish Balay }
21853c7409f5SSatish Balay 
21864a2ae208SSatish Balay #undef __FUNCT__
21874a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
21883c7409f5SSatish Balay /*@C
2189f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2190d850072dSLois Curfman McInnes    SNES options in the database.
21913c7409f5SSatish Balay 
2192fee21e36SBarry Smith    Collective on SNES
2193fee21e36SBarry Smith 
2194c7afd0dbSLois Curfman McInnes    Input Parameters:
2195c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2196c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2197c7afd0dbSLois Curfman McInnes 
2198d850072dSLois Curfman McInnes    Notes:
2199a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2200c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2201d850072dSLois Curfman McInnes 
220236851e7fSLois Curfman McInnes    Level: advanced
220336851e7fSLois Curfman McInnes 
22043c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2205a86d99e1SLois Curfman McInnes 
2206a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
22073c7409f5SSatish Balay @*/
220863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
22093c7409f5SSatish Balay {
2210dfbe8321SBarry Smith   PetscErrorCode ierr;
22113c7409f5SSatish Balay 
22123a40ed3dSBarry Smith   PetscFunctionBegin;
22134482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2214639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
221594b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
22163a40ed3dSBarry Smith   PetscFunctionReturn(0);
22173c7409f5SSatish Balay }
22183c7409f5SSatish Balay 
22194a2ae208SSatish Balay #undef __FUNCT__
22204a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
22219ab63eb5SSatish Balay /*@C
22223c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
22233c7409f5SSatish Balay    SNES options in the database.
22243c7409f5SSatish Balay 
2225c7afd0dbSLois Curfman McInnes    Not Collective
2226c7afd0dbSLois Curfman McInnes 
22273c7409f5SSatish Balay    Input Parameter:
22283c7409f5SSatish Balay .  snes - the SNES context
22293c7409f5SSatish Balay 
22303c7409f5SSatish Balay    Output Parameter:
22313c7409f5SSatish Balay .  prefix - pointer to the prefix string used
22323c7409f5SSatish Balay 
22339ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
22349ab63eb5SSatish Balay    sufficient length to hold the prefix.
22359ab63eb5SSatish Balay 
223636851e7fSLois Curfman McInnes    Level: advanced
223736851e7fSLois Curfman McInnes 
22383c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2239a86d99e1SLois Curfman McInnes 
2240a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
22413c7409f5SSatish Balay @*/
2242e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
22433c7409f5SSatish Balay {
2244dfbe8321SBarry Smith   PetscErrorCode ierr;
22453c7409f5SSatish Balay 
22463a40ed3dSBarry Smith   PetscFunctionBegin;
22474482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2248639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
22493a40ed3dSBarry Smith   PetscFunctionReturn(0);
22503c7409f5SSatish Balay }
22513c7409f5SSatish Balay 
2252b2002411SLois Curfman McInnes 
22534a2ae208SSatish Balay #undef __FUNCT__
22544a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
22553cea93caSBarry Smith /*@C
22563cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
22573cea93caSBarry Smith 
22587f6c08e0SMatthew Knepley   Level: advanced
22593cea93caSBarry Smith @*/
226063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2261b2002411SLois Curfman McInnes {
2262e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2263dfbe8321SBarry Smith   PetscErrorCode ierr;
2264b2002411SLois Curfman McInnes 
2265b2002411SLois Curfman McInnes   PetscFunctionBegin;
2266b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2267c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2268b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2269b2002411SLois Curfman McInnes }
2270da9b6338SBarry Smith 
2271da9b6338SBarry Smith #undef __FUNCT__
2272da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
227363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2274da9b6338SBarry Smith {
2275dfbe8321SBarry Smith   PetscErrorCode ierr;
227677431f27SBarry Smith   PetscInt       N,i,j;
2277da9b6338SBarry Smith   Vec            u,uh,fh;
2278da9b6338SBarry Smith   PetscScalar    value;
2279da9b6338SBarry Smith   PetscReal      norm;
2280da9b6338SBarry Smith 
2281da9b6338SBarry Smith   PetscFunctionBegin;
2282da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2283da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2284da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2285da9b6338SBarry Smith 
2286da9b6338SBarry Smith   /* currently only works for sequential */
2287da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2288da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2289da9b6338SBarry Smith   for (i=0; i<N; i++) {
2290da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
229177431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2292da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2293ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2294da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
22953ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2296da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
229777431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2298da9b6338SBarry Smith       value = -value;
2299da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2300da9b6338SBarry Smith     }
2301da9b6338SBarry Smith   }
2302da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2303da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2304da9b6338SBarry Smith   PetscFunctionReturn(0);
2305da9b6338SBarry Smith }
230671f87433Sdalcinl 
230771f87433Sdalcinl #undef __FUNCT__
2308fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
230971f87433Sdalcinl /*@
2310fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
231171f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
231271f87433Sdalcinl    Newton method.
231371f87433Sdalcinl 
231471f87433Sdalcinl    Collective on SNES
231571f87433Sdalcinl 
231671f87433Sdalcinl    Input Parameters:
231771f87433Sdalcinl +  snes - SNES context
231871f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
231971f87433Sdalcinl 
232071f87433Sdalcinl    Notes:
232171f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
232271f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
232371f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
232471f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
232571f87433Sdalcinl    solver.
232671f87433Sdalcinl 
232771f87433Sdalcinl    Level: advanced
232871f87433Sdalcinl 
232971f87433Sdalcinl    Reference:
233071f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
233171f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
233271f87433Sdalcinl 
233371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
233471f87433Sdalcinl 
2335fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
233671f87433Sdalcinl @*/
2337fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
233871f87433Sdalcinl {
233971f87433Sdalcinl   PetscFunctionBegin;
234071f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
234171f87433Sdalcinl   snes->ksp_ewconv = flag;
234271f87433Sdalcinl   PetscFunctionReturn(0);
234371f87433Sdalcinl }
234471f87433Sdalcinl 
234571f87433Sdalcinl #undef __FUNCT__
2346fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
234771f87433Sdalcinl /*@
2348fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
234971f87433Sdalcinl    for computing relative tolerance for linear solvers within an
235071f87433Sdalcinl    inexact Newton method.
235171f87433Sdalcinl 
235271f87433Sdalcinl    Not Collective
235371f87433Sdalcinl 
235471f87433Sdalcinl    Input Parameter:
235571f87433Sdalcinl .  snes - SNES context
235671f87433Sdalcinl 
235771f87433Sdalcinl    Output Parameter:
235871f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
235971f87433Sdalcinl 
236071f87433Sdalcinl    Notes:
236171f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
236271f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
236371f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
236471f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
236571f87433Sdalcinl    solver.
236671f87433Sdalcinl 
236771f87433Sdalcinl    Level: advanced
236871f87433Sdalcinl 
236971f87433Sdalcinl    Reference:
237071f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
237171f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
237271f87433Sdalcinl 
237371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
237471f87433Sdalcinl 
2375fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
237671f87433Sdalcinl @*/
2377fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
237871f87433Sdalcinl {
237971f87433Sdalcinl   PetscFunctionBegin;
238071f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
238171f87433Sdalcinl   PetscValidPointer(flag,2);
238271f87433Sdalcinl   *flag = snes->ksp_ewconv;
238371f87433Sdalcinl   PetscFunctionReturn(0);
238471f87433Sdalcinl }
238571f87433Sdalcinl 
238671f87433Sdalcinl #undef __FUNCT__
2387fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
238871f87433Sdalcinl /*@
2389fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
239071f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
239171f87433Sdalcinl    Newton method.
239271f87433Sdalcinl 
239371f87433Sdalcinl    Collective on SNES
239471f87433Sdalcinl 
239571f87433Sdalcinl    Input Parameters:
239671f87433Sdalcinl +    snes - SNES context
239771f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
239871f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
239971f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
240071f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
240171f87433Sdalcinl              (0 <= gamma2 <= 1)
240271f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
240371f87433Sdalcinl .    alpha2 - power for safeguard
240471f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
240571f87433Sdalcinl 
240671f87433Sdalcinl    Note:
240771f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
240871f87433Sdalcinl 
240971f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
241071f87433Sdalcinl 
241171f87433Sdalcinl    Level: advanced
241271f87433Sdalcinl 
241371f87433Sdalcinl    Reference:
241471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
241571f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
241671f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
241771f87433Sdalcinl 
241871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
241971f87433Sdalcinl 
2420fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
242171f87433Sdalcinl @*/
2422fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
242371f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
242471f87433Sdalcinl {
2425fa9f3622SBarry Smith   SNESKSPEW *kctx;
242671f87433Sdalcinl   PetscFunctionBegin;
242771f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2428fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
242971f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
243071f87433Sdalcinl 
243171f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
243271f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
243371f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
243471f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
243571f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
243671f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
243771f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
243871f87433Sdalcinl 
243971f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
244071f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
244171f87433Sdalcinl   }
244271f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
244371f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
244471f87433Sdalcinl   }
244571f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
244671f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
244771f87433Sdalcinl   }
244871f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
244971f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
245071f87433Sdalcinl   }
245171f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
245271f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
245371f87433Sdalcinl   }
245471f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
245571f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
245671f87433Sdalcinl   }
245771f87433Sdalcinl   PetscFunctionReturn(0);
245871f87433Sdalcinl }
245971f87433Sdalcinl 
246071f87433Sdalcinl #undef __FUNCT__
2461fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
246271f87433Sdalcinl /*@
2463fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
246471f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
246571f87433Sdalcinl    Newton method.
246671f87433Sdalcinl 
246771f87433Sdalcinl    Not Collective
246871f87433Sdalcinl 
246971f87433Sdalcinl    Input Parameters:
247071f87433Sdalcinl      snes - SNES context
247171f87433Sdalcinl 
247271f87433Sdalcinl    Output Parameters:
247371f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
247471f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
247571f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
247671f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
247771f87433Sdalcinl              (0 <= gamma2 <= 1)
247871f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
247971f87433Sdalcinl .    alpha2 - power for safeguard
248071f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
248171f87433Sdalcinl 
248271f87433Sdalcinl    Level: advanced
248371f87433Sdalcinl 
248471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
248571f87433Sdalcinl 
2486fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
248771f87433Sdalcinl @*/
2488fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
248971f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
249071f87433Sdalcinl {
2491fa9f3622SBarry Smith   SNESKSPEW *kctx;
249271f87433Sdalcinl   PetscFunctionBegin;
249371f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2494fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
249571f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
249671f87433Sdalcinl   if(version)   *version   = kctx->version;
249771f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
249871f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
249971f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
250071f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
250171f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
250271f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
250371f87433Sdalcinl   PetscFunctionReturn(0);
250471f87433Sdalcinl }
250571f87433Sdalcinl 
250671f87433Sdalcinl #undef __FUNCT__
2507fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2508fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
250971f87433Sdalcinl {
251071f87433Sdalcinl   PetscErrorCode ierr;
2511fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
251271f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
251371f87433Sdalcinl 
251471f87433Sdalcinl   PetscFunctionBegin;
251571f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
251671f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
251771f87433Sdalcinl     rtol = kctx->rtol_0;
251871f87433Sdalcinl   } else {
251971f87433Sdalcinl     if (kctx->version == 1) {
252071f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
252171f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
252271f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
252371f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
252471f87433Sdalcinl     } else if (kctx->version == 2) {
252571f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
252671f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
252771f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
252871f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
252971f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
253071f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
253171f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
253271f87433Sdalcinl       stol = PetscMax(rtol,stol);
253371f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
253471f87433Sdalcinl       /* safeguard: avoid oversolving */
253571f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
253671f87433Sdalcinl       stol = PetscMax(rtol,stol);
253771f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
253871f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
253971f87433Sdalcinl   }
254071f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
254171f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
254271f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
254371f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
254471f87433Sdalcinl   PetscFunctionReturn(0);
254571f87433Sdalcinl }
254671f87433Sdalcinl 
254771f87433Sdalcinl #undef __FUNCT__
2548fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2549fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
255071f87433Sdalcinl {
255171f87433Sdalcinl   PetscErrorCode ierr;
2552fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
255371f87433Sdalcinl   PCSide         pcside;
255471f87433Sdalcinl   Vec            lres;
255571f87433Sdalcinl 
255671f87433Sdalcinl   PetscFunctionBegin;
255771f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
255871f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
255971f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
256071f87433Sdalcinl   if (kctx->version == 1) {
256171f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
256271f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
256371f87433Sdalcinl       /* KSP residual is true linear residual */
256471f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
256571f87433Sdalcinl     } else {
256671f87433Sdalcinl       /* KSP residual is preconditioned residual */
256771f87433Sdalcinl       /* compute true linear residual norm */
256871f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
256971f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
257071f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
257171f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
257271f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
257371f87433Sdalcinl     }
257471f87433Sdalcinl   }
257571f87433Sdalcinl   PetscFunctionReturn(0);
257671f87433Sdalcinl }
257771f87433Sdalcinl 
257871f87433Sdalcinl #undef __FUNCT__
257971f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
258071f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
258171f87433Sdalcinl {
258271f87433Sdalcinl   PetscErrorCode ierr;
258371f87433Sdalcinl 
258471f87433Sdalcinl   PetscFunctionBegin;
2585fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
258671f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2587fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
258871f87433Sdalcinl   PetscFunctionReturn(0);
258971f87433Sdalcinl }
2590